CMap.cpp 19 KB

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