CMap.cpp 18 KB

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