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 "../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() : terType("BORDER"), terView(0), riverType(RIVER_NAMES[0]),
  105. riverDir(0), roadType(ROAD_NAMES[0]), roadDir(0), extTileFlags(0), visitable(false),
  106. blocked(false)
  107. {
  108. }
  109. bool TerrainTile::entrableTerrain(const TerrainTile * from) const
  110. {
  111. return entrableTerrain(from ? from->terType.isLand() : true, from ? from->terType.isWater() : true);
  112. }
  113. bool TerrainTile::entrableTerrain(bool allowLand, bool allowSea) const
  114. {
  115. return terType.isPassable()
  116. && ((allowSea && terType.isWater()) || (allowLand && terType.isLand()));
  117. }
  118. bool TerrainTile::isClear(const TerrainTile * from) const
  119. {
  120. return entrableTerrain(from) && !blocked;
  121. }
  122. Obj TerrainTile::topVisitableId(bool excludeTop) const
  123. {
  124. return topVisitableObj(excludeTop) ? topVisitableObj(excludeTop)->ID : Obj(Obj::NO_OBJ);
  125. }
  126. CGObjectInstance * TerrainTile::topVisitableObj(bool excludeTop) const
  127. {
  128. if(visitableObjects.empty() || (excludeTop && visitableObjects.size() == 1))
  129. return nullptr;
  130. if(excludeTop)
  131. return visitableObjects[visitableObjects.size()-2];
  132. return visitableObjects.back();
  133. }
  134. EDiggingStatus TerrainTile::getDiggingStatus(const bool excludeTop) const
  135. {
  136. if(terType.isWater() || !terType.isPassable())
  137. return EDiggingStatus::WRONG_TERRAIN;
  138. int allowedBlocked = excludeTop ? 1 : 0;
  139. if(blockingObjects.size() > allowedBlocked || topVisitableObj(excludeTop))
  140. return EDiggingStatus::TILE_OCCUPIED;
  141. else
  142. return EDiggingStatus::CAN_DIG;
  143. }
  144. bool TerrainTile::hasFavorableWinds() const
  145. {
  146. return extTileFlags & 128;
  147. }
  148. bool TerrainTile::isWater() const
  149. {
  150. return terType.isWater();
  151. }
  152. void CMapHeader::setupEvents()
  153. {
  154. EventCondition victoryCondition(EventCondition::STANDARD_WIN);
  155. EventCondition defeatCondition(EventCondition::DAYS_WITHOUT_TOWN);
  156. defeatCondition.value = 7;
  157. //Victory condition - defeat all
  158. TriggeredEvent standardVictory;
  159. standardVictory.effect.type = EventEffect::VICTORY;
  160. standardVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[5];
  161. standardVictory.identifier = "standardVictory";
  162. standardVictory.description = ""; // TODO: display in quest window
  163. standardVictory.onFulfill = VLC->generaltexth->allTexts[659];
  164. standardVictory.trigger = EventExpression(victoryCondition);
  165. //Loss condition - 7 days without town
  166. TriggeredEvent standardDefeat;
  167. standardDefeat.effect.type = EventEffect::DEFEAT;
  168. standardDefeat.effect.toOtherMessage = VLC->generaltexth->allTexts[8];
  169. standardDefeat.identifier = "standardDefeat";
  170. standardDefeat.description = ""; // TODO: display in quest window
  171. standardDefeat.onFulfill = VLC->generaltexth->allTexts[7];
  172. standardDefeat.trigger = EventExpression(defeatCondition);
  173. triggeredEvents.push_back(standardVictory);
  174. triggeredEvents.push_back(standardDefeat);
  175. victoryIconIndex = 11;
  176. victoryMessage = VLC->generaltexth->victoryConditions[0];
  177. defeatIconIndex = 3;
  178. defeatMessage = VLC->generaltexth->lossCondtions[0];
  179. }
  180. CMapHeader::CMapHeader() : version(EMapFormat::SOD), height(72), width(72),
  181. twoLevel(true), difficulty(1), levelLimit(0), howManyTeams(0), areAnyPlayers(false)
  182. {
  183. setupEvents();
  184. allowedHeroes = VLC->heroh->getDefaultAllowed();
  185. players.resize(PlayerColor::PLAYER_LIMIT_I);
  186. }
  187. CMapHeader::~CMapHeader()
  188. {
  189. }
  190. ui8 CMapHeader::levels() const
  191. {
  192. return (twoLevel ? 2 : 1);
  193. }
  194. CMap::CMap()
  195. : checksum(0), grailPos(-1, -1, -1), grailRadius(0), terrain(nullptr),
  196. guardingCreaturePositions(nullptr),
  197. uidCounter(0)
  198. {
  199. allHeroes.resize(allowedHeroes.size());
  200. allowedAbilities = VLC->skillh->getDefaultAllowed();
  201. allowedArtifact = VLC->arth->getDefaultAllowed();
  202. allowedSpell = VLC->spellh->getDefaultAllowed();
  203. }
  204. CMap::~CMap()
  205. {
  206. if(terrain)
  207. {
  208. for(int z = 0; z < levels(); z++)
  209. {
  210. for(int x = 0; x < width; x++)
  211. {
  212. delete[] terrain[z][x];
  213. delete[] guardingCreaturePositions[z][x];
  214. }
  215. delete[] terrain[z];
  216. delete[] guardingCreaturePositions[z];
  217. }
  218. delete [] terrain;
  219. delete [] guardingCreaturePositions;
  220. }
  221. for(auto obj : objects)
  222. obj.dellNull();
  223. for(auto quest : quests)
  224. quest.dellNull();
  225. resetStaticData();
  226. }
  227. void CMap::removeBlockVisTiles(CGObjectInstance * obj, bool total)
  228. {
  229. const int zVal = obj->pos.z;
  230. for(int fx = 0; fx < obj->getWidth(); ++fx)
  231. {
  232. int xVal = obj->pos.x - fx;
  233. for(int fy = 0; fy < obj->getHeight(); ++fy)
  234. {
  235. int yVal = obj->pos.y - fy;
  236. if(xVal>=0 && xVal < width && yVal>=0 && yVal < height)
  237. {
  238. TerrainTile & curt = terrain[zVal][xVal][yVal];
  239. if(total || obj->visitableAt(xVal, yVal))
  240. {
  241. curt.visitableObjects -= obj;
  242. curt.visitable = curt.visitableObjects.size();
  243. }
  244. if(total || obj->blockingAt(xVal, yVal))
  245. {
  246. curt.blockingObjects -= obj;
  247. curt.blocked = curt.blockingObjects.size();
  248. }
  249. }
  250. }
  251. }
  252. }
  253. void CMap::addBlockVisTiles(CGObjectInstance * obj)
  254. {
  255. const int zVal = obj->pos.z;
  256. for(int fx = 0; fx < obj->getWidth(); ++fx)
  257. {
  258. int xVal = obj->pos.x - fx;
  259. for(int fy = 0; fy < obj->getHeight(); ++fy)
  260. {
  261. int yVal = obj->pos.y - fy;
  262. if(xVal>=0 && xVal < width && yVal >= 0 && yVal < height)
  263. {
  264. TerrainTile & curt = terrain[zVal][xVal][yVal];
  265. if(obj->visitableAt(xVal, yVal))
  266. {
  267. curt.visitableObjects.push_back(obj);
  268. curt.visitable = true;
  269. }
  270. if(obj->blockingAt(xVal, yVal))
  271. {
  272. curt.blockingObjects.push_back(obj);
  273. curt.blocked = true;
  274. }
  275. }
  276. }
  277. }
  278. }
  279. void CMap::calculateGuardingGreaturePositions()
  280. {
  281. int levels = twoLevel ? 2 : 1;
  282. for(int z = 0; z < levels; z++)
  283. {
  284. for(int x = 0; x < width; x++)
  285. {
  286. for(int y = 0; y < height; y++)
  287. {
  288. guardingCreaturePositions[z][x][y] = guardingCreaturePosition(int3(x, y, z));
  289. }
  290. }
  291. }
  292. }
  293. CGHeroInstance * CMap::getHero(int heroID)
  294. {
  295. for(auto & elem : heroesOnMap)
  296. if(elem->subID == heroID)
  297. return elem;
  298. return nullptr;
  299. }
  300. bool CMap::isCoastalTile(const int3 & pos) const
  301. {
  302. //todo: refactoring: extract neighbor tile iterator and use it in GameState
  303. static const int3 dirs[] = { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0),
  304. int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) };
  305. if(!isInTheMap(pos))
  306. {
  307. logGlobal->error("Coastal check outside of map: %s", pos.toString());
  308. return false;
  309. }
  310. if(isWaterTile(pos))
  311. return false;
  312. for (auto & dir : dirs)
  313. {
  314. const int3 hlp = pos + dir;
  315. if(!isInTheMap(hlp))
  316. continue;
  317. const TerrainTile &hlpt = getTile(hlp);
  318. if(hlpt.isWater())
  319. return true;
  320. }
  321. return false;
  322. }
  323. bool CMap::isInTheMap(const int3 & pos) const
  324. {
  325. if(pos.x < 0 || pos.y < 0 || pos.z < 0 || pos.x >= width || pos.y >= height
  326. || pos.z > (twoLevel ? 1 : 0))
  327. {
  328. return false;
  329. }
  330. else
  331. {
  332. return true;
  333. }
  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(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", 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]->getName());
  456. break;
  457. case EventCondition::HAVE_CREATURES:
  458. boost::algorithm::replace_first(event.onFulfill, "%s", VLC->creh->objects[cond.objectType]->nameSing);
  459. boost::algorithm::replace_first(event.onFulfill, "%d", boost::lexical_cast<std::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", boost::lexical_cast<std::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, Obj::EObj(cond.objectType));
  472. if (cond.object)
  473. {
  474. const CGTownInstance *town = dynamic_cast<const CGTownInstance*>(cond.object);
  475. if (town)
  476. boost::algorithm::replace_first(event.onFulfill, "%s", town->name);
  477. const CGHeroInstance *hero = dynamic_cast<const CGHeroInstance*>(cond.object);
  478. if (hero)
  479. boost::algorithm::replace_first(event.onFulfill, "%s", hero->name);
  480. }
  481. break;
  482. case EventCondition::DESTROY:
  483. if (isInTheMap(cond.position))
  484. cond.object = getObjectiveObjectFrom(cond.position, Obj::EObj(cond.objectType));
  485. if (cond.object)
  486. {
  487. const CGHeroInstance *hero = dynamic_cast<const CGHeroInstance*>(cond.object);
  488. if (hero)
  489. boost::algorithm::replace_first(event.onFulfill, "%s", hero->name);
  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((si32)artInstances.size());
  515. artInstances.push_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.push_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((si32)objects.size()))
  551. throw std::runtime_error("Invalid object instance id");
  552. if(obj->instanceName == "")
  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.push_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 = 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