AIGuard.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. ** Command & Conquer Generals(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. virtual StateReturnType onEnter( void );
  122. virtual StateReturnType update( void );
  123. virtual void onExit( StateExitType status );
  124. protected:
  125. // snapshot interface
  126. virtual void crc( Xfer *xfer );
  127. virtual void xfer( Xfer *xfer );
  128. virtual void loadPostProcess();
  129. private:
  130. AIGuardMachine* getGuardMachine() { return (AIGuardMachine*)getMachine(); }
  131. ExitConditions m_exitConditions;
  132. AIAttackState *m_attackState;
  133. };
  134. EMPTY_DTOR(AIGuardInnerState)
  135. //--------------------------------------------------------------------------------------
  136. class AIGuardIdleState : public State
  137. {
  138. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(AIGuardIdleState, "AIGuardIdleState")
  139. public:
  140. AIGuardIdleState( StateMachine *machine ) : State( machine, "AIGuardIdleState" ) { }
  141. virtual StateReturnType onEnter( void );
  142. virtual StateReturnType update( void );
  143. virtual void onExit( StateExitType status );
  144. protected:
  145. // snapshot interface
  146. virtual void crc( Xfer *xfer );
  147. virtual void xfer( Xfer *xfer );
  148. virtual void loadPostProcess();
  149. private:
  150. AIGuardMachine* getGuardMachine() { return (AIGuardMachine*)getMachine(); }
  151. UnsignedInt m_nextEnemyScanTime;
  152. Coord3D m_guardeePos; ///< Where the object we are guarding was last.
  153. };
  154. EMPTY_DTOR(AIGuardIdleState)
  155. //--------------------------------------------------------------------------------------
  156. class AIGuardOuterState : public State
  157. {
  158. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(AIGuardOuterState, "AIGuardOuterState")
  159. public:
  160. AIGuardOuterState( StateMachine *machine ) : State( machine, "AIGuardOuter" )
  161. {
  162. m_attackState = NULL;
  163. }
  164. virtual StateReturnType onEnter( void );
  165. virtual StateReturnType update( void );
  166. virtual void onExit( StateExitType status );
  167. protected:
  168. // snapshot interface
  169. virtual void crc( Xfer *xfer );
  170. virtual void xfer( Xfer *xfer );
  171. virtual void loadPostProcess();
  172. private:
  173. AIGuardMachine* getGuardMachine() { return (AIGuardMachine*)getMachine(); }
  174. ExitConditions m_exitConditions;
  175. AIAttackState *m_attackState;
  176. };
  177. EMPTY_DTOR(AIGuardOuterState)
  178. //--------------------------------------------------------------------------------------
  179. class AIGuardReturnState : public AIInternalMoveToState
  180. {
  181. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(AIGuardReturnState, "AIGuardReturnState")
  182. private:
  183. AIGuardMachine* getGuardMachine() { return (AIGuardMachine*)getMachine(); }
  184. public:
  185. AIGuardReturnState( StateMachine *machine ) : AIInternalMoveToState( machine, "AIGuardReturn" )
  186. {
  187. m_nextReturnScanTime = 0;
  188. }
  189. virtual StateReturnType onEnter( void );
  190. virtual StateReturnType update( void );
  191. virtual void onExit( StateExitType status );
  192. protected:
  193. // snapshot interface
  194. virtual void crc( Xfer *xfer );
  195. virtual void xfer( Xfer *xfer );
  196. virtual void loadPostProcess();
  197. private:
  198. UnsignedInt m_nextReturnScanTime;
  199. };
  200. EMPTY_DTOR(AIGuardReturnState)
  201. //--------------------------------------------------------------------------------------
  202. class AIGuardPickUpCrateState : public AIPickUpCrateState
  203. {
  204. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(AIGuardPickUpCrateState, "AIGuardPickUpCrateState")
  205. public:
  206. AIGuardPickUpCrateState( StateMachine *machine );
  207. virtual StateReturnType onEnter( void );
  208. virtual StateReturnType update( void );
  209. virtual void onExit( StateExitType status );
  210. };
  211. EMPTY_DTOR(AIGuardPickUpCrateState)
  212. //--------------------------------------------------------------------------------------
  213. class AIGuardAttackAggressorState : public State
  214. {
  215. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(AIGuardAttackAggressorState, "AIGuardAttackAggressorState")
  216. public:
  217. AIGuardAttackAggressorState( StateMachine *machine );
  218. virtual StateReturnType onEnter( void );
  219. virtual StateReturnType update( void );
  220. virtual void onExit( StateExitType status );
  221. protected:
  222. // snapshot interface
  223. virtual void crc( Xfer *xfer );
  224. virtual void xfer( Xfer *xfer );
  225. virtual void loadPostProcess();
  226. private:
  227. AIGuardMachine* getGuardMachine() { return (AIGuardMachine*)getMachine(); }
  228. ExitConditions m_exitConditions;
  229. AIAttackState *m_attackState;
  230. };
  231. EMPTY_DTOR(AIGuardAttackAggressorState)
  232. //--------------------------------------------------------------------------------------
  233. #endif /* _H_AIGUARD_ */