botmanager.sp 10 KB

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