csrd_bot_taunt_randomizer.sp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * [CSRD] Bot Taunt Randomizer
  3. *
  4. * Patches taunt command handling so bots may use taunt items.
  5. */
  6. #pragma semicolon 1
  7. #include <sourcemod>
  8. #include <tf2_stocks>
  9. #include <tf2idb>
  10. #pragma newdecls required
  11. #include <dhooks>
  12. #include <sdktools>
  13. #include <stocksoup/log_server>
  14. #include <stocksoup/tf/econ>
  15. #define PLUGIN_VERSION "1.0.0"
  16. public Plugin myinfo = {
  17. name = "[CSRD] Bot Taunt Randomizer",
  18. author = "nosoop",
  19. description = "Allows bots to use taunt items.",
  20. version = PLUGIN_VERSION,
  21. url = "https://git.csrd.science/"
  22. }
  23. Handle g_SDKFindPartnerTauntInitiator;
  24. ArrayList g_TauntEntries[TFClassType];
  25. public void OnPluginStart() {
  26. Handle hGameConf = LoadGameConfigFile("csrd.bot_taunt_randomizer");
  27. if (!hGameConf) {
  28. SetFailState("Failed to load gamedata (csrd.bot_taunt_randomizer).");
  29. }
  30. StartPrepSDKCall(SDKCall_Player);
  31. PrepSDKCall_SetFromConf(hGameConf, SDKConf_Signature,
  32. "CTFPlayer::FindPartnerTauntInitiator()");
  33. PrepSDKCall_SetReturnInfo(SDKType_CBaseEntity, SDKPass_Pointer);
  34. g_SDKFindPartnerTauntInitiator = EndPrepSDKCall();
  35. Handle dt_PlayTauntSceneFromItem = DHookCreateFromConf(hGameConf,
  36. "CTFPlayer::PlayTauntSceneFromItem()");
  37. DHookEnableDetour(dt_PlayTauntSceneFromItem, false, OnPlayTauntSceneFromItem);
  38. Handle dt_HandleTauntCommand = DHookCreateFromConf(hGameConf,
  39. "CTFPlayer::HandleTauntCommand()");
  40. DHookEnableDetour(dt_HandleTauntCommand, false, OnHandleTauntCommand);
  41. delete hGameConf;
  42. for (TFClassType i = TFClass_Unknown; i < TFClassType; i++) {
  43. g_TauntEntries[i] = new ArrayList();
  44. }
  45. }
  46. /**
  47. * Build lists of taunts sorted by class via TF2IDB.
  48. */
  49. public void OnAllPluginsLoaded() {
  50. ArrayList args = new ArrayList();
  51. DBResultSet results = view_as<DBResultSet>(TF2IDB_CustomQuery(
  52. "SELECT id, class FROM tf2idb_class WHERE slot = 'taunt'", args, 4));
  53. if (!results) {
  54. return;
  55. }
  56. while (results.FetchRow()) {
  57. char className[16];
  58. int id = results.FetchInt(0);
  59. results.FetchString(1, className, sizeof(className));
  60. g_TauntEntries[TF2_GetClass(className)].Push(id);
  61. }
  62. delete results;
  63. }
  64. /**
  65. * Makes bots use a taunt command with a non-zero slot if there is no valid taunt partner
  66. * nearby. Without this, bots will only use their default taunt (or participate in any nearby
  67. * partner taunts).
  68. */
  69. public MRESReturn OnHandleTauntCommand(int client, Handle hParams) {
  70. int nTauntSlot = DHookGetParam(hParams, 1);
  71. if (nTauntSlot || !CanRandomTaunt(client)) {
  72. return MRES_Ignored;
  73. }
  74. if (TF2_FindPartnerTauntInitiator(client) > 0) {
  75. return MRES_Ignored;
  76. }
  77. // 90% chance of using a taunt item
  78. if (GetURandomFloat() > 0.90) {
  79. return MRES_Ignored;
  80. }
  81. // no initiator, consider using a taunt item
  82. DHookSetParam(hParams, 1, 8);
  83. return MRES_ChangedHandled;
  84. }
  85. /**
  86. * Allow bots to use a random taunt item.
  87. *
  88. * `CTFPlayer::PlayTauntSceneFromItem()` gets a null pointer passed in if the player attempts
  89. * to taunt with an empty taunt slot.
  90. */
  91. public MRESReturn OnPlayTauntSceneFromItem(int client, Handle hReturn, Handle hParams) {
  92. if (!CanRandomTaunt(client)) {
  93. return MRES_Ignored;
  94. }
  95. int item = DHookGetParam(hParams, 1);
  96. if (item) {
  97. return MRES_Ignored;
  98. }
  99. int taunt = TF2_GetRandomTauntForClass(TF2_GetPlayerClass(client));
  100. if (taunt == DEFINDEX_UNDEFINED) {
  101. return MRES_Ignored;
  102. }
  103. int wearable = TF2_SpawnWearable(taunt);
  104. if (!IsValidEntity(wearable)) {
  105. return MRES_Ignored;
  106. }
  107. // fix taunt slot index so it correctly uses new taunt properties
  108. SetEntProp(client, Prop_Send, "m_nActiveTauntSlot", -1);
  109. SetEntProp(client, Prop_Send, "m_iTauntItemDefIndex", taunt);
  110. DHookSetParam(hParams, 1, TF2_GetEconItemView(wearable));
  111. RemoveEntity(wearable);
  112. return MRES_ChangedHandled;
  113. }
  114. /**
  115. * Returns a pointer to an item entity's `CEconItemView`.
  116. */
  117. Address TF2_GetEconItemView(int item) {
  118. if (!IsValidEntity(item) || !HasEntProp(item, Prop_Send, "m_Item")) {
  119. // we should probably throw an error here
  120. return Address_Null;
  121. }
  122. return GetEntityAddress(item) + view_as<Address>(GetEntSendPropOffs(item, "m_Item", true));
  123. }
  124. /**
  125. * Returns a partner taunt initiator entity if one is nearby, or -1 if not.
  126. */
  127. int TF2_FindPartnerTauntInitiator(int client) {
  128. return SDKCall(g_SDKFindPartnerTauntInitiator, client);
  129. }
  130. /**
  131. * Returns a random class-specific or all-class taunt index.
  132. */
  133. int TF2_GetRandomTauntForClass(TFClassType playerClass) {
  134. int nTauntsAllClass = g_TauntEntries[0].Length;
  135. int nTauntsPlayerClass = g_TauntEntries[playerClass].Length;
  136. if (nTauntsAllClass + nTauntsPlayerClass == 0) {
  137. return DEFINDEX_UNDEFINED;
  138. }
  139. int index = GetRandomInt(0, nTauntsAllClass + nTauntsPlayerClass - 1);
  140. if (index < nTauntsAllClass) {
  141. return g_TauntEntries[0].Get(index);
  142. }
  143. return g_TauntEntries[playerClass].Get(index - nTauntsAllClass);
  144. }
  145. /**
  146. * Helper function for self-testing.
  147. */
  148. bool CanRandomTaunt(int client) {
  149. return IsFakeClient(client);
  150. }