CDrawRoadsOperation.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. static bool ruleIsAny(const std::string & rule)
  138. {
  139. return rule == "?";
  140. }
  141. ///CDrawRoadsOperation
  142. CDrawRoadsOperation::CDrawRoadsOperation(CMap * map, const CTerrainSelection & terrainSel, ERoadType::ERoadType roadType, CRandomGenerator * gen):
  143. CMapOperation(map),terrainSel(terrainSel), roadType(roadType), gen(gen)
  144. {
  145. }
  146. void CDrawRoadsOperation::execute()
  147. {
  148. std::set<int3> invalidated;
  149. for(const auto & pos : terrainSel.getSelectedItems())
  150. {
  151. auto & tile = map->getTile(pos);
  152. tile.roadType = roadType;
  153. auto rect = extendTileAroundSafely(pos);
  154. rect.forEach([&invalidated](const int3 & pos)
  155. {
  156. invalidated.insert(pos);
  157. });
  158. }
  159. updateTiles(invalidated);
  160. }
  161. void CDrawRoadsOperation::undo()
  162. {
  163. //TODO
  164. }
  165. void CDrawRoadsOperation::redo()
  166. {
  167. //TODO
  168. }
  169. std::string CDrawRoadsOperation::getLabel() const
  170. {
  171. return "Draw Roads";
  172. }
  173. bool CDrawRoadsOperation::canApplyPattern(const RoadPattern & pattern) const
  174. {
  175. //TODO: this method should be virtual for river support
  176. return pattern.roadMapping.first >= 0;
  177. }
  178. void CDrawRoadsOperation::flipPattern(RoadPattern& pattern, int flip) const
  179. {
  180. //todo: use cashing here and also in terrain patterns
  181. if(flip == 0)
  182. {
  183. return;
  184. }
  185. if(flip == FLIP_PATTERN_HORIZONTAL || flip == FLIP_PATTERN_BOTH)
  186. {
  187. for(int i = 0; i < 3; ++i)
  188. {
  189. int y = i * 3;
  190. std::swap(pattern.data[y], pattern.data[y + 2]);
  191. }
  192. }
  193. if(flip == FLIP_PATTERN_VERTICAL || flip == FLIP_PATTERN_BOTH)
  194. {
  195. for(int i = 0; i < 3; ++i)
  196. {
  197. std::swap(pattern.data[i], pattern.data[6 + i]);
  198. }
  199. }
  200. }
  201. bool CDrawRoadsOperation::needUpdateTile(const TerrainTile & tile) const
  202. {
  203. return tile.roadType != ERoadType::NO_ROAD; //TODO: this method should be virtual for river support
  204. }
  205. void CDrawRoadsOperation::updateTiles(std::set<int3> & invalidated)
  206. {
  207. for(int3 coord : invalidated)
  208. {
  209. TerrainTile & tile = map->getTile(coord);
  210. ValidationResult result(false);
  211. if(!needUpdateTile(tile))
  212. continue;
  213. int bestPattern = -1;
  214. for(int k = 0; k < patterns.size(); ++k)
  215. {
  216. result = validateTile(patterns[k], coord);
  217. if(result.result)
  218. {
  219. bestPattern = k;
  220. break;
  221. }
  222. }
  223. if(bestPattern != -1)
  224. {
  225. updateTile(tile, patterns[bestPattern], result.flip);
  226. }
  227. }
  228. };
  229. bool CDrawRoadsOperation::tileHasSomething(const int3& pos) const
  230. {
  231. //TODO: this method should be virtual for river support
  232. return map->getTile(pos).roadType != ERoadType::NO_ROAD;
  233. }
  234. void CDrawRoadsOperation::updateTile(TerrainTile & tile, const RoadPattern & pattern, const int flip)
  235. {
  236. //TODO: this method should be virtual for river support
  237. const std::pair<int, int> & mapping = pattern.roadMapping;
  238. tile.roadDir = gen->nextInt(mapping.first, mapping.second);
  239. tile.extTileFlags = (tile.extTileFlags & 0xCF) | (flip << 4);
  240. }
  241. CDrawRoadsOperation::ValidationResult CDrawRoadsOperation::validateTile(const RoadPattern & pattern, const int3 & pos)
  242. {
  243. ValidationResult result(false);
  244. if(!canApplyPattern(pattern))
  245. return result;
  246. for(int flip = 0; flip < 4; ++flip)
  247. {
  248. if((flip == FLIP_PATTERN_BOTH) && !(pattern.hasHFlip && pattern.hasVFlip))
  249. continue;
  250. if((flip == FLIP_PATTERN_HORIZONTAL) && !pattern.hasHFlip)
  251. continue;
  252. if((flip == FLIP_PATTERN_VERTICAL) && !(pattern.hasVFlip))
  253. continue;
  254. RoadPattern flipped = pattern;
  255. flipPattern(flipped, flip);
  256. bool validated = true;
  257. for(int i = 0; i < 9; ++i)
  258. {
  259. if(4 == i)
  260. continue;
  261. int cx = pos.x + (i % 3) - 1;
  262. int cy = pos.y + (i / 3) - 1;
  263. int3 currentPos(cx, cy, pos.z);
  264. bool hasSomething;
  265. if(!map->isInTheMap(currentPos))
  266. {
  267. hasSomething = true; //road/river can go out of map
  268. }
  269. else
  270. {
  271. hasSomething = tileHasSomething(currentPos);
  272. }
  273. if(ruleIsSomething(flipped.data[i]))
  274. {
  275. if(!hasSomething)
  276. {
  277. validated = false;
  278. break;
  279. }
  280. }
  281. else if(ruleIsNone(flipped.data[i]))
  282. {
  283. if(hasSomething)
  284. {
  285. validated = false;
  286. break;
  287. }
  288. }
  289. else
  290. {
  291. assert(ruleIsAny(flipped.data[i]));
  292. }
  293. }
  294. if(validated)
  295. {
  296. result.result = true;
  297. result.flip = flip;
  298. return result;
  299. }
  300. }
  301. return result;
  302. }