Skip to content

Commit

Permalink
Bot Behaviour / Waypoint related improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
pongo1231 committed Aug 11, 2019
1 parent b93c305 commit 3bfdadf
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 50 deletions.
1 change: 1 addition & 0 deletions Pongbot/Bot/BotManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class BotManager : public IConVarBase, public IGameFramable
void KickBot(Bot* bot);
void KickAllBots();
bool BotsInGame() const;

virtual void OnGameFrame();
};

Expand Down
5 changes: 5 additions & 0 deletions Pongbot/Bot/Brain/BotBrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ void BotBrain::OnThink()
OnSpawn();
}

if (!_ObjectivesProvider->IsRoundActive())
{
return;
}

float engineTime = Engine->Time();
if (_ThinkTime < engineTime)
{
Expand Down
13 changes: 8 additions & 5 deletions Pongbot/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ IServerPluginHelpers* IIServerPluginHelpers = nullptr;
IServerGameClients* IIServerGameClients = nullptr;
IEffects* IIEffects = nullptr;
IEngineTrace* IIEngineTrace = nullptr;
IGameEventManager2* IIGameEventManager = nullptr;

std::vector<IGameFramable*> _IGameFramables;

Expand All @@ -34,22 +35,24 @@ SH_DECL_HOOK1_void(IServerGameDLL, GameFrame, SH_NOATTRIB, 0, bool);
bool Main::Load(PluginId id, ISmmAPI* ismm, char* error, size_t maxlen, bool late)
{
PLUGIN_SAVEVARS();

Util::DebugLog("DEBUG MODE IS ON");

GET_V_IFACE_CURRENT(GetEngineFactory, Engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
float initStartTime = Engine->Time();
Util::Log("Initializing...");

GET_V_IFACE_CURRENT(GetServerFactory, IIBotManager, IBotManager, INTERFACEVERSION_PLAYERBOTMANAGER);
GET_V_IFACE_CURRENT(GetServerFactory, Server, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
GET_V_IFACE_CURRENT(GetServerFactory, IIPlayerInfoManager, IPlayerInfoManager, INTERFACEVERSION_PLAYERINFOMANAGER);
GET_V_IFACE_CURRENT(GetEngineFactory, IIServerPluginHelpers, IServerPluginHelpers, INTERFACEVERSION_ISERVERPLUGINHELPERS);
GET_V_IFACE_CURRENT(GetServerFactory, IIServerGameClients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
GET_V_IFACE_CURRENT(GetServerFactory, IIEffects, IEffects, IEFFECTS_INTERFACE_VERSION);
GET_V_IFACE_CURRENT(GetEngineFactory, IIEngineTrace, IEngineTrace, INTERFACEVERSION_ENGINETRACE_SERVER);

Util::DebugLog("DEBUG MODE IS ON");
GET_V_IFACE_CURRENT(GetEngineFactory, IIGameEventManager, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);

SH_ADD_HOOK(IServerGameDLL, GameFrame, Server, SH_MEMBER(this, &Main::_OnGameFrame), true);

float initStartTime = Engine->Time();
Util::Log("Initializing...");

EventHooksProvider::Init();
EntityProvider::Init();
TFClassInfoProvider::Init();
Expand Down
10 changes: 0 additions & 10 deletions Pongbot/Main.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,4 @@ class Main : public ISmmPlugin
void _OnGameFrame(bool simulation);
};

extern Main _Main;
extern IVEngineServer* Engine;
extern IBotManager* IIBotManager;
extern IServerGameDLL* Server;
extern IPlayerInfoManager* IIPlayerInfoManager;
extern IServerPluginHelpers* IIServerPluginHelpers;
extern IServerGameClients* IIServerGameClients;
extern IEffects* IIEffects;
extern IEngineTrace* IIEngineTrace;

PLUGIN_GLOBALVARS();
3 changes: 2 additions & 1 deletion Pongbot/TF2/Entity/EntityProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include "Entity.h"
#include <vector>

class EntityProvider : public IGameFramable {
class EntityProvider : public IGameFramable
{
private:
EntityProvider()
{}
Expand Down
56 changes: 53 additions & 3 deletions Pongbot/TF2/Events/EventHooksProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
#include "../../Bot/BotManager.h"
#include "../../ConVarHolder.h"
#include "../../Util.h"
#include <metamod/ISmmAPI.h>

extern IVEngineServer* Engine;
extern IServerGameDLL* Server;
extern SourceHook::ISourceHook* g_SHPtr;
extern PluginId g_PLID;
extern IGameEventManager2* IIGameEventManager;

SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, 0, bool, char const*, char const*,
char const*, char const*, bool, bool);
SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, 0, bool, char const*, char const*, char const*, char const*, bool, bool);
SH_DECL_HOOK2(IGameEventManager2, FireEvent, SH_NOATTRIB, 0, bool, IGameEvent*, bool);

EventHooksProvider* _EventHooksProvider = nullptr;

Expand All @@ -24,6 +24,11 @@ void EventHooksProvider::Init()

_EventHooksProvider = new EventHooksProvider();
SH_ADD_HOOK(IServerGameDLL, LevelInit, Server, SH_MEMBER(_EventHooksProvider, &EventHooksProvider::_OnLevelInit), true);
SH_ADD_HOOK(IGameEventManager2, FireEvent, IIGameEventManager, SH_MEMBER(_EventHooksProvider, &EventHooksProvider::_OnFireEvent), false);
SH_ADD_HOOK(IGameEventManager2, FireEvent, IIGameEventManager, SH_MEMBER(_EventHooksProvider, &EventHooksProvider::_OnFireEventPost), true);

IIGameEventManager->AddListener(_EventHooksProvider, "teamplay_round_start", true);
IIGameEventManager->AddListener(_EventHooksProvider, "teamplay_round_active", true);
}
}

Expand All @@ -33,7 +38,12 @@ void EventHooksProvider::Destroy()
{
Util::DebugLog("DESTROY EventHooksProvider");

IIGameEventManager->RemoveListener(_EventHooksProvider);

SH_REMOVE_HOOK(IServerGameDLL, LevelInit, Server, SH_MEMBER(_EventHooksProvider, &EventHooksProvider::_OnLevelInit), true);
SH_REMOVE_HOOK(IGameEventManager2, FireEvent, IIGameEventManager, SH_MEMBER(_EventHooksProvider, &EventHooksProvider::_OnFireEvent), false);
SH_REMOVE_HOOK(IGameEventManager2, FireEvent, IIGameEventManager, SH_MEMBER(_EventHooksProvider, &EventHooksProvider::_OnFireEventPost), true);

delete _EventHooksProvider;
_EventHooksProvider = nullptr;
}
Expand Down Expand Up @@ -118,4 +128,44 @@ bool EventHooksProvider::_OnLevelInit(const char* pMapName, char const* pMapEnti
{
eventHooker->OnLevelInit(pMapName, pMapEntities, pOldLevel, pLandmarkName, loadGame, background);
}
}

bool EventHooksProvider::_OnFireEvent(IGameEvent* pEvent, bool bDontBroadcast) const
{
if (!pEvent)
{
return false;
}

const char* name = pEvent->GetName();
if (strcmp(name, "teamplay_round_start") == 0)
{
Util::DebugLog("Round has started!");
for (IEventHooker* eventHooker : _EventHookers)
{
eventHooker->OnRoundStart();
}
}
else if (strcmp(name, "teamplay_round_active") == 0)
{
Util::DebugLog("Round is active!");
for (IEventHooker* eventHooker : _EventHookers)
{
eventHooker->OnRoundActive();
}
}

return true;
}

bool EventHooksProvider::_OnFireEventPost(IGameEvent* pEvent, bool bDontBroadcast) const
{
// TODO: Figure out why pEvent causes a segfault here when used?

if (!pEvent)
{
return false;
}

return true;
}
6 changes: 5 additions & 1 deletion Pongbot/TF2/Events/EventHooksProvider.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#pragma once
#include "../../IGameFramable.h"
#include "../Objectives/ObjectivesProvider.h"
#include <hlsdk/public/igameevents.h>
#include <vector>

class IEventHooker;

class EventHooksProvider : public IGameFramable
class EventHooksProvider : public IGameFramable, public IGameEventListener2
{
private:
EventHooksProvider() {}
Expand All @@ -18,6 +19,7 @@ class EventHooksProvider : public IGameFramable
void RemoveEventHooker(IEventHooker* eventHooker);

virtual void OnGameFrame();
virtual void FireGameEvent(IGameEvent* pEvent) {}

private:
std::vector<IEventHooker*> _EventHookers;
Expand All @@ -26,6 +28,8 @@ class EventHooksProvider : public IGameFramable
void _CheckObjectiveUpdates();
bool _OnLevelInit(const char* pMapName, char const* pMapEntities,
char const* pOldLevel, char const* pLandmarkName, bool loadGame, bool background) const;
virtual bool _OnFireEvent(IGameEvent* pEvent, bool bDontBroadcast) const;
virtual bool _OnFireEventPost(IGameEvent* pEvent, bool bDontBroadcast) const;
};

extern EventHooksProvider* _EventHooksProvider;
2 changes: 2 additions & 0 deletions Pongbot/TF2/Events/IEventHooker.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ class IEventHooker
virtual void OnObjectiveUpdate() {}
virtual void OnLevelInit(const char* pMapName, char const* pMapEntities,
char const* pOldLevel, char const* pLandmarkName, bool loadGame, bool background) {}
virtual void OnRoundStart() {}
virtual void OnRoundActive() {}
};
17 changes: 17 additions & 0 deletions Pongbot/TF2/Objectives/ObjectivesProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ int ObjectivesProvider::GetRoundTimerStatus() const
return _RoundTimer.GetRoundTimerStatus();
}

