Raised This Month: $ Target: $400
 0% 

Custom is_user_alive library/module/plugin


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
Diwat[#26]-Metal
Junior Member
Join Date: Oct 2009
Old 01-20-2014 , 07:21   Custom is_user_alive library/module/plugin
Reply With Quote #1

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?

Last edited by Diwat[#26]-Metal; 01-20-2014 at 07:32.
Diwat[#26]-Metal is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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