Quote:
|
I have tried another solution, but not working, always previous selected weapon will set ammo to 50
|
Because when that event is called player still not have that weapon.
You should note that this method is exploitable.
It's possible to reload weapon and gain 20 extra ammo (50-30) then drop and collect weapon again and repeat given steeps.
Take a look on the code below, it does not have such exploit.
Code:
#include <amxmodx>
#include <engine>
#include <cstrike>
// should be greater than corresponding standard
#define CLIP_AMMO 50
public plugin_init() {
register_event("WeapPickup", "event_weapon_pickup", "be")
}
public event_weapon_pickup(id) {
// get id of collected weapon
new wid = read_data(1)
if (wid == CSW_AK47 || wid == CSW_M4A1) { // if ak47 or m4a1
new param[1]
param[0] = wid
// wait a bit since weapon entity not created yet
set_task(0.1, "task_set_ammo", id, param, 1)
}
}
public task_set_ammo(param[1], id) {
new wid = param[0], wname[20]
// weapon name is the same as weapon entity classname
get_weaponname(wid, wname, 19)
// find weapon entity index by it's classname and owner
new went = find_ent_by_owner(-1, wname, id)
if (!went) // if weapon entity isn't found
return
// get amount of ammo in clip of collected weapon
new clip = cs_get_weapon_ammo(went)
if (clip >= CLIP_AMMO) // if ammo addition isn't required
return
// get amount of player's pack ammo for collected weapon
new pack = cs_get_user_bpammo(id, wid)
if (!pack) // if player have no ammo for that weapon
return
// total available amount of ammo for collected weapon
new total = clip + pack
// here will be stored result ammo amount for clip and pack
new clip2, pack2
// if total ammo greater than or equal to defined clip ammo
if (total >= CLIP_AMMO) {
clip2 = CLIP_AMMO
pack2 = total - CLIP_AMMO
}
else {
clip2 = total
pack2 = 0
}
// set new clip and pack ammo amount for collected weapon
cs_set_weapon_ammo(went, clip2)
cs_set_user_bpammo(id, wid, pack2)
}
But you also should note that this method will not redefine clip capacity.
It only will set 50 ammo for collected weapon's clip and nothing more.
To complete that you need to figure out when weapon is reloaded and perfor similar steeps.