💿🐜 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.

253 lines
7.9 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 "ecs/components/behavior-component.hpp"
  24. #include "ecs/components/collision-component.hpp"
  25. #include "ecs/components/terrain-component.hpp"
  26. #include "ecs/components/transform-component.hpp"
  27. #include "ecs/components/model-component.hpp"
  28. #include "ecs/components/nest-component.hpp"
  29. #include "ecs/components/tool-component.hpp"
  30. #include "ecs/components/marker-component.hpp"
  31. #include "ecs/components/brush-component.hpp"
  32. #include "ecs/archetype.hpp"
  33. #include "ecs/ebt.hpp"
  34. #include <sstream>
  35. #include <stdexcept>
  36. using namespace ecs;
  37. static bool load_behavior_component(archetype& archetype, resource_manager& resource_manager, const std::vector<std::string>& parameters)
  38. {
  39. if (parameters.size() != 2)
  40. {
  41. throw std::runtime_error("load_behavior_component(): Invalid parameter count.");
  42. }
  43. std::string filename = parameters[1];
  44. behavior_component component;
  45. component.behavior_tree = resource_manager.load<ecs::ebt::node>(filename);
  46. if (!component.behavior_tree)
  47. {
  48. std::string message = std::string("load_behavior_component(): Failed to load behavior tree \"") + filename + std::string("\"");
  49. throw std::runtime_error(message);
  50. }
  51. archetype.set<behavior_component>(component);
  52. return true;
  53. }
  54. static bool load_collision_component(archetype& archetype, resource_manager& resource_manager, const std::vector<std::string>& parameters)
  55. {
  56. if (parameters.size() != 2)
  57. {
  58. throw std::runtime_error("load_collision_component(): Invalid parameter count.");
  59. }
  60. std::string filename = parameters[1];
  61. collision_component component;
  62. component.mesh = resource_manager.load<geom::mesh>(filename);
  63. if (!component.mesh)
  64. {
  65. std::string message = std::string("load_collision_component(): Failed to load model \"") + filename + std::string("\"");
  66. throw std::runtime_error(message);
  67. }
  68. archetype.set<collision_component>(component);
  69. return true;
  70. }
  71. static bool load_model_component(archetype& archetype, resource_manager& resource_manager, const std::vector<std::string>& parameters)
  72. {
  73. if (parameters.size() != 2)
  74. {
  75. throw std::runtime_error("load_model_component(): Invalid parameter count.");
  76. }
  77. std::string filename = parameters[1];
  78. model_component component;
  79. component.model = resource_manager.load<model>(filename);
  80. component.instance_count = 0;
  81. component.layers = 1;
  82. if (!component.model)
  83. {
  84. std::string message = std::string("load_model_component(): Failed to load model \"") + filename + std::string("\"");
  85. throw std::runtime_error(message);
  86. }
  87. archetype.set<model_component>(component);
  88. return true;
  89. }
  90. static bool load_nest_component(archetype& archetype, const std::vector<std::string>& parameters)
  91. {
  92. nest_component component;
  93. archetype.set<nest_component>(component);
  94. return true;
  95. }
  96. static bool load_marker_component(archetype& archetype, const std::vector<std::string>& parameters)
  97. {
  98. if (parameters.size() != 2)
  99. {
  100. throw std::runtime_error("load_marker_component(): Invalid parameter count.");
  101. }
  102. marker_component component;
  103. component.color = std::stoi(parameters[1]);
  104. archetype.set<marker_component>(component);
  105. return true;
  106. }
  107. static bool load_brush_component(archetype& archetype, const std::vector<std::string>& parameters)
  108. {
  109. if (parameters.size() != 2)
  110. {
  111. throw std::runtime_error("load_brush_component(): Invalid parameter count.");
  112. }
  113. brush_component component;
  114. component.radius = std::stof(parameters[1]);
  115. archetype.set<brush_component>(component);
  116. return true;
  117. }
  118. static bool load_terrain_component(archetype& archetype, const std::vector<std::string>& parameters)
  119. {
  120. if (parameters.size() != 4)
  121. {
  122. throw std::runtime_error("load_terrain_component(): Invalid parameter count.");
  123. }
  124. terrain_component component;
  125. component.subdivisions = std::stoi(parameters[1]);
  126. component.x = std::stoi(parameters[2]);
  127. component.z = std::stoi(parameters[3]);
  128. archetype.set<terrain_component>(component);
  129. return true;
  130. }
  131. static bool load_tool_component(archetype& archetype, const std::vector<std::string>& parameters)
  132. {
  133. if (parameters.size() != 5)
  134. {
  135. throw std::runtime_error("load_tool_component(): Invalid parameter count.");
  136. }
  137. tool_component component;
  138. component.active = static_cast<bool>(std::stoi(parameters[1]));
  139. component.idle_distance = std::stof(parameters[2]);
  140. component.active_distance = std::stof(parameters[3]);
  141. component.heliotropic = static_cast<bool>(std::stoi(parameters[4]));
  142. archetype.set<tool_component>(component);
  143. return true;
  144. }
  145. static bool load_transform_component(archetype& archetype, const std::vector<std::string>& parameters)
  146. {
  147. if (parameters.size() != 11)
  148. {
  149. throw std::runtime_error("load_transform_component(): Invalid parameter count.");
  150. }
  151. std::stringstream stream;
  152. for (std::size_t i = 1; i < parameters.size(); ++i)
  153. {
  154. stream << parameters[i];
  155. if (i < parameters.size() - 1)
  156. stream << ' ';
  157. }
  158. transform_component component;
  159. stream >> component.local.translation.x;
  160. stream >> component.local.translation.y;
  161. stream >> component.local.translation.z;
  162. stream >> component.local.rotation.w;
  163. stream >> component.local.rotation.x;
  164. stream >> component.local.rotation.y;
  165. stream >> component.local.rotation.z;
  166. stream >> component.local.scale.x;
  167. stream >> component.local.scale.y;
  168. stream >> component.local.scale.z;
  169. component.warp = true;
  170. archetype.set<transform_component>(component);
  171. return true;
  172. }
  173. static bool load_component(archetype& archetype, resource_manager& resource_manager, const std::vector<std::string>& parameters)
  174. {
  175. if (parameters[0] == "behavior") return load_behavior_component(archetype, resource_manager, parameters);
  176. if (parameters[0] == "collision") return load_collision_component(archetype, resource_manager, parameters);
  177. if (parameters[0] == "model") return load_model_component(archetype, resource_manager, parameters);
  178. if (parameters[0] == "nest") return load_nest_component(archetype, parameters);
  179. if (parameters[0] == "terrain") return load_terrain_component(archetype, parameters);
  180. if (parameters[0] == "tool") return load_tool_component(archetype, parameters);
  181. if (parameters[0] == "transform") return load_transform_component(archetype, parameters);
  182. if (parameters[0] == "marker") return load_marker_component(archetype, parameters);
  183. if (parameters[0] == "brush") return load_brush_component(archetype, parameters);
  184. std::string message = std::string("load_component(): Unknown component type \"") + parameters[0] + std::string("\"");
  185. throw std::runtime_error(message);
  186. }
  187. template <>
  188. archetype* resource_loader<archetype>::load(resource_manager* resource_manager, PHYSFS_File* file)
  189. {
  190. ecs::archetype* archetype = new ecs::archetype(resource_manager->get_archetype_registry());
  191. // Load string table from input stream
  192. string_table* table = resource_loader<string_table>::load(resource_manager, file);
  193. // Ensure table is not empty.
  194. if (!table || table->empty())
  195. {
  196. delete table;
  197. return nullptr;
  198. }
  199. // Load components from table rows
  200. for (const string_table_row& row: *table)
  201. {
  202. // Skip empty rows and comments
  203. if (row.empty() || row[0].empty() || row[0][0] == '#')
  204. {
  205. continue;
  206. }
  207. load_component(*archetype, *resource_manager, row);
  208. }
  209. return archetype;
  210. }