CMap.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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 "../mapObjects/CObjectClassesHandler.h"
  18. #include "../mapObjects/CGHeroInstance.h"
  19. #include "../CGeneralTextHandler.h"
  20. #include "../spells/CSpellHandler.h"
  21. #include "../CSkillHandler.h"
  22. #include "CMapEditManager.h"
  23. #include "../serializer/JsonSerializeFormat.h"
  24. VCMI_LIB_NAMESPACE_BEGIN
  25. SHeroName::SHeroName() : heroId(-1)
  26. {
  27. }
  28. PlayerInfo::PlayerInfo(): canHumanPlay(false), canComputerPlay(false),
  29. aiTactic(EAiTactic::RANDOM), isFactionRandom(false), hasRandomHero(false), mainCustomHeroPortrait(-1), mainCustomHeroId(-1), hasMainTown(false),
  30. generateHeroAtMainTown(false), posOfMainTown(-1), team(TeamID::NO_TEAM), /* following are unused */ generateHero(false), p7(0), powerPlaceholders(-1)
  31. {
  32. allowedFactions = VLC->townh->getAllowedFactions();
  33. }
  34. si8 PlayerInfo::defaultCastle() const
  35. {
  36. //if random allowed set it as default
  37. if(isFactionRandom)
  38. return -1;
  39. if(!allowedFactions.empty())
  40. return *allowedFactions.begin();
  41. // fall back to random
  42. return -1;
  43. }
  44. si8 PlayerInfo::defaultHero() const
  45. {
  46. // we will generate hero in front of main town
  47. if((generateHeroAtMainTown && hasMainTown) || hasRandomHero)
  48. {
  49. //random hero
  50. return -1;
  51. }
  52. return -2;
  53. }
  54. bool PlayerInfo::canAnyonePlay() const
  55. {
  56. return canHumanPlay || canComputerPlay;
  57. }
  58. bool PlayerInfo::hasCustomMainHero() const
  59. {
  60. return !mainCustomHeroName.empty() && mainCustomHeroPortrait != -1;
  61. }
  62. EventCondition::EventCondition(EWinLoseType condition):
  63. object(nullptr),
  64. metaType(EMetaclass::INVALID),
  65. value(-1),
  66. objectType(-1),
  67. objectSubtype(-1),
  68. position(-1, -1, -1),
  69. condition(condition)
  70. {
  71. }
  72. EventCondition::EventCondition(EWinLoseType condition, si32 value, si32 objectType, int3 position):
  73. object(nullptr),
  74. metaType(EMetaclass::INVALID),
  75. value(value),
  76. objectType(objectType),
  77. objectSubtype(-1),
  78. position(position),
  79. condition(condition)
  80. {}
  81. void Rumor::serializeJson(JsonSerializeFormat & handler)
  82. {
  83. handler.serializeString("name", name);
  84. handler.serializeString("text", text);
  85. }
  86. DisposedHero::DisposedHero() : heroId(0), portrait(255), players(0)
  87. {
  88. }
  89. CMapEvent::CMapEvent() : players(0), humanAffected(0), computerAffected(0),
  90. firstOccurence(0), nextOccurence(0)
  91. {
  92. }
  93. bool CMapEvent::earlierThan(const CMapEvent & other) const
  94. {
  95. return firstOccurence < other.firstOccurence;
  96. }
  97. bool CMapEvent::earlierThanOrEqual(const CMapEvent & other) const
  98. {
  99. return firstOccurence <= other.firstOccurence;
  100. }
  101. CCastleEvent::CCastleEvent() : town(nullptr)
  102. {
  103. }
  104. TerrainTile::TerrainTile():
  105. terType(nullptr),
  106. terView(0),
  107. riverType(const_cast<RiverType*>(&VLC->terrainTypeHandler->rivers()[River::NO_RIVER])),
  108. riverDir(0),
  109. roadType(const_cast<RoadType*>(&VLC->terrainTypeHandler->roads()[Road::NO_ROAD])),
  110. roadDir(0),
  111. extTileFlags(0),
  112. visitable(false),
  113. blocked(false)
  114. {
  115. }
  116. bool TerrainTile::entrableTerrain(const TerrainTile * from) const
  117. {
  118. return entrableTerrain(from ? from->terType->isLand() : true, from ? from->terType->isWater() : true);
  119. }
  120. bool TerrainTile::entrableTerrain(bool allowLand, bool allowSea) const
  121. {
  122. return terType->isPassable()
  123. && ((allowSea && terType->isWater()) || (allowLand && terType->isLand()));
  124. }
  125. bool TerrainTile::isClear(const TerrainTile * from) const
  126. {
  127. return entrableTerrain(from) && !blocked;
  128. }
  129. Obj TerrainTile::topVisitableId(bool excludeTop) const
  130. {
  131. return topVisitableObj(excludeTop) ? topVisitableObj(excludeTop)->ID : Obj(Obj::NO_OBJ);
  132. }
  133. CGObjectInstance * TerrainTile::topVisitableObj(bool excludeTop) const
  134. {
  135. if(visitableObjects.empty() || (excludeTop && visitableObjects.size() == 1))
  136. return nullptr;
  137. if(excludeTop)
  138. return visitableObjects[visitableObjects.size()-2];
  139. return visitableObjects.back();
  140. }
  141. EDiggingStatus TerrainTile::getDiggingStatus(const bool excludeTop) const
  142. {
  143. if(terType->isWater() || !terType->isPassable())
  144. return EDiggingStatus::WRONG_TERRAIN;
  145. int allowedBlocked = excludeTop ? 1 : 0;
  146. if(blockingObjects.size() > allowedBlocked || topVisitableObj(excludeTop))
  147. return EDiggingStatus::TILE_OCCUPIED;
  148. else
  149. return EDiggingStatus::CAN_DIG;
  150. }
  151. bool TerrainTile::hasFavorableWinds() const
  152. {
  153. return extTileFlags & 128;
  154. }
  155. bool TerrainTile::isWater() const
  156. {
  157. return terType->isWater();
  158. }
  159. void CMapHeader::setupEvents()
  160. {
  161. EventCondition victoryCondition(EventCondition::STANDARD_WIN);
  162. EventCondition defeatCondition(EventCondition::DAYS_WITHOUT_TOWN);
  163. defeatCondition.value = 7;
  164. //Victory condition - defeat all
  165. TriggeredEvent standardVictory;
  166. standardVictory.effect.type = EventEffect::VICTORY;
  167. standardVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[5];
  168. standardVictory.identifier = "standardVictory";
  169. standardVictory.description.clear(); // TODO: display in quest window
  170. standardVictory.onFulfill = VLC->generaltexth->allTexts[659];
  171. standardVictory.trigger = EventExpression(victoryCondition);
  172. //Loss condition - 7 days without town
  173. TriggeredEvent standardDefeat;
  174. standardDefeat.effect.type = EventEffect::DEFEAT;
  175. standardDefeat.effect.toOtherMessage = VLC->generaltexth->allTexts[8];
  176. standardDefeat.identifier = "standardDefeat";
  177. standardDefeat.description.clear(); // TODO: display in quest window
  178. standardDefeat.onFulfill = VLC->generaltexth->allTexts[7];
  179. standardDefeat.trigger = EventExpression(defeatCondition);
  180. triggeredEvents.push_back(standardVictory);
  181. triggeredEvents.push_back(standardDefeat);
  182. victoryIconIndex = 11;
  183. victoryMessage = VLC->generaltexth->victoryConditions[0];
  184. defeatIconIndex = 3;
  185. defeatMessage = VLC->generaltexth->lossCondtions[0];
  186. }
  187. CMapHeader::CMapHeader() : version(EMapFormat::SOD), height(72), width(72),
  188. twoLevel(true), difficulty(1), levelLimit(0), howManyTeams(0), areAnyPlayers(false)
  189. {
  190. setupEvents();
  191. allowedHeroes = VLC->heroh->getDefaultAllowed();
  192. players.resize(PlayerColor::PLAYER_LIMIT_I);
  193. }
  194. CMapHeader::~CMapHeader()
  195. {
  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 (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. if(pos.x < 0 || pos.y < 0 || pos.z < 0 || pos.x >= width || pos.y >= height
  334. || pos.z > (twoLevel ? 1 : 0))
  335. {
  336. return false;
  337. }
  338. else
  339. {
  340. return true;
  341. }
  342. }
  343. TerrainTile & CMap::getTile(const int3 & tile)
  344. {
  345. assert(isInTheMap(tile));
  346. return terrain[tile.z][tile.x][tile.y];
  347. }
  348. const TerrainTile & CMap::getTile(const int3 & tile) const
  349. {
  350. assert(isInTheMap(tile));
  351. return terrain[tile.z][tile.x][tile.y];
  352. }
  353. bool CMap::isWaterTile(const int3 &pos) const
  354. {
  355. return isInTheMap(pos) && getTile(pos).isWater();
  356. }
  357. bool CMap::canMoveBetween(const int3 &src, const int3 &dst) const
  358. {
  359. const TerrainTile * dstTile = &getTile(dst);
  360. const TerrainTile * srcTile = &getTile(src);
  361. return checkForVisitableDir(src, dstTile, dst) && checkForVisitableDir(dst, srcTile, src);
  362. }
  363. bool CMap::checkForVisitableDir(const int3 & src, const TerrainTile *pom, const int3 & dst ) const
  364. {
  365. if (!pom->entrableTerrain()) //rock is never accessible
  366. return false;
  367. for (auto obj : pom->visitableObjects) //checking destination tile
  368. {
  369. if(!vstd::contains(pom->blockingObjects, obj)) //this visitable object is not blocking, ignore
  370. continue;
  371. if (!obj->appearance->isVisitableFrom(src.x - dst.x, src.y - dst.y))
  372. return false;
  373. }
  374. return true;
  375. }
  376. int3 CMap::guardingCreaturePosition (int3 pos) const
  377. {
  378. const int3 originalPos = pos;
  379. // Give monster at position priority.
  380. if (!isInTheMap(pos))
  381. return int3(-1, -1, -1);
  382. const TerrainTile &posTile = getTile(pos);
  383. if (posTile.visitable)
  384. {
  385. for (CGObjectInstance* obj : posTile.visitableObjects)
  386. {
  387. if(obj->blockVisit)
  388. {
  389. if (obj->ID == Obj::MONSTER) // Monster
  390. return pos;
  391. else
  392. return int3(-1, -1, -1); //blockvis objects are not guarded by neighbouring creatures
  393. }
  394. }
  395. }
  396. // See if there are any monsters adjacent.
  397. bool water = posTile.isWater();
  398. pos -= int3(1, 1, 0); // Start with top left.
  399. for (int dx = 0; dx < 3; dx++)
  400. {
  401. for (int dy = 0; dy < 3; dy++)
  402. {
  403. if (isInTheMap(pos))
  404. {
  405. const auto & tile = getTile(pos);
  406. if (tile.visitable && (tile.isWater() == water))
  407. {
  408. for (CGObjectInstance* obj : tile.visitableObjects)
  409. {
  410. if (obj->ID == Obj::MONSTER && checkForVisitableDir(pos, &posTile, originalPos)) // Monster being able to attack investigated tile
  411. {
  412. return pos;
  413. }
  414. }
  415. }
  416. }
  417. pos.y++;
  418. }
  419. pos.y -= 3;
  420. pos.x++;
  421. }
  422. return int3(-1, -1, -1);
  423. }
  424. const CGObjectInstance * CMap::getObjectiveObjectFrom(int3 pos, Obj::EObj type)
  425. {
  426. for (CGObjectInstance * object : getTile(pos).visitableObjects)
  427. {
  428. if (object->ID == type)
  429. return object;
  430. }
  431. // There is weird bug because of which sometimes heroes will not be found properly despite having correct position
  432. // Try to workaround that and find closest object that we can use
  433. logGlobal->error("Failed to find object of type %d at %s", int(type), pos.toString());
  434. logGlobal->error("Will try to find closest matching object");
  435. CGObjectInstance * bestMatch = nullptr;
  436. for (CGObjectInstance * object : objects)
  437. {
  438. if (object && object->ID == type)
  439. {
  440. if (bestMatch == nullptr)
  441. bestMatch = object;
  442. else
  443. {
  444. if (object->pos.dist2dSQ(pos) < bestMatch->pos.dist2dSQ(pos))
  445. bestMatch = object;// closer than one we already found
  446. }
  447. }
  448. }
  449. assert(bestMatch != nullptr); // if this happens - victory conditions or map itself is very, very broken
  450. logGlobal->error("Will use %s from %s", bestMatch->getObjectName(), bestMatch->pos.toString());
  451. return bestMatch;
  452. }
  453. void CMap::checkForObjectives()
  454. {
  455. // NOTE: probably should be moved to MapFormatH3M.cpp
  456. for (TriggeredEvent & event : triggeredEvents)
  457. {
  458. auto patcher = [&](EventCondition cond) -> EventExpression::Variant
  459. {
  460. switch (cond.condition)
  461. {
  462. case EventCondition::HAVE_ARTIFACT:
  463. boost::algorithm::replace_first(event.onFulfill, "%s", VLC->arth->objects[cond.objectType]->getName());
  464. break;
  465. case EventCondition::HAVE_CREATURES:
  466. boost::algorithm::replace_first(event.onFulfill, "%s", VLC->creh->objects[cond.objectType]->nameSing);
  467. boost::algorithm::replace_first(event.onFulfill, "%d", boost::lexical_cast<std::string>(cond.value));
  468. break;
  469. case EventCondition::HAVE_RESOURCES:
  470. boost::algorithm::replace_first(event.onFulfill, "%s", VLC->generaltexth->restypes[cond.objectType]);
  471. boost::algorithm::replace_first(event.onFulfill, "%d", boost::lexical_cast<std::string>(cond.value));
  472. break;
  473. case EventCondition::HAVE_BUILDING:
  474. if (isInTheMap(cond.position))
  475. cond.object = getObjectiveObjectFrom(cond.position, Obj::TOWN);
  476. break;
  477. case EventCondition::CONTROL:
  478. if (isInTheMap(cond.position))
  479. cond.object = getObjectiveObjectFrom(cond.position, Obj::EObj(cond.objectType));
  480. if (cond.object)
  481. {
  482. const CGTownInstance *town = dynamic_cast<const CGTownInstance*>(cond.object);
  483. if (town)
  484. boost::algorithm::replace_first(event.onFulfill, "%s", town->name);
  485. const CGHeroInstance *hero = dynamic_cast<const CGHeroInstance*>(cond.object);
  486. if (hero)
  487. boost::algorithm::replace_first(event.onFulfill, "%s", hero->name);
  488. }
  489. break;
  490. case EventCondition::DESTROY:
  491. if (isInTheMap(cond.position))
  492. cond.object = getObjectiveObjectFrom(cond.position, Obj::EObj(cond.objectType));
  493. if (cond.object)
  494. {
  495. const CGHeroInstance *hero = dynamic_cast<const CGHeroInstance*>(cond.object);
  496. if (hero)
  497. boost::algorithm::replace_first(event.onFulfill, "%s", hero->name);
  498. }
  499. break;
  500. case EventCondition::TRANSPORT:
  501. cond.object = getObjectiveObjectFrom(cond.position, Obj::TOWN);
  502. break;
  503. //break; case EventCondition::DAYS_PASSED:
  504. //break; case EventCondition::IS_HUMAN:
  505. //break; case EventCondition::DAYS_WITHOUT_TOWN:
  506. //break; case EventCondition::STANDARD_WIN:
  507. //TODO: support new condition format
  508. case EventCondition::HAVE_0:
  509. break;
  510. case EventCondition::DESTROY_0:
  511. break;
  512. case EventCondition::HAVE_BUILDING_0:
  513. break;
  514. }
  515. return cond;
  516. };
  517. event.trigger = event.trigger.morph(patcher);
  518. }
  519. }
  520. void CMap::addNewArtifactInstance(CArtifactInstance * art)
  521. {
  522. art->id = ArtifactInstanceID((si32)artInstances.size());
  523. artInstances.push_back(art);
  524. }
  525. void CMap::eraseArtifactInstance(CArtifactInstance * art)
  526. {
  527. //TODO: handle for artifacts removed in map editor
  528. assert(artInstances[art->id.getNum()] == art);
  529. artInstances[art->id.getNum()].dellNull();
  530. }
  531. void CMap::addNewQuestInstance(CQuest* quest)
  532. {
  533. quest->qid = static_cast<si32>(quests.size());
  534. quests.push_back(quest);
  535. }
  536. void CMap::removeQuestInstance(CQuest * quest)
  537. {
  538. //TODO: should be called only by map editor.
  539. //During game, completed quests or quests from removed objects stay forever
  540. //Shift indexes
  541. auto iter = std::next(quests.begin(), quest->qid);
  542. iter = quests.erase(iter);
  543. for (int i = quest->qid; iter != quests.end(); ++i, ++iter)
  544. {
  545. (*iter)->qid = i;
  546. }
  547. }
  548. void CMap::setUniqueInstanceName(CGObjectInstance * obj)
  549. {
  550. //this gives object unique name even if objects are removed later
  551. auto uid = uidCounter++;
  552. boost::format fmt("%s_%d");
  553. fmt % obj->typeName % uid;
  554. obj->instanceName = fmt.str();
  555. }
  556. void CMap::addNewObject(CGObjectInstance * obj)
  557. {
  558. if(obj->id != ObjectInstanceID((si32)objects.size()))
  559. throw std::runtime_error("Invalid object instance id");
  560. if(obj->instanceName.empty())
  561. throw std::runtime_error("Object instance name missing");
  562. if (vstd::contains(instanceNames, obj->instanceName))
  563. throw std::runtime_error("Object instance name duplicated: "+obj->instanceName);
  564. objects.push_back(obj);
  565. instanceNames[obj->instanceName] = obj;
  566. addBlockVisTiles(obj);
  567. //TODO: how about defeated heroes recruited again?
  568. obj->afterAddToMap(this);
  569. }
  570. void CMap::moveObject(CGObjectInstance * obj, const int3 & pos)
  571. {
  572. removeBlockVisTiles(obj);
  573. obj->pos = pos;
  574. addBlockVisTiles(obj);
  575. }
  576. void CMap::removeObject(CGObjectInstance * obj)
  577. {
  578. removeBlockVisTiles(obj);
  579. instanceNames.erase(obj->instanceName);
  580. //update indeces
  581. auto iter = std::next(objects.begin(), obj->id.getNum());
  582. iter = objects.erase(iter);
  583. for(int i = obj->id.getNum(); iter != objects.end(); ++i, ++iter)
  584. {
  585. (*iter)->id = ObjectInstanceID(i);
  586. }
  587. obj->afterRemoveFromMap(this);
  588. //TOOD: Clean artifact instances (mostly worn by hero?) and quests related to this object
  589. }
  590. void CMap::initTerrain()
  591. {
  592. int level = levels();
  593. terrain = new TerrainTile**[level];
  594. guardingCreaturePositions = new int3**[level];
  595. for(int z = 0; z < level; ++z)
  596. {
  597. terrain[z] = new TerrainTile*[width];
  598. guardingCreaturePositions[z] = new int3*[width];
  599. for(int x = 0; x < width; ++x)
  600. {
  601. terrain[z][x] = new TerrainTile[height];
  602. guardingCreaturePositions[z][x] = new int3[height];
  603. }
  604. }
  605. }
  606. CMapEditManager * CMap::getEditManager()
  607. {
  608. if(!editManager) editManager = make_unique<CMapEditManager>(this);
  609. return editManager.get();
  610. }
  611. void CMap::resetStaticData()
  612. {
  613. CGKeys::reset();
  614. CGMagi::reset();
  615. CGObelisk::reset();
  616. CGTownInstance::reset();
  617. }
  618. VCMI_LIB_NAMESPACE_END