1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/*
===========================================================================
Copyright (C) 2024 the OpenMoHAA team
This file is part of OpenMoHAA source code.
OpenMoHAA source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// playerbot.h: Multiplayer bot system.
#pragma once
#include "player.h"
#include "navigate.h"
#include "actorpath.h"
#define MAX_BOT_FUNCTIONS 5
typedef struct nodeAttract_s {
float m_fRespawnTime;
AttractiveNodePtr m_pNode;
} nodeAttract_t;
class BotController;
class BotMovement
{
public:
BotMovement();
void SetControlledEntity(Player *newEntity);
void MoveThink(usercmd_t& botcmd);
void AvoidPath(
Vector vPos,
float fAvoidRadius,
Vector vPreferredDir = vec_zero,
float *vLeashHome = NULL,
float fLeashRadius = 0.0f
);
void MoveNear(Vector vNear, float fRadius, float *vLeashHome = NULL, float fLeashRadius = 0.0f);
void MoveTo(Vector vPos, float *vLeashHome = NULL, float fLeashRadius = 0.0f);
bool MoveToBestAttractivePoint(int iMinPriority = 0);
bool CanMoveTo(Vector vPos);
bool MoveDone();
bool IsMoving(void);
void ClearMove(void);
Vector GetCurrentGoal() const;
private:
void CheckAttractiveNodes();
void CheckEndPos(Entity *entity);
void CheckJump(usercmd_t& botcmd);
void NewMove();
private:
SafePtr<Player> controlledEntity;
AttractiveNodePtr m_pPrimaryAttract;
Container<nodeAttract_t *> m_attractList;
ActorPath m_Path;
Vector m_vCurrentOrigin;
Vector m_vTargetPos;
Vector m_vCurrentGoal;
Vector m_vLastValidDir;
Vector m_vLastValidGoal;
Vector m_vLastCheckPos[2];
float m_fAttractTime;
int m_iTempAwayTime;
int m_iNumBlocks;
int m_iCheckPathTime;
bool m_bPathing;
bool m_bTempAway;
};
class BotRotation
{
public:
BotRotation();
void SetControlledEntity(Player *newEntity);
void TurnThink(usercmd_t& botcmd, usereyes_t& eyeinfo);
const Vector& GetTargetAngles() const;
void SetTargetAngles(Vector vAngles);
void AimAt(Vector vPos);
private:
SafePtr<Player> controlledEntity;
Vector m_vTargetAng;
Vector m_vCurrentAng;
Vector m_vAngSpeed;
float m_fYawSpeedMult;
};
class BotState
{
public:
virtual bool CheckCondition() const = 0;
virtual void Begin() = 0;
virtual void End() = 0;
virtual void Think() = 0;
};
class BotController : public Listener
{
public:
struct botfunc_t {
bool (BotController::*CheckCondition)(void);
void (BotController::*BeginState)(void);
void (BotController::*EndState)(void);
void (BotController::*ThinkState)(void);
};
private:
static botfunc_t botfuncs[];
BotMovement movement;
BotRotation rotation;
// States
int m_iCuriousTime;
int m_iAttackTime;
int m_iConfirmTime;
int m_iAttackStopAimTime;
Vector m_vLastCuriousPos;
Vector m_vNewCuriousPos;
Vector m_vOldEnemyPos;
Vector m_vLastEnemyPos;
Vector m_vLastDeathPos;
SafePtr<Sentient> m_pEnemy;
int m_iEnemyEyesTag;
// Input
usercmd_t m_botCmd;
usereyes_t m_botEyes;
// States
int m_StateCount;
unsigned int m_StateFlags;
ScriptThreadLabel m_RunLabel;
// Taunts
int m_iNextTauntTime;
private:
Weapon* FindWeaponWithAmmo(void);
Weapon* FindMeleeWeapon(void);
void UseWeaponWithAmmo(void);
void CheckUse(void);
void CheckValidWeapon(void);
void State_DefaultBegin(void);
void State_DefaultEnd(void);
void State_Reset(void);
static void InitState_Idle(botfunc_t *func);
bool CheckCondition_Idle(void);
void State_BeginIdle(void);
void State_EndIdle(void);
void State_Idle(void);
static void InitState_Curious(botfunc_t *func);
bool CheckCondition_Curious(void);
void State_BeginCurious(void);
void State_EndCurious(void);
void State_Curious(void);
static void InitState_Attack(botfunc_t *func);
bool CheckCondition_Attack(void);
void State_BeginAttack(void);
void State_EndAttack(void);
void State_Attack(void);
bool IsValidEnemy(Sentient *sent) const;
static void InitState_Grenade(botfunc_t *func);
bool CheckCondition_Grenade(void);
void State_BeginGrenade(void);
void State_EndGrenade(void);
void State_Grenade(void);
static void InitState_Weapon(botfunc_t *func);
bool CheckCondition_Weapon(void);
void State_BeginWeapon(void);
void State_EndWeapon(void);
void State_Weapon(void);
void CheckStates(void);
public:
CLASS_PROTOTYPE(BotController);
BotController();
static void Init(void);
void GetEyeInfo(usereyes_t *eyeinfo);
void GetUsercmd(usercmd_t *ucmd);
void UpdateBotStates(void);
void CheckReload(void);
void AimAtAimNode(void);
void NoticeEvent(Vector vPos, int iType, Entity *pEnt, float fDistanceSquared, float fRadiusSquared);
void ClearEnemy(void);
void SendCommand(const char *text);
void Think();
void Spawned(void);
void Killed(Event *ev);
void GotKill(Event *ev);
void EventStuffText(Event *ev);
BotMovement& GetMovement();
public:
void setControlledEntity(Player *player);
Player *getControlledEntity() const;
private:
SafePtr<Player> controlledEnt;
};
class BotControllerManager : public Listener
{
public:
CLASS_PROTOTYPE(BotControllerManager);
public:
~BotControllerManager();
BotController *createController(Player *player);
void removeController(BotController *controller);
BotController *findController(Entity *ent);
const Container<BotController *>& getControllers() const;
void Init();
void Cleanup();
void ThinkControllers();
private:
Container<BotController *> controllers;
};
class BotManager : public Listener
{
public:
CLASS_PROTOTYPE(BotManager);
public:
BotControllerManager& getControllerManager();
void Init();
void Cleanup();
void Frame();
void BroadcastEvent(Entity *originator, Vector origin, int iType, float radius);
private:
BotControllerManager botControllerManager;
};
extern BotManager botManager;
class PlayerBot : public Player
{
public:
CLASS_PROTOTYPE(PlayerBot);
public:
PlayerBot();
void setController(BotController *controlledBy);
void Spawned(void) override;
void Killed(Event *ev) override;
void GotKill(Event *ev);
private:
BotController *controller;
};
|