CMap.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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. return pos.x >= 0 && pos.y >= 0 && pos.z >= 0 && pos.x < width && pos.y < height && pos.z <= (twoLevel ? 1 : 0);
  289. }
  290. TerrainTile & CMap::getTile(const int3 & tile)
  291. {
  292. assert(isInTheMap(tile));
  293. return terrain[tile.z][tile.x][tile.y];
  294. }
  295. const TerrainTile & CMap::getTile(const int3 & tile) const
  296. {
  297. assert(isInTheMap(tile));
  298. return terrain[tile.z][tile.x][tile.y];
  299. }
  300. bool CMap::isWaterTile(const int3 &pos) const
  301. {
  302. return isInTheMap(pos) && getTile(pos).isWater();
  303. }
  304. bool CMap::canMoveBetween(const int3 &src, const int3 &dst) const
  305. {
  306. const TerrainTile * dstTile = &getTile(dst);
  307. const TerrainTile * srcTile = &getTile(src);
  308. return checkForVisitableDir(src, dstTile, dst) && checkForVisitableDir(dst, srcTile, src);
  309. }
  310. bool CMap::checkForVisitableDir(const int3 & src, const TerrainTile * pom, const int3 & dst) const
  311. {
  312. if (!pom->entrableTerrain()) //rock is never accessible
  313. return false;
  314. for(auto * obj : pom->visitableObjects) //checking destination tile
  315. {
  316. if(!vstd::contains(pom->blockingObjects, obj)) //this visitable object is not blocking, ignore
  317. continue;
  318. if (!obj->appearance->isVisitableFrom(src.x - dst.x, src.y - dst.y))
  319. return false;
  320. }
  321. return true;
  322. }
  323. int3 CMap::guardingCreaturePosition (int3 pos) const
  324. {
  325. const int3 originalPos = pos;
  326. // Give monster at position priority.
  327. if (!isInTheMap(pos))
  328. return int3(-1, -1, -1);
  329. const TerrainTile &posTile = getTile(pos);
  330. if (posTile.visitable)
  331. {
  332. for (CGObjectInstance* obj : posTile.visitableObjects)
  333. {
  334. if (obj->ID == Obj::MONSTER)
  335. return pos;
  336. }
  337. }
  338. // See if there are any monsters adjacent.
  339. bool water = posTile.isWater();
  340. pos -= int3(1, 1, 0); // Start with top left.
  341. for (int dx = 0; dx < 3; dx++)
  342. {
  343. for (int dy = 0; dy < 3; dy++)
  344. {
  345. if (isInTheMap(pos))
  346. {
  347. const auto & tile = getTile(pos);
  348. if (tile.visitable && (tile.isWater() == water))
  349. {
  350. for (CGObjectInstance* obj : tile.visitableObjects)
  351. {
  352. if (obj->ID == Obj::MONSTER && checkForVisitableDir(pos, &posTile, originalPos)) // Monster being able to attack investigated tile
  353. {
  354. return pos;
  355. }
  356. }
  357. }
  358. }
  359. pos.y++;
  360. }
  361. pos.y -= 3;
  362. pos.x++;
  363. }
  364. return int3(-1, -1, -1);
  365. }
  366. const CGObjectInstance * CMap::getObjectiveObjectFrom(const int3 & pos, Obj type)
  367. {
  368. for (CGObjectInstance * object : getTile(pos).visitableObjects)
  369. {
  370. if (object->ID == type)
  371. return object;
  372. }
  373. // There is weird bug because of which sometimes heroes will not be found properly despite having correct position
  374. // Try to workaround that and find closest object that we can use
  375. logGlobal->error("Failed to find object of type %d at %s", type.getNum(), pos.toString());
  376. logGlobal->error("Will try to find closest matching object");
  377. CGObjectInstance * bestMatch = nullptr;
  378. for (CGObjectInstance * object : objects)
  379. {
  380. if (object && object->ID == type)
  381. {
  382. if (bestMatch == nullptr)
  383. bestMatch = object;
  384. else
  385. {
  386. if (object->pos.dist2dSQ(pos) < bestMatch->pos.dist2dSQ(pos))
  387. bestMatch = object;// closer than one we already found
  388. }
  389. }
  390. }
  391. assert(bestMatch != nullptr); // if this happens - victory conditions or map itself is very, very broken
  392. logGlobal->error("Will use %s from %s", bestMatch->getObjectName(), bestMatch->pos.toString());
  393. return bestMatch;
  394. }
  395. void CMap::checkForObjectives()
  396. {
  397. // NOTE: probably should be moved to MapFormatH3M.cpp
  398. for (TriggeredEvent & event : triggeredEvents)
  399. {
  400. auto patcher = [&](EventCondition cond) -> EventExpression::Variant
  401. {
  402. switch (cond.condition)
  403. {
  404. case EventCondition::HAVE_ARTIFACT:
  405. event.onFulfill.replaceTextID(cond.objectType.as<ArtifactID>().toEntity(VLC)->getNameTextID());
  406. break;
  407. case EventCondition::HAVE_CREATURES:
  408. event.onFulfill.replaceTextID(cond.objectType.as<CreatureID>().toEntity(VLC)->getNameSingularTextID());
  409. event.onFulfill.replaceNumber(cond.value);
  410. break;
  411. case EventCondition::HAVE_RESOURCES:
  412. event.onFulfill.replaceName(cond.objectType.as<GameResID>());
  413. event.onFulfill.replaceNumber(cond.value);
  414. break;
  415. case EventCondition::HAVE_BUILDING:
  416. if (isInTheMap(cond.position))
  417. cond.objectID = getObjectiveObjectFrom(cond.position, Obj::TOWN)->id;
  418. break;
  419. case EventCondition::CONTROL:
  420. if (isInTheMap(cond.position))
  421. cond.objectID = getObjectiveObjectFrom(cond.position, cond.objectType.as<MapObjectID>())->id;
  422. if (cond.objectID != ObjectInstanceID::NONE)
  423. {
  424. const auto * town = dynamic_cast<const CGTownInstance *>(objects[cond.objectID].get());
  425. if (town)
  426. event.onFulfill.replaceRawString(town->getNameTranslated());
  427. const auto * hero = dynamic_cast<const CGHeroInstance *>(objects[cond.objectID].get());
  428. if (hero)
  429. event.onFulfill.replaceRawString(hero->getNameTranslated());
  430. }
  431. break;
  432. case EventCondition::DESTROY:
  433. if (isInTheMap(cond.position))
  434. cond.objectID = getObjectiveObjectFrom(cond.position, cond.objectType.as<MapObjectID>())->id;
  435. if (cond.objectID != ObjectInstanceID::NONE)
  436. {
  437. const auto * hero = dynamic_cast<const CGHeroInstance *>(objects[cond.objectID].get());
  438. if (hero)
  439. event.onFulfill.replaceRawString(hero->getNameTranslated());
  440. }
  441. break;
  442. case EventCondition::TRANSPORT:
  443. cond.objectID = getObjectiveObjectFrom(cond.position, Obj::TOWN)->id;
  444. break;
  445. //break; case EventCondition::DAYS_PASSED:
  446. //break; case EventCondition::IS_HUMAN:
  447. //break; case EventCondition::DAYS_WITHOUT_TOWN:
  448. //break; case EventCondition::STANDARD_WIN:
  449. }
  450. return cond;
  451. };
  452. event.trigger = event.trigger.morph(patcher);
  453. }
  454. }
  455. void CMap::addNewArtifactInstance(ConstTransitivePtr<CArtifactInstance> art)
  456. {
  457. art->setId(static_cast<ArtifactInstanceID>(artInstances.size()));
  458. artInstances.emplace_back(art);
  459. }
  460. void CMap::eraseArtifactInstance(CArtifactInstance * art)
  461. {
  462. //TODO: handle for artifacts removed in map editor
  463. assert(artInstances[art->getId().getNum()] == art);
  464. artInstances[art->getId().getNum()].dellNull();
  465. }
  466. void CMap::addNewQuestInstance(CQuest* quest)
  467. {
  468. quest->qid = static_cast<si32>(quests.size());
  469. quests.emplace_back(quest);
  470. }
  471. void CMap::removeQuestInstance(CQuest * quest)
  472. {
  473. //TODO: should be called only by map editor.
  474. //During game, completed quests or quests from removed objects stay forever
  475. //Shift indexes
  476. auto iter = std::next(quests.begin(), quest->qid);
  477. iter = quests.erase(iter);
  478. for (int i = quest->qid; iter != quests.end(); ++i, ++iter)
  479. {
  480. (*iter)->qid = i;
  481. }
  482. }
  483. void CMap::setUniqueInstanceName(CGObjectInstance * obj)
  484. {
  485. //this gives object unique name even if objects are removed later
  486. auto uid = uidCounter++;
  487. boost::format fmt("%s_%d");
  488. fmt % obj->typeName % uid;
  489. obj->instanceName = fmt.str();
  490. }
  491. void CMap::addNewObject(CGObjectInstance * obj)
  492. {
  493. if(obj->id != ObjectInstanceID(static_cast<si32>(objects.size())))
  494. throw std::runtime_error("Invalid object instance id");
  495. if(obj->instanceName.empty())
  496. throw std::runtime_error("Object instance name missing");
  497. if (vstd::contains(instanceNames, obj->instanceName))
  498. throw std::runtime_error("Object instance name duplicated: "+obj->instanceName);
  499. objects.emplace_back(obj);
  500. instanceNames[obj->instanceName] = obj;
  501. addBlockVisTiles(obj);
  502. //TODO: how about defeated heroes recruited again?
  503. obj->afterAddToMap(this);
  504. }
  505. void CMap::moveObject(CGObjectInstance * obj, const int3 & pos)
  506. {
  507. removeBlockVisTiles(obj);
  508. obj->pos = pos;
  509. addBlockVisTiles(obj);
  510. }
  511. void CMap::removeObject(CGObjectInstance * obj)
  512. {
  513. removeBlockVisTiles(obj);
  514. instanceNames.erase(obj->instanceName);
  515. //update indices
  516. auto iter = std::next(objects.begin(), obj->id.getNum());
  517. iter = objects.erase(iter);
  518. for(int i = obj->id.getNum(); iter != objects.end(); ++i, ++iter)
  519. {
  520. (*iter)->id = ObjectInstanceID(i);
  521. }
  522. obj->afterRemoveFromMap(this);
  523. //TODO: Clean artifact instances (mostly worn by hero?) and quests related to this object
  524. //This causes crash with undo/redo in editor
  525. }
  526. bool CMap::isWaterMap() const
  527. {
  528. return waterMap;
  529. }
  530. bool CMap::calculateWaterContent()
  531. {
  532. size_t totalTiles = height * width * levels();
  533. size_t waterTiles = 0;
  534. for(auto tile = terrain.origin(); tile < (terrain.origin() + terrain.num_elements()); ++tile)
  535. {
  536. if (tile->isWater())
  537. {
  538. waterTiles++;
  539. }
  540. }
  541. if (waterTiles >= totalTiles / 100) //At least 1% of area is water
  542. {
  543. waterMap = true;
  544. }
  545. else
  546. {
  547. waterMap = false;
  548. }
  549. return waterMap;
  550. }
  551. void CMap::banWaterContent()
  552. {
  553. banWaterHeroes();
  554. banWaterArtifacts();
  555. banWaterSpells();
  556. banWaterSkills();
  557. }
  558. void CMap::banWaterSpells()
  559. {
  560. vstd::erase_if(allowedSpells, [&](SpellID spell)
  561. {
  562. return spell.toSpell()->onlyOnWaterMap && !isWaterMap();
  563. });
  564. }
  565. void CMap::banWaterArtifacts()
  566. {
  567. vstd::erase_if(allowedArtifact, [&](ArtifactID artifact)
  568. {
  569. return artifact.toArtifact()->onlyOnWaterMap && !isWaterMap();
  570. });
  571. }
  572. void CMap::banWaterSkills()
  573. {
  574. vstd::erase_if(allowedAbilities, [&](SecondarySkill skill)
  575. {
  576. return skill.toSkill()->onlyOnWaterMap && !isWaterMap();
  577. });
  578. }
  579. void CMap::banWaterHeroes()
  580. {
  581. vstd::erase_if(allowedHeroes, [&](HeroTypeID hero)
  582. {
  583. return hero.toHeroType()->onlyOnWaterMap && !isWaterMap();
  584. });
  585. vstd::erase_if(allowedHeroes, [&](HeroTypeID hero)
  586. {
  587. return hero.toHeroType()->onlyOnMapWithoutWater && isWaterMap();
  588. });
  589. }
  590. void CMap::banHero(const HeroTypeID & id)
  591. {
  592. if (!vstd::contains(allowedHeroes, id))
  593. logGlobal->warn("Attempt to ban hero %s, who is already not allowed", id.encode(id));
  594. allowedHeroes.erase(id);
  595. }
  596. void CMap::unbanHero(const HeroTypeID & id)
  597. {
  598. if (vstd::contains(allowedHeroes, id))
  599. logGlobal->warn("Attempt to unban hero %s, who is already allowed", id.encode(id));
  600. allowedHeroes.insert(id);
  601. }
  602. void CMap::initTerrain()
  603. {
  604. terrain.resize(boost::extents[levels()][width][height]);
  605. guardingCreaturePositions.resize(boost::extents[levels()][width][height]);
  606. }
  607. CMapEditManager * CMap::getEditManager()
  608. {
  609. if(!editManager) editManager = std::make_unique<CMapEditManager>(this);
  610. return editManager.get();
  611. }
  612. void CMap::resetStaticData()
  613. {
  614. obeliskCount = 0;
  615. obelisksVisited.clear();
  616. townMerchantArtifacts.clear();
  617. townUniversitySkills.clear();
  618. }
  619. void CMap::resolveQuestIdentifiers()
  620. {
  621. //FIXME: move to CMapLoaderH3M
  622. for (auto & quest : quests)
  623. {
  624. if (quest && quest->killTarget != ObjectInstanceID::NONE)
  625. quest->killTarget = questIdentifierToId[quest->killTarget.getNum()];
  626. }
  627. questIdentifierToId.clear();
  628. }
  629. void CMap::reindexObjects()
  630. {
  631. // Only reindex at editor / RMG operations
  632. std::sort(objects.begin(), objects.end(), [](const CGObjectInstance * lhs, const CGObjectInstance * rhs)
  633. {
  634. // Obstacles first, then visitable, at the end - removable
  635. if (!lhs->isVisitable() && rhs->isVisitable())
  636. return true;
  637. if (lhs->isVisitable() && !rhs->isVisitable())
  638. return false;
  639. // Special case for Windomill - draw on top of other objects
  640. if (lhs->ID != Obj::WINDMILL && rhs->ID == Obj::WINDMILL)
  641. return true;
  642. if (lhs->ID == Obj::WINDMILL && rhs->ID != Obj::WINDMILL)
  643. return false;
  644. if (!lhs->isRemovable() && rhs->isRemovable())
  645. return true;
  646. if (lhs->isRemovable() && !rhs->isRemovable())
  647. return false;
  648. return lhs->pos.y < rhs->pos.y;
  649. });
  650. // instanceNames don't change
  651. for (size_t i = 0; i < objects.size(); ++i)
  652. {
  653. objects[i]->id = ObjectInstanceID(i);
  654. }
  655. }
  656. VCMI_LIB_NAMESPACE_END