HordeUpdate.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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: 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_action(HORDEACTION_HORDE)
  106. {
  107. }
  108. //-------------------------------------------------------------------------------------------------
  109. /*static*/ void HordeUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
  110. {
  111. ModuleData::buildFieldParse(p);
  112. static const FieldParse dataFieldParse[] =
  113. {
  114. { "UpdateRate", INI::parseDurationUnsignedInt, NULL, offsetof(HordeUpdateModuleData, m_updateRate) },
  115. { "KindOf", KindOfMaskType::parseFromINI, NULL, offsetof(HordeUpdateModuleData, m_kindof) },
  116. { "Count", INI::parseInt, NULL, offsetof(HordeUpdateModuleData, m_minCount) },
  117. { "Radius", INI::parseReal, NULL, offsetof(HordeUpdateModuleData, m_minDist) },
  118. { "RubOffRadius", INI::parseReal, NULL, offsetof(HordeUpdateModuleData, m_rubOffRadius) },
  119. { "AlliesOnly", INI::parseBool, NULL, offsetof(HordeUpdateModuleData, m_alliesOnly) },
  120. { "ExactMatch", INI::parseBool, NULL, offsetof(HordeUpdateModuleData, m_exactMatch) },
  121. { "Action", INI::parseIndexList, TheHordeActionTypeNames, offsetof(HordeUpdateModuleData, m_action) },
  122. { "FlagSubObjectNames", INI::parseAsciiStringVector, NULL, offsetof(HordeUpdateModuleData, m_flagSubObjNames) },
  123. { 0, 0, 0, 0 }
  124. };
  125. p.add(dataFieldParse);
  126. }
  127. //-------------------------------------------------------------------------------------------------
  128. //-------------------------------------------------------------------------------------------------
  129. //-------------------------------------------------------------------------------------------------
  130. //-------------------------------------------------------------------------------------------------
  131. HordeUpdate::HordeUpdate( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  132. {
  133. m_inHorde = FALSE;
  134. m_hasFlag = FALSE;
  135. m_lastHordeRefreshFrame = TheGameLogic->getFrame();
  136. // Added By Sadullah Nader
  137. // Initializations missing and needed
  138. m_trueHordeMember = FALSE;
  139. //
  140. UnsignedInt delay = getHordeUpdateModuleData()->m_updateRate;
  141. setWakeFrame(getObject(), UPDATE_SLEEP(GameLogicRandomValue(1, delay)));
  142. }
  143. //-------------------------------------------------------------------------------------------------
  144. HordeUpdate::~HordeUpdate()
  145. {
  146. }
  147. //-------------------------------------------------------------------------------------------------
  148. /** @todo I think we should model the horde list ... so we can do all this without doing
  149. * all this scanning, plus we can give exactly 1 flag to the right person in the
  150. * center of the horde which I think would look better (CBD)
  151. *
  152. * Redesign occurred 10/15, where the flags have been removed replaced by a teraindecal for each horde member
  153. * Thank You for reading, MLorenzen
  154. */
  155. // ------------------------------------------------------------------------------------------------
  156. void HordeUpdate::joinOrLeaveHorde(SimpleObjectIterator *iter, Bool join)
  157. {
  158. Bool prevInHorde = m_inHorde;
  159. m_inHorde = join;
  160. const HordeUpdateModuleData* d = getHordeUpdateModuleData();
  161. switch (d->m_action)
  162. {
  163. case HORDEACTION_HORDE:
  164. {
  165. // give/remove bonus effects
  166. if( prevInHorde != m_inHorde )
  167. {
  168. AIUpdateInterface *ai = getObject()->getAIUpdateInterface();
  169. if( ai )
  170. ai->evaluateMoraleBonus();
  171. else
  172. DEBUG_CRASH(( "HordeUpdate::joinOrLeaveHorde - We (%s) must have an AI to benefit from horde\n",
  173. getObject()->getTemplate()->getName().str() ));
  174. } // end if
  175. }
  176. break;
  177. }
  178. }
  179. //-------------------------------------------------------------------------------------------------
  180. void HordeUpdate::showHideFlag(Bool show)
  181. {
  182. const HordeUpdateModuleData* d = getHordeUpdateModuleData();
  183. if (!d->m_flagSubObjNames.empty())
  184. {
  185. Drawable* draw = getObject()->getDrawable();
  186. Object *obj = getObject();
  187. if (draw && obj)
  188. {
  189. for (std::vector<AsciiString>::const_iterator it = d->m_flagSubObjNames.begin(); it != d->m_flagSubObjNames.end(); ++it)
  190. draw->showSubObject(*it, show);
  191. draw->updateSubObjects();
  192. }
  193. }
  194. }
  195. //-------------------------------------------------------------------------------------------------
  196. /**
  197. this is called whenever a drawable is bound to the object.
  198. drawable is NOT guaranteed to be non-null.
  199. */
  200. void HordeUpdate::onDrawableBoundToObject()
  201. {
  202. /////////////showHideFlag(m_hasFlag);
  203. showHideFlag(FALSE);//hide the flag
  204. }
  205. //-------------------------------------------------------------------------------------------------
  206. UpdateSleepTime HordeUpdate::update( void )
  207. {
  208. //This code handles decals and ONLY decals!
  209. Object *obj = getObject();
  210. if ( ! obj )
  211. return UPDATE_SLEEP_FOREVER;
  212. const HordeUpdateModuleData *md = getHordeUpdateModuleData();
  213. Bool wasInHorde = m_inHorde;
  214. // This is a sticky situation, where refreshing the model state (like from default to damaged, for example)
  215. // will rebuild the terraindecal and set its size to the default size.... since Vehicles have a special size,
  216. // we want to keep it fresh, here, but not do the horde-ing test every frame...
  217. Bool isInfantry = ( obj->isKindOf(KINDOF_INFANTRY) );
  218. if ( isInfantry || (TheGameLogic->getFrame() > m_lastHordeRefreshFrame + md->m_updateRate) )
  219. {
  220. m_lastHordeRefreshFrame = TheGameLogic->getFrame();
  221. PartitionFilterHordeMember hmFilter(getObject(), md);
  222. PartitionFilter *filters[] = { &hmFilter, NULL };
  223. SimpleObjectIterator *iter = ThePartitionManager->iterateObjectsInRange(getObject(), md->m_minDist, FROM_BOUNDINGSPHERE_3D, filters);
  224. MemoryPoolObjectHolder hold(iter);
  225. if ((iter->getCount() >= md->m_minCount - 1) )//we really are in the thick part of the horde
  226. {
  227. m_inHorde = TRUE;
  228. m_trueHordeMember = TRUE;
  229. }
  230. else
  231. {
  232. m_inHorde = FALSE;
  233. m_trueHordeMember = FALSE;/// unless...
  234. Real rubOffRadiusSq = sqr(md->m_rubOffRadius);
  235. for (Object* other = iter->first(); other; other = iter->next())
  236. {
  237. HordeUpdateInterface* hui = getHUI(other);
  238. if ( hui != NULL && hui->isTrueHordeMember() )
  239. {
  240. Real dist = ThePartitionManager->getDistanceSquared(getObject(), other, FROM_CENTER_2D);
  241. if (dist <= rubOffRadiusSq )
  242. {
  243. m_inHorde = TRUE;
  244. break;
  245. }
  246. }
  247. }
  248. }
  249. AIUpdateInterface *ai = getObject()->getAIUpdateInterface();
  250. if( ai )
  251. ai->evaluateMoraleBonus();
  252. }
  253. // This is a sticky situation, where refreshing the model state (like from default to damaged, for example)
  254. // will rebuild the terraindecal and set its size to the default size.... since Vehicles have a special size,
  255. // we want to keep it fresh, here, but not do the horde-ing test every frame...
  256. // This is a weak solution, in that It causes this update to fight the defualt behavior of the modelstate methods,
  257. // But in the interest of not breaking the modelstate changing logic for five hundred other units a week before golden,
  258. // this is my solution... If anyone gets this note on the next project... please please please, fix the resetting of the
  259. // shadows/terraindecals in W3DModelDraw. THanks, ML
  260. Drawable* draw = getObject()->getDrawable();
  261. if ( draw && ! obj->isEffectivelyDead() )
  262. {
  263. if( TheGameLogic->getDrawIconUI() )
  264. {
  265. if(m_inHorde && !obj->isKindOf( KINDOF_PORTABLE_STRUCTURE ) )// this not is a ride-on for overlord
  266. {
  267. TerrainDecalType nuType;// uninitialized
  268. if ( isInfantry )
  269. {
  270. if( obj->testWeaponBonusCondition( WEAPONBONUSCONDITION_NATIONALISM ) == TRUE )
  271. nuType = (TERRAIN_DECAL_HORDE_WITH_NATIONALISM_UPGRADE);
  272. else
  273. nuType =(TERRAIN_DECAL_HORDE);
  274. if (nuType != draw->getTerrainDecalType())
  275. draw->setTerrainDecal(nuType);
  276. }
  277. else
  278. {
  279. Real size = 3.5f * obj->getGeometryInfo().getMajorRadius();
  280. draw->setTerrainDecalSize( size, size );
  281. if( obj->testWeaponBonusCondition( WEAPONBONUSCONDITION_NATIONALISM ) == TRUE )
  282. nuType = (TERRAIN_DECAL_HORDE_WITH_NATIONALISM_UPGRADE_VEHICLE);
  283. else
  284. nuType = (TERRAIN_DECAL_HORDE_VEHICLE);
  285. if (nuType != draw->getTerrainDecalType())
  286. draw->setTerrainDecal(nuType);
  287. }
  288. }
  289. }
  290. else
  291. {
  292. //Scripts have disabled icons so don't draw these!
  293. draw->setTerrainDecal( TERRAIN_DECAL_NONE );
  294. }
  295. if ( ! wasInHorde && m_inHorde )
  296. draw->setTerrainDecalFadeTarget(1.0f, 0.03f);
  297. else if ( wasInHorde && ! m_inHorde )
  298. draw->setTerrainDecalFadeTarget(0.0f, -0.03f);
  299. }
  300. return isInfantry ? UPDATE_SLEEP(md->m_updateRate) : UPDATE_SLEEP_NONE;
  301. }
  302. // ------------------------------------------------------------------------------------------------
  303. /** CRC */
  304. // ------------------------------------------------------------------------------------------------
  305. void HordeUpdate::crc( Xfer *xfer )
  306. {
  307. // extend base class
  308. UpdateModule::crc( xfer );
  309. } // end crc
  310. // ------------------------------------------------------------------------------------------------
  311. /** Xfer method
  312. * Version Info:
  313. * 1: Initial version */
  314. // ------------------------------------------------------------------------------------------------
  315. void HordeUpdate::xfer( Xfer *xfer )
  316. {
  317. // version
  318. XferVersion currentVersion = 1;
  319. XferVersion version = currentVersion;
  320. xfer->xferVersion( &version, currentVersion );
  321. // extend base class
  322. UpdateModule::xfer( xfer );
  323. // in horder
  324. xfer->xferBool( &m_inHorde );
  325. // has flag
  326. xfer->xferBool( &m_hasFlag );
  327. } // end xfer
  328. // ------------------------------------------------------------------------------------------------
  329. /** Load post process */
  330. // ------------------------------------------------------------------------------------------------
  331. void HordeUpdate::loadPostProcess( void )
  332. {
  333. // extend base class
  334. UpdateModule::loadPostProcess();
  335. } // end loadPostProcess