AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Custom is_user_alive library/module/plugin (https://forums.alliedmods.net/showthread.php?t=233781)

Diwat[#26]-Metal 01-20-2014 07:21

Custom is_user_alive library/module/plugin
 
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(PLUGINVERSIONAUTHOR);

    
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(PLUGINVERSIONAUTHOR);

    
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(victimattackershouldgib) {
    
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(PLUGINVERSIONAUTHOR);

    
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_idnum_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(victimattackershouldgib) {
    
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(PLUGINVERSIONAUTHOR);

    
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?

simanovich 01-20-2014 07:39

Re: Custom is_user_alive library/module/plugin
 
Useless shit.
Better to use the build-in native

YamiKaitou 01-20-2014 09:36

Re: Custom is_user_alive library/module/plugin
 
Not error proof. Using the native does not cause any issue and is error proof.

Diwat[#26]-Metal 01-21-2014 15:27

Re: Custom is_user_alive library/module/plugin
 
I do not quite understand this. Can you explain?

fysiks 01-21-2014 17:13

Re: Custom is_user_alive library/module/plugin
 
Quote:

Originally Posted by Diwat[#26]-Metal (Post 2089173)
I do not quite understand this. Can you explain?

Your method is not good. Use is_user_alive().

hleV 01-21-2014 20:02

Re: Custom is_user_alive library/module/plugin
 
Replacing a native with a dynamic native won't do you any good.

Diwat[#26]-Metal 01-24-2014 08:24

Re: Custom is_user_alive library/module/plugin
 
Okey, yes, but suppose that there is no error in this module plan. Faster than I use always is_user_alive native?
I found the post about it - included is_user_bot function - by MeRcyLeZZ:

http://forums.alliedmods.net/showpos...91&postcount=1

So, stay optimized if I replace another custom native function? I think must be

hleV 01-24-2014 08:49

Re: Custom is_user_alive library/module/plugin
 
Quote:

Originally Posted by Diwat[#26]-Metal (Post 2090328)
Okey, yes, but suppose that there is no error in this module plan. Faster than I use always is_user_alive native?

is_user_alive() contacts the module to return the value of a boolean variable (g_players[id].alive, I think) to the plugin.
Your dynamic native creates a new native, it then contacts the module, the module contacts the other plugin to get the value, and then that value goes back to your plugin.

It's not faster. In fact, it should be slower.
If you need a slight performance boost in your plugins, you'll have to create new variables in each one of them as opposed to using just one plugin as an API. You should only do that in big plugins that use very often-called forwards, though.

Backstabnoob 01-24-2014 11:15

Re: Custom is_user_alive library/module/plugin
 
The native is fast as hell and there is really no downside to using it. The only REALLY FUCKING SMALL improvement would be to cache the is_user_alive value, but that can cause issues and other headaches later.

ConnorMcLeod 01-25-2014 06:57

Re: Custom is_user_alive library/module/plugin
 
cache alive status inside a plugin is not a good idea. As Yami said, it is not error proof, some plugins/maps can kill players without triggering things that are caught by your plugin.
Doing this in a extra plugin in order to provide a dynamic native is even worth, because you gonna add some calls between plugins and amxx.

Also, is_user_alive doesn't triggering communication between amxx and metamod.


About prethink, better to use engine module forward client_PreThink, unless you want to post hook it or unless you don't want to hook it all the time.


All times are GMT -4. The time now is 10:11.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.