Veteran Member
Join Date: Feb 2013
Location: Rromania
|
09-19-2017
, 16:39
Re: Need some correction in code for giving nades
|
#35
|
Quote:
Originally Posted by TheSpaniard
wickedd for your information
The code natesh posted :-
PHP Code:
/* Plugin generated by AMXX-Studio */
#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <hamsandwich>
#include <fun>
#define PLUGIN "FreeNadesGiver"
#define VERSION "1.0"
#define AUTHOR "TheSpaniard"
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
RegisterHam(Ham_Spawn, "player", "Fwd_PlayerSpawn_Post", 1);
register_clcmd("say /nade", "Fwd_PlayerSpawn_Post")
}
public Fwd_PlayerSpawn_Post(id)
{
if(!is_user_alive(id) || !(get_user_flags(id) & ADMIN_LEVEL_H))
{
return 0
}
set_task(1.0, "give_nades", id)
return 1
}
public give_nades(id)
{
if(!is_user_alive(id)) return;
give_item(id, "weapon_hegranade")
cs_set_user_bpammo(id, CSW_HEGRENADE, 2)
give_item(id, "weapon_smokegranade")
cs_set_user_bpammo(id, CSW_SMOKEGRENADE, 2)
give_item(id, "weapon_flashbang")
cs_set_user_bpammo(id, CSW_FLASHBANG, 4)
}
its disadvantages:-
it can result in crash as it wont check user ammo and add grenades every 15 seconds and if weapon strip is not included in any loaded plugin the player who did not die that round would have huge no of grenades at round start and server can crash. Although the edit you quoted is wrong but before that he gave me right directions.
code suggested by darthman:-
PHP Code:
/* Plugin generated by AMXX-Studio */
#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <hamsandwich>
#include <fun>
#define PLUGIN "FreeNadesGiver"
#define VERSION "1.0"
#define AUTHOR "TheSpaniard"
#define TASKID 1440
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_event("ResetHUD", "forwardResetHud", "be");
register_clcmd("say /nade", "give_nades")
}
public forwardResetHud(i_Client)
{
if(!(get_user_flags(i_Client) & ADMIN_LEVEL_H))
{
return PLUGIN_HANDLED
}
set_task(15.0, "give_nades", TASKID+i_Client, _, _, "b")
return PLUGIN_CONTINUE
}
public give_nades(id)
{
new henade = user_has_weapon(id, 4, -1)
new heammo = cs_get_user_bpammo(id, CSW_HEGRENADE)
new smokenade = user_has_weapon(id, 9, -1)
new smokeammo = cs_get_user_bpammo(id, CSW_SMOKEGRENADE)
new flashnade = user_has_weapon(id, 25, -1)
new flashammo = cs_get_user_bpammo(id, CSW_FLASHBANG)
if(henade == 1 && heammo << 2)
{
cs_set_user_bpammo(id, CSW_HEGRENADE, 2)
}
else
{
give_item(id, "weapon_hegrenade")
cs_set_user_bpammo(id, CSW_HEGRENADE, 2)
}
if(smokenade == 1 && smokeammo << 2)
{
cs_set_user_bpammo(id, CSW_SMOKEGRENADE, 2)
}
else
{
give_item(id, "weapon_smokegrenade")
cs_set_user_bpammo(id, CSW_SMOKEGRENADE, 2)
}
if(flashnade == 1 && flashammo << 2)
{
cs_set_user_bpammo(id, CSW_FLASHBANG, 4)
}
else
{
give_item(id, "weapon_flashbang")
cs_set_user_bpammo(id, CSW_FLASHBANG, 4)
}
return PLUGIN_HANDLED
}
i made some changes in it:-
PHP Code:
/* Plugin generated by AMXX-Studio */
#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <hamsandwich>
#include <fun>
#define PLUGIN "FreeNadesGiver"
#define VERSION "1.0"
#define AUTHOR "TheSpaniard"
#define TASKID 1440
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
RegisterHam(Ham_Spawn, "player", "Fwd_PlayerSpawn_Post", 1);
register_clcmd("say /nade", "give_nades")
}
public Fwd_PlayerSpawn_Post(id)
{
if((!(get_user_flags(id) & ADMIN_LEVEL_H)))
{
return PLUGIN_HANDLED
}
return PLUGIN_CONTINUE
}
public client_putinserver(id)
{
if(is_user_connected(id))
set_task(15.0, "give_nades", id + TASKID, _, _, "b")
}
public client_disconnect(id)
{
remove_task(id + TASKID);
}
public give_nades(id)
{
new henade = user_has_weapon(id - TASKID, 4, -1)
new heammo = cs_get_user_bpammo(id - TASKID, CSW_HEGRENADE)
new smokenade = user_has_weapon(id - TASKID, 9, -1)
new smokeammo = cs_get_user_bpammo(id - TASKID, CSW_SMOKEGRENADE)
new flashnade = user_has_weapon(id - TASKID, 25, -1)
new flashammo = cs_get_user_bpammo(id - TASKID, CSW_FLASHBANG)
if(henade == 1 && heammo << 2)
{
cs_set_user_bpammo(id - TASKID, CSW_HEGRENADE, 2)
}
else
{
give_item(id - TASKID, "weapon_hegrenade")
cs_set_user_bpammo(id - TASKID, CSW_HEGRENADE, 2)
}
if(smokenade == 1 && smokeammo << 2)
{
cs_set_user_bpammo(id - TASKID, CSW_SMOKEGRENADE, 2)
}
else
{
give_item(id - TASKID, "weapon_smokegrenade")
cs_set_user_bpammo(id - TASKID, CSW_SMOKEGRENADE, 2)
}
if(flashnade == 1 && flashammo << 2)
{
cs_set_user_bpammo(id - TASKID, CSW_FLASHBANG, 4)
}
else
{
give_item(id - TASKID, "weapon_flashbang")
cs_set_user_bpammo(id - TASKID, CSW_FLASHBANG, 4)
}
return PLUGIN_HANDLED
}
and its advantages:-
1.with client_putinserver you can check if the player is alive or not and set task.
2.with client_disconnect you dont need to go through confusing task_exists() function and directly remove task.
3.hamspawn confirms compatibility with csdm and other respawn using plugins.
now you say its poorly coded can you give some optimizations or correction.
if yes, thank you in advance.
|
you should stop giving scripting advice, you're doing more damage than good
__________________
Meanwhile, in 2050:
Quote:
Originally Posted by aron9forever
useless small optimizations
Quote:
Originally Posted by Black Rose
On a map that is 512x512x128 units you end up with 3,355,443,200,000 different "positions". To store each one of those positions individually in the variable "user_or" you need 12 terabytes of memory.
|
|
|
|