[Tutorial] Adding a Backpack IMPROVED There are a few Backpack tutorials out there, and they all do the job more or less. One such is:
case bo_clip:
if (gamestate.ammo == 99)
return;
Change that to:
case bo_clip:
switch (gamestate.backpack)
{
case 0:
if (gamestate.ammo == 99)
return; break;
case 1:
if (gamestate.ammo == 110)
return; break;
}
And put however many cases you want, then do the same everywhere else that that if statement is called. It works, but there are better ways. Here, I present my own code, in which the game automatically calculates the maximum ammo. There are no more switch cases or if statements, just a few variables and a small void function.
First, in WL_DEF.H, look for 'STARTAMMO':
#define STARTAMMO 8
Add the following line underneath:
#define MAXAMMO 100
Now, go down to the Gamestate Structure, and look for this line (if using original stucture):
int ammo;
Change that to:
int ammo,ammomax;
Also, search for:
bo_spear
Change that to:
bo_spear,
bo_backpack
Save and close that file. Now open WL_AGENT.C . Look for:
void GiveAmmo (int ammo);
Add the following underneath:
void GiveBackPack (void);
Now, scroll down to the GiveAmmo Function:
void GiveAmmo (int ammo)
{
if (!gamestate.ammo) // knife was out
{
if (!gamestate.attackframe)
{
gamestate.weapon = gamestate.chosenweapon;
DrawWeapon ();
}
}
gamestate.ammo += ammo;
if (gamestate.ammo > 99)
gamestate.ammo = 99;
DrawAmmo ();
}
Change it to read:
void GiveAmmo (int ammo)
{
if (!gamestate.ammo) // knife was out
{
if (!gamestate.attackframe)
{
gamestate.weapon = gamestate.chosenweapon;
DrawWeapon ();
}
}
gamestate.ammo += ammo;
if (gamestate.ammo > gamestate.ammomax)
gamestate.ammo = gamestate.ammomax;
DrawAmmo ();
}
Instead of checking if gamestate.ammo is equal to 99, it checks if it matches gamestate.ammomax, which currently is equal to 100. Now, directly underneath this function, place this function:
/*==
==== GiveBackPack
==== By Deathshead
====
==== Gives player a
==== backpack so he may
==== hold more ammo
==*/
void GiveBackPack (void)
{
if (gamestate.ammomax < 200)
gamestate.ammomax += AMMOMAX;
else
gamestate.ammomax = 200;
}
This function first checks if gamestate.ammomax is less then 200, if yes, then gamestate.ammomax is increased by the amount of MAXAMMO (which is 10). If gamestate.ammomax is over or equal to 200, then the game resets it to 200. Now, go down to the GetBonus function and look for:
case bo_clip:
if (gamestate.ammo == 99)
return;
SD_PlaySound (GETAMMOSND);
GiveAmmo (8);
break;
Now, change the if statement to look like:
if (gamestate.ammo == gamestate.ammomax)
return;
Do that in the case for bo_clip2 as well. Now, create a new case for bo_backpack:
case bo_backpack:
SD_PlaySound (BONUS2SND);
GivePoints (500);
GiveBackPack ();
break;
Now, whenever bo_backpack is collected, GiveBackPack is activated, giving the player a larger ammo maximum.
What if the the player dies, or starts a NewGame? Well, we need to reset the maximums. Open up WL_MAIN.C and look for the NewGame Function:
void NewGame (int difficulty,int episode)
{
memset (&gamestate,0,sizeof(gamestate));
gamestate.difficulty = difficulty;
gamestate.weapon = gamestate.bestweapon
= gamestate.chosenweapon = wp_pistol;
gamestate.health = 100;
gamestate.ammo = STARTAMMO;
gamestate.lives = 3;
gamestate.nextextra = EXTRAPOINTS;
gamestate.episode=episode;
startgame = true;
}
Add the following line after 'gamestate.ammo = STARTAMMO;':
gamestate.ammomax = 100;
Now, in WL_GAME.C, go to this section of the Died Function:
if (gamestate.lives > -1)
{
gamestate.health = 100;
gamestate.weapon = gamestate.bestweapon
= gamestate.chosenweapon = wp_pistol;
gamestate.ammo = STARTAMMO;
gamestate.keys = 0;
gamestate.attackframe = gamestate.attackcount =
gamestate.weaponframe = 0;
Once again, add the following line underneath 'gamestate.ammo = STARTAMMO;':
gamestate.ammomax = 100;
Now we must make an item use this attriute. Go into WL_ACT1.C, and if you don't wish to make a new sprite, change a sprite that is already there. I will change the first sprite (the water puddle) into our new backpack:
{SPR_STAT_0},
Becomes:
{SPR_STAT_0,bo_backpack},
Now scroll down until you find this:
case bo_food:
case bo_alpo:
case bo_gibs:
case bo_spear:
Add this line underneath:
case bo_backpack:
Now, use your favourite Sprite Editor, and replace the water puddle with the backpack. Now, save and compile, and your new backpack will be ready to use. This can easily be editted for more ammotypes, different totals, etc.
Now, go and code!
-Deathshead
Conner94- 04-06-2005
This is a nice tutorial, better update the sticky! It is also nice to have a tutorial on the fourm casue my tutorial on my site is gone. Though this is to late for war storm.
Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.