PointDefenseLaserUpdate.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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: PointDefenseLaserUpdate.cpp //////////////////////////////////////////////////////////////////////////
  24. // Author: Kris Morness, August 2002
  25. // Desc: Update module to handle independent targeting of point defense laser.
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
  29. #include "Common\BitFlagsIO.h"
  30. #include "Common\RandomValue.h"
  31. #include "Common\ThingTemplate.h"
  32. #include "Common\Xfer.h"
  33. #include "GameClient\Drawable.h"
  34. #include "GameLogic\GameLogic.h"
  35. #include "GameLogic\PartitionManager.h"
  36. #include "GameLogic\Object.h"
  37. #include "GameLogic\ObjectIter.h"
  38. #include "GameLogic\Module\PointDefenseLaserUpdate.h"
  39. #include "GameLogic\Module\PhysicsUpdate.h"
  40. #include "GameLogic\Weapon.h"
  41. //-------------------------------------------------------------------------------------------------
  42. //-------------------------------------------------------------------------------------------------
  43. PointDefenseLaserUpdateModuleData::PointDefenseLaserUpdateModuleData()
  44. {
  45. m_weaponTemplate = NULL;
  46. m_scanFrames = 0;
  47. m_scanRange = 0.0f;
  48. m_velocityFactor = 0.0f;
  49. }
  50. //-------------------------------------------------------------------------------------------------
  51. /*static*/ void PointDefenseLaserUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
  52. {
  53. ModuleData::buildFieldParse(p);
  54. static const FieldParse dataFieldParse[] =
  55. {
  56. { "WeaponTemplate", INI::parseWeaponTemplate, NULL, offsetof( PointDefenseLaserUpdateModuleData, m_weaponTemplate ) },
  57. { "PrimaryTargetTypes", KindOfMaskType::parseFromINI, NULL, offsetof( PointDefenseLaserUpdateModuleData, m_primaryTargetKindOf ) },
  58. { "SecondaryTargetTypes", KindOfMaskType::parseFromINI, NULL, offsetof( PointDefenseLaserUpdateModuleData, m_secondaryTargetKindOf ) },
  59. { "ScanRate", INI::parseDurationUnsignedInt, NULL, offsetof( PointDefenseLaserUpdateModuleData, m_scanFrames ) },
  60. { "ScanRange", INI::parseReal, NULL, offsetof( PointDefenseLaserUpdateModuleData, m_scanRange ) },
  61. { "PredictTargetVelocityFactor", INI::parseReal, NULL, offsetof( PointDefenseLaserUpdateModuleData, m_velocityFactor ) },
  62. { 0, 0, 0, 0 }
  63. };
  64. p.add(dataFieldParse);
  65. }
  66. //-------------------------------------------------------------------------------------------------
  67. PointDefenseLaserUpdate::PointDefenseLaserUpdate( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  68. {
  69. m_bestTargetID = INVALID_ID;
  70. m_nextScanFrames = 0;
  71. m_nextShotAvailableInFrames = 0;
  72. m_inRange = false;
  73. setWakeFrame(getObject(), UPDATE_SLEEP_NONE);// No starting sleep, but we want to sleep later.
  74. }
  75. //-------------------------------------------------------------------------------------------------
  76. //-------------------------------------------------------------------------------------------------
  77. PointDefenseLaserUpdate::~PointDefenseLaserUpdate( void )
  78. {
  79. }
  80. //-------------------------------------------------------------------------------------------------
  81. void PointDefenseLaserUpdate::onObjectCreated()
  82. {
  83. const PointDefenseLaserUpdateModuleData *data = getPointDefenseLaserUpdateModuleData();
  84. //Make sure we have a weapon template
  85. if( !data->m_weaponTemplate )
  86. {
  87. DEBUG_CRASH( ("PointDefenseLaserUpdate for %s doesn't have a valid weapon template",
  88. getObject()->getTemplate()->getName().str() ) );
  89. return;
  90. }
  91. //Make sure our firing range is smaller than the scan range.
  92. WeaponBonus bonus;
  93. bonus.clear();
  94. Real attackRange = data->m_weaponTemplate->getAttackRange( bonus );
  95. if( data->m_scanRange <= attackRange )
  96. {
  97. DEBUG_CRASH( ("PointDefenseLaserUpdate for %s requires the scan range (%.1f) being larger than the firing range (%.1f)",
  98. getObject()->getTemplate()->getName().str(), data->m_scanRange, attackRange ) );
  99. }
  100. }
  101. //-------------------------------------------------------------------------------------------------
  102. /** The update callback. */
  103. //-------------------------------------------------------------------------------------------------
  104. UpdateSleepTime PointDefenseLaserUpdate::update()
  105. {
  106. /// @todo srj use SLEEPY_UPDATE here
  107. //*** HERE'S THE UPDATE PHILOSOPHY ***
  108. //The point defense laser typically has short range, high rate of fire, and shoots at incoming projectiles
  109. //that move fast. This amounts to a potentially very expensive system. Instead of frantically scanning for
  110. //targets, we will scan less frequently (data->m_scanFrames) in a larger radius (data->m_scanRange). When
  111. //this occurs, we'll store the "best" target, and track only that target until the next update or if it is
  112. //killed.
  113. Object *me = getObject();
  114. if( me->isEffectivelyDead() )
  115. return UPDATE_SLEEP_FOREVER;//No more laser fo you.
  116. const PointDefenseLaserUpdateModuleData *data = getPointDefenseLaserUpdateModuleData();
  117. //Optimized firing at acquired target
  118. if( m_nextScanFrames > 0 )
  119. {
  120. m_nextScanFrames--;
  121. fireWhenReady(); //Only happens if something is tracked.
  122. return UPDATE_SLEEP_NONE;
  123. }
  124. m_nextScanFrames = data->m_scanFrames;
  125. //Periodic scanning (expensive)
  126. if( scanClosestTarget() )
  127. {
  128. //1 frame can make a big difference so fire ASAP!
  129. fireWhenReady();
  130. }
  131. return UPDATE_SLEEP_NONE;
  132. }
  133. //-------------------------------------------------------------------------------------------------
  134. void PointDefenseLaserUpdate::fireWhenReady()
  135. {
  136. const PointDefenseLaserUpdateModuleData *data = getPointDefenseLaserUpdateModuleData();
  137. //Track our target
  138. Object *target = TheGameLogic->findObjectByID( m_bestTargetID );
  139. if( target )
  140. {
  141. WeaponBonus bonus;
  142. bonus.clear();
  143. Real fireRange = data->m_weaponTemplate->getAttackRange( bonus );
  144. Object *me = getObject();
  145. Real fDist = sqrt( ThePartitionManager->getDistanceSquared( me, target, FROM_CENTER_2D ) );
  146. if( fDist < fireRange )
  147. {
  148. //We are currently in range!
  149. m_inRange = true;
  150. }
  151. else
  152. {
  153. if( m_inRange )
  154. {
  155. //We were in range last frame, but the target has moved out of firing range, so
  156. //re-evaluate by forcing a new scan.
  157. m_nextScanFrames = GameLogicRandomValue( 0, 3 );
  158. m_bestTargetID = INVALID_ID;
  159. if( !m_nextScanFrames )
  160. {
  161. scanClosestTarget();
  162. m_nextScanFrames = data->m_scanFrames;
  163. target = NULL; //Set target to NULL so we don't shoot at it (might be out of range)
  164. }
  165. }
  166. else
  167. {
  168. //Not in range
  169. m_inRange = false;
  170. }
  171. }
  172. }
  173. if( m_nextShotAvailableInFrames > 0 )
  174. {
  175. //We can't fire this frame.
  176. m_nextShotAvailableInFrames--;
  177. return;
  178. }
  179. WeaponTemplate *wt = data->m_weaponTemplate;
  180. if( wt )
  181. {
  182. WeaponBonus bonus;
  183. //Fire control!
  184. if( target && m_inRange )
  185. {
  186. if( !target->isEffectivelyDead() )
  187. {
  188. Weapon* w = TheWeaponStore->allocateNewWeapon( wt, TERTIARY_WEAPON );
  189. w->loadAmmoNow( getObject() );
  190. w->fireWeapon( getObject(), target );
  191. w->deleteInstance();
  192. // And now that we have shot, set our internal reload timer.
  193. m_nextShotAvailableInFrames = wt->getDelayBetweenShots( bonus );
  194. }
  195. if( target->isEffectivelyDead() )
  196. {
  197. m_nextScanFrames = GameLogicRandomValue( 0, 3 );
  198. m_bestTargetID = INVALID_ID;
  199. if( !m_nextScanFrames )
  200. {
  201. scanClosestTarget();
  202. m_nextScanFrames = data->m_scanFrames;
  203. }
  204. }
  205. }
  206. }
  207. }
  208. //-------------------------------------------------------------------------------------------------
  209. Object* PointDefenseLaserUpdate::scanClosestTarget()
  210. {
  211. const PointDefenseLaserUpdateModuleData *data = getPointDefenseLaserUpdateModuleData();
  212. Object *me = getObject();
  213. Object *bestTargetOutOfRange[2] = { NULL, NULL };
  214. Object *bestTargetInRange[2] = { NULL, NULL };
  215. Real closestDist[2];
  216. Real closestOutsideRange[2];
  217. Int index;
  218. WeaponBonus bonus;
  219. bonus.clear();
  220. Real fireRange = data->m_weaponTemplate->getAttackRange( bonus );
  221. ObjectIterator *iter = ThePartitionManager->iterateObjectsInRange( me->getPosition(), data->m_scanRange, FROM_CENTER_2D );
  222. MemoryPoolObjectHolder hold(iter);
  223. for( Object *other = iter->first(); other; other = iter->next() )
  224. {
  225. if( other->isAnyKindOf( data->m_primaryTargetKindOf ) )
  226. {
  227. //Primary target type
  228. index = 0;
  229. }
  230. else if( other->isAnyKindOf( data->m_secondaryTargetKindOf ) )
  231. {
  232. //Secondary target type (use only if we can't find any primary targets)
  233. index = 1;
  234. }
  235. else
  236. {
  237. //Not a valid target.
  238. continue;
  239. }
  240. // Since we don't have an actual weapon or a weapon set, we lose all of the automatic checks.
  241. // "Borrow" the check for being an AA only laser to stop from shooting planes on airports.
  242. if( !other->isAirborneTarget() && !(data->m_weaponTemplate->getAntiMask() & WEAPON_ANTI_GROUND) )
  243. continue;
  244. // order matters: we want to know if I consider it to be an enemy, not vice versa
  245. if( getObject()->getRelationship( other ) != ENEMIES )
  246. {
  247. //Don't kill our friends!
  248. continue;
  249. }
  250. if( other->testStatus( OBJECT_STATUS_STEALTHED ) && !other->testStatus( OBJECT_STATUS_DETECTED ) && !other->testStatus( OBJECT_STATUS_DISGUISED ) )
  251. {
  252. //We can't see it.
  253. continue;
  254. }
  255. Real fDist = sqrt( ThePartitionManager->getDistanceSquared( me, other, FROM_CENTER_2D ) );
  256. if( fDist <= fireRange )
  257. {
  258. //Inside fire range (which one is closest?)
  259. if( !bestTargetInRange[index] || fDist < closestDist[index] )
  260. {
  261. closestDist[index] = fDist;
  262. bestTargetInRange[index] = other;
  263. }
  264. }
  265. else if( !bestTargetInRange[index] )
  266. {
  267. //Outside fire range.
  268. //Determine where the target will be based on current velocity using (m_velocityFactor * frames)
  269. if( data->m_velocityFactor != 0.0f && !other->isKindOf( KINDOF_IMMOBILE ) )
  270. {
  271. Coord3D pos;
  272. PhysicsBehavior *physics = other->getPhysics();
  273. if( physics )
  274. {
  275. pos.set( physics->getVelocity() );
  276. pos.scale( data->m_velocityFactor );
  277. pos.add( other->getPosition() );
  278. //Recalculate the distance.
  279. fDist = sqrt( ThePartitionManager->getDistanceSquared( me, other, FROM_CENTER_2D ) );
  280. }
  281. }
  282. //Now calculate the best outside range target.
  283. if( !bestTargetOutOfRange[index] || fDist < closestOutsideRange[index] )
  284. {
  285. closestOutsideRange[index] = fDist;
  286. bestTargetOutOfRange[index] = other;
  287. }
  288. }
  289. } // end for, other
  290. if( bestTargetInRange[ 0 ] )
  291. {
  292. //This is the best primary target in range.
  293. m_bestTargetID = bestTargetInRange[ 0 ]->getID();
  294. m_inRange = true;
  295. return bestTargetInRange[ 0 ];
  296. }
  297. if( bestTargetInRange[ 1 ] )
  298. {
  299. //This is the best secondary target in range.
  300. m_bestTargetID = bestTargetInRange[ 1 ]->getID();
  301. m_inRange = true;
  302. return bestTargetInRange[ 1 ];
  303. }
  304. if( bestTargetOutOfRange[ 0 ] )
  305. {
  306. //This is the best primary target out of range.
  307. m_bestTargetID = bestTargetOutOfRange[ 0 ]->getID();
  308. m_inRange = false;
  309. return bestTargetOutOfRange[ 0 ];
  310. }
  311. if( bestTargetOutOfRange[ 1 ] )
  312. {
  313. //This is the best secondary target out of range.
  314. m_bestTargetID = bestTargetOutOfRange[ 1 ]->getID();
  315. m_inRange = false;
  316. return bestTargetOutOfRange[ 1 ];
  317. }
  318. //Utter failure -- nothing on the scope.
  319. m_bestTargetID = INVALID_ID;
  320. m_inRange = false;
  321. return NULL;
  322. }
  323. // ------------------------------------------------------------------------------------------------
  324. /** CRC */
  325. // ------------------------------------------------------------------------------------------------
  326. void PointDefenseLaserUpdate::crc( Xfer *xfer )
  327. {
  328. // extend base class
  329. UpdateModule::crc( xfer );
  330. } // end crc
  331. // ------------------------------------------------------------------------------------------------
  332. /** Xfer method
  333. * Version Info:
  334. * 1: Initial version */
  335. // ------------------------------------------------------------------------------------------------
  336. void PointDefenseLaserUpdate::xfer( Xfer *xfer )
  337. {
  338. // version
  339. XferVersion currentVersion = 1;
  340. XferVersion version = currentVersion;
  341. xfer->xferVersion( &version, currentVersion );
  342. // extend base class
  343. UpdateModule::xfer( xfer );
  344. // best target ID
  345. xfer->xferObjectID( &m_bestTargetID );
  346. // in range
  347. xfer->xferBool( &m_inRange );
  348. // next scan frames
  349. xfer->xferInt( &m_nextScanFrames );
  350. // next shot available in frames
  351. xfer->xferInt( &m_nextShotAvailableInFrames );
  352. } // end xfer
  353. // ------------------------------------------------------------------------------------------------
  354. /** Load post process */
  355. // ------------------------------------------------------------------------------------------------
  356. void PointDefenseLaserUpdate::loadPostProcess( void )
  357. {
  358. // extend base class
  359. UpdateModule::loadPostProcess();
  360. } // end loadPostProcess