CMap.cpp 19 KB

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