botmanager.sp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #pragma semicolon 1
  2. #include <sourcemod>
  3. #include <sdktools>
  4. #include <tf2>
  5. #include <tf2_stocks>
  6. #define PLUGIN_VERSION "1.3.1"
  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 && GameRules_GetRoundState() == RoundState_Stalemate) {
  79. // suspend bot checks during an active arena round
  80. return Plugin_Continue;
  81. }
  82. new clients = GetValidClientCount();
  83. new actual = GetValidClientCount(false);
  84. new bots = GetBotCount();
  85. new realClients = clients - bots;
  86. if(realClients == 0 && GetConVarBool(cvarBotJoinAfterPlayer)) {
  87. if(bots > 0) {
  88. RemoveBot();
  89. }
  90. return Plugin_Continue;
  91. }
  92. if(GetConVarInt(cvarBotQuota) >= MaxClients) {
  93. LogMessage("sm_bot_quota cannot be greater than or equal to maxplayers. Setting sm_bot_quota to \"%d\".", MaxClients - 1);
  94. SetConVarInt(cvarBotQuota, MaxClients - 1);
  95. }
  96. if(clients < GetConVarInt(cvarBotQuota) && actual < (MaxClients - 1)) {
  97. AddBot();
  98. } else if(clients > GetConVarInt(cvarBotQuota) && bots > 0) {
  99. RemoveBot();
  100. }
  101. return Plugin_Continue;
  102. }
  103. GetValidClientCount(bool:excludeTeamsOnly = true) {
  104. new count = 0;
  105. for(new i = 1; i <= MaxClients; i++) {
  106. if(!IsClientInGame(i) || IsClientSourceTV(i) || IsClientReplay(i)) {
  107. continue;
  108. }
  109. // arena mode hacks
  110. if(excludeTeamsOnly && GetConVarBool(cvarOnTeamsOnly) && (GetClientTeam(i) <= 1 || (GameRules_GetProp("m_nGameType") == 4 && GetEntProp(i, Prop_Send, "m_bArenaSpectator")) )) {
  111. continue;
  112. }
  113. count++;
  114. }
  115. return count;
  116. }
  117. GetBotCount() {
  118. new count = 0;
  119. for(new i = 1; i <= MaxClients; i++) {
  120. if(IsClientInGame(i) && !IsClientSourceTV(i) && !IsClientReplay(i) && IsFakeClient(i)) {
  121. count++;
  122. }
  123. }
  124. return count;
  125. }
  126. AddBot() {
  127. new TFTeam:team = TFTeam_Unassigned;
  128. if(!GetConVarBool(cvarGameLogic)) {
  129. if(GetTeamClientCount(2) < GetTeamClientCount(3)) {
  130. team = TFTeam_Red;
  131. } else {
  132. team = TFTeam_Blue;
  133. }
  134. }
  135. new TFClassType:class = TFClass_Unknown;
  136. if(!GetConVarBool(cvarGameLogic)) {
  137. new scout, soldier, pyro, demoman, heavy, engineer, medic, sniper, spy;
  138. GetClassCounts(team, scout, soldier, pyro, demoman, heavy, engineer, medic, sniper, spy);
  139. if(medic == 0) {
  140. class = TFClass_Medic;
  141. } else {
  142. new least = scout;
  143. class = TFClass_Scout;
  144. if(soldier < least) {
  145. least = soldier;
  146. class = TFClass_Soldier;
  147. }
  148. if(pyro < least) {
  149. least = pyro;
  150. class = TFClass_Pyro;
  151. }
  152. if(demoman < least) {
  153. least = demoman;
  154. class = TFClass_DemoMan;
  155. }
  156. if(heavy < least) {
  157. least = heavy;
  158. class = TFClass_Heavy;
  159. }
  160. if(engineer < least) {
  161. least = engineer;
  162. class = TFClass_Engineer;
  163. }
  164. if(sniper < least) {
  165. least = sniper;
  166. class = TFClass_Sniper;
  167. }
  168. if(spy < least) {
  169. least = spy;
  170. class = TFClass_Spy;
  171. }
  172. }
  173. }
  174. new difficulty = -1;
  175. new String:name[MAX_NAME_LENGTH];
  176. Call_StartForward(fwdBotAdd);
  177. Call_PushCellRef(class);
  178. Call_PushCellRef(team);
  179. Call_PushCellRef(difficulty);
  180. Call_PushStringEx(name, sizeof(name), SM_PARAM_STRING_UTF8|SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
  181. Call_Finish();
  182. decl String:strDifficulty[16], String:strTeam[16], String:strClass[16];
  183. switch(difficulty) {
  184. case 0: Format(strDifficulty, sizeof(strDifficulty), "easy");
  185. case 1: Format(strDifficulty, sizeof(strDifficulty), "normal");
  186. case 2: Format(strDifficulty, sizeof(strDifficulty), "hard");
  187. case 3: Format(strDifficulty, sizeof(strDifficulty), "expert");
  188. default: Format(strDifficulty, sizeof(strDifficulty), "");
  189. }
  190. switch(team) {
  191. case TFTeam_Red: Format(strTeam, sizeof(strTeam), "red");
  192. case TFTeam_Blue: Format(strTeam, sizeof(strTeam), "blue");
  193. default: Format(strTeam, sizeof(strTeam), "");
  194. }
  195. switch(class) {
  196. case TFClass_Scout: Format(strClass, sizeof(strClass), "Scout");
  197. case TFClass_Soldier: Format(strClass, sizeof(strClass), "Soldier");
  198. case TFClass_Pyro: Format(strClass, sizeof(strClass), "Pyro");
  199. case TFClass_DemoMan: Format(strClass, sizeof(strClass), "Demoman");
  200. case TFClass_Heavy: Format(strClass, sizeof(strClass), "HeavyWeapons");
  201. case TFClass_Engineer: Format(strClass, sizeof(strClass), "Engineer");
  202. case TFClass_Medic: Format(strClass, sizeof(strClass), "Medic");
  203. case TFClass_Sniper: Format(strClass, sizeof(strClass), "Sniper");
  204. case TFClass_Spy: Format(strClass, sizeof(strClass), "Spy");
  205. default: Format(strClass, sizeof(strClass), "");
  206. }
  207. char quotedName[MAX_NAME_LENGTH + 2];
  208. ReplaceString(name, sizeof(name), "\"", "");
  209. if (strlen(name)) {
  210. Format(quotedName, sizeof(quotedName), "\"%s\"", name);
  211. }
  212. ServerCommand("tf_bot_add %s %s %s %s", strDifficulty, strTeam, strClass, quotedName); // count class team difficulty name (any order)
  213. }
  214. GetClassCounts(TFTeam:team, &scout, &soldier, &pyro, &demoman, &heavy, &engineer, &medic, &sniper, &spy) {
  215. for(new i = 1; i <= MaxClients; i++) {
  216. if(!IsClientInGame(i) || TFTeam:GetClientTeam(i) != team) {
  217. continue;
  218. }
  219. switch(TF2_GetPlayerClass(i)) {
  220. case TFClass_Scout: scout++;
  221. case TFClass_Soldier: soldier++;
  222. case TFClass_Pyro: pyro++;
  223. case TFClass_DemoMan: demoman++;
  224. case TFClass_Heavy: heavy++;
  225. case TFClass_Engineer: engineer++;
  226. case TFClass_Medic: medic++;
  227. case TFClass_Sniper: sniper++;
  228. case TFClass_Spy: spy++;
  229. }
  230. }
  231. }
  232. RemoveBot() {
  233. new teamToKick;
  234. if(GetTeamClientCount(2) > GetTeamClientCount(3)) {
  235. teamToKick = 2;
  236. } else if(GetTeamClientCount(2) < GetTeamClientCount(3)) {
  237. teamToKick = 3;
  238. } else {
  239. teamToKick = GetRandomInt(2, 3);
  240. }
  241. new Handle:bots = CreateArray();
  242. for(new i = 1; i <= MaxClients; i++) {
  243. if(IsClientConnected(i) && !IsClientSourceTV(i) && !IsClientReplay(i) && IsFakeClient(i) && GetClientTeam(i) == teamToKick) {
  244. PushArrayCell(bots, i);
  245. }
  246. }
  247. if(GetArraySize(bots) == 0) {
  248. CloseHandle(bots);
  249. return;
  250. }
  251. new bot = GetArrayCell(bots, GetRandomInt(0, GetArraySize(bots) - 1));
  252. CloseHandle(bots);
  253. Call_StartForward(fwdBotKick);
  254. Call_PushCellRef(bot);
  255. Call_Finish();
  256. ServerCommand("tf_bot_kick \"%N\"", bot);
  257. }
  258. public Event_PlayerConnect(Handle:event, const String:name[], bool:dontBroadcast) {
  259. if(GetEventBool(event, "bot")) {
  260. PushArrayCell(joiningBots, GetEventInt(event, "userid"));
  261. SetEventBroadcast(event, true);
  262. }
  263. }
  264. public Event_PlayerDisconnect(Handle:event, const String:name[], bool:dontBroadcast) {
  265. new client = GetClientOfUserId(GetEventInt(event, "userid"));
  266. if(client == 0) {
  267. return;
  268. }
  269. if(IsFakeClient(client)) {
  270. SetEventBroadcast(event, true);
  271. PrintToChatAll("\x01BOT \x07%06X%N \x01has left the game", GetTeamColor(GetClientTeam(client)), client);
  272. }
  273. }
  274. public Event_PlayerTeam(Handle:event, const String:name[], bool:dontBroadcast) {
  275. new client = GetClientOfUserId(GetEventInt(event, "userid"));
  276. if(client == 0) {
  277. return;
  278. }
  279. if(IsFakeClient(client)) {
  280. SetEventBroadcast(event, true);
  281. new pos;
  282. if((pos = FindValueInArray(joiningBots, GetClientUserId(client))) != -1) {
  283. RemoveFromArray(joiningBots, pos);
  284. PrintToServer("BOT %N has joined the game", client);
  285. PrintToChatAll("\x01BOT \x07%06X%N \x01has joined the game", GetTeamColor(GetEventInt(event, "team")), client);
  286. }
  287. }
  288. }
  289. GetTeamColor(team) {
  290. new value;
  291. switch(team) {
  292. case 1: {
  293. value = 0xCCCCCC;
  294. }
  295. case 2: {
  296. value = 0xFF4040;
  297. }
  298. case 3: {
  299. value = 0x99CCFF;
  300. }
  301. default: {
  302. value = 0x3EFF3E;
  303. }
  304. }
  305. return value;
  306. }