HordeUpdate.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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: HordeUpdate.cpp ////////////////////////////////////////////////////////////////////////
  24. // Author: Steven Johnson, Feb 2002
  25. // Desc: HordeUpdate module
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #define DEFINE_HORDEACTION_NAMES
  30. #include "Common/Player.h"
  31. #include "Common/ThingTemplate.h"
  32. #include "Common/Upgrade.h"
  33. #include "Common/Xfer.h"
  34. #include "Common/BitFlagsIO.h"
  35. #include "GameLogic/PartitionManager.h"
  36. #include "GameLogic/Weapon.h"
  37. #include "GameLogic/Module/AIUpdate.h"
  38. #include "GameLogic/Module/HordeUpdate.h"
  39. #include "GameClient/Drawable.h"
  40. //-------------------------------------------------------------------------------------------------
  41. //-------------------------------------------------------------------------------------------------
  42. //-------------------------------------------------------------------------------------------------
  43. #ifdef _INTERNAL
  44. // for occasional debugging...
  45. //#pragma optimize("", off)
  46. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  47. #endif
  48. static HordeUpdateInterface* getHUI(Object* obj)
  49. {
  50. for( BehaviorModule** u = obj->getBehaviorModules(); *u; ++u )
  51. {
  52. HordeUpdateInterface* hui = (*u)->getHordeUpdateInterface();
  53. if( hui )
  54. return hui;
  55. }
  56. return NULL;
  57. }
  58. //-------------------------------------------------------------------------------------------------
  59. //-------------------------------------------------------------------------------------------------
  60. //-------------------------------------------------------------------------------------------------
  61. class PartitionFilterHordeMember : public PartitionFilter
  62. {
  63. private:
  64. Object* m_obj;
  65. const HordeUpdateModuleData* m_data;
  66. public:
  67. PartitionFilterHordeMember(Object* obj, const HordeUpdateModuleData* data) : m_obj(obj), m_data(data) { }
  68. #if defined(_DEBUG) || defined(_INTERNAL)
  69. virtual const char* debugGetName() { return "PartitionFilterHordeMember"; }
  70. #endif
  71. virtual Bool allow(Object *objOther)
  72. {
  73. // must be exact same type as us (well, maybe)
  74. if (m_data->m_exactMatch && m_obj->getTemplate() != objOther->getTemplate())
  75. return false;
  76. // can only horde with other horde-able things
  77. HordeUpdateInterface *hui = getHUI(objOther);
  78. if( !hui )
  79. return false;
  80. // must match the kindof flags (if any)
  81. if (!objOther->isKindOfMulti(m_data->m_kindof, KINDOFMASK_NONE))
  82. return false;
  83. // must be our ally (well, maybe)
  84. if (m_data->m_alliesOnly && m_obj->getRelationship(objOther) != ALLIES)
  85. return false;
  86. // doh
  87. if (m_obj->isOffMap() != objOther->isOffMap())
  88. return false;
  89. // guess it's horde-worthy!
  90. return true;
  91. }
  92. };
  93. //-------------------------------------------------------------------------------------------------
  94. //-------------------------------------------------------------------------------------------------
  95. //-------------------------------------------------------------------------------------------------
  96. const Int DEFAULT_UPDATE_RATE = LOGICFRAMES_PER_SECOND;
  97. //-------------------------------------------------------------------------------------------------
  98. HordeUpdateModuleData::HordeUpdateModuleData() :
  99. m_updateRate(DEFAULT_UPDATE_RATE),
  100. m_minCount(0),
  101. m_minDist(0.0f),
  102. m_rubOffRadius(20.0f),
  103. m_alliesOnly(true),
  104. m_exactMatch(false),
  105. m_allowedNationalism(TRUE),
  106. m_action(HORDEACTION_HORDE)
  107. {
  108. }
  109. //-------------------------------------------------------------------------------------------------
  110. /*static*/ void HordeUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
  111. {
  112. ModuleData::buildFieldParse(p);
  113. static const FieldParse dataFieldParse[] =
  114. {
  115. { "UpdateRate", INI::parseDurationUnsignedInt, NULL, offsetof(HordeUpdateModuleData, m_updateRate) },
  116. { "KindOf", KindOfMaskType::parseFromINI, NULL, offsetof(HordeUpdateModuleData, m_kindof) },
  117. { "Count", INI::parseInt, NULL, offsetof(HordeUpdateModuleData, m_minCount) },
  118. { "Radius", INI::parseReal, NULL, offsetof(HordeUpdateModuleData, m_minDist) },
  119. { "RubOffRadius", INI::parseReal, NULL, offsetof(HordeUpdateModuleData, m_rubOffRadius) },
  120. { "AlliesOnly", INI::parseBool, NULL, offsetof(HordeUpdateModuleData, m_alliesOnly) },
  121. { "ExactMatch", INI::parseBool, NULL, offsetof(HordeUpdateModuleData, m_exactMatch) },
  122. { "Action", INI::parseIndexList, TheHordeActionTypeNames, offsetof(HordeUpdateModuleData, m_action) },
  123. { "FlagSubObjectNames", INI::parseAsciiStringVector, NULL, offsetof(HordeUpdateModuleData, m_flagSubObjNames) },
  124. { "AllowedNationalism", INI::parseBool, NULL, offsetof(HordeUpdateModuleData, m_allowedNationalism) },
  125. { 0, 0, 0, 0 }
  126. };
  127. p.add(dataFieldParse);
  128. }
  129. //-------------------------------------------------------------------------------------------------
  130. //-------------------------------------------------------------------------------------------------
  131. //-------------------------------------------------------------------------------------------------
  132. //-------------------------------------------------------------------------------------------------
  133. HordeUpdate::HordeUpdate( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  134. {
  135. m_inHorde = FALSE;
  136. m_hasFlag = FALSE;
  137. m_lastHordeRefreshFrame = TheGameLogic->getFrame();
  138. // Added By Sadullah Nader
  139. // Initializations missing and needed
  140. m_trueHordeMember = FALSE;
  141. //
  142. UnsignedInt delay = getHordeUpdateModuleData()->m_updateRate;
  143. setWakeFrame(getObject(), UPDATE_SLEEP(GameLogicRandomValue(1, delay)));
  144. }
  145. //-------------------------------------------------------------------------------------------------
  146. HordeUpdate::~HordeUpdate()
  147. {
  148. }
  149. //-------------------------------------------------------------------------------------------------
  150. Bool HordeUpdate::isAllowedNationalism() const
  151. {
  152. const HordeUpdateModuleData *data = getHordeUpdateModuleData();
  153. return data->m_allowedNationalism;
  154. }
  155. //-------------------------------------------------------------------------------------------------
  156. /** @todo I think we should model the horde list ... so we can do all this without doing
  157. * all this scanning, plus we can give exactly 1 flag to the right person in the
  158. * center of the horde which I think would look better (CBD)
  159. *
  160. * Redesign occurred 10/15, where the flags have been removed replaced by a teraindecal for each horde member
  161. * Thank You for reading, MLorenzen
  162. */
  163. // ------------------------------------------------------------------------------------------------
  164. void HordeUpdate::joinOrLeaveHorde(SimpleObjectIterator *iter, Bool join)
  165. {
  166. Bool prevInHorde = m_inHorde;
  167. m_inHorde = join;
  168. const HordeUpdateModuleData* d = getHordeUpdateModuleData();
  169. switch (d->m_action)
  170. {
  171. case HORDEACTION_HORDE:
  172. {
  173. // give/remove bonus effects
  174. if( prevInHorde != m_inHorde )
  175. {
  176. AIUpdateInterface *ai = getObject()->getAIUpdateInterface();
  177. if( ai )
  178. ai->evaluateMoraleBonus();
  179. else
  180. DEBUG_CRASH(( "HordeUpdate::joinOrLeaveHorde - We (%s) must have an AI to benefit from horde\n",
  181. getObject()->getTemplate()->getName().str() ));
  182. } // end if
  183. }
  184. break;
  185. }
  186. }
  187. //-------------------------------------------------------------------------------------------------
  188. void HordeUpdate::showHideFlag(Bool show)
  189. {
  190. const HordeUpdateModuleData* d = getHordeUpdateModuleData();
  191. if (!d->m_flagSubObjNames.empty())
  192. {
  193. Drawable* draw = getObject()->getDrawable();
  194. Object *obj = getObject();
  195. if (draw && obj)
  196. {
  197. for (std::vector<AsciiString>::const_iterator it = d->m_flagSubObjNames.begin(); it != d->m_flagSubObjNames.end(); ++it)
  198. draw->showSubObject(*it, show);
  199. draw->updateSubObjects();
  200. }
  201. }
  202. }
  203. //-------------------------------------------------------------------------------------------------
  204. /**
  205. this is called whenever a drawable is bound to the object.
  206. drawable is NOT guaranteed to be non-null.
  207. */
  208. void HordeUpdate::onDrawableBoundToObject()
  209. {
  210. /////////////showHideFlag(m_hasFlag);
  211. showHideFlag(FALSE);//hide the flag
  212. }
  213. //-------------------------------------------------------------------------------------------------
  214. UpdateSleepTime HordeUpdate::update( void )
  215. {
  216. //This code handles decals and ONLY decals!
  217. Object *obj = getObject();
  218. if ( ! obj )
  219. return UPDATE_SLEEP_FOREVER;
  220. const HordeUpdateModuleData *md = getHordeUpdateModuleData();
  221. Bool wasInHorde = m_inHorde;
  222. // This is a sticky situation, where refreshing the model state (like from default to damaged, for example)
  223. // will rebuild the terraindecal and set its size to the default size.... since Vehicles have a special size,
  224. // we want to keep it fresh, here, but not do the horde-ing test every frame...
  225. Bool isInfantry = ( obj->isKindOf(KINDOF_INFANTRY) );
  226. if ( isInfantry || (TheGameLogic->getFrame() > m_lastHordeRefreshFrame + md->m_updateRate) )
  227. {
  228. m_lastHordeRefreshFrame = TheGameLogic->getFrame();
  229. PartitionFilterHordeMember hmFilter(getObject(), md);
  230. PartitionFilter *filters[] = { &hmFilter, NULL };
  231. SimpleObjectIterator *iter = ThePartitionManager->iterateObjectsInRange(getObject(), md->m_minDist, FROM_BOUNDINGSPHERE_3D, filters);
  232. MemoryPoolObjectHolder hold(iter);
  233. if ((iter->getCount() >= md->m_minCount - 1) )//we really are in the thick part of the horde
  234. {
  235. m_inHorde = TRUE;
  236. m_trueHordeMember = TRUE;
  237. }
  238. else
  239. {
  240. m_inHorde = FALSE;
  241. m_trueHordeMember = FALSE;/// unless...
  242. Real rubOffRadiusSq = sqr(md->m_rubOffRadius);
  243. for (Object* other = iter->first(); other; other = iter->next())
  244. {
  245. HordeUpdateInterface* hui = getHUI(other);
  246. if ( hui != NULL && hui->isTrueHordeMember() )
  247. {
  248. Real dist = ThePartitionManager->getDistanceSquared(getObject(), other, FROM_CENTER_2D);
  249. if (dist <= rubOffRadiusSq )
  250. {
  251. m_inHorde = TRUE;
  252. break;
  253. }
  254. }
  255. }
  256. }
  257. AIUpdateInterface *ai = getObject()->getAIUpdateInterface();
  258. if( ai )
  259. ai->evaluateMoraleBonus();
  260. }
  261. // This is a sticky situation, where refreshing the model state (like from default to damaged, for example)
  262. // will rebuild the terraindecal and set its size to the default size.... since Vehicles have a special size,
  263. // we want to keep it fresh, here, but not do the horde-ing test every frame...
  264. // This is a weak solution, in that It causes this update to fight the defualt behavior of the modelstate methods,
  265. // But in the interest of not breaking the modelstate changing logic for five hundred other units a week before golden,
  266. // this is my solution... If anyone gets this note on the next project... please please please, fix the resetting of the
  267. // shadows/terraindecals in W3DModelDraw. THanks, ML
  268. Drawable* draw = getObject()->getDrawable();
  269. if ( draw && ! obj->isEffectivelyDead() )
  270. {
  271. if( TheGameLogic->getDrawIconUI() )
  272. {
  273. if(m_inHorde && !obj->isKindOf( KINDOF_PORTABLE_STRUCTURE ) )// this not is a ride-on for overlord
  274. {
  275. TerrainDecalType nuType;// uninitialized
  276. if ( isInfantry )
  277. {
  278. if( obj->testWeaponBonusCondition( WEAPONBONUSCONDITION_NATIONALISM ) == TRUE )
  279. {
  280. if ( obj->testWeaponBonusCondition( WEAPONBONUSCONDITION_FANATICISM ) == TRUE )
  281. nuType = ( TERRAIN_DECAL_HORDE_WITH_FANATICISM_UPGRADE );
  282. else
  283. nuType = ( TERRAIN_DECAL_HORDE_WITH_NATIONALISM_UPGRADE );
  284. }
  285. else
  286. nuType =(TERRAIN_DECAL_HORDE);
  287. if (nuType != draw->getTerrainDecalType())
  288. draw->setTerrainDecal(nuType);
  289. }
  290. else
  291. {
  292. Real size = 3.5f * obj->getGeometryInfo().getMajorRadius();
  293. draw->setTerrainDecalSize( size, size );
  294. if( obj->testWeaponBonusCondition( WEAPONBONUSCONDITION_NATIONALISM ) == TRUE )
  295. {
  296. if ( obj->testWeaponBonusCondition( WEAPONBONUSCONDITION_FANATICISM ) == TRUE )
  297. nuType = ( TERRAIN_DECAL_HORDE_WITH_FANATICISM_UPGRADE );
  298. else
  299. nuType = ( TERRAIN_DECAL_HORDE_WITH_NATIONALISM_UPGRADE );
  300. }
  301. else
  302. nuType = (TERRAIN_DECAL_HORDE_VEHICLE);
  303. if (nuType != draw->getTerrainDecalType())
  304. draw->setTerrainDecal(nuType);
  305. }
  306. }
  307. }
  308. else
  309. {
  310. //Scripts have disabled icons so don't draw these!
  311. draw->setTerrainDecal( TERRAIN_DECAL_NONE );
  312. }
  313. if ( ! wasInHorde && m_inHorde )
  314. draw->setTerrainDecalFadeTarget(1.0f, 0.03f);
  315. else if ( wasInHorde && ! m_inHorde )
  316. draw->setTerrainDecalFadeTarget(0.0f, -0.03f);
  317. }
  318. return isInfantry ? UPDATE_SLEEP(md->m_updateRate) : UPDATE_SLEEP_NONE;
  319. }
  320. // ------------------------------------------------------------------------------------------------
  321. /** CRC */
  322. // ------------------------------------------------------------------------------------------------
  323. void HordeUpdate::crc( Xfer *xfer )
  324. {
  325. // extend base class
  326. UpdateModule::crc( xfer );
  327. } // end crc
  328. // ------------------------------------------------------------------------------------------------
  329. /** Xfer method
  330. * Version Info:
  331. * 1: Initial version */
  332. // ------------------------------------------------------------------------------------------------
  333. void HordeUpdate::xfer( Xfer *xfer )
  334. {
  335. // version
  336. XferVersion currentVersion = 1;
  337. XferVersion version = currentVersion;
  338. xfer->xferVersion( &version, currentVersion );
  339. // extend base class
  340. UpdateModule::xfer( xfer );
  341. // in horder
  342. xfer->xferBool( &m_inHorde );
  343. // has flag
  344. xfer->xferBool( &m_hasFlag );
  345. } // end xfer
  346. // ------------------------------------------------------------------------------------------------
  347. /** Load post process */
  348. // ------------------------------------------------------------------------------------------------
  349. void HordeUpdate::loadPostProcess( void )
  350. {
  351. // extend base class
  352. UpdateModule::loadPostProcess();
  353. } // end loadPostProcess