Skip to content

Commit

Permalink
More AI Events
Browse files Browse the repository at this point in the history
  • Loading branch information
caxanga334 committed Jun 28, 2024
1 parent a84967a commit 5c1723b
Show file tree
Hide file tree
Showing 12 changed files with 343 additions and 4 deletions.
84 changes: 84 additions & 0 deletions extension/bot/interfaces/event_listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class IEventListener
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
virtual void OnRoundStateChanged(); // When the round state changes (IE: round start,end, freeze time end, setup time end, etc...)
virtual void OnFlagTaken(CBaseEntity* flag); // CTF: Flag was stolen
virtual void OnFlagDropped(CBaseEntity* flag); // CTF: Flag was dropped
virtual void OnControlPointCaptured(CBaseEntity* point); // When a control point is captured
virtual void OnControlPointLost(CBaseEntity* point); // When a control point is lost
virtual void OnControlPointContested(CBaseEntity* point); // When a control point is under siege
};

inline void IEventListener::OnTestEventPropagation()
Expand Down Expand Up @@ -220,6 +226,84 @@ inline void IEventListener::OnSound(edict_t* source, const Vector& position, Sou
}
}

inline void IEventListener::OnRoundStateChanged()
{
auto vec = GetListenerVector();

if (vec)
{
for (auto listener : *vec)
{
listener->OnRoundStateChanged();
}
}
}

inline void IEventListener::OnFlagTaken(CBaseEntity* flag)
{
auto vec = GetListenerVector();

if (vec)
{
for (auto listener : *vec)
{
listener->OnFlagTaken(flag);
}
}
}

inline void IEventListener::OnFlagDropped(CBaseEntity* flag)
{
auto vec = GetListenerVector();

if (vec)
{
for (auto listener : *vec)
{
listener->OnFlagDropped(flag);
}
}
}

inline void IEventListener::OnControlPointCaptured(CBaseEntity* point)
{
auto vec = GetListenerVector();

if (vec)
{
for (auto listener : *vec)
{
listener->OnControlPointCaptured(point);
}
}
}

inline void IEventListener::OnControlPointLost(CBaseEntity* point)
{
auto vec = GetListenerVector();

if (vec)
{
for (auto listener : *vec)
{
listener->OnControlPointLost(point);
}
}
}

inline void IEventListener::OnControlPointContested(CBaseEntity* point)
{
auto vec = GetListenerVector();

if (vec)
{
for (auto listener : *vec)
{
listener->OnControlPointContested(point);
}
}
}


#endif // !SMNAV_BOT_EVENT_LISTENER_H_

36 changes: 36 additions & 0 deletions extension/bot/interfaces/tasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,12 @@ class AITask : public IEventListener, public IDecisionQuery
virtual TaskEventResponseResult<BotClass> OnSight(BotClass* bot, edict_t* subject) { return TryContinue(); }
virtual TaskEventResponseResult<BotClass> OnLostSight(BotClass* bot, edict_t* subject) { return TryContinue(); }
virtual TaskEventResponseResult<BotClass> OnSound(BotClass* bot, edict_t* source, const Vector& position, SoundType type, const int volume) { return TryContinue(); }
virtual TaskEventResponseResult<BotClass> OnRoundStateChanged(BotClass* bot) { return TryContinue(); }
virtual TaskEventResponseResult<BotClass> OnFlagTaken(BotClass* bot, CBaseEntity* flag) { return TryContinue(); }
virtual TaskEventResponseResult<BotClass> OnFlagDropped(BotClass* bot, CBaseEntity* flag) { return TryContinue(); }
virtual TaskEventResponseResult<BotClass> OnControlPointCaptured(BotClass* bot, CBaseEntity* point) { return TryContinue(); }
virtual TaskEventResponseResult<BotClass> OnControlPointLost(BotClass* bot, CBaseEntity* point) { return TryContinue(); }
virtual TaskEventResponseResult<BotClass> OnControlPointContested(BotClass* bot, CBaseEntity* point) { return TryContinue(); }

