DieHard Wolfers Forum Index DieHard Wolfers
A Wolfenstein 3d Fan Community


  Hosted by: MCS & Areyep.com - Designed by: BrotherTank

Original Yahoo Forum - Die Hard Archives

AReyeP HomepageAreyep Homepage DieHard Wolfenstein BunkerDieHard Wolfenstein Bunker Log inLog in RegisterRegister Banlist FAQFAQ Search ForumsSearch

  Username:    Password:      Remember me       

[Code] Working with Weapons
Page 1 of 2 Goto page 1, 2  Next
DieHard Wolfers Forum Index -> Code Tutorials View Previous TopicRefresh this PageAdd Topic to your Browser FavoritesSearch ForumsPrint this TopicE-mail TopicGoto Page BottomView Next Topic
Post new topicReply to topic
Author Message
BrotherTank
Forum Administrator
<B>Forum Administrator</B>


Joined: 01 Mar 2003
Last Visit: 13 Sep 2017

Topics: 153
Posts: 2256
Location: Ontario
canada.gif

PostPosted: Fri Dec 24, 2004 8:32 pm
   Subject: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Next PostGoto Bottom of Posts

In the next few posts I will be showing those who aren't familiar with the weapons code, just what it does and how you can modify it.

Greg
BrotherTank
BrotherTank
Forum Administrator
<B>Forum Administrator</B>


Joined: 01 Mar 2003
Last Visit: 13 Sep 2017

Topics: 153
Posts: 2256
Location: Ontario
canada.gif

PostPosted: Fri Dec 24, 2004 8:38 pm
   Subject: [Code] Basic Code - Only have weapons picked up
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

Adding Weapons to the Game - The Base code

Many people have come up with different ways of adding weapons to the game. Here, in the first tutorial below, I present a base that will allow you to add as many weapons as you would like (limit of 10 as coded - need more than that study the code and adjust it accordingly).

Open your "version.h" file and add the following lines:

::: CODE :::

//#define BESTWEAPON         // Player only has weapons picked up
//#define WEAPONCHANGESND    // Make sound when weapon is changed - Enable only if sound added!
//#define WEAPONTHRUST      // Player moves back slightly from weapon firing recoil


Now to enable any of the items, you just remove the "//" from in front of the item.

Open the wl_def.h file and find the "#define NUMWEAPONS 5" line and change that to a 4... In the base code they defined 1 more weapon than was actually in use for the base code.

With that done, lets start changing out some code. Open wl_agent.c and find the "CheckWeaponChange" routine. This new routine will allow up to 10 weapons to be added to your game. You just adjust the "10" in the "for" loop to match the number of weapons you have. Replace it with this:

::: CODE :::

#ifdef BESTWEAPON
// Checks to make sure you have ammo for weapon
// returns "true" if yes and "false" if no
int ChkAtkAmmo (int weapon)
{
   switch (weapon)
   {   
      case wp_knife: return 1;
      case wp_pistol:
      case wp_machinegun:
      case wp_chaingun:
           if (gamestate.ammo > 0 ) return 1;
         break;
   }
   return 0;
}
#endif

void CheckWeaponChange (void)
{
   int   i,kbuttons[10]={sc_1,sc_2,sc_3,sc_4,sc_5,sc_6,sc_7,sc_8,sc_9,sc_0};

// Change the NUMWEAPONS in wl_def.h to match the number of weapons you have
   for (i=0;i<NUMWEAPONS;i++)
      if ((Keyboard[kbuttons[i]]) && (i != gamestate.weapon))
        {
#ifdef BESTWEAPON
           if (gamestate.bestweapon & (1<<i) && ChkAtkAmmo(i))
           {
   #ifdef WEAPONCHANGESND           
              if(gamestate.chosenweapon != i) SD_PlaySound(CHANGEWEAPONSND);
   #endif
#else           
         if (buttonstate[bt_readyknife+i-wp_knife])
         {
   #ifdef WEAPONCHANGESND           
              if(gamestate.chosenweapon != i) SD_PlaySound(CHANGEWEAPONSND);
   #endif
#endif
             gamestate.weapon = gamestate.chosenweapon = i;
              DrawWeapon ();
              DrawAmmo ();
              return;
          }
      }
}


Replace the GiveWeapon routine with this code:

::: CODE :::

#ifdef BESTWEAPON
void GiveWeapon (int weapon)
{
   GiveAmmo (6);   
   gamestate.bestweapon |= (1<<weapon);
   gamestate.weapon = gamestate.chosenweapon = weapon;
   DrawWeapon ();
   DrawAmmo ();
}
#else
void GiveWeapon (int weapon)
{
   GiveAmmo (6);

   if (gamestate.bestweapon<weapon)
      gamestate.bestweapon = gamestate.weapon = gamestate.chosenweapon = weapon;
   DrawWeapon ();
}
#endif


Now replace the "T_Attack" routine with this:

::: CODE :::

/*
===============
=
= T_Attack
=
===============
*/

void   T_Attack (objtype *ob)
{
   struct   atkinf   *cur;
   int i;
   
   UpdateFace ();

   if (gamestate.victoryflag)      // watching the BJ Actor
   {
      VictorySpin ();
      return;
   }
   if ( buttonstate[bt_use] && !buttonheld[bt_use] ) buttonstate[bt_use] = false;
   if ( buttonstate[bt_attack] && !buttonheld[bt_attack]) buttonstate[bt_attack] = false;

   ControlMovement (ob);
   if (gamestate.victoryflag)      // watching the BJ actor
      return;

   plux = player->x >> UNSIGNEDSHIFT;         // scale to fit in unsigned
   pluy = player->y >> UNSIGNEDSHIFT;
   player->tilex = player->x >> TILESHIFT;      // scale to tile values
   player->tiley = player->y >> TILESHIFT;

//
// change frame and fire
//
   gamestate.attackcount -= tics;
   while (gamestate.attackcount <= 0)
   {
      cur = &attackinfo[gamestate.weapon][gamestate.attackframe];
      switch (cur->attack)
      {
#ifdef BESTWEAPON
// End of firing sequence - check ammo and fall back to best possible weapon with ammo
         case -1: 
            ob->state = &s_player;
         
            if (!ChkAtkAmmo (gamestate.weapon) && gamestate.chosenweapon != wp_knife)
            {
               gamestate.weapon = wp_knife;
               for (i=wp_pistol; i<NUMWEAPONS; i++)
               {
                  if (ChkAtkAmmo (i) &&  gamestate.bestweapon & (1<<i))
                  {
                     gamestate.weapon = i;
                  }
               }                     
               DrawWeapon ();
               DrawAmmo ();
            }
            else
            {
               if (gamestate.weapon != gamestate.chosenweapon)
               {
                  if (ChkAtkAmmo(gamestate.chosenweapon))
                     gamestate.weapon = gamestate.chosenweapon;
                  DrawWeapon ();
               }
            }
            gamestate.attackframe = gamestate.weaponframe = 0;
            return;
         
         case 4: // Chaingun - Fall back 2 frames and fire again if button Pressed
            if (!ChkAtkAmmo(gamestate.weapon)) break;
            if (buttonstate[bt_attack]) gamestate.attackframe -= 2;
      
         case 1: // Fire the Weapon
          
          // can only happen with chain gun
            if (!ChkAtkAmmo(wp_chaingun) && gamestate.weapon == wp_chaingun)
               {   gamestate.attackframe++; break; }
         
            if (ChkAtkAmmo(gamestate.weapon))
            {
               GunAttack (ob);
               gamestate.ammo--;
#ifdef WEAPONTHRUST               
               // Move player slightly backwards based on weapon fired
               Thrust(player->angle,-0x200 - (gamestate.chosenweapon * 50) );  // adjust the -0x200 to suit your liking
#endif               
               DrawAmmo ();
               break;
            }
            else
            { return; }
         case 2:
            KnifeAttack (ob);
            break;
         case 3:
            if (ChkAtkAmmo(gamestate.weapon) && buttonstate[bt_attack])
               gamestate.attackframe -= 2;
            break;
   
#else   // Old way of doing things - BESTWEAPON not enabled
         case -1:
            ob->state = &s_player;
            if (!gamestate.ammo)
            {
               gamestate.weapon = wp_knife;
               DrawWeapon ();
            }
            else
            {
               if (gamestate.weapon != gamestate.chosenweapon)
               {
                  gamestate.weapon = gamestate.chosenweapon;
                  DrawWeapon ();
               }
            }
            gamestate.attackframe = gamestate.weaponframe = 0;
            return;

         case 4:
            if (!gamestate.ammo)   break;
            if (buttonstate[bt_attack]) gamestate.attackframe -= 2;
         case 1:
            if (!gamestate.ammo)
            {   // can only happen with chain gun
               gamestate.attackframe++;
               break;
            }
            GunAttack (ob);
            gamestate.ammo--;
#ifdef WEAPONTHRUST               
               // Move player slightly backwards based on weapon fired
               Thrust(player->angle,-0x200 - (gamestate.chosenweapon * 50) ); // adjust the -0x200 to suit your liking
#endif
            DrawAmmo ();
            break;
         case 2:
            KnifeAttack (ob);
            break;
         case 3:
            if (gamestate.ammo && buttonstate[bt_attack])
               gamestate.attackframe -= 2;
            break;
#endif         
      }

      gamestate.attackcount += cur->tics;
      gamestate.attackframe++;
      gamestate.weaponframe = attackinfo[gamestate.weapon][gamestate.attackframe].frame;
   }

}


We're done with that file... Now open "wl_main.c" and replace the "NewGame" routine with this:

::: CODE :::

void NewGame (int difficulty,int episode)
{
   memset (&gamestate,0,sizeof(gamestate));
   gamestate.difficulty = difficulty;
#ifdef BESTWEAPON
   gamestate.weapon = gamestate.chosenweapon = wp_pistol;
   gamestate.bestweapon = 0;
   gamestate.bestweapon |= (1<<wp_knife);
   gamestate.bestweapon |= (1<<wp_pistol);
#else   
   gamestate.weapon = gamestate.bestweapon = gamestate.chosenweapon = wp_pistol;
#endif   
   gamestate.health = 100;
   gamestate.ammo = STARTAMMO;
   gamestate.lives = 3;
   gamestate.nextextra = EXTRAPOINTS;
   gamestate.episode=episode;
   startgame = true;
}


Done with wl_main so now open "wl_game.c" and find the "Died" routine and modify the
"if (gamestate.lives > 0) section of the code to look like this:

::: CODE :::

   if (gamestate.lives > 0)
   {
      gamestate.health = 100;
#ifdef BESTWEAPON
      gamestate.bestweapon = 0 ;
      gamestate.bestweapon |= (1<<wp_knife);
      gamestate.bestweapon |= (1<<wp_pistol);
      gamestate.weapon = gamestate.chosenweapon = wp_pistol;
#else
      gamestate.weapon = gamestate.bestweapon = gamestate.chosenweapon = wp_pistol;
#endif         
      gamestate.ammo = STARTAMMO;
      gamestate.keys = 0;
      gamestate.attackframe = gamestate.attackcount = gamestate.weaponframe = 0;

      DrawKeys ();
      DrawWeapon ();
      DrawAmmo ();
      DrawHealth ();
      DrawFace ();
      DrawLives ();
   }


Those two areas of code (above) are where you set the best weapon the player starts with and how much ammo they have at either the start of a new game or when they die.

Open the "wl_state.c" file and find the "KillActor" routine. Modify the "case ssobj:" section of the code to look like this:

::: CODE :::

#ifdef BESTWEAPON
      if (!(gamestate.bestweapon & (1<<wp_machinegun)))
         PlaceItemType (bo_machinegun,tilex,tiley);
      else
         PlaceItemType (bo_clip2,tilex,tiley);
#else
      if (gamestate.bestweapon < wp_machinegun)
         PlaceItemType (bo_machinegun,tilex,tiley);
      else
         PlaceItemType (bo_clip2,tilex,tiley);
#endif
      break;


Now that's the base done.... Save all your files (if you haven't done so already).

