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