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 2 of 2 Goto page Previous  1, 2
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
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: Mon Feb 05, 2007 11:43 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Next PostGoto Bottom of Posts

Um...explain two weapons with one button? Is that like multiple weapons that can be cycled through with repeated button presses, or a weapon that gets changed/upgraded later in the game, or what?

_________________
Mostly away: revising for finals (argh!)
insurrectionman
DieHard Mutant
DieHard Mutant


Joined: 07 May 2005
Last Visit: 14 Aug 2018

Topics: 87
Posts: 759
Location: Wisconsin
usa.gif

PostPosted: Mon Feb 05, 2007 1:26 pm
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

Like doom's Shotgun and Super shotgun. Press 3 to get shotgun, press it again to receive the supershotgun. I have it sort of working, but it doesn't switch back and forth accurately.

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

New Site: http://www.treesapthief.com
Twitter: @treesapthief
BrotherTank
Forum Administrator
<B>Forum Administrator</B>


Joined: 01 Mar 2003
Last Visit: 13 Sep 2017

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

PostPosted: Mon Feb 05, 2007 3:25 pm
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

Give me an hour or so and I'll give you the fix for it. It's not difficult really (some small playing is needed for the weapon check). I'll post the code once I have tested it to make sure it's working 100%.

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


Joined: 01 Mar 2003
Last Visit: 13 Sep 2017

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

PostPosted: Tue Feb 06, 2007 12:27 am
   Subject: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

First in WL_DEF.H file, I would suggest that everyone playing with the weapons and code change the weapon define to look like this before adding any weapons. This way, NUMWEAPONS is always correct (it's actually 1 higher than we actually have in the original source):

::: CODE :::

typedef enum   {
   wp_knife,            // 0
   wp_pistol,            // 1
   wp_machinegun,         // 2
   wp_chaingun,         // 3
// Insert New Weapons Before the NUMWEAPONS line below   
   NUMWEAPONS
} weapontype;


________________________________________________

Ok InsurrectionMan,

Here it is... You'll actually kick yourself for not getting this... It's so simple really....

First some information that you will need to know for the other sections of the tutorial that I posted earlier in the thread. This will help you modify the other sections and understand thinsg a little better:

::: CODE :::

/*
   This is a specific change for some type of super addition to some weapon
   In this example we use a super shotgun - aka single barrel shotgun and double barrel
   Switchable between 1 single key

   Assuming Weapons are indexed as follows: 
      
   Knife, Pistol, Shotgun, SuperShotgun, Machinegun, Chaingun
      
   the values you need to know are:
                        
   Weapon Name               Keyboard Select       Weapon #      Bestweapon #
   ------------------------------------------------------------------
   Knife                              1                           0                      1
   Pistol                           2                           1                        2
   Shotgun                           3                           2                        4
   Super Shotgun                  3                           3                        8
   Machinegun                     4                           4                      16
   Chaingun                        5                           5                      32

*/



Now for your's specifically the define in wl_def.h would change to:

::: CODE :::
typedef enum   {
   wp_knife,         // 0
   wp_pistol,         // 1
   wp_shotgun,      // 2
   wp_supershotgun,      // 3
   wp_machinegun,      // 4
   wp_chaingun,      // 5
// Insert New Weapons Before the NUMWEAPONS line below   
   NUMWEAPONS
} weapontype;


So now you have the defines for your different shotguns. I assume that you know how to add the bo_?? values for picking it up and such - as well as adding a new weapon.

Now for the big change and you'll kick yourself for not seeing 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; // Knife always has ammo
      case wp_pistol:
#ifdef SEPERATEAMMO
            if (gamestate.ammo) return 1;
#endif
         case wp_shotgun:
         case wp_supershotgun:
#ifdef SEPERATEAMMO
            if (gamestate.ammo1) return 1;
#endif
      case wp_machinegun:
#ifdef SEPERATEAMMO
            if (gamestate.ammo2) return 1;
#endif
      case wp_chaingun:
#ifdef SEPERATEAMMO
   #ifdef SEPERATEAMMOCHAINGUN  // if not defined Chaingun and Machinegun use same ammo
            if (gamestate.ammo3) return 1;   
   #else
            if (gamestate.ammo2) return 1;
#else
            if (gamestate.ammo) return 1;
#endif
         default:              
         break;
   }
   return 0;
}
#endif

