SpectreGunshipUpdate.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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: SpectreGunshipUpdate.cpp //////////////////////////////////////////////////////////////////////////
  24. // Author: Mark Lorenzen, April 2003
  25. // Desc: Update module to handle weapon firing of the SpectreGunship Generals special power.
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  28. #define DEFINE_DEATH_NAMES
  29. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  30. #include "Common\GameAudio.h"
  31. #include "Common\ThingTemplate.h"
  32. #include "Common\ThingFactory.h"
  33. #include "Common\Player.h"
  34. #include "Common\PlayerList.h"
  35. #include "Common\Xfer.h"
  36. #include "Common\ClientUpdateModule.h"
  37. #include "GameClient\ControlBar.h"
  38. #include "GameClient\GameClient.h"
  39. #include "GameClient\Drawable.h"
  40. #include "GameClient\ParticleSys.h"
  41. #include "GameClient\FXList.h"
  42. #include "GameClient/ParticleSys.h"
  43. #include "GameLogic\Locomotor.h"
  44. #include "GameLogic\GameLogic.h"
  45. #include "GameLogic\PartitionManager.h"
  46. #include "GameLogic\Object.h"
  47. #include "GameLogic\ObjectIter.h"
  48. #include "GameLogic\Weaponset.h"
  49. #include "GameLogic\Weapon.h"
  50. #include "GameLogic\TerrainLogic.h"
  51. #include "GameLogic\Module\SpecialPowerModule.h"
  52. #include "GameLogic\Module\SpectreGunshipUpdate.h"
  53. #include "GameLogic\Module\PhysicsUpdate.h"
  54. #include "GameLogic\Module\LaserUpdate.h"
  55. #include "GameLogic\Module\ActiveBody.h"
  56. #include "GameLogic\Module\AIUpdate.h"
  57. #include "GameLogic\Module\ContainModule.h"
  58. #ifdef _INTERNAL
  59. // for occasional debugging...
  60. //#pragma optimize("", off)
  61. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  62. #endif
  63. #define ORBIT_INSERTION_SLOPE_MAX (0.8f)
  64. #define ORBIT_INSERTION_SLOPE_MIN (0.5f)
  65. #define ONE (1.0f)
  66. #define ZERO (0.0f)
  67. #define LOTS_OF_SHOTS (9999)
  68. //-------------------------------------------------------------------------------------------------
  69. //-------------------------------------------------------------------------------------------------
  70. SpectreGunshipUpdateModuleData::SpectreGunshipUpdateModuleData()
  71. {
  72. m_specialPowerTemplate = NULL;
  73. /******BOTH*******//*BOTH*//******BOTH*******//******BOTH*******/ m_attackAreaRadius = 200.0f;
  74. /*************/ m_gattlingStrafeFXParticleSystem = NULL;
  75. /*************/ m_howitzerWeaponTemplate = NULL;
  76. /*************/ m_orbitFrames = 0;
  77. /*************/ m_targetingReticleRadius = 25.0f;
  78. /*************/ m_gunshipOrbitRadius = 250.0f;
  79. m_strafingIncrement = 20.0f;
  80. m_orbitInsertionSlope = 0.7f;
  81. m_howitzerFiringRate = 10;
  82. m_howitzerFollowLag = 0;
  83. m_randomOffsetForHowitzer = 20.0f;
  84. }
  85. static Real zero = 0.0f;
  86. //-------------------------------------------------------------------------------------------------
  87. /*static*/ void SpectreGunshipUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
  88. {
  89. ModuleData::buildFieldParse(p);
  90. static const FieldParse dataFieldParse[] =
  91. {
  92. { "SpecialPowerTemplate", INI::parseSpecialPowerTemplate, NULL, offsetof( SpectreGunshipUpdateModuleData, m_specialPowerTemplate ) },
  93. { "GattlingTemplateName", INI::parseAsciiString, NULL, offsetof( SpectreGunshipUpdateModuleData, m_gattlingTemplateName ) },
  94. { "HowitzerFiringRate", INI::parseDurationUnsignedInt, NULL, offsetof( SpectreGunshipUpdateModuleData, m_howitzerFiringRate ) },
  95. { "OrbitTime", INI::parseDurationUnsignedInt, NULL, offsetof( SpectreGunshipUpdateModuleData, m_orbitFrames ) },
  96. { "HowitzerFollowLag", INI::parseDurationUnsignedInt, NULL, offsetof( SpectreGunshipUpdateModuleData, m_howitzerFollowLag ) },
  97. { "AttackAreaRadius", INI::parseReal, NULL, offsetof( SpectreGunshipUpdateModuleData, m_attackAreaRadius ) },
  98. { "StrafingIncrement", INI::parseReal, NULL, offsetof( SpectreGunshipUpdateModuleData, m_strafingIncrement ) },
  99. { "OrbitInsertionSlope", INI::parseReal, NULL, offsetof( SpectreGunshipUpdateModuleData, m_orbitInsertionSlope ) },
  100. { "RandomOffsetForHowitzer", INI::parseReal, NULL, offsetof( SpectreGunshipUpdateModuleData, m_randomOffsetForHowitzer ) },
  101. { "TargetingReticleRadius", INI::parseReal, NULL, offsetof( SpectreGunshipUpdateModuleData, m_targetingReticleRadius ) },
  102. { "GunshipOrbitRadius", INI::parseReal, NULL, offsetof( SpectreGunshipUpdateModuleData, m_gunshipOrbitRadius ) },
  103. { "HowitzerWeaponTemplate", INI::parseWeaponTemplate, NULL, offsetof( SpectreGunshipUpdateModuleData, m_howitzerWeaponTemplate ) },
  104. { "GattlingStrafeFXParticleSystem", INI::parseParticleSystemTemplate, NULL, offsetof( SpectreGunshipUpdateModuleData, m_gattlingStrafeFXParticleSystem ) },
  105. { "AttackAreaDecal", RadiusDecalTemplate::parseRadiusDecalTemplate, NULL, offsetof( SpectreGunshipUpdateModuleData, m_attackAreaDecalTemplate ) },
  106. { "TargetingReticleDecal", RadiusDecalTemplate::parseRadiusDecalTemplate, NULL, offsetof( SpectreGunshipUpdateModuleData, m_targetingReticleDecalTemplate ) },
  107. { 0, 0, 0, 0 }
  108. };
  109. p.add(dataFieldParse);
  110. }
  111. //-------------------------------------------------------------------------------------------------
  112. SpectreGunshipUpdate::SpectreGunshipUpdate( Thing *thing, const ModuleData* moduleData ) : SpecialPowerUpdateModule( thing, moduleData )
  113. {
  114. m_specialPowerModule = NULL;
  115. m_gattlingID = INVALID_ID;
  116. m_status = GUNSHIP_STATUS_IDLE;
  117. m_initialTargetPosition.zero();
  118. m_overrideTargetDestination.zero();
  119. m_gattlingTargetPosition.zero();
  120. m_positionToShootAt.zero();
  121. m_attackAreaDecal.clear();
  122. m_targetingReticleDecal.clear();
  123. m_orbitEscapeFrame = 0;
  124. m_afterburnerSound = *(getObject()->getTemplate()->getPerUnitSound("Afterburner"));
  125. m_howitzerFireSound = *(getObject()->getTemplate()->getPerUnitSound("HowitzerFire"));
  126. m_okToFireHowitzerCounter = 0;
  127. #if defined TRACKERS
  128. m_howitzerTrackerDecal.clear();
  129. #endif
  130. }
  131. //-------------------------------------------------------------------------------------------------
  132. SpectreGunshipUpdate::~SpectreGunshipUpdate( void )
  133. {
  134. m_attackAreaDecal.clear();
  135. m_targetingReticleDecal.clear();
  136. #if defined TRACKERS
  137. m_howitzerTrackerDecal.clear();
  138. #endif
  139. }
  140. //-------------------------------------------------------------------------------------------------
  141. // Validate that we have the necessary data from the ini file.
  142. //-------------------------------------------------------------------------------------------------
  143. void SpectreGunshipUpdate::onObjectCreated()
  144. {
  145. const SpectreGunshipUpdateModuleData *data = getSpectreGunshipUpdateModuleData();
  146. Object *obj = getObject();
  147. if( !data->m_specialPowerTemplate )
  148. {
  149. DEBUG_CRASH( ("%s object's SpectreGunshipUpdate lacks access to the SpecialPowerTemplate. Needs to be specified in ini.", obj->getTemplate()->getName().str() ) );
  150. return;
  151. }
  152. m_specialPowerModule = obj->getSpecialPowerModule( data->m_specialPowerTemplate );
  153. m_satellitePosition.set( obj->getPosition() );
  154. }
  155. //-------------------------------------------------------------------------------------------------
  156. Bool SpectreGunshipUpdate::initiateIntentToDoSpecialPower(const SpecialPowerTemplate *specialPowerTemplate, const Object *targetObj, const Coord3D *targetPos, const Waypoint *way, UnsignedInt commandOptions )
  157. {
  158. const SpectreGunshipUpdateModuleData *data = getSpectreGunshipUpdateModuleData();
  159. if( m_specialPowerModule->getSpecialPowerTemplate() != specialPowerTemplate )
  160. {
  161. //Check to make sure our modules are connected.
  162. return FALSE;
  163. }
  164. if( !BitTest( commandOptions, COMMAND_FIRED_BY_SCRIPT ) )
  165. {
  166. m_initialTargetPosition.set( targetPos );
  167. m_overrideTargetDestination.set( targetPos );
  168. m_gattlingTargetPosition.set( targetPos );
  169. }
  170. else
  171. {
  172. UnsignedInt now = TheGameLogic->getFrame();
  173. m_specialPowerModule->setReadyFrame( now );
  174. m_initialTargetPosition.set( targetPos );
  175. setLogicalStatus( GUNSHIP_STATUS_INSERTING );
  176. }
  177. Object * gunShip = getObject();
  178. if ( gunShip )
  179. {
  180. // MAKE TWEAKS TO AI SO SHIP MOVES PRETTY
  181. AIUpdateInterface *shipAI = gunShip->getAIUpdateInterface();
  182. if ( shipAI)
  183. {
  184. shipAI->chooseLocomotorSet( LOCOMOTORSET_PANIC );
  185. shipAI->getCurLocomotor()->setAllowInvalidPosition(TRUE);
  186. shipAI->getCurLocomotor()->setUltraAccurate(TRUE); // set ultra-accurate just so AI won't try to adjust our dest
  187. }
  188. Drawable *draw = gunShip->getDrawable();
  189. if ( draw )
  190. draw->clearAndSetModelConditionState( MODELCONDITION_DOOR_1_OPENING, MODELCONDITION_DOOR_1_CLOSING );
  191. friend_enableAfterburners(TRUE);
  192. setLogicalStatus( GUNSHIP_STATUS_INSERTING ); // The gunship is en route to the tharget area, from map-edge
  193. // TELL THE GUNNERS ABOARD THE GUNSHIP TO HOLD THEIR FIRE UNTIL ORBIT INSERTION
  194. ContainModuleInterface *shipContain = gunShip->getContain();
  195. if ( shipContain )
  196. {
  197. Object *newGattling = TheGameLogic->findObjectByID( m_gattlingID );
  198. const ThingTemplate *gattlingTemplate = TheThingFactory->findTemplate( data->m_gattlingTemplateName );
  199. if( newGattling != NULL )
  200. {
  201. m_gattlingID = INVALID_ID;
  202. newGattling = NULL;
  203. }
  204. if ( gattlingTemplate )
  205. {
  206. newGattling = TheThingFactory->newObject( gattlingTemplate, getObject()->getTeam() );
  207. DEBUG_ASSERTCRASH( gunShip, ("SpecterGunshipUpdate failed to find or create a GATTLING object"));
  208. shipContain->addToContain( newGattling );
  209. m_gattlingID = newGattling->getID();
  210. }
  211. Object *gattling = TheGameLogic->findObjectByID( m_gattlingID );
  212. if ( gattling )
  213. gattling->setDisabled ( DISABLED_PARALYZED );
  214. }
  215. }
  216. data->m_attackAreaDecalTemplate.createRadiusDecal( *getObject()->getPosition(), data->m_attackAreaRadius, getObject()->getControllingPlayer(), m_attackAreaDecal);
  217. data->m_targetingReticleDecalTemplate.createRadiusDecal( *getObject()->getPosition(), data->m_targetingReticleRadius, getObject()->getControllingPlayer(), m_targetingReticleDecal);
  218. #if defined TRACKERS
  219. data->m_targetingReticleDecalTemplate.createRadiusDecal( *getObject()->getPosition(), data->m_targetingReticleRadius, getObject()->getControllingPlayer(), m_howitzerTrackerDecal);
  220. #endif
  221. SpecialPowerModuleInterface *spmInterface = getObject()->getSpecialPowerModule( specialPowerTemplate );
  222. if( spmInterface )
  223. {
  224. SpecialPowerModule *spModule = (SpecialPowerModule*)spmInterface;
  225. spModule->markSpecialPowerTriggered( &m_initialTargetPosition );
  226. }
  227. return TRUE;
  228. }
  229. //-------------------------------------------------------------------------------------------------
  230. Bool SpectreGunshipUpdate::isPowerCurrentlyInUse( const CommandButton *command ) const
  231. {
  232. return false;
  233. }
  234. //-------------------------------------------------------------------------------------------------
  235. void SpectreGunshipUpdate::setSpecialPowerOverridableDestination( const Coord3D *loc )
  236. {
  237. Object *me = getObject();
  238. if( !me->isDisabled() )
  239. {
  240. m_overrideTargetDestination = *loc;
  241. if( me->getControllingPlayer() && me->getControllingPlayer()->isLocalPlayer() )
  242. {
  243. AudioEventRTS soundToPlay = *me->getTemplate()->getVoiceAttack();
  244. soundToPlay.setObjectID( me->getID() );
  245. TheAudio->addAudioEvent(&soundToPlay);
  246. }
  247. }
  248. }
  249. //-------------------------------------------------------------------------------------------------
  250. Bool SpectreGunshipUpdate::isPointOffMap( const Coord3D& testPos ) const
  251. {
  252. Region3D mapRegion;
  253. TheTerrainLogic->getExtentIncludingBorder( &mapRegion );
  254. if (!mapRegion.isInRegionNoZ( &testPos ))
  255. return true;
  256. return false;
  257. }
  258. // PARTITION FILTERS!
  259. //-----------------------------------------------------------------------------
  260. class PartitionFilterLiveMapEnemies : public PartitionFilter
  261. {
  262. private:
  263. const Object *m_obj;
  264. public:
  265. PartitionFilterLiveMapEnemies(const Object *obj) : m_obj(obj) { }
  266. virtual Bool allow(Object *objOther)
  267. {
  268. // this is way fast (bit test) so do it first.
  269. if (objOther->isEffectivelyDead())
  270. return false;
  271. // this is also way fast (bit test) so do it next.
  272. if (objOther->isOffMap() != m_obj->isOffMap())
  273. return false;
  274. Relationship r = m_obj->getRelationship(objOther);
  275. if (r != ENEMIES)
  276. return false;
  277. return true;
  278. }
  279. #if defined(_DEBUG) || defined(_INTERNAL)
  280. virtual const char* debugGetName() { return "PartitionFilterLiveMapEnemies"; }
  281. #endif
  282. };
  283. //-----------------------------------------------------------------------------
  284. //-------------------------------------------------------------------------------------------------
  285. /** The update callback. */
  286. //-------------------------------------------------------------------------------------------------
  287. UpdateSleepTime SpectreGunshipUpdate::update()
  288. {
  289. const SpectreGunshipUpdateModuleData *data = getSpectreGunshipUpdateModuleData();
  290. Object *gunship = getObject();
  291. if ( gunship )
  292. {
  293. if ( gunship->isEffectivelyDead() )
  294. return UPDATE_SLEEP_FOREVER;
  295. m_attackAreaDecal.update();
  296. m_targetingReticleDecal.update();
  297. #if defined TRACKERS
  298. m_howitzerTrackerDecal.update();
  299. #endif
  300. AIUpdateInterface *shipAI = gunship->getAIUpdateInterface();
  301. AIUpdateInterface *gattlingAI = NULL;
  302. Object *gattling = TheGameLogic->findObjectByID( m_gattlingID );
  303. if ( gattling )
  304. {
  305. gattlingAI = gattling->getAIUpdateInterface();
  306. }
  307. if ( m_status == GUNSHIP_STATUS_INSERTING || m_status == GUNSHIP_STATUS_ORBITING )
  308. {
  309. // init'l apogee
  310. // target *<-------->*
  311. // posi ^\ /
  312. // | \ /
  313. // perigee | \ /
  314. // | */ declination
  315. // | / \
  316. // v/ * m_satelliteposition
  317. // *
  318. // |
  319. // |
  320. // |
  321. // |
  322. // |
  323. // * gunship->getPosition()
  324. //perigee is the point in the orbital arc nearest the satellite being captured
  325. Coord3D perigee = *gunship->getPosition();
  326. perigee.sub( &m_initialTargetPosition );
  327. perigee.z = zero;
  328. Real distanceToTarget = perigee.length();
  329. perigee.normalize();
  330. //apogee is the anteclockwise point fathest from the perigee line
  331. Coord3D apogee;
  332. apogee.z = zero;
  333. apogee.x = -perigee.y;
  334. apogee.y = perigee.x;
  335. // declination intersects line [p][a], given an attack slope of n1/n2
  336. Coord3D declination;
  337. Real n1 = min( ORBIT_INSERTION_SLOPE_MAX, max(ORBIT_INSERTION_SLOPE_MIN, data->m_orbitInsertionSlope) );
  338. Real n2 = ONE - n1;
  339. declination.z = zero;
  340. declination.x = ( perigee.x * n1 ) + ( apogee.x * n2 );
  341. declination.y = ( perigee.y * n1 ) + ( apogee.y * n2 );
  342. //scale out to the orbital radius
  343. Real orbitalRadius = data->m_gunshipOrbitRadius;
  344. declination.x *= orbitalRadius;
  345. declination.y *= orbitalRadius;
  346. m_satellitePosition.x = m_initialTargetPosition.x + declination.x;
  347. m_satellitePosition.y = m_initialTargetPosition.y + declination.y;
  348. if ( shipAI)
  349. {
  350. shipAI->aiMoveToPosition( &m_satellitePosition, CMD_FROM_AI );
  351. }
  352. Real constraintRadius = data->m_attackAreaRadius - data->m_targetingReticleRadius;
  353. //Constrain Target Override to the targeting radius
  354. Coord3D overrideTargetDelta = m_initialTargetPosition;
  355. overrideTargetDelta.sub( &m_overrideTargetDestination );
  356. if ( overrideTargetDelta.length() > constraintRadius )
  357. {
  358. overrideTargetDelta.normalize();
  359. overrideTargetDelta.x *= constraintRadius;
  360. overrideTargetDelta.y *= constraintRadius;
  361. m_overrideTargetDestination.x = m_initialTargetPosition.x - overrideTargetDelta.x;
  362. m_overrideTargetDestination.y = m_initialTargetPosition.y - overrideTargetDelta.y;
  363. }
  364. m_attackAreaDecal.setPosition( m_initialTargetPosition );
  365. m_targetingReticleDecal.setPosition( m_overrideTargetDestination );
  366. #if defined TRACKERS
  367. m_howitzerTrackerDecal.setPosition( m_gattlingTargetPosition );
  368. #endif
  369. if ( (m_status == GUNSHIP_STATUS_INSERTING) && (distanceToTarget < orbitalRadius ) )// close enough to shoot
  370. {
  371. setLogicalStatus( GUNSHIP_STATUS_ORBITING );
  372. m_orbitEscapeFrame = TheGameLogic->getFrame() + data->m_orbitFrames;
  373. Object *gattling = TheGameLogic->findObjectByID( m_gattlingID );
  374. if ( gattling )
  375. gattling->clearDisabled ( DISABLED_PARALYZED );
  376. AIUpdateInterface *shipAI = gunship->getAIUpdateInterface();
  377. if ( shipAI)
  378. {
  379. shipAI->chooseLocomotorSet( LOCOMOTORSET_NORMAL );
  380. shipAI->getCurLocomotor()->setAllowInvalidPosition(TRUE);
  381. shipAI->getCurLocomotor()->setUltraAccurate(TRUE); // set ultra-accurate just so AI won't try to adjust our dest
  382. }
  383. Drawable *draw = gunship->getDrawable();
  384. if ( draw )
  385. draw->clearAndSetModelConditionState( MODELCONDITION_DOOR_1_CLOSING, MODELCONDITION_DOOR_1_OPENING );
  386. friend_enableAfterburners(FALSE);
  387. }
  388. } // endif status == ORBITING || INSERTING
  389. if ( m_status == GUNSHIP_STATUS_ORBITING )
  390. {
  391. Object *validTargetObject = NULL;
  392. if ( TheGameLogic->getFrame() >= m_orbitEscapeFrame )
  393. {
  394. cleanUp();
  395. setLogicalStatus( GUNSHIP_STATUS_DEPARTING );
  396. // CEASE FIRE, RETURN TO BASE
  397. disengageAndDepartAO( gunship );
  398. }//endif escapeframe
  399. else
  400. {
  401. // ONLY EVERY FEW FRAMES DO WE RE_EVALUATE THE TARGET OBJECT
  402. if ( TheGameLogic->getFrame() %data->m_howitzerFiringRate < ONE )
  403. {
  404. m_positionToShootAt = m_overrideTargetDestination; // unless we get a hit, below
  405. PartitionFilterLiveMapEnemies filterObvious( gunship );
  406. PartitionFilterStealthedAndUndetected filterStealth( gunship, false );
  407. PartitionFilterPossibleToAttack filterAttack(ATTACK_NEW_TARGET, gunship, CMD_FROM_AI);
  408. PartitionFilterFreeOfFog filterFogged( gunship->getControllingPlayer()->getPlayerIndex() );
  409. PartitionFilter *filters[6];
  410. Int numFilters = 0;
  411. filters[numFilters++] = &filterObvious;
  412. filters[numFilters++] = &filterStealth;
  413. filters[numFilters++] = &filterAttack;
  414. filters[numFilters++] = &filterFogged;
  415. filters[numFilters] = NULL;
  416. // THIS WILL FIND A VALID TARGET WITHIN THE TARGETING RETICLE
  417. ObjectIterator *iter = ThePartitionManager->iterateObjectsInRange(&m_overrideTargetDestination,
  418. data->m_targetingReticleRadius,
  419. FROM_BOUNDINGSPHERE_2D,
  420. filters,
  421. ITER_SORTED_NEAR_TO_FAR);
  422. MemoryPoolObjectHolder holder(iter);
  423. for (Object *theEnemy = iter->first(); theEnemy; theEnemy = iter->next())
  424. {
  425. if ( theEnemy && isFairDistanceFromShip( theEnemy ) )
  426. {
  427. validTargetObject = theEnemy;
  428. break;
  429. }
  430. }
  431. // WE WANT THE WIDE_RANGE AUTOACQUIRE POWER DISABLED FOR HUMAN PLAYERS
  432. // SO THAT THE SPECTREGUNSHIP REQUIRES BABYSITTING AT ALL TIMES
  433. if (gunship->getControllingPlayer()->getPlayerType() != PLAYER_HUMAN )
  434. {
  435. if ( ! validTargetObject )
  436. {
  437. // set a flag to start the targeting decal fading, since there is nothing to kill there
  438. // THIS WILL FIND A VALID TARGET ANYWHERE INSIDE THE TARGETING AREA (THE BIG CIRCLE)
  439. ObjectIterator *iter = ThePartitionManager->iterateObjectsInRange(&m_initialTargetPosition,
  440. data->m_attackAreaRadius,
  441. FROM_BOUNDINGSPHERE_2D,
  442. filters,
  443. ITER_SORTED_NEAR_TO_FAR);
  444. MemoryPoolObjectHolder holder(iter);
  445. for (Object *theEnemy = iter->first(); theEnemy; theEnemy = iter->next())
  446. {
  447. if ( theEnemy && isFairDistanceFromShip( theEnemy ) )
  448. {
  449. // WE GOT A HIT!!!! SHOOT HIM!
  450. validTargetObject = theEnemy;
  451. m_positionToShootAt = *validTargetObject->getPosition();
  452. break;
  453. }
  454. }
  455. }
  456. }
  457. //lets keep a constant barrage of gattling bullets on the current aim location
  458. if( gattlingAI )
  459. {
  460. if ( validTargetObject )// either in the reticle or the targeting area
  461. gattlingAI->aiAttackObject( validTargetObject, LOTS_OF_SHOTS, CMD_FROM_AI );
  462. else
  463. gattlingAI->aiAttackPosition( &m_gattlingTargetPosition, LOTS_OF_SHOTS, CMD_FROM_AI );
  464. }
  465. // IF THE GATTLING GUN HAS BEEN PLINKING THE TARGET LONG ENOUGH, LETS FOLLOW WITH A VOLLEY OF HOWITZER FIRE
  466. if ( m_okToFireHowitzerCounter > data->m_howitzerFollowLag )
  467. {
  468. WeaponTemplate *wt = data->m_howitzerWeaponTemplate;
  469. if( wt )
  470. {
  471. Coord3D attackPositionWithRandomOffset;
  472. Real offs = data->m_randomOffsetForHowitzer;
  473. attackPositionWithRandomOffset.x = m_gattlingTargetPosition.x + GameLogicRandomValue( -offs, offs );
  474. attackPositionWithRandomOffset.y = m_gattlingTargetPosition.y + GameLogicRandomValue( -offs, offs );
  475. attackPositionWithRandomOffset.z = m_gattlingTargetPosition.z;
  476. TheWeaponStore->createAndFireTempWeapon( wt, gunship, &attackPositionWithRandomOffset );
  477. m_howitzerFireSound.setObjectID(gunship->getID());
  478. TheAudio->addAudioEvent( &m_howitzerFireSound );
  479. }
  480. }
  481. }//endif frame modulator
  482. // GATTLING TARGETING LOGIC------------------------------------------
  483. const ParticleSystemTemplate *tmp = data->m_gattlingStrafeFXParticleSystem;
  484. if (tmp && gattling && gattling->testStatus( OBJECT_STATUS_IS_FIRING_WEAPON) )
  485. {
  486. // I am going to wind my gattling gun toward the next good spot,
  487. //whether an object's position or a cursor position
  488. Coord3D delta = m_positionToShootAt;
  489. delta.sub( &m_gattlingTargetPosition );
  490. Real dist = delta.length();
  491. if ( dist < data->m_strafingIncrement )
  492. {
  493. m_gattlingTargetPosition = m_positionToShootAt;
  494. ++m_okToFireHowitzerCounter;
  495. }
  496. else
  497. {
  498. m_okToFireHowitzerCounter = ZERO;
  499. delta.normalize();
  500. delta.scale( data->m_strafingIncrement );
  501. m_gattlingTargetPosition.add( &delta );
  502. }
  503. const Player *localPlayer = ThePlayerList->getLocalPlayer();
  504. //Make sure the gunship is visible to the player before drawing effects.
  505. if ( gunship->getShroudedStatus( localPlayer->getPlayerIndex() ) <= OBJECTSHROUD_PARTIAL_CLEAR )
  506. {
  507. // This makes the client smoke effects of the gattling cannon strafing the ground toward the attack position
  508. ParticleSystem *sys = TheParticleSystemManager->createParticleSystem(tmp);
  509. if (sys)
  510. {
  511. Coord3D impactPosition;
  512. impactPosition.x = m_gattlingTargetPosition.x + GameClientRandomValueReal( -5.0f, 5.0f );
  513. impactPosition.y = m_gattlingTargetPosition.y + GameClientRandomValueReal( -5.0f, 5.0f );
  514. impactPosition.z = TheTerrainLogic->getGroundHeight( impactPosition.x, impactPosition.y );
  515. sys->setPosition( &impactPosition );
  516. }
  517. }
  518. }
  519. }// end else
  520. }//not orbiting
  521. else if ( m_status == GUNSHIP_STATUS_DEPARTING )
  522. {
  523. if ( isPointOffMap( *gunship->getPosition() ) )
  524. {
  525. TheGameLogic->destroyObject( gunship );
  526. setLogicalStatus( GUNSHIP_STATUS_IDLE );
  527. cleanUp();
  528. // HERE WE NEED TO CLEAN UP THE TERRAIN DECALS, AND ANYTHING ELSE THAT WAS CREATED IN INIT-INTENT[]
  529. }
  530. }
  531. } // endif gunship
  532. else if ( m_status != GUNSHIP_STATUS_IDLE )
  533. {
  534. //OH MY GOODNESS, THE GUNSHIP MUST HAVE GOTTEN SHOT DOWN!
  535. setLogicalStatus( GUNSHIP_STATUS_IDLE );
  536. cleanUp();
  537. }
  538. return UPDATE_SLEEP_NONE;
  539. }
  540. //-------------------------------------------------------------------------------------------------
  541. void SpectreGunshipUpdate::friend_enableAfterburners(Bool v)
  542. {
  543. Object* gunship = getObject();
  544. if (v)
  545. {
  546. gunship->setModelConditionState(MODELCONDITION_JETAFTERBURNER);
  547. if (!m_afterburnerSound.isCurrentlyPlaying())
  548. {
  549. m_afterburnerSound.setObjectID(gunship->getID());
  550. m_afterburnerSound.setPlayingHandle(TheAudio->addAudioEvent(&m_afterburnerSound));
  551. }
  552. }
  553. else
  554. {
  555. gunship->clearModelConditionState(MODELCONDITION_JETAFTERBURNER);
  556. if (m_afterburnerSound.isCurrentlyPlaying())
  557. {
  558. TheAudio->removeAudioEvent(m_afterburnerSound.getPlayingHandle());
  559. }
  560. }
  561. }
  562. Bool SpectreGunshipUpdate::isFairDistanceFromShip( Object *target )
  563. {
  564. Object *gunship = getObject();
  565. if ( !gunship )
  566. return FALSE;
  567. if ( ! target )
  568. return FALSE;
  569. const Coord3D *targetPosition = target->getPosition();
  570. const Coord3D *gunshipPosition = gunship->getPosition();
  571. Coord3D shipToTargetDelta;
  572. shipToTargetDelta.x = gunshipPosition->x - targetPosition->x;
  573. shipToTargetDelta.y = gunshipPosition->y - targetPosition->y;
  574. shipToTargetDelta.z = 0.0f;
  575. return (shipToTargetDelta.length() > getSpectreGunshipUpdateModuleData()->m_gunshipOrbitRadius * 0.75f );
  576. }
  577. //-------------------------------------------------------------------------------------------------
  578. void SpectreGunshipUpdate::cleanUp()
  579. {
  580. m_attackAreaDecal.clear();
  581. m_targetingReticleDecal.clear();
  582. Object *gattling = TheGameLogic->findObjectByID( m_gattlingID );
  583. if ( gattling )
  584. TheGameLogic->destroyObject( gattling );
  585. #if defined TRACKERS
  586. m_howitzerTrackerDecal.clear();
  587. #endif
  588. }
  589. // --------------------------------------------------------------------------------------------------
  590. void SpectreGunshipUpdate::disengageAndDepartAO( Object *gunship )
  591. {
  592. if ( gunship == NULL )
  593. return;
  594. AIUpdateInterface *shipAI = gunship->getAIUpdateInterface();
  595. if ( shipAI)
  596. {
  597. Coord3D exitPoint;// head off the map in the direction you are facing
  598. gunship->getUnitDirectionVector3D( exitPoint );
  599. Real mapSize = 99999.0f;
  600. exitPoint.x *= mapSize;
  601. exitPoint.y *= mapSize;
  602. exitPoint.add( gunship->getPosition() );
  603. shipAI->aiMoveToPosition( &exitPoint, CMD_FROM_AI );
  604. }
  605. Object *gattling = TheGameLogic->findObjectByID( m_gattlingID );
  606. if ( gattling )
  607. gattling->setDisabled ( DISABLED_PARALYZED );
  608. if ( shipAI)
  609. {
  610. shipAI->chooseLocomotorSet( LOCOMOTORSET_PANIC );
  611. shipAI->getCurLocomotor()->setAllowInvalidPosition(TRUE);
  612. shipAI->getCurLocomotor()->setUltraAccurate(TRUE); // set ultra-accurate just so AI won't try to adjust our dest
  613. }
  614. Drawable *draw = gunship->getDrawable();
  615. if ( draw )
  616. draw->clearAndSetModelConditionState( MODELCONDITION_DOOR_1_OPENING, MODELCONDITION_DOOR_1_CLOSING );
  617. friend_enableAfterburners(TRUE);
  618. cleanUp();
  619. return;
  620. }
  621. //-------------------------------------------------------------------------------------------------
  622. Bool SpectreGunshipUpdate::doesSpecialPowerHaveOverridableDestinationActive() const
  623. {
  624. return m_status < GUNSHIP_STATUS_DEPARTING;
  625. }
  626. // ------------------------------------------------------------------------------------------------
  627. /** CRC */
  628. // ------------------------------------------------------------------------------------------------
  629. void SpectreGunshipUpdate::crc( Xfer *xfer )
  630. {
  631. // extend base class
  632. UpdateModule::crc( xfer );
  633. } // end crc
  634. // ------------------------------------------------------------------------------------------------
  635. /** Xfer method
  636. * Version Info:
  637. * 1: Initial version
  638. * 2: Can't flat-save decals, that's a hella crash (they aren't saved (no memory) and they have a pointer in them). And half the class wasn't saved.
  639. */
  640. // ------------------------------------------------------------------------------------------------
  641. void SpectreGunshipUpdate::xfer( Xfer *xfer )
  642. {
  643. // const SpectreGunshipUpdateModuleData *data = getSpectreGunshipUpdateModuleData();
  644. // version
  645. XferVersion currentVersion = 2;
  646. XferVersion version = currentVersion;
  647. xfer->xferVersion( &version, currentVersion );
  648. // extend base class
  649. UpdateModule::xfer( xfer );
  650. // The initial target destination.
  651. xfer->xferCoord3D( &m_initialTargetPosition );
  652. // The manual override target destination.
  653. xfer->xferCoord3D( &m_overrideTargetDestination );
  654. // The current move-to point of the gunship.
  655. xfer->xferCoord3D( &m_satellitePosition );
  656. // status
  657. xfer->xferUser( &m_status, sizeof( GunshipStatus ) );
  658. xfer->xferUnsignedInt( &m_orbitEscapeFrame );
  659. if( version < 2 )
  660. {
  661. xfer->xferUser( &m_attackAreaDecal, sizeof( RadiusDecal ) );
  662. xfer->xferUser( &m_targetingReticleDecal, sizeof( RadiusDecal ) );
  663. #if defined TRACKERS
  664. xfer->xferUser( &m_howitzerTrackerDecal, sizeof( RadiusDecal ) );
  665. #endif
  666. }
  667. if( version >= 2 )
  668. {
  669. xfer->xferCoord3D( &m_gattlingTargetPosition );
  670. xfer->xferCoord3D( &m_positionToShootAt );
  671. xfer->xferUnsignedInt( &m_okToFireHowitzerCounter );
  672. xfer->xferObjectID( &m_gattlingID );
  673. }
  674. } // end xfer
  675. // ------------------------------------------------------------------------------------------------
  676. /** Load post process */
  677. // ------------------------------------------------------------------------------------------------
  678. void SpectreGunshipUpdate::loadPostProcess( void )
  679. {
  680. // extend base class
  681. UpdateModule::loadPostProcess();
  682. } // end loadPostProcess