ProjectileStreamUpdate.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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: 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. m_targetObject = INVALID_ID;
  52. m_targetPosition.zero();
  53. }
  54. //-------------------------------------------------------------------------------------------------
  55. //-------------------------------------------------------------------------------------------------
  56. ProjectileStreamUpdate::~ProjectileStreamUpdate( void )
  57. {
  58. }
  59. //-------------------------------------------------------------------------------------------------
  60. /** The update callback. */
  61. //-------------------------------------------------------------------------------------------------
  62. UpdateSleepTime ProjectileStreamUpdate::update( void )
  63. {
  64. cullFrontOfList();
  65. // Update the draw module about our points (arrange array so they can read?)
  66. if( considerDying() )
  67. TheGameLogic->destroyObject( getObject() );
  68. return UPDATE_SLEEP_NONE;
  69. }
  70. void ProjectileStreamUpdate::addProjectile( ObjectID sourceID, ObjectID newID, ObjectID victimID, const Coord3D *victimPos )
  71. {
  72. DEBUG_ASSERTCRASH( m_owningObject == INVALID_ID || m_owningObject == sourceID, ("Two objects are trying to use the same Projectile stream.") );//Don't cross the streams!
  73. if( m_owningObject == INVALID_ID )
  74. m_owningObject = sourceID;
  75. // Keep track of target, and insert a hole if the target has changed
  76. if( victimID != INVALID_ID )
  77. {
  78. // Object shot
  79. if( victimID != m_targetObject )
  80. {
  81. // Changed targets, insert a hole to break the stream
  82. m_projectileIDs[ m_nextFreeIndex ] = INVALID_ID;
  83. m_nextFreeIndex = (m_nextFreeIndex + 1) % MAX_PROJECTILE_STREAM;
  84. // And mark this as our new target
  85. m_targetObject = victimID;
  86. }
  87. // Clear position so we know we are an object shot
  88. m_targetPosition.zero();
  89. }
  90. else if( victimPos != NULL )
  91. {
  92. if( ! (m_targetPosition == (*victimPos)) )
  93. {
  94. // New position, so insert hole
  95. m_projectileIDs[ m_nextFreeIndex ] = INVALID_ID;
  96. m_nextFreeIndex = (m_nextFreeIndex + 1) % MAX_PROJECTILE_STREAM;
  97. // And mark this as our new target
  98. m_targetPosition = (*victimPos);
  99. }
  100. // Clear object so we know we are a position shot
  101. m_targetObject = INVALID_ID;
  102. }
  103. else
  104. {
  105. DEBUG_CRASH(("A projectile stream was fired at neither an object nor a position. Probably bad."));
  106. }
  107. // Keep track of the id in a circular array
  108. m_projectileIDs[ m_nextFreeIndex ] = newID;
  109. m_nextFreeIndex = (m_nextFreeIndex + 1) % MAX_PROJECTILE_STREAM;
  110. DEBUG_ASSERTCRASH( m_nextFreeIndex != m_firstValidIndex, ("Need to increase the allowed number of simultaneous particles in ProjectileStreamUpdate.") );
  111. }
  112. void ProjectileStreamUpdate::cullFrontOfList()
  113. {
  114. while( (m_firstValidIndex != m_nextFreeIndex) && (TheGameLogic->findObjectByID( m_projectileIDs[m_firstValidIndex] ) == NULL) )
  115. {
  116. // 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
  117. m_firstValidIndex = (m_firstValidIndex + 1) % MAX_PROJECTILE_STREAM;
  118. }
  119. }
  120. Bool ProjectileStreamUpdate::considerDying()
  121. {
  122. if( m_firstValidIndex == m_nextFreeIndex && m_owningObject != INVALID_ID )
  123. {
  124. //If I have no projectiles to watch, and my master is dead, then yes, I want to die
  125. if( TheGameLogic->findObjectByID(m_owningObject) == NULL )
  126. return TRUE;
  127. }
  128. return FALSE;
  129. }
  130. void ProjectileStreamUpdate::getAllPoints( Vector3 *points, Int *count )
  131. {
  132. Int pointCount = 0;
  133. Int pointIndex = m_firstValidIndex;
  134. Object *obj = TheGameLogic->findObjectByID(m_owningObject);
  135. while( pointIndex != m_nextFreeIndex )
  136. {
  137. // Go through the array I think of as good. Holes in the middle get 0,0,0. I write
  138. // to pointCount because I am unrolling a circular array into the same sized flat one
  139. // since I am writing anyway.
  140. Object *projectile = TheGameLogic->findObjectByID( m_projectileIDs[pointIndex] );
  141. if( projectile )
  142. {
  143. Coord3D thisPoint = *projectile->getPosition();
  144. points[pointCount].X = thisPoint.x;
  145. points[pointCount].Y = thisPoint.y;
  146. points[pointCount].Z = thisPoint.z;
  147. if ( obj && obj->isKindOf( KINDOF_VEHICLE ) ) // this makes the stream skim along my roof, if I have a roof
  148. {
  149. const Coord3D *pos = obj->getPosition();
  150. Real myTop = obj->getGeometryInfo().getMaxHeightAbovePosition() + pos->z + 0.5f;
  151. Coord3D delta;
  152. delta.x = pos->x - points[pointCount].X;
  153. delta.y = pos->y - points[pointCount].Y;
  154. delta.z = 0.0f;
  155. if( delta.length() <= obj->getGeometryInfo().getMajorRadius() * 1.5f )
  156. points[pointCount].Z = MAX( points[pointCount].Z, myTop );
  157. }
  158. }
  159. else
  160. {
  161. points[pointCount].X = 0;
  162. points[pointCount].Y = 0;
  163. points[pointCount].Z = 0;
  164. }
  165. pointIndex = (pointIndex + 1) % MAX_PROJECTILE_STREAM;
  166. pointCount++;
  167. }
  168. *count = pointCount;
  169. }
  170. void ProjectileStreamUpdate::setPosition( const Coord3D *newPosition )
  171. {
  172. Object *me = getObject();
  173. me->setPosition( newPosition );
  174. }
  175. // ------------------------------------------------------------------------------------------------
  176. /** CRC */
  177. // ------------------------------------------------------------------------------------------------
  178. void ProjectileStreamUpdate::crc( Xfer *xfer )
  179. {
  180. // extend base class
  181. UpdateModule::crc( xfer );
  182. } // end crc
  183. // ------------------------------------------------------------------------------------------------
  184. /** Xfer method
  185. * Version Info:
  186. * 1: Initial version
  187. * 2: Target tracking for line breaking
  188. */
  189. // ------------------------------------------------------------------------------------------------
  190. void ProjectileStreamUpdate::xfer( Xfer *xfer )
  191. {
  192. // version
  193. XferVersion currentVersion = 2;
  194. XferVersion version = currentVersion;
  195. xfer->xferVersion( &version, currentVersion );
  196. // extend base class
  197. UpdateModule::xfer( xfer );
  198. // projectile ids
  199. xfer->xferUser( m_projectileIDs, sizeof( ObjectID ) * MAX_PROJECTILE_STREAM );
  200. // next free index
  201. xfer->xferInt( &m_nextFreeIndex );
  202. // first valid index
  203. xfer->xferInt( &m_firstValidIndex );
  204. // owning object
  205. xfer->xferObjectID( &m_owningObject );
  206. if( version >= 2 )
  207. {
  208. xfer->xferObjectID( &m_targetObject );
  209. xfer->xferCoord3D( &m_targetPosition );
  210. }
  211. } // end xfer
  212. // ------------------------------------------------------------------------------------------------
  213. /** Load post process */
  214. // ------------------------------------------------------------------------------------------------
  215. void ProjectileStreamUpdate::loadPostProcess( void )
  216. {
  217. // extend base class
  218. UpdateModule::loadPostProcess();
  219. } // end loadPostProcess