Hello!
An interesting question came to my mind, by at the first, I think need some information:
Some plugins use a PlayerPrethink hook (with engine or fakemeta), where is not recommended use some function - example is_user_alive -, because of cause lag with lot of engine call. (AMXMODX->METAMOD->ENGINE->METAMOD->AMXMODX).
I search it at this forum, and found some example to resolve this problem:
The optimalized way is to store in array the players alive status, and set or get it only when need.
I know that a long time ago, but let's see it again.
So, the problematic, and not optimalized code (bad):
PHP Code:
#include <amxmodx>
#include <fakemeta>
#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "Metal"
public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR);
register_forward(FM_PlayerPreThink, "fw_PlayerPreThink");
}
public fw_PlayerPreThink(id) {
if(!is_user_alive(id))
return FMRES_IGNORED;
// Do something ...
return FMRES_IGNORED;
}
And an example - i think - optimalized "bone-code", for these plugins:
PHP Code:
#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>
#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "Metal"
#define MAXPLAYERS 32
new bool:g_isconnected[MAXPLAYERS+1]; // player connected
new bool:g_isalive[MAXPLAYERS+1]; // player alive
public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR);
register_forward(FM_PlayerPreThink, "fw_PlayerPreThink");
RegisterHam(Ham_Killed, "player", "fw_PlayerKilled");
}
public client_putinserver(id) {
g_isconnected[id] = true;
g_isalive[id] = false;
}
public client_disconnect(id) {
g_isconnected[id] = false;
g_isalive[id] = false;
}
public fw_PlayerPreThink(id) {
if(!g_isalive[id])
return FMRES_IGNORED;
// Do something ...
return FMRES_IGNORED;
}
public fw_PlayerKilled(victim, attacker, shouldgib) {
g_isalive[victim] = false;
}
public fw_PlayerSpawn_Post(id) {
if(!g_isconnected[id])
return HAM_IGNORED;
g_isalive[id] = true;
// Do something ...
return HAM_IGNORED;
}
At this version the plugin keep in the first "layer" (AmxModX), and not call engine.
The question: Suppose that there are a lot of plugins (example 10#), which are use prethink with chechking player alive (and do something is true). If I make a plugin, which is effecting the bone-code, and implement a get native method what is return is user alive, then these plugins more efficiency?
Example (Bone implementation):
PHP Code:
#include <amxmodx>
#include <hamsandwich>
#define PLUGIN "PlayerStatus"
#define VERSION "1.0"
#define AUTHOR "Metal"
#pragma semicolon 1
#define MAXPLAYERS 32
new bool:g_isconnected[MAXPLAYERS+1]; // player connected
new bool:g_isalive[MAXPLAYERS+1]; // player alive
public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR);
RegisterHam(Ham_Killed, "player", "fw_PlayerKilled");
}
public plugin_natives() {
register_library("playerstatus");
register_native("playerstatus_is_user_alive", "native_ps_is_user_alive");
}
public native_ps_is_user_alive(plugin_id, num_params) {
new id = get_param(1);
return g_isalive[id];
}
public client_putinserver(id) {
g_isconnected[id] = true;
g_isalive[id] = false;
}
public client_disconnect(id) {
g_isconnected[id] = false;
g_isalive[id] = false;
}
public fw_PlayerKilled(victim, attacker, shouldgib) {
g_isalive[victim] = false;
}
public fw_PlayerSpawn_Post(id) {
if(g_isconnected[id])
g_isalive[id] = true;
return HAM_IGNORED;
}
Example (one of this plugin use):
PHP Code:
#include <amxmodx>
#include <fakemeta>
#include <playerstatus>
#define PLUGIN "New Plug-In - No 1."
#define VERSION "1.0"
#define AUTHOR "Metal"
public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR);
register_forward(FM_PlayerPreThink, "fw_PlayerPreThink");
}
public fw_PlayerPreThink(id) {
if (!playerstatus_is_user_alive(id)) {
return FMRES_IGNORED;
}
// Do something ...
return FMRES_IGNORED;
}
The Result is: less function hook each plugins.
What do you think about this? Useful? Unnecessary?