
Posted by Maj.Bitch
(209.142.8.*) on February 23, 1999 at 14:52:43:
Title: Random Weapons Assignment at Respawn
Difficulty: Easy
By: Philip (aka Maj.Bitch)
Email: peblair@gv.net
Date: 02-23-99
Note: Please give credit where credit is due.
======================================================
When I test out stuff and I need a specific weapon, every
time I respawn into the game, I have to run around to find
what I needed or do a give_all.. Then I was thinking, why
is it that you start off with the blaster anyway? Why not
have it assign to you a random weapon (full of ammo, of course)?
Well, now you can!! Simply set the server config var in your
autoexec.cfg file (as shown below) and you can have random
weapon assignments to player's at each respawn..
NOTE: You'll need to have my items enhancement already
implemented
into your source (if not, then look at my Quadrant.Zip).. If you've
already got the fast-item switching stuff implemented (or are using
my Quadrant.Zip source) then you're 98% there!
Okay. let's get started!!
======================================================
Also, you need these defines. They should already be in
your source but if not you need to put them in your g_locals.h
file..
// gitem_t->weapmodel
#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
======================================================
You'll have to add these new defines to your g_locals.h file:
//==========================================
//========= MAXIMUM PLAYER UNITS ===========
//==========================================
#define MAX_ROCKETS 50
#define MAX_GRENADES 50
#define MAX_SLUGS 50
#define MAX_SHELLS 100
#define MAX_BULLETS 200
#define MAX_CELLS 200
======================================================
Okay, add these two new functions to the very bottom of
your g_items.c file..
//========================================================
// Returns a random weapon item
//========================================================
gitem_t *G_Random_Weapon(void) {
int i;
do {
i=(((int)(random()*100+0.5))%WEAP_BFG)+1;
} while (i<WEAP_BLASTER ||
i>WEAP_BFG);
switch (i) {
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;
}
//========================================================
// Returns correct ammo amount for weapon item.
//========================================================
int G_FullAmmo(gitem_t *weapon) {
if (weapon==item_shotgun) return MAX_SHELLS;
if (weapon==item_supershotgun) return MAX_SHELLS;
if (weapon==item_machinegun) return MAX_BULLETS;
if (weapon==item_chaingun) return MAX_BULLETS;
if (weapon==item_handgrenade) return MAX_GRENADES;
if (weapon==item_grenadelauncher)
return MAX_GRENADES;
if (weapon==item_rocketlauncher) return MAX_ROCKETS;
if (weapon==item_railgun) return MAX_SLUGS;
if (weapon==item_hyperblaster) return MAX_CELLS;
if (weapon==item_bfg) return MAX_CELLS;
return 0;
}
NOTE: You can comment out certain weapons if you don't want
players in your mod starting out with them (Like the BFG).
======================================================
Be sure to prototype these functions by placing the following
at the bottom of your g_locals.h file..
int G_FullAmmo(gitem_t *item);
gitem_t *G_Random_Weapon(void);
======================================================
Now, find your InitClientPersistant() function in your
p_client.c file and add the following to it:
void InitClientPersistant(gclient_t *client) {
memset(&client->pers, 0, sizeof(client->pers));
//========
START HERE ================
if (sv_randweapons->value) {
// Assign a random weapon for this
player..
client->pers.weapon=G_Random_Weapon();
// Give player full ammo in
inventory..
client->pers.inventory[AmmoIndex(client->pers.weapon)]=G_FullAmmo(client->pers.weapon);
}
else
// Else assign default BLASTER..
client->pers.weapon=item_blaster;
// Only 1 of the selected weapons..
client->pers.selected_item=ITEM_INDEX(client->pers.weapon);
client->pers.inventory[client->pers.selected_item]=1;
//========
END HERE ================
Make this part of your InitClientPersistant() function look
just like this new if-statement...
======================================================
Finally, we need to add the new server config variable..
Add this new extern cvar_t declaration to your g_locals.h
file:
extern cvar_t *sv_randweapons;
Okay, lets formally declare it with the rest of them at the
top
of your g_main.c file by adding the following:
cvar_t *sv_randweapons;
Let's set the default value by placing the following line in
your
InitGame() function like so:
sv_randweapons=gi.cvar("sv_randweapons","0",CVAR_SERVERINFO);
If you want this to default as ON then change the 0 to a
1...
======================================================
Lastly..
Be sure to add the following to your Autoexec.cfg file:
set sv_randweapons 1 // OR 0 if you want it OFF
That's it!!
Now respawn with your random weapon!!
And get out there and KILL ALL THOSE BASTARDS!
Have Fun!!
Philip
Follow Up:
Posted by Maj.Bitch
(209.142.6.*) on February 23, 1999 at 17:45:02:
In Reply to: TUTORIAL:
Random Weapon Assignment at Respawn.. posted by Maj.Bitch on February 23,
1999 at 14:52:43:
Instead of this:
: //========================================================
: gitem_t *G_Random_Weapon(void) {
: int i;
: do {
: i=(((int)(random()*100+0.5))%WEAP_BFG)+1;
: } while (iWEAP_BFG);
: switch (i) {
You can have this:
gitem_t *G_Random_Weapon(void) {
switch ((rand()%WEAP_BFG)+1) {
Same thing..
regards,
philip