CMap.cpp 19 KB

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