autotile_parser.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SuperTux
  2. // Copyright (C) 2020 A. Semphris <semphris@protonmail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "supertux/autotile_parser.hpp"
  17. #include <sstream>
  18. #include <sexp/value.hpp>
  19. #include <sexp/io.hpp>
  20. #include "supertux/gameconfig.hpp"
  21. #include "supertux/globals.hpp"
  22. #include "util/log.hpp"
  23. #include "util/reader_document.hpp"
  24. #include "util/reader_mapping.hpp"
  25. #include "util/file_system.hpp"
  26. AutotileParser::AutotileParser(std::vector<std::unique_ptr<AutotileSet>>& autotilesets, const std::string& filename) :
  27. m_autotilesets(autotilesets),
  28. m_filename(filename),
  29. m_tiles_path()
  30. {
  31. }
  32. void
  33. AutotileParser::parse()
  34. {
  35. m_tiles_path = FileSystem::dirname(m_filename);
  36. auto doc = ReaderDocument::from_file(m_filename);
  37. auto root = doc.get_root();
  38. if (root.get_name() != "supertux-autotiles") {
  39. throw std::runtime_error("File is not a supertux autotile configuration file (Root list doesn't start with 'supertux-autotiles').");
  40. }
  41. auto iter = root.get_mapping().get_iter();
  42. while (iter.next())
  43. {
  44. if (iter.get_key() == "autotileset")
  45. {
  46. ReaderMapping tile_mapping = iter.as_mapping();
  47. parse_autotileset(tile_mapping, false);
  48. }
  49. else if (iter.get_key() == "autotileset-corner")
  50. {
  51. ReaderMapping tile_mapping = iter.as_mapping();
  52. parse_autotileset(tile_mapping, true);
  53. }
  54. else
  55. {
  56. log_warning << "Unknown symbol '" << iter.get_key() << "' in autotile config file" << std::endl;
  57. }
  58. }
  59. }
  60. void
  61. AutotileParser::parse_autotileset(const ReaderMapping& reader, bool corner)
  62. {
  63. std::vector<Autotile*> autotiles;
  64. std::string name = "[unnamed]";
  65. if (!reader.get("name", name))
  66. {
  67. log_warning << "Unnamed autotileset parsed" << std::endl;
  68. }
  69. uint32_t default_id = 0;
  70. if (!reader.get("default", default_id))
  71. {
  72. log_warning << "No default tile for autotileset " << name << std::endl;
  73. }
  74. auto iter = reader.get_iter();
  75. while (iter.next())
  76. {
  77. if (iter.get_key() == "autotile")
  78. {
  79. ReaderMapping tile_mapping = iter.as_mapping();
  80. autotiles.push_back(parse_autotile(tile_mapping, corner));
  81. }
  82. else if (iter.get_key() != "name" && iter.get_key() != "default")
  83. {
  84. log_warning << "Unknown symbol '" << iter.get_key() << "' in autotile config file" << std::endl;
  85. }
  86. }
  87. std::unique_ptr<AutotileSet> autotileset = std::make_unique<AutotileSet>(autotiles, default_id, name, corner);
  88. if (g_config->developer_mode)
  89. {
  90. autotileset->validate();
  91. }
  92. m_autotilesets.push_back(std::move(autotileset));
  93. }
  94. Autotile*
  95. AutotileParser::parse_autotile(const ReaderMapping& reader, bool corner)
  96. {
  97. std::vector<AutotileMask> autotile_masks;
  98. std::vector<std::pair<uint32_t, float>> alt_ids;
  99. uint32_t tile_id;
  100. if (!reader.get("id", tile_id))
  101. {
  102. throw std::runtime_error("Missing 'id' parameter in autotileset config file.");
  103. }
  104. bool solid;
  105. if (!reader.get("solid", solid))
  106. {
  107. if (!corner)
  108. throw std::runtime_error("Missing 'solid' parameter in autotileset config file.");
  109. }
  110. else
  111. {
  112. if (corner)
  113. throw std::runtime_error("'solid' parameter not needed for corner-based tiles in autotileset config file.");
  114. }
  115. auto iter = reader.get_iter();
  116. while (iter.next())
  117. {
  118. if (iter.get_key() == "mask")
  119. {
  120. std::string mask;
  121. iter.get(mask);
  122. if (corner)
  123. {
  124. parse_mask_corner(mask, autotile_masks);
  125. }
  126. else
  127. {
  128. parse_mask(mask, autotile_masks, solid);
  129. }
  130. }
  131. else if (iter.get_key() == "alt-id")
  132. {
  133. ReaderMapping alt_reader = iter.as_mapping();
  134. uint32_t alt_id = 0;
  135. if (!alt_reader.get("id", alt_id))
  136. {
  137. log_warning << "No alt tile for autotileset" << std::endl;
  138. }
  139. float weight = 0.0f;
  140. if (!alt_reader.get("weight", weight))
  141. {
  142. log_warning << "No weight for alt tile id" << std::endl;
  143. }
  144. if (alt_id != 0 && weight != 0.0f)
  145. {
  146. alt_ids.push_back(std::pair<uint32_t, float>(alt_id, weight));
  147. }
  148. }
  149. else if (iter.get_key() != "id" && iter.get_key() != "solid")
  150. {
  151. log_warning << "Unknown symbol '" << iter.get_key() << "' in autotile config file" << std::endl;
  152. }
  153. }
  154. return new Autotile(tile_id, alt_ids, autotile_masks, solid);
  155. }
  156. void
  157. AutotileParser::parse_mask(const std::string& mask, std::vector<AutotileMask>& autotile_masks, bool solid)
  158. {
  159. parse_mask(mask, autotile_masks, solid, false);
  160. }
  161. void
  162. AutotileParser::parse_mask_corner(const std::string& mask, std::vector<AutotileMask>& autotile_masks)
  163. {
  164. parse_mask(mask, autotile_masks, true, true);
  165. }
  166. void
  167. AutotileParser::parse_mask(const std::string& mask, std::vector<AutotileMask>& autotile_masks, bool solid, bool is_corner)
  168. {
  169. size_t mask_size = is_corner ? 4 : 8;
  170. if (mask.size() != mask_size)
  171. {
  172. std::ostringstream msg;
  173. msg << (is_corner ? "Autotile config : corner-based mask isn't exactly 4 characters." :
  174. "Autotile config : mask isn't exactly 8 characters.");
  175. throw std::runtime_error(msg.str());
  176. }
  177. std::vector<uint8_t> masks;
  178. masks.push_back(0);
  179. for (size_t i = 0; i < mask_size; i++)
  180. {
  181. std::vector<uint8_t> new_masks;
  182. switch (mask[i])
  183. {
  184. case '0':
  185. for (uint8_t val : masks)
  186. {
  187. new_masks.push_back(static_cast<uint8_t>(val * 2));
  188. }
  189. break;
  190. case '1':
  191. for (uint8_t val : masks)
  192. {
  193. new_masks.push_back(static_cast<uint8_t>(val * 2 + 1));
  194. }
  195. break;
  196. case '*':
  197. for (uint8_t val : masks)
  198. {
  199. new_masks.push_back(static_cast<uint8_t>(val * 2));
  200. new_masks.push_back(static_cast<uint8_t>(val * 2 + 1));
  201. }
  202. break;
  203. default:
  204. throw std::runtime_error("Autotile config : unrecognized character");
  205. }
  206. masks = new_masks;
  207. }
  208. for (uint8_t val : masks)
  209. {
  210. autotile_masks.push_back(AutotileMask(val, solid));
  211. }
  212. }
  213. /* EOF */