/**
* @brief The task that comes after this
Expand Down Expand Up @@ -1019,6 +1025,36 @@ class AITask : public IEventListener, public IDecisionQuery
{
PROPAGATE_TASK_EVENT_WITH_4_ARGS(OnSound, source, position, type, volume);
}

void OnRoundStateChanged() override final
{
PROPAGATE_TASK_EVENT_WITH_NO_ARGS(OnRoundStateChanged);
}

void OnFlagTaken(CBaseEntity* flag) override final
{
PROPAGATE_TASK_EVENT_WITH_1_ARGS(OnFlagTaken, flag);
}

void OnFlagDropped(CBaseEntity* flag) override final
{
PROPAGATE_TASK_EVENT_WITH_1_ARGS(OnFlagDropped, flag);
}

void OnControlPointCaptured(CBaseEntity* point) override final
{
PROPAGATE_TASK_EVENT_WITH_1_ARGS(OnControlPointCaptured, point);
}

void OnControlPointLost(CBaseEntity* point) override final
{
PROPAGATE_TASK_EVENT_WITH_1_ARGS(OnControlPointLost, point);
}

void OnControlPointContested(CBaseEntity* point) override final
{
PROPAGATE_TASK_EVENT_WITH_1_ARGS(OnControlPointContested, point);
}
};

template<typename BotClass>
Expand Down
3 changes: 1 addition & 2 deletions extension/bot/tf2/tf2bot.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CTF2Bot : public CBaseBot
CTF2BotMovement* GetMovementInterface() const override { return m_tf2movement.get(); }
CTF2BotSensor* GetSensorInterface() const override { return m_tf2sensor.get(); }
CTF2BotBehavior* GetBehaviorInterface() const override { return m_tf2behavior.get(); }
CTF2BotSpyMonitor* GetSpyMonitorInterface() const { return nullptr; }
CTF2BotSpyMonitor* GetSpyMonitorInterface() const { return m_tf2spymonitor.get(); }
int GetMaxHealth() const override;

TeamFortress2::TFClassType GetMyClassType() const;
Expand Down Expand Up @@ -116,7 +116,6 @@ class CTF2Bot : public CBaseBot
CBaseHandle m_myDispenser;
CBaseHandle m_myTeleporterEntrance;
CBaseHandle m_myTeleporterExit;
int m_knownspythink;
CTF2BotUpgradeManager m_upgrademan;

static constexpr float medic_patient_health_critical_level() { return 0.3f; }
Expand Down
7 changes: 7 additions & 0 deletions extension/concommands_debug.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <memory>
#include <string>
#include <sstream>
#include <chrono>

#include <extension.h>
#include <manager.h>
Expand Down Expand Up @@ -127,7 +128,13 @@ CON_COMMAND(sm_navbot_debug_pathfind, "Path finding debug.")

ShortestPathCost cost;
CNavArea* closest = nullptr;
auto tstart = std::chrono::high_resolution_clock::now();
bool path = NavAreaBuildPath(startArea, goalArea, &end, cost, &closest, 10000.0f, -2);
auto tend = std::chrono::high_resolution_clock::now();

const std::chrono::duration<double, std::milli> millis = (tend - tstart);

Msg("NavAreaBuildPath took %f ms.\n", millis.count());

CNavArea* from = nullptr;
CNavArea* to = nullptr;
Expand Down
28 changes: 28 additions & 0 deletions extension/entities/tf2/tf_entities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,31 @@ int tfentities::HBaseObject::GetUpgradeMetal() const
entprops->GetEntProp(GetIndex(), Prop_Send, "m_iUpgradeMetal", value);
return value;
}

int tfentities::HTeamControlPoint::GetGroup() const
{
int value = 0;
entprops->GetEntProp(GetIndex(), Prop_Data, "m_iCPGroup", value);
return value;
}

int tfentities::HTeamControlPoint::GetPointIndex() const
{
int value = 0;
entprops->GetEntProp(GetIndex(), Prop_Data, "m_iPointIndex", value);
return value;
}

TeamFortress2::TFTeam tfentities::HTeamControlPoint::GetDefaultOwner() const
{
int value = 0;
entprops->GetEntProp(GetIndex(), Prop_Data, "m_iDefaultOwner", value);
return static_cast<TeamFortress2::TFTeam>(value);
}

bool tfentities::HTeamControlPoint::IsLocked() const
{
int value = 0;
entprops->GetEntProp(GetIndex(), Prop_Data, "m_bLocked", value);
return value != 0;
}
13 changes: 13 additions & 0 deletions extension/entities/tf2/tf_entities.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ namespace tfentities
}
inline float GetUpgradeProgress() const { return static_cast<float>(GetUpgradeMetal()) / static_cast<float>(GetMaxUpgradeMetal()); }
};

class HTeamControlPoint : public HTFBaseEntity
{
public:
HTeamControlPoint(edict_t* entity) : HTFBaseEntity(entity) {}
HTeamControlPoint(CBaseEntity* entity) : HTFBaseEntity(entity) {}

int GetGroup() const;
int GetPointIndex() const;
int GetArea() const { return GetPointIndex(); }
TeamFortress2::TFTeam GetDefaultOwner() const;
bool IsLocked() const;
};
}

#endif // !SMNAV_TFENTITIES_CAPTUREFLAG_H_
Expand Down
8 changes: 8 additions & 0 deletions extension/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ void CExtManager::UpdateBotQuota()
if (m_quotatarget == 0)
return;

if (!TheNavMesh->IsLoaded())
return;

int humans = 0;
int navbots = 0;
int otherbots = 0;
Expand Down Expand Up @@ -464,6 +467,11 @@ void CExtManager::OnQuotaModeCvarChanged(IConVar* var, const char* pOldValue, fl
extmanager->SetBotQuotaMode(BotQuotaMode::QUOTA_FIXED);
smutils->LogError(myself, "Unknown bot quota mode \"%s\"!", mode);
}

if (!TheNavMesh->IsLoaded())
{
Warning("[NAVBOT] Nav mesh not loaded, bot quota unavailable.\n");
}
}

void CExtManager::OnQuotaTargetCvarChanged(IConVar* var, const char* pOldValue, float flOldValue)
Expand Down
2 changes: 2 additions & 0 deletions extension/mods/tf2/teamfortress2_shareddefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ namespace TeamFortress2
constexpr int TF_AMMO_GRENADES2 = 5;
constexpr int TF_AMMO_GRENADES3 = 6;

constexpr size_t TF_MAX_CONTROL_POINTS = 8;

enum TFClassType
{
TFClass_Unknown = 0,
Expand Down
Loading

0 comments on commit 5c1723b

Please sign in to comment.