2
0

CMap.cpp 19 KB

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