CMapEditManager.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. #include "StdInc.h"
  2. #include "CMapEditManager.h"
  3. #include "../JsonNode.h"
  4. #include "../filesystem/CResourceLoader.h"
  5. #include "../CDefObjInfoHandler.h"
  6. MapRect::MapRect() : x(0), y(0), z(0), width(0), height(0)
  7. {
  8. }
  9. MapRect::MapRect(int3 pos, si32 width, si32 height) : x(pos.x), y(pos.y), z(pos.z), width(width), height(height)
  10. {
  11. }
  12. MapRect MapRect::operator&(const MapRect & rect) const
  13. {
  14. bool intersect = right() > rect.left() && rect.right() > left() &&
  15. bottom() > rect.top() && rect.bottom() > top() &&
  16. z == rect.z;
  17. if(intersect)
  18. {
  19. MapRect ret;
  20. ret.x = std::max(left(), rect.left());
  21. ret.y = std::max(top(), rect.top());
  22. ret.z = rect.z;
  23. ret.width = std::min(right(), rect.right()) - ret.x;
  24. ret.height = std::min(bottom(), rect.bottom()) - ret.y;
  25. return ret;
  26. }
  27. else
  28. {
  29. return MapRect();
  30. }
  31. }
  32. si32 MapRect::left() const
  33. {
  34. return x;
  35. }
  36. si32 MapRect::right() const
  37. {
  38. return x + width;
  39. }
  40. si32 MapRect::top() const
  41. {
  42. return y;
  43. }
  44. si32 MapRect::bottom() const
  45. {
  46. return y + height;
  47. }
  48. int3 MapRect::topLeft() const
  49. {
  50. return int3(x, y, z);
  51. }
  52. int3 MapRect::topRight() const
  53. {
  54. return int3(right(), y, z);
  55. }
  56. int3 MapRect::bottomLeft() const
  57. {
  58. return int3(x, bottom(), z);
  59. }
  60. int3 MapRect::bottomRight() const
  61. {
  62. return int3(right(), bottom(), z);
  63. }
  64. CMapOperation::CMapOperation(CMap * map) : map(map)
  65. {
  66. }
  67. std::string CMapOperation::getLabel() const
  68. {
  69. return "";
  70. }
  71. CMapUndoManager::CMapUndoManager() : undoRedoLimit(10)
  72. {
  73. }
  74. void CMapUndoManager::undo()
  75. {
  76. doOperation(undoStack, redoStack, true);
  77. }
  78. void CMapUndoManager::redo()
  79. {
  80. doOperation(redoStack, undoStack, false);
  81. }
  82. void CMapUndoManager::clearAll()
  83. {
  84. undoStack.clear();
  85. redoStack.clear();
  86. }
  87. int CMapUndoManager::getUndoRedoLimit() const
  88. {
  89. return undoRedoLimit;
  90. }
  91. void CMapUndoManager::setUndoRedoLimit(int value)
  92. {
  93. if(value < 0) throw std::runtime_error("Cannot set a negative value for the undo redo limit.");
  94. undoStack.resize(std::min(undoStack.size(), static_cast<TStack::size_type>(value)));
  95. redoStack.resize(std::min(redoStack.size(), static_cast<TStack::size_type>(value)));
  96. }
  97. const CMapOperation * CMapUndoManager::peekRedo() const
  98. {
  99. return peek(redoStack);
  100. }
  101. const CMapOperation * CMapUndoManager::peekUndo() const
  102. {
  103. return peek(undoStack);
  104. }
  105. void CMapUndoManager::addOperation(unique_ptr<CMapOperation> && operation)
  106. {
  107. undoStack.push_front(std::move(operation));
  108. if(undoStack.size() > undoRedoLimit) undoStack.pop_back();
  109. redoStack.clear();
  110. }
  111. void CMapUndoManager::doOperation(TStack & fromStack, TStack & toStack, bool doUndo)
  112. {
  113. if(fromStack.empty()) return;
  114. auto & operation = fromStack.front();
  115. if(doUndo)
  116. {
  117. operation->undo();
  118. }
  119. else
  120. {
  121. operation->redo();
  122. }
  123. toStack.push_front(std::move(operation));
  124. fromStack.pop_front();
  125. }
  126. const CMapOperation * CMapUndoManager::peek(const TStack & stack) const
  127. {
  128. if(stack.empty()) return nullptr;
  129. return stack.front().get();
  130. }
  131. CMapEditManager::CMapEditManager(CMap * map)
  132. : map(map)
  133. {
  134. }
  135. void CMapEditManager::clearTerrain(CRandomGenerator * gen)
  136. {
  137. for(int i = 0; i < map->width; ++i)
  138. {
  139. for(int j = 0; j < map->height; ++j)
  140. {
  141. map->getTile(int3(i, j, 0)).terType = ETerrainType::WATER;
  142. map->getTile(int3(i, j, 0)).terView = gen->getInteger(20, 32);
  143. if(map->twoLevel)
  144. {
  145. map->getTile(int3(i, j, 1)).terType = ETerrainType::ROCK;
  146. map->getTile(int3(i, j, 1)).terView = 0;
  147. }
  148. }
  149. }
  150. }
  151. void CMapEditManager::drawTerrain(const MapRect & rect, ETerrainType terType, CRandomGenerator * gen)
  152. {
  153. execute(make_unique<DrawTerrainOperation>(map, rect, terType, gen));
  154. }
  155. void CMapEditManager::insertObject(const int3 & pos, CGObjectInstance * obj)
  156. {
  157. execute(make_unique<InsertObjectOperation>(map, pos, obj));
  158. }
  159. void CMapEditManager::execute(unique_ptr<CMapOperation> && operation)
  160. {
  161. operation->execute();
  162. undoManager.addOperation(std::move(operation));
  163. }
  164. void CMapEditManager::undo()
  165. {
  166. undoManager.undo();
  167. }
  168. void CMapEditManager::redo()
  169. {
  170. undoManager.redo();
  171. }
  172. CMapUndoManager & CMapEditManager::getUndoManager()
  173. {
  174. return undoManager;
  175. }
  176. const std::string TerrainViewPattern::FLIP_MODE_SAME_IMAGE = "sameImage";
  177. const std::string TerrainViewPattern::FLIP_MODE_DIFF_IMAGES = "diffImages";
  178. const std::string TerrainViewPattern::RULE_DIRT = "D";
  179. const std::string TerrainViewPattern::RULE_SAND = "S";
  180. const std::string TerrainViewPattern::RULE_TRANSITION = "T";
  181. const std::string TerrainViewPattern::RULE_NATIVE = "N";
  182. const std::string TerrainViewPattern::RULE_ANY = "?";
  183. TerrainViewPattern::TerrainViewPattern() : minPoints(0), flipMode(FLIP_MODE_SAME_IMAGE),
  184. terGroup(ETerrainGroup::NORMAL)
  185. {
  186. }
  187. TerrainViewPattern::WeightedRule::WeightedRule() : points(0)
  188. {
  189. }
  190. bool TerrainViewPattern::WeightedRule::isStandardRule() const
  191. {
  192. return TerrainViewPattern::RULE_ANY == name || TerrainViewPattern::RULE_DIRT == name
  193. || TerrainViewPattern::RULE_NATIVE == name || TerrainViewPattern::RULE_SAND == name
  194. || TerrainViewPattern::RULE_TRANSITION == name;
  195. }
  196. boost::mutex CTerrainViewPatternConfig::smx;
  197. CTerrainViewPatternConfig & CTerrainViewPatternConfig::get()
  198. {
  199. TLockGuard _(smx);
  200. static CTerrainViewPatternConfig instance;
  201. return instance;
  202. }
  203. CTerrainViewPatternConfig::CTerrainViewPatternConfig()
  204. {
  205. const JsonNode config(ResourceID("config/terrainViewPatterns.json"));
  206. const auto & groupMap = config.Struct();
  207. BOOST_FOREACH(const auto & groupPair, groupMap)
  208. {
  209. auto terGroup = getTerrainGroup(groupPair.first);
  210. BOOST_FOREACH(const JsonNode & ptrnNode, groupPair.second.Vector())
  211. {
  212. TerrainViewPattern pattern;
  213. // Read pattern data
  214. const JsonVector & data = ptrnNode["data"].Vector();
  215. if(data.size() != 9)
  216. {
  217. throw std::runtime_error("Size of pattern's data vector has to be 9.");
  218. }
  219. for(int i = 0; i < data.size(); ++i)
  220. {
  221. std::string cell = data[i].String();
  222. boost::algorithm::erase_all(cell, " ");
  223. std::vector<std::string> rules;
  224. boost::split(rules, cell, boost::is_any_of(","));
  225. BOOST_FOREACH(std::string ruleStr, rules)
  226. {
  227. std::vector<std::string> ruleParts;
  228. boost::split(ruleParts, ruleStr, boost::is_any_of("-"));
  229. TerrainViewPattern::WeightedRule rule;
  230. rule.name = ruleParts[0];
  231. if(ruleParts.size() > 1)
  232. {
  233. rule.points = boost::lexical_cast<int>(ruleParts[1]);
  234. }
  235. pattern.data[i].push_back(rule);
  236. }
  237. }
  238. // Read mapping
  239. std::string mappingStr = ptrnNode["mapping"].String();
  240. boost::algorithm::erase_all(mappingStr, " ");
  241. std::vector<std::string> mappings;
  242. boost::split(mappings, mappingStr, boost::is_any_of(","));
  243. BOOST_FOREACH(std::string mapping, mappings)
  244. {
  245. std::vector<std::string> range;
  246. boost::split(range, mapping, boost::is_any_of("-"));
  247. pattern.mapping.push_back(std::make_pair(boost::lexical_cast<int>(range[0]),
  248. boost::lexical_cast<int>(range.size() > 1 ? range[1] : range[0])));
  249. }
  250. // Read optional attributes
  251. pattern.id = ptrnNode["id"].String();
  252. pattern.minPoints = static_cast<int>(ptrnNode["minPoints"].Float());
  253. pattern.flipMode = ptrnNode["flipMode"].String();
  254. if(pattern.flipMode.empty())
  255. {
  256. pattern.flipMode = TerrainViewPattern::FLIP_MODE_SAME_IMAGE;
  257. }
  258. pattern.terGroup = terGroup;
  259. patterns[terGroup].push_back(pattern);
  260. }
  261. }
  262. }
  263. CTerrainViewPatternConfig::~CTerrainViewPatternConfig()
  264. {
  265. }
  266. ETerrainGroup::ETerrainGroup CTerrainViewPatternConfig::getTerrainGroup(const std::string & terGroup) const
  267. {
  268. static const std::map<std::string, ETerrainGroup::ETerrainGroup> terGroups
  269. = boost::assign::map_list_of("normal", ETerrainGroup::NORMAL)("dirt", ETerrainGroup::DIRT)
  270. ("sand", ETerrainGroup::SAND)("water", ETerrainGroup::WATER)("rock", ETerrainGroup::ROCK);
  271. auto it = terGroups.find(terGroup);
  272. if(it == terGroups.end()) throw std::runtime_error(boost::str(boost::format("Terrain group '%s' does not exist.") % terGroup));
  273. return it->second;
  274. }
  275. const std::vector<TerrainViewPattern> & CTerrainViewPatternConfig::getPatternsForGroup(ETerrainGroup::ETerrainGroup terGroup) const
  276. {
  277. return patterns.find(terGroup)->second;
  278. }
  279. const TerrainViewPattern & CTerrainViewPatternConfig::getPatternById(ETerrainGroup::ETerrainGroup terGroup, const std::string & id) const
  280. {
  281. const std::vector<TerrainViewPattern> & groupPatterns = getPatternsForGroup(terGroup);
  282. BOOST_FOREACH(const TerrainViewPattern & pattern, groupPatterns)
  283. {
  284. if(id == pattern.id)
  285. {
  286. return pattern;
  287. }
  288. }
  289. throw std::runtime_error("Pattern with ID not found: " + id);
  290. }
  291. DrawTerrainOperation::DrawTerrainOperation(CMap * map, const MapRect & rect, ETerrainType terType, CRandomGenerator * gen)
  292. : CMapOperation(map), rect(rect), terType(terType), gen(gen)
  293. {
  294. }
  295. void DrawTerrainOperation::execute()
  296. {
  297. for(int i = rect.x; i < rect.x + rect.width; ++i)
  298. {
  299. for(int j = rect.y; j < rect.y + rect.height; ++j)
  300. {
  301. map->getTile(int3(i, j, rect.z)).terType = terType;
  302. }
  303. }
  304. //TODO there are situations where more tiles are affected implicitely
  305. //TODO add coastal bit to extTileFlags appropriately
  306. MapRect viewRect(int3(rect.x - 1, rect.y - 1, rect.z), rect.width + 2, rect.height + 2); // Has to overlap 1 tile around
  307. updateTerrainViews(viewRect & MapRect(int3(0, 0, viewRect.z), map->width, map->height)); // Rect should not overlap map dimensions
  308. }
  309. void DrawTerrainOperation::undo()
  310. {
  311. //TODO
  312. }
  313. void DrawTerrainOperation::redo()
  314. {
  315. //TODO
  316. }
  317. std::string DrawTerrainOperation::getLabel() const
  318. {
  319. return "Draw Terrain";
  320. }
  321. void DrawTerrainOperation::updateTerrainViews(const MapRect & rect)
  322. {
  323. for(int i = rect.x; i < rect.x + rect.width; ++i)
  324. {
  325. for(int j = rect.y; j < rect.y + rect.height; ++j)
  326. {
  327. const auto & patterns =
  328. CTerrainViewPatternConfig::get().getPatternsForGroup(getTerrainGroup(map->getTile(int3(i, j, rect.z)).terType));
  329. // Detect a pattern which fits best
  330. int bestPattern = -1, bestFlip = -1;
  331. std::string transitionReplacement;
  332. for(int k = 0; k < patterns.size(); ++k)
  333. {
  334. const auto & pattern = patterns[k];
  335. for(int flip = 0; flip < 4; ++flip)
  336. {
  337. auto valRslt = validateTerrainView(int3(i, j, rect.z), flip > 0 ? getFlippedPattern(pattern, flip) : pattern);
  338. if(valRslt.result)
  339. {
  340. logGlobal->debugStream() << "Pattern detected at pos " << i << "x" << j << "x" << rect.z << ": P-Nr. " << k
  341. << ", Flip " << flip << ", Repl. " << valRslt.transitionReplacement;
  342. bestPattern = k;
  343. bestFlip = flip;
  344. transitionReplacement = valRslt.transitionReplacement;
  345. break;
  346. }
  347. }
  348. }
  349. if(bestPattern == -1)
  350. {
  351. // This shouldn't be the case
  352. logGlobal->warnStream() << "No pattern detected at pos " << i << "x" << j << "x" << rect.z;
  353. continue;
  354. }
  355. // Get mapping
  356. const TerrainViewPattern & pattern = patterns[bestPattern];
  357. std::pair<int, int> mapping;
  358. if(transitionReplacement.empty())
  359. {
  360. mapping = pattern.mapping[0];
  361. }
  362. else
  363. {
  364. mapping = transitionReplacement == TerrainViewPattern::RULE_DIRT ? pattern.mapping[0] : pattern.mapping[1];
  365. }
  366. // Set terrain view
  367. auto & tile = map->getTile(int3(i, j, rect.z));
  368. if(pattern.flipMode == TerrainViewPattern::FLIP_MODE_SAME_IMAGE)
  369. {
  370. tile.terView = gen->getInteger(mapping.first, mapping.second);
  371. tile.extTileFlags = bestFlip;
  372. }
  373. else
  374. {
  375. const int framesPerRot = 2;
  376. int firstFrame = mapping.first + bestFlip * framesPerRot;
  377. tile.terView = gen->getInteger(firstFrame, firstFrame + framesPerRot - 1);
  378. tile.extTileFlags = 0;
  379. }
  380. }
  381. }
  382. }
  383. ETerrainGroup::ETerrainGroup DrawTerrainOperation::getTerrainGroup(ETerrainType terType) const
  384. {
  385. switch(terType)
  386. {
  387. case ETerrainType::DIRT:
  388. return ETerrainGroup::DIRT;
  389. case ETerrainType::SAND:
  390. return ETerrainGroup::SAND;
  391. case ETerrainType::WATER:
  392. return ETerrainGroup::WATER;
  393. case ETerrainType::ROCK:
  394. return ETerrainGroup::ROCK;
  395. default:
  396. return ETerrainGroup::NORMAL;
  397. }
  398. }
  399. DrawTerrainOperation::ValidationResult DrawTerrainOperation::validateTerrainView(const int3 & pos, const TerrainViewPattern & pattern, int recDepth /*= 0*/) const
  400. {
  401. ETerrainType centerTerType = map->getTile(pos).terType;
  402. int totalPoints = 0;
  403. std::string transitionReplacement;
  404. for(int i = 0; i < 9; ++i)
  405. {
  406. // The center, middle cell can be skipped
  407. if(i == 4)
  408. {
  409. continue;
  410. }
  411. // Get terrain group of the current cell
  412. int cx = pos.x + (i % 3) - 1;
  413. int cy = pos.y + (i / 3) - 1;
  414. bool isAlien = false;
  415. ETerrainType terType;
  416. if(cx < 0 || cx >= map->width || cy < 0 || cy >= map->height)
  417. {
  418. terType = centerTerType;
  419. }
  420. else
  421. {
  422. terType = map->getTile(int3(cx, cy, pos.z)).terType;
  423. if(terType != centerTerType)
  424. {
  425. isAlien = true;
  426. }
  427. }
  428. // Validate all rules per cell
  429. int topPoints = -1;
  430. for(int j = 0; j < pattern.data[i].size(); ++j)
  431. {
  432. TerrainViewPattern::WeightedRule rule = pattern.data[i][j];
  433. if(!rule.isStandardRule())
  434. {
  435. if(recDepth == 0)
  436. {
  437. const auto & patternForRule = CTerrainViewPatternConfig::get().getPatternById(pattern.terGroup, rule.name);
  438. auto rslt = validateTerrainView(int3(cx, cy, pos.z), patternForRule, 1);
  439. if(!rslt.result)
  440. {
  441. return ValidationResult(false);
  442. }
  443. else
  444. {
  445. topPoints = std::max(topPoints, rule.points);
  446. continue;
  447. }
  448. }
  449. else
  450. {
  451. rule.name = TerrainViewPattern::RULE_NATIVE;
  452. }
  453. }
  454. bool nativeTestOk = (rule.name == TerrainViewPattern::RULE_NATIVE || rule.name == TerrainViewPattern::RULE_ANY) && !isAlien;
  455. auto applyValidationRslt = [&](bool rslt)
  456. {
  457. if(rslt)
  458. {
  459. topPoints = std::max(topPoints, rule.points);
  460. }
  461. };
  462. // Validate cell with the ruleset of the pattern
  463. if(pattern.terGroup == ETerrainGroup::NORMAL)
  464. {
  465. bool dirtTestOk = (rule.name == TerrainViewPattern::RULE_DIRT
  466. || rule.name == TerrainViewPattern::RULE_TRANSITION || rule.name == TerrainViewPattern::RULE_ANY)
  467. && isAlien && !isSandType(terType);
  468. bool sandTestOk = (rule.name == TerrainViewPattern::RULE_SAND || rule.name == TerrainViewPattern::RULE_TRANSITION
  469. || rule.name == TerrainViewPattern::RULE_ANY)
  470. && isSandType(terType);
  471. if(transitionReplacement.empty() && (rule.name == TerrainViewPattern::RULE_TRANSITION
  472. || rule.name == TerrainViewPattern::RULE_ANY) && (dirtTestOk || sandTestOk))
  473. {
  474. transitionReplacement = dirtTestOk ? TerrainViewPattern::RULE_DIRT : TerrainViewPattern::RULE_SAND;
  475. }
  476. applyValidationRslt((dirtTestOk && transitionReplacement != TerrainViewPattern::RULE_SAND)
  477. || (sandTestOk && transitionReplacement != TerrainViewPattern::RULE_DIRT)
  478. || nativeTestOk);
  479. }
  480. else if(pattern.terGroup == ETerrainGroup::DIRT)
  481. {
  482. bool sandTestOk = rule.name == TerrainViewPattern::RULE_SAND && isSandType(terType);
  483. bool dirtTestOk = rule.name == TerrainViewPattern::RULE_DIRT && !isSandType(terType) && !nativeTestOk;
  484. applyValidationRslt(rule.name == TerrainViewPattern::RULE_ANY || sandTestOk || dirtTestOk || nativeTestOk);
  485. }
  486. else if(pattern.terGroup == ETerrainGroup::SAND)
  487. {
  488. bool sandTestOk = rule.name == TerrainViewPattern::RULE_SAND && isAlien;
  489. applyValidationRslt(rule.name == TerrainViewPattern::RULE_ANY || sandTestOk || nativeTestOk);
  490. }
  491. else if(pattern.terGroup == ETerrainGroup::WATER)
  492. {
  493. bool sandTestOk = rule.name == TerrainViewPattern::RULE_SAND && terType != ETerrainType::DIRT
  494. && terType != ETerrainType::WATER;
  495. applyValidationRslt(rule.name == TerrainViewPattern::RULE_ANY || sandTestOk || nativeTestOk);
  496. }
  497. else if(pattern.terGroup == ETerrainGroup::ROCK)
  498. {
  499. bool sandTestOk = rule.name == TerrainViewPattern::RULE_SAND && terType != ETerrainType::DIRT
  500. && terType != ETerrainType::ROCK;
  501. applyValidationRslt(rule.name == TerrainViewPattern::RULE_ANY || sandTestOk || nativeTestOk);
  502. }
  503. }
  504. if(topPoints == -1)
  505. {
  506. return ValidationResult(false);
  507. }
  508. else
  509. {
  510. totalPoints += topPoints;
  511. }
  512. }
  513. if(pattern.minPoints > totalPoints)
  514. {
  515. return ValidationResult(false);
  516. }
  517. return ValidationResult(true, transitionReplacement);
  518. }
  519. bool DrawTerrainOperation::isSandType(ETerrainType terType) const
  520. {
  521. switch(terType)
  522. {
  523. case ETerrainType::WATER:
  524. case ETerrainType::SAND:
  525. case ETerrainType::ROCK:
  526. return true;
  527. default:
  528. return false;
  529. }
  530. }
  531. TerrainViewPattern DrawTerrainOperation::getFlippedPattern(const TerrainViewPattern & pattern, int flip) const
  532. {
  533. if(flip == 0)
  534. {
  535. return pattern;
  536. }
  537. TerrainViewPattern ret = pattern;
  538. if(flip == FLIP_PATTERN_HORIZONTAL || flip == FLIP_PATTERN_BOTH)
  539. {
  540. for(int i = 0; i < 3; ++i)
  541. {
  542. int y = i * 3;
  543. std::swap(ret.data[y], ret.data[y + 2]);
  544. }
  545. }
  546. if(flip == FLIP_PATTERN_VERTICAL || flip == FLIP_PATTERN_BOTH)
  547. {
  548. for(int i = 0; i < 3; ++i)
  549. {
  550. std::swap(ret.data[i], ret.data[6 + i]);
  551. }
  552. }
  553. return ret;
  554. }
  555. DrawTerrainOperation::ValidationResult::ValidationResult(bool result, const std::string & transitionReplacement /*= ""*/)
  556. : result(result), transitionReplacement(transitionReplacement)
  557. {
  558. }
  559. InsertObjectOperation::InsertObjectOperation(CMap * map, const int3 & pos, CGObjectInstance * obj)
  560. : CMapOperation(map), pos(pos), obj(obj)
  561. {
  562. }
  563. void InsertObjectOperation::execute()
  564. {
  565. obj->pos = pos;
  566. obj->id = ObjectInstanceID(map->objects.size());
  567. map->objects.push_back(obj);
  568. if(obj->ID == Obj::TOWN)
  569. {
  570. map->towns.push_back(static_cast<CGTownInstance *>(obj));
  571. }
  572. if(obj->ID == Obj::HERO)
  573. {
  574. map->heroes.push_back(static_cast<CGHeroInstance*>(obj));
  575. }
  576. map->addBlockVisTiles(obj);
  577. }
  578. void InsertObjectOperation::undo()
  579. {
  580. //TODO
  581. }
  582. void InsertObjectOperation::redo()
  583. {
  584. execute();
  585. }
  586. std::string InsertObjectOperation::getLabel() const
  587. {
  588. return "Insert Object";
  589. }