CMap.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /*
  2. * CMap.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 "CMap.h"
  12. #include "../CArtHandler.h"
  13. #include "../VCMI_Lib.h"
  14. #include "../CCreatureHandler.h"
  15. #include "../GameSettings.h"
  16. #include "../RiverHandler.h"
  17. #include "../RoadHandler.h"
  18. #include "../TerrainHandler.h"
  19. #include "../entities/hero/CHeroHandler.h"
  20. #include "../mapObjects/CGHeroInstance.h"
  21. #include "../mapObjects/CGTownInstance.h"
  22. #include "../mapObjects/CQuest.h"
  23. #include "../mapObjects/ObjectTemplate.h"
  24. #include "../texts/CGeneralTextHandler.h"
  25. #include "../spells/CSpellHandler.h"
  26. #include "../CSkillHandler.h"
  27. #include "CMapEditManager.h"
  28. #include "CMapOperation.h"
  29. #include "../serializer/JsonSerializeFormat.h"
  30. #include <vstd/RNG.h>
  31. VCMI_LIB_NAMESPACE_BEGIN
  32. void Rumor::serializeJson(JsonSerializeFormat & handler)
  33. {
  34. handler.serializeString("name", name);
  35. handler.serializeStruct("text", text);
  36. }
  37. DisposedHero::DisposedHero() : heroId(0), portrait(255)
  38. {
  39. }
  40. CMapEvent::CMapEvent()
  41. : humanAffected(false)
  42. , computerAffected(false)
  43. , firstOccurrence(0)
  44. , nextOccurrence(0)
  45. {
  46. }
  47. bool CMapEvent::occursToday(int currentDay) const
  48. {
  49. if (currentDay == firstOccurrence + 1)
  50. return true;
  51. if (nextOccurrence == 0)
  52. return false;
  53. if (currentDay < firstOccurrence)
  54. return false;
  55. return (currentDay - firstOccurrence - 1) % nextOccurrence == 0;
  56. }
  57. bool CMapEvent::affectsPlayer(PlayerColor color, bool isHuman) const
  58. {
  59. if (players.count(color) == 0)
  60. return false;
  61. if (!isHuman && !computerAffected)
  62. return false;
  63. if (isHuman && !humanAffected)
  64. return false;
  65. return true;
  66. }
  67. void CMapEvent::serializeJson(JsonSerializeFormat & handler)
  68. {
  69. handler.serializeString("name", name);
  70. handler.serializeStruct("message", message);
  71. if (!handler.saving && handler.getCurrent()["players"].isNumber())
  72. {
  73. // compatibility for old maps
  74. int playersMask = 0;
  75. handler.serializeInt("players", playersMask);
  76. for (int i = 0; i < 8; ++i)
  77. if ((playersMask & (1 << i)) != 0)
  78. players.insert(PlayerColor(i));
  79. }
  80. else
  81. {
  82. handler.serializeIdArray("players", players);
  83. }
  84. handler.serializeInt("humanAffected", humanAffected);
  85. handler.serializeInt("computerAffected", computerAffected);
  86. handler.serializeInt("firstOccurrence", firstOccurrence);
  87. handler.serializeInt("nextOccurrence", nextOccurrence);
  88. resources.serializeJson(handler, "resources");
  89. auto deletedObjects = handler.enterArray("deletedObjectsInstances");
  90. deletedObjects.serializeArray(deletedObjectsInstances);
  91. }
  92. void CCastleEvent::serializeJson(JsonSerializeFormat & handler)
  93. {
  94. CMapEvent::serializeJson(handler);
  95. // TODO: handler.serializeIdArray("buildings", buildings);
  96. {
  97. std::vector<BuildingID> temp(buildings.begin(), buildings.end());
  98. auto a = handler.enterArray("buildings");
  99. a.syncSize(temp);
  100. for(int i = 0; i < temp.size(); ++i)
  101. {
  102. int buildingID = temp[i].getNum();
  103. a.serializeInt(i, buildingID);
  104. buildings.insert(buildingID);
  105. }
  106. }
  107. {
  108. auto a = handler.enterArray("creatures");
  109. a.syncSize(creatures);
  110. for(int i = 0; i < creatures.size(); ++i)
  111. a.serializeInt(i, creatures[i]);
  112. }
  113. }
  114. TerrainTile::TerrainTile():
  115. riverType(River::NO_RIVER),
  116. roadType(Road::NO_ROAD),
  117. terView(0),
  118. riverDir(0),
  119. roadDir(0),
  120. extTileFlags(0)
  121. {
  122. }
  123. bool TerrainTile::isClear(const TerrainTile * from) const
  124. {
  125. return entrableTerrain(from) && !blocked();
  126. }
  127. Obj TerrainTile::topVisitableId(bool excludeTop) const
  128. {
  129. return topVisitableObj(excludeTop) ? topVisitableObj(excludeTop)->ID : Obj(Obj::NO_OBJ);
  130. }
  131. CGObjectInstance * TerrainTile::topVisitableObj(bool excludeTop) const
  132. {
  133. if(visitableObjects.empty() || (excludeTop && visitableObjects.size() == 1))
  134. return nullptr;
  135. if(excludeTop)
  136. return visitableObjects[visitableObjects.size()-2];
  137. return visitableObjects.back();
  138. }
  139. EDiggingStatus TerrainTile::getDiggingStatus(const bool excludeTop) const
  140. {
  141. if(isWater() || !getTerrain()->isPassable())
  142. return EDiggingStatus::WRONG_TERRAIN;
  143. int allowedBlocked = excludeTop ? 1 : 0;
  144. if(blockingObjects.size() > allowedBlocked || topVisitableObj(excludeTop))
  145. return EDiggingStatus::TILE_OCCUPIED;
  146. else
  147. return EDiggingStatus::CAN_DIG;
  148. }
  149. CMap::CMap(IGameCallback * cb)
  150. : GameCallbackHolder(cb)
  151. , grailPos(-1, -1, -1)
  152. , grailRadius(0)
  153. , waterMap(false)
  154. , uidCounter(0)
  155. {
  156. allHeroes.resize(VLC->heroh->size());
  157. allowedAbilities = VLC->skillh->getDefaultAllowed();
  158. allowedArtifact = VLC->arth->getDefaultAllowed();
  159. allowedSpells = VLC->spellh->getDefaultAllowed();
  160. gameSettings = std::make_unique<GameSettings>();
  161. gameSettings->loadBase(VLC->settingsHandler->getFullConfig());
  162. }
  163. CMap::~CMap()
  164. {
  165. getEditManager()->getUndoManager().clearAll();
  166. for(auto obj : objects)
  167. obj.dellNull();
  168. for(auto quest : quests)
  169. quest.dellNull();
  170. for(auto artInstance : artInstances)
  171. artInstance.dellNull();
  172. resetStaticData();
  173. }
  174. void CMap::removeBlockVisTiles(CGObjectInstance * obj, bool total)
  175. {
  176. const int zVal = obj->anchorPos().z;
  177. for(int fx = 0; fx < obj->getWidth(); ++fx)
  178. {
  179. int xVal = obj->anchorPos().x - fx;
  180. for(int fy = 0; fy < obj->getHeight(); ++fy)
  181. {
  182. int yVal = obj->anchorPos().y - fy;
  183. if(xVal>=0 && xVal < width && yVal>=0 && yVal < height)
  184. {
  185. TerrainTile & curt = terrain[zVal][xVal][yVal];
  186. if(total || obj->visitableAt(int3(xVal, yVal, zVal)))
  187. curt.visitableObjects -= obj;
  188. if(total || obj->blockingAt(int3(xVal, yVal, zVal)))
  189. curt.blockingObjects -= obj;
  190. }
  191. }
  192. }
  193. }
  194. void CMap::addBlockVisTiles(CGObjectInstance * obj)
  195. {
  196. const int zVal = obj->anchorPos().z;
  197. for(int fx = 0; fx < obj->getWidth(); ++fx)
  198. {
  199. int xVal = obj->anchorPos().x - fx;
  200. for(int fy = 0; fy < obj->getHeight(); ++fy)
  201. {
  202. int yVal = obj->anchorPos().y - fy;
  203. if(xVal>=0 && xVal < width && yVal >= 0 && yVal < height)
  204. {
  205. TerrainTile & curt = terrain[zVal][xVal][yVal];
  206. if(obj->visitableAt(int3(xVal, yVal, zVal)))
  207. curt.visitableObjects.push_back(obj);
  208. if(obj->blockingAt(int3(xVal, yVal, zVal)))
  209. curt.blockingObjects.push_back(obj);
  210. }
  211. }
  212. }
  213. }
  214. void CMap::calculateGuardingGreaturePositions()
  215. {
  216. int levels = twoLevel ? 2 : 1;
  217. for(int z = 0; z < levels; z++)
  218. {
  219. for(int x = 0; x < width; x++)
  220. {
  221. for(int y = 0; y < height; y++)
  222. {
  223. guardingCreaturePositions[z][x][y] = guardingCreaturePosition(int3(x, y, z));
  224. }
  225. }
  226. }
  227. }
  228. CGHeroInstance * CMap::getHero(HeroTypeID heroID)
  229. {
  230. for(auto & elem : heroesOnMap)
  231. if(elem->getHeroTypeID() == heroID)
  232. return elem;
  233. return nullptr;
  234. }
  235. bool CMap::isCoastalTile(const int3 & pos) const
  236. {
  237. //todo: refactoring: extract neighbour tile iterator and use it in GameState
  238. static const int3 dirs[] = { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0),
  239. int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) };
  240. if(!isInTheMap(pos))
  241. {
  242. logGlobal->error("Coastal check outside of map: %s", pos.toString());
  243. return false;
  244. }
  245. if(getTile(pos).isWater())
  246. return false;
  247. for(const auto & dir : dirs)
  248. {
  249. const int3 hlp = pos + dir;
  250. if(!isInTheMap(hlp))
  251. continue;
  252. const TerrainTile &hlpt = getTile(hlp);
  253. if(hlpt.isWater())
  254. return true;
  255. }
  256. return false;
  257. }
  258. bool CMap::canMoveBetween(const int3 &src, const int3 &dst) const
  259. {
  260. const TerrainTile * dstTile = &getTile(dst);
  261. const TerrainTile * srcTile = &getTile(src);
  262. return checkForVisitableDir(src, dstTile, dst) && checkForVisitableDir(dst, srcTile, src);
  263. }
  264. bool CMap::checkForVisitableDir(const int3 & src, const TerrainTile * pom, const int3 & dst) const
  265. {
  266. if (!pom->entrableTerrain()) //rock is never accessible
  267. return false;
  268. for(auto * obj : pom->visitableObjects) //checking destination tile
  269. {
  270. if(!vstd::contains(pom->blockingObjects, obj)) //this visitable object is not blocking, ignore
  271. continue;
  272. if (!obj->appearance->isVisitableFrom(src.x - dst.x, src.y - dst.y))
  273. return false;
  274. }
  275. return true;
  276. }
  277. int3 CMap::guardingCreaturePosition (int3 pos) const
  278. {
  279. const int3 originalPos = pos;
  280. // Give monster at position priority.
  281. if (!isInTheMap(pos))
  282. return int3(-1, -1, -1);
  283. const TerrainTile &posTile = getTile(pos);
  284. if (posTile.visitable())
  285. {
  286. for (CGObjectInstance* obj : posTile.visitableObjects)
  287. {
  288. if (obj->ID == Obj::MONSTER)
  289. return pos;
  290. }
  291. }
  292. // See if there are any monsters adjacent.
  293. bool water = posTile.isWater();
  294. pos -= int3(1, 1, 0); // Start with top left.
  295. for (int dx = 0; dx < 3; dx++)
  296. {
  297. for (int dy = 0; dy < 3; dy++)
  298. {
  299. if (isInTheMap(pos))
  300. {
  301. const auto & tile = getTile(pos);
  302. if (tile.visitable() && (tile.isWater() == water))
  303. {
  304. for (CGObjectInstance* obj : tile.visitableObjects)
  305. {
  306. if (obj->ID == Obj::MONSTER && checkForVisitableDir(pos, &posTile, originalPos)) // Monster being able to attack investigated tile
  307. {
  308. return pos;
  309. }
  310. }
  311. }
  312. }
  313. pos.y++;
  314. }
  315. pos.y -= 3;
  316. pos.x++;
  317. }
  318. return int3(-1, -1, -1);
  319. }
  320. const CGObjectInstance * CMap::getObjectiveObjectFrom(const int3 & pos, Obj type)
  321. {
  322. for (CGObjectInstance * object : getTile(pos).visitableObjects)
  323. {
  324. if (object->ID == type)
  325. return object;
  326. }
  327. // There is weird bug because of which sometimes heroes will not be found properly despite having correct position
  328. // Try to workaround that and find closest object that we can use
  329. logGlobal->error("Failed to find object of type %d at %s", type.getNum(), pos.toString());
  330. logGlobal->error("Will try to find closest matching object");
  331. CGObjectInstance * bestMatch = nullptr;
  332. for (CGObjectInstance * object : objects)
  333. {
  334. if (object && object->ID == type)
  335. {
  336. if (bestMatch == nullptr)
  337. bestMatch = object;
  338. else
  339. {
  340. if (object->anchorPos().dist2dSQ(pos) < bestMatch->anchorPos().dist2dSQ(pos))
  341. bestMatch = object;// closer than one we already found
  342. }
  343. }
  344. }
  345. assert(bestMatch != nullptr); // if this happens - victory conditions or map itself is very, very broken
  346. logGlobal->error("Will use %s from %s", bestMatch->getObjectName(), bestMatch->anchorPos().toString());
  347. return bestMatch;
  348. }
  349. void CMap::checkForObjectives()
  350. {
  351. // NOTE: probably should be moved to MapFormatH3M.cpp
  352. for (TriggeredEvent & event : triggeredEvents)
  353. {
  354. auto patcher = [&](EventCondition cond) -> EventExpression::Variant
  355. {
  356. switch (cond.condition)
  357. {
  358. case EventCondition::HAVE_ARTIFACT:
  359. event.onFulfill.replaceTextID(cond.objectType.as<ArtifactID>().toEntity(VLC)->getNameTextID());
  360. break;
  361. case EventCondition::HAVE_CREATURES:
  362. event.onFulfill.replaceTextID(cond.objectType.as<CreatureID>().toEntity(VLC)->getNameSingularTextID());
  363. event.onFulfill.replaceNumber(cond.value);
  364. break;
  365. case EventCondition::HAVE_RESOURCES:
  366. event.onFulfill.replaceName(cond.objectType.as<GameResID>());
  367. event.onFulfill.replaceNumber(cond.value);
  368. break;
  369. case EventCondition::HAVE_BUILDING:
  370. if (isInTheMap(cond.position))
  371. cond.objectID = getObjectiveObjectFrom(cond.position, Obj::TOWN)->id;
  372. break;
  373. case EventCondition::CONTROL:
  374. if (isInTheMap(cond.position))
  375. cond.objectID = getObjectiveObjectFrom(cond.position, cond.objectType.as<MapObjectID>())->id;
  376. if (cond.objectID != ObjectInstanceID::NONE)
  377. {
  378. const auto * town = dynamic_cast<const CGTownInstance *>(objects[cond.objectID].get());
  379. if (town)
  380. event.onFulfill.replaceRawString(town->getNameTranslated());
  381. const auto * hero = dynamic_cast<const CGHeroInstance *>(objects[cond.objectID].get());
  382. if (hero)
  383. event.onFulfill.replaceRawString(hero->getNameTranslated());
  384. }
  385. break;
  386. case EventCondition::DESTROY:
  387. if (isInTheMap(cond.position))
  388. cond.objectID = getObjectiveObjectFrom(cond.position, cond.objectType.as<MapObjectID>())->id;
  389. if (cond.objectID != ObjectInstanceID::NONE)
  390. {
  391. const auto * hero = dynamic_cast<const CGHeroInstance *>(objects[cond.objectID].get());
  392. if (hero)
  393. event.onFulfill.replaceRawString(hero->getNameTranslated());
  394. }
  395. break;
  396. case EventCondition::TRANSPORT:
  397. cond.objectID = getObjectiveObjectFrom(cond.position, Obj::TOWN)->id;
  398. break;
  399. //break; case EventCondition::DAYS_PASSED:
  400. //break; case EventCondition::IS_HUMAN:
  401. //break; case EventCondition::DAYS_WITHOUT_TOWN:
  402. //break; case EventCondition::STANDARD_WIN:
  403. }
  404. return cond;
  405. };
  406. event.trigger = event.trigger.morph(patcher);
  407. }
  408. }
  409. void CMap::addNewArtifactInstance(CArtifactSet & artSet)
  410. {
  411. for(const auto & [slot, slotInfo] : artSet.artifactsWorn)
  412. {
  413. if(!slotInfo.locked && slotInfo.getArt())
  414. addNewArtifactInstance(slotInfo.artifact);
  415. }
  416. for(const auto & slotInfo : artSet.artifactsInBackpack)
  417. addNewArtifactInstance(slotInfo.artifact);
  418. }
  419. void CMap::addNewArtifactInstance(ConstTransitivePtr<CArtifactInstance> art)
  420. {
  421. assert(art);
  422. assert(art->getId() == -1);
  423. art->setId(static_cast<ArtifactInstanceID>(artInstances.size()));
  424. artInstances.emplace_back(art);
  425. for(const auto & partInfo : art->getPartsInfo())
  426. addNewArtifactInstance(partInfo.art);
  427. }
  428. void CMap::eraseArtifactInstance(CArtifactInstance * art)
  429. {
  430. //TODO: handle for artifacts removed in map editor
  431. assert(artInstances[art->getId().getNum()] == art);
  432. artInstances[art->getId().getNum()].dellNull();
  433. }
  434. void CMap::moveArtifactInstance(
  435. CArtifactSet & srcSet, const ArtifactPosition & srcSlot,
  436. CArtifactSet & dstSet, const ArtifactPosition & dstSlot)
  437. {
  438. auto art = srcSet.getArt(srcSlot);
  439. removeArtifactInstance(srcSet, srcSlot);
  440. putArtifactInstance(dstSet, art, dstSlot);
  441. }
  442. void CMap::putArtifactInstance(CArtifactSet & set, CArtifactInstance * art, const ArtifactPosition & slot)
  443. {
  444. art->addPlacementMap(set.putArtifact(slot, art));
  445. }
  446. void CMap::removeArtifactInstance(CArtifactSet & set, const ArtifactPosition & slot)
  447. {
  448. auto art = set.getArt(slot);
  449. assert(art);
  450. set.removeArtifact(slot);
  451. CArtifactSet::ArtPlacementMap partsMap;
  452. for(auto & part : art->getPartsInfo())
  453. {
  454. if(part.slot != ArtifactPosition::PRE_FIRST)
  455. partsMap.try_emplace(part.art, ArtifactPosition::PRE_FIRST);
  456. }
  457. art->addPlacementMap(partsMap);
  458. }
  459. void CMap::addNewQuestInstance(CQuest* quest)
  460. {
  461. quest->qid = static_cast<si32>(quests.size());
  462. quests.emplace_back(quest);
  463. }
  464. void CMap::removeQuestInstance(CQuest * quest)
  465. {
  466. //TODO: should be called only by map editor.
  467. //During game, completed quests or quests from removed objects stay forever
  468. //Shift indexes
  469. auto iter = std::next(quests.begin(), quest->qid);
  470. iter = quests.erase(iter);
  471. for (int i = quest->qid; iter != quests.end(); ++i, ++iter)
  472. {
  473. (*iter)->qid = i;
  474. }
  475. }
  476. void CMap::setUniqueInstanceName(CGObjectInstance * obj)
  477. {
  478. //this gives object unique name even if objects are removed later
  479. auto uid = uidCounter++;
  480. boost::format fmt("%s_%d");
  481. fmt % obj->getTypeName() % uid;
  482. obj->instanceName = fmt.str();
  483. }
  484. void CMap::addNewObject(CGObjectInstance * obj)
  485. {
  486. if(obj->id != ObjectInstanceID(static_cast<si32>(objects.size())))
  487. throw std::runtime_error("Invalid object instance id");
  488. if(obj->instanceName.empty())
  489. throw std::runtime_error("Object instance name missing");
  490. if (vstd::contains(instanceNames, obj->instanceName))
  491. throw std::runtime_error("Object instance name duplicated: "+obj->instanceName);
  492. objects.emplace_back(obj);
  493. instanceNames[obj->instanceName] = obj;
  494. addBlockVisTiles(obj);
  495. //TODO: how about defeated heroes recruited again?
  496. obj->afterAddToMap(this);
  497. }
  498. void CMap::moveObject(CGObjectInstance * obj, const int3 & pos)
  499. {
  500. removeBlockVisTiles(obj);
  501. obj->setAnchorPos(pos);
  502. addBlockVisTiles(obj);
  503. }
  504. void CMap::removeObject(CGObjectInstance * obj)
  505. {
  506. removeBlockVisTiles(obj);
  507. instanceNames.erase(obj->instanceName);
  508. //update indices
  509. auto iter = std::next(objects.begin(), obj->id.getNum());
  510. iter = objects.erase(iter);
  511. for(int i = obj->id.getNum(); iter != objects.end(); ++i, ++iter)
  512. {
  513. (*iter)->id = ObjectInstanceID(i);
  514. }
  515. obj->afterRemoveFromMap(this);
  516. //TODO: Clean artifact instances (mostly worn by hero?) and quests related to this object
  517. //This causes crash with undo/redo in editor
  518. }
  519. bool CMap::isWaterMap() const
  520. {
  521. return waterMap;
  522. }
  523. bool CMap::calculateWaterContent()
  524. {
  525. size_t totalTiles = height * width * levels();
  526. size_t waterTiles = 0;
  527. for(auto tile = terrain.origin(); tile < (terrain.origin() + terrain.num_elements()); ++tile)
  528. {
  529. if (tile->isWater())
  530. {
  531. waterTiles++;
  532. }
  533. }
  534. if (waterTiles >= totalTiles / 100) //At least 1% of area is water
  535. {
  536. waterMap = true;
  537. }
  538. else
  539. {
  540. waterMap = false;
  541. }
  542. return waterMap;
  543. }
  544. void CMap::banWaterContent()
  545. {
  546. banWaterHeroes();
  547. banWaterArtifacts();
  548. banWaterSpells();
  549. banWaterSkills();
  550. }
  551. void CMap::banWaterSpells()
  552. {
  553. vstd::erase_if(allowedSpells, [&](SpellID spell)
  554. {
  555. return spell.toSpell()->onlyOnWaterMap && !isWaterMap();
  556. });
  557. }
  558. void CMap::banWaterArtifacts()
  559. {
  560. vstd::erase_if(allowedArtifact, [&](ArtifactID artifact)
  561. {
  562. return artifact.toArtifact()->onlyOnWaterMap && !isWaterMap();
  563. });
  564. }
  565. void CMap::banWaterSkills()
  566. {
  567. vstd::erase_if(allowedAbilities, [&](SecondarySkill skill)
  568. {
  569. return skill.toSkill()->onlyOnWaterMap && !isWaterMap();
  570. });
  571. }
  572. void CMap::banWaterHeroes()
  573. {
  574. vstd::erase_if(allowedHeroes, [&](HeroTypeID hero)
  575. {
  576. return hero.toHeroType()->onlyOnWaterMap && !isWaterMap();
  577. });
  578. vstd::erase_if(allowedHeroes, [&](HeroTypeID hero)
  579. {
  580. return hero.toHeroType()->onlyOnMapWithoutWater && isWaterMap();
  581. });
  582. }
  583. void CMap::banHero(const HeroTypeID & id)
  584. {
  585. if (!vstd::contains(allowedHeroes, id))
  586. logGlobal->warn("Attempt to ban hero %s, who is already not allowed", id.encode(id));
  587. allowedHeroes.erase(id);
  588. }
  589. void CMap::unbanHero(const HeroTypeID & id)
  590. {
  591. if (vstd::contains(allowedHeroes, id))
  592. logGlobal->warn("Attempt to unban hero %s, who is already allowed", id.encode(id));
  593. allowedHeroes.insert(id);
  594. }
  595. void CMap::initTerrain()
  596. {
  597. terrain.resize(boost::extents[levels()][width][height]);
  598. guardingCreaturePositions.resize(boost::extents[levels()][width][height]);
  599. }
  600. CMapEditManager * CMap::getEditManager()
  601. {
  602. if(!editManager) editManager = std::make_unique<CMapEditManager>(this);
  603. return editManager.get();
  604. }
  605. void CMap::resetStaticData()
  606. {
  607. obeliskCount = 0;
  608. obelisksVisited.clear();
  609. townMerchantArtifacts.clear();
  610. townUniversitySkills.clear();
  611. }
  612. void CMap::resolveQuestIdentifiers()
  613. {
  614. //FIXME: move to CMapLoaderH3M
  615. for (auto & quest : quests)
  616. {
  617. if (quest && quest->killTarget != ObjectInstanceID::NONE)
  618. quest->killTarget = questIdentifierToId[quest->killTarget.getNum()];
  619. }
  620. questIdentifierToId.clear();
  621. }
  622. void CMap::reindexObjects()
  623. {
  624. // Only reindex at editor / RMG operations
  625. std::sort(objects.begin(), objects.end(), [](const CGObjectInstance * lhs, const CGObjectInstance * rhs)
  626. {
  627. // Obstacles first, then visitable, at the end - removable
  628. if (!lhs->isVisitable() && rhs->isVisitable())
  629. return true;
  630. if (lhs->isVisitable() && !rhs->isVisitable())
  631. return false;
  632. // Special case for Windomill - draw on top of other objects
  633. if (lhs->ID != Obj::WINDMILL && rhs->ID == Obj::WINDMILL)
  634. return true;
  635. if (lhs->ID == Obj::WINDMILL && rhs->ID != Obj::WINDMILL)
  636. return false;
  637. if (!lhs->isRemovable() && rhs->isRemovable())
  638. return true;
  639. if (lhs->isRemovable() && !rhs->isRemovable())
  640. return false;
  641. return lhs->anchorPos().y < rhs->anchorPos().y;
  642. });
  643. // instanceNames don't change
  644. for (size_t i = 0; i < objects.size(); ++i)
  645. {
  646. objects[i]->id = ObjectInstanceID(i);
  647. }
  648. }
  649. const IGameSettings & CMap::getSettings() const
  650. {
  651. return *gameSettings;
  652. }
  653. void CMap::overrideGameSetting(EGameSettings option, const JsonNode & input)
  654. {
  655. return gameSettings->addOverride(option, input);
  656. }
  657. void CMap::overrideGameSettings(const JsonNode & input)
  658. {
  659. return gameSettings->loadOverrides(input);
  660. }
  661. VCMI_LIB_NAMESPACE_END