CMap.cpp 19 KB

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