FireWeaponCollide.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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: FireWeaponCollide.cpp ///////////////////////////////////////////////////////////////////////////
  24. // Author: Graham Smallwood April 2002
  25. // Desc: Shoot something that collides with me every frame with my weapon
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #define DEFINE_OBJECT_STATUS_NAMES
  30. #include "Common/Xfer.h"
  31. #include "GameLogic/Object.h"
  32. #include "GameLogic/Module/FireWeaponCollide.h"
  33. //-------------------------------------------------------------------------------------------------
  34. //-------------------------------------------------------------------------------------------------
  35. void FireWeaponCollideModuleData::buildFieldParse(MultiIniFieldParse& p)
  36. {
  37. CollideModuleData::buildFieldParse(p);
  38. static const FieldParse dataFieldParse[] =
  39. {
  40. { "CollideWeapon", INI::parseWeaponTemplate, NULL, offsetof( FireWeaponCollideModuleData, m_collideWeaponTemplate ) },
  41. { "FireOnce", INI::parseBool, NULL, offsetof( FireWeaponCollideModuleData, m_fireOnce ) },
  42. { "RequiredStatus", ObjectStatusMaskType::parseFromINI, NULL, offsetof( FireWeaponCollideModuleData, m_requiredStatus ) },
  43. { "ForbiddenStatus", ObjectStatusMaskType::parseFromINI, NULL, offsetof( FireWeaponCollideModuleData, m_forbiddenStatus ) },
  44. { 0, 0, 0, 0 }
  45. };
  46. p.add(dataFieldParse);
  47. }
  48. //-------------------------------------------------------------------------------------------------
  49. //-------------------------------------------------------------------------------------------------
  50. FireWeaponCollide::FireWeaponCollide( Thing *thing, const ModuleData* moduleData ) :
  51. CollideModule( thing, moduleData ),
  52. m_collideWeapon(NULL)
  53. {
  54. m_collideWeapon = TheWeaponStore->allocateNewWeapon(getFireWeaponCollideModuleData()->m_collideWeaponTemplate, PRIMARY_WEAPON);
  55. m_everFired = FALSE;
  56. }
  57. //-------------------------------------------------------------------------------------------------
  58. //-------------------------------------------------------------------------------------------------
  59. FireWeaponCollide::~FireWeaponCollide( void )
  60. {
  61. if (m_collideWeapon)
  62. m_collideWeapon->deleteInstance();
  63. }
  64. //-------------------------------------------------------------------------------------------------
  65. /** The die callback. */
  66. //-------------------------------------------------------------------------------------------------
  67. void FireWeaponCollide::onCollide( Object *other, const Coord3D *loc, const Coord3D *normal )
  68. {
  69. if( other == NULL )
  70. return; //Don't shoot the ground
  71. Object *me = getObject();
  72. // This will fire at you every frame, because multiple people could be colliding and we want
  73. // to hurt them all. Another solution would be to keep a Map of other->objetIDs and
  74. // delays for each individually. However, this solution here is so quick and simple that it
  75. // warrants the "do it eventually if we need it" clause.
  76. if( shouldFireWeapon() )
  77. {
  78. m_collideWeapon->loadAmmoNow( me );
  79. m_collideWeapon->fireWeapon( me, other );
  80. }
  81. }
  82. //-------------------------------------------------------------------------------------------------
  83. Bool FireWeaponCollide::shouldFireWeapon()
  84. {
  85. const FireWeaponCollideModuleData *d = getFireWeaponCollideModuleData();
  86. ObjectStatusMaskType status = getObject()->getStatusBits();
  87. //We need all required status or else we fail
  88. if( !status.testForAll( d->m_requiredStatus ) )
  89. return FALSE;
  90. //If we have any forbidden statii, then fail
  91. if( status.testForAny( d->m_forbiddenStatus ) )
  92. return FALSE;
  93. if( m_everFired && d->m_fireOnce )
  94. return FALSE;// can only fire once ever
  95. return TRUE;
  96. }
  97. // ------------------------------------------------------------------------------------------------
  98. /** CRC */
  99. // ------------------------------------------------------------------------------------------------
  100. void FireWeaponCollide::crc( Xfer *xfer )
  101. {
  102. // extend base class
  103. CollideModule::crc( xfer );
  104. } // end crc
  105. // ------------------------------------------------------------------------------------------------
  106. /** Xfer method
  107. * Version Info:
  108. * 1: Initial version */
  109. // ------------------------------------------------------------------------------------------------
  110. void FireWeaponCollide::xfer( Xfer *xfer )
  111. {
  112. // version
  113. XferVersion currentVersion = 1;
  114. XferVersion version = currentVersion;
  115. xfer->xferVersion( &version, currentVersion );
  116. // extend base class
  117. CollideModule::xfer( xfer );
  118. // weapon
  119. Bool collideWeaponPresent = m_collideWeapon ? TRUE : FALSE;
  120. xfer->xferBool( &collideWeaponPresent );
  121. if( collideWeaponPresent )
  122. {
  123. DEBUG_ASSERTCRASH( m_collideWeapon != NULL,
  124. ("FireWeaponCollide::xfer - m_collideWeapon present mismatch\n") );
  125. xfer->xferSnapshot( m_collideWeapon );
  126. } // end else
  127. else
  128. {
  129. DEBUG_ASSERTCRASH( m_collideWeapon == NULL,
  130. ("FireWeaponCollide::Xfer - m_collideWeapon missing mismatch\n" ));
  131. } // end else
  132. // ever fired
  133. xfer->xferBool( &m_everFired );
  134. } // end xfer
  135. // ------------------------------------------------------------------------------------------------
  136. /** Load post process */
  137. // ------------------------------------------------------------------------------------------------
  138. void FireWeaponCollide::loadPostProcess( void )
  139. {
  140. // extend base class
  141. CollideModule::loadPostProcess();
  142. } // end loadPostProcess