CMap.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. if(terrain)
  214. {
  215. for(int z = 0; z < levels(); z++)
  216. {
  217. for(int x = 0; x < width; x++)
  218. {
  219. delete[] terrain[z][x];
  220. delete[] guardingCreaturePositions[z][x];
  221. }
  222. delete[] terrain[z];
  223. delete[] guardingCreaturePositions[z];
  224. }
  225. delete [] terrain;
  226. delete [] guardingCreaturePositions;
  227. }
  228. for(auto obj : objects)
  229. obj.dellNull();
  230. for(auto quest : quests)
  231. quest.dellNull();
  232. resetStaticData();
  233. }
  234. void CMap::removeBlockVisTiles(CGObjectInstance * obj, bool total)
  235. {
  236. const int zVal = obj->pos.z;
  237. for(int fx = 0; fx < obj->getWidth(); ++fx)
  238. {
  239. int xVal = obj->pos.x - fx;
  240. for(int fy = 0; fy < obj->getHeight(); ++fy)
  241. {
  242. int yVal = obj->pos.y - fy;
  243. if(xVal>=0 && xVal < width && yVal>=0 && yVal < height)
  244. {
  245. TerrainTile & curt = terrain[zVal][xVal][yVal];
  246. if(total || obj->visitableAt(xVal, yVal))
  247. {
  248. curt.visitableObjects -= obj;
  249. curt.visitable = curt.visitableObjects.size();
  250. }
  251. if(total || obj->blockingAt(xVal, yVal))
  252. {
  253. curt.blockingObjects -= obj;
  254. curt.blocked = curt.blockingObjects.size();
  255. }
  256. }
  257. }
  258. }
  259. }
  260. void CMap::addBlockVisTiles(CGObjectInstance * obj)
  261. {
  262. const int zVal = obj->pos.z;
  263. for(int fx = 0; fx < obj->getWidth(); ++fx)
  264. {
  265. int xVal = obj->pos.x - fx;
  266. for(int fy = 0; fy < obj->getHeight(); ++fy)
  267. {
  268. int yVal = obj->pos.y - fy;
  269. if(xVal>=0 && xVal < width && yVal >= 0 && yVal < height)
  270. {
  271. TerrainTile & curt = terrain[zVal][xVal][yVal];
  272. if(obj->visitableAt(xVal, yVal))
  273. {
  274. curt.visitableObjects.push_back(obj);
  275. curt.visitable = true;
  276. }
  277. if(obj->blockingAt(xVal, yVal))
  278. {
  279. curt.blockingObjects.push_back(obj);
  280. curt.blocked = true;
  281. }
  282. }
  283. }
  284. }
  285. }
  286. void CMap::calculateGuardingGreaturePositions()
  287. {
  288. int levels = twoLevel ? 2 : 1;
  289. for(int z = 0; z < levels; z++)
  290. {
  291. for(int x = 0; x < width; x++)
  292. {
  293. for(int y = 0; y < height; y++)
  294. {
  295. guardingCreaturePositions[z][x][y] = guardingCreaturePosition(int3(x, y, z));
  296. }
  297. }
  298. }
  299. }
  300. CGHeroInstance * CMap::getHero(int heroID)
  301. {
  302. for(auto & elem : heroesOnMap)
  303. if(elem->subID == heroID)
  304. return elem;
  305. return nullptr;
  306. }
  307. bool CMap::isCoastalTile(const int3 & pos) const
  308. {
  309. //todo: refactoring: extract neighbor tile iterator and use it in GameState
  310. static const int3 dirs[] = { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0),
  311. int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) };
  312. if(!isInTheMap(pos))
  313. {
  314. logGlobal->error("Coastal check outside of map: %s", pos.toString());
  315. return false;
  316. }
  317. if(isWaterTile(pos))
  318. return false;
  319. for (auto & dir : dirs)
  320. {
  321. const int3 hlp = pos + dir;
  322. if(!isInTheMap(hlp))
  323. continue;
  324. const TerrainTile &hlpt = getTile(hlp);
  325. if(hlpt.isWater())
  326. return true;
  327. }
  328. return false;
  329. }
  330. bool CMap::isInTheMap(const int3 & pos) const
  331. {
  332. if(pos.x < 0 || pos.y < 0 || pos.z < 0 || pos.x >= width || pos.y >= height
  333. || pos.z > (twoLevel ? 1 : 0))
  334. {
  335. return false;
  336. }
  337. else
  338. {
  339. return true;
  340. }
  341. }
  342. TerrainTile & CMap::getTile(const int3 & tile)
  343. {
  344. assert(isInTheMap(tile));
  345. return terrain[tile.z][tile.x][tile.y];
  346. }
  347. const TerrainTile & CMap::getTile(const int3 & tile) const
  348. {
  349. assert(isInTheMap(tile));
  350. return terrain[tile.z][tile.x][tile.y];
  351. }
  352. bool CMap::isWaterTile(const int3 &pos) const
  353. {
  354. return isInTheMap(pos) && getTile(pos).isWater();
  355. }
  356. bool CMap::canMoveBetween(const int3 &src, const int3 &dst) const
  357. {
  358. const TerrainTile * dstTile = &getTile(dst);
  359. const TerrainTile * srcTile = &getTile(src);
  360. return checkForVisitableDir(src, dstTile, dst) && checkForVisitableDir(dst, srcTile, src);
  361. }
  362. bool CMap::checkForVisitableDir(const int3 & src, const TerrainTile *pom, const int3 & dst ) const
  363. {
  364. if (!pom->entrableTerrain()) //rock is never accessible
  365. return false;
  366. for (auto obj : pom->visitableObjects) //checking destination tile
  367. {
  368. if(!vstd::contains(pom->blockingObjects, obj)) //this visitable object is not blocking, ignore
  369. continue;
  370. if (!obj->appearance->isVisitableFrom(src.x - dst.x, src.y - dst.y))
  371. return false;
  372. }
  373. return true;
  374. }
  375. int3 CMap::guardingCreaturePosition (int3 pos) const
  376. {
  377. const int3 originalPos = pos;
  378. // Give monster at position priority.
  379. if (!isInTheMap(pos))
  380. return int3(-1, -1, -1);
  381. const TerrainTile &posTile = getTile(pos);
  382. if (posTile.visitable)
  383. {
  384. for (CGObjectInstance* obj : posTile.visitableObjects)
  385. {
  386. if(obj->blockVisit)
  387. {
  388. if (obj->ID == Obj::MONSTER) // Monster
  389. return pos;
  390. else
  391. return int3(-1, -1, -1); //blockvis objects are not guarded by neighbouring creatures
  392. }
  393. }
  394. }
  395. // See if there are any monsters adjacent.
  396. bool water = posTile.isWater();
  397. pos -= int3(1, 1, 0); // Start with top left.
  398. for (int dx = 0; dx < 3; dx++)
  399. {
  400. for (int dy = 0; dy < 3; dy++)
  401. {
  402. if (isInTheMap(pos))
  403. {
  404. const auto & tile = getTile(pos);
  405. if (tile.visitable && (tile.isWater() == water))
  406. {
  407. for (CGObjectInstance* obj : tile.visitableObjects)
  408. {
  409. if (obj->ID == Obj::MONSTER && checkForVisitableDir(pos, &posTile, originalPos)) // Monster being able to attack investigated tile
  410. {
  411. return pos;
  412. }
  413. }
  414. }
  415. }
  416. pos.y++;
  417. }
  418. pos.y -= 3;
  419. pos.x++;
  420. }
  421. return int3(-1, -1, -1);
  422. }
  423. const CGObjectInstance * CMap::getObjectiveObjectFrom(int3 pos, Obj::EObj type)
  424. {
  425. for (CGObjectInstance * object : getTile(pos).visitableObjects)
  426. {
  427. if (object->ID == type)
  428. return object;
  429. }
  430. // There is weird bug because of which sometimes heroes will not be found properly despite having correct position
  431. // Try to workaround that and find closest object that we can use
  432. logGlobal->error("Failed to find object of type %d at %s", int(type), pos.toString());
  433. logGlobal->error("Will try to find closest matching object");
  434. CGObjectInstance * bestMatch = nullptr;
  435. for (CGObjectInstance * object : objects)
  436. {
  437. if (object && object->ID == type)
  438. {
  439. if (bestMatch == nullptr)
  440. bestMatch = object;
  441. else
  442. {
  443. if (object->pos.dist2dSQ(pos) < bestMatch->pos.dist2dSQ(pos))
  444. bestMatch = object;// closer than one we already found
  445. }
  446. }
  447. }
  448. assert(bestMatch != nullptr); // if this happens - victory conditions or map itself is very, very broken
  449. logGlobal->error("Will use %s from %s", bestMatch->getObjectName(), bestMatch->pos.toString());
  450. return bestMatch;
  451. }
  452. void CMap::checkForObjectives()
  453. {
  454. // NOTE: probably should be moved to MapFormatH3M.cpp
  455. for (TriggeredEvent & event : triggeredEvents)
  456. {
  457. auto patcher = [&](EventCondition cond) -> EventExpression::Variant
  458. {
  459. switch (cond.condition)
  460. {
  461. case EventCondition::HAVE_ARTIFACT:
  462. boost::algorithm::replace_first(event.onFulfill, "%s", VLC->arth->objects[cond.objectType]->getName());
  463. break;
  464. case EventCondition::HAVE_CREATURES:
  465. boost::algorithm::replace_first(event.onFulfill, "%s", VLC->creh->objects[cond.objectType]->nameSing);
  466. boost::algorithm::replace_first(event.onFulfill, "%d", boost::lexical_cast<std::string>(cond.value));
  467. break;
  468. case EventCondition::HAVE_RESOURCES:
  469. boost::algorithm::replace_first(event.onFulfill, "%s", VLC->generaltexth->restypes[cond.objectType]);
  470. boost::algorithm::replace_first(event.onFulfill, "%d", boost::lexical_cast<std::string>(cond.value));
  471. break;
  472. case EventCondition::HAVE_BUILDING:
  473. if (isInTheMap(cond.position))
  474. cond.object = getObjectiveObjectFrom(cond.position, Obj::TOWN);
  475. break;
  476. case EventCondition::CONTROL:
  477. if (isInTheMap(cond.position))
  478. cond.object = getObjectiveObjectFrom(cond.position, Obj::EObj(cond.objectType));
  479. if (cond.object)
  480. {
  481. const CGTownInstance *town = dynamic_cast<const CGTownInstance*>(cond.object);
  482. if (town)
  483. boost::algorithm::replace_first(event.onFulfill, "%s", town->name);
  484. const CGHeroInstance *hero = dynamic_cast<const CGHeroInstance*>(cond.object);
  485. if (hero)
  486. boost::algorithm::replace_first(event.onFulfill, "%s", hero->name);
  487. }
  488. break;
  489. case EventCondition::DESTROY:
  490. if (isInTheMap(cond.position))
  491. cond.object = getObjectiveObjectFrom(cond.position, Obj::EObj(cond.objectType));
  492. if (cond.object)
  493. {
  494. const CGHeroInstance *hero = dynamic_cast<const CGHeroInstance*>(cond.object);
  495. if (hero)
  496. boost::algorithm::replace_first(event.onFulfill, "%s", hero->name);
  497. }
  498. break;
  499. case EventCondition::TRANSPORT:
  500. cond.object = getObjectiveObjectFrom(cond.position, Obj::TOWN);
  501. break;
  502. //break; case EventCondition::DAYS_PASSED:
  503. //break; case EventCondition::IS_HUMAN:
  504. //break; case EventCondition::DAYS_WITHOUT_TOWN:
  505. //break; case EventCondition::STANDARD_WIN:
  506. //TODO: support new condition format
  507. case EventCondition::HAVE_0:
  508. break;
  509. case EventCondition::DESTROY_0:
  510. break;
  511. case EventCondition::HAVE_BUILDING_0:
  512. break;
  513. }
  514. return cond;
  515. };
  516. event.trigger = event.trigger.morph(patcher);
  517. }
  518. }
  519. void CMap::addNewArtifactInstance(CArtifactInstance * art)
  520. {
  521. art->id = ArtifactInstanceID((si32)artInstances.size());
  522. artInstances.push_back(art);
  523. }
  524. void CMap::eraseArtifactInstance(CArtifactInstance * art)
  525. {
  526. //TODO: handle for artifacts removed in map editor
  527. assert(artInstances[art->id.getNum()] == art);
  528. artInstances[art->id.getNum()].dellNull();
  529. }
  530. void CMap::addNewQuestInstance(CQuest* quest)
  531. {
  532. quest->qid = static_cast<si32>(quests.size());
  533. quests.push_back(quest);
  534. }
  535. void CMap::removeQuestInstance(CQuest * quest)
  536. {
  537. //TODO: should be called only by map editor.
  538. //During game, completed quests or quests from removed objects stay forever
  539. //Shift indexes
  540. auto iter = std::next(quests.begin(), quest->qid);
  541. iter = quests.erase(iter);
  542. for (int i = quest->qid; iter != quests.end(); ++i, ++iter)
  543. {
  544. (*iter)->qid = i;
  545. }
  546. }
  547. void CMap::setUniqueInstanceName(CGObjectInstance * obj)
  548. {
  549. //this gives object unique name even if objects are removed later
  550. auto uid = uidCounter++;
  551. boost::format fmt("%s_%d");
  552. fmt % obj->typeName % uid;
  553. obj->instanceName = fmt.str();
  554. }
  555. void CMap::addNewObject(CGObjectInstance * obj)
  556. {
  557. if(obj->id != ObjectInstanceID((si32)objects.size()))
  558. throw std::runtime_error("Invalid object instance id");
  559. if(obj->instanceName.empty())
  560. throw std::runtime_error("Object instance name missing");
  561. if (vstd::contains(instanceNames, obj->instanceName))
  562. throw std::runtime_error("Object instance name duplicated: "+obj->instanceName);
  563. objects.push_back(obj);
  564. instanceNames[obj->instanceName] = obj;
  565. addBlockVisTiles(obj);
  566. //TODO: how about defeated heroes recruited again?
  567. obj->afterAddToMap(this);
  568. }
  569. void CMap::moveObject(CGObjectInstance * obj, const int3 & pos)
  570. {
  571. removeBlockVisTiles(obj);
  572. obj->pos = pos;
  573. addBlockVisTiles(obj);
  574. }
  575. void CMap::removeObject(CGObjectInstance * obj)
  576. {
  577. removeBlockVisTiles(obj);
  578. instanceNames.erase(obj->instanceName);
  579. //update indeces
  580. auto iter = std::next(objects.begin(), obj->id.getNum());
  581. iter = objects.erase(iter);
  582. for(int i = obj->id.getNum(); iter != objects.end(); ++i, ++iter)
  583. {
  584. (*iter)->id = ObjectInstanceID(i);
  585. }
  586. obj->afterRemoveFromMap(this);
  587. //TOOD: Clean artifact instances (mostly worn by hero?) and quests related to this object
  588. }
  589. void CMap::initTerrain()
  590. {
  591. int level = levels();
  592. terrain = new TerrainTile**[level];
  593. guardingCreaturePositions = new int3**[level];
  594. for(int z = 0; z < level; ++z)
  595. {
  596. terrain[z] = new TerrainTile*[width];
  597. guardingCreaturePositions[z] = new int3*[width];
  598. for(int x = 0; x < width; ++x)
  599. {
  600. terrain[z][x] = new TerrainTile[height];
  601. guardingCreaturePositions[z][x] = new int3[height];
  602. }
  603. }
  604. }
  605. CMapEditManager * CMap::getEditManager()
  606. {
  607. if(!editManager) editManager = make_unique<CMapEditManager>(this);
  608. return editManager.get();
  609. }
  610. void CMap::resetStaticData()
  611. {
  612. CGKeys::reset();
  613. CGMagi::reset();
  614. CGObelisk::reset();
  615. CGTownInstance::reset();
  616. }
  617. VCMI_LIB_NAMESPACE_END