CMap.cpp 19 KB

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