You can now enable the BESTWEAPON in the version.h file and that will make sure that the player only has the weapons that they have picked up. You can also enable the WEAPONTHRUST if you want the player to move slightly back dependant on the weapon fired. To add the weapon change sound, you must add the sound as per any adding sounds to the engine tutorial, and then you can enable the weapon change sound as well..

Compile your new source... And enjoy the new options...

In the next post, I will show you how to break this code apart so that each weapon has it's own ammo or variations on that...

Enjoy...

Greg
BrotherTank

Edited for minor fix - added "#" to defines in version.h code.


Last edited by BrotherTank on Tue Dec 28, 2004 8:01 am; edited 1 time in total
BrotherTank
Forum Administrator
<B>Forum Administrator</B>


Joined: 01 Mar 2003
Last Visit: 13 Sep 2017

Topics: 153
Posts: 2256
Location: Ontario
canada.gif

PostPosted: Tue Dec 28, 2004 8:11 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

The WeaponThrust... (uncommenting the #WEAPONTHRUST from your version.h file). It does work, but it is very minor depending on the weapon fired. If you want to exagerate it, try changing the -0x200 to something like -0x1000 and you will see that when you fire the weapon, the player is moved slightly backwards each time the weapon is fired. The -0x200 is a very subtle recoil... you barely see it in the pistol at all...

The other change that you can make is to the actual formula itself...

Thrust(player->angle,-0x200 - (gamestate.chosenweapon * 50) );

adjust the -0x200 (which is the base backward thrust for all weapons...
adjust the 50 to increase the distance thrown back based on weapon used....

You can make the player be thrown back great distances or very subtle (which is what the original is).. just by playing with the numbers.

Greg
BrotherTank


Last edited by BrotherTank on Fri Jan 14, 2005 1:55 pm; edited 1 time in total
BrotherTank
Forum Administrator
<B>Forum Administrator</B>


Joined: 01 Mar 2003
Last Visit: 13 Sep 2017

Topics: 153
Posts: 2256
Location: Ontario
canada.gif

PostPosted: Tue Jan 04, 2005 10:06 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

Seperate or Different Ammo for different Weapons

The first thing to decide is which weapons will share ammo (if at all) and then work out things like:

What's the base starting ammo for that weapon?
What's the added ammo going to be (both sprite and amount)?
Which guards going to drop different ammo or will it just be a picked up ammo defined on the maps?

The original game wasn't that realistic in the sense that the same ammo fit all three weapons (chaingun, machinegun, and pistol)... unless BJ had some descretely different weapons made specifically for him (like James Bond had Q). The other thing to think about was weight of the ammo. ie: You allow your player to carry 200 rounds of chaingun ammo, 200 rounds of machinegun ammo and 200 rounds of pistol ammo, plus the actual weapons for shooting each. I don't know about you, but I didn't see anything about BJ being the Incredible Hulk anywhere in the games description.

So now with the questions out of the way, it's time to start making changes to the code. In this case, I will be making it so that the Chaingun uses a seperate ammo and the machinegun and pistol can share the same ammo. Basically, this is adding 1 ammo type, but the code can be easily expanded to more. Anyhow... On with the code:

Open your "version.h" file and add the following:

//#define SEPERATEAMMO // remove the initial slashes in front to enable seperate ammo

First, we need to add sprites to the Vswap for our new ammo. Follow the proper procedures for adding additional sprites and defining them in your wl_act1.c file, using the bonus trigger "bo_ammo2". With that done and out of the way, now we need to start making changes to the actual code for the weapons.... Open wl_def.h and search for "ammo" in the gamestate define. To that define we will add:

::: CODE :::

#ifdef SEPERATEAMMO
int       ammo2
#endif


With that taken care of, we now need to edit our code for the starting ammo for that weapon when the game starts and when the player dies. To do that we open "wl_main.c" file and edit the "NewGame" function to look like this:

::: CODE :::

void NewGame (int difficulty,int episode)
{
   memset (&gamestate,0,sizeof(gamestate));
   gamestate.difficulty = difficulty;
#ifdef BESTWEAPON
   gamestate.weapon = gamestate.chosenweapon = wp_pistol;
   gamestate.bestweapon = 0;
   gamestate.bestweapon |= (1<<wp_knife);
   gamestate.bestweapon |= (1<<wp_pistol);
#else   
   gamestate.weapon = gamestate.bestweapon = gamestate.chosenweapon = wp_pistol;
#endif   
   gamestate.health = 100;
   gamestate.ammo = STARTAMMO;
#ifdef SEPERATEAMMO
   gamestate.ammo2 = 0;  // Starting ammo for second ammo type
#endif   
   gamestate.lives = 3;
   gamestate.nextextra = EXTRAPOINTS;
   gamestate.episode=episode;
   startgame = true;
}


Next we have to edit the "Died" function, so open up "wl_game.c" and go to that function. Make it look like this:

::: CODE :::

   if (gamestate.lives > 0)
   {
      gamestate.health = 100;
#ifdef BESTWEAPON
      gamestate.bestweapon = 0 ;
      gamestate.bestweapon |= (1<<wp_knife);
      gamestate.bestweapon |= (1<<wp_pistol);
      gamestate.weapon = gamestate.chosenweapon = wp_pistol;
#else
      gamestate.weapon = gamestate.bestweapon = gamestate.chosenweapon = wp_pistol;
#endif         
      gamestate.ammo = STARTAMMO;
#ifdef SEPERATEAMMO
      gamestate.ammo2 = 0;  // Starting ammo for second ammo type
#endif   
      gamestate.keys = 0;
      gamestate.attackframe = gamestate.attackcount = gamestate.weaponframe = 0;

      DrawKeys ();
      DrawWeapon ();
      DrawAmmo ();
      DrawHealth ();
      DrawFace ();
      DrawLives ();
   }


Ok, with that done, now we can start playing with the "wl_agent.c" file which is where most of the weapon stuff happens. First thing to modify is the new ChkAtkAmmo function that I introduced in the last tutorial... So now we edit it to look like this:

::: CODE :::

#ifdef BESTWEAPON
// Checks to make sure you have ammo for weapon
// returns "true" if yes and "false" if no
int ChkAtkAmmo (int weapon)
{
   switch (weapon)
   {   
      case wp_knife: return 1;
      case wp_pistol:
      case wp_machinegun:
#ifndef SEPERATEAMMO      
      case wp_chaingun:
#endif      
           if (gamestate.ammo > 0 ) return 1;
         break;
#ifdef SEPERATEAMMO
      case wp_chaingun:
         if (gamestate.ammo2 > 0 ) return 1;
         break;
#endif      
   }
   return 0;
}
#endif


Next we have to modify the new "T_Attack" routine that was given above. Make it look like this one now:

::: CODE :::

/*
===============
=
= T_Attack
=
===============
*/

void   T_Attack (objtype *ob)
{
   struct   atkinf   *cur;
   int i;
   
   UpdateFace ();

   if (gamestate.victoryflag)      // watching the BJ Actor
   {
      VictorySpin ();
      return;
   }
   if ( buttonstate[bt_use] && !buttonheld[bt_use] ) buttonstate[bt_use] = false;
   if ( buttonstate[bt_attack] && !buttonheld[bt_attack]) buttonstate[bt_attack] = false;

   ControlMovement (ob);
   if (gamestate.victoryflag)      // watching the BJ actor
      return;

   plux = player->x >> UNSIGNEDSHIFT;         // scale to fit in unsigned
   pluy = player->y >> UNSIGNEDSHIFT;
   player->tilex = player->x >> TILESHIFT;      // scale to tile values
   player->tiley = player->y >> TILESHIFT;

//
// change frame and fire
//
   gamestate.attackcount -= tics;
   while (gamestate.attackcount <= 0)
   {
      cur = &attackinfo[gamestate.weapon][gamestate.attackframe];
      switch (cur->attack)
      {
#ifdef BESTWEAPON
// End of firing sequence - check ammo and fall back to best possible weapon with ammo
         case -1: 
            ob->state = &s_player;
         
            if (!ChkAtkAmmo (gamestate.weapon) && gamestate.chosenweapon != wp_knife)
            {
               gamestate.weapon = wp_knife;
               for (i=wp_pistol; i<NUMWEAPONS; i++)
               {
                  if (ChkAtkAmmo (i) &&  gamestate.bestweapon & (1<<i))
                  {
                     gamestate.weapon = i;
                  }
               }                     
               DrawWeapon ();
               DrawAmmo ();
            }
            else
            {
               if (gamestate.weapon != gamestate.chosenweapon)
               {
                  if (ChkAtkAmmo(gamestate.chosenweapon))
                     gamestate.weapon = gamestate.chosenweapon;
                  DrawWeapon ();
               }
            }
            gamestate.attackframe = gamestate.weaponframe = 0;
            return;
         
         case 4: // Chaingun - Fall back 2 frames and fire again if button Pressed
            if (!ChkAtkAmmo(gamestate.weapon)) break;
            if (buttonstate[bt_attack]) gamestate.attackframe -= 2;
      
         case 1: // Fire the Weapon
          
          // can only happen with chain gun
            if (!ChkAtkAmmo(wp_chaingun) && gamestate.weapon == wp_chaingun)
               {   gamestate.attackframe++; break; }
         
            if (ChkAtkAmmo(gamestate.weapon))
            {
               GunAttack (ob);
#ifdef SEPERATEAMMO
               switch (gamestate.weapon)
               {
                  case wp_chaingun:
                     gamestate.ammo2--; break;
                  default:
                     gamestate.ammo--; break;
               }   
#else
               gamestate.ammo--;                  
#endif
#ifdef WEAPONTHRUST               
               // Move player slightly backwards based on weapon fired
               Thrust(player->angle,-0x200 - (gamestate.chosenweapon * 50) );
#endif               
               DrawAmmo ();
               break;
            }
            else
            { return; }
         case 2:
            KnifeAttack (ob);
            break;
         case 3:
            if (ChkAtkAmmo(gamestate.weapon) && buttonstate[bt_attack])
               gamestate.attackframe -= 2;
            break;
   
#else   // Old way of doing things - BESTWEAPON not enabled
         case -1:
            ob->state = &s_player;
            if (!gamestate.ammo)
            {
               gamestate.weapon = wp_knife;
               DrawWeapon ();
            }
            else
            {
               if (gamestate.weapon != gamestate.chosenweapon)
               {
                  gamestate.weapon = gamestate.chosenweapon;
                  DrawWeapon ();
               }
            }
            gamestate.attackframe = gamestate.weaponframe = 0;
            return;

         case 4:
#ifdef SEPERATEAMMO
            if (!gamestate.ammo2) break;
#else         
            if (!gamestate.ammo)   break;
#endif            
            if (buttonstate[bt_attack]) gamestate.attackframe -= 2;
         case 1:
            if (!gamestate.ammo)
            {   // can only happen with chain gun
               gamestate.attackframe++;
               break;
            }
            GunAttack (ob);
#ifdef SEPERATEAMMO
            if (gamestate.weapon == wp_chaingun) gamestate.ammo2--;
            else
#endif            
            gamestate.ammo--;
#ifdef WEAPONTHRUST               
               // Move player slightly backwards based on weapon fired
               Thrust(player->angle,-0x200 - (gamestate.chosenweapon * 50) );
#endif
            DrawAmmo ();
            break;
         case 2:
            KnifeAttack (ob);
            break;
         case 3:
            if (gamestate.ammo && buttonstate[bt_attack])
               gamestate.attackframe -= 2;
            break;
#endif         
      }

      gamestate.attackcount += cur->tics;
      gamestate.attackframe++;
      gamestate.weaponframe = attackinfo[gamestate.weapon][gamestate.attackframe].frame;
   }

}


As you can see the changes are only minor. The other changes that can be made are also very minor, but depend on how you plan on giving the ammo to the player and if you are checking for specific level limits... Here's what I did. First, rather than have a seperate "GiveAmmo" routine for each ammo type, I merged them into 1. So my GiveAmmo looks something like this:

::: CODE :::

#ifdef SEPERATEAMMO
void   GiveAmmo (int ammo, int weapon)
#else
void   GiveAmmo (int ammo)
#endif
{
#ifdef SEPERATEAMMO
   if (!gamestate.ammo && !gamestate.ammo2)
#else   
   if (!gamestate.ammo)            // knife was out
#endif   
   {
      if (!gamestate.attackframe)
      {
         gamestate.weapon = gamestate.chosenweapon;
         DrawWeapon ();
      }
   }
#ifdef SEPERATEAMMO   
   switch (weapon)
   {   
      case wp_pistol:
      case wp_machinegun:
         gamestate.ammo += ammo;
         if (gamestate.ammo > 99) gamestate.ammo = 99;
         break;
      case wp_chaingun:
         gamestate.ammo2 += ammo;
         if (gamestate.ammo2 > 99) gamestate.ammo2 = 99;
         break;
   }
#else
   gamestate.ammo += ammo;
   if (gamestate.ammo > 99) gamestate.ammo = 99;
#endif
   DrawAmmo ();
}


At the top of the "wl_agent.c" file and in the "wl_def.h" file I also changed the GiveAmmo definitions to look like this:

::: CODE :::

#ifdef SEPERATEAMMO
void GiveAmmo (int ammo, int weapon);
#else
void GiveAmmo (int ammo)
#endif


I also changed the "DrawAmmo" routine to look like this:

::: CODE :::

void   DrawAmmo (void)
{
#ifdef SEPERATEAMMO
   switch (gamestate.weapon)
   {
      case wp_knife:
         ammo = 0;
         break;
      case wp_pistol:
      case wp_machinegun:
         LatchNumber (27,16,2,gamestate.ammo);
         break;
      case wp_chaingun:
         LatchNumber (27,16,2,gamestate.ammo2);
         break;
   }
#else            
   LatchNumber (27,16,2,gamestate.ammo);
#endif   
}


The next thing to search for is all the "GiveAmmo" calls within the "GetBonus" routine and edit them so that you have something like this:

Originally it says something like:

::: CODE :::

   GiveAmmo(4);


And we modify it to look like this:

::: CODE :::

#ifdef SEPERATEAMMO
   GiveAmmo(4,wp_pistol);
#else
   GiveAmmo(4);
#endif


You have to do this for each of the GiveAmmo calls within the GetBonus routine. What we are doing here is telling the code to include the weapon that you wish to have the ammo given to. ie: for the new "bo_ammo2" case that you should have already defined and installed based on adding the sprites for this new ammo type, you would code the new case for the "GetBonus" routine now do it as:

::: CODE :::

#ifdef SEPERATEAMMO
   case bo_ammo2:
      if (gamestate.ammo2 >= 99) return; // Don't pick up if maxed out ammo
      GiveAmmo (25, wp_chaingun);
      SD_PlaySound (GETAMMOSND);
      break;
#endif


That's it for splitting the Ammo... You can split it as many ways as you would like.

In the next few posts (in the near future), I will add additional weapons and such... thus explaining more of the code...

Greg
BrotherTank


Last edited by BrotherTank on Sun Feb 13, 2005 9:51 am; edited 1 time in total
BrotherTank
Forum Administrator
<B>Forum Administrator</B>


Joined: 01 Mar 2003
Last Visit: 13 Sep 2017

Topics: 153
Posts: 2256
Location: Ontario
canada.gif

PostPosted: Fri Jan 14, 2005 1:49 pm
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

Ok...

With the adding of the base information lets take a step back and look at how the different settings within the wl_agent.c file work. Starting with the weapons definition array:

::: CODE :::

struct atkinf  // weapon attack information
{
   char   tics, attack, frame;      
} attackinfo[NUMWEAPONS][12] =   // weapon attack information set into weapon variable array
{
{ {6,0,1},{6,2,2},{6,0,3},{6,-1,4} }, // Knife
{ {6,0,1},{6,1,2},{6,0,3},{6,-1,4} }, // Pistol
{ {6,0,1},{6,1,2},{6,3,3},{6,-1,4} }, // Machinegun
{ {6,0,1},{6,1,2},{6,4,3},{6,-1,4} }, // Chaingun
};


We have 5 weapon frames sprites for each weapon. In wl_draw.c it assumes that if you aren't firing to use the first weapon sprite, so this array holds the information telling it which weapon firing sprite to use, how long to display it, and what to make the program do while displaying that frame. We have 4 groups (1 for each firing sprite for each weapon) of 3 numbers or group.

So breaking this down we see that the first number in each group of 3 variables ("6" as set in each) refers to the number of tics in 70th's of a second that the graphic should be drawn. Each tic in the game is 1/70th of a second. The second number in each group of 3 variables refers to the value sent to the "T_Attack" routine (discussed below). And the last of the numbers in each group of 3 variables tells the wl_draw.c file which weapon firing frame to draw during the shooting process.

Numbers passed to the "T_Attack" routine via the second number in each group of 3 variables are as follows:

0 = Do Nothing - Player is waiting
1 = Shoot Single Shot
2 = Attack with knife - Call "KnifeAttack" routine
3 = Shoot Single Shot
4 = Shoot Multiple Shots - Rapid Fire
-1 = Stop firing - return to basic resting/holding weapon position

You might be saying, but the 1 and the 3 are both single shots. Yes, this is true, but there is a difference. So to see that difference lets look at the "T_Attack" routine a little closer. Looking back at the last set of changes we made... lets assume that we are using the "BESTWEAPON" portion of the code (so I can remove the other lines for space). I have commented the code more so that it might make more sense to those new to this section and to help explain the function a little better:

::: CODE :::

/*
===============
=
= T_Attack
=
===============
*/
void   T_Attack (objtype *ob)
{
   struct   atkinf   *cur;     // Pulls in the defines from our weapon variable array
   int i;
   
   UpdateFace ();               // Update BJ's face on status bar

   if (gamestate.victoryflag)   // If we have won - watch the BJ Actor spin and jump in victory
   {
      VictorySpin ();
      return;
   }
   
   // Clear out the button states and or set if using an attack button
   if ( buttonstate[bt_use] && !buttonheld[bt_use] ) buttonstate[bt_use] = false;
   if ( buttonstate[bt_attack] && !buttonheld[bt_attack]) buttonstate[bt_attack] = false;

   ControlMovement (ob);       // Move BJ if movement keys are pressed
   
   // If we have won - Victory is finished so return to endgame
   if (gamestate.victoryflag)   return;

   plux = player->x >> UNSIGNEDSHIFT;      // convert/scale players "x" to fit in unsigned
   pluy = player->y >> UNSIGNEDSHIFT;      // convert/scale players "y" to fit in unsigned
   player->tilex = player->x >> TILESHIFT; // convert/scale player "x" to tile values (0 to 63)
   player->tiley = player->y >> TILESHIFT; // convert/scale player "x" to tile values (0 to 63)
//
// change frame and fire
//
   gamestate.attackcount -= tics; // Set attackcount time - tics
   while (gamestate.attackcount <= 0) // While attackcount is negative loop
   {
      cur = &attackinfo[gamestate.weapon][gamestate.attackframe]; // Set the weapon frame
      switch (cur->attack) // process the state from weapon values array (second value)
      {
#ifdef BESTWEAPON
// value "-1" = End of firing sequence
//              check ammo and fall back to best possible weapon with ammo
         case -1: 
            ob->state = &s_player;
            // Check - do we have ammo for weapon being used and we aren't using the knife
            if (!ChkAtkAmmo (gamestate.weapon) && gamestate.chosenweapon != wp_knife)
            {  // if not Loop starting from knife and set player weapon to
               // best weapon we have with ammo
               gamestate.weapon = wp_knife;
               for (i=wp_pistol; i<NUMWEAPONS; i++)
               {
                  if (ChkAtkAmmo (i) &&  gamestate.bestweapon & (1<<i))
                  {
                     gamestate.weapon = i;
                  }
               }                     
               DrawWeapon ();  // Update the statusbar with any changes
               DrawAmmo ();
            }
            // We still have ammo, but do we have ammo for the actual chosen weapon
            // and is the chosen weapon the weapon in use?
            else
            {
               if (gamestate.weapon != gamestate.chosenweapon)
               {  // weapon and chosenweapon aren't the same so try and use the
                  // weapon of choice or our prefered weapon
                  if (ChkAtkAmmo(gamestate.chosenweapon))
                     gamestate.weapon = gamestate.chosenweapon;
                  DrawWeapon (); // Update the status bar
               }
            }
            // Set the attack frame and weapon frame to "0" for the wl_draw code...
            gamestate.attackframe = gamestate.weaponframe = 0; 
            return;
// value "4" - Used for rapid firing weapons
         case 4:
            if (!ChkAtkAmmo(gamestate.weapon)) break;  // No Ammo for weapon - Stop firing
            // We have ammo so Fall back 2 frames in attack sequence and drop into case 1
            // below firing the weapon again if the attack or fire button is pressed
            if (buttonstate[bt_attack]) gamestate.attackframe -= 2;
// value "1" - Fire the Weapon in use
         case 1:
          
             // For rapid fire weapons (chaingun) we must check to make sure we still have
             // ammo for the rapid fire weapon - // original-can only happen with chain gun
            if (!ChkAtkAmmo(wp_chaingun) && gamestate.weapon == wp_chaingun)
               {   gamestate.attackframe++; break; } // No Ammo - Break out of firing sequence
         
            if (ChkAtkAmmo(gamestate.weapon)) // If we have ammo start firing process
            {
               GunAttack (ob); // fires the weapon-makes sound and such
#ifdef SEPERATEAMMO
               switch (gamestate.weapons)      // reduce proper ammo type based on weapon used
               {
                  case wp_chaingun:
                     gamestate.ammo2--; break;
                  default:
                     gamestate.ammo--; break;
               }   
#else
               gamestate.ammo--;               // reduce ammo by 1 - single ammo type
#endif
#ifdef WEAPONTHRUST               
               // Move player slightly backwards based on weapon fired
               Thrust(player->angle,-0x200 - (gamestate.chosenweapon * 50) );
               // The above line moves the player back 0x200 plus 50 times the weapon in use
               // ie: weapon chaingun (value 4) we are moved back 0x200 plus 200 (4 times 50)
#endif               
               DrawAmmo (); // Update ammo value on statusbar
               break;
            }
            else
            { return; }
// value "2" - Use the KnifeAttack routine (no ammo required)            
         case 2:
            KnifeAttack (ob);
            break;
// value "3" - Single shot but fallback like the chaingun
// ie: we are still in attack mode - haven't set the weapon back to resting point
// but rather are ready to shoot again if attack button is held down.   
         case 3:
            if (ChkAtkAmmo(gamestate.weapon) && buttonstate[bt_attack])
               gamestate.attackframe -= 2;
            break;
   
#else   
// Old way of doing things - BESTWEAPON not enabled
// Code removed for space during this part of the tutorial
#endif         
      }
      // Adjust the attackcount tics for wl_draw.c routines
      gamestate.attackcount += cur->tics; 
      gamestate.attackframe++; // incrementattack frame
      // set weapon frame to proper attack frame for wl_draw.c routines
      gamestate.weaponframe = attackinfo[gamestate.weapon][gamestate.attackframe].frame;
   }
}


Ok... with that explained, now I can explain the difference between the values 1 and 3 for the single shot setup. Basically by using the value "1" you are telling the game to fire a single shot and stop. The player must then release and press the fire button again for it to fire another shot. The value "3" on the otherhand says we fire only a single shot and if the firing button is held down continue to fire.

Now that you understand the values and what they do, you can now create and modify your weapons to act as you wish in your game. Example... the pistol fires 1 shot and then waits for a button to be pressed again for it to shoot again. We can make it fire continuously if the fire button is held just by changing 1 value in the weapons value array (shown at the top of this post).

We would change:

::: CODE :::

{ {6,0,1},{6,1,2},{6,0,3},{6,-1,4} }, // Pistol


to this:

::: CODE :::

{ {6,0,1},{6,1,2},{6,3,3},{6,-1,4} }, // Pistol


If you are adding weapons (which is what our next post will be about), then you just need to remember to use the proper values for the second variable in our groups of 3 variables.

I hope that makes things easier for everyone to understand how the weapons variable array works and the variables that affect the "T_Attack" function or weapon firing.

Greg
BrotherTank
RichterBelmont12
DieHard Wolfer
DieHard Wolfer


Joined: 14 Aug 2004
Last Visit: 13 Nov 2017

Topics: 97
Posts: 1505
Location: New Jersey
usa.gif

PostPosted: Tue Feb 15, 2005 5:30 pm
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

ok I got an error I have 9 Weapons in my game I pick up my new weapons and when I swich my new weapons to the machinegunI don't see my new weapons

_________________
http://www.youtube.com/RichterBelmont12

https://www.facebook.com/RichterBelmont123

https://twitter.com/Richterbelmont6
RichterBelmont12
DieHard Wolfer
DieHard Wolfer


Joined: 14 Aug 2004
Last Visit: 13 Nov 2017

Topics: 97
Posts: 1505
Location: New Jersey
usa.gif

PostPosted: Tue Feb 15, 2005 5:44 pm
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

ok this part of the code
::: CODE :::
switch (weapon)
   {   
      case wp_knife: return 1;
      case wp_pistol:
      case wp_machinegun:
      case wp_chaingun:
           if (gamestate.ammo > 0 ) return 1;
         break;
   }

I have 9 Weapons do i need to add a case?

_________________
http://www.youtube.com/RichterBelmont12

https://www.facebook.com/RichterBelmont123

https://twitter.com/Richterbelmont6
RichterBelmont12
DieHard Wolfer
DieHard Wolfer


Joined: 14 Aug 2004
Last Visit: 13 Nov 2017

Topics: 97
Posts: 1505
Location: New Jersey
usa.gif

PostPosted: Tue Feb 15, 2005 6:08 pm
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

ok I got it to work but When pick up any weapon it draws to that weapon

_________________
http://www.youtube.com/RichterBelmont12

https://www.facebook.com/RichterBelmont123

https://twitter.com/Richterbelmont6
BrotherTank
Forum Administrator
<B>Forum Administrator</B>


Joined: 01 Mar 2003
Last Visit: 13 Sep 2017

Topics: 153
Posts: 2256
Location: Ontario
canada.gif

PostPosted: Tue Feb 15, 2005 6:11 pm
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

Kyo Kusanagi wrote:
ok I got an error I have 9 Weapons in my game I pick up my new weapons and when I swich my new weapons to the machinegunI don't see my new weapons


I haven't gotten to the adding of new weapons in the tutorial.... and as I stated that is next. I can't say when it will be complete as I am still working on finishing some other projects.

Kyo Kusanagi wrote:
I have 9 Weapons do i need to add a case?


Funny that you quote part of the code but are ambiguous as to which part it is from. Looking back it looks like that is from the "ChkAtkAmmo" routine. That routine deals with whether or not a weapon has ammo and can be used. It returns true if the weapon has ammo or false if it does not. You could have 1000 weapons but the information you have given doesn't tell me anything about what you have done to the code I wrote.

If you can't figure it out for yourself, then I am afraid you will have to wait until I write the next section of the tutorial. The code shouldn't be that hard to follow, but again, I don't know what you have done to it, so I can't even begin to help. Please do not post any of your code in this thread. If you need help, then stick with the thread you already created in the Code Crackers section. There is no need to have multiple threads asking the same thing.

BrotherTank
Zombie_Plan
DieHard Wolfer
DieHard Wolfer


Joined: 12 Oct 2004
Last Visit: 07 Jun 2016

Topics: 101
Posts: 1614
Location: A hole in the wall
australia.gif

PostPosted: Wed Feb 16, 2005 1:00 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

Now we are all replying. I wanted to say, The Seperate Ammo part had quite a few problems when installed into fresh source. I can't list what they are now, but could you check it out for me? Sorry.

_________________
Zombieskittles

Welcome to 2016, where DHW still thinks it is okay to ban the world's most popular email service provider, and I can't have an avatar over 10kb
BrotherTank
Forum Administrator
<B>Forum Administrator</B>


Joined: 01 Mar 2003
Last Visit: 13 Sep 2017

Topics: 153
Posts: 2256
Location: Ontario
canada.gif

PostPosted: Wed Feb 16, 2005 9:13 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

Deathshead wrote:
Now we are all replying. I wanted to say, The Seperate Ammo part had quite a few problems when installed into fresh source. I can't list what they are now, but could you check it out for me? Sorry.


The only part of the tutorial that is "missing" or "has problems" is that it doesn't list how to install the actual sprite and define in both the wl_def.h and the wl_act1.c files. Those items deal with things within your own code that can vary from person to person depending on the changes you've already made to your code. But, if you followed the tutorial you should be able to see that you need to make changes based on what you plan to do with the weapons. ie: if you pick up the chaingun, it should be made to call GiveAmmo with both the amount of ammo you wish it to have and the wp_chaingun type. Then as the tutorial says, you must change the other "bo_??" items that deal with giving ammo to account for the new ammo type... ie: bo_machinegun bonus should be changed to read GiveAmmo(amount, bo_machinegun)

The main reason that most people have problems with these tutorials is that they don't understand the code and what it is doing. That is why I took the time to show the code in tutorial part 3 and give a more detailed account of what the code does. Other tutorials out there may change things or make changes required for you to use the code within your source compatible. Sorry, but I can't account for other changes from other tutorials.

I've tested each one of these changes out and they work properly, but again, I understand the code.

BrotherTank

_________________

The probability of someone watching you is directly related to the Stupidity of your actions
&
The two most abundant things in the Universe are Hydrogen & Stupidity - H.Ellison

Which of those is more relevant to you??
Zombie_Plan
DieHard Wolfer
DieHard Wolfer


Joined: 12 Oct 2004
Last Visit: 07 Jun 2016

Topics: 101
Posts: 1614
Location: A hole in the wall
australia.gif

PostPosted: Thu Oct 13, 2005 1:34 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

When's the next installment?
ronwolf1705
Moderator
<B>Moderator</B>


Joined: 31 Jul 2006
Last Visit: 9:51 ago.

Topics: 69
Posts: 3673

blank.gif

PostPosted: Sat Sep 23, 2006 6:51 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

Deathwish wrote:
When's the next installment?


It's an old topic, but I just recently found it. I hope that, if there ever comes a next installment, that it will be something like adding a rocket launcher or adding a portable medkit, since those tutorials all use other CheckWeaponChange routines and for someone who's new to coding (someone like me) it's very hard to make changes so that they actually work.
ronwolf1705
Moderator
<B>Moderator</B>


Joined: 31 Jul 2006
Last Visit: 9:51 ago.

Topics: 69
Posts: 3673

blank.gif

PostPosted: Sun Sep 24, 2006 7:21 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

How do I let the TAB+I work correctly with the BESTWEAPON tutorial? When I press TAB+I, I only get the machine gun + 50 ammo. Normal, when you press TAB+I several times, you'd get all the weapons, but this time I only get the machinegun.
Chris
DieHard Wolfer
DieHard Wolfer


Joined: 11 Mar 2003
Last Visit: 30 Oct 2017

Topics: 55
Posts: 2115
Location: Canada
blank.gif

PostPosted: Sun Sep 24, 2006 9:24 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

I'm just taking a wild guess here, but what if you try something like this?

::: CODE :::
   else if (Keyboard[sc_I])         // I = item cheat
   {
      CenterWindow (12,3);
      US_PrintCentered ("Free items!");
      VW_UpdateScreen();
      GivePoints (100000);
      HealSelf (99);
      if
(!(gamestate.bestweapon & 2))
         GiveWeapon (wp_pistol);
      else if
(!(gamestate.bestweapon & 4))
         GiveWeapon (wp_machinegun);
      else if
(!(gamestate.bestweapon & 8))
         GiveWeapon (wp_chaingun);

      gamestate.ammo += 50;
      if (gamestate.ammo > 99 || gamestate.ammo < 0)
         gamestate.ammo = 99;
      DrawAmmo ();
      IN_Ack ();
      return 1;
   }

Or this:

::: CODE :::
   else if (Keyboard[sc_I])         // I = item cheat
   {
      int i;
      CenterWindow (12,3);
      US_PrintCentered ("Free items!");
      VW_UpdateScreen();
      GivePoints (100000);
      HealSelf (99);
      for (i = 0; i < NUMWEAPONS; i++)
      {
         if
(!(gamestate.bestweapon & (1<<i)))
         {
            GiveWeapon(i);
            i = NUMWEAPONS;
         }
      }

      gamestate.ammo += 50;
      if (gamestate.ammo > 99 || gamestate.ammo < 0)
         gamestate.ammo = 99;
      DrawAmmo ();
      IN_Ack ();
      return 1;
   }


Last edited by Chris on Sun Sep 24, 2006 9:47 pm; edited 1 time in total
ronwolf1705
Moderator
<B>Moderator</B>


Joined: 31 Jul 2006
Last Visit: 9:51 ago.

Topics: 69
Posts: 3673

blank.gif

PostPosted: Sun Sep 24, 2006 11:33 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

I tried both. The first one gave me 7 errors, so I removed the !, and I got no errors, but when I pressed TAB+I in game I only got ammo, the weapon didn't change, it remained the knife. The same with the second. Also, I couldn't pick up any weapons.
Chris
DieHard Wolfer
DieHard Wolfer


Joined: 11 Mar 2003
Last Visit: 30 Oct 2017

Topics: 55
Posts: 2115
Location: Canada
blank.gif

PostPosted: Sun Sep 24, 2006 9:51 pm
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

Ah ok. Logically, it needs the "not" sign (!) in there, or something similar, but I guess the format I used doesn't work.

I edited both examples again with the stuff in blue. Do either of them compile now?
ronwolf1705
Moderator
<B>Moderator</B>


Joined: 31 Jul 2006
Last Visit: 9:51 ago.

Topics: 69
Posts: 3673

blank.gif

PostPosted: Mon Sep 25, 2006 6:40 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

The second did compile (without the changes in blue), only the first one gave me errors. I will try them.
BrotherTank
Forum Administrator
<B>Forum Administrator</B>


Joined: 01 Mar 2003
Last Visit: 13 Sep 2017

Topics: 153
Posts: 2256
Location: Ontario
canada.gif

PostPosted: Mon Sep 25, 2006 2:07 pm
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

Actually, the code will not worry about which weapons you do have, so it's easier just to give each weapon to the user when the MLI is pressed.

So you would do something like this:

::: CODE :::

#IFDEF BESTWEAPON
   GiveWeapon (wp_pistol);
   GiveWeapon (wp_machinegun);
   GiveWeapon (wp_chaingun);
   // Add any other weapons here.
#ELSE
   GiveWeapon (wp_chaingun);
#ENDIF


Then if you have seperate ammo's for each then you would also have to adjust the ammo amounts here as well. If you wanted to you could also add a counter so that each time MLI is pressed then it would give the next weapon to the player. The other thing that you could do to make the code much simpler is just do this:

Knife = 1
Pistol = 2
Machinegun = 4
Chaingun = 8

So we add these numbers together and get 15. So rather than doing all of the GiveWeapon calls you could simply put the line:

gamestate.bestweapon = 15;


Hope that helps...

Greg
BrotherTank
ronwolf1705
Moderator
<B>Moderator</B>


Joined: 31 Jul 2006
Last Visit: 9:51 ago.

Topics: 69
Posts: 3673

blank.gif

PostPosted: Tue Sep 26, 2006 11:08 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

The MLI code works fine thanks to your code, but how do I let the TAB+I code work correctly with your BESTWEAPON-tutorial?
BrotherTank
Forum Administrator
<B>Forum Administrator</B>


Joined: 01 Mar 2003
Last Visit: 13 Sep 2017

Topics: 153
Posts: 2256
Location: Ontario
canada.gif

PostPosted: Tue Sep 26, 2006 9:36 pm
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

The TAB-I code basically looks to see what the current best weapon is and adds the next higher to it. So quickly off the top of my head you might do something like this:

::: CODE :::


   else if (Keyboard[sc_I])         // I = item cheat
   {
      CenterWindow (12,3);
      US_PrintCentered ("Free items!");
      VW_UpdateScreen();
      GivePoints (100000);
      HealSelf (99);
#ifdef BESTWEAPON  // assuming that chaingun is your best weapon and not held at time
      for (i = wp_chaingun; i >= wp_knife; i--)
      {
         if ((gamestate.bestweapon & (1<<i) && i < wp_chaingun)
         {
            GiveWeapon (i+1);
            i = 0;
         }
      }
#else
      if (gamestate.bestweapon<wp_chaingun)
         GiveWeapon (gamestate.bestweapon+1);
#endif
      gamestate.ammo += 50;
      if (gamestate.ammo > 99)
         gamestate.ammo = 99;
      DrawAmmo ();
      IN_Ack ();
      return 1;
   }


Again, you can play with the different ammo types based on the weapons in play as well. I did this off the top of my head and hasn't been tested, but I'm pretty sure that this should work.

Hope that helps...

Greg
BrotherTank

PS: Sorry for not being around for the past few weeks and the delay in answering - as it hasn't been a good time in my life right now... but anyhow... enjoy...
ronwolf1705
Moderator
<B>Moderator</B>


Joined: 31 Jul 2006
Last Visit: 9:51 ago.

Topics: 69
Posts: 3673

blank.gif

PostPosted: Wed Sep 27, 2006 5:31 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

I read about your health in the Howling Wolfers section. No wonder you didn't answer for a couple of weeks! But the code didn't work, I only got ammo and health, no weapons.
Chris
DieHard Wolfer
DieHard Wolfer


Joined: 11 Mar 2003
Last Visit: 30 Oct 2017

Topics: 55
Posts: 2115
Location: Canada
blank.gif

PostPosted: Wed Sep 27, 2006 7:39 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

@ronwolf: One thing that would be helpful for you (not just for this case, but for every case), and would save people from doing the work for you, would be to do some bug testing. Brothertank explained how the code works, and gave you an idea on how to change the weapons. So, now that you know how gamestate.bestweapon works, the first test you could do is to look at your value for gamestate.bestweapon before and after you run the "gun adding" codes in Tab-I that we posted.

How do you do that? One way I find easy is to throw gamestate.bestweapon into gamestate.score, like this:

::: CODE :::
   else if (Keyboard[sc_I])         // I = item cheat
   {
      CenterWindow (12,3);
      US_PrintCentered ("Free items!");
      VW_UpdateScreen();
      GivePoints (100000);
      HealSelf (99);
      gamestate.score = gamestate.bestweapon;
      DrawScore();
      IN_Ack();

      
      // Your weapon adding code


      gamestate.score = gamestate.bestweapon;
      DrawScore();
      IN_Ack();

      
      gamestate.ammo += 50;
      if (gamestate.ammo > 99)
         gamestate.ammo = 99;
      DrawAmmo ();
      IN_Ack ();
      return 1;
   }

Try inserting that before and after to see what score you get. That's just one way, there are many others (like using US_PrintUnsigned).

As Brothertank said, here is how each gamestate.bestweapon value is stored:

1 - Knife
2 - Pistol
4 - Machinegun
8 - Chaingun


So, if you're just starting the level, and have only the pistol and the knife, your score should start off as 3 (1-Knife + 2-Pistol).
To see where this is initialized, look back at Brothertank's first post and you will notice 3 "gamestate.bestweapon = lines" in NewGame.

The "IN_Ack()" means that you have press a key to continue. So, after you see the 3 in your score, and you press a key, what should the gamestate.bestweapon value be after you get the Machinegun? Think about that for a second. The Machine Gun is 4, the Pistol is 2, and the Knife is 1. So, 4 + 2 + 1 is 7. If your score does say 7 after you pressed Tab-I, then you know the code in Tab-I isn't the problem. If it doesn't, you could easily change the "gun adding" code yourself with something like this:

::: CODE :::
if (gamestate.bestweapon == 3)
   gamestate.bestweapon = 7;

Try adding that, then restart the game and try Tab-I again. Does it say 7 now? And can you press 3 to get the Machine Gun?

Once you have the first three weapons, changing the value of gamestate.bestweapon to 15 will let you have all 4 weapons, as Brothertank explained (which is because 1-knife + 2-pistol + 3-machinegun + 4-chaingun = 15). So, assuming that the player always starts off wih the pistol, you could use something like this for your "gun adding" code in Tab-I (where I put the "// Your weapon code" in blue) to take care of both scenarios:

::: CODE :::
   else if (Keyboard[sc_I])         // I = item cheat
   {
      CenterWindow (12,3);
      US_PrintCentered ("Free items!");
      VW_UpdateScreen();
      GivePoints (100000);
      HealSelf (99);

      if (gamestate.bestweapon == 3)
         gamestate.bestweapon = 7;
      else
         gamestate.bestweapon = 15;

      
      gamestate.ammo += 50;
      if (gamestate.ammo > 99)
         gamestate.ammo = 99;
      DrawAmmo ();
      IN_Ack ();
      return 1;
   }

Now, try pressing 4. Does it show the chaingun?

Note: Using GiveWeapon(wp_machinegun) and GiveWeapon(wp_chaingun) is a better idea than "gamestate.bestweapon = 7" and "gamestate.bestweapon = 15" because it actually switches the weapon for you. Once you know gamestate.bestweapon works, try using GiveWeapon instead, like this:

::: CODE :::
      if (gamestate.bestweapon == 3)
         GiveWeapon (wp_machinegun);
      else
         GiveWeapon (wp_chaingun);

If the "score test" still works, but the weapons don't behave properly, you know that Tab-I isn't the problem, because you clearly defined exactly which weapons you should get for each scenario, and tested to see that the value did get stored into gamestate.bestweapon. Right? So, at this point, if there is still something wrong, I would look at other areas to see where the problem could be (eg. you didn't type Brothertank's original code in correctly, some other function doesn't work right, etc.).
ronwolf1705
Moderator
<B>Moderator</B>


Joined: 31 Jul 2006
Last Visit: 9:51 ago.

Topics: 69
Posts: 3673

blank.gif

PostPosted: Wed Sep 27, 2006 11:41 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

Thanks Chris, everything works correctly now.
Rux
DieHard Guard
DieHard Guard


Joined: 17 Dec 2006
Last Visit: 15 Nov 2015

Topics: 34
Posts: 202
Location: Port Angeles, WA
usa.gif

PostPosted: Sat Dec 23, 2006 10:23 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

I'm getting kind of a weird bug when I go to switch weapons. If I press say... 1 and 2 at the same time, the graphics flicker between the knife and the pistol. When I let go, everything is fine but to my knowledge it didn't do this before and it bugs the hell out of me. I don't have a clue how to fix it, or where to start. Granted, there is no reason to be pressing 1 and 2 at the same time, but it still might come up.

_________________
Like Metroid? Like fangames? Like top-down games? Like FUN? Head over to www.metroidcoven.com and check out my game called Metroid: Coven.
the_fish
Bring 'em On
Bring 'em On


Joined: 18 Nov 2006
Last Visit: 07 Nov 2008

Topics: 12
Posts: 133
Location: England
uk.gif

PostPosted: Sat Dec 23, 2006 4:28 pm
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

I'm not sure that's a bug...the input manager (at least in my version of the code...) treats a key held down like it does in any word processor, or whatever, and rrrrreeeeeeaaaappppeeeeeaaaatttttsssss the keypress - its not as simple as storing whether the key is depressed or not, which sucks as lot as it causes stuff like this.

[technical]DOS uses interrupts to update the keyboard states, and the changeweapon function fires whenever it receives an appropriate one of these. In contrast, Linux stores its keystates in a file somewhere that a program can look at ('poll') to get key input. You could change the code to check the keystates once per playloop instead, the same way movement is done (PollKeyboard function), but its a lot of effort.[/technical]

I think the game is receiving input to switch to weapon 1, then weapon 2, then back to weapon 1 again etc. in quick succession, hence the flickering. There'd be no reason to do this in game (unless you wanted to randomly pick a weapon to use), so I doubt it'd be that important.

Are you sure this didn't happen in the unaltered code?
Rux
DieHard Guard
DieHard Guard


Joined: 17 Dec 2006
Last Visit: 15 Nov 2015

Topics: 34
Posts: 202
Location: Port Angeles, WA
usa.gif

PostPosted: Sat Dec 23, 2006 4:49 pm
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

Yes, it doesn't do this at all. Say you have the pistol and knife. You press 1 you get knife. Press 2, pistol. 1 and 2 together... pistol. 1 and 2 then you let go of 1, you get pistol. 1 and 2 then you let go of 2, you get the knife. Something from some previous tutorial (or maybe this one for that matter) must have changed something that controlled the flickering.

_________________
Like Metroid? Like fangames? Like top-down games? Like FUN? Head over to www.metroidcoven.com and check out my game called Metroid: Coven.
Rux
DieHard Guard
DieHard Guard


Joined: 17 Dec 2006
Last Visit: 15 Nov 2015

Topics: 34
Posts: 202
Location: Port Angeles, WA
usa.gif

PostPosted: Sun Dec 24, 2006 1:30 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

Aside from that, when my new weapon runs out of ammo while firing, it gets stuck on the firing frame instead of returning to its ready state! As in, it looks like it is frozen with its shooting graphic!! Also, once, I switch to my knife, I can't to another weapon!! The only way to do that after getting stuck with my knife is the MLI cheat... I used the seperate ammo tutorial and the first tutorial (both in this thread). This is all very frustrating... Evil or Very Mad

_________________
Like Metroid? Like fangames? Like top-down games? Like FUN? Head over to www.metroidcoven.com and check out my game called Metroid: Coven.
BrotherTank
Forum Administrator
<B>Forum Administrator</B>


Joined: 01 Mar 2003
Last Visit: 13 Sep 2017

Topics: 153
Posts: 2256
Location: Ontario
canada.gif

PostPosted: Sun Dec 24, 2006 10:10 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

Arclese wrote:
Aside from that, when my new weapon runs out of ammo while firing, it gets stuck on the firing frame instead of returning to its ready state! As in, it looks like it is frozen with its shooting graphic!! Also, once, I switch to my knife, I can't to another weapon!! The only way to do that after getting stuck with my knife is the MLI cheat... I used the seperate ammo tutorial and the first tutorial (both in this thread). This is all very frustrating... Evil or Very Mad


Then backtrack.... Hopefully you followed the number one rule and made backups...

All the sections of this thread have been tested and tested and proven to work.
The image flickering doesn't happen here... And what I have posted is exact to what is in my code. As to the weapons getting "stuck" as you say... when you run out of ammo, then you can't select the weapon... hitting MLI will restock the ammo and allow you to select the weapon again.

So my suggestion - if you think it's not working properly, is to go back to the backup and restore your working copy to pre-installing of this tutorial.. and then install it again - unless it is working properly and you just didn't understand that this tutorial only lets you have:

1. the weapons that you pick up.
2. use those weapons picked up only when you have ammo.

Sounds like with the flickering that you should probably restore and re-install anyhow...

Greg
BrotherTank
insurrectionman
DieHard Mutant
DieHard Mutant


Joined: 07 May 2005
Last Visit: 09 Dec 2017

Topics: 86
Posts: 752
Location: Wisconsin
usa.gif

PostPosted: Mon Feb 05, 2007 9:54 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Bottom of Posts

I decided to use this tutorial for my weapons and it saved me a buttload of memory. Unfortunately it may not support 2 weapons with 1 button. Can anyone help me out with that?

_________________
I'm back! I missed this community!
Youtube Channel: TreeSapThief

New Site: http://www.treesapthief.com
Twitter: @treesapthief
Display posts from previous:   
Post new topicReply to topic Time synchronized with the forum server time
DieHard Wolfers Forum Index -> Code Tutorials View Previous TopicRefresh this PageAdd Topic to your Browser FavoritesSearch ForumsPrint this TopicE-mail TopicGoto Page TopView Next Topic
Page 1 of 2 Goto page 1, 2  Next
Jump to:  

Related topics
 Topics   Replies   Views   Last Post 
No new posts [Code] Blood Splatz Tutorial - Blood Splatters
Author: Dugtrio17
45 11219 Tue Mar 11, 2008 12:02 am
AlumiuN View latest post
No new posts [Code] Display Different Ammo Types on Statusbar-BrotherTank
Author: BrotherTank
1 2381 Fri Feb 11, 2005 9:18 pm
Zombie_Plan View latest post
No new posts [Code] Adding a Frames per Second Counter - Darkone
Author: BrotherTank
0 1703 Sat Mar 13, 2004 2:07 pm
BrotherTank View latest post
No new posts [Code] Changing an Enemies Attack Strength - BrotherTank
Author: BrotherTank
0 1969 Tue Jan 27, 2004 10:29 am
BrotherTank View latest post
No new posts [Code] Changing Weapons -CheckWeaponChange- BrotherTank
Author: BrotherTank
2 2700 Sun Oct 26, 2003 1:21 am
Guest View latest post
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
   You cannot delete your posts in this forum
You cannot vote in polls in this forum


Copyright ©2003-2008 DieHard Wolfers
A Modified subBunker Theme by BrotherTank
Powered by phpBB © 2001, 2005 phpBB Group