tf_afkbot.sp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * [TF2] AFK TFBot
  3. *
  4. * Performs some dirty hacks so clients have an underlying TFBot AI that can be toggled.
  5. */
  6. #pragma semicolon 1
  7. #include <sourcemod>
  8. #include <sdkhooks>
  9. #include <sdktools>
  10. #include <dhooks>
  11. #pragma newdecls required
  12. #include <stocksoup/color_literals>
  13. #define PLUGIN_VERSION "1.0.0"
  14. public Plugin myinfo = {
  15. name = "[TF2] TFAFKBot",
  16. author = "nosoop",
  17. description = "TFBots conjoined to human players.",
  18. version = PLUGIN_VERSION,
  19. url = "localhost"
  20. }
  21. Handle g_AllocatePlayerBotEntity;
  22. Handle g_SDKCallPEntityOfEntIndex, g_SDKCallNextBotFakeClient;
  23. Handle g_DHookProcessUsercmd, g_DHookPlayerIsBot, g_DHookNextBotFakeClient;
  24. static bool g_bInBotPhysicsSimulate[MAXPLAYERS + 1];
  25. static bool g_bIsIdleBot[MAXPLAYERS + 1];
  26. public void OnPluginStart() {
  27. Handle hGameConf = LoadGameConfigFile("tf2.afkbot");
  28. if (!hGameConf) {
  29. SetFailState("Failed to load gamedata (tf2.afkbot).");
  30. }
  31. StartPrepSDKCall(SDKCall_Static);
  32. PrepSDKCall_SetFromConf(hGameConf, SDKConf_Signature, "CTFBot::AllocatePlayerEntity()");
  33. PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
  34. PrepSDKCall_AddParameter(SDKType_String, SDKPass_Pointer);
  35. g_AllocatePlayerBotEntity = EndPrepSDKCall();
  36. if (!g_AllocatePlayerBotEntity) {
  37. SetFailState("Failed to create SDKCall for function CTFBot::AllocatePlayerEntity()");
  38. }
  39. StartPrepSDKCall(SDKCall_Static);
  40. PrepSDKCall_SetFromConf(hGameConf, SDKConf_Signature, "EDICT_NUM()");
  41. PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
  42. PrepSDKCall_SetReturnInfo(SDKType_PlainOldData, SDKPass_Plain);
  43. g_SDKCallPEntityOfEntIndex = EndPrepSDKCall();
  44. Handle dt_ClientPutInServer = DHookCreateFromConf(hGameConf, "ClientPutInServer()");
  45. DHookEnableDetour(dt_ClientPutInServer, false, OnClientPutInServerPre);
  46. Handle dt_BotUpdate = DHookCreateFromConf(hGameConf, "NextBotPlayer<CTFPlayer>::Update()");
  47. DHookEnableDetour(dt_BotUpdate, false, OnBotUpdate);
  48. Handle dt_BotEntPhysicsSimulate = DHookCreateFromConf(hGameConf, "CTFBot::PhysicsSimulate()");
  49. DHookEnableDetour(dt_BotEntPhysicsSimulate, false, OnBotEntPhysicsSimulate);
  50. DHookEnableDetour(dt_BotEntPhysicsSimulate, true, OnBotEntPhysicsSimulatePost);
  51. g_DHookProcessUsercmd = DHookCreateFromConf(hGameConf, "CBasePlayer::ProcessUsercmds()");
  52. g_DHookPlayerIsBot = DHookCreateFromConf(hGameConf, "CBasePlayer::IsBot()");
  53. g_DHookNextBotFakeClient = DHookCreateFromConf(hGameConf, "NextBotPlayer<CTFPlayer>::IsFakeClient()");
  54. StartPrepSDKCall(SDKCall_Player);
  55. PrepSDKCall_SetFromConf(hGameConf, SDKConf_Virtual, "NextBotPlayer<CTFPlayer>::IsFakeClient()");
  56. PrepSDKCall_SetReturnInfo(SDKType_Bool, SDKPass_Plain);
  57. g_SDKCallNextBotFakeClient = EndPrepSDKCall();
  58. delete hGameConf;
  59. RegConsoleCmd("sm_afkbot", ToggleAFKBot);
  60. }
  61. public void OnPluginEnd() {
  62. // re-enable client prediction
  63. for (int i = 1; i <= MaxClients; i++) {
  64. if (IsClientInGame(i) && !IsFakeClient(i)) {
  65. SetClientPrediction(i, true);
  66. }
  67. }
  68. }
  69. public Action ToggleAFKBot(int client, int argc) {
  70. if (!IsPlayerNextBot(client)) {
  71. ReplyToCommand(client, "No underlying bot logic available. Reconnecting...");
  72. ClientCommand(client, "retry");
  73. return Plugin_Handled;
  74. }
  75. g_bIsIdleBot[client] = !g_bIsIdleBot[client];
  76. SetClientPrediction(client, !g_bIsIdleBot[client]);
  77. PrintColoredChatEx(client, CHAT_SOURCE_SELF, COLOR_TEAM ... "Beep boop (%d).",
  78. g_bIsIdleBot[client]);
  79. return Plugin_Handled;
  80. }
  81. public void OnMapStart() {
  82. for (int i = 1; i <= MaxClients; i++) {
  83. if (IsClientInGame(i)) {
  84. OnClientPutInServer(i);
  85. }
  86. }
  87. }
  88. /**
  89. * Intercepts the default player entity allocation step and uses the TFBot allocator instead.
  90. */
  91. public MRESReturn OnClientPutInServerPre(Handle hParams) {
  92. int client = DHookGetParam(hParams, 1);
  93. char name[MAX_NAME_LENGTH];
  94. DHookGetParamString(hParams, 2, name, sizeof(name));
  95. AllocatePlayerBotEntity(client, name);
  96. // equivalent to CBasePlayer::SetPlayerName()
  97. SetEntPropString(client, Prop_Data, "m_szNetname", name);
  98. SetEntityFlags(client, GetEntityFlags(client) & ~FL_FAKECLIENT);
  99. PrintToServer("[afkbot] ocpis");
  100. return MRES_Supercede;
  101. }
  102. public void OnClientPutInServer(int client) {
  103. if (!IsFakeClient(client)) {
  104. DHookEntity(g_DHookProcessUsercmd, false, client, .callback = OnPlayerProcessUsercmds);
  105. DHookEntity(g_DHookPlayerIsBot, false, client, .callback = OnPlayerIsBot);
  106. DHookEntity(g_DHookNextBotFakeClient, false, client, .callback = OnPlayerIsFakeClient);
  107. g_bIsIdleBot[client] = false;
  108. }
  109. }
  110. /**
  111. * Disables NextBot updates on human players that aren't configured to be AFK.
  112. */
  113. public MRESReturn OnBotUpdate(int client) {
  114. if (!IsFakeClient(client) && !g_bIsIdleBot[client]) {
  115. return MRES_Supercede;
  116. }
  117. return MRES_Ignored;
  118. }
  119. /**
  120. * Scoped check to determine if CBasePlayer::ProcessUsercmds() is called from within the context
  121. * of a bot or if it is not.
  122. *
  123. * Because of the filthy, filthy hacks we're doing to make this work, both bot and player
  124. * usercmds are called, and without this check, the player gets updated twice as quickly
  125. * (having interesting side effects in that the player moves much faster).
  126. */
  127. public MRESReturn OnBotEntPhysicsSimulate(int client) {
  128. g_bInBotPhysicsSimulate[client] = true;
  129. return MRES_Ignored;
  130. }
  131. public MRESReturn OnBotEntPhysicsSimulatePost(int client) {
  132. g_bInBotPhysicsSimulate[client] = false;
  133. return MRES_Ignored;
  134. }
  135. /**
  136. * Only perform the usercommand processing for bots or not bots, depending on if the player is
  137. * being controlled by a bot or not.
  138. *
  139. * Dead players always have their usercommands processed so freezecams and respawns work.
  140. *
  141. * Precondition: OnClientPutInServer() only hooks this on human players.
  142. */
  143. public MRESReturn OnPlayerProcessUsercmds(int client, Handle hParams) {
  144. /**
  145. * drop inputs that occur inside / outside of CTFBot::PhysicsSimulate()
  146. * dependent on whether the player is idle or not
  147. */
  148. return (g_bInBotPhysicsSimulate[client] == g_bIsIdleBot[client]) || !IsPlayerAlive(client)?
  149. MRES_Ignored : MRES_Supercede;
  150. }
  151. static bool s_bGetTrueFakeClientResult;
  152. stock bool IsPlayerNextBot(int client) {
  153. s_bGetTrueFakeClientResult = true;
  154. bool result = SDKCall(g_SDKCallNextBotFakeClient, client);
  155. s_bGetTrueFakeClientResult = false;
  156. return result;
  157. }
  158. /**
  159. * Overrides the result of NextBotPlayer<CTFPlayer>::IsFakeClient(). This controls the result
  160. * of `tf_bot_count` and seemingly deals with loadouts.
  161. *
  162. * NextBot logic will only run when this returns `true`.
  163. * Loadout caching (?) will only occur when this returns `false`, though we can set it to true
  164. * afterwards without any known consequences.
  165. *
  166. * Precondition: OnClientPutInServer() only hooks this on human players.
  167. */
  168. public MRESReturn OnPlayerIsFakeClient(int client, Handle hReturn) {
  169. if (s_bGetTrueFakeClientResult) {
  170. return MRES_Ignored;
  171. }
  172. DHookSetReturn(hReturn, g_bIsIdleBot[client]);
  173. return MRES_Supercede;
  174. }
  175. /**
  176. * Overrides the result of CBasePlayer::IsBot().
  177. * This is checked when updating m_iPing during `CPlayerResource::UpdatePlayerData`.
  178. */
  179. public MRESReturn OnPlayerIsBot(int client, Handle hReturn) {
  180. DHookSetReturn(hReturn, false);
  181. return MRES_Supercede;
  182. }
  183. /**
  184. * Allocates a bot to the player entity.
  185. *
  186. * Uses a semi-filthy hack to get the edict_t* from an entity index, as sdktools/vdecoder.cpp
  187. * won't let us act on a freed edict (so we pass the edict_t pointer as POD instead).
  188. * A proper version would actually SDKCall on `server->PEntityOfEntIndex()` or reverse-engineer
  189. * the function, but ain't nobody got time for that.
  190. */
  191. void AllocatePlayerBotEntity(int client, const char[] name) {
  192. Address pEdict = SDKCall(g_SDKCallPEntityOfEntIndex, client);
  193. SDKCall(g_AllocatePlayerBotEntity, pEdict, name);
  194. }
  195. /**
  196. * Sets client-side prediction on a client.
  197. */
  198. void SetClientPrediction(int client, bool bPrediction) {
  199. // https://github.com/Pelipoika/TF2_Idlebot/blob/master/idlebot.sp
  200. FindConVar("sv_client_predict").ReplicateToClient(client, bPrediction? "-1" : "0");
  201. SetEntProp(client, Prop_Data, "m_bLagCompensation", bPrediction);
  202. SetEntProp(client, Prop_Data, "m_bPredictWeapons", bPrediction);
  203. }