Raised This Month: $45 Target: $400
 11% 

Custom is_user_alive library/module/plugin


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Diwat[#26]-Metal
Junior Member
Join Date: Oct 2009
Old 01-20-2014 , 08: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 08:32.
Diwat[#26]-Metal is offline
simanovich
AlliedModders Donor
Join Date: Jun 2012
Location: Israel
Old 01-20-2014 , 08:39   Re: Custom is_user_alive library/module/plugin
Reply With Quote #2

Useless shit.
Better to use the build-in native
__________________

I'm awesome, and I really like cookies.
simanovich is offline
YamiKaitou
Has a lovely bunch of coconuts
Join Date: Apr 2006
Location: Texas
Old 01-20-2014 , 10:36   Re: Custom is_user_alive library/module/plugin
Reply With Quote #3

Not error proof. Using the native does not cause any issue and is error proof.
__________________
ProjectYami Laboratories

I do not browse the forums regularly anymore. If you need me for anything (asking questions or anything else), then PM me (be descriptive in your PM, message containing only a link to a thread will be ignored).
YamiKaitou is offline
Diwat[#26]-Metal
Junior Member
Join Date: Oct 2009
Old 01-21-2014 , 16:27   Re: Custom is_user_alive library/module/plugin
Reply With Quote #4

I do not quite understand this. Can you explain?
__________________
work, work, work
Diwat[#26]-Metal is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-21-2014 , 18:13   Re: Custom is_user_alive library/module/plugin
Reply With Quote #5

Quote:
Originally Posted by Diwat[#26]-Metal View Post
I do not quite understand this. Can you explain?
Your method is not good. Use is_user_alive().
__________________
fysiks is offline
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 01-21-2014 , 21:02   Re: Custom is_user_alive library/module/plugin
Reply With Quote #6

Replacing a native with a dynamic native won't do you any good.
__________________

Last edited by hleV; 01-21-2014 at 21:03.
hleV is offline
Diwat[#26]-Metal
Junior Member
Join Date: Oct 2009
Old 01-24-2014 , 09:24   Re: Custom is_user_alive library/module/plugin
Reply With Quote #7

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
__________________
work, work, work

Last edited by Diwat[#26]-Metal; 01-24-2014 at 09:25.
Diwat[#26]-Metal is offline
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 01-24-2014 , 09:49   Re: Custom is_user_alive library/module/plugin
Reply With Quote #8

Quote:
Originally Posted by Diwat[#26]-Metal View Post
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.
__________________

Last edited by hleV; 01-24-2014 at 09:53.
hleV is offline
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 01-24-2014 , 12:15   Re: Custom is_user_alive library/module/plugin
Reply With Quote #9

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.
__________________
Currently busy working on a very large scale anime database project.
Backstabnoob is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 01-25-2014 , 07:57   Re: Custom is_user_alive library/module/plugin
Reply With Quote #10

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.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Reply


Thread Tools
Display Modes

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 11:35.


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