View Single Post
Re: Remove all bots when there is no human player on the server
Old
  (#3)
e-t172
French
 
e-t172's Avatar
 
Status: Offline
Posts: 3
Join Date: Apr 2007
Location: FRANCE
Idea Re: Remove all bots when there is no human player on the server - 24-04-2007

I hate wasting the CPU time of my machines so I coded it myself. Here is the patch :

Code:
diff -r -u podbot-orig/bot_globals.cpp podbot/bot_globals.cpp
--- podbot-orig/bot_globals.cpp    2007-03-24 14:58:48.000000000 +0100
+++ podbot/bot_globals.cpp    2007-04-24 20:00:07.000000000 +0200
@@ -151,6 +151,7 @@
 int g_i_botthink_index;  // KWo - 02.05.2006
 float g_fAutoKillTime = 0.0;  // KWo - 02.05.2006
 bool g_bAliveHumansPrevious = FALSE;  // KWo - 02.05.2006
+bool g_bHumansPrevious = true;
 int g_iUpdGlExpState = 0;  // KWo - 02.05.2006
 bool g_bRecalcKills = FALSE;  // KWo - 02.05.2006
 
@@ -180,6 +181,7 @@
 float g_f_cv_dangerfactor;         // KWo - 06.04.2006
 bool g_b_cv_chat;                  // KWo - 06.04.2006
 bool g_b_cv_autokill;              // KWo - 02.05.2006
+bool g_b_cv_autoremove;
 float g_f_cv_autokilldelay;        // KWo - 02.05.2006
 bool g_b_cv_ffa;                   // KWo - 04.10.2006
 bool g_b_cv_radio;                 // KWo - 03.02.2007
@@ -236,6 +238,7 @@
    "pb_" "aim_target_anticipation_ratio",  // KWo - 04.03.2006
    "pb_" "aim_type",
    "pb_" "autokill",        // KWo - 02.05.2006
+   "pb_" "autoremove",
    "pb_" "autokilldelay",   // KWo - 02.05.2006
    "pb_" "bot_join_team",   // KWo - 16.09.2006
    "pb_" "bot_quota_match", // KWo - 16.09.2006
diff -r -u podbot-orig/bot_globals.h podbot/bot_globals.h
--- podbot-orig/bot_globals.h    2007-03-24 14:58:40.000000000 +0100
+++ podbot/bot_globals.h    2007-04-24 19:55:14.000000000 +0200
@@ -144,6 +144,7 @@
 extern float g_f_cv_dangerfactor; // KWo 06.04.2006
 extern bool g_b_cv_chat; // KWo 06.04.2006
 extern bool g_b_cv_autokill;  // KWo - 02.05.2006
+extern bool g_b_cv_autoremove;
 extern float g_f_cv_autokilldelay;  // KWo - 02.05.2006
 extern bool g_b_cv_ffa;  // KWo - 04.10.2006
 extern bool g_b_cv_radio;  // KWo - 03.02.2007
@@ -168,6 +169,7 @@
 extern int g_i_botthink_index;      // KWo - 02.05.2006
 extern float g_fAutoKillTime;       // KWo - 02.05.2006
 extern bool g_bAliveHumansPrevious; // KWo - 02.05.2006
+extern bool g_bHumansPrevious;
 extern int g_iUpdGlExpState;        // KWo - 02.05.2006
 extern bool g_bRecalcKills;         // KWo - 02.05.2006
 
diff -r -u podbot-orig/bot.h podbot/bot.h
--- podbot-orig/bot.h    2007-03-26 22:04:12.000000000 +0200
+++ podbot/bot.h    2007-04-24 19:49:52.000000000 +0200
@@ -118,6 +118,7 @@
    PBCVAR_AIM_TARGET_ANTICIPATION_RATIO,  // KWo - 04.03.2006
    PBCVAR_AIM_TYPE,
    PBCVAR_AUTOKILL,          // KWo - 02.05.2006
+   PBCVAR_AUTOREMOVE,
    PBCVAR_AUTOKILLDELAY,     // KWo - 02.05.2006
    PBCVAR_BOTJOINTEAM,       // KWo - 16.09.2006
    PBCVAR_BOTQUOTAMATCH,     // KWo - 16.09.2006
diff -r -u podbot-orig/dll.cpp podbot/dll.cpp
--- podbot-orig/dll.cpp    2007-03-24 21:38:44.000000000 +0100
+++ podbot/dll.cpp    2007-04-24 20:26:25.000000000 +0200
@@ -41,6 +41,7 @@
    {const_cast<char *>(g_rgpszPbCvars[PBCVAR_AIM_TARGET_ANTICIPATION_RATIO]), "2.2", FCVAR_SERVER | FCVAR_EXTDLL},  // KWo - 04.03.2006
    {const_cast<char *>(g_rgpszPbCvars[PBCVAR_AIM_TYPE]),                 "4",    FCVAR_SERVER | FCVAR_EXTDLL},
    {const_cast<char *>(g_rgpszPbCvars[PBCVAR_AUTOKILL]),                 "0.0",  FCVAR_SERVER | FCVAR_EXTDLL},      // KWo - 02.05.2006
+   {const_cast<char *>(g_rgpszPbCvars[PBCVAR_AUTOREMOVE]),               "0.0",  FCVAR_SERVER | FCVAR_EXTDLL},
    {const_cast<char *>(g_rgpszPbCvars[PBCVAR_AUTOKILLDELAY]),            "45.0",  FCVAR_SERVER | FCVAR_EXTDLL},     // KWo - 02.05.2006
    {const_cast<char *>(g_rgpszPbCvars[PBCVAR_BOTJOINTEAM]),              "ANY",  FCVAR_SERVER | FCVAR_EXTDLL},      // KWo - 16.09.2006
    {const_cast<char *>(g_rgpszPbCvars[PBCVAR_BOTQUOTAMATCH]),            "0.0",  FCVAR_SERVER | FCVAR_EXTDLL},      // KWo - 16.09.2006
@@ -1761,6 +1762,9 @@
    bool bAliveHumans; // KWo - 02.05.2006
 
    g_bPathWasCalculated = FALSE; // KWo 24.03.2007
+   
+   char kickcmd[80];
+   int iSkill, iPersonality, iTeam, iClass;
 
    max_bots = g_i_cv_MaxBots;    // KWo - 06.04.2006
    min_bots = g_i_cv_MinBots;    // KWo - 06.04.2006
@@ -2095,6 +2099,95 @@
          UTIL_ServerPrint (g_szWaypointMessage);
       }
    }