void CheckWeaponChange (void)
{
   int   i,kbuttons[11]={sc_1,sc_2,sc_3,sc_3,sc_4,sc_5,sc_6,sc_7,sc_8,sc_9,sc_0};
  for (i=0;i<NUMWEAPONS;i++)
  {
     if ((Keyboard[kbuttons[i]]) && (i != gamestate.weapon))
    {
#ifdef BESTWEAPON
         if ((gamestate.bestweapon & (1<<i)) && ChkAtkAmmo(i))
#else   
         if (buttonstate[bt_readyknife+i-wp_knife])
#endif
         {
   #ifdef WEAPONCHANGESND
            if(gamestate.chosenweapon != i)
            {
      #ifdef NOKNIFESNDATALL
               if (i)
                {
      #endif
      #ifdef SEPERATEWEAPONCHANGESNDS
                  switch (i)
                     case wp_knife:
         #ifdef NOKNIFESND
                        break;
         #else
                        SD_PlaySound(CHANGEKNIFEWEAPONSND); break;
         #endif               
                     case wp_pistol:
                        SD_PlaySound(CHANGEPISTOLWEAPONSND); break;
                     case wp_machinegun:
                        SD_PlaySound(CHANGEMACHINEWEAPONSND); break;
                     case wp_chaingun:
                        SD_PlaySound(CHANGECHAINGUNWEAPONSND); break;
                     default:
      #endif
                        SD_PlaySound(CHANGEWEAPONSND);
      #ifdef NOKNIFESNDATALL
               }
      #endif      
   #endif
            IN_ClearKeysDown();
            gamestate.weapon = gamestate.chosenweapon = i;
            DrawWeapon ();
            DrawAmmo ();
            return;
         }
      }
   }
}


That's right... all we did was include a second "sc_3" in the keyboard defines. It's that simple. If you press "3" and are using any other weapon, then it chooses the first shotgun (which is normal). If you press "3" again, it will switch to the second shotgun. To understand the routine a little more.....

The above routine scans through the kbuttons[array] until it finds the key that is pressed and checks to see if that is the weapon in use. If it's not, then it says "use this" if we actually have the weapon and ammo for it. It will continue to check the kbutton array until it has reached the end or Max weapons (NUMWEAPONS). It doesn't care what keys are in the array... or what order.

That's actually the beauty of this routine and why I wrote it the way that I did. It doesn't require any special if statements or the like. What it does look for is: What key is pressed? Do we have that weapon? Do we have Ammo? Use if Yes to those questions... Not or No to the questions...... Keep searching to see if it matches something else.

Hope that helps...

Greg
BrotherTank

PS: As you can see I included some of the other stuff I was toying with, but I did remove the other weapons like Flamethrower and Rocket Launcher as they require more changes to the code and additional routines.
insurrectionman
DieHard Mutant
DieHard Mutant


Joined: 07 May 2005
Last Visit: 14 Aug 2018

Topics: 87
Posts: 759
Location: Wisconsin
usa.gif

PostPosted: Tue Feb 06, 2007 7:25 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

Okay, I did that, but I never added the IN_ClearKeysDown(); and if I pressed 3 and had at least one weapon, it also had the other weapon. I don't know why, and when i would select between weapons it could randomly come up the shotgun or supershotgun. I also added gun change bob to this and it would go down, but not come up because it was selecting between each weapon at every tic. Long story short, does the IN_ClearKeysDown prevent this?

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

New Site: http://www.treesapthief.com
Twitter: @treesapthief
BrotherTank
Forum Administrator
<B>Forum Administrator</B>


Joined: 01 Mar 2003
Last Visit: 13 Sep 2017

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

PostPosted: Tue Feb 06, 2007 12:17 pm
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

insurrectionman wrote:
Okay, I did that, but I never added the IN_ClearKeysDown(); and if I pressed 3 and had at least one weapon, it also had the other weapon.


If you had both weapons then you didn't create the seperate bo_?? assignments for each of the weapons.... The In_ClearKeysDown() I'll explain below....

insurrectionman wrote:
I don't know why, and when i would select between weapons it could randomly come up the shotgun or supershotgun.


There must have been something else wrong in your code then as all this code does is bind each key to a weapon. I'll go through the logic of the code again and maybe that will help you understand.

insurrectionman wrote:
I also added gun change bob to this and it would go down, but not come up because it was selecting between each weapon at every tic. Long story short, does the IN_ClearKeysDown prevent this?


Adding the change gun bob shouldn't affect anything - providing it's added correctly. The IN_ClearKeysDown is just a routine that clears any other key presses waiting in the Input Buffer once it has made a selection.

Anyhow... For your situation, you have to look at the weapons you want as two seperate weapons. You have to add the two weapons to the weapon define in wl_def.h add the new sprites for both weapons in the sprite list and so on. Basically, you do everything for adding a new weapon twice.

For Examples sake, lets say that you are adding 2 weapons to the original game.

wp_shotgun and wp_supershotgun

So your weapon define in wl_def.h would look like:

::: CODE :::
typedef enum   {
   wp_knife,         // 0
   wp_pistol,         // 1
   wp_shotgun,      // 2
   wp_supershotgun,      // 3
   wp_machinegun,      // 4
   wp_chaingun,      // 5
// Insert New Weapons Before the NUMWEAPONS line below   
   NUMWEAPONS
} weapontype;


The CheckWeaponChange routine I coded works like this:

::: CODE :::

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

kbuttons[#]={sc_?} is an array that holds the keyboard buttons that the program will react to.
There needs to be '1' element of the array for each weapon in your game.
I included in the original code 10 different sc_? codes to define up to 10 different keys to 10 different weapons, but you can modify that section of the code to look like this:

::: CODE :::
int   i,kbuttons[NUMWEAPONS]={sc_1,sc_2,sc_3,sc_4,sc_5,sc_6};


So the array really only needs 1 key assignment for each weapon. Now the above would assign 1 different key for each of the weapons defined in the game. You want 1 key to do both weapons (the "3" on the keyboard), so we change that line to read:

::: CODE :::
int   i,kbuttons[NUMWEAPONS]={sc_1,sc_2,sc_3,sc_3,sc_4,sc_5};


Now the rest of the code I will just comment within the code bubble.

::: CODE :::

// Loop from weapon 0 - Knife to NUMWEAPONS (5) in our example case.
  for (i=0;i<NUMWEAPONS;i++) 
  {
// If the Keyboard Scan Code Array Item "i" is pressed and we are not currently
// using that weapon then go to next condition check - if 'No' then loop to next array
// check and next weapon.
     if ((Keyboard[kbuttons[i]]) && (i != gamestate.weapon))
    {
// The next 'if' lines check to make sure that you have the weapon you wanted
// to select and you actually have ammo.  If yes, then start the weapon assignment
// or more simply: 'Choose that weapon' as we have it and the ammo.
#ifdef BESTWEAPON
         if ((gamestate.bestweapon & (1<<i)) && ChkAtkAmmo(i))
#else   
         if (buttonstate[bt_readyknife+i-wp_knife] && i <= gamestate.bestweapon)
#endif
         {
   #ifdef WEAPONCHANGESND  // This stuff is here but not required unless you are adding sounds to the engine for use when the player is changing weapons.
            if(gamestate.chosenweapon != i)  // if not equal weapon chosen to weapon previously chosen - yes we want to make a sound
            {
      #ifdef NOKNIFESNDATALL
               if (i) //  knife is weapon '0' so if weapon chosen is knife 'i' would be zero or false - make no sound at all
                {
      #endif
      #ifdef SEPERATEWEAPONCHANGESNDS
                  switch (i) // choose the proper selected weapon and make a sound when changing weapons
                     case wp_knife:
         #ifdef NOKNIFESND
                        break;
         #else
                        SD_PlaySound(CHANGEKNIFEWEAPONSND); break;
         #endif               
                     case wp_pistol:
                        SD_PlaySound(CHANGEPISTOLWEAPONSND); break;
                     case wp_machinegun:
                        SD_PlaySound(CHANGEMACHINEWEAPONSND); break;
                     case wp_chaingun:
                        SD_PlaySound(CHANGECHAINGUNWEAPONSND); break;
                     default:
      #endif
                        SD_PlaySound(CHANGEWEAPONSND);
      #ifdef NOKNIFESNDATALL
               }
      #endif       
   #endif  // end of the weapon change sound stuff.
            IN_ClearKeysDown(); // We passed the above conditions so we don't need any more key presses - so clear the input buffer.
            gamestate.weapon = gamestate.chosenweapon = i; // Assign the weapon we have and the chosen weapon (for weapon fallback when you run out of ammo)
            DrawWeapon (); // Update the weapon chosen onto the statusbar
            DrawAmmo (); // Update the ammo we have on the statusbar - now if you are not using seperate ammo, you don't need this line really.  If you are using seperate ammo this line is needed as we need to update the screen to match ammo to chosen weapon.
            return; //  Exit the loop and continue in the game - we have found - chosen - and assigned the weapon the player has requested.
         } // end of we have weapon and ammo so give player that weapon check
      } // end of we checked key press and is it currently chosen check
   } // End of for-next loop - keep checking everything inside the loop until we have checked everything that has been defined as a weapon change key.  If we get here after checking everything (looping until we have reached NUMWEAPONS) then nothing matched so we have done nothing and given the player nothing.  If we did find something within the loop that matches the right conditions the 'return' line above exits this loop and returns control to the player.
} // End of function


Now... If you remove the Gun change bob, and use only the original code, this should work 100% on it's own. If it's not then you have done something wrong, or missed something in adding the weapons.

The routine itself is pretty much bulletproof.

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


Joined: 31 Jul 2006
Last Visit: 8:19 ago.

Topics: 72
Posts: 3762

blank.gif

PostPosted: Sat Jun 09, 2007 8: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've added both the BESTWEAPON and the Seperate Ammo tutorial, but now everytime I select the knife, when I have both ammo types, I can't select the gun and machinegun, only the chaingun. Can anyone help me out?

EDIT:

Nevermind, I have fixed it.
insurrectionman
DieHard Mutant
DieHard Mutant


Joined: 07 May 2005
Last Visit: 14 Aug 2018

Topics: 87
Posts: 759
Location: Wisconsin
usa.gif

PostPosted: Fri Jan 25, 2008 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

I have implemented this into the Wolf4SDL source code, (not that hard rite?) but there is one line that this newer engine does not support and that is:

gamestate.bestweapon |= (1<<weapon);

because bestweapon is a weapontype, instead of short, it doesn't allow the |=, so is there an alternate way to do this, keeping the same kind of byte code, 1,2,4,8,16,etc.?

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

New Site: http://www.treesapthief.com
Twitter: @treesapthief
Tricob
Moderator
<B>Moderator</B>


Joined: 14 Mar 2005
Last Visit: 18:50 ago.

Topics: 163
Posts: 8166
Location: Neo-traditions, Inc.
usa.gif

PostPosted: Fri Jan 25, 2008 8: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 haven't seen "|=" before. I'm familiar with "!=", but not "|=". Confused
AlumiuN
DieHard Wolfer
DieHard Wolfer


Joined: 29 Nov 2007
Last Visit: 1:09 ago.

Topics: 38
Posts: 2466
Location: Christchurch, New Zealand
newzealand.gif

PostPosted: Sat Jan 26, 2008 12:46 pm
   Subject: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

Ahhh, the bitwise operators. Well, you could write a function like this somewhere:

::: CODE :::
long GetBit(long bitnum)
{
    return pow(2, bitnum)
}


and then throw this line at the top of the file that this is in:

::: CODE :::
#include <math.h>


Finally, change:

::: CODE :::
gamestate.bestweapon |= (1<<weapon);


to

::: CODE :::
gamestate.bestweapon += GetBit(1<<weapon);


I think that's right, but the bitnum may need a -1 on it. I'm not sure. Smile Of course, I could be entirely wrong. Sad
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: Sun Jan 27, 2008 5:38 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

Agh! Though AlumiuN's code may work, I just recoiled in horror at using sophisticated functions to perform bitwise operations. It's the proverbial sledgehammer cracking the nut. Waaay inefficient (won't matter, because you're calling this way less than once per frame, but it's still bad practise)

Not sure if the compiler would allow this, but try casting the 'weapontype's to 'short'. The following fudge might do it:

::: CODE :::

short sTemp;

...

sTemp = (short) gamestate.bestweapon;
sTemp |= (1<<((short) weapon));
gamestate.bestweapon = (weapontype) sTemp;


But without actually trying this in your compiler I won't know if it works...

_________________
Mostly away: revising for finals (argh!)
insurrectionman
DieHard Mutant
DieHard Mutant


Joined: 07 May 2005
Last Visit: 14 Aug 2018

Topics: 87
Posts: 759
Location: Wisconsin
usa.gif

PostPosted: Sun Jan 27, 2008 11:31 am
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Next PostGoto Bottom of Posts

hey the_fish, your method worked, it looks like something I needed, but couldn't put the pieces together correctly, alumiun's didn't work at all, so thanks for the help.

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

New Site: http://www.treesapthief.com
Twitter: @treesapthief
AlumiuN
DieHard Wolfer
DieHard Wolfer


Joined: 29 Nov 2007
Last Visit: 1:09 ago.

Topics: 38
Posts: 2466
Location: Christchurch, New Zealand
newzealand.gif

PostPosted: Sun Jan 27, 2008 3:29 pm
   Subject: Re: [Code] Working with Weapons
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Bottom of Posts

I forgot about casting. That's much simpler. I didn't think my code would work. Smile
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 2 of 2 Goto page Previous  1, 2
Jump to:  

Related topics
 Topics   Replies   Views   Last Post 
No new posts [Code] Blood Splatz Tutorial - Blood Splatters
Author: Dugtrio17
45 13370 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 2723 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 1989 Sat Mar 13, 2004 2:07 pm
BrotherTank View latest post
No new posts [Code] Changing an Enemies Attack Strength - BrotherTank
Author: BrotherTank
0 2300 Tue Jan 27, 2004 10:29 am
BrotherTank View latest post
No new posts [Code] Changing Weapons -CheckWeaponChange- BrotherTank
Author: BrotherTank
2 3088 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