(Old code tutorial) Adding a Backpack IMPROVED from Wolf Planet forum

View previous topic View next topic Go down

(Old code tutorial) Adding a Backpack IMPROVED from Wolf Planet forum

Post by Officer-M. John (Admin) on Sat Mar 26, 2016 9:02 pm

There are a few Backpack tutorials out there, and they all do the job more or less. One such is:

Code:

  case   bo_clip:
       if (gamestate.ammo == 99)
           return;

Change that to:

Code:

  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':

Code:

#define STARTAMMO   8

Add the following line underneath:

Code:

#define MAXAMMO      100

Now, go down to the Gamestate Structure, and look for this line (if using original stucture):

Code:

int   ammo;

Change that to:

Code:

int ammo,ammomax;

Also, search for:

Code:

bo_spear

Change that to:

Code:

bo_spear,
bo_backpack

Save and close that file. Now open WL_AGENT.C . Look for:

Code:

void GiveAmmo (int ammo);

Add the following underneath:

Code:

void GiveBackPack (void);

Now, scroll down to the GiveAmmo Function:

Code:

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:

Code:

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:

Code:

/*==
==== 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:

Code:

case   bo_clip:
        if (gamestate.ammo == 99)
           return;

        SD_PlaySound (GETAMMOSND);
        GiveAmmo (8);
        break;

Now, change the if statement to look like:

Code:

if (gamestate.ammo == gamestate.ammomax)
         return;

Do that in the case for bo_clip2 as well. Now, create a new case for bo_backpack:

Code:

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:

Code:

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;':

Code:

gamestate.ammomax = 100;

Now, in WL_GAME.C, go to this section of the Died Function:

Code:

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;':

Code:

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:

Code:

{SPR_STAT_0},

Becomes:

Code:

{SPR_STAT_0,bo_backpack},

Now scroll down until you find this:

Code:

   case   bo_food:
   case   bo_alpo:
   case   bo_gibs:
   case   bo_spear:

Add this line underneath:

Code:

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

Writte by Deathshead on Wed Apr 06, 2005 11:29 am
avatar
Officer-M. John (Admin)
Admin

Posts : 328
Join date : 2015-10-02
Age : 19

View user profile http://wolf3dfanboys.usersboard.com

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum