botmanager.sp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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.5"
  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: Format(strDifficulty, sizeof(strDifficulty), "easy");
  176. case 1: Format(strDifficulty, sizeof(strDifficulty), "normal");
  177. case 2: Format(strDifficulty, sizeof(strDifficulty), "hard");
  178. case 3: Format(strDifficulty, sizeof(strDifficulty), "expert");
  179. default: Format(strDifficulty, sizeof(strDifficulty), "");
  180. }
  181. switch (team) {
  182. case TFTeam_Red: Format(strTeam, sizeof(strTeam), "red");
  183. case TFTeam_Blue: Format(strTeam, sizeof(strTeam), "blue");
  184. default: Format(strTeam, sizeof(strTeam), "");
  185. }
  186. switch (class) {
  187. case TFClass_Scout: Format(strClass, sizeof(strClass), "Scout");
  188. case TFClass_Soldier: Format(strClass, sizeof(strClass), "Soldier");
  189. case TFClass_Pyro: Format(strClass, sizeof(strClass), "Pyro");
  190. case TFClass_DemoMan: Format(strClass, sizeof(strClass), "Demoman");
  191. case TFClass_Heavy: Format(strClass, sizeof(strClass), "HeavyWeapons");
  192. case TFClass_Engineer: Format(strClass, sizeof(strClass), "Engineer");
  193. case TFClass_Medic: Format(strClass, sizeof(strClass), "Medic");
  194. case TFClass_Sniper: Format(strClass, sizeof(strClass), "Sniper");
  195. case TFClass_Spy: Format(strClass, sizeof(strClass), "Spy");
  196. default: Format(strClass, sizeof(strClass), "");
  197. }
  198. char quotedName[MAX_NAME_LENGTH + 2];
  199. ReplaceString(name, sizeof(name), "\"", "");
  200. if (strlen(name)) {
  201. Format(quotedName, sizeof(quotedName), "\"%s\"", name);
  202. }
  203. ServerCommand("tf_bot_add %s %s %s %s", strDifficulty, strTeam, strClass, quotedName); // count class team difficulty name (any order)
  204. }
  205. void GetClassCounts(TFTeam team, int numClass[TFClassType]) {
  206. for (int i = 1; i <= MaxClients; i++) {
  207. if (!IsClientInGame(i) || TF2_GetClientTeam(i) != team) {
  208. continue;
  209. }
  210. numClass[ TF2_GetPlayerClass(i) ]++;
  211. }
  212. }
  213. void RemoveBot() {
  214. int teamToKick;
  215. if (GetTeamClientCount(2) > GetTeamClientCount(3)) {
  216. teamToKick = 2;
  217. } else if (GetTeamClientCount(2) < GetTeamClientCount(3)) {
  218. teamToKick = 3;
  219. } else {
  220. teamToKick = GetRandomInt(2, 3);
  221. }
  222. ArrayList bots = CreateArray();
  223. for (int i = 1; i <= MaxClients; i++) {
  224. if (IsClientConnected(i) && !IsClientSourceTV(i) && !IsClientReplay(i) &&
  225. IsFakeClient(i) && GetClientTeam(i) == teamToKick) {
  226. bots.Push(i);
  227. }
  228. }
  229. if (!bots.Length) {
  230. delete bots;
  231. return;
  232. }
  233. int bot = bots.Get(GetRandomInt(0, bots.Length - 1));
  234. delete bots;
  235. Call_StartForward(fwdBotKick);
  236. Call_PushCellRef(bot);
  237. Call_Finish();
  238. ServerCommand("tf_bot_kick \"%N\"", bot);
  239. }
  240. public Action Event_PlayerConnect(Event event, const char[] name, bool dontBroadcast) {
  241. if (event.GetBool("bot")) {
  242. joiningBots.Push(event.GetInt("userid"));
  243. event.BroadcastDisabled = true;
  244. // more arena hacks
  245. ServerCommand("namelockid %d 1", event.GetInt("userid"));
  246. }
  247. return Plugin_Continue;
  248. }
  249. public Action Event_PlayerDisconnect(Event event, const char[] name, bool dontBroadcast) {
  250. int client = GetClientOfUserId(event.GetInt("userid"));
  251. if (!client) {
  252. return Plugin_Continue;
  253. }
  254. if (IsFakeClient(client)) {
  255. event.BroadcastDisabled = true;
  256. PrintToChatAll("\x01BOT \x07%06X%N \x01has left the game", GetTeamColor(GetClientTeam(client)), client);
  257. }
  258. return Plugin_Continue;
  259. }
  260. public Action Event_PlayerTeam(Event event, const char[] name, bool dontBroadcast) {
  261. int client = GetClientOfUserId(event.GetInt("userid"));
  262. if (!client) {
  263. return Plugin_Continue;
  264. }
  265. if (IsFakeClient(client)) {
  266. event.BroadcastDisabled = true;
  267. int pos;
  268. if ((pos = joiningBots.FindValue(GetClientUserId(client))) != -1) {
  269. joiningBots.Erase(pos);
  270. PrintToServer("BOT %N has joined the game", client);
  271. PrintToChatAll("\x01BOT \x07%06X%N \x01has joined the game",
  272. GetTeamColor(event.GetInt("team")), client);
  273. }
  274. }
  275. return Plugin_Continue;
  276. }
  277. int GetTeamColor(int team) {
  278. switch(team) {
  279. case 1: {
  280. return 0xCCCCCC;
  281. }
  282. case 2: {
  283. return 0xFF4040;
  284. }
  285. case 3: {
  286. return 0x99CCFF;
  287. }
  288. }
  289. return 0x3EFF3E;
  290. }