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

237 lines
7.4 KiB

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