CDrawRoadsOperation.cpp 6.2 KB

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