💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

251 lines
8.1 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "resource-loader.hpp"
  20. #include "resource-manager.hpp"
  21. #include "string-table.hpp"
  22. #include "renderer/model.hpp"
  23. #include "entity/components/behavior.hpp"
  24. #include "entity/components/collision.hpp"
  25. #include "entity/components/terrain.hpp"
  26. #include "entity/components/transform.hpp"
  27. #include "entity/components/model.hpp"
  28. #include "entity/components/nest.hpp"
  29. #include "entity/components/tool.hpp"
  30. #include "entity/components/marker.hpp"
  31. #include "entity/components/brush.hpp"
  32. #include "entity/archetype.hpp"
  33. #include "entity/ebt.hpp"
  34. #include <sstream>
  35. #include <stdexcept>
  36. static bool load_component_behavior(entity::archetype& archetype, resource_manager& resource_manager, const std::vector<std::string>& parameters)
  37. {
  38. if (parameters.size() != 2)
  39. {
  40. throw std::runtime_error("load_component_behavior(): Invalid parameter count.");
  41. }
  42. std::string filename = parameters[1];
  43. entity::component::behavior component;
  44. component.behavior_tree = resource_manager.load<entity::ebt::node>(filename);
  45. if (!component.behavior_tree)
  46. {
  47. std::string message = std::string("load_component_behavior(): Failed to load behavior tree \"") + filename + std::string("\"");
  48. throw std::runtime_error(message);
  49. }
  50. archetype.set<entity::component::behavior>(component);
  51. return true;
  52. }
  53. static bool load_component_collision(entity::archetype& archetype, resource_manager& resource_manager, const std::vector<std::string>& parameters)
  54. {
  55. if (parameters.size() != 2)
  56. {
  57. throw std::runtime_error("load_component_collision(): Invalid parameter count.");
  58. }
  59. std::string filename = parameters[1];
  60. entity::component::collision component;
  61. component.mesh = resource_manager.load<geom::mesh>(filename);
  62. if (!component.mesh)
  63. {
  64. std::string message = std::string("load_component_collision(): Failed to load model \"") + filename + std::string("\"");
  65. throw std::runtime_error(message);
  66. }
  67. archetype.set<entity::component::collision>(component);
  68. return true;
  69. }
  70. static bool load_component_model(entity::archetype& archetype, resource_manager& resource_manager, const std::vector<std::string>& parameters)
  71. {
  72. if (parameters.size() != 2)
  73. {
  74. throw std::runtime_error("load_component_model(): Invalid parameter count.");
  75. }
  76. std::string filename = parameters[1];
  77. entity::component::model component;
  78. component.render_model = resource_manager.load<model>(filename);
  79. component.instance_count = 0;
  80. component.layers = 1;
  81. if (!component.render_model)
  82. {
  83. std::string message = std::string("load_component_model(): Failed to load model \"") + filename + std::string("\"");
  84. throw std::runtime_error(message);
  85. }
  86. archetype.set<entity::component::model>(component);
  87. return true;
  88. }
  89. static bool load_component_nest(entity::archetype& archetype, const std::vector<std::string>& parameters)
  90. {
  91. entity::component::nest component;
  92. archetype.set<entity::component::nest>(component);
  93. return true;
  94. }
  95. static bool load_component_marker(entity::archetype& archetype, const std::vector<std::string>& parameters)
  96. {
  97. if (parameters.size() != 2)
  98. {
  99. throw std::runtime_error("load_component_marker(): Invalid parameter count.");
  100. }
  101. entity::component::marker component;
  102. component.color = std::stoi(parameters[1]);
  103. archetype.set<entity::component::marker>(component);
  104. return true;
  105. }
  106. static bool load_component_brush(entity::archetype& archetype, const std::vector<std::string>& parameters)
  107. {
  108. if (parameters.size() != 2)
  109. {
  110. throw std::runtime_error("load_component_brush(): Invalid parameter count.");
  111. }
  112. entity::component::brush component;
  113. component.radius = std::stof(parameters[1]);
  114. archetype.set<entity::component::brush>(component);
  115. return true;
  116. }
  117. static bool load_component_terrain(entity::archetype& archetype, const std::vector<std::string>& parameters)
  118. {
  119. if (parameters.size() != 4)
  120. {
  121. throw std::runtime_error("load_component_terrain(): Invalid parameter count.");
  122. }
  123. entity::component::terrain component;
  124. component.subdivisions = std::stoi(parameters[1]);
  125. component.x = std::stoi(parameters[2]);
  126. component.z = std::stoi(parameters[3]);
  127. archetype.set<entity::component::terrain>(component);
  128. return true;
  129. }
  130. static bool load_component_tool(entity::archetype& archetype, const std::vector<std::string>& parameters)
  131. {
  132. if (parameters.size() != 5)
  133. {
  134. throw std::runtime_error("load_component_tool(): Invalid parameter count.");
  135. }
  136. entity::component::tool component;
  137. component.active = static_cast<bool>(std::stoi(parameters[1]));
  138. component.idle_distance = std::stof(parameters[2]);
  139. component.active_distance = std::stof(parameters[3]);
  140. component.heliotropic = static_cast<bool>(std::stoi(parameters[4]));
  141. archetype.set<entity::component::tool>(component);
  142. return true;
  143. }
  144. static bool load_component_transform(entity::archetype& archetype, const std::vector<std::string>& parameters)
  145. {
  146. if (parameters.size() != 11)
  147. {
  148. throw std::runtime_error("load_component_transform(): Invalid parameter count.");
  149. }
  150. std::stringstream stream;
  151. for (std::size_t i = 1; i < parameters.size(); ++i)
  152. {
  153. stream << parameters[i];
  154. if (i < parameters.size() - 1)
  155. stream << ' ';
  156. }
  157. entity::component::transform component;
  158. stream >> component.local.translation.x;
  159. stream >> component.local.translation.y;
  160. stream >> component.local.translation.z;
  161. stream >> component.local.rotation.w;
  162. stream >> component.local.rotation.x;
  163. stream >> component.local.rotation.y;
  164. stream >> component.local.rotation.z;
  165. stream >> component.local.scale.x;
  166. stream >> component.local.scale.y;
  167. stream >> component.local.scale.z;
  168. component.warp = true;
  169. archetype.set<entity::component::transform>(component);
  170. return true;
  171. }
  172. static bool load_component(entity::archetype& archetype, resource_manager& resource_manager, const std::vector<std::string>& parameters)
  173. {
  174. if (parameters[0] == "behavior") return load_component_behavior(archetype, resource_manager, parameters);
  175. if (parameters[0] == "collision") return load_component_collision(archetype, resource_manager, parameters);
  176. if (parameters[0] == "model") return load_component_model(archetype, resource_manager, parameters);
  177. if (parameters[0] == "nest") return load_component_nest(archetype, parameters);
  178. if (parameters[0] == "terrain") return load_component_terrain(archetype, parameters);
  179. if (parameters[0] == "tool") return load_component_tool(archetype, parameters);
  180. if (parameters[0] == "transform") return load_component_transform(archetype, parameters);
  181. if (parameters[0] == "marker") return load_component_marker(archetype, parameters);
  182. if (parameters[0] == "brush") return load_component_brush(archetype, parameters);
  183. std::string message = std::string("load_component(): Unknown component type \"") + parameters[0] + std::string("\"");
  184. throw std::runtime_error(message);
  185. }
  186. template <>
  187. entity::archetype* resource_loader<entity::archetype>::load(resource_manager* resource_manager, PHYSFS_File* file)
  188. {
  189. entity::archetype* archetype = new entity::archetype(resource_manager->get_archetype_registry());
  190. // Load string table from input stream
  191. string_table* table = resource_loader<string_table>::load(resource_manager, file);
  192. // Ensure table is not empty.
  193. if (!table || table->empty())
  194. {
  195. delete table;
  196. return nullptr;
  197. }
  198. // Load components from table rows
  199. for (const string_table_row& row: *table)
  200. {
  201. // Skip empty rows and comments
  202. if (row.empty() || row[0].empty() || row[0][0] == '#')
  203. {
  204. continue;
  205. }
  206. load_component(*archetype, *resource_manager, row);
  207. }
  208. return archetype;
  209. }