Wilson [29th ID]
09-01-2006, 06:45 PM
This tutorial is for scripters who have made a plugin or too - I didn't go extremely indepth with it to provide for newbies yet.
You'll notice if you give a client ammo, their HUD either does not reflect it or it gets messed up and doesn't display all of it. There is a way to fix that, but first you must understand there is a difference between "clip ammo" and "backpack ammo."
Clip ammo - the ammo you're currently firing that's inside your gun - clip/magazine/whatever it's called for that gun.
Backpack ammo - the ammo that's "in your backpack" - the extra clips/magazines
Updating backpack ammo is actually easier than updating clip ammo. To do this, you use the event "AmmoX". In order to actually update through AmmoX, however, you need to know the channel of the weapon you're trying to update. These channels can be found through a message logger such as Damaged Soul's, but fortunately for you I've found them all and they're in this tutorial.
It helps to define the channels so you don't have to remember them. This is similar to a const file, but you simply add it to the top of your plugin.
The commented text to the right of the value is what weapons use that channel. This list includes all weapons in day of defeat, so enjoy.
#define AMMO_SMG 1 // thompson, greasegun, sten, mp40
#define AMMO_ALTRIFLE 2 // carbine, k43, mg34
#define AMMO_RIFLE 3 // garand, enfield, scoped enfield, k98, scoped k98
#define AMMO_PISTOL 4 // colt, webley, luger
#define AMMO_SPRING 5 // springfield
#define AMMO_HEAVY 6 // bar, bren, stg44, fg42, scoped fg42
#define AMMO_MG42 7 // mg42
#define AMMO_30CAL 8 // 30cal
#define AMMO_GREN 9 // grenades (should be all 3 types)
#define AMMO_ROCKET 13 // bazooka, piat, panzerschreck
Now to actually update the backpack ammo, you can create a function like below.
public set_ammo(channel, ammo)
message_begin(MSG_ONE,gmsgAmmoX,{0,0,0},id);
write_byte(channel);
write_byte(ammo);
message_end();
}
Then you can call the function like below. This will set the K98 to have 3 backpack clips (they each contain 5 rounds)
set_ammo(AMMO_RIFLE, 15)
Also keep in mind that AmmoX only updates the HUD - it does not actually adjust any pdata which contains the actual variable of how much ammunition the player has. That is what dod_set_user_ammo() does but I'll let you read the .inc files for that.
Basically, if you only do the above, when you reload, it will go to the number it was at before (minus 1 since you reloaded)
--------------------------------------------------------
Now setting the clip itself is a lot harder and it took a while to figure out. Just use this function.
This will set the k98 clip to have only 4 rounds.
set_clip(id, "weapon_kar", 4)
public set_clip(id,const weapon[],clip) {
new currentent = -1, gunid = 0
// get origin
new Float:origin[3];
entity_get_vector(id,EV_VEC_origin,origin);
while((currentent = find_ent_in_sphere(currentent,origin,Float:1.0)) != 0) {
new classname[32];
entity_get_string(currentent,EV_SZ_classname,class name,31);
if(equal(classname,weapon))
gunid = currentent
}
set_pdata_int(gunid,108,clip); // set their ammo
return PLUGIN_CONTINUE
}
Note that the clip setting does affect pdata and not just the HUD.
Like I said, this is for intermediate level scripters. Most new scripters won't find this as usefull as others who have a few plugins they couldn't release cause the hud gets screwed up :-P
You'll notice if you give a client ammo, their HUD either does not reflect it or it gets messed up and doesn't display all of it. There is a way to fix that, but first you must understand there is a difference between "clip ammo" and "backpack ammo."
Clip ammo - the ammo you're currently firing that's inside your gun - clip/magazine/whatever it's called for that gun.
Backpack ammo - the ammo that's "in your backpack" - the extra clips/magazines
Updating backpack ammo is actually easier than updating clip ammo. To do this, you use the event "AmmoX". In order to actually update through AmmoX, however, you need to know the channel of the weapon you're trying to update. These channels can be found through a message logger such as Damaged Soul's, but fortunately for you I've found them all and they're in this tutorial.
It helps to define the channels so you don't have to remember them. This is similar to a const file, but you simply add it to the top of your plugin.
The commented text to the right of the value is what weapons use that channel. This list includes all weapons in day of defeat, so enjoy.
#define AMMO_SMG 1 // thompson, greasegun, sten, mp40
#define AMMO_ALTRIFLE 2 // carbine, k43, mg34
#define AMMO_RIFLE 3 // garand, enfield, scoped enfield, k98, scoped k98
#define AMMO_PISTOL 4 // colt, webley, luger
#define AMMO_SPRING 5 // springfield
#define AMMO_HEAVY 6 // bar, bren, stg44, fg42, scoped fg42
#define AMMO_MG42 7 // mg42
#define AMMO_30CAL 8 // 30cal
#define AMMO_GREN 9 // grenades (should be all 3 types)
#define AMMO_ROCKET 13 // bazooka, piat, panzerschreck
Now to actually update the backpack ammo, you can create a function like below.
public set_ammo(channel, ammo)
message_begin(MSG_ONE,gmsgAmmoX,{0,0,0},id);
write_byte(channel);
write_byte(ammo);
message_end();
}
Then you can call the function like below. This will set the K98 to have 3 backpack clips (they each contain 5 rounds)
set_ammo(AMMO_RIFLE, 15)
Also keep in mind that AmmoX only updates the HUD - it does not actually adjust any pdata which contains the actual variable of how much ammunition the player has. That is what dod_set_user_ammo() does but I'll let you read the .inc files for that.
Basically, if you only do the above, when you reload, it will go to the number it was at before (minus 1 since you reloaded)
--------------------------------------------------------
Now setting the clip itself is a lot harder and it took a while to figure out. Just use this function.
This will set the k98 clip to have only 4 rounds.
set_clip(id, "weapon_kar", 4)
public set_clip(id,const weapon[],clip) {
new currentent = -1, gunid = 0
// get origin
new Float:origin[3];
entity_get_vector(id,EV_VEC_origin,origin);
while((currentent = find_ent_in_sphere(currentent,origin,Float:1.0)) != 0) {
new classname[32];
entity_get_string(currentent,EV_SZ_classname,class name,31);
if(equal(classname,weapon))
gunid = currentent
}
set_pdata_int(gunid,108,clip); // set their ammo
return PLUGIN_CONTINUE
}
Note that the clip setting does affect pdata and not just the HUD.
Like I said, this is for intermediate level scripters. Most new scripters won't find this as usefull as others who have a few plugins they couldn't release cause the hud gets screwed up :-P