CRmgTemplateZone.cpp 37 KB

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