OpenMoHAA 0.82.0
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 void CheckAttractiveNodes();
70 void CheckEndPos(Entity *entity);
71 void CheckJump(usercmd_t& botcmd);
72 void CheckJumpOverEdge(usercmd_t& botcmd);
73 void NewMove();
74 Vector FixDeltaFromCollision(const Vector& delta);
75
76private:
77 SafePtr<Player> controlledEntity;
78 AttractiveNodePtr m_pPrimaryAttract;
79 Container<nodeAttract_t *> m_attractList;
80 IPather *m_pPath;
81 int m_iLastMoveTime;
82
83 Vector m_vCurrentOrigin;
84 Vector m_vTargetPos;
85 Vector m_vCurrentGoal;
86 Vector m_vLastValidDir;
87 Vector m_vLastValidGoal;
88 Vector m_vLastCheckPos[2];
89 float m_fAttractTime;
90 int m_iTempAwayTime;
91 int m_iNumBlocks;
92 int m_iCheckPathTime;
93 int m_iLastBlockTime;
94 int m_iTempAwayState;
95 bool m_bPathing;
96
100
101 bool m_bAvoidCollision;
102 int m_iCollisionCheckTime;
103 Vector m_vTempCollisionAvoidance;
104
108
109 bool m_bJump;
110 int m_iJumpCheckTime;
111 Vector m_vJumpLocation;
112};
113
114class BotRotation
115{
116public:
117 BotRotation();
118
119 void SetControlledEntity(Player *newEntity);
120
121 void TurnThink(usercmd_t& botcmd, usereyes_t& eyeinfo);
122 const Vector& GetTargetAngles() const;
123 void SetTargetAngles(Vector vAngles);
124 void AimAt(Vector vPos);
125
126private:
127 SafePtr<Player> controlledEntity;
128
129 Vector m_vTargetAng;
130 Vector m_vCurrentAng;
131 Vector m_vAngSpeed;
132 float m_fYawSpeedMult;
133};
134
136{
137public:
138 virtual bool CheckCondition() const = 0;
139 virtual void Begin() = 0;
140 virtual void End() = 0;
141 virtual void Think() = 0;
142};
143
144class BotController : public Listener
145{
146public:
147 struct botfunc_t {
148 bool (BotController::*CheckCondition)(void);
149 void (BotController::*BeginState)(void);
150 void (BotController::*EndState)(void);
151 void (BotController::*ThinkState)(void);
152 };
153
154private:
155 static botfunc_t botfuncs[];
156
157 BotMovement movement;
158 BotRotation rotation;
159
160 // States
161 int m_iCuriousTime;
162 int m_iAttackTime;
163 int m_iConfirmTime;
164 int m_iAttackStopAimTime;
165 Vector m_vLastCuriousPos;
166 Vector m_vNewCuriousPos;
167 Vector m_vOldEnemyPos;
168 Vector m_vLastEnemyPos;
169 Vector m_vLastDeathPos;
170 SafePtr<Sentient> m_pEnemy;
171 int m_iEnemyEyesTag;
172
173 // Input
174 usercmd_t m_botCmd;
175 usereyes_t m_botEyes;
176
177 // States
178 int m_StateCount;
179 unsigned int m_StateFlags;
180 ScriptThreadLabel m_RunLabel;
181
182 // Taunts
183 int m_iNextTauntTime;
184
185private:
186 DelegateHandle delegateHandle_gotKill;
187 DelegateHandle delegateHandle_killed;
188 DelegateHandle delegateHandle_stufftext;
189
190private:
191 Weapon *FindWeaponWithAmmo(void);
192 Weapon *FindMeleeWeapon(void);
193 void UseWeaponWithAmmo(void);
194
195 void CheckUse(void);
196 void CheckValidWeapon(void);
197
198 void State_DefaultBegin(void);
199 void State_DefaultEnd(void);
200 void State_Reset(void);
201
202 static void InitState_Idle(botfunc_t *func);
203 bool CheckCondition_Idle(void);
204 void State_BeginIdle(void);
205 void State_EndIdle(void);
206 void State_Idle(void);
207
208 static void InitState_Curious(botfunc_t *func);
209 bool CheckCondition_Curious(void);
210 void State_BeginCurious(void);
211 void State_EndCurious(void);
212 void State_Curious(void);
213
214 static void InitState_Attack(botfunc_t *func);
215 bool CheckCondition_Attack(void);
216 void State_BeginAttack(void);
217 void State_EndAttack(void);
218 void State_Attack(void);
219 bool IsValidEnemy(Sentient *sent) const;
220
221 static void InitState_Grenade(botfunc_t *func);
222 bool CheckCondition_Grenade(void);
223 void State_BeginGrenade(void);
224 void State_EndGrenade(void);
225 void State_Grenade(void);
226
227 static void InitState_Weapon(botfunc_t *func);
228 bool CheckCondition_Weapon(void);
229 void State_BeginWeapon(void);
230 void State_EndWeapon(void);
231 void State_Weapon(void);
232
233 void CheckStates(void);
234
235public:
236 CLASS_PROTOTYPE(BotController);
237
238 BotController();
239 ~BotController();
240
241 static void Init(void);
242
243 void GetEyeInfo(usereyes_t *eyeinfo);
244 void GetUsercmd(usercmd_t *ucmd);
245
246 void UpdateBotStates(void);
247 void CheckReload(void);
248
249 void AimAtAimNode(void);
250
251 void NoticeEvent(Vector vPos, int iType, Entity *pEnt, float fDistanceSquared, float fRadiusSquared);
252 void ClearEnemy(void);
253
254 void SendCommand(const char *text);
255
256 void Think();
257
258 void Spawned(void);
259
260 void Killed(const Event& ev);
261 void GotKill(const Event& ev);
262 void EventStuffText(const str& text);
263
264 BotMovement& GetMovement();
265
266public:
267 void setControlledEntity(Player *player);
268 Player *getControlledEntity() const;
269
270private:
271 SafePtr<Player> controlledEnt;
272};
273
274class BotControllerManager : public Listener
275{
276public:
277 CLASS_PROTOTYPE(BotControllerManager);
278
279public:
281
282 BotController *createController(Player *player);
283 void removeController(BotController *controller);
284 BotController *findController(Entity *ent);
285 const Container<BotController *>& getControllers() const;
286
287 void Init();
288 void Cleanup();
289 void ThinkControllers();
290
291private:
292 Container<BotController *> controllers;
293};
294
295class BotManager : public Listener
296{
297public:
298 CLASS_PROTOTYPE(BotManager);
299
300public:
301 BotControllerManager& getControllerManager();
302
303 void Init();
304 void Cleanup();
305 void Frame();
306 void BroadcastEvent(Entity *originator, Vector origin, int iType, float radius);
307
308private:
309 BotControllerManager botControllerManager;
310};
311
312extern BotManager botManager;
Definition playerbot.h:275
Definition playerbot.h:145
Definition playerbot.h:296
Definition playerbot.h:40
Definition playerbot.h:115
Definition playerbot.h:136
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:147
Definition delegate.h:31
Definition playerbot.h:32