AssistedTargetingUpdate.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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: 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. #ifdef _INTERNAL
  40. // for occasional debugging...
  41. //#pragma optimize("", off)
  42. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  43. #endif
  44. //-------------------------------------------------------------------------------------------------
  45. //-------------------------------------------------------------------------------------------------
  46. void AssistedTargetingUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
  47. {
  48. UpdateModuleData::buildFieldParse(p);
  49. static const FieldParse dataFieldParse[] =
  50. {
  51. { "AssistingClipSize", INI::parseInt, NULL, offsetof( AssistedTargetingUpdateModuleData, m_clipSize ) },
  52. { "AssistingWeaponSlot", INI::parseLookupList, TheWeaponSlotTypeNamesLookupList, offsetof( AssistedTargetingUpdateModuleData, m_weaponSlot ) },
  53. { "LaserFromAssisted", INI::parseAsciiString, NULL, offsetof( AssistedTargetingUpdateModuleData, m_laserFromAssistedName ) },
  54. { "LaserToTarget", INI::parseAsciiString, NULL, offsetof( AssistedTargetingUpdateModuleData, m_laserToTargetName ) },
  55. { 0, 0, 0, 0 }
  56. };
  57. p.add(dataFieldParse);
  58. }
  59. //-------------------------------------------------------------------------------------------------
  60. //-------------------------------------------------------------------------------------------------
  61. AssistedTargetingUpdate::AssistedTargetingUpdate( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  62. {
  63. m_laserFromAssisted = NULL;
  64. m_laserToTarget = NULL;
  65. }
  66. //-------------------------------------------------------------------------------------------------
  67. //-------------------------------------------------------------------------------------------------
  68. AssistedTargetingUpdate::~AssistedTargetingUpdate( void )
  69. {
  70. }
  71. //-------------------------------------------------------------------------------------------------
  72. //-------------------------------------------------------------------------------------------------
  73. Bool AssistedTargetingUpdate::isFreeToAssist() const
  74. {
  75. // The reload times of my two weapons are tied together, so Ready is indicitive of either.
  76. const Object *me = getObject();
  77. if( !me->isAbleToAttack() )
  78. return FALSE;// This will cover under construction among other things
  79. Bool ready = me->getCurrentWeapon() && me->getCurrentWeapon()->getStatus() == READY_TO_FIRE;
  80. return ready;
  81. }
  82. //-------------------------------------------------------------------------------------------------
  83. //-------------------------------------------------------------------------------------------------
  84. void AssistedTargetingUpdate::assistAttack( const Object *requestingObject, Object *victimObject )
  85. {
  86. const AssistedTargetingUpdateModuleData *md = getAssistedTargetingUpdateModuleData();
  87. Object *me = getObject();
  88. if( !me->getAI() )
  89. return;
  90. // lock it just till the weapon is empty or the attack is "done"
  91. me->setWeaponLock( md->m_weaponSlot, LOCKED_TEMPORARILY );
  92. me->getAI()->aiAttackObject( victimObject, md->m_clipSize, CMD_FROM_AI );
  93. if( m_laserFromAssisted )
  94. makeFeedbackLaser( m_laserFromAssisted, requestingObject, me );
  95. if( m_laserToTarget )
  96. makeFeedbackLaser( m_laserToTarget, me, victimObject );
  97. }
  98. //-------------------------------------------------------------------------------------------------
  99. //-------------------------------------------------------------------------------------------------
  100. void AssistedTargetingUpdate::makeFeedbackLaser( const ThingTemplate *laserTemplate, const Object *from, const Object *to )
  101. {
  102. if( !getObject()->getControllingPlayer() )
  103. return;
  104. Team *laserTeam = getObject()->getControllingPlayer()->getDefaultTeam();
  105. Object *laser = TheThingFactory->newObject( laserTemplate, laserTeam );
  106. if( !laser )
  107. return;
  108. // Give it a good basis in reality to ensure it can draw when on screen.
  109. laser->setPosition(from->getPosition());
  110. Drawable *draw = laser->getDrawable();
  111. static const NameKeyType key_LaserUpdate = NAMEKEY( "LaserUpdate" );
  112. LaserUpdate *update = (LaserUpdate*)draw->findClientUpdateModule( key_LaserUpdate );
  113. if( !update )
  114. {
  115. TheGameLogic->destroyObject( laser );
  116. return;
  117. }
  118. update->initLaser( getObject(), to, from->getPosition(), to->getPosition(), "" );
  119. }
  120. //-------------------------------------------------------------------------------------------------
  121. //-------------------------------------------------------------------------------------------------
  122. UpdateSleepTime AssistedTargetingUpdate::update( void )
  123. {
  124. const AssistedTargetingUpdateModuleData *d = getAssistedTargetingUpdateModuleData();
  125. m_laserFromAssisted = TheThingFactory->findTemplate( d->m_laserFromAssistedName );
  126. m_laserToTarget =TheThingFactory->findTemplate( d->m_laserFromAssistedName );
  127. return UPDATE_SLEEP_FOREVER;
  128. }
  129. // ------------------------------------------------------------------------------------------------
  130. /** CRC */
  131. // ------------------------------------------------------------------------------------------------
  132. void AssistedTargetingUpdate::crc( Xfer *xfer )
  133. {
  134. // extend base class
  135. UpdateModule::crc( xfer );
  136. } // end crc
  137. // ------------------------------------------------------------------------------------------------
  138. /** Xfer method
  139. * Version Info:
  140. * 1: Initial version */
  141. // ------------------------------------------------------------------------------------------------
  142. void AssistedTargetingUpdate::xfer( Xfer *xfer )
  143. {
  144. // version
  145. XferVersion currentVersion = 1;
  146. XferVersion version = currentVersion;
  147. xfer->xferVersion( &version, currentVersion );
  148. // extend base class
  149. UpdateModule::xfer( xfer );
  150. } // end xfer
  151. // ------------------------------------------------------------------------------------------------
  152. /** Load post process */
  153. // ------------------------------------------------------------------------------------------------
  154. void AssistedTargetingUpdate::loadPostProcess( void )
  155. {
  156. const AssistedTargetingUpdateModuleData *d = getAssistedTargetingUpdateModuleData();
  157. m_laserFromAssisted = TheThingFactory->findTemplate( d->m_laserFromAssistedName );
  158. m_laserToTarget =TheThingFactory->findTemplate( d->m_laserFromAssistedName );
  159. // extend base class
  160. UpdateModule::loadPostProcess();
  161. } // end loadPostProcess