Skip to content

Commit

Permalink
Add OnSpawn to events
Browse files Browse the repository at this point in the history
  • Loading branch information
pongo1231 committed Aug 13, 2019
1 parent fb767a7 commit 93fcf3e
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 42 deletions.
10 changes: 5 additions & 5 deletions Pongbot/Bot/Bot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void Bot::Think()
_IBotController->RunPlayerMove(&cmd);
}

const Player Bot::GetPlayer() const
Player Bot::GetPlayer() const
{
return _Player;
}
Expand All @@ -87,12 +87,12 @@ edict_t *Bot::GetEdict() const

bool Bot::Exists() const
{
return GetPlayer().Exists();
return _Player.Exists();
}

Vector Bot::GetPos() const
{
return GetPlayer().GetPos();
return _Player.GetPos();
}

Vector Bot::GetEarPos() const
Expand Down Expand Up @@ -123,7 +123,7 @@ TFClass Bot::GetClass() const

TFTeam Bot::GetTeam() const
{
return GetPlayer().GetTeam();
return _Player.GetTeam();
}

BotVisibles* Bot::GetBotVisibles() const
Expand Down Expand Up @@ -187,7 +187,7 @@ void Bot::SetSelectedWeapon(WeaponSlot weapon)

bool Bot::IsDead() const
{
return GetPlayer().IsDead();
return _Player.IsDead();
}

void Bot::ChangeClass(TFClass tfClass)
Expand Down
4 changes: 2 additions & 2 deletions Pongbot/Bot/Bot.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Bot
const char* Name;

void Think();
const Player GetPlayer() const;
Player GetPlayer() const;
edict_t* GetEdict() const;
bool Exists() const;
Vector GetPos() const;
Expand All @@ -45,7 +45,7 @@ class Bot
IServerEntity* GetIServerEntity() const;

private:
const Player _Player;
Player _Player;
edict_t* _Edict;
IPlayerInfo* _IPlayerInfo;
IBotController* _IBotController;
Expand Down
50 changes: 20 additions & 30 deletions Pongbot/Bot/Brain/BotBrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,22 @@ extern IVEngineServer* Engine;

void BotBrain::OnThink()
{
if (_GetBot()->IsDead())
if (_GetBot()->IsDead() || !_ObjectivesProvider->IsRoundActive())
{
_IsBotDead = true;
return;
}
else
{
if (_IsBotDead)
{
OnSpawn();
}

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

float engineTime = Engine->Time();
if (_ThinkTime < engineTime)
{
_ThinkTime = engineTime + _ConVarHolder->CVarBotBrainThinkTick->GetFloat();
_OnThink();
_DefaultThink();
}
float engineTime = Engine->Time();
if (_ThinkTime < engineTime)
{
_ThinkTime = engineTime + _ConVarHolder->CVarBotBrainThinkTick->GetFloat();
_OnThink();
_DefaultThink();
}

if (_HasBotTask() && _BotTask->OnThink())
{
_ClearTask();
}
if (_HasBotTask() && _BotTask->OnThink())
{
_ClearTask();
}
}

Expand Down Expand Up @@ -116,12 +104,15 @@ void BotBrain::_DefaultThink()
}
}

void BotBrain::OnSpawn()
void BotBrain::OnSpawn(Player player)
{
_ClearTask();
_ResetState();
_GetBot()->SetSelectedWeapon(WEAPON_PRIMARY);
_OnSpawn();
if (_ABot->GetPlayer() == player)
{
_ClearTask();
_ResetState();
_GetBot()->SetSelectedWeapon(WEAPON_PRIMARY);
_OnSpawn();
}
}

void BotBrain::OnObjectiveUpdate()
Expand Down Expand Up @@ -159,7 +150,6 @@ void BotBrain::_ClearTask()
void BotBrain::_ResetState()
{
_States = 0;
_IsBotDead = false;
}

void BotBrain::_AddState(BotState state)
Expand Down
8 changes: 4 additions & 4 deletions Pongbot/Bot/Brain/BotBrain.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ enum BotState
// TODO: Signal stuff like needing ammo or so here
};

class Player;

class BotBrain : public IEventHooker
{
public:
BotBrain(Bot* bot) : _ABot(bot), _BotTask(nullptr), _IsBotDead(false), _ThinkTime(0.f),
_States(0) {} /* To invoke OnSpawn() */
BotBrain(Bot* bot) : _ABot(bot), _BotTask(nullptr), _ThinkTime(0.f), _States(0) {} /* To invoke OnSpawn() */

public:
void OnThink();
void OnSpawn();

virtual void OnSpawn(Player player);
virtual void OnObjectiveUpdate();

protected:
Expand All @@ -34,7 +35,6 @@ class BotBrain : public IEventHooker
BotTask* _BotTask;
float _ThinkTime;
unsigned int _States;
bool _IsBotDead;

void _DefaultThink();
void _ClearTask();
Expand Down
6 changes: 6 additions & 0 deletions Pongbot/TF2/Entity/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "Player.h"
#include "EntityDataProvider.h"

extern IVEngineServer* Engine;
extern IServerGameClients *IIServerGameClients;
extern IPlayerInfoManager *IIPlayerInfoManager;

Expand Down Expand Up @@ -91,4 +92,9 @@ void Player::SetAngle(QAngle angle)
// TODO
//_EntityDataProvider->SetDataOfEntity<QAngle>(*this, DATA_PLAYER_ANGLE, angle);
}
}

