Raised This Month: $51 Target: $400
 12% 

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
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 09-24-2013 , 17:22   Re: Is Player Controlling a Bot?
Reply With Quote #9

Quote:
Originally Posted by 11530 View Post
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.
Aha, I thought spcomp might have ignored that little bit of sanity. Oh well, hand rolling what a C++ compiler would generate is the way.

PHP Code:
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;

The const there was doing absolutely nothing.
__________________
asherkin is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 09-24-2013 , 20:31   Re: Is Player Controlling a Bot?
Reply With Quote #10

Quote:
Originally Posted by asherkin View Post
Aha, I thought spcomp might have ignored that little bit of sanity. Oh well, hand rolling what a C++ compiler would generate is the way.

PHP Code:
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;

Haha, I didn't think to use two statics. Much better. But yea, in this case, I pretty much use const for every parameter I expect to be constant which I think is a valid model. Obviously more useful in larger functions, but it stops third-party clients changing a variable when they didn't intend to, and I have to assume the client could alter any part of the code e.g. take the following useless example:

PHP Code:
PrintToServer("%d"client == 1); 
If the client accidentally wrote:

PHP Code:
PrintToServer("%d"client 1); 
Then no error would be shown, unless the const qualifier had been used. I use it almost instinctively these days.
__________________

Last edited by 11530; 09-24-2013 at 20:32.
11530 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 00:22.


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