+   
+   if (g_b_cv_autoremove && (g_bHumansPrevious) && num_humans <= 0)
+   {
+       g_bHumansPrevious = false;
+       
+       UTIL_HostPrint ("No more human players, removing bots\n");
+       
+       // taken from lines 2644-2667 (removebots) :
+       
+       // removebots is against BotsQuotaMatch - needs make it zero;
+      if (g_i_cv_BotsQuotaMatch > 0) // 16.09.2006
+      {
+         CVAR_SET_STRING (g_rgpszPbCvars[PBCVAR_BOTQUOTAMATCH], "0");
+         g_i_cv_BotsQuotaMatch = 0;
+      }
+
+      g_iPeoBotsKept = ((g_i_cv_MinBots == 0) && !g_bIsDedicatedServer)? 1 : g_i_cv_MinBots;
+
+      for (bot_index = 0; bot_index < gpGlobals->maxClients; bot_index++)
+      {
+         // Reset our Creation Tab if there are still Bots waiting to be spawned
+         memset (&BotCreateTab[bot_index], 0, sizeof (createbot_t));
+         botcreation_time = 0.0;
+
+         // is this slot used?
+         if (bots[bot_index].is_used && !FNullEnt (bots[bot_index].pEdict))
+         {
+            snprintf (kickcmd, sizeof (kickcmd), "kick \"%s\"\n", STRING (bots[bot_index].pEdict->v.netname));
+            SERVER_COMMAND (kickcmd); // kick the bot using (kick "name")
+            bots[bot_index].is_used = FALSE; // KWo - 11.02.2006
+         }
+      }
+   }
+   
+   if (g_b_cv_autoremove && (!g_bHumansPrevious) && num_humans > 0)
+   {
+       g_bHumansPrevious = true;
+       
+       UTIL_HostPrint ("A human is playing, filling server with bots\n");
+       
+       // taken from lines 2540-2585 (fillserver) :
+       
+       // fillserver is against BotsQuotaMatch - needs make it zero;
+      if (g_i_cv_BotsQuotaMatch > 0) // 16.09.2006
+      {
+         CVAR_SET_STRING (g_rgpszPbCvars[PBCVAR_BOTQUOTAMATCH], "0");
+         g_i_cv_BotsQuotaMatch = 0;
+      }
+      if ((arg1 != NULL) && (*arg1 != 0))
+         iSkill = atoi (arg1);
+      else
+         iSkill = 101;
+
+      if ((arg2 != NULL) && (*arg2 != 0))
+         iPersonality = atoi (arg2) - 1;
+      else
+         iPersonality = 5;
+
+      if ((arg3 != NULL) && (*arg3 != 0))
+      {
+         iTeam = atoi (arg3);
+         CVAR_SET_STRING ("mp_limitteams", "0");
+         CVAR_SET_STRING ("mp_autoteambalance", "0");
+      }
+      else
+         iTeam = 5;
+
+      if ((arg4 != NULL) && (*arg4 != 0))
+         iClass = atoi (arg4);
+      else
+         iClass = 5;
+
+      for (bot_index = 0; bot_index < gpGlobals->maxClients; bot_index++)
+      {
+         if (!BotCreateTab[bot_index].bNeedsCreation)
+         {
+            memset (&BotCreateTab[bot_index], 0, sizeof (createbot_t));
+            BotCreateTab[bot_index].bot_skill = iSkill;
+            BotCreateTab[bot_index].bot_personality = iPersonality;
+            BotCreateTab[bot_index].bot_team = iTeam;
+            BotCreateTab[bot_index].bot_class = iClass;
+            BotCreateTab[bot_index].bNeedsCreation = TRUE;
+         }
+      }
+      g_iPeoBotsKept = (max_bots == 0) ? gpGlobals->maxClients : max_bots;
+
+      if (botcreation_time == 0.0)
+         botcreation_time = gpGlobals->time;
+   }
 
 // autokill function here - KWo 02.05.2006
  