bool ObjectivesProvider::IsRoundActive() const
{
return _RoundActive;
}

void ObjectivesProvider::OnGameFrame()
{
if (!_BotManager->BotsInGame())
Expand All @@ -86,6 +91,18 @@ void ObjectivesProvider::OnLevelInit(const char* pMapName, char const* pMapEntit
_UpdateRoundTimer();
}

void ObjectivesProvider::OnRoundStart()
{
Util::DebugLog("RoundActive = false");
_RoundActive = false;
}

void ObjectivesProvider::OnRoundActive()
{
Util::DebugLog("RoundActive = true");
_RoundActive = true;
}

void ObjectivesProvider::_UpdateCTFObjectives()
{
_RedObjectives.clear();
Expand Down
10 changes: 7 additions & 3 deletions Pongbot/TF2/Objectives/ObjectivesProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ struct Objective
int _Status;
};

class ObjectivesProvider : public IGameFramable, IEventHooker
class ObjectivesProvider : public IGameFramable, public IEventHooker
{
private:
ObjectivesProvider() {}
ObjectivesProvider() : _RoundActive(false) {}

public:
static void Init();
Expand All @@ -64,15 +64,19 @@ class ObjectivesProvider : public IGameFramable, IEventHooker
std::vector<Objective> GetBotDefendObjectives(Bot* bot) const;
std::vector<Objective> GetAllObjectives() const;
int GetRoundTimerStatus() const;
bool IsRoundActive() const;

virtual void OnGameFrame();
virtual void OnLevelInit(const char* pMapName, char const* pMapEntities,
char const* pOldLevel, char const* pLandmarkName, bool loadGame, bool background);

virtual void OnRoundStart();
virtual void OnRoundActive();

private:
std::vector<Objective> _RedObjectives;
std::vector<Objective> _BlueObjectives;
RoundTimer _RoundTimer;
bool _RoundActive;

void _UpdateCTFObjectives();
void _UpdateRoundTimer();
Expand Down
2 changes: 1 addition & 1 deletion Pongbot/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace Util
va_start(args, text);
vsnprintf(userText, sizeof(userText), text, args);
va_end(args);
sprintf(prefixedText, "[Pongbot] %s\n", userText);
sprintf(prefixedText, "[Pongbot | DEBUG] %s\n", userText);

g_SMAPI->ConPrintf(prefixedText);
#endif
Expand Down
32 changes: 10 additions & 22 deletions Pongbot/Waypoint/WaypointFileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,13 @@ void WaypointFileManager::Read()
std::ifstream file(fileName);
char fileBuffer[512];
int connectedNodeIds[256][256];
// Fill connectedNodeIds with -1 to not confuse with node id 0 instead
memset(connectedNodeIds, -1, sizeof(connectedNodeIds));

uint8_t i = 0;
int i = 0;
while (file.getline(fileBuffer, sizeof(fileBuffer)))
{
char lineBuffer[256][16];
char* context = nullptr;
uint8_t j = 0;
int j = 0;
#ifdef _WIN32
char* token = strtok_s(fileBuffer, ":", &context);
do
Expand All @@ -69,7 +67,7 @@ void WaypointFileManager::Read()
char* token = strtok_r(fileBuffer, ":", &context);
do
{
strncpy(lineBuffer[j++], token, sizeof(lineBuffer));
strncpy(lineBuffer[j++], token, sizeof(lineBuffer[j]));
}
while (token = strtok_r(nullptr, ":", &context));
#endif
Expand All @@ -79,7 +77,8 @@ void WaypointFileManager::Read()
_WaypointNodes->push_back(node);

// Store connected node ids for reference
for (uint16_t k = 5; k < 260; k++)
int k;
for (k = 5; k < 260; k++)
{
char* content = lineBuffer[k];
if (content[0] == '\\')
Expand All @@ -91,34 +90,23 @@ void WaypointFileManager::Read()
connectedNodeIds[i][k - 5] = atoi(content);
}
}
connectedNodeIds[i][k - 5] = -1;

i++;
}

// Now handle the node connections
for (i = 0; i < _WaypointNodes->size(); i++)
{
for (uint8_t j = 0; ; j++)
for (int j = 0; connectedNodeIds[i][j] != -1 && j < 255; j++)
{
if (connectedNodeIds[i][j] == -1)
for (WaypointNode* nodeToConnect : *_WaypointNodes)
{
break;
}
else
{
for (WaypointNode* nodeToConnect : *_WaypointNodes)
if (nodeToConnect->Id == connectedNodeIds[i][j])
{
if (nodeToConnect->Id == connectedNodeIds[i][j])
{
(*_WaypointNodes)[i]->ConnectToNode(nodeToConnect);
}
(*_WaypointNodes)[i]->ConnectToNode(nodeToConnect);
}
}

if (j == 255)
{
break;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Pongbot/Waypoint/WaypointFileManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class WaypointNode;

class WaypointFileManager : public IConVarBase, IEventHooker
class WaypointFileManager : public IConVarBase, public IEventHooker
{
private:
WaypointFileManager() {}
Expand Down
4 changes: 2 additions & 2 deletions Pongbot/Waypoint/WaypointManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,8 @@ CON_COMMAND(pongbot_waypoint_togglenodeflag, "Adds/Removes a flag to a waypoint
WaypointNodeFlagInfo info = pair.second;
#ifdef _WIN32
if (_strcmpi(flagName, info.Name) == 0)
#elif LINUX
if (strcmpi(flagName, info.Name) == 0)
#elif _LINUX
if (strcasecmp(flagName, info.Name) == 0)
#endif
{
int flag = pair.first;
Expand Down
1 change: 1 addition & 0 deletions Pongbot/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <hlsdk/public/shake.h>
#include <hlsdk/game/shared/IEffects.h>
#include <hlsdk/public/eiface.h>
#include <hlsdk/public/igameevents.h>
#include <hlsdk/public/game/server/iplayerinfo.h>
#include <hlsdk/public/mathlib/mathlib.h>
#include <hlsdk/public/edict.h>
Expand Down
2 changes: 1 addition & 1 deletion waypoints/ctf_turbine.pbw
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
0:2880.93:-769.362:24.0313:1:1:42:\
0:2880.93:-769.362:24.0313:1:42:1:\
1:2378.63:-759.67:24.0313:1:0:2:\
2:2378.4:-443.879:24.0313:0:1:3:44:\
3:1730.91:-449.92:24.0313:0:2:4:52:\
Expand Down

0 comments on commit 3bfdadf

Please sign in to comment.