ScummVM API documentation
GameEnemy.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * Copyright (C) 2006-2010 - Frictional Games
24  *
25  * This file is part of Penumbra Overture.
26  */
27 
28 #ifndef GAME_GAME_ENEMY_H
29 #define GAME_GAME_ENEMY_H
30 
31 #include "hpl1/engine/engine.h"
32 #include "hpl1/penumbra-overture/GameEntity.h"
33 
34 #include "hpl1/penumbra-overture/CharacterMove.h"
35 
36 using namespace hpl;
37 
38 class cGameTrigger;
39 class cCharacterMove;
40 class iGameEntity;
41 
42 //-----------------------------------------
43 
44 #define STATE_IDLE 0
45 #define STATE_HUNT 1
46 #define STATE_ATTACK 2
47 #define STATE_FLEE 3
48 #define STATE_KNOCKDOWN 4
49 #define STATE_DEAD 5
50 #define STATE_PATROL 6
51 #define STATE_INVESTIGATE 7
52 #define STATE_BREAKDOOR 8
53 #define STATE_CALLBACKUP 9
54 #define STATE_MOVETO 10
55 #define STATE_EAT 11
56 #define STATE_ATTENTION 12
57 #define STATE_NUM 13
58 
59 //-----------------------------------------
60 
61 enum eEnemyMoveState {
62  eEnemyMoveState_Backward,
63  eEnemyMoveState_Stopped,
64  eEnemyMoveState_Walking,
65  eEnemyMoveState_Running,
66  eEnemyMoveState_LastEnum
67 };
68 
69 //-----------------------------------------
70 
72  kSerializableClassInit(cEnemyPatrolNode) public : cEnemyPatrolNode() {}
73  cEnemyPatrolNode(const tString &asNode, float afTime, const tString &asAnim) : msNodeName(asNode), mfWaitTime(afTime), msAnimation(asAnim) {}
74 
75  tString msNodeName;
76  float mfWaitTime;
77  tString msAnimation;
78 };
79 
80 //-----------------------------------------
81 
83 public:
84  void Reset();
85  bool OnIntersect(iPhysicsBody *pBody, cPhysicsRayParams *apParams);
86  bool Intersected();
87 
88 private:
89  bool mbIntersected;
90  iPhysicsBody *mpGrabBody;
91 };
92 
93 //------------------------------------------
94 
96 public:
97  bool GetGround(const cVector3f &avStartPos, const cVector3f &avDir,
98  cVector3f *apDestPosition, cVector3f *apDestNormal,
99  float afMaxDistance = 10);
100 
101  bool OnIntersect(iPhysicsBody *pBody, cPhysicsRayParams *apParams);
102 
103 private:
104  bool mbIntersected;
105  float mfMinDist;
106  cVector3f mvPos;
107  cVector3f mvNormal;
108  float mfMaxDistance;
109 };
110 
111 //------------------------------------------
112 
114 public:
115  bool CheckDoor(const cVector3f &avStart, const cVector3f &avEnd);
116 
117  bool BeforeIntersect(iPhysicsBody *pBody);
118  bool OnIntersect(iPhysicsBody *pBody, cPhysicsRayParams *apParams);
119 
120 private:
121  bool mbIntersected;
122 };
123 
124 //------------------------------------------
125 
127  kSerializableClassInit(iGameEnemy_SaveData);
128 
129 public:
130  bool mbHasBeenActivated;
131 
132  cVector3f mvCharBodyPosition;
133  cVector3f mvCharBodyRotation;
134  int mlCurrentPatrolNode;
135 
136  float mfDisappearTime;
137  bool mbDisappearActive;
138  bool mbHasDisappeared;
139 
140  bool mbUsesTriggers;
141 
142  cVector3f mvLastPlayerPos;
143  tString msOnDeathCallback;
144  tString msOnAttackCallback;
145  cContainerVec<cEnemyPatrolNode> mvPatrolNodes;
146 
147  iGameEntity *CreateEntity();
148 };
149 
150 //-----------------------------------------
151 
153 public:
154  iGameEnemyState(int alId, cInit *apInit, iGameEnemy *apEnemy);
155  virtual ~iGameEnemyState() = default;
156 
157  virtual void OnEnterState(iGameEnemyState *apPrevState) = 0;
158  virtual void OnLeaveState(iGameEnemyState *apNextState) = 0;
159 
160  virtual void OnUpdate(float afTimeStep) = 0;
161 
162  virtual void OnDraw() = 0;
163  virtual void OnPostSceneDraw() = 0;
164 
165  virtual void OnSeePlayer(const cVector3f &avPosition, float afChance) = 0;
166  virtual bool OnHearNoise(const cVector3f &avPosition, float afVolume) = 0;
167  virtual void OnTakeHit(float afDamage) = 0;
168  virtual void OnDeath(float afDamage) = 0;
169  virtual void OnAnimationOver(const tString &asAnimName) = 0;
170  virtual void OnFlashlight(const cVector3f &avPosition) = 0;
171 
172  void SetPreviousState(int alX) { mlPreviousState = alX; }
173  int GetPreviousState() { return mlPreviousState; }
174 
175  int GetId() { return mlId; }
176 
177 protected:
178  int mlId;
179  cInit *mpInit;
180  cPlayer *mpPlayer;
181  iGameEnemy *mpEnemy;
182  cCharacterMove *mpMover;
183 
184  int mlPreviousState;
185 };
186 
187 //-----------------------------------------
188 
189 class iGameEnemy : public iGameEntity {
190  typedef iGameEntity super;
191  friend class cEntityLoader_GameEnemy;
192 
193 public:
194  iGameEnemy(cInit *apInit, const tString &asName, TiXmlElement *apGameElem);
195  virtual ~iGameEnemy(void);
196 
197  // Special
198  void LoadBaseProperties(TiXmlElement *apGameElem);
199 
200  // Actions
201  void OnPlayerInteract();
202  void OnPlayerPick();
203 
204  void Setup(cWorld3D *apWorld);
205 
206  void OnWorldLoad();
207  void OnPostLoadScripts();
208  void OnWorldExit();
209  void OnDraw();
210  void OnPostSceneDraw();
211  virtual void ExtraPostSceneDraw() {}
212 
213  void Update(float afTimeStep);
214 
215  bool IsLoading() { return mbLoading; }
216 
217  void OnDamage(float afX);
218  void OnDeath(float afX);
219 
220  void OnFlashlight(const cVector3f &avPos);
221 
222  virtual void OnSetActive(bool abX);
223 
224  virtual bool MoveToPos(const cVector3f &avFeetPos) = 0;
225 
226  virtual void ShowPlayer(const cVector3f &avPlayerFeetPos) {}
227 
228  virtual bool IsFighting() { return false; }
229 
230  // Physics
231  void SetupBody();
232 
233  // Properties
234  cVector3f GetPosition();
235  void SetFOV(float afAngle) { mfFOV = afAngle; }
236 
237  const tString &GetHitPS() { return msHitPS; }
238  const tString &GetEnemyType() { return msEnemyType; }
239 
240  const tString &GetCloseMusic() { return msCloseMusic; }
241  int GetCloseMusicPrio() { return mlCloseMusicPrio; }
242  float GetCloseMusicStartDist() { return mfCloseMusicStartDist; }
243  float GetCloseMusicStopDist() { return mfCloseMusicStopDist; }
244 
245  const tString &GetAttackMusic() { return msAttackMusic; }
246  int GetAttackMusicPrio() { return mlAttackMusicPrio; }
247 
248  // States
249  void AddState(iGameEnemyState *apState);
250  void ChangeState(int alId);
251  iGameEnemyState *GetState(int alId);
252  int GetCurrentStateId() { return mlCurrentState; }
253 
254  // Triggers
255  bool HandleTrigger(cGameTrigger *apTrigger);
256 
257  eGameTriggerType GetTriggerTypes() { return mTriggerTypes; }
258 
259  float GetTriggerUpdateCount() { return mfTriggerUpdateCount; }
260  void SetTriggerUpdateCount(float afX) { mfTriggerUpdateCount = afX; }
261  float GetTriggerUpdateRate() { return mfTriggerUpdateRate; }
262 
263  void SetSkipSoundTriggerCount(float afX) { mfSkipSoundTriggerCount = afX; }
264 
265  // Content control
266  void PlayAnim(const tString &asName, bool abLoop, float afFadeTime,
267  bool abDependsOnSpeed = false, float afSpeedMul = 1.0f,
268  bool abSyncWithPrevFrame = false,
269  bool abOverideMoveState = true);
270  void UseMoveStateAnimations();
271 
272  cAnimationState *GetCurrentAnimation() { return mpCurrentAnimation; }
273 
274  void PlaySound(const tString &asName);
275 
276  // Callabcks
277  void SetOnDeathCallback(const tString &asFunc) { msOnDeathCallback = asFunc; }
278  void SetOnAttackCallback(const tString &asFunc) { msOnAttackCallback = asFunc; }
279  tString GetOnAttackCallback() { return msOnAttackCallback; }
280 
281  // AI
282  bool CanSeePlayer();
283  cCharacterMove *GetMover() { return mpMover; }
284  cVector3f GetLastPlayerPos() { return mvLastPlayerPos; }
285  void SetLastPlayerPos(const cVector3f &avPos) { mvLastPlayerPos = avPos; }
286 
287  void SetTempPosition(const cVector3f &avPos) { mvTempPosition = avPos; }
288  cVector3f GetTempPosition() { return mvTempPosition; }
289 
290  void SetTempFloat(float afX) { mfTempFloat = afX; }
291  float GetTempFloat() { return mfTempFloat; }
292 
293  cEnemyFindGround *GetGroundFinder() { return &mFindGround; }
294 
295  cEnemyCheckForDoor *GetDoorChecker() { return &mDoorCheck; }
296  bool CheckForDoor();
297 
298  bool CheckForTeamMate(float afMaxDist, bool abCheckIfFighting);
299 
300  bool IsAttracted() { return mbIsAttracted; }
301  void SetAttracted(bool abX) { mbIsAttracted = abX; }
302 
303  bool GetUsesTriggers() { return mbUsesTriggers; }
304  void SetUsesTriggers(bool abX) { mbUsesTriggers = abX; }
305 
306  // Patrolling
307  void AddPatrolNode(const tString &asNode, float afTime, const tString &asAnimation);
308  cEnemyPatrolNode *GetPatrolNode(int alIdx) { return &mvPatrolNodes[alIdx]; }
309  int GetPatrolNodeNum() { return (int)mvPatrolNodes.size(); }
310  cEnemyPatrolNode *CurrentPatrolNode() { return &mvPatrolNodes[mlCurrentPatrolNode]; }
311 
312  void ClearPatrolNodes();
313 
314  int GetCurrentPatrolNode() { return mlCurrentPatrolNode; }
315  float GetWaitTime() { return mfWaitTime; }
316  float GetWaitTimeCount() { return mfWaitTimeCount; }
317 
318  void SetCurrentPatrolNode(int alX) { mlCurrentPatrolNode = alX; }
319  void IncCurrentPatrolNode() {
320  mlCurrentPatrolNode++;
321  if (mlCurrentPatrolNode >= (int)mvPatrolNodes.size())
322  mlCurrentPatrolNode = 0;
323  }
324  void SetWaitTime(float afX) { mfWaitTime = afX; }
325  void SetWaitTimeCount(float afX) { mfWaitTimeCount = afX; }
326  void AddWaitTimeCount(float afX) { mfWaitTimeCount += afX; }
327 
328  float GetDoorBreakCount() { return mfDoorBreakCount; }
329  void SetDoorBreakCount(float afX) { mfDoorBreakCount = afX; }
330  void AddDoorBreakCount(float afX) { mfDoorBreakCount += afX; }
331 
332  const cVector3f &GetModelOffsetAngles() { return mvModelOffsetAngles; }
333 
334  // Virtual
335  virtual void OnUpdate(float afTimeStep) = 0;
336  virtual void OnLoad() = 0;
337 
338  // SaveObject implementation
339  iGameEntity_SaveData *CreateSaveData();
340  void SaveToSaveData(iGameEntity_SaveData *apSaveData);
341  void LoadFromSaveData(iGameEntity_SaveData *apSaveData);
342 
343 protected:
344  void UpdateEnemyPose(float afTimeStep);
345 
346  void UpdateAnimations(float afTimeStep);
347  void UpdateCheckForPlayer(float afTimeStep);
348 
349  bool HandleSoundTrigger(cGameTrigger *apTrigger);
350 
351  bool LineOfSight(const cVector3f &avPos, const cVector3f &avSize);
352 
353  bool mbSetFeetAtGroundOnStart;
354  bool mbAttachMeshToBody;
355  bool mbRemoveAttackerOnDisable;
356 
357  tVector3fVec mvRayStartPoses;
358  tVector3fVec mvRayEndPoses;
359 
360  cEnemyFindGround mFindGround;
361 
362  eGameTriggerType mTriggerTypes;
363 
364  cCharacterMove *mpMover;
365 
366  tString msEnemyType;
367 
368  float mfTriggerUpdateCount;
369  float mfTriggerUpdateRate;
370 
371  float mfSkipSoundTriggerCount;
372 
373  cAStarHandler *mpAStarAir;
374  cAStarHandler *mpAStarGround;
375  cAINodeContainer *mpNodeContainerAir;
376  cAINodeContainer *mpNodeContainerGround;
377 
378  cLineOfSightRayCallback mRayCallback;
379 
381  int mlCurrentState;
382 
383  cEnemyCheckForDoor mDoorCheck;
384 
385  cAnimationState *mpCurrentAnimation;
386  bool mbAnimationIsSpeedDependant;
387  float mfAnimationSpeedMul;
388 
389  bool mbOverideMoveState;
390  eEnemyMoveState mMoveState;
391 
392  cVector3f mvTempPosition;
393  float mfTempFloat;
394 
395  tStringVec mvPreloadSounds;
396 
397  bool mbLoading;
398 
399  bool mbAlignToGroundNormal;
400  cMatrixf m_mtxStartPose;
401  cMatrixf m_mtxGoalPose;
402  float mfPoseCount;
403 
404  bool mbHasBeenActivated;
405 
406  bool mbUsesTriggers;
407 
409  // Variable Properties
410  cVector3f mvLastPlayerPos;
411  float mfCanSeePlayerCount;
412  float mfCheckForPlayerCount;
413  float mfCheckForPlayerRate;
414  bool mbCanSeePlayer;
415  int mlPlayerInLOSCount;
416  int mlMaxPlayerInLOSCount;
417 
418  float mfCalcPlayerHiddenPosCount;
419 
420  float mfDamageSoundTimer;
421 
422  tString msOnDeathCallback;
423  tString msOnAttackCallback;
424 
425  Common::Array<cEnemyPatrolNode> mvPatrolNodes;
426 
427  int mlCurrentPatrolNode;
428  float mfWaitTime;
429  float mfWaitTimeCount;
430 
431  float mfDoorBreakCount;
432 
433  bool mbIsAttracted;
434 
436  // Properties to be implemented
437  bool mbDisappear;
438  float mfDisappearMinTime;
439  float mfDisappearMaxTime;
440  float mfDisappearMinDistance;
441  tString msDisappearPS;
442  tString msDisappearSound;
443  bool mbDisappearFreezesRagdoll;
444 
445  float mfDisappearTime;
446  bool mbDisappearActive;
447  bool mbHasDisappeared;
448 
449  tString msCloseMusic;
450  int mlCloseMusicPrio;
451  float mfCloseMusicStartDist;
452  float mfCloseMusicStopDist;
453 
454  tString msAttackMusic;
455  int mlAttackMusicPrio;
456 
457  bool mbShowDebug;
458 
459  tString msGroundNodeType;
460 
461  cVector3f mvBodySize;
462  float mfBodyMass;
463 
464  float mfMaxForwardSpeed;
465  float mfMaxBackwardSpeed;
466 
467  float mfAcceleration;
468  float mfDeacceleration;
469 
470  float mfMaxTurnSpeed;
471  float mfAngleDistTurnMul;
472 
473  float mfMinBreakAngle;
474  float mfBreakAngleMul;
475 
476  float mfSpeedMoveAnimMul;
477  float mfTurnSpeedMoveAnimMul;
478 
479  cMatrixf m_mtxModelOffset;
480  cVector3f mvModelOffsetAngles;
481 
482  float mfIdleToMoveLimit;
483  float mfMoveToLideLimit;
484 
485  float mfMaxPushMass;
486  float mfPushForce;
487 
488  float mfMaxSeeDist;
489 
490  float mfMinAttackDist;
491 
492  float mfFOV;
493  float mfFOVXMul;
494 
495  float mfMaxHealth;
496 
497  float mfStoppedToWalkSpeed;
498  float mfWalkToStoppedSpeed;
499  float mfWalkToRunSpeed;
500  float mfRunToWalkSpeed;
501  float mfMoveAnimSpeedMul;
502  tString msBackwardAnim;
503  tString msStoppedAnim;
504  tString msWalkAnim;
505  tString msRunAnim;
506 
507  float mfMinStunSpeed;
508  float mfMinStunImpulse;
509  float mfObjectCollideStun;
510  float mfStunDamageMul;
511 
512  tString msHitPS;
513 };
514 
515 //--------------------------------------
516 
518 
519 public:
520  cEntityLoader_GameEnemy(const tString &asName, cInit *apInit);
522 
523 private:
524  void BeforeLoad(TiXmlElement *apRootElem, const cMatrixf &a_mtxTransform, cWorld3D *apWorld);
525  void AfterLoad(TiXmlElement *apRootElem, const cMatrixf &a_mtxTransform, cWorld3D *apWorld);
526 
527  cInit *mpInit;
528 };
529 
530 //--------------------------------------
531 
532 #endif // GAME_GAME_ENEMY_H
Definition: AI.h:36
Definition: str.h:59
Definition: PhysicsWorld.h:92
Definition: GameEnemy.h:95
Definition: AStar.h:84
Definition: GameEnemy.h:126
Definition: GameEnemy.h:113
Definition: TriggerHandler.h:42
Definition: PhysicsWorld.h:100
Definition: AnimationState.h:80
Definition: Player.h:145
Definition: SerializeClass.h:230
Definition: GameEntity.h:76
Definition: PhysicsBody.h:117
Definition: World3D.h:179
Definition: AINodeContainer.h:158
Definition: EntityLoader_Object.h:52
Definition: tinyxml.h:864
Definition: GameEnemy.h:152
Definition: CharacterMove.h:69
Definition: GameEnemy.h:189
Definition: GameEntity.h:126
Definition: GameEnemy.h:71
Definition: Init.h:70
Definition: GameEnemy.h:517
Definition: GameEnemy.h:82