2
0

CMap.cpp 19 KB

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