AIGuard.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: AIGuard.h
  24. /*---------------------------------------------------------------------------*/
  25. /* EA Pacific */
  26. /* Confidential Information */
  27. /* Copyright (C) 2001 - All Rights Reserved */
  28. /* DO NOT DISTRIBUTE */
  29. /*---------------------------------------------------------------------------*/
  30. /* Project: RTS3 */
  31. /* File name: AIGuard.h */
  32. /* Created: John K. McDonald, Jr., 3/29/2002 */
  33. /* Desc: // Define Guard states for AI */
  34. /* Revision History: */
  35. /* 3/29/2002 : Initial creation */
  36. /*---------------------------------------------------------------------------*/
  37. #pragma once
  38. #ifndef _H_AIGUARD_
  39. #define _H_AIGUARD_
  40. // INCLUDES ///////////////////////////////////////////////////////////////////
  41. #include "Common/GameMemory.h"
  42. #include "GameLogic/AIStateMachine.h"
  43. #include "GameLogic/GameLogic.h"
  44. #include "GameLogic/Object.h"
  45. #include "GameLogic/AI.h"
  46. // DEFINES ////////////////////////////////////////////////////////////////////
  47. // TYPE DEFINES ///////////////////////////////////////////////////////////////
  48. enum
  49. {
  50. // prevent collisions with other states that we might use, (namely AI_IDLE)
  51. AI_GUARD_INNER = 5000, ///< Attack anything within this area till death
  52. AI_GUARD_IDLE, ///< Wait till something shows up to attack.
  53. AI_GUARD_OUTER, ///< Attack anything within this area that has been aggressive, until the timer expires
  54. AI_GUARD_RETURN, ///< Restore to a position within the inner circle
  55. AI_GUARD_GET_CRATE, ///< Pick up a crate from an enemy we killed.
  56. AI_GUARD_ATTACK_AGGRESSOR, ///< Attack something that attacked me (that I can attack)
  57. };
  58. //--------------------------------------------------------------------------------------
  59. class ExitConditions : public AttackExitConditionsInterface
  60. {
  61. public:
  62. enum ExitConditionsEnum
  63. {
  64. ATTACK_ExitIfOutsideRadius = 0x01,
  65. ATTACK_ExitIfExpiredDuration = 0x02,
  66. ATTACK_ExitIfNoUnitFound = 0x04
  67. };
  68. Int m_conditionsToConsider;
  69. Coord3D m_center; // can be updated at any time by owner
  70. Real m_radiusSqr; // can be updated at any time by owner
  71. UnsignedInt m_attackGiveUpFrame; // frame at which we give up (if using)
  72. ExitConditions() : m_attackGiveUpFrame(0), m_conditionsToConsider(0), m_radiusSqr(0.0f)
  73. {
  74. //Added By Sadullah Nader
  75. // Initializations missing and needed
  76. m_center.zero();
  77. }
  78. virtual Bool shouldExit(const StateMachine* machine) const;
  79. };
  80. // FORWARD DECLARATIONS ///////////////////////////////////////////////////////
  81. //--------------------------------------------------------------------------------------
  82. class AIGuardMachine : public StateMachine
  83. {
  84. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE( AIGuardMachine, "AIGuardMachinePool" );
  85. private:
  86. ObjectID m_targetToGuard;
  87. const PolygonTrigger * m_areaToGuard;
  88. Coord3D m_positionToGuard;
  89. ObjectID m_nemesisToAttack;
  90. GuardMode m_guardMode;
  91. protected:
  92. // snapshot interface
  93. virtual void crc( Xfer *xfer );
  94. virtual void xfer( Xfer *xfer );
  95. virtual void loadPostProcess();
  96. public:
  97. /**
  98. * The implementation of this constructor defines the states
  99. * used by this machine.
  100. */
  101. AIGuardMachine( Object *owner );
  102. Object* findTargetToGuardByID( void ) { return TheGameLogic->findObjectByID(m_targetToGuard); }
  103. void setTargetToGuard( const Object *object ) { m_targetToGuard = object ? object->getID() : INVALID_ID; }
  104. const Coord3D *getPositionToGuard( void ) const { return &m_positionToGuard; }
  105. void setTargetPositionToGuard( const Coord3D *pos) { m_positionToGuard = *pos; }
  106. const PolygonTrigger *getAreaToGuard( void ) const { return m_areaToGuard; }
  107. void setAreaToGuard( const PolygonTrigger *area) { m_areaToGuard = area; }
  108. void setNemesisID(ObjectID id) { m_nemesisToAttack = id; }
  109. ObjectID getNemesisID() const { return m_nemesisToAttack; }
  110. GuardMode getGuardMode() const { return m_guardMode; }
  111. void setGuardMode(GuardMode guardMode) { m_guardMode = guardMode; }
  112. Bool lookForInnerTarget(void);
  113. static Real getStdGuardRange(const Object* obj);
  114. };
  115. //--------------------------------------------------------------------------------------
  116. class AIGuardInnerState : public State
  117. {
  118. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(AIGuardInnerState, "AIGuardInnerState")
  119. public:
  120. AIGuardInnerState( StateMachine *machine ) : State( machine, "AIGuardInner" )
  121. {
  122. m_attackState = 0;
  123. m_enterState = 0;
  124. }
  125. virtual Bool isAttack() const { return m_attackState ? m_attackState->isAttack() : FALSE; }
  126. virtual StateReturnType onEnter( void );
  127. virtual StateReturnType update( void );
  128. virtual void onExit( StateExitType status );
  129. protected:
  130. // snapshot interface
  131. virtual void crc( Xfer *xfer );
  132. virtual void xfer( Xfer *xfer );
  133. virtual void loadPostProcess();
  134. private:
  135. AIGuardMachine* getGuardMachine() { return (AIGuardMachine*)getMachine(); }
  136. ExitConditions m_exitConditions;
  137. AIAttackState *m_attackState;
  138. AIEnterState *m_enterState;
  139. };
  140. EMPTY_DTOR(AIGuardInnerState)
  141. //--------------------------------------------------------------------------------------
  142. class AIGuardIdleState : public State
  143. {
  144. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(AIGuardIdleState, "AIGuardIdleState")
  145. public:
  146. AIGuardIdleState( StateMachine *machine ) : State( machine, "AIGuardIdleState" ) { }
  147. virtual Bool isAttack() const { return FALSE; }
  148. virtual Bool isGuardIdle() const { return TRUE; }
  149. virtual StateReturnType onEnter( void );
  150. virtual StateReturnType update( void );
  151. virtual void onExit( StateExitType status );
  152. protected:
  153. // snapshot interface
  154. virtual void crc( Xfer *xfer );
  155. virtual void xfer( Xfer *xfer );
  156. virtual void loadPostProcess();
  157. private:
  158. AIGuardMachine* getGuardMachine() { return (AIGuardMachine*)getMachine(); }
  159. UnsignedInt m_nextEnemyScanTime;
  160. Coord3D m_guardeePos; ///< Where the object we are guarding was last.
  161. };
  162. EMPTY_DTOR(AIGuardIdleState)
  163. //--------------------------------------------------------------------------------------
  164. class AIGuardOuterState : public State
  165. {
  166. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(AIGuardOuterState, "AIGuardOuterState")
  167. public:
  168. AIGuardOuterState( StateMachine *machine ) : State( machine, "AIGuardOuter" )
  169. {
  170. m_attackState = NULL;
  171. }
  172. virtual Bool isAttack() const { return m_attackState ? m_attackState->isAttack() : FALSE; }
  173. virtual StateReturnType onEnter( void );
  174. virtual StateReturnType update( void );
  175. virtual void onExit( StateExitType status );
  176. protected:
  177. // snapshot interface
  178. virtual void crc( Xfer *xfer );
  179. virtual void xfer( Xfer *xfer );
  180. virtual void loadPostProcess();
  181. private:
  182. AIGuardMachine* getGuardMachine() { return (AIGuardMachine*)getMachine(); }
  183. ExitConditions m_exitConditions;
  184. AIAttackState *m_attackState;
  185. };
  186. EMPTY_DTOR(AIGuardOuterState)
  187. //--------------------------------------------------------------------------------------
  188. class AIGuardReturnState : public AIInternalMoveToState
  189. {
  190. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(AIGuardReturnState, "AIGuardReturnState")
  191. private:
  192. AIGuardMachine* getGuardMachine() { return (AIGuardMachine*)getMachine(); }
  193. public:
  194. AIGuardReturnState( StateMachine *machine ) : AIInternalMoveToState( machine, "AIGuardReturn" )
  195. {
  196. m_nextReturnScanTime = 0;
  197. }
  198. virtual Bool isAttack() const { return FALSE; }
  199. virtual StateReturnType onEnter( void );
  200. virtual StateReturnType update( void );
  201. virtual void onExit( StateExitType status );
  202. protected:
  203. // snapshot interface
  204. virtual void crc( Xfer *xfer );
  205. virtual void xfer( Xfer *xfer );
  206. virtual void loadPostProcess();
  207. private:
  208. UnsignedInt m_nextReturnScanTime;
  209. };
  210. EMPTY_DTOR(AIGuardReturnState)
  211. //--------------------------------------------------------------------------------------
  212. class AIGuardPickUpCrateState : public AIPickUpCrateState
  213. {
  214. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(AIGuardPickUpCrateState, "AIGuardPickUpCrateState")
  215. public:
  216. AIGuardPickUpCrateState( StateMachine *machine );
  217. virtual Bool isAttack() const { return FALSE; }
  218. virtual StateReturnType onEnter( void );
  219. virtual StateReturnType update( void );
  220. virtual void onExit( StateExitType status );
  221. };
  222. EMPTY_DTOR(AIGuardPickUpCrateState)
  223. //--------------------------------------------------------------------------------------
  224. class AIGuardAttackAggressorState : public State
  225. {
  226. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(AIGuardAttackAggressorState, "AIGuardAttackAggressorState")
  227. public:
  228. AIGuardAttackAggressorState( StateMachine *machine );
  229. virtual Bool isAttack() const { return m_attackState ? m_attackState->isAttack() : FALSE; }
  230. virtual StateReturnType onEnter( void );
  231. virtual StateReturnType update( void );
  232. virtual void onExit( StateExitType status );
  233. protected:
  234. // snapshot interface
  235. virtual void crc( Xfer *xfer );
  236. virtual void xfer( Xfer *xfer );
  237. virtual void loadPostProcess();
  238. private:
  239. AIGuardMachine* getGuardMachine() { return (AIGuardMachine*)getMachine(); }
  240. ExitConditions m_exitConditions;
  241. AIAttackState *m_attackState;
  242. };
  243. EMPTY_DTOR(AIGuardAttackAggressorState)
  244. //--------------------------------------------------------------------------------------
  245. #endif /* _H_AIGUARD_ */