CMapEditManager.cpp 18 KB

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