CMapEditManager.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #include "StdInc.h"
  2. #include "CMapEditManager.h"
  3. #include "../JsonNode.h"
  4. #include "../Filesystem/CResourceLoader.h"
  5. #include "../CDefObjInfoHandler.h"
  6. const std::string TerrainViewPattern::FLIP_MODE_SAME_IMAGE = "sameImage";
  7. const std::string TerrainViewPattern::FLIP_MODE_DIFF_IMAGES = "diffImages";
  8. const std::string TerrainViewPattern::RULE_DIRT = "D";
  9. const std::string TerrainViewPattern::RULE_SAND = "S";
  10. const std::string TerrainViewPattern::RULE_TRANSITION = "T";
  11. const std::string TerrainViewPattern::RULE_NATIVE = "N";
  12. const std::string TerrainViewPattern::RULE_ANY = "?";
  13. TerrainViewPattern::TerrainViewPattern() : minPoints(0), flipMode(FLIP_MODE_SAME_IMAGE),
  14. terGroup(ETerrainGroup::NORMAL)
  15. {
  16. }
  17. CTerrainViewPatternConfig::CTerrainViewPatternConfig()
  18. {
  19. const JsonNode config(ResourceID("config/terrainViewPatterns.json"));
  20. const std::map<std::string, ETerrainGroup::ETerrainGroup> terGroups
  21. = boost::assign::map_list_of("normal", ETerrainGroup::NORMAL)("dirt", ETerrainGroup::DIRT)
  22. ("sand", ETerrainGroup::SAND)("water", ETerrainGroup::WATER)("rock", ETerrainGroup::ROCK);
  23. BOOST_FOREACH(auto terMapping, terGroups)
  24. {
  25. BOOST_FOREACH(const JsonNode & ptrnNode, config[terMapping.first].Vector())
  26. {
  27. TerrainViewPattern pattern;
  28. // Read pattern data
  29. const JsonVector & data = ptrnNode["data"].Vector();
  30. if(data.size() != 9)
  31. {
  32. throw std::runtime_error("Size of pattern's data vector has to be 9.");
  33. }
  34. for(int i = 0; i < data.size(); ++i)
  35. {
  36. std::string cell = data[i].String();
  37. boost::algorithm::erase_all(cell, " ");
  38. std::vector<std::string> rules;
  39. boost::split(rules, cell, boost::is_any_of(","));
  40. BOOST_FOREACH(std::string ruleStr, rules)
  41. {
  42. std::vector<std::string> rule;
  43. boost::split(rule, ruleStr, boost::is_any_of("-"));
  44. std::pair<std::string, int> pair;
  45. pair.first = rule[0];
  46. if(rule.size() > 1)
  47. {
  48. pair.second = boost::lexical_cast<int>(rule[1]);
  49. }
  50. else
  51. {
  52. pair.second = 0;
  53. }
  54. pattern.data[i].push_back(pair);
  55. }
  56. }
  57. // Read mapping
  58. std::string mappingStr = ptrnNode["mapping"].String();
  59. boost::algorithm::erase_all(mappingStr, " ");
  60. std::vector<std::string> mappings;
  61. boost::split(mappings, mappingStr, boost::is_any_of(","));
  62. BOOST_FOREACH(std::string mapping, mappings)
  63. {
  64. std::vector<std::string> range;
  65. boost::split(range, mapping, boost::is_any_of("-"));
  66. pattern.mapping.push_back(std::make_pair(boost::lexical_cast<int>(range[0]),
  67. boost::lexical_cast<int>(range.size() > 1 ? range[1] : range[0])));
  68. }
  69. // Read optional attributes
  70. pattern.id = ptrnNode["id"].String();
  71. pattern.minPoints = static_cast<int>(ptrnNode["minPoints"].Float());
  72. pattern.flipMode = ptrnNode["flipMode"].String();
  73. if(pattern.flipMode.empty())
  74. {
  75. pattern.flipMode = TerrainViewPattern::FLIP_MODE_SAME_IMAGE;
  76. }
  77. pattern.terGroup = terMapping.second;
  78. patterns[terMapping.second].push_back(pattern);
  79. }
  80. }
  81. }
  82. const std::vector<TerrainViewPattern> & CTerrainViewPatternConfig::getPatternsForGroup(ETerrainGroup::ETerrainGroup terGroup) const
  83. {
  84. return patterns.find(terGroup)->second;
  85. }
  86. CMapEditManager::CMapEditManager(const CTerrainViewPatternConfig * terViewPatternConfig, CMap * map, int randomSeed /*= std::time(nullptr)*/)
  87. : map(map), terViewPatternConfig(terViewPatternConfig)
  88. {
  89. gen.seed(randomSeed);
  90. }
  91. void CMapEditManager::clearTerrain()
  92. {
  93. for(int i = 0; i < map->width; ++i)
  94. {
  95. for(int j = 0; j < map->height; ++j)
  96. {
  97. map->terrain[i][j][0].terType = ETerrainType::WATER;
  98. map->terrain[i][j][0].terView = gen.getInteger(20, 32);
  99. if(map->twoLevel)
  100. {
  101. map->terrain[i][j][1].terType = ETerrainType::ROCK;
  102. map->terrain[i][j][1].terView = 0;
  103. }
  104. }
  105. }
  106. }
  107. void CMapEditManager::drawTerrain(ETerrainType::ETerrainType terType, int posx, int posy, int width, int height, bool underground)
  108. {
  109. bool mapLevel = underground ? 1 : 0;
  110. for(int i = posx; i < posx + width; ++i)
  111. {
  112. for(int j = posy; j < posy + height; ++j)
  113. {
  114. map->terrain[i][j][mapLevel].terType = terType;
  115. }
  116. }
  117. //TODO there are situations where more tiles are affected implicitely
  118. //TODO add coastal bit to extTileFlags appropriately
  119. //updateTerrainViews(posx - 1, posy - 1, width + 2, height + 2, mapLevel);
  120. }
  121. void CMapEditManager::updateTerrainViews(int posx, int posy, int width, int height, int mapLevel)
  122. {
  123. for(int i = posx; i < posx + width; ++i)
  124. {
  125. for(int j = posy; j < posy + height; ++j)
  126. {
  127. const std::vector<TerrainViewPattern> & patterns =
  128. terViewPatternConfig->getPatternsForGroup(getTerrainGroup(map->terrain[i][j][mapLevel].terType));
  129. // Detect a pattern which fits best
  130. int totalPoints, bestPattern, bestFlip = -1;
  131. std::string transitionReplacement;
  132. for(int i = 0; i < patterns.size(); ++i)
  133. {
  134. const TerrainViewPattern & pattern = patterns[i];
  135. for(int flip = 0; flip < 3; ++flip)
  136. {
  137. ValidationResult valRslt = validateTerrainView(i, j, mapLevel, flip > 0 ? getFlippedPattern(pattern, flip) : pattern);
  138. if(valRslt.result)
  139. {
  140. if(valRslt.points > totalPoints)
  141. {
  142. totalPoints = valRslt.points;
  143. bestPattern = i;
  144. bestFlip = flip;
  145. transitionReplacement = valRslt.transitionReplacement;
  146. }
  147. break;
  148. }
  149. }
  150. }
  151. if(bestPattern == -1)
  152. {
  153. continue;
  154. }
  155. // Get mapping
  156. const TerrainViewPattern & pattern = patterns[bestPattern];
  157. std::pair<int, int> mapping;
  158. if(transitionReplacement.empty())
  159. {
  160. mapping = pattern.mapping[0];
  161. }
  162. else
  163. {
  164. mapping = transitionReplacement == TerrainViewPattern::RULE_DIRT ? pattern.mapping[0] : pattern.mapping[1];
  165. }
  166. // Set terrain view
  167. if(pattern.flipMode == TerrainViewPattern::FLIP_MODE_SAME_IMAGE)
  168. {
  169. map->terrain[i][j][mapLevel].terView = gen.getInteger(mapping.first, mapping.second);
  170. map->terrain[i][j][mapLevel].extTileFlags = bestFlip;
  171. }
  172. else
  173. {
  174. int range = (mapping.second - mapping.first) / 4;
  175. map->terrain[i][j][mapLevel].terView = gen.getInteger(mapping.first + bestFlip * range,
  176. mapping.first + (bestFlip + 1) * range - 1);
  177. map->terrain[i][j][mapLevel].extTileFlags = 0;
  178. }
  179. }
  180. }
  181. }
  182. ETerrainGroup::ETerrainGroup CMapEditManager::getTerrainGroup(ETerrainType::ETerrainType terType) const
  183. {
  184. switch(terType)
  185. {
  186. case ETerrainType::DIRT:
  187. return ETerrainGroup::DIRT;
  188. case ETerrainType::SAND:
  189. return ETerrainGroup::SAND;
  190. case ETerrainType::WATER:
  191. return ETerrainGroup::WATER;
  192. case ETerrainType::ROCK:
  193. return ETerrainGroup::ROCK;
  194. default:
  195. return ETerrainGroup::NORMAL;
  196. }
  197. }
  198. CMapEditManager::ValidationResult CMapEditManager::validateTerrainView(int posx, int posy, int mapLevel, const TerrainViewPattern & pattern) const
  199. {
  200. ETerrainType::ETerrainType centerTerType = map->terrain[posx][posy][mapLevel].terType;
  201. int totalPoints = 0;
  202. std::string transitionReplacement;
  203. for(int i = 0; i < 9; ++i)
  204. {
  205. // The center, middle cell can be skipped
  206. if(i == 4)
  207. {
  208. continue;
  209. }
  210. // Get terrain group of the current cell
  211. int cx = posx + (i % 3) - 1;
  212. int cy = posy + (i / 3) - 1;
  213. bool isAlien = false;
  214. ETerrainType::ETerrainType terType;
  215. if(cx < 0 || cx >= map->width || cy < 0 || cy >= map->height)
  216. {
  217. terType = centerTerType;
  218. }
  219. else
  220. {
  221. terType = map->terrain[cx][cy][mapLevel].terType;
  222. if(terType != centerTerType)
  223. {
  224. isAlien = true;
  225. }
  226. }
  227. // Validate all rules per cell
  228. int topPoints = -1;
  229. for(int j = 0; j < pattern.data[i].size(); ++j)
  230. {
  231. const std::pair<std::string, int> & rulePair = pattern.data[i][j];
  232. const std::string & rule = rulePair.first;
  233. bool isNative = (rule == TerrainViewPattern::RULE_NATIVE || rule == TerrainViewPattern::RULE_ANY) && !isAlien;
  234. auto validationRslt = [&](bool rslt)
  235. {
  236. if(rslt)
  237. {
  238. topPoints = std::max(topPoints, rulePair.second);
  239. }
  240. return rslt;
  241. };
  242. // Validate cell with the ruleset of the pattern
  243. bool validation;
  244. if(pattern.terGroup == ETerrainGroup::NORMAL)
  245. {
  246. bool isDirt = (rule == TerrainViewPattern::RULE_DIRT
  247. || rule == TerrainViewPattern::RULE_TRANSITION || rule == TerrainViewPattern::RULE_ANY)
  248. && isAlien && !isSandType(terType);
  249. bool isSand = (rule == TerrainViewPattern::RULE_SAND || rule == TerrainViewPattern::RULE_TRANSITION
  250. || rule == TerrainViewPattern::RULE_ANY)
  251. && isSandType(terType);
  252. if(transitionReplacement.empty() && (rule == TerrainViewPattern::RULE_TRANSITION
  253. || rule == TerrainViewPattern::RULE_ANY) && (isDirt || isSand))
  254. {
  255. transitionReplacement = isDirt ? TerrainViewPattern::RULE_DIRT : TerrainViewPattern::RULE_SAND;
  256. }
  257. validation = validationRslt((isDirt && transitionReplacement != TerrainViewPattern::RULE_SAND)
  258. || (isSand && transitionReplacement != TerrainViewPattern::RULE_DIRT)
  259. || isNative);
  260. }
  261. else if(pattern.terGroup == ETerrainGroup::DIRT)
  262. {
  263. bool isSand = rule == TerrainViewPattern::RULE_SAND && isSandType(terType);
  264. bool isDirt = rule == TerrainViewPattern::RULE_DIRT && !isSandType(terType) && !isNative;
  265. validation = validationRslt(rule == TerrainViewPattern::RULE_ANY || isSand || isDirt || isNative);
  266. }
  267. else if(pattern.terGroup == ETerrainGroup::SAND || pattern.terGroup == ETerrainGroup::WATER ||
  268. pattern.terGroup == ETerrainGroup::ROCK)
  269. {
  270. bool isSand = rule == TerrainViewPattern::RULE_SAND && isSandType(terType) && !isNative;
  271. validation = validationRslt(rule == TerrainViewPattern::RULE_ANY || isSand || isNative);
  272. }
  273. if(!validation)
  274. {
  275. return ValidationResult(false);
  276. }
  277. }
  278. if(topPoints == -1)
  279. {
  280. return ValidationResult(false);
  281. }
  282. else
  283. {
  284. totalPoints += topPoints;
  285. }
  286. }
  287. if(pattern.minPoints > totalPoints)
  288. {
  289. return ValidationResult(false);
  290. }
  291. return ValidationResult(true, totalPoints, transitionReplacement);
  292. }
  293. bool CMapEditManager::isSandType(ETerrainType::ETerrainType terType) const
  294. {
  295. switch(terType)
  296. {
  297. case ETerrainType::WATER:
  298. case ETerrainType::SAND:
  299. case ETerrainType::ROCK:
  300. return true;
  301. default:
  302. return false;
  303. }
  304. }
  305. TerrainViewPattern CMapEditManager::getFlippedPattern(const TerrainViewPattern & pattern, int flip) const
  306. {
  307. if(flip == 0)
  308. {
  309. return pattern;
  310. }
  311. TerrainViewPattern ret = pattern;
  312. if(flip == FLIP_PATTERN_HORIZONTAL || flip == FLIP_PATTERN_BOTH)
  313. {
  314. for(int i = 0; i < 3; ++i)
  315. {
  316. int y = i * 3;
  317. std::swap(ret.data[y], ret.data[y + 2]);
  318. }
  319. }
  320. if(flip == FLIP_PATTERN_VERTICAL || flip == FLIP_PATTERN_BOTH)
  321. {
  322. for(int i = 0; i < 3; ++i)
  323. {
  324. std::swap(ret.data[i], ret.data[6 + i]);
  325. }
  326. }
  327. return ret;
  328. }
  329. void CMapEditManager::insertObject(CGObjectInstance * obj, int posx, int posy, bool underground)
  330. {
  331. obj->pos = int3(posx, posy, underground ? 1 : 0);
  332. obj->id = map->objects.size();
  333. map->objects.push_back(obj);
  334. if(obj->ID == Obj::TOWN)
  335. {
  336. map->towns.push_back(static_cast<CGTownInstance *>(obj));
  337. }
  338. if(obj->ID == Obj::HERO)
  339. {
  340. map->heroes.push_back(static_cast<CGHeroInstance*>(obj));
  341. }
  342. map->addBlockVisTiles(obj);
  343. }
  344. CMapEditManager::ValidationResult::ValidationResult(bool result, int points /*= 0*/, const std::string & transitionReplacement /*= ""*/)
  345. : result(result), points(points), transitionReplacement(transitionReplacement)
  346. {
  347. }