AssistedTargetingUpdate.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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: AssistedTargetingUpdate.cpp /////////////////////////////////////////////////////////////////////////
  24. // Author: Graham Smallwood, September 2002
  25. // Desc: Outside influences can tell me to attack something out of my normal targeting range
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #define DEFINE_WEAPONSLOTTYPE_NAMES
  30. #include "Common/Player.h"
  31. #include "Common/ThingFactory.h"
  32. #include "Common/Xfer.h"
  33. #include "GameLogic/Object.h"
  34. #include "GameLogic/Weapon.h"
  35. #include "GameLogic/WeaponSet.h"
  36. #include "GameLogic/Module/AIUpdate.h"
  37. #include "GameLogic/Module/AssistedTargetingUpdate.h"
  38. #include "GameLogic/Module/LaserUpdate.h"
  39. //-------------------------------------------------------------------------------------------------
  40. //-------------------------------------------------------------------------------------------------
  41. void AssistedTargetingUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
  42. {
  43. UpdateModuleData::buildFieldParse(p);
  44. static const FieldParse dataFieldParse[] =
  45. {
  46. { "AssistingClipSize", INI::parseInt, NULL, offsetof( AssistedTargetingUpdateModuleData, m_clipSize ) },
  47. { "AssistingWeaponSlot", INI::parseLookupList, TheWeaponSlotTypeNamesLookupList, offsetof( AssistedTargetingUpdateModuleData, m_weaponSlot ) },
  48. { "LaserFromAssisted", INI::parseThingTemplate, NULL, offsetof( AssistedTargetingUpdateModuleData, m_laserFromAssisted ) },
  49. { "LaserToTarget", INI::parseThingTemplate, NULL, offsetof( AssistedTargetingUpdateModuleData, m_laserToTarget ) },
  50. { 0, 0, 0, 0 }
  51. };
  52. p.add(dataFieldParse);
  53. }
  54. //-------------------------------------------------------------------------------------------------
  55. //-------------------------------------------------------------------------------------------------
  56. AssistedTargetingUpdate::AssistedTargetingUpdate( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  57. {
  58. setWakeFrame(getObject(), UPDATE_SLEEP_FOREVER);
  59. }
  60. //-------------------------------------------------------------------------------------------------
  61. //-------------------------------------------------------------------------------------------------
  62. AssistedTargetingUpdate::~AssistedTargetingUpdate( void )
  63. {
  64. }
  65. //-------------------------------------------------------------------------------------------------
  66. //-------------------------------------------------------------------------------------------------
  67. Bool AssistedTargetingUpdate::isFreeToAssist() const
  68. {
  69. // The reload times of my two weapons are tied together, so Ready is indicitive of either.
  70. const Object *me = getObject();
  71. if( !me->isAbleToAttack() )
  72. return FALSE;// This will cover under construction among other things
  73. Bool ready = me->getCurrentWeapon() && me->getCurrentWeapon()->getStatus() == READY_TO_FIRE;
  74. return ready;
  75. }
  76. //-------------------------------------------------------------------------------------------------
  77. //-------------------------------------------------------------------------------------------------
  78. void AssistedTargetingUpdate::assistAttack( const Object *requestingObject, Object *victimObject )
  79. {
  80. const AssistedTargetingUpdateModuleData *md = getAssistedTargetingUpdateModuleData();
  81. Object *me = getObject();
  82. if( !me->getAI() )
  83. return;
  84. // lock it just till the weapon is empty or the attack is "done"
  85. me->setWeaponLock( md->m_weaponSlot, LOCKED_TEMPORARILY );
  86. me->getAI()->aiAttackObject( victimObject, md->m_clipSize, CMD_FROM_AI );
  87. if( md->m_laserFromAssisted )
  88. makeFeedbackLaser( md->m_laserFromAssisted, requestingObject, me );
  89. if( md->m_laserToTarget )
  90. makeFeedbackLaser( md->m_laserToTarget, me, victimObject );
  91. }
  92. //-------------------------------------------------------------------------------------------------
  93. //-------------------------------------------------------------------------------------------------
  94. void AssistedTargetingUpdate::makeFeedbackLaser( const ThingTemplate *laserTemplate, const Object *from, const Object *to )
  95. {
  96. if( !getObject()->getControllingPlayer() )
  97. return;
  98. Team *laserTeam = getObject()->getControllingPlayer()->getDefaultTeam();
  99. Object *laser = TheThingFactory->newObject( laserTemplate, laserTeam );
  100. if( !laser )
  101. return;
  102. Drawable *draw = laser->getDrawable();
  103. static const NameKeyType key_LaserUpdate = NAMEKEY( "LaserUpdate" );
  104. LaserUpdate *update = (LaserUpdate*)draw->findClientUpdateModule( key_LaserUpdate );
  105. if( !update )
  106. {
  107. TheGameLogic->destroyObject( laser );
  108. return;
  109. }
  110. /** @todo In case they increase the duration of these, initLaser should be able to take
  111. Objects as args instead of positions.
  112. */
  113. update->initLaser( getObject(), from->getPosition(), to->getPosition() );
  114. }
  115. //-------------------------------------------------------------------------------------------------
  116. //-------------------------------------------------------------------------------------------------
  117. UpdateSleepTime AssistedTargetingUpdate::update( void )
  118. {
  119. return UPDATE_SLEEP_FOREVER;
  120. }
  121. // ------------------------------------------------------------------------------------------------
  122. /** CRC */
  123. // ------------------------------------------------------------------------------------------------
  124. void AssistedTargetingUpdate::crc( Xfer *xfer )
  125. {
  126. // extend base class
  127. UpdateModule::crc( xfer );
  128. } // end crc
  129. // ------------------------------------------------------------------------------------------------
  130. /** Xfer method
  131. * Version Info:
  132. * 1: Initial version */
  133. // ------------------------------------------------------------------------------------------------
  134. void AssistedTargetingUpdate::xfer( Xfer *xfer )
  135. {
  136. // version
  137. XferVersion currentVersion = 1;
  138. XferVersion version = currentVersion;
  139. xfer->xferVersion( &version, currentVersion );
  140. // extend base class
  141. UpdateModule::xfer( xfer );
  142. } // end xfer
  143. // ------------------------------------------------------------------------------------------------
  144. /** Load post process */
  145. // ------------------------------------------------------------------------------------------------
  146. void AssistedTargetingUpdate::loadPostProcess( void )
  147. {
  148. // extend base class
  149. UpdateModule::loadPostProcess();
  150. } // end loadPostProcess