GenerateMinefieldBehavior.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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: GenerateMinefieldBehavior.cpp ///////////////////////////////////////////////////////////////////////
  24. // Author:
  25. // Desc:
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #define DEFINE_SLOWDEATHPHASE_NAMES
  30. #include "Common/GlobalData.h"
  31. #include "Common/Thing.h"
  32. #include "Common/ThingFactory.h"
  33. #include "Common/ThingTemplate.h"
  34. #include "Common/INI.h"
  35. #include "Common/Player.h"
  36. #include "Common/RandomValue.h"
  37. #include "Common/Xfer.h"
  38. #include "GameClient/Drawable.h"
  39. #include "GameClient/FXList.h"
  40. #include "GameClient/InGameUI.h"
  41. #include "GameLogic/GameLogic.h"
  42. #include "GameLogic/Module/BodyModule.h"
  43. #include "GameLogic/Module/GenerateMinefieldBehavior.h"
  44. #include "GameLogic/Module/AIUpdate.h"
  45. #include "GameLogic/Object.h"
  46. #include "GameLogic/ObjectIter.h"
  47. #include "GameLogic/ObjectCreationList.h"
  48. #include "GameLogic/PartitionManager.h"
  49. #include "GameLogic/Weapon.h"
  50. #include "GameClient/Drawable.h"
  51. #ifdef _INTERNAL
  52. // for occasional debugging...
  53. //#pragma optimize("", off)
  54. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  55. #endif
  56. //-------------------------------------------------------------------------------------------------
  57. GenerateMinefieldBehaviorModuleData::GenerateMinefieldBehaviorModuleData()
  58. {
  59. m_mineName.clear();
  60. m_genFX = NULL;
  61. m_distanceAroundObject = TheGlobalData->m_standardMinefieldDistance;
  62. m_minesPerSquareFoot = TheGlobalData->m_standardMinefieldDensity;
  63. m_onDeath = false;
  64. m_borderOnly = true;
  65. m_alwaysCircular = false;
  66. m_smartBorder = false;
  67. m_smartBorderSkipInterior = true;
  68. m_randomJitter = 0.0f;
  69. m_skipIfThisMuchUnderStructure = 0.33f;
  70. }
  71. //-------------------------------------------------------------------------------------------------
  72. /*static*/ void GenerateMinefieldBehaviorModuleData::buildFieldParse(MultiIniFieldParse& p)
  73. {
  74. static const FieldParse dataFieldParse[] =
  75. {
  76. { "MineName", INI::parseAsciiString, NULL, offsetof( GenerateMinefieldBehaviorModuleData, m_mineName ) },
  77. { "GenerationFX", INI::parseFXList, NULL, offsetof( GenerateMinefieldBehaviorModuleData, m_genFX ) },
  78. { "DistanceAroundObject", INI::parseReal, NULL, offsetof( GenerateMinefieldBehaviorModuleData, m_distanceAroundObject ) },
  79. { "MinesPerSquareFoot", INI::parseReal, NULL, offsetof( GenerateMinefieldBehaviorModuleData, m_minesPerSquareFoot ) },
  80. { "GenerateOnlyOnDeath", INI::parseBool, NULL, offsetof(GenerateMinefieldBehaviorModuleData, m_onDeath) },
  81. { "BorderOnly", INI::parseBool, NULL, offsetof(GenerateMinefieldBehaviorModuleData, m_borderOnly) },
  82. { "SmartBorder", INI::parseBool, NULL, offsetof(GenerateMinefieldBehaviorModuleData, m_smartBorder) },
  83. { "SmartBorderSkipInterior", INI::parseBool, NULL, offsetof(GenerateMinefieldBehaviorModuleData, m_smartBorderSkipInterior) },
  84. { "AlwaysCircular", INI::parseBool, NULL, offsetof(GenerateMinefieldBehaviorModuleData, m_alwaysCircular) },
  85. { "RandomJitter", INI::parsePercentToReal, NULL, offsetof(GenerateMinefieldBehaviorModuleData, m_randomJitter) },
  86. { "SkipIfThisMuchUnderStructure", INI::parsePercentToReal, NULL, offsetof(GenerateMinefieldBehaviorModuleData, m_skipIfThisMuchUnderStructure) },
  87. { 0, 0, 0, 0 }
  88. };
  89. BehaviorModuleData::buildFieldParse(p);
  90. p.add(dataFieldParse);
  91. p.add(UpgradeMuxData::getFieldParse(), offsetof( GenerateMinefieldBehaviorModuleData, m_upgradeMuxData ));
  92. }
  93. //-------------------------------------------------------------------------------------------------
  94. //-------------------------------------------------------------------------------------------------
  95. GenerateMinefieldBehavior::GenerateMinefieldBehavior( Thing *thing, const ModuleData* moduleData ) : BehaviorModule( thing, moduleData )
  96. {
  97. m_target.zero();
  98. m_generated = false;
  99. m_hasTarget = false;
  100. }
  101. //-------------------------------------------------------------------------------------------------
  102. //-------------------------------------------------------------------------------------------------
  103. GenerateMinefieldBehavior::~GenerateMinefieldBehavior( void )
  104. {
  105. }
  106. //-------------------------------------------------------------------------------------------------
  107. //-------------------------------------------------------------------------------------------------
  108. void GenerateMinefieldBehavior::upgradeImplementation()
  109. {
  110. placeMines();
  111. }
  112. //-------------------------------------------------------------------------------------------------
  113. //-------------------------------------------------------------------------------------------------
  114. void GenerateMinefieldBehavior::onDie( const DamageInfo *damageInfo )
  115. {
  116. const GenerateMinefieldBehaviorModuleData* d = getGenerateMinefieldBehaviorModuleData();
  117. if (d->m_onDeath)
  118. {
  119. placeMines();
  120. }
  121. }
  122. //-------------------------------------------------------------------------------------------------
  123. //-------------------------------------------------------------------------------------------------
  124. void GenerateMinefieldBehavior::setMinefieldTarget(const Coord3D* pos)
  125. {
  126. if (pos)
  127. {
  128. m_hasTarget = true;
  129. m_target = *pos;
  130. }
  131. else
  132. {
  133. m_hasTarget = false;
  134. m_target.zero();
  135. }
  136. }
  137. //-------------------------------------------------------------------------------------------------
  138. //-------------------------------------------------------------------------------------------------
  139. const Coord3D* GenerateMinefieldBehavior::getMinefieldTarget() const
  140. {
  141. return m_hasTarget ? &m_target : getObject()->getPosition();
  142. }
  143. //-------------------------------------------------------------------------------------------------
  144. //-------------------------------------------------------------------------------------------------
  145. static Bool isAnythingTooClose2D(const std::vector<Object*>& v, const Coord3D& pos, Real minDistSqr)
  146. {
  147. for (std::vector<Object*>::const_iterator it = v.begin(); it != v.end(); ++it)
  148. {
  149. const Coord3D* p = (*it)->getPosition();
  150. Real distSqr = sqr(p->x - pos.x) + sqr(p->y - pos.y);
  151. if (distSqr < minDistSqr)
  152. return true;
  153. }
  154. return false;
  155. }
  156. //-------------------------------------------------------------------------------------------------
  157. //-------------------------------------------------------------------------------------------------
  158. static void offsetBySmallRandomAmount(Coord3D& pt, Real maxAmt)
  159. {
  160. pt.x += GameLogicRandomValueReal(-maxAmt, maxAmt);
  161. pt.y += GameLogicRandomValueReal(-maxAmt, maxAmt);
  162. }
  163. //-------------------------------------------------------------------------------------------------
  164. //-------------------------------------------------------------------------------------------------
  165. Object* GenerateMinefieldBehavior::placeMineAt(const Coord3D& pt, const ThingTemplate* mineTemplate, Team* team, const Object* producer)
  166. {
  167. Coord3D tmp = pt;
  168. tmp.z = 99999.0f;
  169. PathfindLayerEnum layer = TheTerrainLogic->getHighestLayerForDestination(&tmp);
  170. if (layer == LAYER_GROUND && TheTerrainLogic->isUnderwater(pt.x, pt.y))
  171. return NULL;
  172. if (layer == LAYER_GROUND && TheTerrainLogic->isCliffCell(pt.x, pt.y))
  173. return NULL;
  174. Real orient = GameLogicRandomValueReal(-PI, PI);
  175. // if the mine will be "mostly" under a structure, don't place it.
  176. // for now, "mostly" means "central third of radius would overlap"
  177. const GenerateMinefieldBehaviorModuleData* d = getGenerateMinefieldBehaviorModuleData();
  178. GeometryInfo geom = mineTemplate->getTemplateGeometryInfo();
  179. Real mineRadius = mineTemplate->getTemplateGeometryInfo().getBoundingCircleRadius();
  180. geom.expandFootprint(mineRadius * -(1.0f - d->m_skipIfThisMuchUnderStructure));
  181. ObjectIterator *iter = ThePartitionManager->iteratePotentialCollisions( &pt, geom, orient );
  182. MemoryPoolObjectHolder hold(iter);
  183. for (Object* them = iter->first(); them; them = iter->next())
  184. {
  185. if (them->isKindOf(KINDOF_STRUCTURE))
  186. return NULL;
  187. }
  188. Object* mine = TheThingFactory->newObject(mineTemplate, team);
  189. mine->setPosition(&pt);
  190. mine->setOrientation(orient);
  191. mine->setProducer(producer);
  192. for (BehaviorModule** bmi = mine->getBehaviorModules(); *bmi; ++bmi)
  193. {
  194. LandMineInterface* lmi = (*bmi)->getLandMineInterface();
  195. if (lmi)
  196. {
  197. lmi->setScootParms(*producer->getPosition(), pt);
  198. break;
  199. }
  200. }
  201. return mine;
  202. }
  203. //-------------------------------------------------------------------------------------------------
  204. //-------------------------------------------------------------------------------------------------
  205. void GenerateMinefieldBehavior::placeMinesAlongLine(const Coord3D& posStart, const Coord3D& posEnd, const ThingTemplate* mineTemplate, Bool skipOneAtStart)
  206. {
  207. const Object* obj = getObject();
  208. const GenerateMinefieldBehaviorModuleData* d = getGenerateMinefieldBehaviorModuleData();
  209. Team* team = obj->getControllingPlayer()->getDefaultTeam();
  210. Real dx = posEnd.x - posStart.x;
  211. Real dy = posEnd.y - posStart.y;
  212. Real len = sqrt(sqr(dx) + sqr(dy));
  213. Real mineRadius = mineTemplate->getTemplateGeometryInfo().getBoundingCircleRadius();
  214. Real mineDiameter = mineRadius * 2.0f;
  215. Real mineJitter = mineRadius*d->m_randomJitter;
  216. Int numMines = REAL_TO_INT_CEIL(len / mineDiameter);
  217. if (numMines < 1)
  218. numMines = 1;
  219. Real inc = len/numMines;
  220. for (Real place = skipOneAtStart ? inc : 0; place <= len; place += inc)
  221. {
  222. Coord3D pt;
  223. pt.x = posStart.x + place * dx / len;
  224. pt.y = posStart.y + place * dy / len;
  225. pt.z = TheTerrainLogic->getGroundHeight( pt.x, pt.y );
  226. offsetBySmallRandomAmount(pt, mineJitter);
  227. placeMineAt(pt, mineTemplate, team, obj);
  228. }
  229. }
  230. //-------------------------------------------------------------------------------------------------
  231. //-------------------------------------------------------------------------------------------------
  232. static void makeCorner(const Coord3D& pos, Real majorRadius, Real minorRadius, const Matrix3D& mtx, Coord3D& corner)
  233. {
  234. Vector3 tmp;
  235. tmp.X = majorRadius;
  236. tmp.Y = minorRadius;
  237. tmp.Z = 0;
  238. Matrix3D::Transform_Vector(mtx, tmp, &tmp);
  239. corner.x = tmp.X;
  240. corner.y = tmp.Y;
  241. corner.z = tmp.Z;
  242. }
  243. //-------------------------------------------------------------------------------------------------
  244. //-------------------------------------------------------------------------------------------------
  245. void GenerateMinefieldBehavior::placeMinesAroundRect(const Coord3D& pos, Real majorRadius, Real minorRadius, const ThingTemplate* mineTemplate)
  246. {
  247. const Object* obj = getObject();
  248. const Matrix3D* mtx = obj->getTransformMatrix();
  249. Coord3D pt[4];
  250. makeCorner(pos, majorRadius, minorRadius, *mtx, pt[0]);
  251. makeCorner(pos, -majorRadius, minorRadius, *mtx, pt[1]);
  252. makeCorner(pos, -majorRadius, -minorRadius, *mtx, pt[2]);
  253. makeCorner(pos, majorRadius, -minorRadius, *mtx, pt[3]);
  254. placeMinesAlongLine(pt[0], pt[1], mineTemplate, true);
  255. placeMinesAlongLine(pt[1], pt[2], mineTemplate, true);
  256. placeMinesAlongLine(pt[2], pt[3], mineTemplate, true);
  257. placeMinesAlongLine(pt[3], pt[0], mineTemplate, true);
  258. }
  259. //-------------------------------------------------------------------------------------------------
  260. //-------------------------------------------------------------------------------------------------
  261. void GenerateMinefieldBehavior::placeMinesAroundCircle(const Coord3D& pos, Real radius, const ThingTemplate* mineTemplate)
  262. {
  263. const Object* obj = getObject();
  264. const GenerateMinefieldBehaviorModuleData* d = getGenerateMinefieldBehaviorModuleData();
  265. Team* team = obj->getControllingPlayer()->getDefaultTeam();
  266. Real circum = 2.0f * PI * radius;
  267. Real mineRadius = mineTemplate->getTemplateGeometryInfo().getBoundingCircleRadius();
  268. Real mineDiameter = mineRadius * 2.0f;
  269. Real mineJitter = mineRadius*d->m_randomJitter;
  270. Int numMines = REAL_TO_INT_CEIL(circum / mineDiameter);
  271. if (numMines < 1)
  272. numMines = 1;
  273. Real angleInc = (2*PI)/numMines;
  274. Real angleLim = (2*PI) - angleInc*0.5f;
  275. for (Real angle = 0; angle < angleLim; angle += angleInc)
  276. {
  277. Coord3D pt;
  278. pt.x = pos.x + radius * Cos(angle);
  279. pt.y = pos.y + radius * Sin(angle);
  280. pt.z = TheTerrainLogic->getGroundHeight( pt.x, pt.y );
  281. offsetBySmallRandomAmount(pt, mineJitter);
  282. placeMineAt(pt, mineTemplate, team, obj);
  283. }
  284. }
  285. //-------------------------------------------------------------------------------------------------
  286. //-------------------------------------------------------------------------------------------------
  287. void GenerateMinefieldBehavior::placeMinesInFootprint(const GeometryInfo& geom, const ThingTemplate* mineTemplate)
  288. {
  289. const Object* obj = getObject();
  290. const GenerateMinefieldBehaviorModuleData* d = getGenerateMinefieldBehaviorModuleData();
  291. Team* team = obj->getControllingPlayer()->getDefaultTeam();
  292. Real area = geom.getFootprintArea();
  293. Int numMines = REAL_TO_INT_CEIL(d->m_minesPerSquareFoot * area);
  294. if (numMines < 1)
  295. numMines = 1;
  296. const Coord3D* target = getMinefieldTarget();
  297. std::vector<Object*> minesCreatedSoFar;
  298. Real minDistSqr = sqr(mineTemplate->getTemplateGeometryInfo().getBoundingCircleRadius() * 2.0f);
  299. for (int i = 0; i < numMines; ++i)
  300. {
  301. Coord3D pt;
  302. Int maxRetry = 100;
  303. do
  304. {
  305. geom.makeRandomOffsetWithinFootprint(pt);
  306. pt.x += target->x;
  307. pt.y += target->y;
  308. pt.z += target->z;
  309. --maxRetry;
  310. } while (isAnythingTooClose2D(minesCreatedSoFar, pt, minDistSqr) && maxRetry > 0);
  311. DEBUG_ASSERTCRASH(maxRetry>0,("ran out of retries %f",minDistSqr));
  312. if (getObject()->getGeometryInfo().isPointInFootprint(*target, pt))
  313. continue;
  314. Object* mine = placeMineAt(pt, mineTemplate, team, obj); // can return null.
  315. if (mine)
  316. minesCreatedSoFar.push_back(mine);
  317. }
  318. }
  319. //-------------------------------------------------------------------------------------------------
  320. //-------------------------------------------------------------------------------------------------
  321. void GenerateMinefieldBehavior::placeMines()
  322. {
  323. if (m_generated)
  324. return;
  325. m_generated = true;
  326. const Object* obj = getObject();
  327. const GenerateMinefieldBehaviorModuleData* d = getGenerateMinefieldBehaviorModuleData();
  328. const ThingTemplate* mineTemplate = TheThingFactory->findTemplate(d->m_mineName);
  329. if (!mineTemplate)
  330. {
  331. DEBUG_CRASH(("mine %s not found\n",d->m_mineName.str()));
  332. return;
  333. }
  334. const Coord3D* target = getMinefieldTarget();
  335. if (d->m_smartBorder)
  336. {
  337. GeometryInfo geom = obj->getGeometryInfo();
  338. if (!d->m_smartBorderSkipInterior)
  339. {
  340. geom = mineTemplate->getTemplateGeometryInfo();
  341. placeMineAt(*target, mineTemplate, obj->getControllingPlayer()->getDefaultTeam(), obj);
  342. }
  343. if (d->m_alwaysCircular)
  344. geom.set(GEOMETRY_CYLINDER, false, 1, geom.getBoundingCircleRadius(), geom.getBoundingCircleRadius());
  345. Real mineRadius = mineTemplate->getTemplateGeometryInfo().getBoundingCircleRadius();
  346. Real mineDiameter = mineRadius * 2.0f;
  347. geom.expandFootprint(mineRadius);
  348. do
  349. {
  350. if (geom.getGeomType() == GEOMETRY_BOX && !d->m_alwaysCircular)
  351. {
  352. placeMinesAroundRect(*target, geom.getMajorRadius(), geom.getMinorRadius(), mineTemplate);
  353. }
  354. else
  355. {
  356. placeMinesAroundCircle(*target, geom.getMajorRadius(), mineTemplate);
  357. }
  358. geom.expandFootprint(mineDiameter);
  359. } while (geom.getBoundingCircleRadius() < d->m_distanceAroundObject);
  360. }
  361. else if (d->m_borderOnly)
  362. {
  363. GeometryInfo geom = obj->getGeometryInfo();
  364. geom.expandFootprint(d->m_distanceAroundObject);
  365. if (geom.getGeomType() == GEOMETRY_BOX && !d->m_alwaysCircular)
  366. {
  367. placeMinesAroundRect(*target, geom.getMajorRadius(), geom.getMinorRadius(), mineTemplate);
  368. }
  369. else
  370. {
  371. placeMinesAroundCircle(*target, geom.getMajorRadius(), mineTemplate);
  372. }
  373. }
  374. else
  375. {
  376. GeometryInfo geom = obj->getGeometryInfo();
  377. geom.expandFootprint(d->m_distanceAroundObject);
  378. if (d->m_alwaysCircular)
  379. geom.set(GEOMETRY_CYLINDER, false, 1, geom.getBoundingCircleRadius(), geom.getBoundingCircleRadius());
  380. placeMinesInFootprint(geom, mineTemplate);
  381. }
  382. FXList::doFXObj(d->m_genFX, obj);
  383. }
  384. // ------------------------------------------------------------------------------------------------
  385. /** CRC */
  386. // ------------------------------------------------------------------------------------------------
  387. void GenerateMinefieldBehavior::crc( Xfer *xfer )
  388. {
  389. // extend base class
  390. BehaviorModule::crc( xfer );
  391. // extend base class
  392. UpgradeMux::upgradeMuxCRC( xfer );
  393. } // end crc
  394. // ------------------------------------------------------------------------------------------------
  395. /** Xfer method
  396. * Version Info:
  397. * 1: Initial version */
  398. // ------------------------------------------------------------------------------------------------
  399. void GenerateMinefieldBehavior::xfer( Xfer *xfer )
  400. {
  401. // version
  402. XferVersion currentVersion = 1;
  403. XferVersion version = currentVersion;
  404. xfer->xferVersion( &version, currentVersion );
  405. // base class
  406. BehaviorModule::xfer( xfer );
  407. // mux "base class"
  408. UpgradeMux::upgradeMuxXfer( xfer );
  409. // generated
  410. xfer->xferBool( &m_generated );
  411. xfer->xferBool( &m_hasTarget );
  412. xfer->xferCoord3D( &m_target );
  413. } // end xfer
  414. // ------------------------------------------------------------------------------------------------
  415. /** Load post process */
  416. // ------------------------------------------------------------------------------------------------
  417. void GenerateMinefieldBehavior::loadPostProcess( void )
  418. {
  419. // extend base class
  420. BehaviorModule::loadPostProcess();
  421. // extend base class
  422. UpgradeMux::upgradeMuxLoadPostProcess();
  423. } // end loadPostProcess