ProjectileStreamUpdate.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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: ProjectileStreamUpdate.cpp //////////////////////////////////////////////////////////////////////////
  24. // Author: Graham Smallwood, May 2002
  25. // Desc: Tracks all projectiles fired so they can be drawn as a stream
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #include "Common/Xfer.h"
  30. #include "GameLogic/GameLogic.h"
  31. #include "GameLogic/Object.h"
  32. #include "GameLogic/Module/ProjectileStreamUpdate.h"
  33. #include "WWMath/Vector3.h"
  34. #ifdef _INTERNAL
  35. // for occasional debugging...
  36. //#pragma optimize("", off)
  37. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  38. #endif
  39. //-------------------------------------------------------------------------------------------------
  40. //-------------------------------------------------------------------------------------------------
  41. ProjectileStreamUpdate::ProjectileStreamUpdate( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  42. {
  43. ObjectID m_projectileIDs[MAX_PROJECTILE_STREAM];
  44. for( Int index = 0; index < MAX_PROJECTILE_STREAM; index++ )
  45. {
  46. m_projectileIDs[index] = INVALID_ID;
  47. }
  48. m_owningObject = INVALID_ID;
  49. m_nextFreeIndex = 0;
  50. m_firstValidIndex = 0;
  51. }
  52. //-------------------------------------------------------------------------------------------------
  53. //-------------------------------------------------------------------------------------------------
  54. ProjectileStreamUpdate::~ProjectileStreamUpdate( void )
  55. {
  56. }
  57. //-------------------------------------------------------------------------------------------------
  58. /** The update callback. */
  59. //-------------------------------------------------------------------------------------------------
  60. UpdateSleepTime ProjectileStreamUpdate::update( void )
  61. {
  62. cullFrontOfList();
  63. // Update the draw module about our points (arrange array so they can read?)
  64. if( considerDying() )
  65. TheGameLogic->destroyObject( getObject() );
  66. return UPDATE_SLEEP_NONE;
  67. }
  68. void ProjectileStreamUpdate::addProjectile( ObjectID sourceID, ObjectID newID )
  69. {
  70. DEBUG_ASSERTCRASH( m_owningObject == INVALID_ID || m_owningObject == sourceID, ("Two objects are trying to use the same Projectile stream.") );//Don't cross the streams!
  71. if( m_owningObject == INVALID_ID )
  72. m_owningObject = sourceID;
  73. // Keep track of the id in a circular array
  74. m_projectileIDs[ m_nextFreeIndex ] = newID;
  75. m_nextFreeIndex = (m_nextFreeIndex + 1) % MAX_PROJECTILE_STREAM;
  76. DEBUG_ASSERTCRASH( m_nextFreeIndex != m_firstValidIndex, ("Need to increase the allowed number of simultaneous particles in ProjectileStreamUpdate.") );
  77. }
  78. void ProjectileStreamUpdate::cullFrontOfList()
  79. {
  80. while( (m_firstValidIndex != m_nextFreeIndex) && (TheGameLogic->findObjectByID( m_projectileIDs[m_firstValidIndex] ) == NULL) )
  81. {
  82. // Chew off the front if they are gone. Don't chew on the middle, as bad ones there are just a break in the chain
  83. m_firstValidIndex = (m_firstValidIndex + 1) % MAX_PROJECTILE_STREAM;
  84. }
  85. }
  86. Bool ProjectileStreamUpdate::considerDying()
  87. {
  88. if( m_firstValidIndex == m_nextFreeIndex && m_owningObject != INVALID_ID )
  89. {
  90. //If I have no projectiles to watch, and my master is dead, then yes, I want to die
  91. if( TheGameLogic->findObjectByID(m_owningObject) == NULL )
  92. return TRUE;
  93. }
  94. return FALSE;
  95. }
  96. void ProjectileStreamUpdate::getAllPoints( Vector3 *points, Int *count )
  97. {
  98. Int pointCount = 0;
  99. Int pointIndex = m_firstValidIndex;
  100. Object *obj = TheGameLogic->findObjectByID(m_owningObject);
  101. while( pointIndex != m_nextFreeIndex )
  102. {
  103. // Go through the array I think of as good. Holes in the middle get 0,0,0. I write
  104. // to pointCount because I am unrolling a circular array into the same sized flat one
  105. // since I am writing anyway.
  106. Object *projectile = TheGameLogic->findObjectByID( m_projectileIDs[pointIndex] );
  107. if( projectile )
  108. {
  109. Coord3D thisPoint = *projectile->getPosition();
  110. points[pointCount].X = thisPoint.x;
  111. points[pointCount].Y = thisPoint.y;
  112. points[pointCount].Z = thisPoint.z;
  113. if ( obj && obj->isKindOf( KINDOF_VEHICLE ) ) // this makes the stream skim along my roof, if I have a roof
  114. {
  115. const Coord3D *pos = obj->getPosition();
  116. Real myTop = obj->getGeometryInfo().getMaxHeightAbovePosition() + pos->z + 0.5f;
  117. Coord3D delta;
  118. delta.x = pos->x - points[pointCount].X;
  119. delta.y = pos->y - points[pointCount].Y;
  120. delta.z = 0.0f;
  121. if( delta.length() <= obj->getGeometryInfo().getMajorRadius() * 1.5f )
  122. points[pointCount].Z = MAX( points[pointCount].Z, myTop );
  123. }
  124. }
  125. else
  126. {
  127. points[pointCount].X = 0;
  128. points[pointCount].Y = 0;
  129. points[pointCount].Z = 0;
  130. }
  131. pointIndex = (pointIndex + 1) % MAX_PROJECTILE_STREAM;
  132. pointCount++;
  133. }
  134. *count = pointCount;
  135. }
  136. void ProjectileStreamUpdate::setPosition( const Coord3D *newPosition )
  137. {
  138. Object *me = getObject();
  139. me->setPosition( newPosition );
  140. }
  141. // ------------------------------------------------------------------------------------------------
  142. /** CRC */
  143. // ------------------------------------------------------------------------------------------------
  144. void ProjectileStreamUpdate::crc( Xfer *xfer )
  145. {
  146. // extend base class
  147. UpdateModule::crc( xfer );
  148. } // end crc
  149. // ------------------------------------------------------------------------------------------------
  150. /** Xfer method
  151. * Version Info:
  152. * 1: Initial version */
  153. // ------------------------------------------------------------------------------------------------
  154. void ProjectileStreamUpdate::xfer( Xfer *xfer )
  155. {
  156. // version
  157. XferVersion currentVersion = 1;
  158. XferVersion version = currentVersion;
  159. xfer->xferVersion( &version, currentVersion );
  160. // extend base class
  161. UpdateModule::xfer( xfer );
  162. // projectile ids
  163. xfer->xferUser( m_projectileIDs, sizeof( ObjectID ) * MAX_PROJECTILE_STREAM );
  164. // next free index
  165. xfer->xferInt( &m_nextFreeIndex );
  166. // first valid index
  167. xfer->xferInt( &m_firstValidIndex );
  168. // owning object
  169. xfer->xferObjectID( &m_owningObject );
  170. } // end xfer
  171. // ------------------------------------------------------------------------------------------------
  172. /** Load post process */
  173. // ------------------------------------------------------------------------------------------------
  174. void ProjectileStreamUpdate::loadPostProcess( void )
  175. {
  176. // extend base class
  177. UpdateModule::loadPostProcess();
  178. } // end loadPostProcess