AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   Is Player Controlling a Bot? (https://forums.alliedmods.net/showthread.php?t=226693)

TnTSCS 09-21-2013 18:12

Is Player Controlling a Bot?
 
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



asherkin 09-21-2013 18:39

Re: Is Player Controlling a Bot?
 
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.

TnTSCS 09-23-2013 10:14

Re: Is Player Controlling a Bot?
 
Adjusted the code, thanks for the heads up asherkin :)

11530 09-23-2013 18:06

Re: Is Player Controlling a Bot?
 
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. :bee:
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);



Powerlord 09-24-2013 09:13

Re: Is Player Controlling a Bot?
 
Quote:

Originally Posted by 11530 (Post 2039776)
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. :bee:
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?

11530 09-24-2013 14:45

Re: Is Player Controlling a Bot?
 
Quote:

Originally Posted by Powerlord (Post 2039971)
...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.

asherkin 09-24-2013 14:49

Re: Is Player Controlling a Bot?
 
Quote:

Originally Posted by 11530 (Post 2040122)
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;



11530 09-24-2013 16:49

Re: Is Player Controlling a Bot?
 
Quote:

Originally Posted by asherkin (Post 2040124)
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.

asherkin 09-24-2013 17:22

Re: Is Player Controlling a Bot?
 
Quote:

Originally Posted by 11530 (Post 2040157)
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.

11530 09-24-2013 20:31

Re: Is Player Controlling a Bot?
 
Quote:

Originally Posted by asherkin (Post 2040171)
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. :bacon:


All times are GMT -4. The time now is 18:35.

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