DemoTrapUpdate.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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: DemoTrapUpdate.cpp //////////////////////////////////////////////////////////////////////////
  24. // Author: Kris Morness, August 2002
  25. // Desc: Update module to handle demo trap proximity triggering.
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  28. #define DEFINE_WEAPONSLOTTYPE_NAMES
  29. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  30. #include "Common\BitFlagsIO.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\DemoTrapUpdate.h"
  39. #include "GameLogic\Module\PhysicsUpdate.h"
  40. #include "GameLogic\Weaponset.h"
  41. #include "GameLogic\Weapon.h"
  42. //-------------------------------------------------------------------------------------------------
  43. //-------------------------------------------------------------------------------------------------
  44. DemoTrapUpdateModuleData::DemoTrapUpdateModuleData()
  45. {
  46. m_defaultsToProximityMode = false;
  47. m_friendlyDetonation = false;
  48. m_manualModeWeaponSlot = PRIMARY_WEAPON;
  49. m_detonationWeaponSlot = PRIMARY_WEAPON;
  50. m_proximityModeWeaponSlot = PRIMARY_WEAPON;
  51. m_triggerDetonationRange = 0.0f;
  52. m_scanFrames = 0;
  53. m_detonationWeaponTemplate = NULL;
  54. m_detonateWhenKilled = false;
  55. }
  56. //-------------------------------------------------------------------------------------------------
  57. /*static*/ void DemoTrapUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
  58. {
  59. ModuleData::buildFieldParse(p);
  60. static const FieldParse dataFieldParse[] =
  61. {
  62. { "DefaultProximityMode", INI::parseBool, NULL, offsetof( DemoTrapUpdateModuleData, m_defaultsToProximityMode ) },
  63. { "DetonationWeaponSlot", INI::parseLookupList, TheWeaponSlotTypeNamesLookupList, offsetof( DemoTrapUpdateModuleData, m_detonationWeaponSlot ) },
  64. { "ProximityModeWeaponSlot", INI::parseLookupList, TheWeaponSlotTypeNamesLookupList, offsetof( DemoTrapUpdateModuleData, m_proximityModeWeaponSlot ) },
  65. { "ManualModeWeaponSlot", INI::parseLookupList, TheWeaponSlotTypeNamesLookupList, offsetof( DemoTrapUpdateModuleData, m_manualModeWeaponSlot ) },
  66. { "TriggerDetonationRange", INI::parseReal, NULL, offsetof( DemoTrapUpdateModuleData, m_triggerDetonationRange ) },
  67. { "IgnoreTargetTypes", KindOfMaskType::parseFromINI, NULL, offsetof( DemoTrapUpdateModuleData, m_ignoreKindOf ) },
  68. { "ScanRate", INI::parseDurationUnsignedInt, NULL, offsetof( DemoTrapUpdateModuleData, m_scanFrames ) },
  69. { "AutoDetonationWithFriendsInvolved", INI::parseBool, NULL, offsetof( DemoTrapUpdateModuleData, m_friendlyDetonation ) },
  70. { "DetonationWeapon", INI::parseWeaponTemplate, NULL, offsetof( DemoTrapUpdateModuleData, m_detonationWeaponTemplate ) },
  71. { "DetonateWhenKilled", INI::parseBool, NULL, offsetof( DemoTrapUpdateModuleData, m_detonateWhenKilled ) },
  72. { 0, 0, 0, 0 }
  73. };
  74. p.add(dataFieldParse);
  75. }
  76. //-------------------------------------------------------------------------------------------------
  77. DemoTrapUpdate::DemoTrapUpdate( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  78. {
  79. m_nextScanFrames = 0;
  80. m_detonated = false;
  81. }
  82. //-------------------------------------------------------------------------------------------------
  83. //-------------------------------------------------------------------------------------------------
  84. DemoTrapUpdate::~DemoTrapUpdate( void )
  85. {
  86. }
  87. //-------------------------------------------------------------------------------------------------
  88. // Validate that we have the necessary data from the ini file.
  89. //-------------------------------------------------------------------------------------------------
  90. void DemoTrapUpdate::onObjectCreated()
  91. {
  92. const DemoTrapUpdateModuleData *data = getDemoTrapUpdateModuleData();
  93. if( data->m_detonationWeaponSlot == data->m_proximityModeWeaponSlot ||
  94. data->m_detonationWeaponSlot == data->m_manualModeWeaponSlot ||
  95. data->m_proximityModeWeaponSlot == data->m_manualModeWeaponSlot )
  96. {
  97. DEBUG_CRASH( ("The demo trap requires three weaponslots: One for each of the detonation mode, proximity mode, and manual mode.") );
  98. }
  99. getObject()->setWeaponSetFlag( WEAPONSET_VETERAN );
  100. if( data->m_defaultsToProximityMode )
  101. {
  102. // lock it just till the weapon is empty or the attack is "done"
  103. getObject()->setWeaponLock( data->m_proximityModeWeaponSlot, LOCKED_TEMPORARILY );
  104. }
  105. else
  106. {
  107. // lock it just till the weapon is empty or the attack is "done"
  108. getObject()->setWeaponLock( data->m_manualModeWeaponSlot, LOCKED_TEMPORARILY );
  109. }
  110. }
  111. //-------------------------------------------------------------------------------------------------
  112. /** The update callback. */
  113. //-------------------------------------------------------------------------------------------------
  114. UpdateSleepTime DemoTrapUpdate::update()
  115. {
  116. /// @todo srj use SLEEPY_UPDATE here
  117. const DemoTrapUpdateModuleData *data = getDemoTrapUpdateModuleData();
  118. if( m_detonated )
  119. {
  120. return UPDATE_SLEEP_NONE;
  121. }
  122. Object *me = getObject();
  123. if( me->testStatus(OBJECT_STATUS_UNDER_CONSTRUCTION) || me->testStatus(OBJECT_STATUS_SOLD) )
  124. {
  125. return UPDATE_SLEEP_NONE;
  126. }
  127. if( me->isEffectivelyDead() )
  128. {
  129. if( data->m_detonateWhenKilled )
  130. {
  131. detonate();
  132. }
  133. return UPDATE_SLEEP_NONE;
  134. }
  135. //Get the current weapon slot -- this determines what mode we're in.
  136. WeaponSlotType weaponSlot = getObject()->getCurrentWeapon()->getWeaponSlot();
  137. if( weaponSlot == data->m_detonationWeaponSlot )
  138. {
  139. //We've been externally triggered by the press of a command button.
  140. detonate();
  141. return UPDATE_SLEEP_NONE;
  142. }
  143. //Don't scan every frame for performance reasons.
  144. if( m_nextScanFrames > 0 )
  145. {
  146. m_nextScanFrames--;
  147. return UPDATE_SLEEP_NONE;
  148. }
  149. if( weaponSlot == data->m_manualModeWeaponSlot )
  150. {
  151. //Don't scan!
  152. return UPDATE_SLEEP_NONE;
  153. }
  154. //Reset timer here -- because if we are in manual mode, and switch, we want instant
  155. //gratification (if possible).
  156. m_nextScanFrames = data->m_scanFrames;
  157. //Scan for a valid enemy in proximity range.
  158. ObjectIterator *iter = ThePartitionManager->iterateObjectsInRange( me->getPosition(), data->m_triggerDetonationRange, FROM_CENTER_2D );
  159. MemoryPoolObjectHolder hold(iter);
  160. Bool shallDetonate = false;
  161. //Now iterate through each object in range and check to see if it should detonate us!
  162. for( Object *other = iter->first(); other; other = iter->next() )
  163. {
  164. if( other->isAnyKindOf( data->m_ignoreKindOf ) )
  165. {
  166. //Skip specified types to ignore.
  167. continue;
  168. }
  169. if( other->isEffectivelyDead() )
  170. {
  171. continue;
  172. }
  173. // order matters: we want to know if I consider it to be an enemy, not vice versa
  174. if( getObject()->getRelationship( other ) != ENEMIES )
  175. {
  176. if( !data->m_friendlyDetonation )
  177. {
  178. //Not allowed to proximity detonate with friends nearby
  179. return UPDATE_SLEEP_NONE;
  180. }
  181. //Don't shoot our friends!
  182. continue;
  183. }
  184. if( other->isAboveTerrain() )
  185. {
  186. //Don't detonate on anything airborne.
  187. continue;
  188. }
  189. //Anyone close enough?
  190. Real fDist = ThePartitionManager->getDistanceSquared( me, other, FROM_CENTER_2D );
  191. if( fDist <= data->m_triggerDetonationRange * data->m_triggerDetonationRange )
  192. {
  193. //Yeehaw!
  194. shallDetonate = true;
  195. if( data->m_friendlyDetonation )
  196. {
  197. //Okay, no need to look for friends because we don't care. All we care
  198. //about is the fact that there is an enemy nearby!
  199. break;
  200. }
  201. }
  202. }
  203. if( shallDetonate )
  204. {
  205. //Enemy in proximity and we are in proximity detonation mode, so trigger the explosion
  206. //and kill them!!! Muwahahaha!
  207. detonate();
  208. }
  209. return UPDATE_SLEEP_NONE;
  210. }
  211. // ------------------------------------------------------------------------------------------------
  212. // ------------------------------------------------------------------------------------------------
  213. void DemoTrapUpdate::detonate()
  214. {
  215. const DemoTrapUpdateModuleData *data = getDemoTrapUpdateModuleData();
  216. Object *me = getObject();
  217. // Only shoot the weapon if not being built or sold.
  218. if( !me->testStatus(OBJECT_STATUS_UNDER_CONSTRUCTION) && !me->testStatus(OBJECT_STATUS_SOLD) )
  219. TheWeaponStore->createAndFireTempWeapon( data->m_detonationWeaponTemplate, me, me->getPosition() );
  220. me->kill();
  221. m_detonated = true;
  222. }
  223. // ------------------------------------------------------------------------------------------------
  224. /** CRC */
  225. // ------------------------------------------------------------------------------------------------
  226. void DemoTrapUpdate::crc( Xfer *xfer )
  227. {
  228. // extend base class
  229. UpdateModule::crc( xfer );
  230. } // end crc
  231. // ------------------------------------------------------------------------------------------------
  232. /** Xfer method
  233. * Version Info:
  234. * 1: Initial version */
  235. // ------------------------------------------------------------------------------------------------
  236. void DemoTrapUpdate::xfer( Xfer *xfer )
  237. {
  238. // version
  239. XferVersion currentVersion = 1;
  240. XferVersion version = currentVersion;
  241. xfer->xferVersion( &version, currentVersion );
  242. // extend base class
  243. UpdateModule::xfer( xfer );
  244. // next scan frames
  245. xfer->xferInt( &m_nextScanFrames );
  246. // detonated
  247. xfer->xferBool( &m_detonated );
  248. } // end xfer
  249. // ------------------------------------------------------------------------------------------------
  250. /** Load post process */
  251. // ------------------------------------------------------------------------------------------------
  252. void DemoTrapUpdate::loadPostProcess( void )
  253. {
  254. // extend base class
  255. UpdateModule::loadPostProcess();
  256. } // end loadPostProcess