CRmgTemplateZone.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  1. /*
  2. * CRmgTemplateZone.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CRmgTemplateZone.h"
  12. #include "../mapping/CMapEditManager.h"
  13. #include "../mapping/CMap.h"
  14. #include "../VCMI_Lib.h"
  15. #include "../CTownHandler.h"
  16. #include "../CCreatureHandler.h"
  17. #include "../CSpellHandler.h" //for choosing random spells
  18. #include "../mapObjects/CommonConstructors.h"
  19. #include "../mapObjects/MapObjects.h" //needed to resolve templates for CommonConstructors.h
  20. #include "../mapObjects/CGPandoraBox.h"
  21. #include "../mapObjects/CRewardableObject.h"
  22. class CMap;
  23. class CMapEditManager;
  24. //class CGObjectInstance;
  25. CRmgTemplateZone::CTownInfo::CTownInfo() : townCount(0), castleCount(0), townDensity(0), castleDensity(0)
  26. {
  27. }
  28. int CRmgTemplateZone::CTownInfo::getTownCount() const
  29. {
  30. return townCount;
  31. }
  32. void CRmgTemplateZone::CTownInfo::setTownCount(int value)
  33. {
  34. if(value < 0)
  35. throw rmgException("Negative value for town count not allowed.");
  36. townCount = value;
  37. }
  38. int CRmgTemplateZone::CTownInfo::getCastleCount() const
  39. {
  40. return castleCount;
  41. }
  42. void CRmgTemplateZone::CTownInfo::setCastleCount(int value)
  43. {
  44. if(value < 0)
  45. throw rmgException("Negative value for castle count not allowed.");
  46. castleCount = value;
  47. }
  48. int CRmgTemplateZone::CTownInfo::getTownDensity() const
  49. {
  50. return townDensity;
  51. }
  52. void CRmgTemplateZone::CTownInfo::setTownDensity(int value)
  53. {
  54. if(value < 0)
  55. throw rmgException("Negative value for town density not allowed.");
  56. townDensity = value;
  57. }
  58. int CRmgTemplateZone::CTownInfo::getCastleDensity() const
  59. {
  60. return castleDensity;
  61. }
  62. void CRmgTemplateZone::CTownInfo::setCastleDensity(int value)
  63. {
  64. if(value < 0)
  65. throw rmgException("Negative value for castle density not allowed.");
  66. castleDensity = value;
  67. }
  68. CTileInfo::CTileInfo():nearestObjectDistance(INT_MAX), terrain(ETerrainType::WRONG)
  69. {
  70. occupied = ETileType::POSSIBLE; //all tiles are initially possible to place objects or passages
  71. }
  72. int CTileInfo::getNearestObjectDistance() const
  73. {
  74. return nearestObjectDistance;
  75. }
  76. void CTileInfo::setNearestObjectDistance(int value)
  77. {
  78. nearestObjectDistance = std::max(0, value); //never negative (or unitialized)
  79. }
  80. bool CTileInfo::shouldBeBlocked() const
  81. {
  82. return occupied == ETileType::BLOCKED;
  83. }
  84. bool CTileInfo::isBlocked() const
  85. {
  86. return occupied == ETileType::BLOCKED || occupied == ETileType::USED;
  87. }
  88. bool CTileInfo::isPossible() const
  89. {
  90. return occupied == ETileType::POSSIBLE;
  91. }
  92. bool CTileInfo::isFree() const
  93. {
  94. return occupied == ETileType::FREE;
  95. }
  96. bool CTileInfo::isUsed() const
  97. {
  98. return occupied == ETileType::USED;
  99. }
  100. void CTileInfo::setOccupied(ETileType::ETileType value)
  101. {
  102. occupied = value;
  103. }
  104. ETerrainType CTileInfo::getTerrainType() const
  105. {
  106. return terrain;
  107. }
  108. void CTileInfo::setTerrainType(ETerrainType value)
  109. {
  110. terrain = value;
  111. }
  112. CRmgTemplateZone::CRmgTemplateZone() :
  113. id(0),
  114. type(ETemplateZoneType::PLAYER_START),
  115. size(1),
  116. townsAreSameType(false),
  117. matchTerrainToTown(true),
  118. townType(0),
  119. terrainType (ETerrainType::GRASS),
  120. zoneMonsterStrength(EMonsterStrength::ZONE_NORMAL),
  121. totalDensity(0)
  122. {
  123. townTypes = getDefaultTownTypes();
  124. terrainTypes = getDefaultTerrainTypes();
  125. }
  126. TRmgTemplateZoneId CRmgTemplateZone::getId() const
  127. {
  128. return id;
  129. }
  130. void CRmgTemplateZone::setId(TRmgTemplateZoneId value)
  131. {
  132. if(value <= 0)
  133. throw rmgException(boost::to_string(boost::format("Zone %d id should be greater than 0.") %id));
  134. id = value;
  135. }
  136. ETemplateZoneType::ETemplateZoneType CRmgTemplateZone::getType() const
  137. {
  138. return type;
  139. }
  140. void CRmgTemplateZone::setType(ETemplateZoneType::ETemplateZoneType value)
  141. {
  142. type = value;
  143. }
  144. int CRmgTemplateZone::getSize() const
  145. {
  146. return size;
  147. }
  148. void CRmgTemplateZone::setSize(int value)
  149. {
  150. if(value <= 0)
  151. throw rmgException(boost::to_string(boost::format("Zone %d size needs to be greater than 0.") % id));
  152. size = value;
  153. }
  154. boost::optional<int> CRmgTemplateZone::getOwner() const
  155. {
  156. return owner;
  157. }
  158. void CRmgTemplateZone::setOwner(boost::optional<int> value)
  159. {
  160. if(!(*value >= 0 && *value <= PlayerColor::PLAYER_LIMIT_I))
  161. throw rmgException(boost::to_string(boost::format ("Owner of zone %d has to be in range 0 to max player count.") %id));
  162. owner = value;
  163. }
  164. const CRmgTemplateZone::CTownInfo & CRmgTemplateZone::getPlayerTowns() const
  165. {
  166. return playerTowns;
  167. }
  168. void CRmgTemplateZone::setPlayerTowns(const CTownInfo & value)
  169. {
  170. playerTowns = value;
  171. }
  172. const CRmgTemplateZone::CTownInfo & CRmgTemplateZone::getNeutralTowns() const
  173. {
  174. return neutralTowns;
  175. }
  176. void CRmgTemplateZone::setNeutralTowns(const CTownInfo & value)
  177. {
  178. neutralTowns = value;
  179. }
  180. bool CRmgTemplateZone::getTownsAreSameType() const
  181. {
  182. return townsAreSameType;
  183. }
  184. void CRmgTemplateZone::setTownsAreSameType(bool value)
  185. {
  186. townsAreSameType = value;
  187. }
  188. const std::set<TFaction> & CRmgTemplateZone::getTownTypes() const
  189. {
  190. return townTypes;
  191. }
  192. void CRmgTemplateZone::setTownTypes(const std::set<TFaction> & value)
  193. {
  194. townTypes = value;
  195. }
  196. std::set<TFaction> CRmgTemplateZone::getDefaultTownTypes() const
  197. {
  198. std::set<TFaction> defaultTowns;
  199. auto towns = VLC->townh->getDefaultAllowed();
  200. for(int i = 0; i < towns.size(); ++i)
  201. {
  202. if(towns[i]) defaultTowns.insert(i);
  203. }
  204. return defaultTowns;
  205. }
  206. bool CRmgTemplateZone::getMatchTerrainToTown() const
  207. {
  208. return matchTerrainToTown;
  209. }
  210. void CRmgTemplateZone::setMatchTerrainToTown(bool value)
  211. {
  212. matchTerrainToTown = value;
  213. }
  214. const std::set<ETerrainType> & CRmgTemplateZone::getTerrainTypes() const
  215. {
  216. return terrainTypes;
  217. }
  218. void CRmgTemplateZone::setTerrainTypes(const std::set<ETerrainType> & value)
  219. {
  220. assert(value.find(ETerrainType::WRONG) == value.end() && value.find(ETerrainType::BORDER) == value.end() &&
  221. value.find(ETerrainType::WATER) == value.end() && value.find(ETerrainType::ROCK) == value.end());
  222. terrainTypes = value;
  223. }
  224. std::set<ETerrainType> CRmgTemplateZone::getDefaultTerrainTypes() const
  225. {
  226. std::set<ETerrainType> terTypes;
  227. static const ETerrainType::EETerrainType allowedTerTypes[] = {ETerrainType::DIRT, ETerrainType::SAND, ETerrainType::GRASS, ETerrainType::SNOW,
  228. ETerrainType::SWAMP, ETerrainType::ROUGH, ETerrainType::SUBTERRANEAN, ETerrainType::LAVA};
  229. for (auto & allowedTerType : allowedTerTypes)
  230. terTypes.insert(allowedTerType);
  231. return terTypes;
  232. }
  233. void CRmgTemplateZone::setMinesAmount (TResource res, ui16 amount)
  234. {
  235. mines[res] = amount;
  236. }
  237. std::map<TResource, ui16> CRmgTemplateZone::getMinesInfo() const
  238. {
  239. return mines;
  240. }
  241. void CRmgTemplateZone::addConnection(TRmgTemplateZoneId otherZone)
  242. {
  243. connections.push_back (otherZone);
  244. }
  245. std::vector<TRmgTemplateZoneId> CRmgTemplateZone::getConnections() const
  246. {
  247. return connections;
  248. }
  249. void CRmgTemplateZone::setMonsterStrength (EMonsterStrength::EMonsterStrength val)
  250. {
  251. assert (vstd::iswithin(val, EMonsterStrength::ZONE_WEAK, EMonsterStrength::ZONE_STRONG));
  252. zoneMonsterStrength = val;
  253. }
  254. void CRmgTemplateZone::setTotalDensity (ui16 val)
  255. {
  256. totalDensity = val;
  257. }
  258. ui16 CRmgTemplateZone::getTotalDensity () const
  259. {
  260. return totalDensity;
  261. }
  262. void CRmgTemplateZone::addTreasureInfo(CTreasureInfo & info)
  263. {
  264. treasureInfo.push_back(info);
  265. }
  266. std::vector<CTreasureInfo> CRmgTemplateZone::getTreasureInfo()
  267. {
  268. return treasureInfo;
  269. }
  270. std::set<int3>* CRmgTemplateZone::getFreePaths()
  271. {
  272. return &freePaths;
  273. }
  274. float3 CRmgTemplateZone::getCenter() const
  275. {
  276. return center;
  277. }
  278. void CRmgTemplateZone::setCenter(const float3 &f)
  279. {
  280. //limit boundaries to (0,1) square
  281. center = float3 (std::min(std::max(f.x, 0.f), 1.f), std::min(std::max(f.y, 0.f), 1.f), f.z);
  282. }
  283. bool CRmgTemplateZone::pointIsIn(int x, int y)
  284. {
  285. return true;
  286. }
  287. int3 CRmgTemplateZone::getPos() const
  288. {
  289. return pos;
  290. }
  291. void CRmgTemplateZone::setPos(const int3 &Pos)
  292. {
  293. pos = Pos;
  294. }
  295. void CRmgTemplateZone::addTile (const int3 &pos)
  296. {
  297. tileinfo.insert(pos);
  298. }
  299. std::set<int3> CRmgTemplateZone::getTileInfo () const
  300. {
  301. return tileinfo;
  302. }
  303. void CRmgTemplateZone::discardDistantTiles (CMapGenerator* gen, float distance)
  304. {
  305. //TODO: mark tiles beyond zone as unavailable, but allow to connect with adjacent zones
  306. //for (auto tile : tileinfo)
  307. //{
  308. // if (tile.dist2d(this->pos) > distance)
  309. // {
  310. // gen->setOccupied(tile, ETileType::USED);
  311. // //gen->setOccupied(tile, ETileType::BLOCKED); //fixme: crash at rendering?
  312. // }
  313. //}
  314. vstd::erase_if (tileinfo, [distance, this](const int3 &tile) -> bool
  315. {
  316. return tile.dist2d(this->pos) > distance;
  317. });
  318. }
  319. void CRmgTemplateZone::createBorder(CMapGenerator* gen)
  320. {
  321. for (auto tile : tileinfo)
  322. {
  323. gen->foreach_neighbour (tile, [this, gen](int3 &pos)
  324. {
  325. if (!vstd::contains(this->tileinfo, pos))
  326. {
  327. gen->foreach_neighbour (pos, [this, gen](int3 &pos)
  328. {
  329. if (gen->isPossible(pos))
  330. gen->setOccupied (pos, ETileType::BLOCKED);
  331. });
  332. }
  333. });
  334. }
  335. }
  336. void CRmgTemplateZone::fractalize(CMapGenerator* gen)
  337. {
  338. std::vector<int3> clearedTiles (freePaths.begin(), freePaths.end());
  339. std::set<int3> possibleTiles;
  340. std::set<int3> tilesToClear; //will be set clear
  341. std::set<int3> tilesToIgnore; //will be erased in this iteration
  342. //the more treasure density, the greater distance between paths. Scaling is experimental.
  343. const float minDistance = std::sqrt(totalDensity * 3);
  344. for (auto tile : tileinfo)
  345. {
  346. if (gen->isFree(tile))
  347. clearedTiles.push_back(tile);
  348. else if (gen->isPossible(tile))
  349. possibleTiles.insert(tile);
  350. }
  351. assert (clearedTiles.size()); //this should come from zone connections
  352. while (possibleTiles.size())
  353. {
  354. for (auto tileToMakePath : possibleTiles)
  355. {
  356. //find closest free tile
  357. float currentDistance = 1e10;
  358. int3 closestTile (-1,-1,-1);
  359. for (auto clearTile : clearedTiles)
  360. {
  361. float distance = tileToMakePath.dist2d(clearTile);
  362. if (distance < currentDistance)
  363. {
  364. currentDistance = distance;
  365. closestTile = clearTile;
  366. }
  367. if (currentDistance <= minDistance)
  368. {
  369. //this tile is close enough. Forget about it and check next one
  370. tilesToIgnore.insert (tileToMakePath);
  371. break;
  372. }
  373. }
  374. //if tiles is not close enough, make path to it
  375. if (currentDistance > minDistance)
  376. {
  377. crunchPath (gen, tileToMakePath, closestTile, id, &tilesToClear);
  378. break; //next iteration - use already cleared tiles
  379. }
  380. }
  381. for (auto tileToClear : tilesToClear)
  382. {
  383. //move cleared tiles from one set to another
  384. clearedTiles.push_back(tileToClear);
  385. vstd::erase_if_present(possibleTiles, tileToClear);
  386. }
  387. for (auto tileToClear : tilesToIgnore)
  388. {
  389. //these tiles are already connected, ignore them
  390. vstd::erase_if_present(possibleTiles, tileToClear);
  391. }
  392. if (tilesToClear.empty()) //nothing else can be done (?)
  393. break;
  394. tilesToClear.clear(); //empty this container
  395. tilesToIgnore.clear();
  396. }
  397. for (auto tile : clearedTiles)
  398. {
  399. freePaths.insert(tile);
  400. }
  401. if (0) //enable to debug
  402. {
  403. std::ofstream out(boost::to_string(boost::format("zone %d") % id));
  404. int levels = gen->map->twoLevel ? 2 : 1;
  405. int width = gen->map->width;
  406. int height = gen->map->height;
  407. for (int k = 0; k < levels; k++)
  408. {
  409. for(int j=0; j<height; j++)
  410. {
  411. for (int i=0; i<width; i++)
  412. {
  413. out << (int)vstd::contains(freePaths, int3(i,j,k));
  414. }
  415. out << std::endl;
  416. }
  417. out << std::endl;
  418. }
  419. }
  420. logGlobal->infoStream() << boost::format ("Zone %d subdivided fractally") %id;
  421. }
  422. bool CRmgTemplateZone::crunchPath (CMapGenerator* gen, const int3 &src, const int3 &dst, TRmgTemplateZoneId zone, std::set<int3>* clearedTiles)
  423. {
  424. /*
  425. make shortest path with free tiles, reachning dst or closest already free tile. Avoid blocks.
  426. do not leave zone border
  427. */
  428. bool result = false;
  429. bool end = false;
  430. int3 currentPos = src;
  431. float distance = currentPos.dist2dSQ (dst);
  432. while (!end)
  433. {
  434. if (currentPos == dst)
  435. {
  436. result = true;
  437. break;
  438. }
  439. auto lastDistance = distance;
  440. gen->foreach_neighbour (currentPos, [this, gen, &currentPos, dst, &distance, &result, &end, clearedTiles](int3 &pos)
  441. {
  442. if (!result) //not sure if lambda is worth it...
  443. {
  444. if (pos == dst)
  445. {
  446. result = true;
  447. end = true;
  448. }
  449. if (pos.dist2dSQ (dst) < distance)
  450. {
  451. if (!gen->isBlocked(pos))
  452. {
  453. if (vstd::contains (tileinfo, pos))
  454. {
  455. if (gen->isPossible(pos))
  456. {
  457. gen->setOccupied (pos, ETileType::FREE);
  458. if (clearedTiles)
  459. clearedTiles->insert(pos);
  460. currentPos = pos;
  461. distance = currentPos.dist2dSQ (dst);
  462. }
  463. else if (gen->isFree(pos))
  464. {
  465. end = true;
  466. result = true;
  467. }
  468. else
  469. throw rmgException(boost::to_string(boost::format("Tile %s of uknown type found on path") % pos()));
  470. }
  471. }
  472. }
  473. }
  474. });
  475. if (!(result || distance < lastDistance)) //we do not advance, use more avdnaced pathfinding algorithm?
  476. {
  477. logGlobal->warnStream() << boost::format ("No tile closer than %s found on path from %s to %s") %currentPos %src %dst;
  478. break;
  479. }
  480. }
  481. return result;
  482. }
  483. void CRmgTemplateZone::addRequiredObject(CGObjectInstance * obj, si32 strength)
  484. {
  485. requiredObjects.push_back(std::make_pair(obj, strength));
  486. }
  487. bool CRmgTemplateZone::addMonster(CMapGenerator* gen, int3 &pos, si32 strength, bool clearSurroundingTiles, bool zoneGuard)
  488. {
  489. //precalculate actual (randomized) monster strength based on this post
  490. //http://forum.vcmi.eu/viewtopic.php?p=12426#12426
  491. int mapMonsterStrength = gen->mapGenOptions->getMonsterStrength();
  492. int monsterStrength = (zoneGuard ? 0 : zoneMonsterStrength) + mapMonsterStrength - 1; //array index from 0 to 4
  493. static const int value1[] = {2500, 1500, 1000, 500, 0};
  494. static const int value2[] = {7500, 7500, 7500, 5000, 5000};
  495. static const float multiplier1[] = {0.5, 0.75, 1.0, 1.5, 1.5};
  496. static const float multiplier2[] = {0.5, 0.75, 1.0, 1.0, 1.5};
  497. int strength1 = std::max(0.f, (strength - value1[monsterStrength]) * multiplier1[monsterStrength]);
  498. int strength2 = std::max(0.f, (strength - value2[monsterStrength]) * multiplier2[monsterStrength]);
  499. strength = strength1 + strength2;
  500. if (strength < 2000)
  501. return false; //no guard at all
  502. CreatureID creId = CreatureID::NONE;
  503. int amount = 0;
  504. std::vector<CreatureID> possibleCreatures;
  505. for (auto cre : VLC->creh->creatures)
  506. {
  507. if (cre->special)
  508. continue;
  509. if ((cre->AIValue * (cre->ammMin + cre->ammMax) / 2 < strength) && (strength < cre->AIValue * 100)) //at least one full monster. size between minimum size of given stack and 100
  510. {
  511. possibleCreatures.push_back(cre->idNumber);
  512. }
  513. }
  514. if (possibleCreatures.size())
  515. {
  516. creId = *RandomGeneratorUtil::nextItem(possibleCreatures, gen->rand);
  517. amount = strength / VLC->creh->creatures[creId]->AIValue;
  518. if (amount >= 4)
  519. amount *= gen->rand.nextDouble(0.75, 1.25);
  520. }
  521. else //just pick any available creature
  522. {
  523. creId = CreatureID(132); //Azure Dragon
  524. amount = strength / VLC->creh->creatures[creId]->AIValue;
  525. }
  526. auto guard = new CGCreature();
  527. guard->ID = Obj::MONSTER;
  528. guard->subID = creId;
  529. auto hlp = new CStackInstance(creId, amount);
  530. //will be set during initialization
  531. guard->putStack(SlotID(0), hlp);
  532. //logGlobal->traceStream() << boost::format ("Adding stack of %d %s. Map monster strenght %d, zone monster strength %d, base monster value %d")
  533. // % amount % VLC->creh->creatures[creId]->namePl % mapMonsterStrength % zoneMonsterStrength % strength;
  534. placeObject(gen, guard, pos);
  535. if (clearSurroundingTiles)
  536. {
  537. //do not spawn anything near monster
  538. gen->foreach_neighbour (pos, [gen](int3 pos)
  539. {
  540. if (gen->isPossible(pos))
  541. gen->setOccupied(pos, ETileType::FREE);
  542. });
  543. }
  544. return true;
  545. }
  546. bool CRmgTemplateZone::createTreasurePile (CMapGenerator* gen, int3 &pos)
  547. {
  548. CTreasurePileInfo info;
  549. std::map<int3, CGObjectInstance *> treasures;
  550. std::set<int3> boundary;
  551. int3 guardPos (-1,-1,-1);
  552. info.nextTreasurePos = pos;
  553. //default values
  554. int maxValue = 5000;
  555. int minValue = 1500;
  556. if (treasureInfo.size())
  557. {
  558. //roulette wheel
  559. int r = gen->rand.nextInt (1, totalDensity);
  560. for (auto t : treasureInfo)
  561. {
  562. if (r <= t.threshold)
  563. {
  564. maxValue = t.max;
  565. minValue = t.min;
  566. break;
  567. }
  568. }
  569. }
  570. int currentValue = 0;
  571. CGObjectInstance * object = nullptr;
  572. while (currentValue < minValue)
  573. {
  574. treasures[info.nextTreasurePos] = nullptr;
  575. for (auto treasurePos : treasures)
  576. {
  577. gen->foreach_neighbour (treasurePos.first, [gen, &boundary](int3 pos)
  578. {
  579. boundary.insert(pos);
  580. });
  581. }
  582. for (auto treasurePos : treasures)
  583. {
  584. //leaving only boundary around objects
  585. vstd::erase_if_present (boundary, treasurePos.first);
  586. }
  587. for (auto tile : boundary)
  588. {
  589. //we can't extend boundary anymore
  590. if (!(gen->isBlocked(tile) || gen->isPossible(tile)))
  591. break;
  592. }
  593. int remaining = maxValue - currentValue;
  594. ObjectInfo oi = getRandomObject(gen, info, remaining);
  595. object = oi.generateObject();
  596. if (!object)
  597. {
  598. vstd::erase_if_present(treasures, info.nextTreasurePos);
  599. break;
  600. }
  601. else
  602. {
  603. //remove from possible objects
  604. auto oiptr = std::find(possibleObjects.begin(), possibleObjects.end(), oi);
  605. assert (oiptr != possibleObjects.end());
  606. oiptr->maxPerZone--;
  607. //TODO
  608. //update treasure pile area
  609. int3 visitablePos = info.nextTreasurePos;
  610. //TODO: actually we need to check is object is either !blockVisit or removable after visit - this means object below can be accessed
  611. if (oi.templ.isVisitableFromTop())
  612. info.visitableFromTopPositions.insert(visitablePos); //can be accessed from any direction
  613. else
  614. info.visitableFromBottomPositions.insert(visitablePos); //can be accessed only from bottom or side
  615. for (auto blockedOffset : oi.templ.getBlockedOffsets())
  616. {
  617. int3 blockPos = info.nextTreasurePos + blockedOffset + oi.templ.getVisitableOffset(); //object will be moved to align vistable pos to treasure pos
  618. info.occupiedPositions.insert(blockPos);
  619. info.blockedPositions.insert(blockPos);
  620. }
  621. info.occupiedPositions.insert(visitablePos);
  622. }
  623. currentValue += oi.value;
  624. treasures[info.nextTreasurePos] = object;
  625. //now find place for next object
  626. int3 placeFound(-1,-1,-1);
  627. for (auto tile : boundary)
  628. {
  629. if (gen->isPossible(tile)) //we can place new treasure only on possible tile
  630. {
  631. bool here = true;
  632. gen->foreach_neighbour (tile, [gen, &here](int3 pos)
  633. {
  634. if (!(gen->isBlocked(pos) || gen->isPossible(pos)))
  635. here = false;
  636. });
  637. if (here)
  638. {
  639. placeFound = tile;
  640. break;
  641. }
  642. }
  643. }
  644. if (placeFound.valid())
  645. info.nextTreasurePos = placeFound;
  646. }
  647. if (treasures.size())
  648. {
  649. //find object closest to zone center, then connect it to the middle of the zone
  650. int3 closestFreeTile (-1,-1,-1);
  651. if (info.visitableFromBottomPositions.size()) //get random treasure tile, starting from objects accessible only from bottom
  652. closestFreeTile = findClosestTile (freePaths, *RandomGeneratorUtil::nextItem(info.visitableFromBottomPositions, gen->rand));
  653. else
  654. closestFreeTile = findClosestTile (freePaths, *RandomGeneratorUtil::nextItem(info.visitableFromTopPositions, gen->rand));
  655. int3 closestTile = int3(-1,-1,-1);
  656. float minDistance = 1e10;
  657. for (auto visitablePos : info.visitableFromBottomPositions) //objects that are not visitable from top must be accessible from bottom or side
  658. {
  659. if (closestFreeTile.dist2d(visitablePos) < minDistance)
  660. {
  661. closestTile = visitablePos + int3 (0, 1, 0); //start below object (y+1), possibly even outside the map (?)
  662. minDistance = closestFreeTile.dist2d(visitablePos);
  663. }
  664. }
  665. if (!closestTile.valid())
  666. {
  667. for (auto visitablePos : info.visitableFromTopPositions) //all objects are accessible from any direction
  668. {
  669. if (closestFreeTile.dist2d(visitablePos) < minDistance)
  670. {
  671. closestTile = visitablePos;
  672. minDistance = closestFreeTile.dist2d(visitablePos);
  673. }
  674. }
  675. }
  676. assert (closestTile.valid());
  677. for (auto tile : info.occupiedPositions)
  678. {
  679. if (gen->map->isInTheMap(tile)) //pile boundary may reach map border
  680. gen->setOccupied(tile, ETileType::BLOCKED); //so that crunch path doesn't cut through objects
  681. }
  682. if (!crunchPath (gen, closestTile, closestFreeTile, id))
  683. {
  684. //we can't connect this pile, just block it off and start over
  685. for (auto treasure : treasures)
  686. {
  687. if (gen->isPossible(treasure.first))
  688. gen->setOccupied (treasure.first, ETileType::BLOCKED);
  689. }
  690. return true;
  691. }
  692. //update boundary around our objects, including knowledge about objects visitable from bottom
  693. boundary.clear();
  694. for (auto tile : info.visitableFromBottomPositions)
  695. {
  696. gen->foreach_neighbour (tile, [tile, &boundary](int3 pos)
  697. {
  698. if (pos.y >= tile.y) //don't block these objects from above
  699. boundary.insert(pos);
  700. });
  701. }
  702. for (auto tile : info.visitableFromTopPositions)
  703. {
  704. gen->foreach_neighbour (tile, [&boundary](int3 pos)
  705. {
  706. boundary.insert(pos);
  707. });
  708. }
  709. for (auto tile : boundary) //guard must be standing there
  710. {
  711. if (gen->isFree(tile)) //this tile could be already blocked, don't place a monster here
  712. {
  713. guardPos = tile;
  714. break;
  715. }
  716. }
  717. if (guardPos.valid())
  718. {
  719. for (auto treasure : treasures)
  720. {
  721. int3 visitableOffset = treasure.second->getVisitableOffset();
  722. placeObject(gen, treasure.second, treasure.first + visitableOffset);
  723. }
  724. if (addMonster(gen, guardPos, currentValue, false))
  725. {//block only if the object is guarded
  726. for (auto tile : boundary)
  727. {
  728. if (gen->isPossible(tile))
  729. gen->setOccupied (tile, ETileType::BLOCKED);
  730. }
  731. //do not spawn anything near monster
  732. gen->foreach_neighbour (guardPos, [gen](int3 pos)
  733. {
  734. if (gen->isPossible(pos))
  735. gen->setOccupied(pos, ETileType::FREE);
  736. });
  737. }
  738. }
  739. else //we couldn't make a connection to this location, block it
  740. {
  741. for (auto treasure : treasures)
  742. {
  743. if (gen->isPossible(treasure.first))
  744. gen->setOccupied (treasure.first, ETileType::BLOCKED);
  745. }
  746. }
  747. return true;
  748. }
  749. else //we did not place eveyrthing successfully
  750. return false;
  751. }
  752. void CRmgTemplateZone::initTownType (CMapGenerator* gen)
  753. {
  754. //FIXME: handle case that this player is not present -> towns should be set to neutral
  755. int totalTowns = 0;
  756. auto addNewTowns = [&totalTowns, gen, this](int count, bool hasFort, PlayerColor player)
  757. {
  758. for (int i = 0; i < count; i++)
  759. {
  760. auto town = new CGTownInstance();
  761. town->ID = Obj::TOWN;
  762. if (this->townsAreSameType)
  763. town->subID = townType;
  764. else
  765. town->subID = *RandomGeneratorUtil::nextItem(VLC->townh->getAllowedFactions(), gen->rand); //TODO: check allowed town types for this zone
  766. town->tempOwner = player;
  767. if (hasFort)
  768. town->builtBuildings.insert(BuildingID::FORT);
  769. town->builtBuildings.insert(BuildingID::DEFAULT);
  770. if (!totalTowns)
  771. {
  772. //first town in zone sets the facton of entire zone
  773. town->subID = townType;
  774. //register MAIN town of zone
  775. gen->registerZone(town->subID);
  776. //first town in zone goes in the middle
  777. placeAndGuardObject(gen, town, getPos() + town->getVisitableOffset(), 0);
  778. }
  779. else
  780. addRequiredObject (town);
  781. totalTowns++;
  782. }
  783. };
  784. if ((type == ETemplateZoneType::CPU_START) || (type == ETemplateZoneType::PLAYER_START))
  785. {
  786. //set zone types to player faction, generate main town
  787. logGlobal->infoStream() << "Preparing playing zone";
  788. int player_id = *owner - 1;
  789. auto & playerInfo = gen->map->players[player_id];
  790. if (playerInfo.canAnyonePlay())
  791. {
  792. PlayerColor player(player_id);
  793. townType = gen->mapGenOptions->getPlayersSettings().find(player)->second.getStartingTown();
  794. if(townType == CMapGenOptions::CPlayerSettings::RANDOM_TOWN)
  795. townType = *RandomGeneratorUtil::nextItem(VLC->townh->getAllowedFactions(), gen->rand); // all possible towns, skip neutral
  796. auto town = new CGTownInstance();
  797. town->ID = Obj::TOWN;
  798. town->subID = townType;
  799. town->tempOwner = player;
  800. town->builtBuildings.insert(BuildingID::FORT);
  801. town->builtBuildings.insert(BuildingID::DEFAULT);
  802. //towns are big objects and should be centered around visitable position
  803. placeAndGuardObject(gen, town, getPos() + town->getVisitableOffset(), 0); //generate no guards, but free path to entrance
  804. totalTowns++;
  805. //register MAIN town of zone only
  806. gen->registerZone (town->subID);
  807. logGlobal->traceStream() << "Fill player info " << player_id;
  808. // Update player info
  809. playerInfo.allowedFactions.clear();
  810. playerInfo.allowedFactions.insert (townType);
  811. playerInfo.hasMainTown = true;
  812. playerInfo.posOfMainTown = town->pos - town->getVisitableOffset();
  813. playerInfo.generateHeroAtMainTown = true;
  814. //now create actual towns
  815. addNewTowns (playerTowns.getCastleCount() - 1, true, player);
  816. addNewTowns (playerTowns.getTownCount(), false, player);
  817. //requiredObjects.push_back(town);
  818. }
  819. else
  820. {
  821. type = ETemplateZoneType::TREASURE;
  822. townType = *RandomGeneratorUtil::nextItem(VLC->townh->getAllowedFactions(), gen->rand);
  823. logGlobal->infoStream() << "Skipping this zone cause no player";
  824. }
  825. }
  826. else //no player
  827. {
  828. townType = *RandomGeneratorUtil::nextItem(VLC->townh->getAllowedFactions(), gen->rand);
  829. }
  830. addNewTowns (neutralTowns.getCastleCount(), true, PlayerColor::NEUTRAL);
  831. addNewTowns (neutralTowns.getTownCount(), false, PlayerColor::NEUTRAL);
  832. }
  833. void CRmgTemplateZone::initTerrainType (CMapGenerator* gen)
  834. {
  835. if (matchTerrainToTown)
  836. terrainType = VLC->townh->factions[townType]->nativeTerrain;
  837. else
  838. terrainType = *RandomGeneratorUtil::nextItem(terrainTypes, gen->rand);
  839. //TODO: allow new types of terrain?
  840. if (pos.z)
  841. {
  842. if (terrainType != ETerrainType::LAVA)
  843. terrainType = ETerrainType::SUBTERRANEAN;
  844. }
  845. else
  846. {
  847. if (terrainType == ETerrainType::SUBTERRANEAN)
  848. terrainType = ETerrainType::DIRT;
  849. }
  850. paintZoneTerrain (gen, terrainType);
  851. }
  852. void CRmgTemplateZone::paintZoneTerrain (CMapGenerator* gen, ETerrainType terrainType)
  853. {
  854. std::vector<int3> tiles;
  855. for (auto tile : tileinfo)
  856. {
  857. tiles.push_back (tile);
  858. }
  859. gen->editManager->getTerrainSelection().setSelection(tiles);
  860. gen->editManager->drawTerrain(terrainType, &gen->rand);
  861. }
  862. bool CRmgTemplateZone::placeMines (CMapGenerator* gen)
  863. {
  864. std::vector<Res::ERes> required_mines;
  865. required_mines.push_back(Res::ERes::WOOD);
  866. required_mines.push_back(Res::ERes::ORE);
  867. static const Res::ERes woodOre[] = {Res::ERes::WOOD, Res::ERes::ORE};
  868. static const Res::ERes preciousResources[] = {Res::ERes::GEMS, Res::ERes::CRYSTAL, Res::ERes::MERCURY, Res::ERes::SULFUR};
  869. //TODO: factory / copy constructor?
  870. for (const auto & res : woodOre)
  871. {
  872. //TODO: these 2 should be close to town (within 12 tiles radius)
  873. for (int i = 0; i < mines[res]; i++)
  874. {
  875. auto mine = new CGMine();
  876. mine->ID = Obj::MINE;
  877. mine->subID = static_cast<si32>(res);
  878. mine->producedResource = res;
  879. mine->producedQuantity = mine->defaultResProduction();
  880. addRequiredObject(mine, 1500);
  881. }
  882. }
  883. for (const auto & res : preciousResources)
  884. {
  885. for (int i = 0; i < mines[res]; i++)
  886. {
  887. auto mine = new CGMine();
  888. mine->ID = Obj::MINE;
  889. mine->subID = static_cast<si32>(res);
  890. mine->producedResource = res;
  891. mine->producedQuantity = mine->defaultResProduction();
  892. addRequiredObject(mine, 3500);
  893. }
  894. }
  895. for (int i = 0; i < mines[Res::GOLD]; i++)
  896. {
  897. auto mine = new CGMine();
  898. mine->ID = Obj::MINE;
  899. mine->subID = static_cast<si32>(Res::GOLD);
  900. mine->producedResource = Res::GOLD;
  901. mine->producedQuantity = mine->defaultResProduction();
  902. addRequiredObject(mine, 7000);
  903. }
  904. return true;
  905. }
  906. bool CRmgTemplateZone::createRequiredObjects(CMapGenerator* gen)
  907. {
  908. logGlobal->infoStream() << "Creating required objects";
  909. for(const auto &obj : requiredObjects)
  910. {
  911. int3 pos;
  912. logGlobal->traceStream() << "Looking for place";
  913. if ( ! findPlaceForObject(gen, obj.first, 3, pos))
  914. {
  915. logGlobal->errorStream() << boost::format("Failed to fill zone %d due to lack of space") %id;
  916. //TODO CLEANUP!
  917. return false;
  918. }
  919. logGlobal->traceStream() << "Place found";
  920. placeObject (gen, obj.first, pos);
  921. guardObject (gen, obj.first, obj.second, (obj.first->ID == Obj::MONOLITH_TWO_WAY));
  922. }
  923. return true;
  924. }
  925. void CRmgTemplateZone::createTreasures(CMapGenerator* gen)
  926. {
  927. const double minDistance = 3;
  928. do {
  929. int3 pos;
  930. if ( ! findPlaceForTreasurePile(gen, minDistance, pos))
  931. {
  932. break;
  933. }
  934. createTreasurePile (gen, pos);
  935. } while(true);
  936. }
  937. void CRmgTemplateZone::createObstacles(CMapGenerator* gen)
  938. {
  939. if (pos.z) //underground
  940. {
  941. std::vector<int3> rockTiles;
  942. for (auto tile : tileinfo)
  943. {
  944. if (gen->shouldBeBlocked(tile))
  945. {
  946. bool placeRock = true;
  947. gen->foreach_neighbour (tile, [gen, &placeRock](int3 &pos)
  948. {
  949. if (!(gen->shouldBeBlocked(pos) || gen->isPossible(pos)))
  950. placeRock = false;
  951. });
  952. if (placeRock)
  953. {
  954. rockTiles.push_back(tile);
  955. }
  956. }
  957. }
  958. gen->editManager->getTerrainSelection().setSelection(rockTiles);
  959. gen->editManager->drawTerrain(ETerrainType::ROCK, &gen->rand);
  960. //for (auto tile : rockTiles)
  961. //{
  962. // gen->setOccupied (tile, ETileType::USED);
  963. // gen->foreach_neighbour (tile, [gen](int3 &pos)
  964. // {
  965. // if (!gen->isUsed(pos))
  966. // gen->setOccupied (pos, ETileType::BLOCKED);
  967. // });
  968. //}
  969. }
  970. //get all possible obstacles for this terrain
  971. for (auto primaryID : VLC->objtypeh->knownObjects())
  972. {
  973. for (auto secondaryID : VLC->objtypeh->knownSubObjects(primaryID))
  974. {
  975. auto handler = VLC->objtypeh->getHandlerFor(primaryID, secondaryID);
  976. if (handler->isStaticObject())
  977. {
  978. for (auto temp : handler->getTemplates())
  979. {
  980. if (temp.canBePlacedAt(terrainType) && temp.getBlockMapOffset().valid())
  981. possibleObstacles.push_back(temp);
  982. }
  983. }
  984. }
  985. }
  986. auto sel = gen->editManager->getTerrainSelection();
  987. sel.clearSelection();
  988. auto tryToPlaceObstacleHere = [this, gen](int3& tile)-> bool
  989. {
  990. auto temp = *RandomGeneratorUtil::nextItem(possibleObstacles, gen->rand);
  991. int3 obstaclePos = tile - temp.getBlockMapOffset();
  992. if (canObstacleBePlacedHere(gen, temp, obstaclePos)) //can be placed here
  993. {
  994. auto obj = VLC->objtypeh->getHandlerFor(temp.id, temp.subid)->create(temp);
  995. placeObject(gen, obj, obstaclePos);
  996. return true;
  997. }
  998. return false;
  999. };
  1000. for (auto tile : tileinfo)
  1001. {
  1002. if (gen->shouldBeBlocked(tile)) //fill tiles that should be blocked with obstacles
  1003. {
  1004. while (!tryToPlaceObstacleHere(tile));
  1005. }
  1006. else if (gen->isPossible(tile))
  1007. {
  1008. //try to place random obstacle once - if not possible, leave it clear
  1009. tryToPlaceObstacleHere(tile);
  1010. }
  1011. }
  1012. }
  1013. bool CRmgTemplateZone::fill(CMapGenerator* gen)
  1014. {
  1015. initTownType(gen);
  1016. initTerrainType(gen);
  1017. freePaths.insert(pos); //zone center should be always clear to allow other tiles to connect
  1018. addAllPossibleObjects (gen);
  1019. placeMines(gen);
  1020. createRequiredObjects(gen);
  1021. fractalize(gen); //after required objects are created and linked with their own paths
  1022. createTreasures(gen);
  1023. createObstacles(gen);
  1024. logGlobal->infoStream() << boost::format ("Zone %d filled successfully") %id;
  1025. return true;
  1026. }
  1027. bool CRmgTemplateZone::findPlaceForTreasurePile(CMapGenerator* gen, si32 min_dist, int3 &pos)
  1028. {
  1029. //si32 min_dist = sqrt(tileinfo.size()/density);
  1030. int best_distance = 0;
  1031. bool result = false;
  1032. //logGlobal->infoStream() << boost::format("Min dist for density %f is %d") % density % min_dist;
  1033. for(auto tile : tileinfo)
  1034. {
  1035. auto dist = gen->getTile(tile).getNearestObjectDistance();
  1036. if (gen->isPossible(tile) && (dist >= min_dist) && (dist > best_distance))
  1037. {
  1038. bool allTilesAvailable = true;
  1039. gen->foreach_neighbour (tile, [&gen, &allTilesAvailable](int3 neighbour)
  1040. {
  1041. if (!(gen->isPossible(neighbour) || gen->isBlocked(neighbour)))
  1042. {
  1043. allTilesAvailable = false; //all present tiles must be already blocked or ready for new objects
  1044. }
  1045. });
  1046. if (allTilesAvailable)
  1047. {
  1048. best_distance = dist;
  1049. pos = tile;
  1050. result = true;
  1051. }
  1052. }
  1053. }
  1054. if (result)
  1055. {
  1056. gen->setOccupied(pos, ETileType::BLOCKED); //block that tile
  1057. }
  1058. return result;
  1059. }
  1060. bool CRmgTemplateZone::canObstacleBePlacedHere(CMapGenerator* gen, ObjectTemplate &temp, int3 &pos)
  1061. {
  1062. if (!gen->map->isInTheMap(pos)) //blockmap may fit in the map, but botom-right corner does not
  1063. return false;
  1064. auto tilesBlockedByObject = temp.getBlockedOffsets();
  1065. for (auto blockingTile : tilesBlockedByObject)
  1066. {
  1067. int3 t = pos + blockingTile;
  1068. if (!gen->map->isInTheMap(t) || !(gen->isPossible(t) || gen->shouldBeBlocked(t)))
  1069. {
  1070. return false; //if at least one tile is not possible, object can't be placed here
  1071. }
  1072. }
  1073. return true;
  1074. }
  1075. bool CRmgTemplateZone::isAccessibleFromAnywhere (CMapGenerator* gen, ObjectTemplate &appearance, int3 &tile, const std::set<int3> &tilesBlockedByObject) const
  1076. {
  1077. bool accessible = false;
  1078. for (int x = -1; x < 2; x++)
  1079. {
  1080. for (int y = -1; y <2; y++)
  1081. {
  1082. if (x && y) //check only if object is visitable from another tile
  1083. {
  1084. int3 offset = appearance.getVisitableOffset() + int3(x, y, 0);
  1085. if (!vstd::contains(tilesBlockedByObject, offset))
  1086. {
  1087. int3 nearbyPos = tile + offset;
  1088. if (gen->map->isInTheMap(nearbyPos))
  1089. {
  1090. if (appearance.isVisitableFrom(x, y) && !gen->isBlocked(nearbyPos))
  1091. accessible = true;
  1092. }
  1093. }
  1094. }
  1095. };
  1096. }
  1097. return accessible;
  1098. }
  1099. bool CRmgTemplateZone::findPlaceForObject(CMapGenerator* gen, CGObjectInstance* obj, si32 min_dist, int3 &pos)
  1100. {
  1101. //we need object apperance to deduce free tiles
  1102. if (obj->appearance.id == Obj::NO_OBJ)
  1103. {
  1104. auto templates = VLC->objtypeh->getHandlerFor(obj->ID, obj->subID)->getTemplates(gen->map->getTile(getPos()).terType);
  1105. if (templates.empty())
  1106. throw rmgException(boost::to_string(boost::format("Did not find graphics for object (%d,%d) at %s") %obj->ID %obj->subID %pos));
  1107. obj->appearance = templates.front();
  1108. }
  1109. //si32 min_dist = sqrt(tileinfo.size()/density);
  1110. int best_distance = 0;
  1111. bool result = false;
  1112. //si32 w = gen->map->width;
  1113. //si32 h = gen->map->height;
  1114. //logGlobal->infoStream() << boost::format("Min dist for density %f is %d") % density % min_dist;
  1115. auto tilesBlockedByObject = obj->getBlockedOffsets();
  1116. for (auto tile : tileinfo)
  1117. {
  1118. //object must be accessible from at least one surounding tile
  1119. if (!isAccessibleFromAnywhere(gen, obj->appearance, tile, tilesBlockedByObject))
  1120. continue;
  1121. auto ti = gen->getTile(tile);
  1122. auto dist = ti.getNearestObjectDistance();
  1123. //avoid borders
  1124. if (gen->isPossible(tile) && (dist >= min_dist) && (dist > best_distance))
  1125. {
  1126. bool allTilesAvailable = true;
  1127. for (auto blockingTile : tilesBlockedByObject)
  1128. {
  1129. int3 t = tile + blockingTile;
  1130. if (!gen->map->isInTheMap(t) || !gen->isPossible(t))
  1131. {
  1132. allTilesAvailable = false; //if at least one tile is not possible, object can't be placed here
  1133. break;
  1134. }
  1135. }
  1136. if (allTilesAvailable)
  1137. {
  1138. best_distance = dist;
  1139. pos = tile;
  1140. result = true;
  1141. }
  1142. }
  1143. }
  1144. if (result)
  1145. {
  1146. gen->setOccupied(pos, ETileType::BLOCKED); //block that tile
  1147. }
  1148. return result;
  1149. }
  1150. void CRmgTemplateZone::checkAndPlaceObject(CMapGenerator* gen, CGObjectInstance* object, const int3 &pos)
  1151. {
  1152. if (!gen->map->isInTheMap(pos))
  1153. throw rmgException(boost::to_string(boost::format("Position of object %d at %s is outside the map") % object->id % pos));
  1154. object->pos = pos;
  1155. if (object->isVisitable() && !gen->map->isInTheMap(object->visitablePos()))
  1156. throw rmgException(boost::to_string(boost::format("Visitable tile %s of object %d at %s is outside the map") % object->visitablePos() % object->id % object->pos()));
  1157. for (auto tile : object->getBlockedPos())
  1158. {
  1159. if (!gen->map->isInTheMap(tile))
  1160. throw rmgException(boost::to_string(boost::format("Tile %s of object %d at %s is outside the map") % tile() % object->id % object->pos()));
  1161. }
  1162. if (object->appearance.id == Obj::NO_OBJ)
  1163. {
  1164. auto terrainType = gen->map->getTile(pos).terType;
  1165. auto templates = VLC->objtypeh->getHandlerFor(object->ID, object->subID)->getTemplates(terrainType);
  1166. if (templates.empty())
  1167. throw rmgException(boost::to_string(boost::format("Did not find graphics for object (%d,%d) at %s (terrain %d)") %object->ID %object->subID %pos %terrainType));
  1168. object->appearance = templates.front();
  1169. }
  1170. gen->map->addBlockVisTiles(object);
  1171. gen->editManager->insertObject(object, pos);
  1172. //logGlobal->traceStream() << boost::format ("Successfully inserted object (%d,%d) at pos %s") %object->ID %object->subID %pos();
  1173. }
  1174. void CRmgTemplateZone::placeObject(CMapGenerator* gen, CGObjectInstance* object, const int3 &pos)
  1175. {
  1176. //logGlobal->traceStream() << boost::format("Inserting object at %d %d") % pos.x % pos.y;
  1177. checkAndPlaceObject (gen, object, pos);
  1178. auto points = object->getBlockedPos();
  1179. if (object->isVisitable())
  1180. points.insert(pos + object->getVisitableOffset());
  1181. points.insert(pos);
  1182. for(auto p : points)
  1183. {
  1184. if (gen->map->isInTheMap(p))
  1185. {
  1186. gen->setOccupied(p, ETileType::USED);
  1187. }
  1188. }
  1189. for(auto tile : tileinfo)
  1190. {
  1191. si32 d = pos.dist2dSQ(tile); //optimization, only relative distance is interesting
  1192. gen->setNearestObjectDistance(tile, std::min(d, gen->getNearestObjectDistance(tile)));
  1193. }
  1194. }
  1195. void CRmgTemplateZone::placeAndGuardObject(CMapGenerator* gen, CGObjectInstance* object, const int3 &pos, si32 str, bool zoneGuard)
  1196. {
  1197. placeObject(gen, object, pos);
  1198. guardObject(gen, object, str, zoneGuard);
  1199. }
  1200. std::vector<int3> CRmgTemplateZone::getAccessibleOffsets (CMapGenerator* gen, CGObjectInstance* object)
  1201. {
  1202. //get all tiles from which this object can be accessed
  1203. int3 visitable = object->visitablePos();
  1204. std::vector<int3> tiles;
  1205. auto tilesBlockedByObject = object->getBlockedPos(); //absolue value, as object is already placed
  1206. gen->foreach_neighbour(visitable, [&](int3& pos)
  1207. {
  1208. if (gen->isPossible(pos))
  1209. {
  1210. if (!vstd::contains(tilesBlockedByObject, pos))
  1211. {
  1212. if (object->appearance.isVisitableFrom(pos.x - visitable.x, pos.y - visitable.y) && !gen->isBlocked(pos)) //TODO: refactor - info about visitability from absolute coordinates
  1213. {
  1214. tiles.push_back(pos);
  1215. }
  1216. }
  1217. };
  1218. });
  1219. return tiles;
  1220. }
  1221. bool CRmgTemplateZone::guardObject(CMapGenerator* gen, CGObjectInstance* object, si32 str, bool zoneGuard)
  1222. {
  1223. logGlobal->traceStream() << boost::format("Guard object at %s") % object->pos();
  1224. std::vector<int3> tiles = getAccessibleOffsets (gen, object);
  1225. int3 guardTile(-1,-1,-1);
  1226. for (auto tile : tiles)
  1227. {
  1228. //crunching path may fail if center of teh zone is dirrectly over wide object
  1229. if (crunchPath (gen, tile, findClosestTile(freePaths, tile), id)) //make sure object is accessible before surrounding it with blocked tiles
  1230. {
  1231. guardTile = tile;
  1232. break;
  1233. }
  1234. }
  1235. if (!guardTile.valid())
  1236. {
  1237. logGlobal->errorStream() << boost::format("Failed to crunch path to object at %s") % object->pos();
  1238. return false;
  1239. }
  1240. if (addMonster (gen, guardTile, str, false, zoneGuard)) //do not place obstacles around unguarded object
  1241. {
  1242. for (auto pos : tiles)
  1243. {
  1244. if (!gen->isFree(pos))
  1245. gen->setOccupied(pos, ETileType::BLOCKED);
  1246. }
  1247. gen->foreach_neighbour (guardTile, [&](int3& pos)
  1248. {
  1249. if (gen->isPossible(pos))
  1250. gen->setOccupied (pos, ETileType::FREE);
  1251. });
  1252. gen->setOccupied (guardTile, ETileType::USED);
  1253. }
  1254. else //allow no guard or other object in front of this object
  1255. {
  1256. for (auto tile : tiles)
  1257. if (gen->isPossible(tile))
  1258. gen->setOccupied (tile, ETileType::FREE);
  1259. }
  1260. return true;
  1261. }
  1262. ObjectInfo CRmgTemplateZone::getRandomObject (CMapGenerator* gen, CTreasurePileInfo &info, ui32 value)
  1263. {
  1264. //int objectsVisitableFromBottom = 0; //for debug
  1265. std::vector<std::pair<ui32, ObjectInfo>> tresholds;
  1266. ui32 total = 0;
  1267. ui32 minValue = 0.25f * value;
  1268. //roulette wheel
  1269. for (auto oi : possibleObjects)
  1270. {
  1271. if (oi.value >= minValue && oi.value <= value && oi.maxPerZone > 0)
  1272. {
  1273. int3 newVisitableOffset = oi.templ.getVisitableOffset(); //visitablePos assumes object will be shifter by visitableOffset
  1274. int3 newVisitablePos = info.nextTreasurePos;
  1275. if (!oi.templ.isVisitableFromTop())
  1276. {
  1277. //objectsVisitableFromBottom++;
  1278. //there must be free tiles under object
  1279. auto blockedOffsets = oi.templ.getBlockedOffsets();
  1280. if (!isAccessibleFromAnywhere(gen, oi.templ, newVisitablePos, blockedOffsets))
  1281. continue;
  1282. }
  1283. //NOTE: y coordinate grows downwards
  1284. if (info.visitableFromBottomPositions.size() + info.visitableFromTopPositions.size()) //do not try to match first object in zone
  1285. {
  1286. bool fitsHere = false;
  1287. if (oi.templ.isVisitableFromTop()) //new can be accessed from any direction
  1288. {
  1289. for (auto tile : info.visitableFromTopPositions)
  1290. {
  1291. int3 actualTile = tile + newVisitableOffset;
  1292. if (newVisitablePos.areNeighbours(actualTile)) //we access other removable object from any position
  1293. {
  1294. fitsHere = true;
  1295. break;
  1296. }
  1297. }
  1298. for (auto tile : info.visitableFromBottomPositions)
  1299. {
  1300. int3 actualTile = tile + newVisitableOffset;
  1301. if (newVisitablePos.areNeighbours(actualTile) && newVisitablePos.y >= actualTile.y) //we access existing static object from side or bottom only
  1302. {
  1303. fitsHere = true;
  1304. break;
  1305. }
  1306. }
  1307. }
  1308. else //if new object is not visitable from top, it must be accessible from below or side
  1309. {
  1310. for (auto tile : info.visitableFromTopPositions)
  1311. {
  1312. int3 actualTile = tile + newVisitableOffset;
  1313. if (newVisitablePos.areNeighbours(actualTile) && newVisitablePos.y <= actualTile.y) //we access existing removable object from top or side only
  1314. {
  1315. fitsHere = true;
  1316. break;
  1317. }
  1318. }
  1319. for (auto tile : info.visitableFromBottomPositions)
  1320. {
  1321. int3 actualTile = tile + newVisitableOffset;
  1322. if (newVisitablePos.areNeighbours(actualTile) && newVisitablePos.y == actualTile.y) //we access other static object from side only
  1323. {
  1324. fitsHere = true;
  1325. break;
  1326. }
  1327. }
  1328. }
  1329. if (!fitsHere)
  1330. continue;
  1331. }
  1332. //now check blockmap, including our already reserved pile area
  1333. bool fitsBlockmap = true;
  1334. std::set<int3> blockedOffsets = oi.templ.getBlockedOffsets();
  1335. blockedOffsets.insert (newVisitableOffset);
  1336. for (auto blockingTile : blockedOffsets)
  1337. {
  1338. int3 t = info.nextTreasurePos + newVisitableOffset + blockingTile;
  1339. if (!gen->map->isInTheMap(t) || vstd::contains(info.occupiedPositions, t))
  1340. {
  1341. fitsBlockmap = false; //if at least one tile is not possible, object can't be placed here
  1342. break;
  1343. }
  1344. if (!(gen->isPossible(t) || gen->isBlocked(t))) //blocked tiles of object may cover blocked tiles, but not used or free tiles
  1345. {
  1346. fitsBlockmap = false;
  1347. break;
  1348. }
  1349. }
  1350. if (!fitsBlockmap)
  1351. continue;
  1352. total += oi.probability;
  1353. tresholds.push_back (std::make_pair (total, oi));
  1354. }
  1355. }
  1356. //logGlobal->infoStream() << boost::format ("Number of objects visitable from bottom: %d") % objectsVisitableFromBottom;
  1357. //Generate pandora Box with gold if the value is extremely high
  1358. ObjectInfo oi;
  1359. if (tresholds.empty())
  1360. {
  1361. if (minValue > 20000) //we don't have object valuable enough
  1362. {
  1363. oi.generateObject = [minValue]() -> CGObjectInstance *
  1364. {
  1365. auto obj = new CGPandoraBox();
  1366. obj->ID = Obj::PANDORAS_BOX;
  1367. obj->subID = 0;
  1368. obj->resources[Res::GOLD] = minValue;
  1369. return obj;
  1370. };
  1371. oi.setTemplate(Obj::PANDORAS_BOX, 0, terrainType);
  1372. oi.value = minValue;
  1373. oi.probability = 0;
  1374. }
  1375. else
  1376. {
  1377. oi.generateObject = [gen]() -> CGObjectInstance *
  1378. {
  1379. return nullptr;
  1380. };
  1381. oi.setTemplate(Obj::PANDORAS_BOX, 0, terrainType); //TODO: null template or something? should be never used, but hell knows
  1382. oi.value = 0;
  1383. oi.probability = 0;
  1384. }
  1385. return oi;
  1386. }
  1387. int r = gen->rand.nextInt (1, total);
  1388. for (auto t : tresholds)
  1389. {
  1390. if (r <= t.first)
  1391. return t.second;
  1392. }
  1393. //FIXME: control reaches end of non-void function. Missing return?
  1394. }
  1395. void CRmgTemplateZone::addAllPossibleObjects (CMapGenerator* gen)
  1396. {
  1397. ObjectInfo oi;
  1398. oi.maxPerMap = std::numeric_limits<ui32>().max();
  1399. int numZones = gen->getZones().size();
  1400. for (auto primaryID : VLC->objtypeh->knownObjects())
  1401. {
  1402. for (auto secondaryID : VLC->objtypeh->knownSubObjects(primaryID))
  1403. {
  1404. auto handler = VLC->objtypeh->getHandlerFor(primaryID, secondaryID);
  1405. if (!handler->isStaticObject() && handler->getRMGInfo().value)
  1406. {
  1407. for (auto temp : handler->getTemplates())
  1408. {
  1409. if (temp.canBePlacedAt(terrainType))
  1410. {
  1411. oi.generateObject = [gen, temp]() -> CGObjectInstance *
  1412. {
  1413. return VLC->objtypeh->getHandlerFor(temp.id, temp.subid)->create(temp);
  1414. };
  1415. auto rmgInfo = handler->getRMGInfo();
  1416. oi.value = rmgInfo.value;
  1417. oi.probability = rmgInfo.rarity;
  1418. oi.templ = temp;
  1419. oi.maxPerZone = rmgInfo.zoneLimit;
  1420. vstd::amin (oi.maxPerZone, rmgInfo.mapLimit / numZones); //simple, but should distribute objects evenly on large maps
  1421. possibleObjects.push_back (oi);
  1422. }
  1423. }
  1424. }
  1425. }
  1426. }
  1427. //all following objects are unlimited
  1428. oi.maxPerZone = std::numeric_limits<ui32>().max();
  1429. //dwellings
  1430. auto subObjects = VLC->objtypeh->knownSubObjects(Obj::CREATURE_GENERATOR1);
  1431. //don't spawn original "neutral" dwellings that got replaced by Conflux dwellings in AB
  1432. static int elementalConfluxROE[] = {7, 13, 16, 47};
  1433. for (int i = 0; i < 4; i++)
  1434. vstd::erase_if_present(subObjects, elementalConfluxROE[i]);
  1435. for (auto secondaryID : subObjects)
  1436. {
  1437. auto dwellingHandler = dynamic_cast<const CDwellingInstanceConstructor*>(VLC->objtypeh->getHandlerFor(Obj::CREATURE_GENERATOR1, secondaryID).get());
  1438. auto creatures = dwellingHandler->getProducedCreatures();
  1439. if (creatures.empty())
  1440. continue;
  1441. auto cre = creatures.front();
  1442. if (cre->faction == townType)
  1443. {
  1444. oi.value = cre->AIValue * cre->growth * (1 + (float)(gen->getZoneCount(cre->faction)) / gen->getTotalZoneCount() + (float)(gen->getZoneCount(cre->faction) / 2)); //TODO: include town count in formula
  1445. oi.probability = 40;
  1446. for (auto temp : dwellingHandler->getTemplates())
  1447. {
  1448. if (temp.canBePlacedAt(terrainType))
  1449. {
  1450. oi.generateObject = [gen, temp, secondaryID, dwellingHandler]() -> CGObjectInstance *
  1451. {
  1452. auto obj = VLC->objtypeh->getHandlerFor(Obj::CREATURE_GENERATOR1, secondaryID)->create(temp);
  1453. //dwellingHandler->configureObject(obj, gen->rand);
  1454. obj->tempOwner = PlayerColor::NEUTRAL;
  1455. return obj;
  1456. };
  1457. oi.templ = temp;
  1458. possibleObjects.push_back (oi);
  1459. }
  1460. }
  1461. }
  1462. }
  1463. static const int scrollValues[] = {500, 2000, 3000, 4000, 5000};
  1464. for (int i = 0; i < 5; i++)
  1465. {
  1466. oi.generateObject = [i, gen]() -> CGObjectInstance *
  1467. {
  1468. auto obj = new CGArtifact();
  1469. obj->ID = Obj::SPELL_SCROLL;
  1470. obj->subID = 0;
  1471. std::vector<SpellID> out;
  1472. //TODO: unify with cb->getAllowedSpells?
  1473. for (ui32 i = 0; i < gen->map->allowedSpell.size(); i++) //spellh size appears to be greater (?)
  1474. {
  1475. const CSpell *spell = SpellID(i).toSpell();
  1476. if (gen->map->allowedSpell[spell->id] && spell->level == i+1)
  1477. {
  1478. out.push_back(spell->id);
  1479. }
  1480. }
  1481. auto a = CArtifactInstance::createScroll(RandomGeneratorUtil::nextItem(out, gen->rand)->toSpell());
  1482. gen->map->addNewArtifactInstance(a);
  1483. obj->storedArtifact = a;
  1484. return obj;
  1485. };
  1486. oi.setTemplate (Obj::SPELL_SCROLL, 0, terrainType);
  1487. oi.value = scrollValues[i];
  1488. oi.probability = 30;
  1489. possibleObjects.push_back (oi);
  1490. }
  1491. //pandora box with gold
  1492. for (int i = 1; i < 5; i++)
  1493. {
  1494. oi.generateObject = [i]() -> CGObjectInstance *
  1495. {
  1496. auto obj = new CGPandoraBox();
  1497. obj->ID = Obj::PANDORAS_BOX;
  1498. obj->subID = 0;
  1499. obj->resources[Res::GOLD] = i * 5000;
  1500. return obj;
  1501. };
  1502. oi.setTemplate (Obj::PANDORAS_BOX, 0, terrainType);
  1503. oi.value = i * 5000;;
  1504. oi.probability = 5;
  1505. possibleObjects.push_back (oi);
  1506. }
  1507. //pandora box with experience
  1508. for (int i = 1; i < 5; i++)
  1509. {
  1510. oi.generateObject = [i]() -> CGObjectInstance *
  1511. {
  1512. auto obj = new CGPandoraBox();
  1513. obj->ID = Obj::PANDORAS_BOX;
  1514. obj->subID = 0;
  1515. obj->gainedExp = i * 5000;
  1516. return obj;
  1517. };
  1518. oi.setTemplate (Obj::PANDORAS_BOX, 0, terrainType);
  1519. oi.value = i * 6000;;
  1520. oi.probability = 20;
  1521. possibleObjects.push_back (oi);
  1522. }
  1523. //pandora box with creatures
  1524. static const int tierValues[] = {5000, 7000, 9000, 12000, 16000, 21000, 27000};
  1525. for (auto creature : VLC->creh->creatures)
  1526. {
  1527. if (!creature->special && VLC->townh->factions[creature->faction]->nativeTerrain == terrainType)
  1528. {
  1529. int actualTier = creature->level > 7 ? 6 : creature->level-1;
  1530. int creaturesAmount = tierValues[actualTier] / creature->AIValue;
  1531. if (creaturesAmount <= 5)
  1532. {
  1533. }
  1534. else if (creaturesAmount <= 12)
  1535. {
  1536. (creaturesAmount /= 2) *= 2;
  1537. }
  1538. else if (creaturesAmount <= 50)
  1539. {
  1540. creaturesAmount = boost::math::round((float)creaturesAmount / 5) * 5;
  1541. }
  1542. else if (creaturesAmount <= 12)
  1543. {
  1544. creaturesAmount = boost::math::round((float)creaturesAmount / 10) * 10;
  1545. }
  1546. oi.generateObject = [creature, creaturesAmount]() -> CGObjectInstance *
  1547. {
  1548. auto obj = new CGPandoraBox();
  1549. obj->ID = Obj::PANDORAS_BOX;
  1550. obj->subID = 0;
  1551. auto stack = new CStackInstance(creature, creaturesAmount);
  1552. obj->creatures.putStack(SlotID(0), stack);
  1553. return obj;
  1554. };
  1555. oi.setTemplate (Obj::PANDORAS_BOX, 0, terrainType);
  1556. oi.value = (2 * (creature->AIValue) * creaturesAmount * (1 + (float)(gen->getZoneCount(creature->faction)) / gen->getTotalZoneCount()))/3; //TODO: count number of towns on the map
  1557. oi.probability = 3;
  1558. possibleObjects.push_back (oi);
  1559. }
  1560. }
  1561. //Pandora with 12 spells of certain level
  1562. for (int i = 1; i <= GameConstants::SPELL_LEVELS; i++)
  1563. {
  1564. oi.generateObject = [i, gen]() -> CGObjectInstance *
  1565. {
  1566. auto obj = new CGPandoraBox();
  1567. obj->ID = Obj::PANDORAS_BOX;
  1568. obj->subID = 0;
  1569. std::vector <CSpell *> spells;
  1570. for (auto spell : VLC->spellh->objects)
  1571. {
  1572. if (!spell->isSpecialSpell() && spell->level == i)
  1573. spells.push_back(spell);
  1574. }
  1575. RandomGeneratorUtil::randomShuffle(spells, gen->rand);
  1576. for (int j = 0; j < std::min<int>(12, spells.size()); j++)
  1577. {
  1578. obj->spells.push_back (spells[j]->id);
  1579. }
  1580. return obj;
  1581. };
  1582. oi.setTemplate (Obj::PANDORAS_BOX, 0, terrainType);
  1583. oi.value = (i + 1) * 2500; //5000 - 15000
  1584. oi.probability = 2;
  1585. possibleObjects.push_back (oi);
  1586. }
  1587. //Pandora with 15 spells of certain school
  1588. for (int i = 1; i <= 4; i++)
  1589. {
  1590. oi.generateObject = [i, gen]() -> CGObjectInstance *
  1591. {
  1592. auto obj = new CGPandoraBox();
  1593. obj->ID = Obj::PANDORAS_BOX;
  1594. obj->subID = 0;
  1595. std::vector <CSpell *> spells;
  1596. for (auto spell : VLC->spellh->objects)
  1597. {
  1598. if (!spell->isSpecialSpell())
  1599. {
  1600. bool school = false; //TODO: we could have better interface for iterating schools
  1601. switch (i)
  1602. {
  1603. case 1:
  1604. school = spell->air;
  1605. case 2:
  1606. school = spell->earth;
  1607. case 3:
  1608. school = spell->fire;
  1609. case 4:
  1610. school = spell->water;
  1611. }
  1612. if (school)
  1613. spells.push_back(spell);
  1614. }
  1615. }
  1616. RandomGeneratorUtil::randomShuffle(spells, gen->rand);
  1617. for (int j = 0; j < std::min<int>(15, spells.size()); j++)
  1618. {
  1619. obj->spells.push_back (spells[j]->id);
  1620. }
  1621. return obj;
  1622. };
  1623. oi.setTemplate (Obj::PANDORAS_BOX, 0, terrainType);
  1624. oi.value = 15000;
  1625. oi.probability = 2;
  1626. possibleObjects.push_back (oi);
  1627. }
  1628. // Pandora box with 60 random spells
  1629. oi.generateObject = [gen]() -> CGObjectInstance *
  1630. {
  1631. auto obj = new CGPandoraBox();
  1632. obj->ID = Obj::PANDORAS_BOX;
  1633. obj->subID = 0;
  1634. std::vector <CSpell *> spells;
  1635. for (auto spell : VLC->spellh->objects)
  1636. {
  1637. if (!spell->isSpecialSpell())
  1638. spells.push_back(spell);
  1639. }
  1640. RandomGeneratorUtil::randomShuffle(spells, gen->rand);
  1641. for (int j = 0; j < std::min<int>(60, spells.size()); j++)
  1642. {
  1643. obj->spells.push_back (spells[j]->id);
  1644. }
  1645. return obj;
  1646. };
  1647. oi.setTemplate (Obj::PANDORAS_BOX, 0, terrainType);
  1648. oi.value = 3000;
  1649. oi.probability = 2;
  1650. possibleObjects.push_back (oi);
  1651. }
  1652. void ObjectInfo::setTemplate (si32 type, si32 subtype, ETerrainType terrainType)
  1653. {
  1654. templ = VLC->objtypeh->getHandlerFor(type, subtype)->getTemplates(terrainType).front();
  1655. }