DemoTrapUpdate.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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: 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. if( other->isKindOf( KINDOF_DOZER ) )
  174. {
  175. //If we're dealing with a dozer... check if it's trying to disarm me. If so, don't blow up!
  176. Weapon *weapon = other->getCurrentWeapon();
  177. if( weapon && weapon->getDamageType() == DAMAGE_DISARM )
  178. {
  179. //Also check if it's attacking, because it seems to stay in disarm mode.
  180. if( other->testStatus( OBJECT_STATUS_IS_ATTACKING ) )
  181. {
  182. continue;
  183. }
  184. }
  185. }
  186. // order matters: we want to know if I consider it to be an enemy, not vice versa
  187. if( getObject()->getRelationship( other ) != ENEMIES )
  188. {
  189. if( !data->m_friendlyDetonation )
  190. {
  191. //Not allowed to proximity detonate with friends nearby
  192. return UPDATE_SLEEP_NONE;
  193. }
  194. //Don't shoot our friends!
  195. continue;
  196. }
  197. if( other->isAboveTerrain() )
  198. {
  199. //Don't detonate on anything airborne.
  200. continue;
  201. }
  202. //Anyone close enough?
  203. Real fDist = ThePartitionManager->getDistanceSquared( me, other, FROM_CENTER_2D );
  204. if( fDist <= data->m_triggerDetonationRange * data->m_triggerDetonationRange )
  205. {
  206. //Yeehaw!
  207. shallDetonate = true;
  208. if( data->m_friendlyDetonation )
  209. {
  210. //Okay, no need to look for friends because we don't care. All we care
  211. //about is the fact that there is an enemy nearby!
  212. break;
  213. }
  214. }
  215. }
  216. if( shallDetonate )
  217. {
  218. //Enemy in proximity and we are in proximity detonation mode, so trigger the explosion
  219. //and kill them!!! Muwahahaha!
  220. detonate();
  221. }
  222. return UPDATE_SLEEP_NONE;
  223. }
  224. // ------------------------------------------------------------------------------------------------
  225. // ------------------------------------------------------------------------------------------------
  226. void DemoTrapUpdate::detonate()
  227. {
  228. const DemoTrapUpdateModuleData *data = getDemoTrapUpdateModuleData();
  229. Object *me = getObject();
  230. // Only shoot the weapon if not being built or sold.
  231. if( !me->testStatus(OBJECT_STATUS_UNDER_CONSTRUCTION) && !me->testStatus(OBJECT_STATUS_SOLD) )
  232. TheWeaponStore->createAndFireTempWeapon( data->m_detonationWeaponTemplate, me, me->getPosition() );
  233. me->kill();
  234. m_detonated = true;
  235. }
  236. // ------------------------------------------------------------------------------------------------
  237. /** CRC */
  238. // ------------------------------------------------------------------------------------------------
  239. void DemoTrapUpdate::crc( Xfer *xfer )
  240. {
  241. // extend base class
  242. UpdateModule::crc( xfer );
  243. } // end crc
  244. // ------------------------------------------------------------------------------------------------
  245. /** Xfer method
  246. * Version Info:
  247. * 1: Initial version */
  248. // ------------------------------------------------------------------------------------------------
  249. void DemoTrapUpdate::xfer( Xfer *xfer )
  250. {
  251. // version
  252. XferVersion currentVersion = 1;
  253. XferVersion version = currentVersion;
  254. xfer->xferVersion( &version, currentVersion );
  255. // extend base class
  256. UpdateModule::xfer( xfer );
  257. // next scan frames
  258. xfer->xferInt( &m_nextScanFrames );
  259. // detonated
  260. xfer->xferBool( &m_detonated );
  261. } // end xfer
  262. // ------------------------------------------------------------------------------------------------
  263. /** Load post process */
  264. // ------------------------------------------------------------------------------------------------
  265. void DemoTrapUpdate::loadPostProcess( void )
  266. {
  267. // extend base class
  268. UpdateModule::loadPostProcess();
  269. } // end loadPostProcess