botmanager.sp 9.7 KB

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