Weapon Chains

 

Varient 2 of the multi weapon select code. Still doesn't work quite right as when you don't have any ammo for the first weapon in the chain it doesn't let you continue through the chain. (and when you reach the end of the chain, it stops as it has problems with the first.) I'm still working on it, but it's possibly a limitation of the way I've done it.


All the changes are made in a single function in G_cmds.c. the function is Cmd_Use_f Look through the code below. to add extra chains, you'll need to increase the size of the chains array. it should be fairly understandable.

void Cmd_Use_f (edict_t *ent)
{
        int                    index;
        gitem_t        *it;
        char           *s;
        
// begin multiweapon select variable declarations
        int i,j;
        char    *chains[2][8]= {
                               {"Blaster","Rocket Launcher","Machinegun","end of chain"},
                               {"Shotgun","Railgun","BFG10K","end of chain"}
                               };
        char           *current_weapon;
        int            ammo_index;
        gitem_t        *ammo_item;
// end multiweapon select variable declarations
        
 
        s = gi.args();
        it = FindItem (s);
 
 
// Begin multiweapon select. 
        i=0;
        j=0;
 
        for (i=0;i<2;i++)
               if (!Q_strcasecmp(s, chains[i][0]))
                       break;         // Chain start found
        if (i<2) // the start of a chain has been found. So progress through the chain.
        {
               
               current_weapon=ent->client->pers.weapon->pickup_name;
               for (j=0; Q_strcasecmp(chains[i][j],"end of chain");j++) // go through the chain till you reach the end
               if (!Q_strcasecmp(current_weapon,chains[i][j]))
               {
                       if (!Q_strcasecmp(chains[i][j],"end of chain"))
                               break;
                       it=FindItem (chains [i][j+1]);
                       if (!it)
                       {
                               it=FindItem (chains [i][0]);
                               break;
                       }
                       current_weapon=chains [i][j+1];
                       index = ITEM_INDEX(it);
                       ammo_item = FindItem(it->ammo);
                       ammo_index = ITEM_INDEX(ammo_item);
                       if ((ent->client->pers.inventory[index])&&(ent->client->pers.inventory[ammo_index]))
                       {
                               break;
                       }
               }
        }       
// End of multiweapon select
 
        if (!it)
        {
               gi.cprintf (ent, PRINT_HIGH, "unknown item: %s\n", s);
               return;
        }
        if (!it->use)
        {
               gi.cprintf (ent, PRINT_HIGH, "Item is not usable.\n");
               return;
        }
        index = ITEM_INDEX(it);
        if (!ent->client->pers.inventory[index])
        {
               gi.cprintf (ent, PRINT_HIGH, "Out of item: %s\n", s);
               return;
        }
 
        it->use (ent, it);
}

I'm still working on the code, but the stuff above will work (but not quite the way I wanted)


Skunkworks Tutorials