FireSpreadUpdate.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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: FireSpreadUpdate.cpp /////////////////////////////////////////////////////////////////////////
  24. // Author: Graham Smallwood, April 2002
  25. // Desc: Update looks for ::Aflame and explicitly ignites someone nearby if set
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #include "Common/RandomValue.h"
  30. #include "Common/Xfer.h"
  31. #include "GameLogic/GameLogic.h"
  32. #include "GameLogic/Object.h"
  33. #include "GameLogic/ObjectCreationList.h"
  34. #include "GameLogic/PartitionManager.h"
  35. #include "GameLogic/Module/FireSpreadUpdate.h"
  36. #include "GameLogic/Module/FlammableUpdate.h"
  37. //-------------------------------------------------------------------------------------------------
  38. //-------------------------------------------------------------------------------------------------
  39. //-------------------------------------------------------------------------------------------------
  40. // This is a one sided query, as in I am not checking for "Flammable By Me", I'm simply testing for a property
  41. class PartitionFilterFlammable : public PartitionFilter
  42. {
  43. public:
  44. PartitionFilterFlammable(){ }
  45. virtual Bool allow(Object *objOther);
  46. #if defined(_DEBUG) || defined(_INTERNAL)
  47. virtual const char* debugGetName() { return "PartitionFilterFlammable"; }
  48. #endif
  49. };
  50. //-------------------------------------------------------------------------------------------------
  51. Bool PartitionFilterFlammable::allow(Object *objOther)
  52. {
  53. // It must be burnable in general, and burnable now
  54. static NameKeyType key_FlammableUpdate = NAMEKEY("FlammableUpdate");
  55. FlammableUpdate* fu = (FlammableUpdate*)objOther->findUpdateModule(key_FlammableUpdate);
  56. if (fu == NULL)
  57. return FALSE;
  58. if( ! fu->wouldIgnite() )
  59. return FALSE;
  60. return TRUE;
  61. }
  62. //-------------------------------------------------------------------------------------------------
  63. //-------------------------------------------------------------------------------------------------
  64. //-------------------------------------------------------------------------------------------------
  65. FireSpreadUpdateModuleData::FireSpreadUpdateModuleData()
  66. {
  67. m_minSpreadTryDelayData = 0;
  68. m_maxSpreadTryDelayData = 0;
  69. m_oclEmbers = NULL;
  70. m_spreadTryRange = 0;
  71. }
  72. //-------------------------------------------------------------------------------------------------
  73. /*static*/ void FireSpreadUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
  74. {
  75. UpdateModuleData::buildFieldParse(p);
  76. static const FieldParse dataFieldParse[] =
  77. {
  78. { "OCLEmbers", INI::parseObjectCreationList, NULL, offsetof( FireSpreadUpdateModuleData, m_oclEmbers ) },
  79. { "MinSpreadDelay", INI::parseDurationUnsignedInt, NULL, offsetof( FireSpreadUpdateModuleData, m_minSpreadTryDelayData ) },
  80. { "MaxSpreadDelay", INI::parseDurationUnsignedInt, NULL, offsetof( FireSpreadUpdateModuleData, m_maxSpreadTryDelayData ) },
  81. { "SpreadTryRange", INI::parseReal, NULL, offsetof( FireSpreadUpdateModuleData, m_spreadTryRange ) },
  82. { 0, 0, 0, 0 }
  83. };
  84. p.add(dataFieldParse);
  85. }
  86. //-------------------------------------------------------------------------------------------------
  87. //-------------------------------------------------------------------------------------------------
  88. FireSpreadUpdate::FireSpreadUpdate( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  89. {
  90. setWakeFrame(getObject(), UPDATE_SLEEP_FOREVER);
  91. }
  92. //-------------------------------------------------------------------------------------------------
  93. //-------------------------------------------------------------------------------------------------
  94. FireSpreadUpdate::~FireSpreadUpdate( void )
  95. {
  96. }
  97. //-------------------------------------------------------------------------------------------------
  98. //-------------------------------------------------------------------------------------------------
  99. UpdateSleepTime FireSpreadUpdate::update( void )
  100. {
  101. const FireSpreadUpdateModuleData* d = getFireSpreadUpdateModuleData();
  102. Object* me = getObject();
  103. if( (me->getStatusBits() & OBJECT_STATUS_AFLAME) == 0 )
  104. return UPDATE_SLEEP_FOREVER; // not on fire -- sleep forever
  105. {
  106. ObjectCreationList::create( d->m_oclEmbers, getObject(), NULL );
  107. if( d->m_spreadTryRange != 0 )
  108. {
  109. // This will spread fire explicitly
  110. PartitionFilterFlammable fFilter;
  111. PartitionFilter *filters[] = { &fFilter, NULL };
  112. // SimpleObjectIterator *iter = NULL;
  113. // iter = ThePartitionManager->iterateObjectsInRange(getObject(),
  114. // d->m_spreadTryRange,
  115. // FROM_CENTER_3D,
  116. // filters,
  117. // ITER_SORTED_NEAR_TO_FAR
  118. // );
  119. // MemoryPoolObjectHolder hold(iter);
  120. // Object *objectToLight = iter->first();
  121. //
  122. // srj sez: the above code is stupid and slow. since we only want the closest object,
  123. // just ask for that; the above has to find ALL objects in range, but we ignore all
  124. // but the first (closest).
  125. //
  126. Object* objectToLight = ThePartitionManager->getClosestObject(getObject(), d->m_spreadTryRange, FROM_CENTER_3D, filters);
  127. if( objectToLight )
  128. {
  129. static NameKeyType key_FlammableUpdate = NAMEKEY("FlammableUpdate");
  130. FlammableUpdate* fu = (FlammableUpdate*)objectToLight->findUpdateModule(key_FlammableUpdate);
  131. if( fu )
  132. fu->tryToIgnite();
  133. }
  134. }
  135. return UPDATE_SLEEP(calcNextSpreadDelay());
  136. }
  137. }
  138. //-------------------------------------------------------------------------------------------------
  139. //-------------------------------------------------------------------------------------------------
  140. void FireSpreadUpdate::startFireSpreading()
  141. {
  142. if ((getObject()->getStatusBits() & OBJECT_STATUS_AFLAME) == 0)
  143. return; // sorry, must be on fire
  144. setWakeFrame(getObject(), UPDATE_SLEEP(calcNextSpreadDelay()));
  145. }
  146. //-------------------------------------------------------------------------------------------------
  147. //-------------------------------------------------------------------------------------------------
  148. UnsignedInt FireSpreadUpdate::calcNextSpreadDelay()
  149. {
  150. const FireSpreadUpdateModuleData* d = getFireSpreadUpdateModuleData();
  151. UnsignedInt delay = GameLogicRandomValue( d->m_minSpreadTryDelayData, d->m_maxSpreadTryDelayData );
  152. if (delay < 1)
  153. delay = 1;
  154. return delay;
  155. }
  156. // ------------------------------------------------------------------------------------------------
  157. /** CRC */
  158. // ------------------------------------------------------------------------------------------------
  159. void FireSpreadUpdate::crc( Xfer *xfer )
  160. {
  161. // extend base class
  162. UpdateModule::crc( xfer );
  163. } // end crc
  164. // ------------------------------------------------------------------------------------------------
  165. /** Xfer method
  166. * Version Info:
  167. * 1: Initial version */
  168. // ------------------------------------------------------------------------------------------------
  169. void FireSpreadUpdate::xfer( Xfer *xfer )
  170. {
  171. // version
  172. XferVersion currentVersion = 1;
  173. XferVersion version = currentVersion;
  174. xfer->xferVersion( &version, currentVersion );
  175. // extend base class
  176. UpdateModule::xfer( xfer );
  177. } // end xfer
  178. // ------------------------------------------------------------------------------------------------
  179. /** Load post process */
  180. // ------------------------------------------------------------------------------------------------
  181. void FireSpreadUpdate::loadPostProcess( void )
  182. {
  183. // extend base class
  184. UpdateModule::loadPostProcess();
  185. } // end loadPostProcess