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

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