PilotFindVehicleUpdate.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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: PilotFindVehicleUpdate.cpp //////////////////////////////////////////////////////////////////////////
  24. // Author: Kris Morness, August 2002
  25. // Desc: Update module to handle independent targeting of hazards to cleanup.
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
  29. #define DEFINE_WEAPONSLOTTYPE_NAMES
  30. #include "GameClient\Drawable.h"
  31. #include "Common/ActionManager.h"
  32. #include "Common/Player.h"
  33. #include "Common/RandomValue.h"
  34. #include "Common/ThingTemplate.h"
  35. #include "Common/Xfer.h"
  36. #include "GameLogic\GameLogic.h"
  37. #include "GameLogic\PartitionManager.h"
  38. #include "GameLogic\Object.h"
  39. #include "GameLogic\ObjectIter.h"
  40. #include "GameLogic\Module\PilotFindVehicleUpdate.h"
  41. #include "GameLogic\Module\AIUpdate.h"
  42. #include "GameLogic\Module\CollideModule.h"
  43. //-------------------------------------------------------------------------------------------------
  44. //-------------------------------------------------------------------------------------------------
  45. PilotFindVehicleUpdateModuleData::PilotFindVehicleUpdateModuleData()
  46. {
  47. m_scanFrames = 0;
  48. m_scanRange = 0.0f;
  49. m_minHealth = 0.5f;
  50. }
  51. //-------------------------------------------------------------------------------------------------
  52. /*static*/ void PilotFindVehicleUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
  53. {
  54. ModuleData::buildFieldParse(p);
  55. static const FieldParse dataFieldParse[] =
  56. {
  57. { "ScanRate", INI::parseDurationUnsignedInt, NULL, offsetof( PilotFindVehicleUpdateModuleData, m_scanFrames ) },
  58. { "ScanRange", INI::parseReal, NULL, offsetof( PilotFindVehicleUpdateModuleData, m_scanRange ) },
  59. { "MinHealth", INI::parseReal, NULL, offsetof( PilotFindVehicleUpdateModuleData, m_minHealth ) },
  60. { 0, 0, 0, 0 }
  61. };
  62. p.add(dataFieldParse);
  63. }
  64. //-------------------------------------------------------------------------------------------------
  65. PilotFindVehicleUpdate::PilotFindVehicleUpdate( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  66. {
  67. m_didMoveToBase = false;
  68. setWakeFrame(getObject(), UPDATE_SLEEP_NONE);
  69. }
  70. //-------------------------------------------------------------------------------------------------
  71. //-------------------------------------------------------------------------------------------------
  72. PilotFindVehicleUpdate::~PilotFindVehicleUpdate( void )
  73. {
  74. }
  75. //-------------------------------------------------------------------------------------------------
  76. void PilotFindVehicleUpdate::onObjectCreated()
  77. {
  78. }
  79. //-------------------------------------------------------------------------------------------------
  80. /** The update callback. */
  81. //-------------------------------------------------------------------------------------------------
  82. UpdateSleepTime PilotFindVehicleUpdate::update()
  83. {
  84. Object *obj = getObject();
  85. if (obj->getControllingPlayer()->getPlayerType() == PLAYER_HUMAN) {
  86. // This is an ai only behavior.
  87. return UPDATE_SLEEP_FOREVER;
  88. }
  89. const PilotFindVehicleUpdateModuleData *data = getPilotFindVehicleUpdateModuleData();
  90. AIUpdateInterface *ai = obj->getAI();
  91. if (ai==NULL) return UPDATE_SLEEP_FOREVER;
  92. if( !ai->isIdle() )
  93. {
  94. return UPDATE_SLEEP(data->m_scanFrames);
  95. }
  96. //Periodic scanning (expensive)
  97. Object *upgradeUnit = scanClosestTarget();
  98. if(upgradeUnit)
  99. {
  100. ai->aiEnter(upgradeUnit, CMD_FROM_AI);
  101. m_didMoveToBase = false;
  102. } else {
  103. Coord3D pos;
  104. // Try moving to base 1 time.
  105. if (!m_didMoveToBase && obj->getControllingPlayer()->getAiBaseCenter(&pos)) {
  106. ai->aiMoveToPosition(&pos, CMD_FROM_AI);
  107. m_didMoveToBase = true;
  108. }
  109. }
  110. return UPDATE_SLEEP(data->m_scanFrames);
  111. }
  112. //-------------------------------------------------------------------------------------------------
  113. Object* PilotFindVehicleUpdate::scanClosestTarget()
  114. {
  115. const PilotFindVehicleUpdateModuleData *data = getPilotFindVehicleUpdateModuleData();
  116. Object *me = getObject();
  117. PartitionFilterAcceptByKindOf kindFilter(MAKE_KINDOF_MASK(KINDOF_VEHICLE), KINDOFMASK_NONE);
  118. PartitionFilterAlive aliveFilter;
  119. PartitionFilterPlayer playerFilter(me->getControllingPlayer(), true);
  120. PartitionFilterSameMapStatus filterMapStatus(getObject());
  121. PartitionFilter *filters[5];
  122. filters[0] = &kindFilter;
  123. filters[1] = &aliveFilter;
  124. filters[2] = &playerFilter;
  125. filters[3] = &filterMapStatus;
  126. filters[4] = NULL;
  127. ObjectIterator *iter = ThePartitionManager->iterateObjectsInRange( me->getPosition(), data->m_scanRange,
  128. FROM_CENTER_2D, filters, ITER_SORTED_NEAR_TO_FAR );
  129. MemoryPoolObjectHolder hold(iter);
  130. for( Object *other = iter->first(); other; other = iter->next() )
  131. {
  132. // Check health.
  133. BodyModuleInterface *body = other->getBodyModule();
  134. if (!body) continue;
  135. // If we're real healthy, don't bother looking for healing.
  136. if (body->getHealth() < body->getMaxHealth()*data->m_minHealth)
  137. {
  138. continue;
  139. }
  140. // first, see if we'd like to collide with 'other'
  141. for (BehaviorModule** m = me->getBehaviorModules(); *m; ++m)
  142. {
  143. CollideModuleInterface* collide = (*m)->getCollide();
  144. if (!collide)
  145. continue;
  146. if( collide->wouldLikeToCollideWith( other ) )
  147. {
  148. return other;
  149. }
  150. }
  151. }
  152. return NULL;
  153. }
  154. // ------------------------------------------------------------------------------------------------
  155. /** CRC */
  156. // ------------------------------------------------------------------------------------------------
  157. void PilotFindVehicleUpdate::crc( Xfer *xfer )
  158. {
  159. // extend base class
  160. UpdateModule::crc( xfer );
  161. } // end crc
  162. // ------------------------------------------------------------------------------------------------
  163. /** Xfer method
  164. * Version Info:
  165. * 1: Initial version */
  166. // ------------------------------------------------------------------------------------------------
  167. void PilotFindVehicleUpdate::xfer( Xfer *xfer )
  168. {
  169. // version
  170. XferVersion currentVersion = 1;
  171. XferVersion version = currentVersion;
  172. xfer->xferVersion( &version, currentVersion );
  173. // extend base class
  174. UpdateModule::xfer( xfer );
  175. // did move to base
  176. xfer->xferBool( &m_didMoveToBase );
  177. } // end xfer
  178. // ------------------------------------------------------------------------------------------------
  179. /** Load post process */
  180. // ------------------------------------------------------------------------------------------------
  181. void PilotFindVehicleUpdate::loadPostProcess( void )
  182. {
  183. // extend base class
  184. UpdateModule::loadPostProcess();
  185. } // end loadPostProcess