CMap.cpp 19 KB

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