I don't know if I'm in the right area, sorry to the admins if I'm not
Good morning, I was playing with some AI's and asked them if they understood AMXX so I did some tests
some were good, others didn't even make sense, I don't know if anyone has already done this test but I found it interesting
I took the plugin "Advanced Bullet Damage" and asked him to optimize the code so what do you think?
New Code
PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <fakemeta_util>
#define PLUGIN "Advanced Bullet Damage"
#define VERSION "1.0"
#define AUTHOR "Sn!ff3r"
new g_type, g_enabled, g_recieved, bool:g_showrecieved, g_hudmsg1, g_hudmsg2
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_event("Damage", "on_damage", "b", "2!0", "3=0", "4!0")
register_event("HLTV", "on_new_round", "a", "1=0", "2=0")
g_type = register_cvar("amx_bulletdamage", "1")
g_recieved = register_cvar("amx_bulletdamage_recieved", "1")
g_hudmsg1 = CreateHudSyncObj()
g_hudmsg2 = CreateHudSyncObj()
}
public on_new_round()
{
g_enabled = get_pcvar_num(g_type)
g_showrecieved = get_pcvar_num(g_recieved)
}
public on_damage(id)
{
if (!g_enabled)
return
static attacker, damage
attacker = get_user_attacker(id)
damage = read_data(2)
if (g_showrecieved)
{
set_hudmessage(255, 0, 0, 0.45, 0.50, 2, 0.1, 4.0, 0.1, 0.1, -1)
ShowSyncHudMsg(id, g_hudmsg2, "%i^n", damage)
}
if (is_user_connected(attacker))
{
if (g_enabled == 1 || (g_enabled == 2 && fm_is_ent_visible(attacker, id)))
{
set_hudmessage(0, 100, 200, -1.0, 0.55, 2, 0.1, 4.0, 0.02, 0.02, -1)
ShowSyncHudMsg(attacker, g_hudmsg1, "%i^n", damage)
}
}
}
That's the change AI made
Code:
In this optimized version, the following improvements have been made:
- Removed unnecessary static declarations within the loop.
- The g_showrecieved variable is set directly by the value of the g_recieved cvar.
- Simplified the logic for checking the value of g_enabled using an if .
- Removed the unnecessary switch and replaced it with an if to check the value of g_enabled .
- Removed the is_user_connected(attacker) condition from the switch, as it is already checked in the outer if.
Be sure to test the optimized code and adjust it as necessary. If you have any more questions, feel free to ask!