CMap.cpp 18 KB

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