CDrawRoadsOperation.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * CDrawRoadsOperation.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 "CDrawRoadsOperation.h"
  12. #include "CMap.h"
  13. #include "../RoadHandler.h"
  14. #include "../RiverHandler.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. const std::vector<CDrawLinesOperation::LinePattern> CDrawLinesOperation::patterns =
  17. {
  18. //single tile. fall-back pattern
  19. {
  20. {
  21. "-","-","-",
  22. "-","+","-",
  23. "-","-","-"
  24. },
  25. {14,14},
  26. {9,9},
  27. false,
  28. false
  29. },
  30. //Road straight with angle
  31. {
  32. {
  33. "?","-","+",
  34. "-","+","+",
  35. "+","+","?"
  36. },
  37. {2,5},
  38. {-1,-1},
  39. true,
  40. true
  41. },
  42. //Turn
  43. {
  44. {
  45. "?","-","?",
  46. "-","+","+",
  47. "?","+","?"
  48. },
  49. {0,1},
  50. {0,3},
  51. true,
  52. true
  53. },
  54. //Dead end horizontal
  55. {
  56. {
  57. "?","-","?",
  58. "-","+","+",
  59. "?","-","?"
  60. },
  61. {15,15},{11,12},
  62. true,
  63. false
  64. },
  65. //Dead end vertical
  66. {
  67. {
  68. "?","-","?",
  69. "-","+","-",
  70. "?","+","?"
  71. },
  72. {14,14},{9,10},
  73. false,
  74. true
  75. },
  76. //T-cross horizontal
  77. {
  78. {
  79. "?","+","?",
  80. "-","+","+",
  81. "?","+","?"
  82. },
  83. {6,7},{7,8},
  84. true,
  85. false
  86. },
  87. //T-cross vertical
  88. {
  89. {
  90. "?","-","?",
  91. "+","+","+",
  92. "?","+","?"
  93. },
  94. {8,9},{5,6},
  95. false,
  96. true
  97. },
  98. //Straight Horizontal
  99. {
  100. {
  101. "?","-","?",
  102. "+","+","+",
  103. "?","-","?"
  104. },
  105. {12,13},{11,12},
  106. false,
  107. false
  108. },
  109. //Straight Vertical
  110. {
  111. {
  112. "?","+","?",
  113. "-","+","-",
  114. "?","+","?"
  115. },
  116. {10,11},{9,10},
  117. false,
  118. false
  119. },
  120. //X-cross
  121. {
  122. {
  123. "?","+","?",
  124. "+","+","+",
  125. "?","+","?"
  126. },
  127. {16,16},{4,4},
  128. false,
  129. false
  130. }
  131. };
  132. static bool ruleIsNone(const std::string & rule)
  133. {
  134. return rule == "-";
  135. }
  136. static bool ruleIsSomething(const std::string & rule)
  137. {
  138. return rule == "+";
  139. }
  140. #ifndef NDEBUG
  141. static bool ruleIsAny(const std::string & rule)
  142. {
  143. return rule == "?";
  144. }
  145. #endif
  146. ///CDrawLinesOperation
  147. CDrawLinesOperation::CDrawLinesOperation(CMap * map, CTerrainSelection terrainSel, CRandomGenerator * gen):
  148. CMapOperation(map),
  149. terrainSel(std::move(terrainSel)),
  150. gen(gen)
  151. {
  152. }
  153. ///CDrawRoadsOperation
  154. CDrawRoadsOperation::CDrawRoadsOperation(CMap * map, const CTerrainSelection & terrainSel, RoadId roadType, CRandomGenerator * gen):
  155. CDrawLinesOperation(map, terrainSel,gen),
  156. roadType(roadType)
  157. {
  158. }
  159. ///CDrawRiversOperation
  160. CDrawRiversOperation::CDrawRiversOperation(CMap * map, const CTerrainSelection & terrainSel, RiverId riverType, CRandomGenerator * gen):
  161. CDrawLinesOperation(map, terrainSel, gen),
  162. riverType(riverType)
  163. {
  164. }
  165. void CDrawLinesOperation::execute()
  166. {
  167. std::set<int3> invalidated;
  168. for(const auto & pos : terrainSel.getSelectedItems())
  169. {
  170. executeTile(map->getTile(pos));
  171. auto rect = extendTileAroundSafely(pos);
  172. rect.forEach([&invalidated](const int3 & pos)
  173. {
  174. invalidated.insert(pos);
  175. });
  176. }
  177. updateTiles(invalidated);
  178. }
  179. void CDrawLinesOperation::undo()
  180. {
  181. //TODO
  182. }
  183. void CDrawLinesOperation::redo()
  184. {
  185. //TODO
  186. }
  187. void CDrawLinesOperation::flipPattern(LinePattern& pattern, int flip) const
  188. {
  189. //todo: use cashing here and also in terrain patterns
  190. if(flip == 0)
  191. {
  192. return;
  193. }
  194. if(flip == FLIP_PATTERN_HORIZONTAL || flip == FLIP_PATTERN_BOTH)
  195. {
  196. for(int i = 0; i < 3; ++i)
  197. {
  198. int y = i * 3;
  199. std::swap(pattern.data[y], pattern.data[y + 2]);
  200. }
  201. }
  202. if(flip == FLIP_PATTERN_VERTICAL || flip == FLIP_PATTERN_BOTH)
  203. {
  204. for(int i = 0; i < 3; ++i)
  205. {
  206. std::swap(pattern.data[i], pattern.data[6 + i]);
  207. }
  208. }
  209. }
  210. void CDrawLinesOperation::updateTiles(std::set<int3> & invalidated)
  211. {
  212. for(const int3 & coord : invalidated)
  213. {
  214. TerrainTile & tile = map->getTile(coord);
  215. ValidationResult result(false);
  216. if(!needUpdateTile(tile))
  217. continue;
  218. int bestPattern = -1;
  219. for(int k = 0; k < patterns.size(); ++k)
  220. {
  221. result = validateTile(patterns[k], coord);
  222. if(result.result)
  223. {
  224. bestPattern = k;
  225. break;
  226. }
  227. }
  228. if(bestPattern != -1)
  229. {
  230. updateTile(tile, patterns[bestPattern], result.flip);
  231. }
  232. }
  233. }
  234. CDrawLinesOperation::ValidationResult CDrawLinesOperation::validateTile(const LinePattern & pattern, const int3 & pos)
  235. {
  236. ValidationResult result(false);
  237. if(!canApplyPattern(pattern))
  238. return result;
  239. for(int flip = 0; flip < 4; ++flip)
  240. {
  241. if((flip == FLIP_PATTERN_BOTH) && !(pattern.hasHFlip && pattern.hasVFlip))
  242. continue;
  243. if((flip == FLIP_PATTERN_HORIZONTAL) && !pattern.hasHFlip)
  244. continue;
  245. if((flip == FLIP_PATTERN_VERTICAL) && !(pattern.hasVFlip))
  246. continue;
  247. LinePattern flipped = pattern;
  248. flipPattern(flipped, flip);
  249. bool validated = true;
  250. for(int i = 0; i < 9; ++i)
  251. {
  252. if(4 == i)
  253. continue;
  254. int cx = pos.x + (i % 3) - 1;
  255. int cy = pos.y + (i / 3) - 1;
  256. int3 currentPos(cx, cy, pos.z);
  257. bool hasSomething = map->isInTheMap(currentPos) && tileHasSomething(currentPos);
  258. if(ruleIsSomething(flipped.data[i]))
  259. {
  260. if(!hasSomething)
  261. {
  262. validated = false;
  263. break;
  264. }
  265. }
  266. else if(ruleIsNone(flipped.data[i]))
  267. {
  268. if(hasSomething)
  269. {
  270. validated = false;
  271. break;
  272. }
  273. }
  274. else
  275. {
  276. assert(ruleIsAny(flipped.data[i]));
  277. }
  278. }
  279. if(validated)
  280. {
  281. result.result = true;
  282. result.flip = flip;
  283. return result;
  284. }
  285. }
  286. return result;
  287. }
  288. std::string CDrawRoadsOperation::getLabel() const
  289. {
  290. return "Draw Roads";
  291. }
  292. std::string CDrawRiversOperation::getLabel() const
  293. {
  294. return "Draw Rivers";
  295. }
  296. void CDrawRoadsOperation::executeTile(TerrainTile & tile)
  297. {
  298. tile.roadType = const_cast<RoadType*>(VLC->roadTypeHandler->getByIndex(roadType.getNum()));
  299. }
  300. void CDrawRiversOperation::executeTile(TerrainTile & tile)
  301. {
  302. tile.riverType = const_cast<RiverType*>(VLC->riverTypeHandler->getByIndex(riverType.getNum()));
  303. }
  304. bool CDrawRoadsOperation::canApplyPattern(const LinePattern & pattern) const
  305. {
  306. return pattern.roadMapping.first >= 0;
  307. }
  308. bool CDrawRiversOperation::canApplyPattern(const LinePattern & pattern) const
  309. {
  310. return pattern.riverMapping.first >= 0;
  311. }
  312. bool CDrawRoadsOperation::needUpdateTile(const TerrainTile & tile) const
  313. {
  314. return tile.roadType->getId() != Road::NO_ROAD;
  315. }
  316. bool CDrawRiversOperation::needUpdateTile(const TerrainTile & tile) const
  317. {
  318. return tile.riverType->getId() != River::NO_RIVER;
  319. }
  320. bool CDrawRoadsOperation::tileHasSomething(const int3& pos) const
  321. {
  322. return map->getTile(pos).roadType->getId() != Road::NO_ROAD;
  323. }
  324. bool CDrawRiversOperation::tileHasSomething(const int3& pos) const
  325. {
  326. return map->getTile(pos).riverType->getId() != River::NO_RIVER;
  327. }
  328. void CDrawRoadsOperation::updateTile(TerrainTile & tile, const LinePattern & pattern, const int flip)
  329. {
  330. const std::pair<int, int> & mapping = pattern.roadMapping;
  331. tile.roadDir = gen->nextInt(mapping.first, mapping.second);
  332. tile.extTileFlags = (tile.extTileFlags & 0b11001111) | (flip << 4);
  333. }
  334. void CDrawRiversOperation::updateTile(TerrainTile & tile, const LinePattern & pattern, const int flip)
  335. {
  336. const std::pair<int, int> & mapping = pattern.riverMapping;
  337. tile.riverDir = gen->nextInt(mapping.first, mapping.second);
  338. tile.extTileFlags = (tile.extTileFlags & 0b00111111) | (flip << 2);
  339. }
  340. VCMI_LIB_NAMESPACE_END