[Tutorial] A better way to make New Ammo Types I have recently been on a coding frenzy, and created a new way to make seperate ammo, which only uses one gamestate! Sound impossible? Read on, and see how possible it actually is. I got this idea from looking at BrotherTank's original post on Darkone's Raycastor (back when it didn't work).
I will only provide the base code. If you compile 'as-is', the game will still only have one type. This way, you can fit it to match your needs.
Now, our first target is WL_DEF.H. Look for this line:
#define STARTAMMO 8
Add this underneath:
// ----- New Defines ----- \\
#define AMMO
Do you see where I am going with this? If not, you'll understand soon.
Now, go down to the gamestate structure and look at this line:
int ammo;
Change that to:
unsigned char ammo[AMMO];
I have change the variable to an unsigned char, as it doesn't need to be an int unless it exceeds 255 or goes below 0, which is almost never the case with ammo. Also, I have added something extra to the variable. is, in this case, used to specify what weapon you want the variable for. You can have as many as you want for the one variable, and the compiler doesn't mind. Now lets specify what weapons there are. Underneath the gamestate structure, add this code:
typedef AMMO
{
pistol,
machinegun,
chaingun
};
Here, I have put types for all three guns. Even though I have done this, I am only coding for pistol ammo, which will be universal for all guns. Now, you must go through WL_MAIN.C,WL_AGENT.C, and any other WL_***.C files that call on the gamestate.ammo variable, and change them all to gamestate.ammo. Now, to prepare the code for any other types you add, we must go into WL_AGENT.C. Look for:
void GiveAmmo (int ammo);
Change that line to this:
void GiveAmmo (int type,unsigned char ammo);
And don't forget to do the same in WL_DEF.H. Now go down and change the GiveAmmo routine to read:
/*==
==== GiveAmmo
==== Editted by Deathshead
==*/
void GiveAmmo (int type,unsigned char ammo)
{
if (!gamestate.ammo[pistol]) // knife was out
{
if (!gamestate.attackframe)
{
gamestate.weapon = gamestate.chosenweapon;
DrawWeapon ();
}
}
gamestate.ammo[type] += ammo;
if (gamestate.ammo[type] > 99)
gamestate.ammo[type] = 99;
DrawAmmo ();
}
This adds an extra call to Give ammo. This new call checks what ammo type you're giving the ammo to, then adjusts the code to fit. Now, change all calls to GiveAmmo, to read something simliar to:
GiveAmmo (pistol,6);
Now, that should be it. The codes all there for you to make more ammo types, just make a few changes where needed. If I have missed anything, please let me know, and I will update the code. This was pratically a cut and paste job from my source in Extremeties, so it might be missing something.
Now, should you use this, please send your finished project to deathshead.frozenfire@gmail.com , I would like to see your wicked game!
Hint: This same method can also be used on my Backpack code so you don't need to make a new variable for every ammo types maximum capacity...
Have fun
-Deathshead
Conner94- 04-24-2005
Nice someone got this in a tutorial, I thought of the Give Ammo thing a little while back but you still needed more gamestates! Wow, this is awsome!
Deathshead- 04-24-2005
Thanks man. It looked good when I made it as well.
By the By, I'll take this moment to say that the Wolf Source website is up at www.wolfsource.tk . It only has minimal stuff on it now (like my source tutorials, two graphics, etc.
If you wish to submit something, please send it to deathshead.frozenfire@gmail.com
Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.