FireWeaponCollide.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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: 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", INI::parseBitString32, TheObjectStatusBitNames, offsetof( FireWeaponCollideModuleData, m_requiredStatus ) },
  43. { "ForbiddenStatus", INI::parseBitString32, TheObjectStatusBitNames, 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. UnsignedInt status = getObject()->getStatusBits();
  87. if( (status & d->m_requiredStatus) != d->m_requiredStatus )
  88. return FALSE;
  89. if( (status & d->m_forbiddenStatus) != 0 )
  90. return FALSE;
  91. if( m_everFired && d->m_fireOnce )
  92. return FALSE;// can only fire once ever
  93. return TRUE;
  94. }
  95. // ------------------------------------------------------------------------------------------------
  96. /** CRC */
  97. // ------------------------------------------------------------------------------------------------
  98. void FireWeaponCollide::crc( Xfer *xfer )
  99. {
  100. // extend base class
  101. CollideModule::crc( xfer );
  102. } // end crc
  103. // ------------------------------------------------------------------------------------------------
  104. /** Xfer method
  105. * Version Info:
  106. * 1: Initial version */
  107. // ------------------------------------------------------------------------------------------------
  108. void FireWeaponCollide::xfer( Xfer *xfer )
  109. {
  110. // version
  111. XferVersion currentVersion = 1;
  112. XferVersion version = currentVersion;
  113. xfer->xferVersion( &version, currentVersion );
  114. // extend base class
  115. CollideModule::xfer( xfer );
  116. // weapon
  117. Bool collideWeaponPresent = m_collideWeapon ? TRUE : FALSE;
  118. xfer->xferBool( &collideWeaponPresent );
  119. if( collideWeaponPresent )
  120. {
  121. DEBUG_ASSERTCRASH( m_collideWeapon != NULL,
  122. ("FireWeaponCollide::xfer - m_collideWeapon present mismatch\n") );
  123. xfer->xferSnapshot( m_collideWeapon );
  124. } // end else
  125. else
  126. {
  127. DEBUG_ASSERTCRASH( m_collideWeapon == NULL,
  128. ("FireWeaponCollide::Xfer - m_collideWeapon missing mismatch\n" ));
  129. } // end else
  130. // ever fired
  131. xfer->xferBool( &m_everFired );
  132. } // end xfer
  133. // ------------------------------------------------------------------------------------------------
  134. /** Load post process */
  135. // ------------------------------------------------------------------------------------------------
  136. void FireWeaponCollide::loadPostProcess( void )
  137. {
  138. // extend base class
  139. CollideModule::loadPostProcess();
  140. } // end loadPostProcess