diff -r -u podbot-orig/util.cpp podbot/util.cpp
--- podbot-orig/util.cpp    2007-03-25 21:07:36.000000000 +0200
+++ podbot/util.cpp    2007-04-24 19:48:10.000000000 +0200
@@ -886,6 +886,18 @@
       if (CVAR_GET_FLOAT(g_rgpszPbCvars[PBCVAR_AUTOKILL]) > 0.f)
          g_b_cv_autokill = true;
    }
+   
+   g_b_cv_autoremove = false;
+   if (g_rgcvarPointer[PBCVAR_AUTOREMOVE])  // KWo - 13.10.2006
+   {
+      if (g_rgcvarPointer[PBCVAR_AUTOREMOVE]->value > 0.f)
+         g_b_cv_autoremove = true;
+   }
+   else
+   {
+      if (CVAR_GET_FLOAT(g_rgpszPbCvars[PBCVAR_AUTOREMOVE]) > 0.f)
+         g_b_cv_autoremove = true;
+   }
 
    if (g_rgcvarPointer[PBCVAR_AUTOKILLDELAY])  // KWo - 13.10.2006
       g_f_cv_autokilldelay = g_rgcvarPointer[PBCVAR_AUTOKILLDELAY]->value;
To use the new feature : pb_autoremove 1. It has been designed not to interfere too much with manually adding/removing the bots. Basically, it works by doing "removebots" when there is no more human players on the server, and by doing "fillserver" when there is human players on the server.
Hope it'll be useful to someone

Last edited by e-t172; 24-04-2007 at 23:14..
  
Reply With Quote