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

Persistent Bot Named XYZ


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
doublejz
Junior Member
Join Date: Mar 2022
Old 04-06-2022 , 18:22   Persistent Bot Named XYZ
Reply With Quote #1

So long story short one of our old time members has past away. In tribute we've been trying to come up with a way to always have a bot named after him. Currently we have the bot quota set to 5 followed by the bot_add XYZ which works and keeps 6 bots in the server until players join.

When a player joins, there is that chance that bot XYZ gets kicked leaving the other bots in the server.

What we are trying to do is:
Determine if bot(s) are present and if so, check to see if one of them is named XYZ. If there isn't then we would rename a random bot to XYZ.

I might be going about this all wrong but this was my initial thought process. Any help is appreciated, as I'm trying to get back into scripting here after being absent for ~10+ years.

PHP Code:
public OnPluginStart()
{
    
HookEvent("round_start"OnRoundStartEventHookMode_PostNoCopy);
}

public 
OnRoundStart(Handle:event, const String:name[], bool:dontBroadcast)
{
    
decl String:t_name[64];
    
Format(t_namesizeof(t_name), "Name");
    new 
count 0;
    for (new 
1<= MaxClientsi++)
    {
    if(
IsClientInGame(i))
        {
        
count++;
        
GetClientName(it_namesizeof(t_name));
/*
*        Store somewhere?? array??? how??
*/
        
}
    }
/*    Test output to see if [BOT] was found and if any are named XYZ
*    If [BOT] was found but not XYZ, rename a bot with [BOT]-?????? to [BOT]-XYZ
*    sm_rename i "[Bot]-XYZ"
*/
       
else
       
       return 
Plugin_Handled;

doublejz is offline
xerox8521
Senior Member
Join Date: Sep 2011
Old 04-08-2022 , 06:29   Re: Persistent Bot Named XYZ
Reply With Quote #2

You could just save the Entity Reference in a variable and then check in OnClientDisconnect if the Entity Reference of the player that disconnected matches the saved variable and assign the name to a new bot.

Keep in mind that OnClientDisconnect is called on each map change.

Also in your for loop you are missing the IsFakeClient check.
xerox8521 is offline
`666
AlliedModders Donor
Join Date: Jan 2006
Old 04-08-2022 , 09:57   Re: Persistent Bot Named XYZ
Reply With Quote #3

Tested only in insurgency

PHP Code:
#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>
#include <sdktools>

int        g_iNameSet 0;
char    g_sOriginalName[32];

public 
Plugin myinfo = {
    
name        "bot_xyz",
    
author        "",
    
description    "",
    
version        "1.0",
    
url            ""
};

public 
void OnPluginStart() {
    
HookEvent("player_team"Event_PlayerTeamEventHookMode_Post);

    for (
int i 1<= MaxClientsi++) {
        if (!
IsClientInGame(i) || !IsFakeClient(i)) {
            continue;
        }
        
SetName(i);
        break;
    }
}

public 
void OnClientDisconnect(int client) {
    if (
client == g_iNameSet && client 0) {
        
int iHumans 0,
            
iBot 0;
    
        for (
int i 1<= MaxClientsi++) {
            if (!
IsClientInGame(i) || == g_iNameSet) {
                continue;
            }
            if (!
IsFakeClient(i)) {
                
iHumans++;
                continue;
            }

            
iBot i;

            if (
iHumans) {
                break;
            }
        }

        if (
iHumans && iBot) {
            
SetName(iBot);
        } else {
            
g_iNameSet 0;
        }
    }
}

public 
Action Event_PlayerTeam(Event event, const char[] namebool dontBroadcast) {
    if (
g_iNameSet) {
        return 
Plugin_Continue;
    }

    
int client GetClientOfUserId(event.GetInt("userid"));
    if (
client && IsClientInGame(client) && IsFakeClient(client)) {
        
SetName(client);
    }

    return 
Plugin_Continue;
}

void SetName(int client) {
    
//Remember original name so we can restore on plugin unload
    
GetClientName(clientg_sOriginalNamesizeof(g_sOriginalName));
    
SetClientName(client"[Bot]-XYZ");
    
g_iNameSet client;
}

public 
void OnPluginEnd() {
    if (
g_iNameSet && IsClientInGame(g_iNameSet) && IsFakeClient(g_iNameSet)) {
        
SetClientName(g_iNameSetg_sOriginalName);
    }

`666 is offline
doublejz
Junior Member
Join Date: Mar 2022
Old 04-08-2022 , 12:58   Re: Persistent Bot Named XYZ
Reply With Quote #4

Quote:
Originally Posted by `666 View Post
Tested only in insurgency

PHP Code:
#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>
#include <sdktools>

int        g_iNameSet 0;
char    g_sOriginalName[32];

public 
Plugin myinfo = {
    
name        "bot_xyz",
    
author        "",
    
description    "",
    
version        "1.0",
    
url            ""
};

public 
void OnPluginStart() {
    
HookEvent("player_team"Event_PlayerTeamEventHookMode_Post);

    for (
int i 1<= MaxClientsi++) {
        if (!
IsClientInGame(i) || !IsFakeClient(i)) {
            continue;
        }
        
SetName(i);
        break;
    }
}

public 
void OnClientDisconnect(int client) {
    if (
client == g_iNameSet && client 0) {
        
int iHumans 0,
            
iBot 0;
    
        for (
int i 1<= MaxClientsi++) {
            if (!
IsClientInGame(i) || == g_iNameSet) {
                continue;
            }
            if (!
IsFakeClient(i)) {
                
iHumans++;
                continue;
            }

            
iBot i;

            if (
iHumans) {
                break;
            }
        }

        if (
iHumans && iBot) {
            
SetName(iBot);
        } else {
            
g_iNameSet 0;
        }
    }
}

public 
Action Event_PlayerTeam(Event event, const char[] namebool dontBroadcast) {
    if (
g_iNameSet) {
        return 
Plugin_Continue;
    }

    
int client GetClientOfUserId(event.GetInt("userid"));
    if (
client && IsClientInGame(client) && IsFakeClient(client)) {
        
SetName(client);
    }

    return 
Plugin_Continue;
}

void SetName(int client) {
    
//Remember original name so we can restore on plugin unload
    
GetClientName(clientg_sOriginalNamesizeof(g_sOriginalName));
    
SetClientName(client"[Bot]-XYZ");
    
g_iNameSet client;
}

public 
void OnPluginEnd() {
    if (
g_iNameSet && IsClientInGame(g_iNameSet) && IsFakeClient(g_iNameSet)) {
        
SetClientName(g_iNameSetg_sOriginalName);
    }

This appears to work as expected. Thanks a bunch!
doublejz 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 10:20.


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