int Player::GetUserId() const
{
return Exists() ? Engine->GetPlayerUserId(GetEdict()) : -1;
}
1 change: 1 addition & 0 deletions Pongbot/TF2/Entity/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Player : public Entity
IPlayerInfo* GetPlayerInfo() const;
QAngle GetAngle() const;
void SetAngle(QAngle angle);
int GetUserId() const;

private:
IPlayerInfo* _IIPlayerInfo;
Expand Down
1 change: 1 addition & 0 deletions Pongbot/TF2/Entity/RoundTimer.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#include "Entity.h"

class RoundTimer : public Entity
Expand Down
14 changes: 14 additions & 0 deletions Pongbot/TF2/Events/EventHooksProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "../../Bot/BotManager.h"
#include "../../ConVarHolder.h"
#include "../../Util.h"
#include "../Entity/Player.h"

extern IVEngineServer* Engine;
extern IServerGameDLL* Server;
Expand All @@ -29,6 +30,7 @@ void EventHooksProvider::Init()

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

Expand Down Expand Up @@ -154,6 +156,18 @@ bool EventHooksProvider::_OnFireEvent(IGameEvent* pEvent, bool bDontBroadcast) c
eventHooker->OnRoundActive();
}
}
else if (strcmp(name, "player_spawn") == 0)
{
int userId = pEvent->GetInt("userid", -1);
Player player = Util::GetPlayerFromUserId(userId);
if (player.Exists())
{
for (IEventHooker* eventHooker : _EventHookers)
{
eventHooker->OnSpawn(player);
}
}
}

return true;
}
Expand Down
2 changes: 2 additions & 0 deletions Pongbot/TF2/Events/IEventHooker.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include "../Entity/Player.h"

class IEventHooker
{
Expand All @@ -12,4 +13,5 @@ class IEventHooker
char const* pOldLevel, char const* pLandmarkName, bool loadGame, bool background) {}
virtual void OnRoundStart() {}
virtual void OnRoundActive() {}
virtual void OnSpawn(Player player) {}
};
18 changes: 18 additions & 0 deletions Pongbot/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ namespace Util
IIEngineTrace->TraceRay(traceLine, fMask, traceFilter, traceResult);
}

Player GetPlayerFromUserId(int userId)
{
if (userId < 0)
{
return Player();
}

for (Player player : GetAllPlayers())
{
if (player.GetUserId() == userId)
{
return player;
}
}

return Player();
}

QAngle GetLookAtAngleForPos(Bot* bot, Vector lookAtPos)
{
Vector vectorAngle = lookAtPos - bot->GetEarPos();
Expand Down
1 change: 1 addition & 0 deletions Pongbot/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace Util
std::vector<Player> GetAllPlayers();
void TraceLine(Vector startPos, Vector targetPos, unsigned int fMask, ITraceFilter* traceFilter,
trace_t* traceResult);
Player GetPlayerFromUserId(int userId);

QAngle GetLookAtAngleForPos(Bot* bot, Vector lookAtPos);
Vector2D GetIdealMoveSpeedsToPos(Bot* bot, Vector targetPos);
Expand Down
3 changes: 2 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ COMPILE_FLAGS = -Wall -Wextra -fpermissive -w -DPOSIX -Dstricmp=strcasecmp -D_st
# -fno-rtti
INCLUDES = -I $(MLIBS) -I $(MLIBS)/hlsdk $(addprefix -I,$(shell find $(MLIBS)/hlsdk/public -type d -print)) -I $(MLIBS)/hlsdk/engine \
-I $(MLIBS)/hlsdk/mathlib -I $(MLIBS)/hlsdk/vstdlib -I $(MLIBS)/hlsdk/game -I $(MLIBS)/hlsdk/game/tier1 -I $(MLIBS)/hlsdk/game/tier0 \
-I $(MLIBS)/hlsdk/tier0 -I $(MLIBS)/hlsdk/tier1 -I $(MLIBS)/hlsdk/game/server -I $(MLIBS)/hlsdk/game/shared -I $(MLIBS)/metamod -I $(MLIBS)/metamod/sourcehook -I Pongbot
-I $(MLIBS)/hlsdk/tier0 -I $(MLIBS)/hlsdk/tier1 -I $(MLIBS)/hlsdk/game/server -I $(MLIBS)/hlsdk/game/shared -I $(MLIBS)/metamod \
-I $(MLIBS)/metamod/sourcehook -I Pongbot

# Space-separated pkg-config libraries used by this project
LIBS = -L$(MLIBS)/hlsdk/lib/linux -ltier0_srv -lvstdlib_srv -l:mathlib_i486.a -l:tier1_i486.a -l:tier2_i486.a -l:tier3_i486.a -ldl
Expand Down

0 comments on commit 93fcf3e

Please sign in to comment.