botmanager.sp 9.4 KB

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