CMap.cpp 18 KB

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