Raised This Month: $ Target: $400
 0% 

Is Player Controlling a Bot?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
TnTSCS
AlliedModders Donor
Join Date: Oct 2010
Location: Undisclosed...
Old 09-21-2013 , 18:12   Is Player Controlling a Bot?
Reply With Quote #1

Thought I posted this before in here, but I think I've only posted it in the CSS Bank plugin thread... Anyways, for CS:GO (and maybe others), here's a good way to check if a client is controlling a bot (thanks asherkin):

bad way

PHP Code:
/**
 * Check if a player is controlling a bot
 * @param    client    Player's client index
 * @return    True if player is controlling a bot, false otherwise
 */
bool:IsPlayerControllingBot(client)  

    static 
bool:hasChecked false
    static 
bool:isAvailable false
    if (!
hasChecked) { 
        
isAvailable FindSendPropOffs("CBasePlayer""m_bIsControllingBot") != -1
        
hasChecked true
    } 

    return 
isAvailable && GetEntProp(clientProp_Send"m_bIsControllingBot") == 1

__________________
View my Plugins | Donate

Last edited by TnTSCS; 09-24-2013 at 17:32.
TnTSCS is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 09-21-2013 , 18:39   Re: Is Player Controlling a Bot?
Reply With Quote #2

Just use GetEntProp, what you're doing is dangerous if a non-client index is ever passed to that function by mistake, and gives you no benefit at all.
__________________
asherkin is offline
TnTSCS
AlliedModders Donor
Join Date: Oct 2010
Location: Undisclosed...
Old 09-23-2013 , 10:14   Re: Is Player Controlling a Bot?
Reply With Quote #3

Adjusted the code, thanks for the heads up asherkin
__________________
View my Plugins | Donate
TnTSCS is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 09-23-2013 , 18:06   Re: Is Player Controlling a Bot?
Reply With Quote #4

Might still be prudent to check if the netprop exists in the first place, at the expense of using OnPluginStart too (unless you don't mind checking it's availability every use). Also handles FindSendPropInfo returning 0 too, though this code could be used for any boolean netprop, mind you.
PHP Code:
new bool:g_bIsAvailable;

public 
OnPluginStart()
{
    
g_bIsAvailable = (FindSendPropInfo("CBasePlayer""m_bIsControllingBot") > 0);
}

bool:IsPlayerControllingBot(const client
{
    return (
g_bIsAvailable && GetEntProp(clientProp_Send"m_bIsControllingBot") == 1);

__________________

Last edited by 11530; 09-23-2013 at 18:32.
11530 is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 09-24-2013 , 09:13   Re: Is Player Controlling a Bot?
Reply With Quote #5

Quote:
Originally Posted by 11530 View Post
Might still be prudent to check if the netprop exists in the first place, at the expense of using OnPluginStart too (unless you don't mind checking it's availability every use). Also handles FindSendPropInfo returning 0 too, though this code could be used for any boolean netprop, mind you.
PHP Code:
new bool:g_bIsAvailable;

public 
OnPluginStart()
{
    
g_bIsAvailable = (FindSendPropInfo("CBasePlayer""m_bIsControllingBot") > 0);
}

bool:IsPlayerControllingBot(const client
{
    return (
g_bIsAvailable && GetEntProp(clientProp_Send"m_bIsControllingBot") == 1);

...in a game-specific plugin?
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 09-24-2013 , 14:45   Re: Is Player Controlling a Bot?
Reply With Quote #6

Quote:
Originally Posted by Powerlord View Post
...in a game-specific plugin?
No. For example, any plugin which could be labelled as [ANY] whilst still having netprops/datamaps which might not necessarily work in a certain game.
__________________
11530 is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 09-24-2013 , 14:49   Re: Is Player Controlling a Bot?
Reply With Quote #7

Quote:
Originally Posted by 11530 View Post
No. For example, any plugin which could be labelled as [ANY] whilst still having netprops/datamaps which might not necessarily work in a certain game.
The correct way to implement that is with |static| anyway.
FindSendPropOffs is also more performant for this case.
const-qualifying the param also means nothing.

PHP Code:
bool:IsPlayerControllingBot(client
{
    static 
bIsAvailable FindSendPropOffs("CBasePlayer""m_bIsControllingBot") != -1;
    return 
bIsAvailable && GetEntProp(clientProp_Send"m_bIsControllingBot") == 1;

__________________

Last edited by asherkin; 09-24-2013 at 14:50.
asherkin is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 09-24-2013 , 16:49   Re: Is Player Controlling a Bot?
Reply With Quote #8

Quote:
Originally Posted by asherkin View Post
The correct way to implement that is with |static| anyway.
FindSendPropOffs is also more performant for this case.
const-qualifying the param also means nothing.

PHP Code:
bool:IsPlayerControllingBot(client
{
    static 
bIsAvailable FindSendPropOffs("CBasePlayer""m_bIsControllingBot") != -1;
    return 
bIsAvailable && GetEntProp(clientProp_Send"m_bIsControllingBot") == 1;

I too tried using a static for it yesterday, as I like keeping scope limited, but the compiler throws up an error if you use it, stating "error 008: must be a constant expression; assumed zero". That's why I had to force it up to a generic global.

Using const is just good design practice. Stops people from making inadvertent mistakes.
__________________

Last edited by 11530; 09-24-2013 at 16:51.
11530 is offline
TnTSCS
AlliedModders Donor
Join Date: Oct 2010
Location: Undisclosed...
Old 09-25-2013 , 08:30   Re: Is Player Controlling a Bot?
Reply With Quote #9

I'm glad my ignorance has sparked all of this... I guess I didn't really need to post the method, but this thread has been good for me to read

Thanks for responding and the free education everyone
__________________
View my Plugins | Donate
TnTSCS is offline
PlayBoy31
Senior Member
Join Date: May 2011
Location: into the void
Old 09-25-2013 , 13:13   Re: Is Player Controlling a Bot?
Reply With Quote #10

i think its better to use HookEvent("bot_takeover", Ev_bot_takeover);

then you will not have problem with findsendprop etc....
PlayBoy31 is offline
Reply



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 18:34.


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