I made a grenade launcher with a revolver-type drum that holds 6 grenades. I'm having trouble with the reloading though. I successfully made it to check how many grenades are left in the drum when the reload starts and play the anim for putting a grenade in the drum as many times as needed, and block the attack until the reloading finishes. However, I'm having trouble when I tried to make it stop the reloading prematurely with the attack button and only keep as many grenades in the drum as have already been put in when I stop it.
Here are some parts of the code that are connected to the reloading process:
PHP Code:
//Time needed for inserting one grenade
#define RELOAD_TIME 1.9
new Float:gtime[33];
new Float:refilltime[33] = 0.0;
new g_refilledAmmo[33] = 0;
//Called when the primary attack button is pressed
public ev_attack1(id) {
//Check if the reloading has finished
if (gtime[id] < (get_gametime() - refilltime[id]))
{
wpn_playanim(id,anim_throw );
emit_sound(id, CHAN_WEAPON, s_FIRE_1, 1.0, ATTN_NORM, 0, PITCH_NORM);
if(g_UserRocket[id][rocket_entity] != -1) return PLUGIN_CONTINUE
shoot_rocket(id);
client_cmd(id, "-attack");
return PLUGIN_CONTINUE;
}
//The reloading hasn't finished, stop it and calculate grenades in drum
else
{
refilltime[id] = 0;
new usrwpn = wpn_has_weapon(id,g_wpnid);
new newammo = wpn_get_userinfo(id,usr_wpn_ammo1,usrwpn) + g_refilledAmmo[id] - 4;
wpn_set_userinfo(id,usr_wpn_ammo1,usrwpn,newammo);
client_cmd(id, "-attack");
return PLUGIN_CONTINUE;
}
}
//Start reload process
public ev_reload(id) {
//Save the time the reload starts
gtime[id] = get_gametime();
g_refilledAmmo[id] = 0;
//Start inserting of one grenade
insert_gren(id);
new usrwpn = wpn_has_weapon(id,g_wpnid);
//Check how many grenades left in drum and decide how many more to add
switch(wpn_get_userinfo(id,usr_wpn_ammo1,usrwpn))
{
//Drum empty, insert 5 grenades in addition to the one before the switch()
case 0:
{
set_task(RELOAD_TIME,"insert_gren",id,"",0,"a",5);
//Calculate how long is the reload going to take
refilltime[id] = RELOAD_TIME*6;
return PLUGIN_CONTINUE;
}
//1 grenade left in drum, insert 4 grenades in addition to the one before the switch()
case 1:
{
set_task(RELOAD_TIME,"insert_gren",id,"",0,"a",4);
refilltime[id] = RELOAD_TIME*5;
return PLUGIN_CONTINUE;
}
//2 grenades left in drum
case 2:
{
set_task(RELOAD_TIME,"insert_gren",id,"",0,"a",3);
refilltime[id] = RELOAD_TIME*4;
return PLUGIN_CONTINUE;
}
//3 grenades left in drum
case 3:
{
set_task(RELOAD_TIME,"insert_gren",id,"",0,"a",2);
refilltime[id] = RELOAD_TIME*3;
return PLUGIN_CONTINUE;
}
//4 grenades left in drum
case 4:
{
set_task(RELOAD_TIME,"insert_gren",id,"",0,"a",1);
refilltime[id] = RELOAD_TIME*2;
return PLUGIN_CONTINUE;
}
//only one grenade was shot, no grenades needed in addition to the first reloaded one, just calculate the reloading time
case 5:
{
refilltime[id] = RELOAD_TIME;
return PLUGIN_CONTINUE;
}
}
return PLUGIN_CONTINUE;
}
//Insert one grenade in the drum
public insert_gren(id)
{
wpn_playanim(id, anim_pullpin);
g_refilledAmmo[id]++; //Calculate how many grenades were inserted during the reload
}
So in case 0, when the gun has to reload 6 grenades, there is no problem, but in the other cases, if I stop the reloading, I get over 20 grenades in the clip.
I added one line client_print(id, print_center,"reloaded ammo: %d", g_refilledAmmo[id]); at the end of the insert_gren function and found out that when I'm reloading all 6 grenades, g_refilledAmmo prints 1, 2, 3, 4, 5 just as it should, when I reload 5 or less, it prints something like 1, 11, 23, 35 and so on, I don't remember the exact numbers but you get the idea.
I tried a lot of different things but was unable to fix this. So, I really need your help!
__________________