From b62e6790fe7d35fbbe4d1050c4be3e733f03361f Mon Sep 17 00:00:00 2001 From: caxanga334 <10157643+caxanga334@users.noreply.github.com> Date: Fri, 21 Jun 2024 14:31:38 -0300 Subject: [PATCH] Fix Compatibility Between 32/64 TF2 --- .../tasks/medic/tf2bot_medic_main_task.cpp | 50 ++++++++++++++++++- .../tf2/tasks/medic/tf2bot_medic_main_task.h | 4 +- .../tasks/medic/tf2bot_medic_retreat_task.cpp | 41 ++++++++++++++- .../tasks/medic/tf2bot_medic_retreat_task.h | 2 + extension/mods/tf2/nav/tfnavarea.cpp | 12 ++--- extension/navmesh/nav_file.cpp | 6 +++ extension/navmesh/nav_mesh.h | 2 +- premake5.lua | 3 +- 8 files changed, 109 insertions(+), 11 deletions(-) diff --git a/extension/bot/tf2/tasks/medic/tf2bot_medic_main_task.cpp b/extension/bot/tf2/tasks/medic/tf2bot_medic_main_task.cpp index 80949b5..d37b00a 100644 --- a/extension/bot/tf2/tasks/medic/tf2bot_medic_main_task.cpp +++ b/extension/bot/tf2/tasks/medic/tf2bot_medic_main_task.cpp @@ -4,8 +4,11 @@ #include #include #include +#include #include +#include #include "tf2bot_medic_retreat_task.h" +#include "tf2bot_medic_revive_task.h" #include "tf2bot_medic_main_task.h" #undef max @@ -31,11 +34,26 @@ TaskResult CTF2BotMedicMainTask::OnTaskUpdate(CTF2Bot* bot) LookForPatients(bot); } + if (CTeamFortress2Mod::GetTF2Mod()->GetCurrentGameMode() == TeamFortress2::GameModeType::GM_MVM) + { + if (!tf2lib::IsPlayerInvulnerable(bot->GetIndex()) || !IsCurrentPatientValid()) + { + CBaseEntity* marker = FindReviveMarker(bot); + + if (marker != nullptr) + { + return PauseFor(new CTF2BotMedicReviveTask(marker), "Reviving dead teammate."); + } + } + } + if (IsCurrentPatientValid()) { + m_haspatienttimer.Start(); EquipMedigun(bot); + PathToPatient(bot); - if (bot->GetRangeTo(m_patient.ToEdict()) <= medigun_max_range()) + if (bot->GetRangeTo(m_patient.ToEdict()) <= medigun_max_range() && bot->GetSensorInterface()->IsLineOfSightClear(m_patient.Get())) { bot->GetControlInterface()->AimAt(m_patient.ToEdict(), IPlayerController::LOOK_COMBAT, 0.5f, "Looking at my patient to heal them."); @@ -58,6 +76,13 @@ TaskResult CTF2BotMedicMainTask::OnTaskUpdate(CTF2Bot* bot) } } } + else + { + if (!LookForPatients(bot) && m_haspatienttimer.IsGreaterThen(3.0f)) + { + return PauseFor(new CTF2BotMedicRetreatTask, "No patient, retreating to find one."); + } + } return Continue(); } @@ -229,3 +254,26 @@ float CTF2BotMedicMainTask::GetUbercharge(CTF2Bot* me) return 0.0f; } + +CBaseEntity* CTF2BotMedicMainTask::FindReviveMarker(CTF2Bot* me) +{ + CBaseEntity* marker = nullptr; + float t = std::numeric_limits::max(); + Vector origin = me->GetAbsOrigin(); + + UtilHelpers::ForEachEntityOfClassname("entity_revive_marker", [&marker, &t, &origin](int index, edict_t* edict, CBaseEntity* entity) -> bool { + tfentities::HTFBaseEntity be(entity); + + float d = (be.GetAbsOrigin() - origin).Length(); + + if (d < t && d <= 2048.0f) + { + marker = entity; + t = d; + } + + return true; + }); + + return marker; +} diff --git a/extension/bot/tf2/tasks/medic/tf2bot_medic_main_task.h b/extension/bot/tf2/tasks/medic/tf2bot_medic_main_task.h index 7500cd8..fa1fae5 100644 --- a/extension/bot/tf2/tasks/medic/tf2bot_medic_main_task.h +++ b/extension/bot/tf2/tasks/medic/tf2bot_medic_main_task.h @@ -45,7 +45,9 @@ class CTF2BotMedicMainTask : public AITask float GetUbercharge(CTF2Bot* me); - static constexpr float MEDIC_MAX_DISTANCE = 300.0f; + CBaseEntity* FindReviveMarker(CTF2Bot* me); + + static constexpr float MEDIC_MAX_DISTANCE = 200.0f; static constexpr auto medigun_max_range() { return 420.0f; } static constexpr auto heal_dot_tolerance() { return 0.95f; } }; diff --git a/extension/bot/tf2/tasks/medic/tf2bot_medic_retreat_task.cpp b/extension/bot/tf2/tasks/medic/tf2bot_medic_retreat_task.cpp index 215db98..8c43d02 100644 --- a/extension/bot/tf2/tasks/medic/tf2bot_medic_retreat_task.cpp +++ b/extension/bot/tf2/tasks/medic/tf2bot_medic_retreat_task.cpp @@ -1,3 +1,5 @@ +#include + #include #include #include @@ -5,9 +7,14 @@ #include #include "tf2bot_medic_retreat_task.h" +#undef max +#undef min +#undef clamp + TaskResult CTF2BotMedicRetreatTask::OnTaskStart(CTF2Bot* bot, AITask* pastTask) { - m_goal = bot->GetHomePos(); + m_goal = GetRetreatPosition(bot); + CTF2BotPathCost cost(bot); if (!m_nav.ComputePathToPosition(bot, m_goal, cost, 0.0f, true)) { @@ -55,3 +62,35 @@ TaskResult CTF2BotMedicRetreatTask::OnTaskUpdate(CTF2Bot* bot) return Continue(); } + +// Retreat to the nearest alive teammate or to my spawn point if none is found +Vector CTF2BotMedicRetreatTask::GetRetreatPosition(CTF2Bot* me) const +{ + CBaseEntity* nearestTeammate = nullptr; + float t = std::numeric_limits::max(); + auto myteam = me->GetMyTFTeam(); + Vector origin = me->GetAbsOrigin(); + Vector goal; + + UtilHelpers::ForEachPlayer([&t, &nearestTeammate, &myteam, &origin, &goal](int client, edict_t* entity, SourceMod::IGamePlayer* player) { + if (player->IsInGame() && UtilHelpers::IsPlayerAlive(client) && tf2lib::GetEntityTFTeam(client) == myteam) + { + CBaseExtPlayer them(entity); + float d = (them.GetAbsOrigin() - origin).Length(); + + if (d < t) + { + t = d; + nearestTeammate = them.GetEntity(); + goal = them.GetAbsOrigin(); + } + } + }); + + if (nearestTeammate != nullptr) + { + return goal; + } + + return me->GetHomePos(); +} diff --git a/extension/bot/tf2/tasks/medic/tf2bot_medic_retreat_task.h b/extension/bot/tf2/tasks/medic/tf2bot_medic_retreat_task.h index 206c6ed..ccb77f1 100644 --- a/extension/bot/tf2/tasks/medic/tf2bot_medic_retreat_task.h +++ b/extension/bot/tf2/tasks/medic/tf2bot_medic_retreat_task.h @@ -22,6 +22,8 @@ class CTF2BotMedicRetreatTask : public AITask CountdownTimer m_repathtimer; static constexpr float home_range() { return 256.0f; } + + Vector GetRetreatPosition(CTF2Bot* me) const; }; diff --git a/extension/mods/tf2/nav/tfnavarea.cpp b/extension/mods/tf2/nav/tfnavarea.cpp index 52213cc..4ea500f 100644 --- a/extension/mods/tf2/nav/tfnavarea.cpp +++ b/extension/mods/tf2/nav/tfnavarea.cpp @@ -17,9 +17,9 @@ void CTFNavArea::Save(std::fstream& filestream, uint32_t version) { CNavArea::Save(filestream, version); // Save base first - filestream.write(reinterpret_cast(&m_tfattributes), sizeof(m_tfattributes)); - filestream.write(reinterpret_cast(&m_tfpathattributes), sizeof(m_tfpathattributes)); - filestream.write(reinterpret_cast(&m_mvmattributes), sizeof(m_mvmattributes)); + filestream.write(reinterpret_cast(&m_tfattributes), sizeof(int)); + filestream.write(reinterpret_cast(&m_tfpathattributes), sizeof(int)); + filestream.write(reinterpret_cast(&m_mvmattributes), sizeof(int)); } NavErrorType CTFNavArea::Load(std::fstream& filestream, uint32_t version, uint32_t subVersion) @@ -33,9 +33,9 @@ NavErrorType CTFNavArea::Load(std::fstream& filestream, uint32_t version, uint32 if (subVersion > 0) { - filestream.read(reinterpret_cast(&m_tfattributes), sizeof(m_tfattributes)); - filestream.read(reinterpret_cast(&m_tfpathattributes), sizeof(m_tfpathattributes)); - filestream.read(reinterpret_cast(&m_mvmattributes), sizeof(m_mvmattributes)); + filestream.read(reinterpret_cast(&m_tfattributes), sizeof(int)); + filestream.read(reinterpret_cast(&m_tfpathattributes), sizeof(int)); + filestream.read(reinterpret_cast(&m_mvmattributes), sizeof(int)); if (!filestream.good()) { diff --git a/extension/navmesh/nav_file.cpp b/extension/navmesh/nav_file.cpp index 595c8e1..5d4ee3c 100644 --- a/extension/navmesh/nav_file.cpp +++ b/extension/navmesh/nav_file.cpp @@ -32,6 +32,12 @@ #include #include +/* +* Reminders: +* size_t / std::size_t will change size between 32/64 bits. If saving a size_t, save as uint64_t! +*/ + + class NavMeshFileHeader { public: diff --git a/extension/navmesh/nav_mesh.h b/extension/navmesh/nav_mesh.h index 5b58646..fa780ed 100644 --- a/extension/navmesh/nav_mesh.h +++ b/extension/navmesh/nav_mesh.h @@ -182,7 +182,7 @@ enum GetNavAreaFlags_t class PlaceDirectory { public: - typedef std::size_t IndexType; // Loaded/Saved as UnsignedShort. Change this and you'll have to version. + typedef uint32_t IndexType; // Loaded/Saved as UnsignedShort. Change this and you'll have to version. PlaceDirectory(void) { diff --git a/premake5.lua b/premake5.lua index 8d5b3fd..b112c8a 100644 --- a/premake5.lua +++ b/premake5.lua @@ -121,7 +121,8 @@ workspace "navbot" defines { "NDEBUG" } targetdir "build/bin/%{cfg.architecture}/release" optimize "Full" - flags { "LinkTimeOptimization" } + flags { "LinkTimeOptimization" } + visibility "Hidden" filter { "platforms:Win32" } system "Windows"