Assigning a Random Weapon at Respawn


 

Posted by Maj.Bitch (24.95.222.*) at 11:54 AM, 3/18/2001:

Here is a repost of an earlier tutorial. I wrote this because I thought it would be an interesting change to the gameplay if everybody started off with a randomly assigned weapon. It does add another dimension to the game because you never know what you're gonna get! Also, its an easy mod to make AND you can comment out different weapons in the Random_Weapon() function which you don't want assigned to player's at respawn (like the bfg). Or, change it to assign only certain weapons to certain teams. Play around with it or use it just like it is. Your choice.

Here it is:

<C&NBSP;CODE>
First, make sure you have these defines in your g_local.h file.  You may already have them
if not, then use these.


#define WEAP_BLASTER           1
#define WEAP_SHOTGUN           2
#define WEAP_SUPERSHOTGUN      3
#define WEAP_MACHINEGUN        4
#define WEAP_CHAINGUN          5
#define WEAP_GRENADES          6
#define WEAP_GRENADELAUNCHER   7
#define WEAP_ROCKETLAUNCHER    8
#define WEAP_HYPERBLASTER      9
#define WEAP_RAILGUN          10
#define WEAP_BFG              11

Next, Add these new functions to the bottom of your g_items.c file:

//========================================================
qboolean IsWeapon(gitem_t *item) {
  return (item && (item->flags & IT_WEAPON));
}

//========================================================
int FullAmmo(gitem_t *weapon) {

  if (!IsWeapon(weapon)) return 0;

  if (weapon==item_shotgun)         return 100;
  if (weapon==item_supershotgun)    return 100;
  if (weapon==item_machinegun)      return 200;
  if (weapon==item_chaingun)        return 200;
  if (weapon==item_handgrenade)     return 50;
  if (weapon==item_grenadelauncher) return 50;
  if (weapon==item_rocketlauncher)  return 50;
  if (weapon==item_railgun)         return 50;
  if (weapon==item_hyperblaster)    return 200;
  if (weapon==item_bfg)             return 200;

  return 0;
}

//========================================================
int AmmoIndexForWeapon(gitem_t *weapon) {

  if (weapon==item_shotgun)         return ITEM_INDEX(item_shells);
  if (weapon==item_supershotgun)    return ITEM_INDEX(item_shells);
  if (weapon==item_machinegun)      return ITEM_INDEX(item_bullets);
  if (weapon==item_chaingun)        return ITEM_INDEX(item_bullets);
  if (weapon==item_handgrenade)     return ITEM_INDEX(item_grenades);
  if (weapon==item_grenadelauncher) return ITEM_INDEX(item_grenades);
  if (weapon==item_rocketlauncher)  return ITEM_INDEX(item_rockets);
  if (weapon==item_railgun)         return ITEM_INDEX(item_slugs);
  if (weapon==item_hyperblaster)    return ITEM_INDEX(item_cells);
  if (weapon==item_bfg)             return ITEM_INDEX(item_cells);

  return ITEM_INDEX(item_shells); // Must return something!
}

//========================================================
gitem_t *Random_Weapon(void) {

  switch ((rand()%(WEAP_BFG-1))+1) {
    case WEAP_BLASTER:         return item_blaster;
    case WEAP_SHOTGUN:         return item_shotgun;
    case WEAP_SUPERSHOTGUN:    return item_supershotgun;
    case WEAP_MACHINEGUN:      return item_machinegun;
    case WEAP_CHAINGUN:        return item_chaingun;
    case WEAP_GRENADES:        return item_grenades;
    case WEAP_GRENADELAUNCHER: return item_grenadelauncher;
    case WEAP_ROCKETLAUNCHER:  return item_rocketlauncher;
    case WEAP_HYPERBLASTER:    return item_hyperblaster;
    case WEAP_RAILGUN:         return item_railgun;
    case WEAP_BFG:             return item_bfg; }

  return item_blaster;
}

Make sure you put these prototypes in one of your header files:

int FullAmmo(gitem_t *);
gitem_t *Random_Weapon(void);
int AmmoIndexForWeapon(gitem_t *);

Now, change the top part of your InitClientPersistant() function in your p_client.c file
to look like this:

//==================================================
void InitClientPersistant(gclient_t *client) {

  memset(&client->pers,0,sizeof(client->pers));

  // Assign a random weapon for this player..
  client->pers.weapon=Random_Weapon();
  if (client->pers.weapon != item_blaster)
    client->pers.inventory[AmmoIndexForWeapon(client->pers.weapon)]=FullAmmo(client->pers.weapon);
  else
    client->pers.inventory[client->pers.selected_item]=1;

 --------- Rest of Function Continues --------
     ---- assign max_health, etc... -----

</C&NBSP;CODE>

You'll notice that every time the player spawns into the game the function PutClientInServer() is called, and at the very bottom of that function the player is assigned whatever weapon is in their client_persistant struct (which is always reset back to item_blaster in InitClientPersistant). So, what I did was to change InitClientPersistant around so that it picks a random weapon for you to be later assigned to you at the bottom of PutClientInServer.

That's it!

Have Fun!

Maj.Bitch