CMapEditManager.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. assert(value >= 0);
  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<CDrawTerrainOperation>(map, rect, terType, gen));
  154. }
  155. void CMapEditManager::insertObject(const int3 & pos, CGObjectInstance * obj)
  156. {
  157. execute(make_unique<CInsertObjectOperation>(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. assert(!pattern.id.empty());
  253. pattern.minPoints = static_cast<int>(ptrnNode["minPoints"].Float());
  254. pattern.maxPoints = static_cast<int>(ptrnNode["maxPoints"].Float());
  255. if(pattern.maxPoints == 0) pattern.maxPoints = std::numeric_limits<int>::max();
  256. pattern.flipMode = ptrnNode["flipMode"].String();
  257. if(pattern.flipMode.empty())
  258. {
  259. pattern.flipMode = TerrainViewPattern::FLIP_MODE_SAME_IMAGE;
  260. }
  261. pattern.terGroup = terGroup;
  262. patterns[terGroup].push_back(pattern);
  263. }
  264. }
  265. }
  266. CTerrainViewPatternConfig::~CTerrainViewPatternConfig()
  267. {
  268. }
  269. ETerrainGroup::ETerrainGroup CTerrainViewPatternConfig::getTerrainGroup(const std::string & terGroup) const
  270. {
  271. static const std::map<std::string, ETerrainGroup::ETerrainGroup> terGroups
  272. = boost::assign::map_list_of("normal", ETerrainGroup::NORMAL)("dirt", ETerrainGroup::DIRT)
  273. ("sand", ETerrainGroup::SAND)("water", ETerrainGroup::WATER)("rock", ETerrainGroup::ROCK);
  274. auto it = terGroups.find(terGroup);
  275. if(it == terGroups.end()) throw std::runtime_error(boost::str(boost::format("Terrain group '%s' does not exist.") % terGroup));
  276. return it->second;
  277. }
  278. const std::vector<TerrainViewPattern> & CTerrainViewPatternConfig::getPatternsForGroup(ETerrainGroup::ETerrainGroup terGroup) const
  279. {
  280. return patterns.find(terGroup)->second;
  281. }
  282. const TerrainViewPattern & CTerrainViewPatternConfig::getPatternById(ETerrainGroup::ETerrainGroup terGroup, const std::string & id) const
  283. {
  284. const std::vector<TerrainViewPattern> & groupPatterns = getPatternsForGroup(terGroup);
  285. BOOST_FOREACH(const TerrainViewPattern & pattern, groupPatterns)
  286. {
  287. if(id == pattern.id)
  288. {
  289. return pattern;
  290. }
  291. }
  292. throw std::runtime_error("Pattern with ID not found: " + id);
  293. }
  294. CDrawTerrainOperation::CDrawTerrainOperation(CMap * map, const MapRect & rect, ETerrainType terType, CRandomGenerator * gen)
  295. : CMapOperation(map), rect(rect), terType(terType), gen(gen)
  296. {
  297. }
  298. void CDrawTerrainOperation::execute()
  299. {
  300. for(int i = rect.x; i < rect.x + rect.width; ++i)
  301. {
  302. for(int j = rect.y; j < rect.y + rect.height; ++j)
  303. {
  304. map->getTile(int3(i, j, rect.z)).terType = terType;
  305. }
  306. }
  307. //TODO there are situations where more tiles are affected implicitely
  308. //TODO add coastal bit to extTileFlags appropriately
  309. MapRect viewRect(int3(rect.x - 1, rect.y - 1, rect.z), rect.width + 2, rect.height + 2); // Has to overlap 1 tile around
  310. updateTerrainViews(viewRect & MapRect(int3(0, 0, viewRect.z), map->width, map->height)); // Rect should not overlap map dimensions
  311. }
  312. void CDrawTerrainOperation::undo()
  313. {
  314. //TODO
  315. }
  316. void CDrawTerrainOperation::redo()
  317. {
  318. //TODO
  319. }
  320. std::string CDrawTerrainOperation::getLabel() const
  321. {
  322. return "Draw Terrain";
  323. }
  324. void CDrawTerrainOperation::updateTerrainViews(const MapRect & rect)
  325. {
  326. for(int x = rect.x; x < rect.x + rect.width; ++x)
  327. {
  328. for(int y = rect.y; y < rect.y + rect.height; ++y)
  329. {
  330. const auto & patterns =
  331. CTerrainViewPatternConfig::get().getPatternsForGroup(getTerrainGroup(map->getTile(int3(x, y, rect.z)).terType));
  332. // Detect a pattern which fits best
  333. int bestPattern = -1;
  334. ValidationResult valRslt(false);
  335. for(int k = 0; k < patterns.size(); ++k)
  336. {
  337. const auto & pattern = patterns[k];
  338. valRslt = validateTerrainView(int3(x, y, rect.z), pattern);
  339. if(valRslt.result)
  340. {
  341. logGlobal->debugStream() << "Pattern detected at pos " << x << "x" << y << "x" << rect.z << ": P-Nr. " << pattern.id
  342. << ", Flip " << valRslt.flip << ", Repl. " << valRslt.transitionReplacement;
  343. bestPattern = k;
  344. break;
  345. }
  346. }
  347. //assert(bestPattern != -1);
  348. if(bestPattern == -1)
  349. {
  350. // This shouldn't be the case
  351. logGlobal->warnStream() << "No pattern detected at pos " << x << "x" << y << "x" << rect.z;
  352. continue;
  353. }
  354. // Get mapping
  355. const TerrainViewPattern & pattern = patterns[bestPattern];
  356. std::pair<int, int> mapping;
  357. if(valRslt.transitionReplacement.empty())
  358. {
  359. mapping = pattern.mapping[0];
  360. }
  361. else
  362. {
  363. mapping = valRslt.transitionReplacement == TerrainViewPattern::RULE_DIRT ? pattern.mapping[0] : pattern.mapping[1];
  364. }
  365. // Set terrain view
  366. auto & tile = map->getTile(int3(x, y, rect.z));
  367. if(pattern.flipMode == TerrainViewPattern::FLIP_MODE_SAME_IMAGE)
  368. {
  369. tile.terView = gen->getInteger(mapping.first, mapping.second);
  370. tile.extTileFlags = valRslt.flip;
  371. }
  372. else
  373. {
  374. const int framesPerRot = 2;
  375. int firstFrame = mapping.first + valRslt.flip * framesPerRot;
  376. tile.terView = gen->getInteger(firstFrame, firstFrame + framesPerRot - 1);
  377. tile.extTileFlags = 0;
  378. }
  379. }
  380. }
  381. }
  382. ETerrainGroup::ETerrainGroup CDrawTerrainOperation::getTerrainGroup(ETerrainType terType) const
  383. {
  384. switch(terType)
  385. {
  386. case ETerrainType::DIRT:
  387. return ETerrainGroup::DIRT;
  388. case ETerrainType::SAND:
  389. return ETerrainGroup::SAND;
  390. case ETerrainType::WATER:
  391. return ETerrainGroup::WATER;
  392. case ETerrainType::ROCK:
  393. return ETerrainGroup::ROCK;
  394. default:
  395. return ETerrainGroup::NORMAL;
  396. }
  397. }
  398. CDrawTerrainOperation::ValidationResult CDrawTerrainOperation::validateTerrainView(const int3 & pos, const TerrainViewPattern & pattern, int recDepth /*= 0*/) const
  399. {
  400. for(int flip = 0; flip < 4; ++flip)
  401. {
  402. auto valRslt = validateTerrainViewInner(pos, flip > 0 ? getFlippedPattern(pattern, flip) : pattern, recDepth);
  403. if(valRslt.result)
  404. {
  405. valRslt.flip = flip;
  406. return valRslt;
  407. }
  408. }
  409. return ValidationResult(false);
  410. }
  411. CDrawTerrainOperation::ValidationResult CDrawTerrainOperation::validateTerrainViewInner(const int3 & pos, const TerrainViewPattern & pattern, int recDepth /*= 0*/) const
  412. {
  413. ETerrainType centerTerType = map->getTile(pos).terType;
  414. int totalPoints = 0;
  415. std::string transitionReplacement;
  416. for(int i = 0; i < 9; ++i)
  417. {
  418. // The center, middle cell can be skipped
  419. if(i == 4)
  420. {
  421. continue;
  422. }
  423. // Get terrain group of the current cell
  424. int cx = pos.x + (i % 3) - 1;
  425. int cy = pos.y + (i / 3) - 1;
  426. int3 currentPos(cx, cy, pos.z);
  427. bool isAlien = false;
  428. ETerrainType terType;
  429. if(!map->isInTheMap(currentPos))
  430. {
  431. terType = centerTerType;
  432. }
  433. else
  434. {
  435. terType = map->getTile(currentPos).terType;
  436. if(terType != centerTerType)
  437. {
  438. isAlien = true;
  439. }
  440. }
  441. // Validate all rules per cell
  442. int topPoints = -1;
  443. for(int j = 0; j < pattern.data[i].size(); ++j)
  444. {
  445. TerrainViewPattern::WeightedRule rule = pattern.data[i][j];
  446. if(!rule.isStandardRule())
  447. {
  448. if(recDepth == 0)
  449. {
  450. if(map->isInTheMap(currentPos) && terType == centerTerType)
  451. {
  452. const auto & patternForRule = CTerrainViewPatternConfig::get().getPatternById(pattern.terGroup, rule.name);
  453. auto rslt = validateTerrainView(currentPos, patternForRule, 1);
  454. if(rslt.result) topPoints = std::max(topPoints, rule.points);
  455. }
  456. continue;
  457. }
  458. else
  459. {
  460. rule.name = TerrainViewPattern::RULE_NATIVE;
  461. }
  462. }
  463. bool nativeTestOk = (rule.name == TerrainViewPattern::RULE_NATIVE || rule.name == TerrainViewPattern::RULE_ANY) && !isAlien;
  464. auto applyValidationRslt = [&](bool rslt)
  465. {
  466. if(rslt)
  467. {
  468. topPoints = std::max(topPoints, rule.points);
  469. }
  470. };
  471. // Validate cell with the ruleset of the pattern
  472. if(pattern.terGroup == ETerrainGroup::NORMAL)
  473. {
  474. bool dirtTestOk = (rule.name == TerrainViewPattern::RULE_DIRT
  475. || rule.name == TerrainViewPattern::RULE_TRANSITION || rule.name == TerrainViewPattern::RULE_ANY)
  476. && isAlien && !isSandType(terType);
  477. bool sandTestOk = (rule.name == TerrainViewPattern::RULE_SAND || rule.name == TerrainViewPattern::RULE_TRANSITION
  478. || rule.name == TerrainViewPattern::RULE_ANY)
  479. && isSandType(terType);
  480. if(transitionReplacement.empty() && rule.name == TerrainViewPattern::RULE_TRANSITION
  481. && (dirtTestOk || sandTestOk))
  482. {
  483. transitionReplacement = dirtTestOk ? TerrainViewPattern::RULE_DIRT : TerrainViewPattern::RULE_SAND;
  484. }
  485. if(rule.name == TerrainViewPattern::RULE_TRANSITION)
  486. {
  487. applyValidationRslt((dirtTestOk && transitionReplacement != TerrainViewPattern::RULE_SAND) ||
  488. (sandTestOk && transitionReplacement != TerrainViewPattern::RULE_DIRT));
  489. }
  490. else
  491. {
  492. applyValidationRslt(dirtTestOk || sandTestOk || nativeTestOk);
  493. }
  494. }
  495. else if(pattern.terGroup == ETerrainGroup::DIRT)
  496. {
  497. bool sandTestOk = rule.name == TerrainViewPattern::RULE_SAND && isSandType(terType);
  498. bool dirtTestOk = rule.name == TerrainViewPattern::RULE_DIRT && !isSandType(terType) && !nativeTestOk;
  499. applyValidationRslt(rule.name == TerrainViewPattern::RULE_ANY || sandTestOk || dirtTestOk || nativeTestOk);
  500. }
  501. else if(pattern.terGroup == ETerrainGroup::SAND)
  502. {
  503. bool sandTestOk = rule.name == TerrainViewPattern::RULE_SAND && isAlien;
  504. applyValidationRslt(rule.name == TerrainViewPattern::RULE_ANY || sandTestOk || nativeTestOk);
  505. }
  506. else if(pattern.terGroup == ETerrainGroup::WATER)
  507. {
  508. bool sandTestOk = rule.name == TerrainViewPattern::RULE_SAND && terType != ETerrainType::DIRT
  509. && terType != ETerrainType::WATER;
  510. applyValidationRslt(rule.name == TerrainViewPattern::RULE_ANY || sandTestOk || nativeTestOk);
  511. }
  512. else if(pattern.terGroup == ETerrainGroup::ROCK)
  513. {
  514. bool sandTestOk = rule.name == TerrainViewPattern::RULE_SAND && terType != ETerrainType::DIRT
  515. && terType != ETerrainType::ROCK;
  516. applyValidationRslt(rule.name == TerrainViewPattern::RULE_ANY || sandTestOk || nativeTestOk);
  517. }
  518. }
  519. if(topPoints == -1)
  520. {
  521. return ValidationResult(false);
  522. }
  523. else
  524. {
  525. totalPoints += topPoints;
  526. }
  527. }
  528. if(totalPoints >= pattern.minPoints && totalPoints <= pattern.maxPoints)
  529. {
  530. return ValidationResult(true, transitionReplacement);
  531. }
  532. else
  533. {
  534. return ValidationResult(false);
  535. }
  536. }
  537. bool CDrawTerrainOperation::isSandType(ETerrainType terType) const
  538. {
  539. switch(terType)
  540. {
  541. case ETerrainType::WATER:
  542. case ETerrainType::SAND:
  543. case ETerrainType::ROCK:
  544. return true;
  545. default:
  546. return false;
  547. }
  548. }
  549. TerrainViewPattern CDrawTerrainOperation::getFlippedPattern(const TerrainViewPattern & pattern, int flip) const
  550. {
  551. if(flip == 0)
  552. {
  553. return pattern;
  554. }
  555. TerrainViewPattern ret = pattern;
  556. if(flip == FLIP_PATTERN_HORIZONTAL || flip == FLIP_PATTERN_BOTH)
  557. {
  558. for(int i = 0; i < 3; ++i)
  559. {
  560. int y = i * 3;
  561. std::swap(ret.data[y], ret.data[y + 2]);
  562. }
  563. }
  564. if(flip == FLIP_PATTERN_VERTICAL || flip == FLIP_PATTERN_BOTH)
  565. {
  566. for(int i = 0; i < 3; ++i)
  567. {
  568. std::swap(ret.data[i], ret.data[6 + i]);
  569. }
  570. }
  571. return ret;
  572. }
  573. CDrawTerrainOperation::ValidationResult::ValidationResult(bool result, const std::string & transitionReplacement /*= ""*/)
  574. : result(result), transitionReplacement(transitionReplacement)
  575. {
  576. }
  577. CInsertObjectOperation::CInsertObjectOperation(CMap * map, const int3 & pos, CGObjectInstance * obj)
  578. : CMapOperation(map), pos(pos), obj(obj)
  579. {
  580. }
  581. void CInsertObjectOperation::execute()
  582. {
  583. obj->pos = pos;
  584. obj->id = ObjectInstanceID(map->objects.size());
  585. map->objects.push_back(obj);
  586. if(obj->ID == Obj::TOWN)
  587. {
  588. map->towns.push_back(static_cast<CGTownInstance *>(obj));
  589. }
  590. if(obj->ID == Obj::HERO)
  591. {
  592. map->heroes.push_back(static_cast<CGHeroInstance*>(obj));
  593. }
  594. map->addBlockVisTiles(obj);
  595. }
  596. void CInsertObjectOperation::undo()
  597. {
  598. //TODO
  599. }
  600. void CInsertObjectOperation::redo()
  601. {
  602. execute();
  603. }
  604. std::string CInsertObjectOperation::getLabel() const
  605. {
  606. return "Insert Object";
  607. }