Skip to content

Commit

Permalink
Fix Compatibility Between 32/64 TF2
Browse files Browse the repository at this point in the history
  • Loading branch information
caxanga334 committed Jun 21, 2024
1 parent 05cd66b commit b62e679
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 11 deletions.
50 changes: 49 additions & 1 deletion extension/bot/tf2/tasks/medic/tf2bot_medic_main_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
#include <extension.h>
#include <util/helpers.h>
#include <bot/tf2/tf2bot.h>
#include <mods/tf2/teamfortress2mod.h>
#include <mods/tf2/tf2lib.h>
#include <entities/tf2/tf_entities.h>
#include "tf2bot_medic_retreat_task.h"
#include "tf2bot_medic_revive_task.h"
#include "tf2bot_medic_main_task.h"

#undef max
Expand All @@ -31,11 +34,26 @@ TaskResult<CTF2Bot> 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.");

Expand All @@ -58,6 +76,13 @@ TaskResult<CTF2Bot> 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();
}
Expand Down Expand Up @@ -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<float>::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;
}
4 changes: 3 additions & 1 deletion extension/bot/tf2/tasks/medic/tf2bot_medic_main_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class CTF2BotMedicMainTask : public AITask<CTF2Bot>

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; }
};
Expand Down
41 changes: 40 additions & 1 deletion extension/bot/tf2/tasks/medic/tf2bot_medic_retreat_task.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
#include <limits>

#include <extension.h>
#include <util/helpers.h>
#include <util/librandom.h>
#include <bot/tf2/tf2bot.h>
#include <mods/tf2/tf2lib.h>
#include "tf2bot_medic_retreat_task.h"

#undef max
#undef min
#undef clamp

TaskResult<CTF2Bot> CTF2BotMedicRetreatTask::OnTaskStart(CTF2Bot* bot, AITask<CTF2Bot>* pastTask)
{
m_goal = bot->GetHomePos();
m_goal = GetRetreatPosition(bot);

CTF2BotPathCost cost(bot);
if (!m_nav.ComputePathToPosition(bot, m_goal, cost, 0.0f, true))
{
Expand Down Expand Up @@ -55,3 +62,35 @@ TaskResult<CTF2Bot> 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<float>::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();
}
2 changes: 2 additions & 0 deletions extension/bot/tf2/tasks/medic/tf2bot_medic_retreat_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class CTF2BotMedicRetreatTask : public AITask<CTF2Bot>
CountdownTimer m_repathtimer;

static constexpr float home_range() { return 256.0f; }

Vector GetRetreatPosition(CTF2Bot* me) const;
};


Expand Down
12 changes: 6 additions & 6 deletions extension/mods/tf2/nav/tfnavarea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ void CTFNavArea::Save(std::fstream& filestream, uint32_t version)
{
CNavArea::Save(filestream, version); // Save base first

filestream.write(reinterpret_cast<char*>(&m_tfattributes), sizeof(m_tfattributes));
filestream.write(reinterpret_cast<char*>(&m_tfpathattributes), sizeof(m_tfpathattributes));
filestream.write(reinterpret_cast<char*>(&m_mvmattributes), sizeof(m_mvmattributes));
filestream.write(reinterpret_cast<char*>(&m_tfattributes), sizeof(int));
filestream.write(reinterpret_cast<char*>(&m_tfpathattributes), sizeof(int));
filestream.write(reinterpret_cast<char*>(&m_mvmattributes), sizeof(int));
}

NavErrorType CTFNavArea::Load(std::fstream& filestream, uint32_t version, uint32_t subVersion)
Expand All @@ -33,9 +33,9 @@ NavErrorType CTFNavArea::Load(std::fstream& filestream, uint32_t version, uint32

if (subVersion > 0)
{
filestream.read(reinterpret_cast<char*>(&m_tfattributes), sizeof(m_tfattributes));
filestream.read(reinterpret_cast<char*>(&m_tfpathattributes), sizeof(m_tfpathattributes));
filestream.read(reinterpret_cast<char*>(&m_mvmattributes), sizeof(m_mvmattributes));
filestream.read(reinterpret_cast<char*>(&m_tfattributes), sizeof(int));
filestream.read(reinterpret_cast<char*>(&m_tfpathattributes), sizeof(int));
filestream.read(reinterpret_cast<char*>(&m_mvmattributes), sizeof(int));

if (!filestream.good())
{
Expand Down
6 changes: 6 additions & 0 deletions extension/navmesh/nav_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
#include <filesystem.h>
#include <eiface.h>

/*
* 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:
Expand Down
2 changes: 1 addition & 1 deletion extension/navmesh/nav_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
3 changes: 2 additions & 1 deletion premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit b62e679

Please sign in to comment.