botmanager.sp 10 KB

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