From afabac76a3f9e6eef459a5319984cec09c6e1827 Mon Sep 17 00:00:00 2001 From: caxanga334 <10157643+caxanga334@users.noreply.github.com> Date: Tue, 14 May 2024 23:24:18 -0300 Subject: [PATCH] Migrate To SourceHooks Use SourceHooks instead of game events. --- PackageScript | 1 + extension/AMBuilder | 3 +- extension/bot/basebot.cpp | 13 +- extension/bot/basebot.h | 23 +++- extension/bot/basebot_hooks.cpp | 95 +++++++++++++++ extension/bot/interfaces/event_listener.h | 47 ++++++-- extension/bot/interfaces/tasks.h | 48 +++++--- extension/extension.cpp | 75 +++--------- extension/extension.h | 2 - extension/mods/basemod.cpp | 7 +- extension/mods/basemod_gameevents.cpp | 2 +- extension/mods/tf2/teamfortress2mod.cpp | 9 +- extension/mods/tf2/tf2mod_gameevents.cpp | 2 + extension/mods/tf2/tf2mod_gameevents.h | 12 +- extension/navmesh/nav_entities.h | 2 +- extension/navmesh/nav_ladder.h | 2 +- extension/sdkports/sdk_ehandle.cpp | 9 ++ extension/sdkports/sdk_ehandle.h | 138 ++++++++++++++++++++++ extension/sdkports/sdk_takedamageinfo.h | 10 ++ extension/util/Handle.cpp | 30 ----- extension/util/Handle.h | 85 ------------- gamedata/navbot.games/game.dod.txt | 16 ++- gamedata/navbot.games/game.tf.txt | 23 ++++ 23 files changed, 429 insertions(+), 225 deletions(-) create mode 100644 extension/bot/basebot_hooks.cpp create mode 100644 extension/sdkports/sdk_ehandle.cpp create mode 100644 extension/sdkports/sdk_ehandle.h create mode 100644 extension/sdkports/sdk_takedamageinfo.h delete mode 100644 extension/util/Handle.cpp delete mode 100644 extension/util/Handle.h create mode 100644 gamedata/navbot.games/game.tf.txt diff --git a/PackageScript b/PackageScript index db5e308..9e8371a 100644 --- a/PackageScript +++ b/PackageScript @@ -53,6 +53,7 @@ CopyFiles('extension', 'addons/sourcemod/extensions', CopyFiles('gamedata/navbot.games', 'addons/sourcemod/gamedata/navbot.games', [ 'common.games.txt', 'game.dod.txt', + 'game.tf.txt', 'master.games.txt' ] ) diff --git a/extension/AMBuilder b/extension/AMBuilder index 8322fea..d1473ef 100644 --- a/extension/AMBuilder +++ b/extension/AMBuilder @@ -14,6 +14,7 @@ sourceFiles = [ 'sdkports/debugoverlay_shared.cpp', 'sdkports/studio.cpp', 'sdkports/gametrace_server.cpp', + 'sdkports/sdk_ehandle.cpp', 'entities/baseentity.cpp', 'entities/basecombatchar.cpp', 'entities/basecombatweapon.cpp', @@ -35,7 +36,6 @@ sourceFiles = [ # Utility 'util/BaseEntity.cpp', 'util/EntityUtils.cpp', - 'util/Handle.cpp', 'util/SimpleException.cpp', 'util/UtilTrace.cpp', 'util/entprops.cpp', @@ -56,6 +56,7 @@ sourceFiles = [ 'bot/interfaces/path/meshnavigator.cpp', 'bot/basebot.cpp', 'bot/basebot_debug.cpp', + 'bot/basebot_hooks.cpp', 'concommands_bots.cpp', 'concommands_debug.cpp', # TF2 Bot diff --git a/extension/bot/basebot.cpp b/extension/bot/basebot.cpp index d491a07..2974a9b 100644 --- a/extension/bot/basebot.cpp +++ b/extension/bot/basebot.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -12,7 +11,7 @@ #include #include #include -#include +#include #include "basebot.h" extern CGlobalVars* gpGlobals; @@ -216,6 +215,9 @@ CBaseBot::CBaseBot(edict_t* edict) : CBaseExtPlayer(edict), m_debugtextoffset = 0; m_weapons.reserve(MAX_WEAPONS); m_weaponupdatetimer = 5; + m_shhooks.reserve(32); + + AddHooks(); } CBaseBot::~CBaseBot() @@ -226,6 +228,13 @@ CBaseBot::~CBaseBot() delete m_basebehavior; m_interfaces.clear(); m_listeners.clear(); + + for (auto& hook : m_shhooks) + { + SH_REMOVE_HOOK_ID(hook); + } + + m_shhooks.clear(); } std::vector* CBaseBot::GetListenerVector() diff --git a/extension/bot/basebot.h b/extension/bot/basebot.h index 585789b..df7a2fb 100644 --- a/extension/bot/basebot.h +++ b/extension/bot/basebot.h @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -30,6 +31,19 @@ class CBaseBot : public CBaseExtPlayer, public IEventListener CBaseBot(edict_t* edict); ~CBaseBot() override; + static bool InitHooks(SourceMod::IGameConfig* gd_navbot, SourceMod::IGameConfig* gd_sdkhooks); + +private: + void AddHooks(); + + void Hook_Spawn(); + void Hook_Touch(CBaseEntity* pOther); + int Hook_OnTakeDamage_Alive(const CTakeDamageInfo& info); + void Hook_Event_Killed(const CTakeDamageInfo& info); + void Hook_Event_KilledOther(CBaseEntity* pVictim, const CTakeDamageInfo& info); + +public: + // Event propagation std::vector* GetListenerVector() override; @@ -80,7 +94,9 @@ class CBaseBot : public CBaseExtPlayer, public IEventListener virtual void Spawn(); // Called on the first spawn call virtual void FirstSpawn(); - // Called when the player_death event is fired for this bot + // Called when this bot touches another entity + virtual void Touch(CBaseEntity* pOther) {} + // Called when this bot dies virtual void Killed() {} // Called by the OnTakeDamage_Alive hook. virtual void OnTakeDamage_Alive(const CTakeDamageInfo& info); @@ -179,6 +195,9 @@ class CBaseBot : public CBaseExtPlayer, public IEventListener protected: bool m_isfirstspawn; + // Adds a SourceHook Hook into the hook list to be removed when this is destroyed + void AddSourceHookID(int hookID) { m_shhooks.push_back(hookID); } + private: int m_nextupdatetime; int m_joingametime; // delay between joingame attempts @@ -200,6 +219,8 @@ class CBaseBot : public CBaseExtPlayer, public IEventListener std::vector m_weapons; int m_weaponupdatetimer; Vector m_homepos; // Position where the bot spawned + std::vector m_shhooks; // IDs of SourceHook's hooks + IntervalTimer m_burningtimer; void ExecuteQueuedCommands(); }; diff --git a/extension/bot/basebot_hooks.cpp b/extension/bot/basebot_hooks.cpp new file mode 100644 index 0000000..bc1236e --- /dev/null +++ b/extension/bot/basebot_hooks.cpp @@ -0,0 +1,95 @@ +#include +#include +#include "basebot.h" + +SH_DECL_MANUALHOOK0_void(CBaseBot_Spawn, 0, 0, 0) +SH_DECL_MANUALHOOK1_void(CBaseBot_Touch, 0, 0, 0, CBaseEntity*); +SH_DECL_MANUALHOOK1(CBaseBot_OnTakeDamage_Alive, 0, 0, 0, int, const CTakeDamageInfo&) +SH_DECL_MANUALHOOK1_void(CBaseBot_Event_Killed, 0, 0, 0, const CTakeDamageInfo&) +SH_DECL_MANUALHOOK2_void(CBaseBot_Event_KilledOther, 0, 0, 0, CBaseEntity*, const CTakeDamageInfo&) + +bool CBaseBot::InitHooks(SourceMod::IGameConfig* gd_navbot, SourceMod::IGameConfig* gd_sdkhooks) +{ + int offset = 0; + + if (!gd_sdkhooks->GetOffset("Spawn", &offset)) { return false; } + SH_MANUALHOOK_RECONFIGURE(CBaseBot_Spawn, offset, 0, 0); + + if (!gd_sdkhooks->GetOffset("Touch", &offset)) { return false; } + SH_MANUALHOOK_RECONFIGURE(CBaseBot_Touch, offset, 0, 0); + + if (!gd_sdkhooks->GetOffset("OnTakeDamage_Alive", &offset)) { return false; } + SH_MANUALHOOK_RECONFIGURE(CBaseBot_OnTakeDamage_Alive, offset, 0, 0); + + if (!gd_navbot->GetOffset("Event_Killed", &offset)) { return false; } + SH_MANUALHOOK_RECONFIGURE(CBaseBot_Event_Killed, offset, 0, 0); + + if (!gd_navbot->GetOffset("Event_KilledOther", &offset)) { return false; } + SH_MANUALHOOK_RECONFIGURE(CBaseBot_Event_KilledOther, offset, 0, 0); + + return true; +} + +// TO-DO: Move the PlayerRunCMD hook to here too! + +void CBaseBot::AddHooks() +{ + CBaseEntity* ifaceptr = GetEntity(); + +#ifdef EXT_DEBUG + if (ifaceptr == nullptr) + { + smutils->LogError(myself, "CBaseBot::AddHooks NULL CBaseEntity!"); + } +#endif // EXT_DEBUG + + // Add hooks for this bot instance, hooks are removed on the destructor + m_shhooks.push_back(SH_ADD_MANUALHOOK(CBaseBot_Spawn, ifaceptr, SH_MEMBER(this, &CBaseBot::Hook_Spawn), false)); + m_shhooks.push_back(SH_ADD_MANUALHOOK(CBaseBot_Touch, ifaceptr, SH_MEMBER(this, &CBaseBot::Hook_Touch), false)); + m_shhooks.push_back(SH_ADD_MANUALHOOK(CBaseBot_OnTakeDamage_Alive, ifaceptr, SH_MEMBER(this, &CBaseBot::Hook_OnTakeDamage_Alive), false)); + m_shhooks.push_back(SH_ADD_MANUALHOOK(CBaseBot_Event_Killed, ifaceptr, SH_MEMBER(this, &CBaseBot::Hook_Event_Killed), false)); + m_shhooks.push_back(SH_ADD_MANUALHOOK(CBaseBot_Event_KilledOther, ifaceptr, SH_MEMBER(this, &CBaseBot::Hook_Event_KilledOther), false)); +} + +void CBaseBot::Hook_Spawn() +{ + Spawn(); + RETURN_META(MRES_IGNORED); +} + +void CBaseBot::Hook_Touch(CBaseEntity* pOther) +{ + Touch(pOther); + OnContact(pOther); + RETURN_META(MRES_IGNORED); +} + +int CBaseBot::Hook_OnTakeDamage_Alive(const CTakeDamageInfo& info) +{ + if (info.GetDamageType() & DMG_BURN) + { + if (!m_burningtimer.HasStarted() || m_burningtimer.IsGreaterThen(1.0f)) + { + m_burningtimer.Start(); + OnIgnited(info); + } + } + + + OnTakeDamage_Alive(info); + OnInjured(info); + RETURN_META_VALUE(MRES_IGNORED, 0); +} + +void CBaseBot::Hook_Event_Killed(const CTakeDamageInfo& info) +{ + Killed(); + OnKilled(info); + RETURN_META(MRES_IGNORED); +} + +void CBaseBot::Hook_Event_KilledOther(CBaseEntity* pVictim, const CTakeDamageInfo& info) +{ + OnOtherKilled(pVictim, info); + RETURN_META(MRES_IGNORED); +} diff --git a/extension/bot/interfaces/event_listener.h b/extension/bot/interfaces/event_listener.h index 762d6d6..fa817a4 100644 --- a/extension/bot/interfaces/event_listener.h +++ b/extension/bot/interfaces/event_listener.h @@ -8,6 +8,7 @@ class CBaseBot; class CPath; class CBaseEntity; class Vector; +class CTakeDamageInfo; struct edict_t; // Interface for receiving events @@ -40,9 +41,11 @@ class IEventListener virtual void OnUnstuck(); // bot was stuck and is no longer stuck virtual void OnMoveToFailure(CPath* path, MovementFailureType reason); virtual void OnMoveToSuccess(CPath* path); - virtual void OnInjured(edict_t* attacker = nullptr); // when the bot takes damage - virtual void OnKilled(edict_t* attacker = nullptr); // when the bot is killed - virtual void OnOtherKilled(edict_t* victim, edict_t* attacker = nullptr); // when another player gets killed + virtual void OnContact(CBaseEntity* pOther); // Something touched the bot + virtual void OnIgnited(const CTakeDamageInfo& info); // The bot is on fire and/or taking fire damage + virtual void OnInjured(const CTakeDamageInfo& info); // when the bot takes damage + virtual void OnKilled(const CTakeDamageInfo& info); // when the bot is killed + virtual void OnOtherKilled(CBaseEntity* pVictim, const CTakeDamageInfo& info); // when another player gets killed virtual void OnSight(edict_t* subject); // when the bot spots an entity virtual void OnLostSight(edict_t* subject); // when the bot loses sight of an entity virtual void OnSound(edict_t* source, const Vector& position, SoundType type, const int volume); // when the bot hears an entity @@ -113,7 +116,7 @@ inline void IEventListener::OnMoveToSuccess(CPath* path) } } -inline void IEventListener::OnInjured(edict_t* attacker) +inline void IEventListener::OnContact(CBaseEntity* pOther) { auto vec = GetListenerVector(); @@ -121,12 +124,12 @@ inline void IEventListener::OnInjured(edict_t* attacker) { for (auto listener : *vec) { - listener->OnInjured(attacker); + listener->OnContact(pOther); } } } -inline void IEventListener::OnKilled(edict_t* attacker) +inline void IEventListener::OnIgnited(const CTakeDamageInfo& info) { auto vec = GetListenerVector(); @@ -134,12 +137,12 @@ inline void IEventListener::OnKilled(edict_t* attacker) { for (auto listener : *vec) { - listener->OnKilled(attacker); + listener->OnIgnited(info); } } } -inline void IEventListener::OnOtherKilled(edict_t* victim, edict_t* attacker) +inline void IEventListener::OnInjured(const CTakeDamageInfo& info) { auto vec = GetListenerVector(); @@ -147,7 +150,33 @@ inline void IEventListener::OnOtherKilled(edict_t* victim, edict_t* attacker) { for (auto listener : *vec) { - listener->OnOtherKilled(victim, attacker); + listener->OnInjured(info); + } + } +} + +inline void IEventListener::OnKilled(const CTakeDamageInfo& info) +{ + auto vec = GetListenerVector(); + + if (vec) + { + for (auto listener : *vec) + { + listener->OnKilled(info); + } + } +} + +inline void IEventListener::OnOtherKilled(CBaseEntity* pVictim, const CTakeDamageInfo& info) +{ + auto vec = GetListenerVector(); + + if (vec) + { + for (auto listener : *vec) + { + listener->OnOtherKilled(pVictim, info); } } } diff --git a/extension/bot/interfaces/tasks.h b/extension/bot/interfaces/tasks.h index 5b4e007..6b22c42 100644 --- a/extension/bot/interfaces/tasks.h +++ b/extension/bot/interfaces/tasks.h @@ -15,6 +15,8 @@ // Forward declaration template class AITask; class CPath; +class CBaseEntity; +class CTakeDamageInfo; // Types of results an AI Task can have enum TaskResultType @@ -548,9 +550,11 @@ class AITask : public IEventListener, public IDecisionQuery virtual TaskEventResponseResult OnUnstuck(BotClass* bot) { return TryContinue(); } virtual TaskEventResponseResult OnMoveToFailure(BotClass* bot, CPath* path, IEventListener::MovementFailureType reason) { return TryContinue(); } virtual TaskEventResponseResult OnMoveToSuccess(BotClass* bot, CPath* path) { return TryContinue(); } - virtual TaskEventResponseResult OnInjured(BotClass* bot, edict_t* attacker = nullptr) { return TryContinue(); } - virtual TaskEventResponseResult OnKilled(BotClass* bot, edict_t* attacker = nullptr) { return TryContinue(); } - virtual TaskEventResponseResult OnOtherKilled(BotClass* bot, edict_t* victim, edict_t* attacker = nullptr) { return TryContinue(); } + virtual TaskEventResponseResult OnContact(BotClass* bot, CBaseEntity* pOther) { return TryContinue(); } + virtual TaskEventResponseResult OnIgnited(BotClass* bot, const CTakeDamageInfo& info) { return TryContinue(); } + virtual TaskEventResponseResult OnInjured(BotClass* bot, const CTakeDamageInfo& info) { return TryContinue(); } + virtual TaskEventResponseResult OnKilled(BotClass* bot, const CTakeDamageInfo& info) { return TryContinue(); } + virtual TaskEventResponseResult OnOtherKilled(BotClass* bot, CBaseEntity* pVictim, const CTakeDamageInfo& info) { return TryContinue(); } virtual TaskEventResponseResult OnSight(BotClass* bot, edict_t* subject) { return TryContinue(); } virtual TaskEventResponseResult OnLostSight(BotClass* bot, edict_t* subject) { return TryContinue(); } virtual TaskEventResponseResult OnSound(BotClass* bot, edict_t* source, const Vector& position, SoundType type, const int volume) { return TryContinue(); } @@ -941,57 +945,67 @@ class AITask : public IEventListener, public IDecisionQuery AITask* ProcessTaskPause(BotClass* bot, AITaskManager* manager, AITask* task); TaskResult ProcessTaskResume(BotClass* bot, AITaskManager* manager, AITask* task); - virtual void OnTestEventPropagation() override final + void OnTestEventPropagation() override final { PROPAGATE_TASK_EVENT_WITH_NO_ARGS(OnTestEventPropagation); } - virtual void OnStuck() override final + void OnStuck() override final { PROPAGATE_TASK_EVENT_WITH_NO_ARGS(OnStuck); } - virtual void OnUnstuck() override final + void OnUnstuck() override final { PROPAGATE_TASK_EVENT_WITH_NO_ARGS(OnUnstuck); } - virtual void OnMoveToFailure(CPath* path, IEventListener::MovementFailureType reason) override final + void OnMoveToFailure(CPath* path, IEventListener::MovementFailureType reason) override final { PROPAGATE_TASK_EVENT_WITH_2_ARGS(OnMoveToFailure, path, reason); } - virtual void OnMoveToSuccess(CPath* path) override final + void OnMoveToSuccess(CPath* path) override final { PROPAGATE_TASK_EVENT_WITH_1_ARGS(OnMoveToSuccess, path); } - virtual void OnInjured(edict_t* attacker = nullptr) override final + void OnContact(CBaseEntity* pOther) override final { - PROPAGATE_TASK_EVENT_WITH_1_ARGS(OnInjured, attacker); + PROPAGATE_TASK_EVENT_WITH_1_ARGS(OnContact, pOther); } - virtual void OnKilled(edict_t* attacker = nullptr) override final + void OnIgnited(const CTakeDamageInfo& info) override final { - PROPAGATE_TASK_EVENT_WITH_1_ARGS(OnKilled, attacker); + PROPAGATE_TASK_EVENT_WITH_1_ARGS(OnIgnited, info); } - virtual void OnOtherKilled(edict_t* victim, edict_t* attacker = nullptr) override final + void OnInjured(const CTakeDamageInfo& info) override final { - PROPAGATE_TASK_EVENT_WITH_2_ARGS(OnOtherKilled, victim, attacker); + PROPAGATE_TASK_EVENT_WITH_1_ARGS(OnInjured, info); } - virtual void OnSight(edict_t* subject) override final + void OnKilled(const CTakeDamageInfo& info) override final + { + PROPAGATE_TASK_EVENT_WITH_1_ARGS(OnKilled, info); + } + + void OnOtherKilled(CBaseEntity* pVictim, const CTakeDamageInfo& info) override final + { + PROPAGATE_TASK_EVENT_WITH_2_ARGS(OnOtherKilled, pVictim, info); + } + + void OnSight(edict_t* subject) override final { PROPAGATE_TASK_EVENT_WITH_1_ARGS(OnSight, subject); } - virtual void OnLostSight(edict_t* subject) override final + void OnLostSight(edict_t* subject) override final { PROPAGATE_TASK_EVENT_WITH_1_ARGS(OnLostSight, subject); } - virtual void OnSound(edict_t* source, const Vector& position, SoundType type, const int volume) override final + void OnSound(edict_t* source, const Vector& position, SoundType type, const int volume) override final { PROPAGATE_TASK_EVENT_WITH_4_ARGS(OnSound, source, position, type, volume); } diff --git a/extension/extension.cpp b/extension/extension.cpp index 85a0c87..9293979 100644 --- a/extension/extension.cpp +++ b/extension/extension.cpp @@ -42,16 +42,13 @@ // Need this for CUserCmd class definition #include -#include -#include +#include /** * @file extension.cpp * @brief Implement extension code here. */ - - CGlobalVars* gpGlobals = nullptr; IVDebugOverlay* debugoverlay = nullptr; // IVEngineServer* engine = nullptr; @@ -92,7 +89,6 @@ SH_DECL_HOOK1_void(IServerGameDLL, GameFrame, SH_NOATTRIB, 0, bool); // SDKs that requires a runplayercommand hook SH_DECL_MANUALHOOK2_void(MH_PlayerRunCommand, 0, 0, 0, CUserCmd*, IMoveHelper*) -SH_DECL_MANUALHOOK1(MH_CBasePlayer_OnTakeDamage_Alive, 0, 0, 0, int, const CTakeDamageInfo&) SMEXT_LINK(&g_NavBotExt); @@ -162,7 +158,6 @@ bool NavBotExt::SDK_OnLoad(char* error, size_t maxlen, bool late) { extension = this; m_hookruncmd = false; - m_hasbaseplayerhooks = true; // we will always hook these, set to false on gamedata failure m_gamedata = nullptr; randomgen->ReSeed(); // set the initial seed based on the clock @@ -212,18 +207,20 @@ bool NavBotExt::SDK_OnLoad(char* error, size_t maxlen, bool late) gameconfs->CloseGameConfigFile(gamedata); gamedata = nullptr; - if (gameconfs->LoadGameConfigFile("sdkhooks.games", &gamedata, error, maxlen)) + if (!gameconfs->LoadGameConfigFile("sdkhooks.games", &gamedata, error, maxlen)) { - offset = 0; - if (gamedata->GetOffset("OnTakeDamage_Alive", &offset)) - { - SH_MANUALHOOK_RECONFIGURE(MH_CBasePlayer_OnTakeDamage_Alive, offset, 0, 0); - } - else - { - m_hasbaseplayerhooks = false; - smutils->LogError(myself, "Failed to get CBaseEntity::OnTakeDamage_Alive vtable offset from SDKHooks gamedata!"); - } + const char* message = "Failed to load SDK Tools gamedata!"; + maxlen = strlen(message); + strcpy(error, message); + return false; + } + + if (!CBaseBot::InitHooks(m_gamedata, gamedata)) + { + const char* message = "Failed to setup SourceHooks (CBaseBot)!"; + maxlen = strlen(message); + strcpy(error, message); + return false; } // This stuff needs to be after any load failures so we don't causes other stuff to crash @@ -354,11 +351,6 @@ void NavBotExt::OnClientPutInServer(int client) { SH_ADD_MANUALHOOK(MH_PlayerRunCommand, baseent, SH_MEMBER(this, &NavBotExt::Hook_PlayerRunCommand), false); } - - if (m_hasbaseplayerhooks) - { - SH_ADD_MANUALHOOK(MH_CBasePlayer_OnTakeDamage_Alive, baseent, SH_MEMBER(this, &NavBotExt::Hook_CBaseEntity_OnTakeDamage_Alive), false); - } } } @@ -374,11 +366,6 @@ void NavBotExt::OnClientDisconnecting(int client) { SH_REMOVE_MANUALHOOK(MH_PlayerRunCommand, baseent, SH_MEMBER(this, &NavBotExt::Hook_PlayerRunCommand), false); } - - if (m_hasbaseplayerhooks) - { - SH_REMOVE_MANUALHOOK(MH_CBasePlayer_OnTakeDamage_Alive, baseent, SH_MEMBER(this, &NavBotExt::Hook_CBaseEntity_OnTakeDamage_Alive), false); - } } } @@ -423,37 +410,3 @@ void NavBotExt::Hook_PlayerRunCommand(CUserCmd* usercmd, IMoveHelper* movehelper RETURN_META(MRES_IGNORED); } - -int NavBotExt::Hook_CBaseEntity_OnTakeDamage_Alive(const CTakeDamageInfo& info) -{ - CBaseEntity* victim = META_IFACEPTR(CBaseEntity); - int index = gamehelpers->EntityToBCompatRef(victim); - - CBaseBot* bot = extmanager->GetBotByIndex(index); - - if (bot) - { - bot->OnTakeDamage_Alive(info); - } - -#ifdef EXT_DEBUG - CBaseEntity* attacker = info.GetAttacker(); - CBaseEntity* inflictor = info.GetInflictor(); - - char message[256]{}; - - if (attacker) - { - auto classname = gamehelpers->GetEntityClassname(attacker); - - if (classname && classname[0]) - { - ke::SafeSprintf(message, sizeof(message), "%p <%s> %p", attacker, classname, inflictor); - } - } - - rootconsole->ConsolePrint("%s", message); -#endif // EXT_DEBUG - - RETURN_META_VALUE(MRES_IGNORED, 0); -} diff --git a/extension/extension.h b/extension/extension.h index 875790a..4d76afd 100644 --- a/extension/extension.h +++ b/extension/extension.h @@ -280,14 +280,12 @@ class NavBotExt : public SDKExtension, public IConCommandBaseAccessor, public So void Hook_GameFrame(bool simulating); void Hook_PlayerRunCommand(CUserCmd* usercmd, IMoveHelper* movehelper) const; - int Hook_CBaseEntity_OnTakeDamage_Alive(const CTakeDamageInfo& info); inline bool ShouldCallRunPlayerCommand() const { return !m_hookruncmd; } inline SourceMod::IGameConfig* GetExtensionGameData() { return m_gamedata; } private: bool m_hookruncmd; - bool m_hasbaseplayerhooks; // true if we have gamedata for ontakedamage and other hooks SourceMod::IGameConfig* m_gamedata; }; diff --git a/extension/mods/basemod.cpp b/extension/mods/basemod.cpp index a793178..70a8442 100644 --- a/extension/mods/basemod.cpp +++ b/extension/mods/basemod.cpp @@ -29,10 +29,11 @@ CBaseBot* CBaseMod::AllocateBot(edict_t* edict) void CBaseMod::RegisterGameEvents() { - auto evmanager = GetGameEventManager(); + // auto evmanager = GetGameEventManager(); - evmanager->RegisterEventReceiver(new CPlayerSpawnEvent); - evmanager->RegisterEventReceiver(new CPlayerHurtEvent); + // evmanager->RegisterEventReceiver(new CPlayerSpawnEvent); + // evmanager->RegisterEventReceiver(new CPlayerHurtEvent); + // No longer used, we now hook the vtable } const Vector& CBaseMod::GetPlayerHullMins() diff --git a/extension/mods/basemod_gameevents.cpp b/extension/mods/basemod_gameevents.cpp index c4c3df1..df92406 100644 --- a/extension/mods/basemod_gameevents.cpp +++ b/extension/mods/basemod_gameevents.cpp @@ -60,6 +60,6 @@ void CPlayerHurtEvent::OnGameEvent(IGameEvent* gameevent) if (bot != nullptr) { - bot->OnInjured(pAttacker); + // bot->OnInjured(pAttacker); } } diff --git a/extension/mods/tf2/teamfortress2mod.cpp b/extension/mods/tf2/teamfortress2mod.cpp index 2a95d1d..e473a81 100644 --- a/extension/mods/tf2/teamfortress2mod.cpp +++ b/extension/mods/tf2/teamfortress2mod.cpp @@ -142,10 +142,11 @@ void CTeamFortress2Mod::OnMapEnd() void CTeamFortress2Mod::RegisterGameEvents() { - auto em = GetGameEventManager(); - em->RegisterEventReceiver(new CPlayerSpawnEvent); - em->RegisterEventReceiver(new CPlayerHurtEvent); - em->RegisterEventReceiver(new CTF2PlayerDeathEvent); + // Not used, replaced by sourcehooks + // auto em = GetGameEventManager(); + // em->RegisterEventReceiver(new CPlayerSpawnEvent); + // em->RegisterEventReceiver(new CPlayerHurtEvent); + // em->RegisterEventReceiver(new CTF2PlayerDeathEvent); // don't call base, TF2 uses modified generic source events } diff --git a/extension/mods/tf2/tf2mod_gameevents.cpp b/extension/mods/tf2/tf2mod_gameevents.cpp index 5b9dfa7..ebb729a 100644 --- a/extension/mods/tf2/tf2mod_gameevents.cpp +++ b/extension/mods/tf2/tf2mod_gameevents.cpp @@ -6,6 +6,7 @@ #include "teamfortress2mod.h" #include "tf2mod_gameevents.h" +/* void CTF2PlayerDeathEvent::OnGameEvent(IGameEvent* gameevent) { int victim = playerhelpers->GetClientOfUserId(gameevent->GetInt("userid")); @@ -71,3 +72,4 @@ void CTF2PlayerDeathEvent::OnGameEvent(IGameEvent* gameevent) } } } +*/ \ No newline at end of file diff --git a/extension/mods/tf2/tf2mod_gameevents.h b/extension/mods/tf2/tf2mod_gameevents.h index 0f9f134..62f3cb6 100644 --- a/extension/mods/tf2/tf2mod_gameevents.h +++ b/extension/mods/tf2/tf2mod_gameevents.h @@ -4,12 +4,12 @@ #include -class CTF2PlayerDeathEvent : public IEventReceiver -{ -public: - CTF2PlayerDeathEvent() : IEventReceiver("player_death", Mods::MOD_TF2) {} - virtual void OnGameEvent(IGameEvent* gameevent) override; -}; +//class CTF2PlayerDeathEvent : public IEventReceiver +//{ +//public: +// CTF2PlayerDeathEvent() : IEventReceiver("player_death", Mods::MOD_TF2) {} +// virtual void OnGameEvent(IGameEvent* gameevent) override; +//}; #endif // !NAVBOT_TF2_MOD_GAME_EVENTS_H_ diff --git a/extension/navmesh/nav_entities.h b/extension/navmesh/nav_entities.h index f065bcb..d441869 100644 --- a/extension/navmesh/nav_entities.h +++ b/extension/navmesh/nav_entities.h @@ -13,7 +13,7 @@ #define NAV_ENTITIES_H #include "nav_mesh.h" -#include +#include #include #include #include diff --git a/extension/navmesh/nav_ladder.h b/extension/navmesh/nav_ladder.h index fc65eb5..b57a180 100644 --- a/extension/navmesh/nav_ladder.h +++ b/extension/navmesh/nav_ladder.h @@ -18,7 +18,7 @@ #include #include "nav.h" -#include +#include class CUtlBuffer; class CNavMesh; diff --git a/extension/sdkports/sdk_ehandle.cpp b/extension/sdkports/sdk_ehandle.cpp new file mode 100644 index 0000000..8beda53 --- /dev/null +++ b/extension/sdkports/sdk_ehandle.cpp @@ -0,0 +1,9 @@ +#include +#include "sdk_ehandle.h" + +IHandleEntity* CBaseHandle::Get() const +{ + return g_EntList->LookupEntity(*this); +} + + diff --git a/extension/sdkports/sdk_ehandle.h b/extension/sdkports/sdk_ehandle.h new file mode 100644 index 0000000..d326a75 --- /dev/null +++ b/extension/sdkports/sdk_ehandle.h @@ -0,0 +1,138 @@ +#ifndef NAVBOT_SDKPORTS_EHANDLE_H_ +#define NAVBOT_SDKPORTS_EHANDLE_H_ +#pragma once + +#include + +// -------------------------------------------------------------------------------------------------- // +// CHandle. +// -------------------------------------------------------------------------------------------------- // +template< class T > +class CHandle : public CBaseHandle +{ +public: + + CHandle(); + CHandle(int iEntry, int iSerialNumber); + CHandle(const CBaseHandle& handle); + CHandle(T* pVal); + + // The index should have come from a call to ToInt(). If it hasn't, you're in trouble. + static CHandle FromIndex(int index); + + T* Get() const; + void Set(const T* pVal); + + operator T* (); + operator T* () const; + + bool operator !() const; + bool operator==(T* val) const; + bool operator!=(T* val) const; + const CBaseHandle& operator=(const T* val); + + T* operator->() const; +}; + + +// ----------------------------------------------------------------------- // +// Inlines. +// ----------------------------------------------------------------------- // + +template +CHandle::CHandle() +{ +} + + +template +CHandle::CHandle(int iEntry, int iSerialNumber) +{ + Init(iEntry, iSerialNumber); +} + + +template +CHandle::CHandle(const CBaseHandle& handle) + : CBaseHandle(handle) +{ +} + + +template +CHandle::CHandle(T* pObj) +{ + Term(); + Set(pObj); +} + + +template +inline CHandle CHandle::FromIndex(int index) +{ + CHandle ret; + ret.m_Index = index; + return ret; +} + + +template +inline T* CHandle::Get() const +{ + return (T*)CBaseHandle::Get(); +} + + +template +inline CHandle::operator T* () +{ + return Get(); +} + +template +inline CHandle::operator T* () const +{ + return Get(); +} + + +template +inline bool CHandle::operator !() const +{ + return !Get(); +} + +template +inline bool CHandle::operator==(T* val) const +{ + return Get() == val; +} + +template +inline bool CHandle::operator!=(T* val) const +{ + return Get() != val; +} + +template +void CHandle::Set(const T* pVal) +{ + CBaseHandle::Set(reinterpret_cast(pVal)); +} + +template +inline const CBaseHandle& CHandle::operator=(const T* val) +{ + Set(val); + return *this; +} + +template +T* CHandle::operator -> () const +{ + return Get(); +} + +typedef CHandle EHANDLE; + +#endif // !NAVBOT_SDKPORTS_EHANDLE_H_ \ No newline at end of file diff --git a/extension/sdkports/sdk_takedamageinfo.h b/extension/sdkports/sdk_takedamageinfo.h new file mode 100644 index 0000000..85f70d4 --- /dev/null +++ b/extension/sdkports/sdk_takedamageinfo.h @@ -0,0 +1,10 @@ +#ifndef NAVBOT_SDKPORTS_CTAKEDAMAGEINFO_H_ +#define NAVBOT_SDKPORTS_CTAKEDAMAGEINFO_H_ +#pragma once + +// Get def from SDK unless problematic +#include "sdk_ehandle.h" +#include "takedamageinfo.h" + + +#endif // !NAVBOT_SDKPORTS_CTAKEDAMAGEINFO_H_ diff --git a/extension/util/Handle.cpp b/extension/util/Handle.cpp deleted file mode 100644 index 5736749..0000000 --- a/extension/util/Handle.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Handle.cpp - * - * Created on: Apr 15, 2017 - */ - -#include "Handle.h" -#include - -#include - -/* -IHandleEntity* CBaseHandle::Get() const -{ - extern CGlobalVars *gpGlobals; - for (int i = 0; i < gpGlobals->maxEntities; i++) { - extern IVEngineServer* engine; - edict_t* ent = engine->PEntityOfEntIndex(i); - if (ent != nullptr && m_Index == ent->m_NetworkSerialNumber) { - return ent->GetIServerEntity(); - } - } - return nullptr; -} -*/ - -IHandleEntity* CBaseHandle::Get() const -{ - return g_EntList->LookupEntity(*this); -} diff --git a/extension/util/Handle.h b/extension/util/Handle.h deleted file mode 100644 index b2a06fe..0000000 --- a/extension/util/Handle.h +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Handle.h - * - * Created on: Apr 15, 2017 - */ - -#ifndef UTILS_VALVE_NAVMESH_HANDLE_H_ -#define UTILS_VALVE_NAVMESH_HANDLE_H_ - -#include - -class CBaseEntity; - -// -------------------------------------------------------------------------------------------------- // -// CHandle. -// -------------------------------------------------------------------------------------------------- // -template -class CHandle: public CBaseHandle { -public: - - CHandle() { - - } - - CHandle(int iEntry, int iSerialNumber) { - Init(iEntry, iSerialNumber); - - } - - CHandle(const CBaseHandle &handle) : - CBaseHandle(handle) { - - } - CHandle(T *pVal) { - Term(); - Set(pVal); - } - - // The index should have come from a call to ToInt(). If it hasn't, you're in trouble. - static CHandle FromIndex(int index) { - CHandle ret; - ret.m_Index = index; - return ret; - } - - T* Get() const { - return (T*)CBaseHandle::Get(); - } - - void Set(const T* pVal) { - CBaseHandle::Set(reinterpret_cast(pVal)); - } - operator T*() { - return Get(); - } - - operator T*() const { - return Get(); - } - - bool operator !() const { - return !Get(); - } - - bool operator==(T *val) const { - return Get() == val; - } - - bool operator!=(T *val) const { - return Get() != val; - } - - const CBaseHandle& operator=(const T *val) { - Set (val); - return *this; - } - - T* operator->() const { - return Get(); - } -}; - -typedef CHandle EHANDLE; - -#endif /* UTILS_VALVE_NAVMESH_HANDLE_H_ */ diff --git a/gamedata/navbot.games/game.dod.txt b/gamedata/navbot.games/game.dod.txt index 82c0769..c31732d 100644 --- a/gamedata/navbot.games/game.dod.txt +++ b/gamedata/navbot.games/game.dod.txt @@ -1,12 +1,26 @@ "Games" { /* OnPlayerRunCMD Hook */ - "#default" + "dod" { "Keys" { // If a mod needs to hook OnPlayerRunCMD, set this to 1 "HookPlayerRunCMD" "1" } + + "Offsets" + { + "Event_Killed" + { + "windows" "66" + "linux" "67" + } + "Event_KilledOther" + { + "windows" "67" + "linux" "68" + } + } } } diff --git a/gamedata/navbot.games/game.tf.txt b/gamedata/navbot.games/game.tf.txt new file mode 100644 index 0000000..03174d3 --- /dev/null +++ b/gamedata/navbot.games/game.tf.txt @@ -0,0 +1,23 @@ +"Games" +{ + "tf" + { + "Offsets" + { + "Event_Killed" + { + "windows" "66" + "windows64" "66" + "linux" "67" + "linux64" "67" + } + "Event_KilledOther" + { + "windows" "67" + "windows64" "67" + "linux" "68" + "linux64" "68" + } + } + } +}