CRmgTemplateZone.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  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/CObjectClassesHandler.h"
  19. #include "../mapObjects/CGPandoraBox.h"
  20. #include "../mapObjects/CRewardableObject.h"
  21. class CMap;
  22. class CMapEditManager;
  23. CRmgTemplateZone::CTownInfo::CTownInfo() : townCount(0), castleCount(0), townDensity(0), castleDensity(0)
  24. {
  25. }
  26. int CRmgTemplateZone::CTownInfo::getTownCount() const
  27. {
  28. return townCount;
  29. }
  30. void CRmgTemplateZone::CTownInfo::setTownCount(int value)
  31. {
  32. if(value < 0)
  33. throw rmgException("Negative value for town count not allowed.");
  34. townCount = value;
  35. }
  36. int CRmgTemplateZone::CTownInfo::getCastleCount() const
  37. {
  38. return castleCount;
  39. }
  40. void CRmgTemplateZone::CTownInfo::setCastleCount(int value)
  41. {
  42. if(value < 0)
  43. throw rmgException("Negative value for castle count not allowed.");
  44. castleCount = value;
  45. }
  46. int CRmgTemplateZone::CTownInfo::getTownDensity() const
  47. {
  48. return townDensity;
  49. }
  50. void CRmgTemplateZone::CTownInfo::setTownDensity(int value)
  51. {
  52. if(value < 0)
  53. throw rmgException("Negative value for town density not allowed.");
  54. townDensity = value;
  55. }
  56. int CRmgTemplateZone::CTownInfo::getCastleDensity() const
  57. {
  58. return castleDensity;
  59. }
  60. void CRmgTemplateZone::CTownInfo::setCastleDensity(int value)
  61. {
  62. if(value < 0)
  63. throw rmgException("Negative value for castle density not allowed.");
  64. castleDensity = value;
  65. }
  66. CTileInfo::CTileInfo():nearestObjectDistance(INT_MAX), terrain(ETerrainType::WRONG)
  67. {
  68. occupied = ETileType::POSSIBLE; //all tiles are initially possible to place objects or passages
  69. }
  70. int CTileInfo::getNearestObjectDistance() const
  71. {
  72. return nearestObjectDistance;
  73. }
  74. void CTileInfo::setNearestObjectDistance(int value)
  75. {
  76. nearestObjectDistance = std::max(0, value); //never negative (or unitialized)
  77. }
  78. bool CTileInfo::shouldBeBlocked() const
  79. {
  80. return occupied == ETileType::BLOCKED;
  81. }
  82. bool CTileInfo::isBlocked() const
  83. {
  84. return occupied == ETileType::BLOCKED || occupied == ETileType::USED;
  85. }
  86. bool CTileInfo::isPossible() const
  87. {
  88. return occupied == ETileType::POSSIBLE;
  89. }
  90. bool CTileInfo::isFree() const
  91. {
  92. return occupied == ETileType::FREE;
  93. }
  94. void CTileInfo::setOccupied(ETileType::ETileType value)
  95. {
  96. occupied = value;
  97. }
  98. ETerrainType CTileInfo::getTerrainType() const
  99. {
  100. return terrain;
  101. }
  102. void CTileInfo::setTerrainType(ETerrainType value)
  103. {
  104. terrain = value;
  105. }
  106. CRmgTemplateZone::CRmgTemplateZone() :
  107. id(0),
  108. type(ETemplateZoneType::PLAYER_START),
  109. size(1),
  110. townsAreSameType(false),
  111. matchTerrainToTown(true),
  112. townType(0),
  113. terrainType (ETerrainType::GRASS),
  114. zoneMonsterStrength(EMonsterStrength::ZONE_NORMAL),
  115. totalDensity(0)
  116. {
  117. townTypes = getDefaultTownTypes();
  118. terrainTypes = getDefaultTerrainTypes();
  119. }
  120. TRmgTemplateZoneId CRmgTemplateZone::getId() const
  121. {
  122. return id;
  123. }
  124. void CRmgTemplateZone::setId(TRmgTemplateZoneId value)
  125. {
  126. if(value <= 0)
  127. throw rmgException(boost::to_string(boost::format("Zone %d id should be greater than 0.") %id));
  128. id = value;
  129. }
  130. ETemplateZoneType::ETemplateZoneType CRmgTemplateZone::getType() const
  131. {
  132. return type;
  133. }
  134. void CRmgTemplateZone::setType(ETemplateZoneType::ETemplateZoneType value)
  135. {
  136. type = value;
  137. }
  138. int CRmgTemplateZone::getSize() const
  139. {
  140. return size;
  141. }
  142. void CRmgTemplateZone::setSize(int value)
  143. {
  144. if(value <= 0)
  145. throw rmgException(boost::to_string(boost::format("Zone %d size needs to be greater than 0.") % id));
  146. size = value;
  147. }
  148. boost::optional<int> CRmgTemplateZone::getOwner() const
  149. {
  150. return owner;
  151. }
  152. void CRmgTemplateZone::setOwner(boost::optional<int> value)
  153. {
  154. if(!(*value >= 0 && *value <= PlayerColor::PLAYER_LIMIT_I))
  155. throw rmgException(boost::to_string(boost::format ("Owner of zone %d has to be in range 0 to max player count.") %id));
  156. owner = value;
  157. }
  158. const CRmgTemplateZone::CTownInfo & CRmgTemplateZone::getPlayerTowns() const
  159. {
  160. return playerTowns;
  161. }
  162. void CRmgTemplateZone::setPlayerTowns(const CTownInfo & value)
  163. {
  164. playerTowns = value;
  165. }
  166. const CRmgTemplateZone::CTownInfo & CRmgTemplateZone::getNeutralTowns() const
  167. {
  168. return neutralTowns;
  169. }
  170. void CRmgTemplateZone::setNeutralTowns(const CTownInfo & value)
  171. {
  172. neutralTowns = value;
  173. }
  174. bool CRmgTemplateZone::getTownsAreSameType() const
  175. {
  176. return townsAreSameType;
  177. }
  178. void CRmgTemplateZone::setTownsAreSameType(bool value)
  179. {
  180. townsAreSameType = value;
  181. }
  182. const std::set<TFaction> & CRmgTemplateZone::getTownTypes() const
  183. {
  184. return townTypes;
  185. }
  186. void CRmgTemplateZone::setTownTypes(const std::set<TFaction> & value)
  187. {
  188. townTypes = value;
  189. }
  190. std::set<TFaction> CRmgTemplateZone::getDefaultTownTypes() const
  191. {
  192. std::set<TFaction> defaultTowns;
  193. auto towns = VLC->townh->getDefaultAllowed();
  194. for(int i = 0; i < towns.size(); ++i)
  195. {
  196. if(towns[i]) defaultTowns.insert(i);
  197. }
  198. return defaultTowns;
  199. }
  200. bool CRmgTemplateZone::getMatchTerrainToTown() const
  201. {
  202. return matchTerrainToTown;
  203. }
  204. void CRmgTemplateZone::setMatchTerrainToTown(bool value)
  205. {
  206. matchTerrainToTown = value;
  207. }
  208. const std::set<ETerrainType> & CRmgTemplateZone::getTerrainTypes() const
  209. {
  210. return terrainTypes;
  211. }
  212. void CRmgTemplateZone::setTerrainTypes(const std::set<ETerrainType> & value)
  213. {
  214. assert(value.find(ETerrainType::WRONG) == value.end() && value.find(ETerrainType::BORDER) == value.end() &&
  215. value.find(ETerrainType::WATER) == value.end() && value.find(ETerrainType::ROCK) == value.end());
  216. terrainTypes = value;
  217. }
  218. std::set<ETerrainType> CRmgTemplateZone::getDefaultTerrainTypes() const
  219. {
  220. std::set<ETerrainType> terTypes;
  221. static const ETerrainType::EETerrainType allowedTerTypes[] = {ETerrainType::DIRT, ETerrainType::SAND, ETerrainType::GRASS, ETerrainType::SNOW,
  222. ETerrainType::SWAMP, ETerrainType::ROUGH, ETerrainType::SUBTERRANEAN, ETerrainType::LAVA};
  223. for (auto & allowedTerType : allowedTerTypes)
  224. terTypes.insert(allowedTerType);
  225. return terTypes;
  226. }
  227. void CRmgTemplateZone::setMinesAmount (TResource res, ui16 amount)
  228. {
  229. mines[res] = amount;
  230. }
  231. std::map<TResource, ui16> CRmgTemplateZone::getMinesInfo() const
  232. {
  233. return mines;
  234. }
  235. void CRmgTemplateZone::addConnection(TRmgTemplateZoneId otherZone)
  236. {
  237. connections.push_back (otherZone);
  238. }
  239. std::vector<TRmgTemplateZoneId> CRmgTemplateZone::getConnections() const
  240. {
  241. return connections;
  242. }
  243. void CRmgTemplateZone::setMonsterStrength (EMonsterStrength::EMonsterStrength val)
  244. {
  245. assert (vstd::iswithin(val, EMonsterStrength::ZONE_WEAK, EMonsterStrength::ZONE_STRONG));
  246. zoneMonsterStrength = val;
  247. }
  248. void CRmgTemplateZone::setTotalDensity (ui16 val)
  249. {
  250. totalDensity = val;
  251. }
  252. ui16 CRmgTemplateZone::getTotalDensity () const
  253. {
  254. return totalDensity;
  255. }
  256. void CRmgTemplateZone::addTreasureInfo(CTreasureInfo & info)
  257. {
  258. treasureInfo.push_back(info);
  259. }
  260. std::vector<CTreasureInfo> CRmgTemplateZone::getTreasureInfo()
  261. {
  262. return treasureInfo;
  263. }
  264. float3 CRmgTemplateZone::getCenter() const
  265. {
  266. return center;
  267. }
  268. void CRmgTemplateZone::setCenter(const float3 &f)
  269. {
  270. //limit boundaries to (0,1) square
  271. center = float3 (std::min(std::max(f.x, 0.f), 1.f), std::min(std::max(f.y, 0.f), 1.f), f.z);
  272. }
  273. bool CRmgTemplateZone::pointIsIn(int x, int y)
  274. {
  275. return true;
  276. }
  277. int3 CRmgTemplateZone::getPos() const
  278. {
  279. return pos;
  280. }
  281. void CRmgTemplateZone::setPos(const int3 &Pos)
  282. {
  283. pos = Pos;
  284. }
  285. void CRmgTemplateZone::addTile (const int3 &pos)
  286. {
  287. tileinfo.insert(pos);
  288. }
  289. std::set<int3> CRmgTemplateZone::getTileInfo () const
  290. {
  291. return tileinfo;
  292. }
  293. void CRmgTemplateZone::createBorder(CMapGenerator* gen)
  294. {
  295. for (auto tile : tileinfo)
  296. {
  297. gen->foreach_neighbour (tile, [this, gen](int3 &pos)
  298. {
  299. if (!vstd::contains(this->tileinfo, pos))
  300. {
  301. gen->foreach_neighbour (pos, [this, gen](int3 &pos)
  302. {
  303. if (gen->isPossible(pos))
  304. gen->setOccupied (pos, ETileType::BLOCKED);
  305. });
  306. }
  307. });
  308. }
  309. }
  310. void CRmgTemplateZone::fractalize(CMapGenerator* gen)
  311. {
  312. std::vector<int3> clearedTiles;
  313. std::set<int3> possibleTiles;
  314. std::set<int3> tilesToClear; //will be set clear
  315. std::set<int3> tilesToIgnore; //will be erased in this iteration
  316. const float minDistance = std::sqrt(totalDensity);
  317. for (auto tile : tileinfo)
  318. {
  319. if (gen->isFree(tile))
  320. clearedTiles.push_back(tile);
  321. else if (gen->isPossible(tile))
  322. possibleTiles.insert(tile);
  323. }
  324. if (clearedTiles.empty()) //this should come from zone connections
  325. {
  326. clearedTiles.push_back(pos); //zone center should be always clear
  327. }
  328. while (possibleTiles.size())
  329. {
  330. for (auto tileToMakePath : possibleTiles)
  331. {
  332. //find closest free tile
  333. float currentDistance = 1e10;
  334. int3 closestTile (-1,-1,-1);
  335. for (auto clearTile : clearedTiles)
  336. {
  337. float distance = tileToMakePath.dist2d(clearTile);
  338. if (distance < currentDistance)
  339. {
  340. currentDistance = distance;
  341. closestTile = clearTile;
  342. }
  343. if (currentDistance <= minDistance)
  344. {
  345. //this tile is close enough. Forget about it and check next one
  346. tilesToIgnore.insert (tileToMakePath);
  347. break;
  348. }
  349. }
  350. //if tiles is not close enough, make path to it
  351. if (currentDistance > minDistance)
  352. {
  353. crunchPath (gen, tileToMakePath, closestTile, id, &tilesToClear);
  354. break; //next iteration - use already cleared tiles
  355. }
  356. }
  357. for (auto tileToClear : tilesToClear)
  358. {
  359. //move cleared tiles from one set to another
  360. clearedTiles.push_back(tileToClear);
  361. vstd::erase_if_present(possibleTiles, tileToClear);
  362. }
  363. for (auto tileToClear : tilesToIgnore)
  364. {
  365. //these tiles are already connected, ignore them
  366. vstd::erase_if_present(possibleTiles, tileToClear);
  367. }
  368. if (tilesToClear.empty()) //nothing else can be done (?)
  369. break;
  370. tilesToClear.clear(); //empty this container
  371. tilesToIgnore.clear();
  372. }
  373. logGlobal->infoStream() << boost::format ("Zone %d subdivided fractally") %id;
  374. }
  375. bool CRmgTemplateZone::crunchPath (CMapGenerator* gen, const int3 &src, const int3 &dst, TRmgTemplateZoneId zone, std::set<int3>* clearedTiles)
  376. {
  377. /*
  378. make shortest path with free tiles, reachning dst or closest already free tile. Avoid blocks.
  379. do not leave zone border
  380. */
  381. bool result = false;
  382. bool end = false;
  383. int3 currentPos = src;
  384. float distance = currentPos.dist2dSQ (dst);
  385. while (!end)
  386. {
  387. if (currentPos == dst)
  388. {
  389. result = true;
  390. break;
  391. }
  392. auto lastDistance = distance;
  393. gen->foreach_neighbour (currentPos, [this, gen, &currentPos, dst, &distance, &result, &end, clearedTiles](int3 &pos)
  394. {
  395. if (!result) //not sure if lambda is worth it...
  396. {
  397. if (pos == dst)
  398. {
  399. result = true;
  400. end = true;
  401. }
  402. if (pos.dist2dSQ (dst) < distance)
  403. {
  404. if (!gen->isBlocked(pos))
  405. {
  406. if (vstd::contains (tileinfo, pos))
  407. {
  408. if (gen->isPossible(pos))
  409. {
  410. gen->setOccupied (pos, ETileType::FREE);
  411. if (clearedTiles)
  412. clearedTiles->insert(pos);
  413. currentPos = pos;
  414. distance = currentPos.dist2dSQ (dst);
  415. }
  416. else if (gen->isFree(pos))
  417. {
  418. end = true;
  419. result = true;
  420. }
  421. else
  422. throw rmgException(boost::to_string(boost::format("Tile %s of uknown type found on path") % pos()));
  423. }
  424. }
  425. }
  426. }
  427. });
  428. if (!(result || distance < lastDistance)) //we do not advance, use more avdnaced pathfinding algorithm?
  429. {
  430. logGlobal->warnStream() << boost::format ("No tile closer than %s found on path from %s to %s") %currentPos %src %dst;
  431. break;
  432. }
  433. }
  434. return result;
  435. }
  436. void CRmgTemplateZone::addRequiredObject(CGObjectInstance * obj, si32 strength)
  437. {
  438. requiredObjects.push_back(std::make_pair(obj, strength));
  439. }
  440. bool CRmgTemplateZone::addMonster(CMapGenerator* gen, int3 &pos, si32 strength)
  441. {
  442. //precalculate actual (randomized) monster strength based on this post
  443. //http://forum.vcmi.eu/viewtopic.php?p=12426#12426
  444. int mapMonsterStrength = gen->mapGenOptions->getMonsterStrength();
  445. int monsterStrength = zoneMonsterStrength + mapMonsterStrength - 1; //array index from 0 to 4
  446. static const int value1[] = {2500, 1500, 1000, 500, 0};
  447. static const int value2[] = {7500, 7500, 7500, 5000, 5000};
  448. static const float multiplier1[] = {0.5, 0.75, 1.0, 1.5, 1.5};
  449. static const float multiplier2[] = {0.5, 0.75, 1.0, 1.0, 1.5};
  450. int strength1 = std::max(0.f, (strength - value1[monsterStrength]) * multiplier1[monsterStrength]);
  451. int strength2 = std::max(0.f, (strength - value2[monsterStrength]) * multiplier2[monsterStrength]);
  452. strength = strength1 + strength2;
  453. if (strength < 2000)
  454. return false; //no guard at all
  455. CreatureID creId = CreatureID::NONE;
  456. int amount = 0;
  457. std::vector<CreatureID> possibleCreatures;
  458. for (auto cre : VLC->creh->creatures)
  459. {
  460. if (cre->special)
  461. continue;
  462. 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
  463. {
  464. possibleCreatures.push_back(cre->idNumber);
  465. }
  466. }
  467. if (possibleCreatures.size())
  468. {
  469. creId = *RandomGeneratorUtil::nextItem(possibleCreatures, gen->rand);
  470. amount = strength / VLC->creh->creatures[creId]->AIValue;
  471. if (amount >= 4)
  472. amount *= gen->rand.nextDouble(0.75, 1.25);
  473. }
  474. else //just pick any available creature
  475. {
  476. creId = CreatureID(132); //Azure Dragon
  477. amount = strength / VLC->creh->creatures[creId]->AIValue;
  478. }
  479. auto guard = new CGCreature();
  480. guard->ID = Obj::MONSTER;
  481. guard->subID = creId;
  482. auto hlp = new CStackInstance(creId, amount);
  483. //will be set during initialization
  484. guard->putStack(SlotID(0), hlp);
  485. placeObject(gen, guard, pos);
  486. return true;
  487. }
  488. bool CRmgTemplateZone::createTreasurePile (CMapGenerator* gen, int3 &pos)
  489. {
  490. std::map<int3, CGObjectInstance *> treasures;
  491. std::set<int3> boundary;
  492. int3 guardPos (-1,-1,-1);
  493. int3 nextTreasurePos = pos;
  494. //default values
  495. int maxValue = 5000;
  496. int minValue = 1500;
  497. if (treasureInfo.size())
  498. {
  499. //roulette wheel
  500. int r = gen->rand.nextInt (1, totalDensity);
  501. for (auto t : treasureInfo)
  502. {
  503. if (r <= t.threshold)
  504. {
  505. maxValue = t.max;
  506. minValue = t.min;
  507. break;
  508. }
  509. }
  510. }
  511. int currentValue = 0;
  512. CGObjectInstance * object = nullptr;
  513. while (currentValue < minValue)
  514. {
  515. //TODO: this works only for 1-tile objects
  516. //make sure our shape is consistent
  517. treasures[nextTreasurePos] = nullptr;
  518. for (auto treasurePos : treasures)
  519. {
  520. gen->foreach_neighbour (treasurePos.first, [gen, &boundary](int3 pos)
  521. {
  522. boundary.insert(pos);
  523. });
  524. }
  525. for (auto treasurePos : treasures)
  526. {
  527. //leaving only boundary around objects
  528. vstd::erase_if_present (boundary, treasurePos.first);
  529. }
  530. for (auto tile : boundary)
  531. {
  532. //we can't extend boundary anymore
  533. if (!(gen->isBlocked(tile) || gen->isPossible(tile)))
  534. break;
  535. }
  536. int remaining = maxValue - currentValue;
  537. auto oi = getRandomObject(gen, remaining);
  538. object = oi.generateObject();
  539. if (!object)
  540. break;
  541. currentValue += oi.value;
  542. treasures[nextTreasurePos] = object;
  543. //now find place for next object
  544. int3 placeFound(-1,-1,-1);
  545. //FIXME: find out why teh following code causes crashes
  546. //std::vector<int3> boundaryVec(boundary.begin(), boundary.end());
  547. //RandomGeneratorUtil::randomShuffle(boundaryVec, gen->rand);
  548. //for (auto tile : boundaryVec)
  549. for (auto tile : boundary)
  550. {
  551. if (gen->isPossible(tile)) //we can place new treasure only on possible tile
  552. {
  553. bool here = true;
  554. gen->foreach_neighbour (tile, [gen, &here](int3 pos)
  555. {
  556. if (!(gen->isBlocked(pos) || gen->isPossible(pos)))
  557. here = false;
  558. });
  559. if (here)
  560. {
  561. placeFound = tile;
  562. break;
  563. }
  564. }
  565. }
  566. if (placeFound.valid())
  567. nextTreasurePos = placeFound;
  568. }
  569. if (treasures.size())
  570. {
  571. //find object closest to zone center, then con nect it to the middle of the zone
  572. int3 zoneCenter = getPos();
  573. int3 closestTile = int3(-1,-1,-1);
  574. float minDistance = 1e10;
  575. for (auto treasure : treasures)
  576. {
  577. if (zoneCenter.dist2d(treasure.first) < minDistance)
  578. {
  579. closestTile = treasure.first;
  580. minDistance = zoneCenter.dist2d(treasure.first);
  581. }
  582. }
  583. assert (closestTile.valid());
  584. if (!crunchPath (gen, closestTile, getPos(), id)) //make sure pile is connected to the middle of zone
  585. {
  586. for (auto treasure : treasures)
  587. {
  588. if (gen->isPossible(treasure.first))
  589. gen->setOccupied (treasure.first, ETileType::BLOCKED);
  590. }
  591. return true;
  592. }
  593. for (auto tile : boundary) //guard must be standing there
  594. {
  595. if (gen->isFree(tile)) //this tile could be already blocked, don't place a monster here
  596. {
  597. guardPos = tile;
  598. break;
  599. }
  600. }
  601. if (guardPos.valid())
  602. {
  603. for (auto treasure : treasures)
  604. {
  605. placeObject(gen, treasure.second, treasure.first - treasure.second->getVisitableOffset());
  606. }
  607. if (addMonster(gen, guardPos, currentValue))
  608. {//block only if the object is guarded
  609. for (auto tile : boundary)
  610. {
  611. if (gen->isPossible(tile))
  612. gen->setOccupied (tile, ETileType::BLOCKED);
  613. }
  614. }
  615. }
  616. else //we couldn't make a connection to this location, block it
  617. {
  618. for (auto treasure : treasures)
  619. {
  620. if (gen->isPossible(treasure.first))
  621. gen->setOccupied (treasure.first, ETileType::BLOCKED);
  622. }
  623. }
  624. return true;
  625. }
  626. else //we did not place eveyrthing successfully
  627. return false;
  628. }
  629. void CRmgTemplateZone::initTownType (CMapGenerator* gen)
  630. {
  631. //FIXME: handle case that this player is not present -> towns should be set to neutral
  632. int totalTowns = 0;
  633. if ((type == ETemplateZoneType::CPU_START) || (type == ETemplateZoneType::PLAYER_START))
  634. {
  635. //set zone types to player faction, generate main town
  636. logGlobal->infoStream() << "Preparing playing zone";
  637. int player_id = *owner - 1;
  638. auto & playerInfo = gen->map->players[player_id];
  639. if (playerInfo.canAnyonePlay())
  640. {
  641. PlayerColor player(player_id);
  642. townType = gen->mapGenOptions->getPlayersSettings().find(player)->second.getStartingTown();
  643. if(townType == CMapGenOptions::CPlayerSettings::RANDOM_TOWN)
  644. townType = *RandomGeneratorUtil::nextItem(VLC->townh->getAllowedFactions(), gen->rand); // all possible towns, skip neutral
  645. auto town = new CGTownInstance();
  646. town->ID = Obj::TOWN;
  647. town->subID = townType;
  648. town->tempOwner = player;
  649. town->builtBuildings.insert(BuildingID::FORT);
  650. town->builtBuildings.insert(BuildingID::DEFAULT);
  651. placeObject(gen, town, getPos() + town->getVisitableOffset()); //towns are big objects and should be centered around visitable position
  652. totalTowns++;
  653. logGlobal->traceStream() << "Fill player info " << player_id;
  654. // Update player info
  655. playerInfo.allowedFactions.clear();
  656. playerInfo.allowedFactions.insert (townType);
  657. playerInfo.hasMainTown = true;
  658. playerInfo.posOfMainTown = town->pos - int3(2, 0, 0);
  659. playerInfo.generateHeroAtMainTown = true;
  660. //now create actual towns
  661. for (int i = 1; i < playerTowns.getCastleCount(); i++)
  662. {
  663. auto town = new CGTownInstance();
  664. town->ID = Obj::TOWN;
  665. if (townsAreSameType)
  666. town->subID = townType;
  667. else
  668. town->subID = *RandomGeneratorUtil::nextItem(VLC->townh->getAllowedFactions(), gen->rand); //TODO: check allowed town types for this zone
  669. town->tempOwner = player;
  670. town->builtBuildings.insert(BuildingID::FORT);
  671. town->builtBuildings.insert(BuildingID::DEFAULT);
  672. addRequiredObject (town);
  673. totalTowns++;
  674. }
  675. for (int i = 0; i < playerTowns.getTownCount(); i++)
  676. {
  677. auto town = new CGTownInstance();
  678. town->ID = Obj::TOWN;
  679. if (townsAreSameType)
  680. town->subID = townType;
  681. else
  682. town->subID = *RandomGeneratorUtil::nextItem(VLC->townh->getAllowedFactions(), gen->rand); //TODO: check allowed town types for this zone
  683. town->tempOwner = player;
  684. town->builtBuildings.insert(BuildingID::DEFAULT);
  685. addRequiredObject (town);
  686. totalTowns++;
  687. }
  688. //requiredObjects.push_back(town);
  689. }
  690. else
  691. {
  692. type = ETemplateZoneType::TREASURE;
  693. townType = *RandomGeneratorUtil::nextItem(VLC->townh->getAllowedFactions(), gen->rand);
  694. logGlobal->infoStream() << "Skipping this zone cause no player";
  695. }
  696. }
  697. else //no player
  698. {
  699. townType = *RandomGeneratorUtil::nextItem(VLC->townh->getAllowedFactions(), gen->rand);
  700. }
  701. for (int i = 0; i < neutralTowns.getCastleCount(); i++)
  702. {
  703. auto town = new CGTownInstance();
  704. town->ID = Obj::TOWN;
  705. if (townsAreSameType || totalTowns == 0) //first town must match zone type
  706. town->subID = townType;
  707. else
  708. town->subID = *RandomGeneratorUtil::nextItem(VLC->townh->getAllowedFactions(), gen->rand); //TODO: check allowed town types for this zone
  709. town->tempOwner = PlayerColor::NEUTRAL;
  710. town->builtBuildings.insert(BuildingID::FORT);
  711. town->builtBuildings.insert(BuildingID::DEFAULT);
  712. if (!totalTowns) //first town in zone goes in the middle
  713. placeObject(gen, town, getPos() + town->getVisitableOffset());
  714. else
  715. addRequiredObject (town);
  716. totalTowns++;
  717. }
  718. for (int i = 0; i < neutralTowns.getTownCount(); i++)
  719. {
  720. auto town = new CGTownInstance();
  721. town->ID = Obj::TOWN;
  722. if (townsAreSameType || totalTowns == 0)
  723. town->subID = townType;
  724. else
  725. town->subID = *RandomGeneratorUtil::nextItem(VLC->townh->getAllowedFactions(), gen->rand); //TODO: check allowed town types for this zone
  726. town->tempOwner = PlayerColor::NEUTRAL;
  727. town->builtBuildings.insert(BuildingID::DEFAULT);
  728. if (!totalTowns)
  729. placeObject(gen, town, getPos() + town->getVisitableOffset());
  730. else
  731. addRequiredObject (town);
  732. totalTowns++;
  733. }
  734. }
  735. void CRmgTemplateZone::initTerrainType (CMapGenerator* gen)
  736. {
  737. if (matchTerrainToTown)
  738. terrainType = VLC->townh->factions[townType]->nativeTerrain;
  739. else
  740. terrainType = *RandomGeneratorUtil::nextItem(terrainTypes, gen->rand);
  741. //paint zone with matching terrain
  742. std::vector<int3> tiles;
  743. for (auto tile : tileinfo)
  744. {
  745. tiles.push_back (tile);
  746. }
  747. gen->editManager->getTerrainSelection().setSelection(tiles);
  748. gen->editManager->drawTerrain(terrainType, &gen->rand);
  749. }
  750. bool CRmgTemplateZone::placeMines (CMapGenerator* gen)
  751. {
  752. std::vector<Res::ERes> required_mines;
  753. required_mines.push_back(Res::ERes::WOOD);
  754. required_mines.push_back(Res::ERes::ORE);
  755. static const Res::ERes woodOre[] = {Res::ERes::WOOD, Res::ERes::ORE};
  756. static const Res::ERes preciousResources[] = {Res::ERes::GEMS, Res::ERes::CRYSTAL, Res::ERes::MERCURY, Res::ERes::SULFUR};
  757. //TODO: factory / copy constructor?
  758. for (const auto & res : woodOre)
  759. {
  760. //TODO: these 2 should be close to town (within 12 tiles radius)
  761. for (int i = 0; i < mines[res]; i++)
  762. {
  763. auto mine = new CGMine();
  764. mine->ID = Obj::MINE;
  765. mine->subID = static_cast<si32>(res);
  766. mine->producedResource = res;
  767. mine->producedQuantity = mine->defaultResProduction();
  768. addRequiredObject(mine, 1500);
  769. }
  770. }
  771. for (const auto & res : preciousResources)
  772. {
  773. for (int i = 0; i < mines[res]; i++)
  774. {
  775. auto mine = new CGMine();
  776. mine->ID = Obj::MINE;
  777. mine->subID = static_cast<si32>(res);
  778. mine->producedResource = res;
  779. mine->producedQuantity = mine->defaultResProduction();
  780. addRequiredObject(mine, 3500);
  781. }
  782. }
  783. for (int i = 0; i < mines[Res::GOLD]; i++)
  784. {
  785. auto mine = new CGMine();
  786. mine->ID = Obj::MINE;
  787. mine->subID = static_cast<si32>(Res::GOLD);
  788. mine->producedResource = Res::GOLD;
  789. mine->producedQuantity = mine->defaultResProduction();
  790. addRequiredObject(mine, 7000);
  791. }
  792. return true;
  793. }
  794. bool CRmgTemplateZone::createRequiredObjects(CMapGenerator* gen)
  795. {
  796. logGlobal->infoStream() << "Creating required objects";
  797. for(const auto &obj : requiredObjects)
  798. {
  799. int3 pos;
  800. logGlobal->traceStream() << "Looking for place";
  801. if ( ! findPlaceForObject(gen, obj.first, 3, pos))
  802. {
  803. logGlobal->errorStream() << boost::format("Failed to fill zone %d due to lack of space") %id;
  804. //TODO CLEANUP!
  805. return false;
  806. }
  807. logGlobal->traceStream() << "Place found";
  808. placeObject(gen, obj.first, pos);
  809. guardObject (gen, obj.first, obj.second);
  810. }
  811. return true;
  812. }
  813. void CRmgTemplateZone::createTreasures(CMapGenerator* gen)
  814. {
  815. const double minDistance = 3;
  816. do {
  817. int3 pos;
  818. if ( ! findPlaceForTreasurePile(gen, minDistance, pos))
  819. {
  820. break;
  821. }
  822. createTreasurePile (gen, pos);
  823. } while(true);
  824. }
  825. void CRmgTemplateZone::createObstacles(CMapGenerator* gen)
  826. {
  827. auto sel = gen->editManager->getTerrainSelection();
  828. sel.clearSelection();
  829. for (auto tile : tileinfo)
  830. {
  831. //test code - block all the map to show paths clearly
  832. //if (gen->isPossible(tile))
  833. // gen->setOccupied(tile, ETileType::BLOCKED);
  834. if (gen->shouldBeBlocked(tile)) //fill tiles that should be blocked with obstacles
  835. {
  836. auto obj = new CGObjectInstance();
  837. obj->ID = static_cast<Obj>(130);
  838. obj->subID = 0;
  839. placeObject(gen, obj, tile);
  840. }
  841. }
  842. }
  843. bool CRmgTemplateZone::fill(CMapGenerator* gen)
  844. {
  845. addAllPossibleObjects (gen);
  846. initTownType(gen);
  847. initTerrainType(gen);
  848. placeMines(gen);
  849. createRequiredObjects(gen);
  850. fractalize(gen); //after required objects are created and linked with their own paths
  851. createTreasures(gen);
  852. createObstacles(gen);
  853. logGlobal->infoStream() << boost::format ("Zone %d filled successfully") %id;
  854. return true;
  855. }
  856. bool CRmgTemplateZone::findPlaceForTreasurePile(CMapGenerator* gen, si32 min_dist, int3 &pos)
  857. {
  858. //si32 min_dist = sqrt(tileinfo.size()/density);
  859. int best_distance = 0;
  860. bool result = false;
  861. //logGlobal->infoStream() << boost::format("Min dist for density %f is %d") % density % min_dist;
  862. for(auto tile : tileinfo)
  863. {
  864. auto dist = gen->getTile(tile).getNearestObjectDistance();
  865. if (gen->isPossible(tile) && (dist >= min_dist) && (dist > best_distance))
  866. {
  867. bool allTilesAvailable = true;
  868. gen->foreach_neighbour (tile, [&gen, &allTilesAvailable](int3 neighbour)
  869. {
  870. if (!(gen->isPossible(neighbour) || gen->isBlocked(neighbour)))
  871. {
  872. allTilesAvailable = false; //all present tiles must be already blocked or ready for new objects
  873. }
  874. });
  875. if (allTilesAvailable)
  876. {
  877. best_distance = dist;
  878. pos = tile;
  879. result = true;
  880. }
  881. }
  882. }
  883. if (result)
  884. {
  885. gen->setOccupied(pos, ETileType::BLOCKED); //block that tile
  886. }
  887. return result;
  888. }
  889. bool CRmgTemplateZone::findPlaceForObject(CMapGenerator* gen, CGObjectInstance* obj, si32 min_dist, int3 &pos)
  890. {
  891. //we need object apperance to deduce free tiles
  892. if (obj->appearance.id == Obj::NO_OBJ)
  893. {
  894. auto templates = VLC->objtypeh->getHandlerFor(obj->ID, obj->subID)->getTemplates(gen->map->getTile(getPos()).terType);
  895. if (templates.empty())
  896. throw rmgException(boost::to_string(boost::format("Did not find graphics for object (%d,%d) at %s") %obj->ID %obj->subID %pos));
  897. obj->appearance = templates.front();
  898. }
  899. //si32 min_dist = sqrt(tileinfo.size()/density);
  900. int best_distance = 0;
  901. bool result = false;
  902. //si32 w = gen->map->width;
  903. //si32 h = gen->map->height;
  904. //logGlobal->infoStream() << boost::format("Min dist for density %f is %d") % density % min_dist;
  905. auto tilesBlockedByObject = obj->getBlockedOffsets();
  906. for (auto tile : tileinfo)
  907. {
  908. //object must be accessible from at least one surounding tile
  909. bool accessible = false;
  910. for (int x = -1; x < 2; x++)
  911. for (int y = -1; y <2; y++)
  912. {
  913. if (x && y) //check only if object is visitable from another tile
  914. {
  915. int3 offset = obj->getVisitableOffset() + int3(x, y, 0);
  916. if (!vstd::contains(tilesBlockedByObject, offset))
  917. {
  918. int3 nearbyPos = tile + offset;
  919. if (gen->map->isInTheMap(nearbyPos))
  920. {
  921. if (obj->appearance.isVisitableFrom(x, y) && !gen->isBlocked(nearbyPos))
  922. accessible = true;
  923. }
  924. }
  925. }
  926. };
  927. if (!accessible)
  928. continue;
  929. auto ti = gen->getTile(tile);
  930. auto dist = ti.getNearestObjectDistance();
  931. //avoid borders
  932. if (gen->isPossible(tile) && (dist >= min_dist) && (dist > best_distance))
  933. {
  934. bool allTilesAvailable = true;
  935. for (auto blockingTile : tilesBlockedByObject)
  936. {
  937. int3 t = tile + blockingTile;
  938. if (!gen->map->isInTheMap(t) || !gen->isPossible(t))
  939. {
  940. allTilesAvailable = false; //if at least one tile is not possible, object can't be placed here
  941. break;
  942. }
  943. }
  944. if (allTilesAvailable)
  945. {
  946. best_distance = dist;
  947. pos = tile;
  948. result = true;
  949. }
  950. }
  951. }
  952. if (result)
  953. {
  954. gen->setOccupied(pos, ETileType::BLOCKED); //block that tile
  955. }
  956. return result;
  957. }
  958. void CRmgTemplateZone::checkAndPlaceObject(CMapGenerator* gen, CGObjectInstance* object, const int3 &pos)
  959. {
  960. if (!gen->map->isInTheMap(pos))
  961. throw rmgException(boost::to_string(boost::format("Position of object %d at %s is outside the map") % object->id % object->pos()));
  962. object->pos = pos;
  963. if (object->isVisitable() && !gen->map->isInTheMap(object->visitablePos()))
  964. 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()));
  965. for (auto tile : object->getBlockedPos())
  966. {
  967. if (!gen->map->isInTheMap(tile))
  968. throw rmgException(boost::to_string(boost::format("Tile %s of object %d at %s is outside the map") % tile() % object->id % object->pos()));
  969. }
  970. if (object->appearance.id == Obj::NO_OBJ)
  971. {
  972. auto templates = VLC->objtypeh->getHandlerFor(object->ID, object->subID)->getTemplates(gen->map->getTile(pos).terType);
  973. if (templates.empty())
  974. throw rmgException(boost::to_string(boost::format("Did not find graphics for object (%d,%d) at %s") %object->ID %object->subID %pos));
  975. object->appearance = templates.front();
  976. }
  977. gen->map->addBlockVisTiles(object);
  978. gen->editManager->insertObject(object, pos);
  979. //logGlobal->traceStream() << boost::format ("Successfully inserted object (%d,%d) at pos %s") %object->ID %object->subID %pos();
  980. }
  981. void CRmgTemplateZone::placeObject(CMapGenerator* gen, CGObjectInstance* object, const int3 &pos)
  982. {
  983. //logGlobal->traceStream() << boost::format("Inserting object at %d %d") % pos.x % pos.y;
  984. checkAndPlaceObject (gen, object, pos);
  985. auto points = object->getBlockedPos();
  986. if (object->isVisitable())
  987. points.insert(pos + object->getVisitableOffset());
  988. points.insert(pos);
  989. for(auto p : points)
  990. {
  991. if (gen->map->isInTheMap(p))
  992. {
  993. gen->setOccupied(p, ETileType::USED);
  994. }
  995. }
  996. for(auto tile : tileinfo)
  997. {
  998. si32 d = pos.dist2d(tile);
  999. gen->setNearestObjectDistance(tile, std::min(d, gen->getNearestObjectDistance(tile)));
  1000. }
  1001. }
  1002. std::vector<int3> CRmgTemplateZone::getAccessibleOffsets (CMapGenerator* gen, CGObjectInstance* object)
  1003. {
  1004. //get all tiles from which this object can be accessed
  1005. int3 visitable = object->visitablePos();
  1006. std::vector<int3> tiles;
  1007. auto tilesBlockedByObject = object->getBlockedPos(); //absolue value, as object is already placed
  1008. gen->foreach_neighbour(visitable, [&](int3& pos)
  1009. {
  1010. if (gen->isPossible(pos))
  1011. {
  1012. if (!vstd::contains(tilesBlockedByObject, pos))
  1013. {
  1014. if (object->appearance.isVisitableFrom(pos.x - visitable.x, pos.y - visitable.y) && !gen->isBlocked(pos)) //TODO: refactor - info about visitability from absolute coordinates
  1015. {
  1016. tiles.push_back(pos);
  1017. }
  1018. }
  1019. };
  1020. });
  1021. return tiles;
  1022. }
  1023. bool CRmgTemplateZone::guardObject(CMapGenerator* gen, CGObjectInstance* object, si32 str)
  1024. {
  1025. logGlobal->traceStream() << boost::format("Guard object at %s") % object->pos();
  1026. std::vector<int3> tiles = getAccessibleOffsets (gen, object);
  1027. int3 guardTile(-1,-1,-1);
  1028. for (auto tile : tiles)
  1029. {
  1030. //crunching path may fail if center of teh zone is dirrectly over wide object
  1031. if (crunchPath (gen, tile, getPos(), id)) //make sure object is accessible before surrounding it with blocked tiles
  1032. {
  1033. guardTile = tile;
  1034. break;
  1035. }
  1036. }
  1037. if (!guardTile.valid())
  1038. {
  1039. logGlobal->errorStream() << boost::format("Failed to crunch path to object at %s") % object->pos();
  1040. return false;
  1041. }
  1042. if (addMonster (gen, guardTile, str)) //do not place obstacles around unguarded object
  1043. {
  1044. for (auto pos : tiles)
  1045. {
  1046. if (!gen->isFree(pos))
  1047. gen->setOccupied(pos, ETileType::BLOCKED);
  1048. }
  1049. gen->setOccupied (guardTile, ETileType::USED);
  1050. }
  1051. else
  1052. gen->setOccupied (guardTile, ETileType::FREE);
  1053. return true;
  1054. }
  1055. ObjectInfo CRmgTemplateZone::getRandomObject (CMapGenerator* gen, ui32 value)
  1056. {
  1057. std::vector<std::pair<ui32, ObjectInfo>> tresholds;
  1058. ui32 total = 0;
  1059. ui32 minValue = 0.25f * value;
  1060. //roulette wheel
  1061. for (auto oi : possibleObjects)
  1062. {
  1063. if (oi.value >= minValue && oi.value <= value)
  1064. {
  1065. //TODO: check place for non-removable object
  1066. //problem: we need at least template for the object that does not yet exist
  1067. total += oi.probability;
  1068. tresholds.push_back (std::make_pair (total, oi));
  1069. }
  1070. }
  1071. //TODO: generate pandora box with gold if the value is very high
  1072. if (tresholds.empty())
  1073. {
  1074. ObjectInfo oi;
  1075. if (minValue > 20000) //we don't have object valuable enough
  1076. {
  1077. oi.generateObject = [minValue]() -> CGObjectInstance *
  1078. {
  1079. auto obj = new CGPandoraBox();
  1080. obj->ID = Obj::PANDORAS_BOX;
  1081. obj->subID = 0;
  1082. obj->resources[Res::GOLD] = minValue;
  1083. return obj;
  1084. };
  1085. oi.value = minValue;
  1086. oi.probability = 0;
  1087. }
  1088. else
  1089. {
  1090. oi.generateObject = [gen]() -> CGObjectInstance *
  1091. {
  1092. return nullptr;
  1093. };
  1094. oi.value = 0;
  1095. oi.probability = 0;
  1096. }
  1097. return oi;
  1098. }
  1099. int r = gen->rand.nextInt (1, total);
  1100. for (auto t : tresholds)
  1101. {
  1102. if (r <= t.first)
  1103. return t.second;
  1104. }
  1105. //FIXME: control reaches end of non-void function. Missing return?
  1106. }
  1107. void CRmgTemplateZone::addAllPossibleObjects (CMapGenerator* gen)
  1108. {
  1109. //TODO: move typical objects to config
  1110. ObjectInfo oi;
  1111. static const Res::ERes preciousRes[] = {Res::ERes::CRYSTAL, Res::ERes::GEMS, Res::ERes::MERCURY, Res::ERes::SULFUR};
  1112. for (int i = 0; i < 4; i++)
  1113. {
  1114. oi.generateObject = [i, gen]() -> CGObjectInstance *
  1115. {
  1116. auto obj = new CGResource();
  1117. obj->ID = Obj::RESOURCE;
  1118. obj->subID = static_cast<si32>(preciousRes[i]);
  1119. obj->amount = 0;
  1120. return obj;
  1121. };
  1122. oi.value = 1400;
  1123. oi.probability = 300;
  1124. possibleObjects.push_back (oi);
  1125. }
  1126. static const Res::ERes woodOre[] = {Res::ERes::WOOD, Res::ERes::ORE};
  1127. for (int i = 0; i < 2; i++)
  1128. {
  1129. oi.generateObject = [i, gen]() -> CGObjectInstance *
  1130. {
  1131. auto obj = new CGResource();
  1132. obj->ID = Obj::RESOURCE;
  1133. obj->subID = static_cast<si32>(woodOre[i]);
  1134. obj->amount = 0;
  1135. return obj;
  1136. };
  1137. oi.value = 1400;
  1138. oi.probability = 300;
  1139. possibleObjects.push_back (oi);
  1140. }
  1141. oi.generateObject = [gen]() -> CGObjectInstance *
  1142. {
  1143. auto obj = new CGResource();
  1144. obj->ID = Obj::RESOURCE;
  1145. obj->subID = static_cast<si32>(Res::ERes::GOLD);
  1146. obj->amount = 0;
  1147. return obj;
  1148. };
  1149. oi.value = 750;
  1150. oi.probability = 300;
  1151. possibleObjects.push_back (oi);
  1152. oi.generateObject = [gen]() -> CGObjectInstance *
  1153. {
  1154. auto obj = new CGPickable();
  1155. obj->ID = Obj::TREASURE_CHEST;
  1156. obj->subID = 0;
  1157. return obj;
  1158. };
  1159. oi.value = 1500;
  1160. oi.probability = 1000;
  1161. possibleObjects.push_back (oi);
  1162. oi.generateObject = [gen]() -> CGObjectInstance *
  1163. {
  1164. auto obj = new CGArtifact();
  1165. obj->ID = Obj::RANDOM_TREASURE_ART;
  1166. obj->subID = 0;
  1167. auto a = new CArtifactInstance();
  1168. gen->map->addNewArtifactInstance(a);
  1169. obj->storedArtifact = a;
  1170. return obj;
  1171. };
  1172. oi.value = 2000;
  1173. oi.probability = 150;
  1174. possibleObjects.push_back (oi);
  1175. oi.generateObject = [gen]() -> CGObjectInstance *
  1176. {
  1177. auto obj = new CGArtifact();
  1178. obj->ID = Obj::RANDOM_MINOR_ART;
  1179. obj->subID = 0;
  1180. auto a = new CArtifactInstance();
  1181. gen->map->addNewArtifactInstance(a);
  1182. obj->storedArtifact = a;
  1183. return obj;
  1184. };
  1185. oi.value = 5000;
  1186. oi.probability = 150;
  1187. possibleObjects.push_back (oi);
  1188. oi.generateObject = [gen]() -> CGObjectInstance *
  1189. {
  1190. auto obj = new CGArtifact();
  1191. obj->ID = Obj::RANDOM_MAJOR_ART;
  1192. obj->subID = 0;
  1193. auto a = new CArtifactInstance();
  1194. gen->map->addNewArtifactInstance(a);
  1195. obj->storedArtifact = a;
  1196. return obj;
  1197. };
  1198. oi.value = 10000;
  1199. oi.probability = 150;
  1200. possibleObjects.push_back (oi);
  1201. oi.generateObject = [gen]() -> CGObjectInstance *
  1202. {
  1203. auto obj = new CGArtifact();
  1204. obj->ID = Obj::RANDOM_RELIC_ART;
  1205. obj->subID = 0;
  1206. auto a = new CArtifactInstance();
  1207. gen->map->addNewArtifactInstance(a);
  1208. obj->storedArtifact = a;
  1209. return obj;
  1210. };
  1211. oi.value = 20000;
  1212. oi.probability = 150;
  1213. possibleObjects.push_back (oi);
  1214. static const int scrollValues[] = {500, 2000, 3000, 4000, 5000};
  1215. for (int i = 0; i < 5; i++)
  1216. {
  1217. oi.generateObject = [i, gen]() -> CGObjectInstance *
  1218. {
  1219. auto obj = new CGArtifact();
  1220. obj->ID = Obj::SPELL_SCROLL;
  1221. obj->subID = 0;
  1222. std::vector<SpellID> out;
  1223. //TODO: unify with cb->getAllowedSpells?
  1224. for (ui32 i = 0; i < gen->map->allowedSpell.size(); i++) //spellh size appears to be greater (?)
  1225. {
  1226. const CSpell *spell = SpellID(i).toSpell();
  1227. if (gen->map->allowedSpell[spell->id] && spell->level == i+1)
  1228. {
  1229. out.push_back(spell->id);
  1230. }
  1231. }
  1232. auto a = CArtifactInstance::createScroll(RandomGeneratorUtil::nextItem(out, gen->rand)->toSpell());
  1233. gen->map->addNewArtifactInstance(a);
  1234. obj->storedArtifact = a;
  1235. return obj;
  1236. };
  1237. oi.value = scrollValues[i];
  1238. oi.probability = 30;
  1239. possibleObjects.push_back (oi);
  1240. }
  1241. //non-removable object for test
  1242. //oi.generateObject = [gen]() -> CGObjectInstance *
  1243. //{
  1244. // auto obj = new CGMagicWell();
  1245. // obj->ID = Obj::MAGIC_WELL;
  1246. // obj->subID = 0;
  1247. // return obj;
  1248. //};
  1249. //oi.value = 250;
  1250. //oi.probability = 100;
  1251. //possibleObjects.push_back (oi);
  1252. //oi.generateObject = [gen]() -> CGObjectInstance *
  1253. //{
  1254. // auto obj = new CGObelisk();
  1255. // obj->ID = Obj::OBELISK;
  1256. // obj->subID = 0;
  1257. // return obj;
  1258. //};
  1259. //oi.value = 3500;
  1260. //oi.probability = 200;
  1261. //possibleObjects.push_back (oi);
  1262. //oi.generateObject = [gen]() -> CGObjectInstance *
  1263. //{
  1264. // auto obj = new CBank();
  1265. // obj->ID = Obj::CREATURE_BANK;
  1266. // obj->subID = 5; //naga bank
  1267. // return obj;
  1268. //};
  1269. //oi.value = 3000;
  1270. //oi.probability = 100;
  1271. //possibleObjects.push_back (oi);
  1272. }