botmanager.sp 11 KB

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