CDrawRoadsOperation.cpp 7.5 KB

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