CMap.cpp 20 KB

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