botmanager.sp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #pragma semicolon 1
  2. #include <sourcemod>
  3. #include <sdktools>
  4. #include <tf2>
  5. #include <tf2_stocks>
  6. #pragma newdecls required
  7. #define PLUGIN_VERSION "1.3.7"
  8. public Plugin myinfo = {
  9. name = "[TF2] Bot Manager",
  10. author = "nosoop (forked from Dr. McKay)",
  11. description = "Allows for customization of TFBots",
  12. version = PLUGIN_VERSION,
  13. url = "http://csrd.science"
  14. };
  15. ConVar cvarBotQuota;
  16. ConVar cvarBotJoinAfterPlayer;
  17. ConVar cvarGameLogic;
  18. ConVar cvarSupportedMap;
  19. ConVar cvarOnTeamsOnly;
  20. ConVar tf_bot_quota;
  21. ArrayList joiningBots; // userid
  22. Handle fwdBotAdd, fwdBotKick;
  23. public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max) {
  24. char game[64];
  25. GetGameFolderName(game, sizeof(game));
  26. if(!StrEqual(game, "tf")) {
  27. strcopy(error, err_max, "Bot Manager only works on Team Fortress 2");
  28. return APLRes_Failure;
  29. }
  30. RegPluginLibrary("botmanager");
  31. return APLRes_Success;
  32. }
  33. public void OnPluginStart() {
  34. cvarBotQuota = CreateConVar("sm_bot_quota", "0", "Number of players to keep in the server");
  35. cvarBotJoinAfterPlayer = CreateConVar("sm_bot_join_after_player", "1",
  36. "If nonzero, bots wait until a player joins before entering the game.");
  37. cvarGameLogic = CreateConVar("sm_bot_game_logic", "1",
  38. "0 = use plugin logic when assigning bots, 1 = use game logic");
  39. cvarSupportedMap = CreateConVar("sm_bot_supported_map", "1",
  40. "If nonzero, bots will only be added on maps that have nav files");
  41. cvarOnTeamsOnly = CreateConVar("sm_bot_on_team_only", "1",
  42. "If nonzero, players will only be considered \"in-game\" if they're on a team for "
  43. ... "purposes of determining the bot count");
  44. tf_bot_quota = FindConVar("tf_bot_quota");
  45. HookEvent("player_connect_client", Event_PlayerConnect, EventHookMode_Pre);
  46. HookEvent("player_disconnect", Event_PlayerDisconnect, EventHookMode_Pre);
  47. HookEvent("player_team", Event_PlayerTeam, EventHookMode_Pre);
  48. joiningBots = new ArrayList();
  49. fwdBotAdd = CreateGlobalForward("Bot_OnBotAdd", ET_Single, Param_CellByRef, Param_CellByRef,
  50. Param_CellByRef, Param_String);
  51. fwdBotKick = CreateGlobalForward("Bot_OnBotKick", ET_Single, Param_CellByRef);
  52. ConVar cvar = FindConVar("tf_bot_quota_mode");
  53. cvar.SetString("normal");
  54. cvar.AddChangeHook(OnConVarChange);
  55. cvar = FindConVar("tf_bot_join_after_player");
  56. cvar.IntValue = 0;
  57. cvar.AddChangeHook(OnConVarChange);
  58. }
  59. public void OnConVarChange(ConVar convar, const char[] oldValue, const char[] newValue) {
  60. char name[64];
  61. convar.GetName(name, sizeof(name));
  62. if (StrEqual(name, "tf_bot_quota_mode")) {
  63. LogMessage("tf_bot_quota_mode cannot be changed while Bot Manager is running. tf_bot_quota_mode set to \"normal\".");
  64. SetConVarString(convar, "normal");
  65. } else if (StrEqual(name, "tf_bot_join_after_player")) {
  66. LogMessage("tf_bot_join_after_player cannot be changed while Bot Manager is running. tf_bot_join_after_player set to \"0\". Use sm_bot_join_after_player for similar functionality.");
  67. SetConVarInt(convar, 0);
  68. }
  69. }
  70. public void OnConfigsExecuted() {
  71. char buffer[64];
  72. GetCurrentMap(buffer, sizeof(buffer));
  73. Format(buffer, sizeof(buffer), "maps/%s.nav", buffer);
  74. if (FileExists(buffer, true) || !cvarSupportedMap.BoolValue) {
  75. CreateTimer(0.1, Timer_CheckBotNum, _, TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE);
  76. } else {
  77. LogMessage("Bots are not supported on this map. Bot Manager disabled.");
  78. }
  79. }
  80. public void OnMapEnd() {
  81. tf_bot_quota.IntValue = 0; // Prevents an issue that happens at mapchange
  82. }
  83. public Action Timer_CheckBotNum(Handle timer) {
  84. if (GameRules_GetProp("m_nGameType") == 4) {
  85. // suspend bot checks during an active arena round
  86. switch (GameRules_GetRoundState()) {
  87. case RoundState_Stalemate, RoundState_TeamWin: {
  88. return Plugin_Continue;
  89. }
  90. }
  91. }
  92. int clients = GetValidClientCount();
  93. int actual = GetValidClientCount(false);
  94. int bots = GetBotCount();
  95. int realClients = clients - bots;
  96. if (realClients == 0 && cvarBotJoinAfterPlayer.BoolValue) {
  97. if(bots > 0) {
  98. RemoveBot();
  99. }
  100. return Plugin_Continue;
  101. }
  102. if (cvarBotQuota.IntValue >= MaxClients) {
  103. LogMessage("sm_bot_quota cannot be greater than or equal to maxplayers. Setting sm_bot_quota to \"%d\".", MaxClients - 1);
  104. cvarBotQuota.IntValue = MaxClients - 1;
  105. }
  106. if (clients < cvarBotQuota.IntValue && actual < (MaxClients - 1)) {
  107. AddBot();
  108. } else if (clients > cvarBotQuota.IntValue && bots > 0) {
  109. RemoveBot();
  110. }
  111. return Plugin_Continue;
  112. }
  113. int GetValidClientCount(bool excludeTeamsOnly = true) {
  114. int count = 0;
  115. for (int i = 1; i <= MaxClients; i++) {
  116. if (!IsClientInGame(i) || IsClientSourceTV(i) || IsClientReplay(i)) {
  117. continue;
  118. }
  119. // arena mode hacks
  120. if (excludeTeamsOnly && cvarOnTeamsOnly.BoolValue && (GetClientTeam(i) <= 1
  121. || (GameRules_GetProp("m_nGameType") == 4 && GetEntProp(i, Prop_Send, "m_bArenaSpectator")) )) {
  122. continue;
  123. }
  124. count++;
  125. }
  126. return count;
  127. }
  128. int GetBotCount() {
  129. int count = 0;
  130. for (int i = 1; i <= MaxClients; i++) {
  131. if (IsClientInGame(i) && !IsClientSourceTV(i) && !IsClientReplay(i) && IsFakeClient(i)) {
  132. count++;
  133. }
  134. }
  135. return count;
  136. }
  137. void AddBot() {
  138. TFTeam team = TFTeam_Unassigned;
  139. if (!cvarGameLogic.BoolValue) {
  140. if (GetTeamClientCount(2) < GetTeamClientCount(3)) {
  141. team = TFTeam_Red;
  142. } else {
  143. team = TFTeam_Blue;
  144. }
  145. }
  146. TFClassType class = TFClass_Unknown;
  147. if (!cvarGameLogic.BoolValue) {
  148. int numClass[TFClassType];
  149. GetClassCounts(team, numClass);
  150. if(!numClass[TFClass_Medic]) {
  151. class = TFClass_Medic;
  152. } else {
  153. static TFClassType iter[] = { TFClass_Scout, TFClass_Soldier, TFClass_Pyro,
  154. TFClass_DemoMan, TFClass_Heavy, TFClass_Engineer, TFClass_Sniper,
  155. TFClass_Spy };
  156. TFClassType lowest = TFClass_Scout;
  157. for (int i = 1; i < sizeof(iter); i++) {
  158. if (numClass[ iter[i] ] < numClass[lowest]) {
  159. lowest = iter[i];
  160. }
  161. }
  162. class = lowest;
  163. }
  164. }
  165. int difficulty = -1;
  166. char name[MAX_NAME_LENGTH];
  167. Call_StartForward(fwdBotAdd);
  168. Call_PushCellRef(class);
  169. Call_PushCellRef(team);
  170. Call_PushCellRef(difficulty);
  171. Call_PushStringEx(name, sizeof(name), SM_PARAM_STRING_UTF8|SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
  172. Call_Finish();
  173. char strDifficulty[16], strTeam[16], strClass[16];
  174. switch (difficulty) {
  175. case 0: strDifficulty = "easy";
  176. case 1: strDifficulty = "normal";
  177. case 2: strDifficulty = "hard";
  178. case 3: strDifficulty = "expert";
  179. }
  180. switch (team) {
  181. case TFTeam_Red: strTeam = "red";
  182. case TFTeam_Blue: strTeam = "blue";
  183. }
  184. switch (class) {
  185. case TFClass_Scout: strClass = "Scout";
  186. case TFClass_Soldier: strClass = "Soldier";
  187. case TFClass_Pyro: strClass = "Pyro";
  188. case TFClass_DemoMan: strClass = "Demoman";
  189. case TFClass_Heavy: strClass = "HeavyWeapons";
  190. case TFClass_Engineer: strClass = "Engineer";
  191. case TFClass_Medic: strClass = "Medic";
  192. case TFClass_Sniper: strClass = "Sniper";
  193. case TFClass_Spy: strClass = "Spy";
  194. }
  195. char quotedName[MAX_NAME_LENGTH + 2];
  196. ReplaceString(name, sizeof(name), "\"", "");
  197. if (strlen(name)) {
  198. Format(quotedName, sizeof(quotedName), "\"%s\"", name);
  199. }
  200. ServerCommand("tf_bot_add %s %s %s %s", strDifficulty, strTeam, strClass, quotedName); // count class team difficulty name (any order)
  201. }
  202. void GetClassCounts(TFTeam team, int numClass[TFClassType]) {
  203. for (int i = 1; i <= MaxClients; i++) {
  204. if (!IsClientInGame(i) || TF2_GetClientTeam(i) != team) {
  205. continue;
  206. }
  207. numClass[ TF2_GetPlayerClass(i) ]++;
  208. }
  209. }
  210. void RemoveBot() {
  211. int teamToKick;
  212. if (GetTeamClientCount(2) > GetTeamClientCount(3)) {
  213. teamToKick = 2;
  214. } else if (GetTeamClientCount(2) < GetTeamClientCount(3)) {
  215. teamToKick = 3;
  216. } else {
  217. teamToKick = GetRandomInt(2, 3);
  218. }
  219. int botKickCandidates[MAXPLAYERS + 1], numBots;
  220. for (int i = 1; i <= MaxClients; i++) {
  221. if (IsClientConnected(i) && !IsClientSourceTV(i) && !IsClientReplay(i) &&
  222. IsFakeClient(i) && GetClientTeam(i) == teamToKick) {
  223. botKickCandidates[numBots++] = i;
  224. }
  225. }
  226. if (!numBots) {
  227. return;
  228. }
  229. int bot = botKickCandidates[ GetRandomInt(0, numBots - 1) ];
  230. Call_StartForward(fwdBotKick);
  231. Call_PushCellRef(bot);
  232. Call_Finish();
  233. ServerCommand("tf_bot_kick \"%N\"", bot);
  234. }
  235. public Action Event_PlayerConnect(Event event, const char[] name, bool dontBroadcast) {
  236. if (event.GetBool("bot")) {
  237. joiningBots.Push(event.GetInt("userid"));
  238. event.BroadcastDisabled = true;
  239. // more arena hacks
  240. ServerCommand("namelockid %d 1", event.GetInt("userid"));
  241. }
  242. return Plugin_Continue;
  243. }
  244. public Action Event_PlayerDisconnect(Event event, const char[] name, bool dontBroadcast) {
  245. int client = GetClientOfUserId(event.GetInt("userid"));
  246. if (!client) {
  247. return Plugin_Continue;
  248. }
  249. if (IsFakeClient(client)) {
  250. event.BroadcastDisabled = true;
  251. PrintToChatAll("\x01BOT \x07%06X%N \x01has left the game", GetTeamColor(GetClientTeam(client)), client);
  252. }
  253. return Plugin_Continue;
  254. }
  255. public Action Event_PlayerTeam(Event event, const char[] name, bool dontBroadcast) {
  256. int client = GetClientOfUserId(event.GetInt("userid"));
  257. if (!client) {
  258. return Plugin_Continue;
  259. }
  260. if (IsFakeClient(client)) {
  261. event.BroadcastDisabled = true;
  262. int pos;
  263. if ((pos = joiningBots.FindValue(GetClientUserId(client))) != -1) {
  264. joiningBots.Erase(pos);
  265. PrintToServer("BOT %N has joined the game", client);
  266. PrintToChatAll("\x01BOT \x07%06X%N \x01has joined the game",
  267. GetTeamColor(event.GetInt("team")), client);
  268. }
  269. }
  270. return Plugin_Continue;
  271. }
  272. int GetTeamColor(int team) {
  273. switch(team) {
  274. case 1: {
  275. return 0xCCCCCC;
  276. }
  277. case 2: {
  278. return 0xFF4040;
  279. }
  280. case 3: {
  281. return 0x99CCFF;
  282. }
  283. }
  284. return 0x3EFF3E;
  285. }