GarrisonContain.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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: GarrisonContain.h ////////////////////////////////////////////////////////////////////////
  24. // Author: Colin Day, February 2002
  25. // Desc: Contain module for structures that can be garrisoned
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #pragma once
  28. #ifndef __GARRISONCONTAIN_H_
  29. #define __GARRISONCONTAIN_H_
  30. // USER INCLUDES //////////////////////////////////////////////////////////////////////////////////
  31. #include "GameLogic/Module/OpenContain.h"
  32. #include "Common/ModelState.h"
  33. //-------------------------------------------------------------------------------------------------
  34. // ------------------------------------------------------------------------------------------------
  35. class GarrisonContainModuleData : public OpenContainModuleData
  36. {
  37. public:
  38. struct InitialRoster
  39. {
  40. AsciiString templateName;
  41. Int count;
  42. };
  43. Bool m_doIHealObjects;
  44. Real m_framesForFullHeal;
  45. Bool m_mobileGarrison;
  46. Bool m_immuneToClearBuildingAttacks;
  47. Bool m_isEnclosingContainer;
  48. InitialRoster m_initialRoster;
  49. GarrisonContainModuleData( void );
  50. static void buildFieldParse(MultiIniFieldParse& p)
  51. {
  52. OpenContainModuleData::buildFieldParse(p);
  53. static const FieldParse dataFieldParse[] =
  54. {
  55. { "MobileGarrison", INI::parseBool, NULL, offsetof( GarrisonContainModuleData, m_mobileGarrison ) },
  56. { "HealObjects", INI::parseBool, NULL, offsetof( GarrisonContainModuleData, m_doIHealObjects ) },
  57. { "TimeForFullHeal", INI::parseDurationReal, NULL, offsetof( GarrisonContainModuleData, m_framesForFullHeal ) },
  58. { "InitialRoster", parseInitialRoster, NULL, 0 },
  59. { "ImmuneToClearBuildingAttacks", INI::parseBool, NULL, offsetof( GarrisonContainModuleData, m_immuneToClearBuildingAttacks ) },
  60. { "IsEnclosingContainer", INI::parseBool, NULL, offsetof( GarrisonContainModuleData, m_isEnclosingContainer ) },
  61. { 0, 0, 0, 0 }
  62. };
  63. p.add(dataFieldParse);
  64. };
  65. static void parseInitialRoster( INI* ini, void *instance, void *store, const void* )
  66. {
  67. GarrisonContainModuleData* self = (GarrisonContainModuleData*)instance;
  68. const char* name = ini->getNextToken();
  69. const char* countStr = ini->getNextTokenOrNull();
  70. Int count = countStr ? INI::scanInt(countStr) : 1;
  71. self->m_initialRoster.templateName.set(name);
  72. self->m_initialRoster.count = count;
  73. };
  74. };
  75. //-------------------------------------------------------------------------------------------------
  76. /** A GarrisonContain is used for objects that can be garrisoned, heh, go figure */
  77. //-------------------------------------------------------------------------------------------------
  78. class GarrisonContain : public OpenContain
  79. {
  80. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE( GarrisonContain, "GarrisonContain" )
  81. MAKE_STANDARD_MODULE_MACRO_WITH_MODULE_DATA( GarrisonContain, GarrisonContainModuleData )
  82. public:
  83. GarrisonContain( Thing *thing, const ModuleData* moduleData );
  84. // virtual destructor prototype provided by memory pool declaration
  85. virtual UpdateSleepTime update( void ); ///< called once per frame
  86. virtual Bool isValidContainerFor( const Object* obj, Bool checkCapacity) const; // Garrison has an extra check forbidding any containment if ReallyDamaged
  87. virtual Bool isGarrisonable() const { return true; } ///< can this unit be Garrisoned? (ick)
  88. virtual Bool isBustable() const { return TRUE; } ///< can this container get busted by a bunkerbuster
  89. virtual Bool isImmuneToClearBuildingAttacks() const { return getGarrisonContainModuleData()->m_immuneToClearBuildingAttacks; }
  90. virtual Bool isHealContain() const { return false; } ///< true when container only contains units while healing (not a transport!)
  91. virtual Bool isTunnelContain() const { return FALSE; }
  92. virtual Bool isPassengerAllowedToFire( ObjectID id = INVALID_ID ) const; ///< Hey, can I shoot out of this container?
  93. virtual Bool isEnclosingContainerFor( const Object *obj ) const { return getGarrisonContainModuleData()->m_isEnclosingContainer; }
  94. virtual Bool isSpecialOverlordStyleContainer() const {return FALSE;}
  95. virtual void removeAllContained( Bool exposeStealthUnits ); ///< remove all contents of this open container
  96. virtual void exitObjectViaDoor( Object *exitObj, ExitDoorType exitDoor ); ///< exit one of our content items from us
  97. virtual void exitObjectByBudding( Object *newObj, Object *budHost ) { return; };
  98. virtual void onContaining( Object *obj, Bool wasSelected ); ///< object now contains 'obj'
  99. virtual void onRemoving( Object *obj ); ///< object no longer contains 'obj'
  100. virtual void onSelling( void );
  101. // A Garrison Contain must eject all passengers when it crosses the ReallyDamaged threshold.
  102. virtual void onBodyDamageStateChange( const DamageInfo* damageInfo,
  103. BodyDamageType oldState,
  104. BodyDamageType newState); ///< Die Interface state change callback
  105. /**
  106. return the player that *appears* to control this unit, given an observing player.
  107. if null, use getObject()->getControllingPlayer() instead.
  108. */
  109. virtual const Player* getApparentControllingPlayer( const Player* observingPlayer ) const;
  110. virtual void recalcApparentControllingPlayer( void );
  111. virtual Bool isDisplayedOnControlBar() const {return TRUE;}///< Does this container display its contents on the ControlBar?
  112. virtual void onDamage( DamageInfo *info );
  113. virtual void setEvacDisposition( EvacDisposition disp ) { m_evacDisposition = disp; };
  114. protected:
  115. virtual void redeployOccupants( void ); ///< redeploy the occupants of us at all available garrison points
  116. virtual void onObjectCreated();
  117. void validateRallyPoint( void ); ///< validate (if necessary) and pick (if possible) an exit rally point
  118. virtual Bool calcBestGarrisonPosition( Coord3D *sourcePos, const Coord3D *targetPos );
  119. virtual Bool attemptBestFirePointPosition( Object *source, Weapon *weapon, Object *victim );
  120. virtual Bool attemptBestFirePointPosition( Object *source, Weapon *weapon, const Coord3D *targetPos );
  121. void updateEffects( void ); ///< do any effects needed per frame
  122. void loadGarrisonPoints( void ); ///< load garrison point position data and save for later
  123. void putObjectAtBestGarrisonPoint( Object *obj, Object *target, const Coord3D *targetPos ); ///< place object at position of the best garrison point to use for its target
  124. void putObjectAtGarrisonPoint( Object *obj, ObjectID targetID, Int conditionIndex, Int index ); ///< place object at the specified garrison point index
  125. enum { SEARCH_FOR_REMOVE = -1 };
  126. void removeObjectFromGarrisonPoint( Object *obj, Int index = SEARCH_FOR_REMOVE );///< remove object from the garrison point placement
  127. void addValidObjectsToGarrisonPoints( void ); ///< add any objects with targets to a garrison point
  128. void removeInvalidObjectsFromGarrisonPoints( void ); ///< remove objects with invalid targets from valid points
  129. void trackTargets( void ); ///< keep attackers at the closest garrison point to their active target
  130. void matchObjectsToGarrisonPoints( void ); ///< Every frame, and whenever anyone enters or leaves
  131. void positionObjectsAtStationGarrisonPoints( void ); ///< enforce that everybody stays at their pre-assigned space
  132. void loadStationGarrisonPoints( void );
  133. Bool pickAStationForMe( const Object *pbj );
  134. void removeObjectFromStationPoint( const Object *obj );
  135. enum { GARRISON_INDEX_INVALID = -1 };
  136. Int findConditionIndex( void ); ///< find the condition index to use given the current object body state
  137. Int getObjectGarrisonPointIndex( Object *obj ); ///< get the garrison point index object is at (if present)
  138. Int findClosestFreeGarrisonPointIndex( Int conditionIndex,
  139. const Coord3D *targetPos ); ///< find closest free garrison point to the target location
  140. void healObjects( void ); ///< heal all the objects within me
  141. void healSingleObject( Object *obj, Real frames ); ///< heal just one of the objects within me
  142. void moveObjectsWithMe( void ); ///< translates all the garrisoned object to this->getObject()->getPosition()
  143. private:
  144. enum { MAX_GARRISON_POINTS = 40 };
  145. //
  146. // The max units inside any garrisoned structure is 10. Since the units will "move around"
  147. // the inside of the structure to be close to their targets, we need a max of 10 garrison points
  148. // on each side of the building to accomodate everybody inside
  149. //
  150. // ----------------------------------------------------------------------------------------------
  151. struct GarrisonPointData
  152. {
  153. union
  154. {
  155. Object * object; ///< object at this garrison point
  156. ObjectID objectID; ///< for loading
  157. };
  158. ObjectID targetID; ///< object ID that is our current target
  159. UnsignedInt placeFrame; ///< frame we were placed at this garrison point
  160. UnsignedInt lastEffectFrame; ///< last frame we fired our effects on
  161. union
  162. {
  163. Drawable * effect; ///< effect object for showing gun barrels and muzzle flash fire
  164. DrawableID effectID; ///< for loading
  165. };
  166. };
  167. struct StationPointData
  168. {
  169. ObjectID occupantID;
  170. Coord3D position;
  171. };
  172. // ----------------------------------------------------------------------------------------------
  173. enum
  174. {
  175. GARRISON_POINT_PRISTINE,
  176. GARRISON_POINT_DAMAGED,
  177. GARRISON_POINT_REALLY_DAMAGED,
  178. MAX_GARRISON_POINT_CONDITIONS ///< leave this last
  179. };
  180. Team * m_originalTeam; ///< our original team before we were garrisoned
  181. GarrisonPointData m_garrisonPointData[ MAX_GARRISON_POINTS ]; ///< the garrison point placement data
  182. Int m_garrisonPointsInUse;
  183. Coord3D m_garrisonPoint[ MAX_GARRISON_POINT_CONDITIONS ][ MAX_GARRISON_POINTS ]; ///< the garrison point positions (in world coords) for pristine, damaged, and really damaged
  184. Coord3D m_exitRallyPoint; ///< Point to rally at when exiting structure (if possible)
  185. std::vector<StationPointData> m_stationPointList;
  186. Bool m_stationGarrisonPointsInitialized; ///< DO NOT XFER THIS!!! TRUE once we have loaded the pre-assigned garrison point positions from the art
  187. Bool m_garrisonPointsInitialized; ///< TRUE once we have loaded the garrison point positions from the art
  188. Bool m_hideGarrisonedStateFromNonallies; ///< if T, don't appear to be garrisoned (all stealthy)
  189. Bool m_rallyValid; ///< TRUE when m_exitRallyPoint is valid
  190. EvacDisposition m_evacDisposition;
  191. };
  192. #endif // __GARRISONCONTAIN_H_