botmanager.sp 9.7 KB

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