CMap.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*
  2. * CMap.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CMap.h"
  12. #include "../CArtHandler.h"
  13. #include "../VCMI_Lib.h"
  14. #include "../CCreatureHandler.h"
  15. #include "../CTownHandler.h"
  16. #include "../CHeroHandler.h"
  17. #include "../RiverHandler.h"
  18. #include "../RoadHandler.h"
  19. #include "../TerrainHandler.h"
  20. #include "../mapObjects/CObjectClassesHandler.h"
  21. #include "../mapObjects/CGHeroInstance.h"
  22. #include "../CGeneralTextHandler.h"
  23. #include "../spells/CSpellHandler.h"
  24. #include "../CSkillHandler.h"
  25. #include "CMapEditManager.h"
  26. #include "CMapOperation.h"
  27. #include "../serializer/JsonSerializeFormat.h"
  28. VCMI_LIB_NAMESPACE_BEGIN
  29. SHeroName::SHeroName() : heroId(-1)
  30. {
  31. }
  32. PlayerInfo::PlayerInfo(): canHumanPlay(false), canComputerPlay(false),
  33. aiTactic(EAiTactic::RANDOM), isFactionRandom(false), hasRandomHero(false), mainCustomHeroPortrait(-1), mainCustomHeroId(-1), hasMainTown(false),
  34. generateHeroAtMainTown(false), posOfMainTown(-1), team(TeamID::NO_TEAM)
  35. {
  36. allowedFactions = VLC->townh->getAllowedFactions();
  37. }
  38. si8 PlayerInfo::defaultCastle() const
  39. {
  40. //if random allowed set it as default
  41. if(isFactionRandom)
  42. return -1;
  43. if(!allowedFactions.empty())
  44. return *allowedFactions.begin();
  45. // fall back to random
  46. return -1;
  47. }
  48. si8 PlayerInfo::defaultHero() const
  49. {
  50. // we will generate hero in front of main town
  51. if((generateHeroAtMainTown && hasMainTown) || hasRandomHero)
  52. {
  53. //random hero
  54. return -1;
  55. }
  56. return -2;
  57. }
  58. bool PlayerInfo::canAnyonePlay() const
  59. {
  60. return canHumanPlay || canComputerPlay;
  61. }
  62. bool PlayerInfo::hasCustomMainHero() const
  63. {
  64. return !mainCustomHeroName.empty() && mainCustomHeroPortrait != -1;
  65. }
  66. EventCondition::EventCondition(EWinLoseType condition):
  67. object(nullptr),
  68. metaType(EMetaclass::INVALID),
  69. value(-1),
  70. objectType(-1),
  71. objectSubtype(-1),
  72. position(-1, -1, -1),
  73. condition(condition)
  74. {
  75. }
  76. EventCondition::EventCondition(EWinLoseType condition, si32 value, si32 objectType, const int3 & position):
  77. object(nullptr),
  78. metaType(EMetaclass::INVALID),
  79. value(value),
  80. objectType(objectType),
  81. objectSubtype(-1),
  82. position(position),
  83. condition(condition)
  84. {}
  85. void Rumor::serializeJson(JsonSerializeFormat & handler)
  86. {
  87. handler.serializeString("name", name);
  88. handler.serializeString("text", text);
  89. }
  90. DisposedHero::DisposedHero() : heroId(0), portrait(255), players(0)
  91. {
  92. }
  93. CMapEvent::CMapEvent() : players(0), humanAffected(0), computerAffected(0),
  94. firstOccurence(0), nextOccurence(0)
  95. {
  96. }
  97. bool CMapEvent::earlierThan(const CMapEvent & other) const
  98. {
  99. return firstOccurence < other.firstOccurence;
  100. }
  101. bool CMapEvent::earlierThanOrEqual(const CMapEvent & other) const
  102. {
  103. return firstOccurence <= other.firstOccurence;
  104. }
  105. CCastleEvent::CCastleEvent() : town(nullptr)
  106. {
  107. }
  108. TerrainTile::TerrainTile():
  109. terType(nullptr),
  110. terView(0),
  111. riverType(VLC->riverTypeHandler->getById(River::NO_RIVER)),
  112. riverDir(0),
  113. roadType(VLC->roadTypeHandler->getById(Road::NO_ROAD)),
  114. roadDir(0),
  115. extTileFlags(0),
  116. visitable(false),
  117. blocked(false)
  118. {
  119. }
  120. bool TerrainTile::entrableTerrain(const TerrainTile * from) const
  121. {
  122. return entrableTerrain(from ? from->terType->isLand() : true, from ? from->terType->isWater() : true);
  123. }
  124. bool TerrainTile::entrableTerrain(bool allowLand, bool allowSea) const
  125. {
  126. return terType->isPassable()
  127. && ((allowSea && terType->isWater()) || (allowLand && terType->isLand()));
  128. }
  129. bool TerrainTile::isClear(const TerrainTile * from) const
  130. {
  131. return entrableTerrain(from) && !blocked;
  132. }
  133. Obj TerrainTile::topVisitableId(bool excludeTop) const
  134. {
  135. return topVisitableObj(excludeTop) ? topVisitableObj(excludeTop)->ID : Obj(Obj::NO_OBJ);
  136. }
  137. CGObjectInstance * TerrainTile::topVisitableObj(bool excludeTop) const
  138. {
  139. if(visitableObjects.empty() || (excludeTop && visitableObjects.size() == 1))
  140. return nullptr;
  141. if(excludeTop)
  142. return visitableObjects[visitableObjects.size()-2];
  143. return visitableObjects.back();
  144. }
  145. EDiggingStatus TerrainTile::getDiggingStatus(const bool excludeTop) const
  146. {
  147. if(terType->isWater() || !terType->isPassable())
  148. return EDiggingStatus::WRONG_TERRAIN;
  149. int allowedBlocked = excludeTop ? 1 : 0;
  150. if(blockingObjects.size() > allowedBlocked || topVisitableObj(excludeTop))
  151. return EDiggingStatus::TILE_OCCUPIED;
  152. else
  153. return EDiggingStatus::CAN_DIG;
  154. }
  155. bool TerrainTile::hasFavorableWinds() const
  156. {
  157. return extTileFlags & 128;
  158. }
  159. bool TerrainTile::isWater() const
  160. {
  161. return terType->isWater();
  162. }
  163. void CMapHeader::setupEvents()
  164. {
  165. EventCondition victoryCondition(EventCondition::STANDARD_WIN);
  166. EventCondition defeatCondition(EventCondition::DAYS_WITHOUT_TOWN);
  167. defeatCondition.value = 7;
  168. //Victory condition - defeat all
  169. TriggeredEvent standardVictory;
  170. standardVictory.effect.type = EventEffect::VICTORY;
  171. standardVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[5];
  172. standardVictory.identifier = "standardVictory";
  173. standardVictory.description.clear(); // TODO: display in quest window
  174. standardVictory.onFulfill = VLC->generaltexth->allTexts[659];
  175. standardVictory.trigger = EventExpression(victoryCondition);
  176. //Loss condition - 7 days without town
  177. TriggeredEvent standardDefeat;
  178. standardDefeat.effect.type = EventEffect::DEFEAT;
  179. standardDefeat.effect.toOtherMessage = VLC->generaltexth->allTexts[8];
  180. standardDefeat.identifier = "standardDefeat";
  181. standardDefeat.description.clear(); // TODO: display in quest window
  182. standardDefeat.onFulfill = VLC->generaltexth->allTexts[7];
  183. standardDefeat.trigger = EventExpression(defeatCondition);
  184. triggeredEvents.push_back(standardVictory);
  185. triggeredEvents.push_back(standardDefeat);
  186. victoryIconIndex = 11;
  187. victoryMessage = VLC->generaltexth->victoryConditions[0];
  188. defeatIconIndex = 3;
  189. defeatMessage = VLC->generaltexth->lossCondtions[0];
  190. }
  191. CMapHeader::CMapHeader() : version(EMapFormat::VCMI), height(72), width(72),
  192. twoLevel(true), difficulty(1), levelLimit(0), howManyTeams(0), areAnyPlayers(false)
  193. {
  194. setupEvents();
  195. allowedHeroes = VLC->heroh->getDefaultAllowed();
  196. players.resize(PlayerColor::PLAYER_LIMIT_I);
  197. }
  198. ui8 CMapHeader::levels() const
  199. {
  200. return (twoLevel ? 2 : 1);
  201. }
  202. CMap::CMap()
  203. : checksum(0), grailPos(-1, -1, -1), grailRadius(0), terrain(nullptr),
  204. guardingCreaturePositions(nullptr),
  205. uidCounter(0)
  206. {
  207. allHeroes.resize(allowedHeroes.size());
  208. allowedAbilities = VLC->skillh->getDefaultAllowed();
  209. allowedArtifact = VLC->arth->getDefaultAllowed();
  210. allowedSpell = VLC->spellh->getDefaultAllowed();
  211. }
  212. CMap::~CMap()
  213. {
  214. getEditManager()->getUndoManager().clearAll();
  215. if(terrain)
  216. {
  217. for(int z = 0; z < levels(); z++)
  218. {
  219. for(int x = 0; x < width; x++)
  220. {
  221. delete[] terrain[z][x];
  222. delete[] guardingCreaturePositions[z][x];
  223. }
  224. delete[] terrain[z];
  225. delete[] guardingCreaturePositions[z];
  226. }
  227. delete [] terrain;
  228. delete [] guardingCreaturePositions;
  229. }
  230. for(auto obj : objects)
  231. obj.dellNull();
  232. for(auto quest : quests)
  233. quest.dellNull();
  234. resetStaticData();
  235. }
  236. void CMap::removeBlockVisTiles(CGObjectInstance * obj, bool total)
  237. {
  238. const int zVal = obj->pos.z;
  239. for(int fx = 0; fx < obj->getWidth(); ++fx)
  240. {
  241. int xVal = obj->pos.x - fx;
  242. for(int fy = 0; fy < obj->getHeight(); ++fy)
  243. {
  244. int yVal = obj->pos.y - fy;
  245. if(xVal>=0 && xVal < width && yVal>=0 && yVal < height)
  246. {
  247. TerrainTile & curt = terrain[zVal][xVal][yVal];
  248. if(total || obj->visitableAt(xVal, yVal))
  249. {
  250. curt.visitableObjects -= obj;
  251. curt.visitable = curt.visitableObjects.size();
  252. }
  253. if(total || obj->blockingAt(xVal, yVal))
  254. {
  255. curt.blockingObjects -= obj;
  256. curt.blocked = curt.blockingObjects.size();
  257. }
  258. }
  259. }
  260. }
  261. }
  262. void CMap::addBlockVisTiles(CGObjectInstance * obj)
  263. {
  264. const int zVal = obj->pos.z;
  265. for(int fx = 0; fx < obj->getWidth(); ++fx)
  266. {
  267. int xVal = obj->pos.x - fx;
  268. for(int fy = 0; fy < obj->getHeight(); ++fy)
  269. {
  270. int yVal = obj->pos.y - fy;
  271. if(xVal>=0 && xVal < width && yVal >= 0 && yVal < height)
  272. {
  273. TerrainTile & curt = terrain[zVal][xVal][yVal];
  274. if(obj->visitableAt(xVal, yVal))
  275. {
  276. curt.visitableObjects.push_back(obj);
  277. curt.visitable = true;
  278. }
  279. if(obj->blockingAt(xVal, yVal))
  280. {
  281. curt.blockingObjects.push_back(obj);
  282. curt.blocked = true;
  283. }
  284. }
  285. }
  286. }
  287. }
  288. void CMap::calculateGuardingGreaturePositions()
  289. {
  290. int levels = twoLevel ? 2 : 1;
  291. for(int z = 0; z < levels; z++)
  292. {
  293. for(int x = 0; x < width; x++)
  294. {
  295. for(int y = 0; y < height; y++)
  296. {
  297. guardingCreaturePositions[z][x][y] = guardingCreaturePosition(int3(x, y, z));
  298. }
  299. }
  300. }
  301. }
  302. CGHeroInstance * CMap::getHero(int heroID)
  303. {
  304. for(auto & elem : heroesOnMap)
  305. if(elem->subID == heroID)
  306. return elem;
  307. return nullptr;
  308. }
  309. bool CMap::isCoastalTile(const int3 & pos) const
  310. {
  311. //todo: refactoring: extract neighbor tile iterator and use it in GameState
  312. static const int3 dirs[] = { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0),
  313. int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) };
  314. if(!isInTheMap(pos))
  315. {
  316. logGlobal->error("Coastal check outside of map: %s", pos.toString());
  317. return false;
  318. }
  319. if(isWaterTile(pos))
  320. return false;
  321. for(const auto & dir : dirs)
  322. {
  323. const int3 hlp = pos + dir;
  324. if(!isInTheMap(hlp))
  325. continue;
  326. const TerrainTile &hlpt = getTile(hlp);
  327. if(hlpt.isWater())
  328. return true;
  329. }
  330. return false;
  331. }
  332. bool CMap::isInTheMap(const int3 & pos) const
  333. {
  334. return pos.x >= 0 && pos.y >= 0 && pos.z >= 0 && pos.x < width && pos.y < height && pos.z <= (twoLevel ? 1 : 0);
  335. }
  336. TerrainTile & CMap::getTile(const int3 & tile)
  337. {
  338. assert(isInTheMap(tile));
  339. return terrain[tile.z][tile.x][tile.y];
  340. }
  341. const TerrainTile & CMap::getTile(const int3 & tile) const
  342. {
  343. assert(isInTheMap(tile));
  344. return terrain[tile.z][tile.x][tile.y];
  345. }
  346. bool CMap::isWaterTile(const int3 &pos) const
  347. {
  348. return isInTheMap(pos) && getTile(pos).isWater();
  349. }
  350. bool CMap::canMoveBetween(const int3 &src, const int3 &dst) const
  351. {
  352. const TerrainTile * dstTile = &getTile(dst);
  353. const TerrainTile * srcTile = &getTile(src);
  354. return checkForVisitableDir(src, dstTile, dst) && checkForVisitableDir(dst, srcTile, src);
  355. }
  356. bool CMap::checkForVisitableDir(const int3 & src, const TerrainTile * pom, const int3 & dst) const
  357. {
  358. if (!pom->entrableTerrain()) //rock is never accessible
  359. return false;
  360. for(auto * obj : pom->visitableObjects) //checking destination tile
  361. {
  362. if(!vstd::contains(pom->blockingObjects, obj)) //this visitable object is not blocking, ignore
  363. continue;
  364. if (!obj->appearance->isVisitableFrom(src.x - dst.x, src.y - dst.y))
  365. return false;
  366. }
  367. return true;
  368. }
  369. int3 CMap::guardingCreaturePosition (int3 pos) const
  370. {
  371. const int3 originalPos = pos;
  372. // Give monster at position priority.
  373. if (!isInTheMap(pos))
  374. return int3(-1, -1, -1);
  375. const TerrainTile &posTile = getTile(pos);
  376. if (posTile.visitable)
  377. {
  378. for (CGObjectInstance* obj : posTile.visitableObjects)
  379. {
  380. if(obj->blockVisit)
  381. {
  382. if (obj->ID == Obj::MONSTER) // Monster
  383. return pos;
  384. else
  385. return int3(-1, -1, -1); //blockvis objects are not guarded by neighbouring creatures
  386. }
  387. }
  388. }
  389. // See if there are any monsters adjacent.
  390. bool water = posTile.isWater();
  391. pos -= int3(1, 1, 0); // Start with top left.
  392. for (int dx = 0; dx < 3; dx++)
  393. {
  394. for (int dy = 0; dy < 3; dy++)
  395. {
  396. if (isInTheMap(pos))
  397. {
  398. const auto & tile = getTile(pos);
  399. if (tile.visitable && (tile.isWater() == water))
  400. {
  401. for (CGObjectInstance* obj : tile.visitableObjects)
  402. {
  403. if (obj->ID == Obj::MONSTER && checkForVisitableDir(pos, &posTile, originalPos)) // Monster being able to attack investigated tile
  404. {
  405. return pos;
  406. }
  407. }
  408. }
  409. }
  410. pos.y++;
  411. }
  412. pos.y -= 3;
  413. pos.x++;
  414. }
  415. return int3(-1, -1, -1);
  416. }
  417. const CGObjectInstance * CMap::getObjectiveObjectFrom(const int3 & pos, Obj::EObj type)
  418. {
  419. for (CGObjectInstance * object : getTile(pos).visitableObjects)
  420. {
  421. if (object->ID == type)
  422. return object;
  423. }
  424. // There is weird bug because of which sometimes heroes will not be found properly despite having correct position
  425. // Try to workaround that and find closest object that we can use
  426. logGlobal->error("Failed to find object of type %d at %s", static_cast<int>(type), pos.toString());
  427. logGlobal->error("Will try to find closest matching object");
  428. CGObjectInstance * bestMatch = nullptr;
  429. for (CGObjectInstance * object : objects)
  430. {
  431. if (object && object->ID == type)
  432. {
  433. if (bestMatch == nullptr)
  434. bestMatch = object;
  435. else
  436. {
  437. if (object->pos.dist2dSQ(pos) < bestMatch->pos.dist2dSQ(pos))
  438. bestMatch = object;// closer than one we already found
  439. }
  440. }
  441. }
  442. assert(bestMatch != nullptr); // if this happens - victory conditions or map itself is very, very broken
  443. logGlobal->error("Will use %s from %s", bestMatch->getObjectName(), bestMatch->pos.toString());
  444. return bestMatch;
  445. }
  446. void CMap::checkForObjectives()
  447. {
  448. // NOTE: probably should be moved to MapFormatH3M.cpp
  449. for (TriggeredEvent & event : triggeredEvents)
  450. {
  451. auto patcher = [&](EventCondition cond) -> EventExpression::Variant
  452. {
  453. switch (cond.condition)
  454. {
  455. case EventCondition::HAVE_ARTIFACT:
  456. boost::algorithm::replace_first(event.onFulfill, "%s", VLC->arth->objects[cond.objectType]->getNameTranslated());
  457. break;
  458. case EventCondition::HAVE_CREATURES:
  459. boost::algorithm::replace_first(event.onFulfill, "%s", VLC->creh->objects[cond.objectType]->getNameSingularTranslated());
  460. boost::algorithm::replace_first(event.onFulfill, "%d", std::to_string(cond.value));
  461. break;
  462. case EventCondition::HAVE_RESOURCES:
  463. boost::algorithm::replace_first(event.onFulfill, "%s", VLC->generaltexth->restypes[cond.objectType]);
  464. boost::algorithm::replace_first(event.onFulfill, "%d", std::to_string(cond.value));
  465. break;
  466. case EventCondition::HAVE_BUILDING:
  467. if (isInTheMap(cond.position))
  468. cond.object = getObjectiveObjectFrom(cond.position, Obj::TOWN);
  469. break;
  470. case EventCondition::CONTROL:
  471. if (isInTheMap(cond.position))
  472. cond.object = getObjectiveObjectFrom(cond.position, static_cast<Obj::EObj>(cond.objectType));
  473. if (cond.object)
  474. {
  475. const auto * town = dynamic_cast<const CGTownInstance *>(cond.object);
  476. if (town)
  477. boost::algorithm::replace_first(event.onFulfill, "%s", town->getNameTranslated());
  478. const auto * hero = dynamic_cast<const CGHeroInstance *>(cond.object);
  479. if (hero)
  480. boost::algorithm::replace_first(event.onFulfill, "%s", hero->getNameTranslated());
  481. }
  482. break;
  483. case EventCondition::DESTROY:
  484. if (isInTheMap(cond.position))
  485. cond.object = getObjectiveObjectFrom(cond.position, static_cast<Obj::EObj>(cond.objectType));
  486. if (cond.object)
  487. {
  488. const auto * hero = dynamic_cast<const CGHeroInstance *>(cond.object);
  489. if (hero)
  490. boost::algorithm::replace_first(event.onFulfill, "%s", hero->getNameTranslated());
  491. }
  492. break;
  493. case EventCondition::TRANSPORT:
  494. cond.object = getObjectiveObjectFrom(cond.position, Obj::TOWN);
  495. break;
  496. //break; case EventCondition::DAYS_PASSED:
  497. //break; case EventCondition::IS_HUMAN:
  498. //break; case EventCondition::DAYS_WITHOUT_TOWN:
  499. //break; case EventCondition::STANDARD_WIN:
  500. //TODO: support new condition format
  501. case EventCondition::HAVE_0:
  502. break;
  503. case EventCondition::DESTROY_0:
  504. break;
  505. case EventCondition::HAVE_BUILDING_0:
  506. break;
  507. }
  508. return cond;
  509. };
  510. event.trigger = event.trigger.morph(patcher);
  511. }
  512. }
  513. void CMap::addNewArtifactInstance(CArtifactInstance * art)
  514. {
  515. art->id = ArtifactInstanceID(static_cast<si32>(artInstances.size()));
  516. artInstances.emplace_back(art);
  517. }
  518. void CMap::eraseArtifactInstance(CArtifactInstance * art)
  519. {
  520. //TODO: handle for artifacts removed in map editor
  521. assert(artInstances[art->id.getNum()] == art);
  522. artInstances[art->id.getNum()].dellNull();
  523. }
  524. void CMap::addNewQuestInstance(CQuest* quest)
  525. {
  526. quest->qid = static_cast<si32>(quests.size());
  527. quests.emplace_back(quest);
  528. }
  529. void CMap::removeQuestInstance(CQuest * quest)
  530. {
  531. //TODO: should be called only by map editor.
  532. //During game, completed quests or quests from removed objects stay forever
  533. //Shift indexes
  534. auto iter = std::next(quests.begin(), quest->qid);
  535. iter = quests.erase(iter);
  536. for (int i = quest->qid; iter != quests.end(); ++i, ++iter)
  537. {
  538. (*iter)->qid = i;
  539. }
  540. }
  541. void CMap::setUniqueInstanceName(CGObjectInstance * obj)
  542. {
  543. //this gives object unique name even if objects are removed later
  544. auto uid = uidCounter++;
  545. boost::format fmt("%s_%d");
  546. fmt % obj->typeName % uid;
  547. obj->instanceName = fmt.str();
  548. }
  549. void CMap::addNewObject(CGObjectInstance * obj)
  550. {
  551. if(obj->id != ObjectInstanceID(static_cast<si32>(objects.size())))
  552. throw std::runtime_error("Invalid object instance id");
  553. if(obj->instanceName.empty())
  554. throw std::runtime_error("Object instance name missing");
  555. if (vstd::contains(instanceNames, obj->instanceName))
  556. throw std::runtime_error("Object instance name duplicated: "+obj->instanceName);
  557. objects.emplace_back(obj);
  558. instanceNames[obj->instanceName] = obj;
  559. addBlockVisTiles(obj);
  560. //TODO: how about defeated heroes recruited again?
  561. obj->afterAddToMap(this);
  562. }
  563. void CMap::moveObject(CGObjectInstance * obj, const int3 & pos)
  564. {
  565. removeBlockVisTiles(obj);
  566. obj->pos = pos;
  567. addBlockVisTiles(obj);
  568. }
  569. void CMap::removeObject(CGObjectInstance * obj)
  570. {
  571. removeBlockVisTiles(obj);
  572. instanceNames.erase(obj->instanceName);
  573. //update indeces
  574. auto iter = std::next(objects.begin(), obj->id.getNum());
  575. iter = objects.erase(iter);
  576. for(int i = obj->id.getNum(); iter != objects.end(); ++i, ++iter)
  577. {
  578. (*iter)->id = ObjectInstanceID(i);
  579. }
  580. obj->afterRemoveFromMap(this);
  581. //TOOD: Clean artifact instances (mostly worn by hero?) and quests related to this object
  582. }
  583. void CMap::initTerrain()
  584. {
  585. int level = levels();
  586. terrain = new TerrainTile**[level];
  587. guardingCreaturePositions = new int3**[level];
  588. for(int z = 0; z < level; ++z)
  589. {
  590. terrain[z] = new TerrainTile*[width];
  591. guardingCreaturePositions[z] = new int3*[width];
  592. for(int x = 0; x < width; ++x)
  593. {
  594. terrain[z][x] = new TerrainTile[height];
  595. guardingCreaturePositions[z][x] = new int3[height];
  596. }
  597. }
  598. }
  599. CMapEditManager * CMap::getEditManager()
  600. {
  601. if(!editManager) editManager = std::make_unique<CMapEditManager>(this);
  602. return editManager.get();
  603. }
  604. void CMap::resetStaticData()
  605. {
  606. CGKeys::reset();
  607. CGMagi::reset();
  608. CGObelisk::reset();
  609. CGTownInstance::reset();
  610. }
  611. VCMI_LIB_NAMESPACE_END