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