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

234 lines
7.5 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_tool(entity::archetype& archetype, const std::vector<std::string>& parameters)
  118. {
  119. if (parameters.size() != 5)
  120. {
  121. throw std::runtime_error("load_component_tool(): Invalid parameter count.");
  122. }
  123. entity::component::tool component;
  124. component.active = static_cast<bool>(std::stoi(parameters[1]));
  125. component.idle_distance = std::stof(parameters[2]);
  126. component.active_distance = std::stof(parameters[3]);
  127. component.heliotropic = static_cast<bool>(std::stoi(parameters[4]));
  128. archetype.set<entity::component::tool>(component);
  129. return true;
  130. }
  131. static bool load_component_transform(entity::archetype& archetype, const std::vector<std::string>& parameters)
  132. {
  133. if (parameters.size() != 11)
  134. {
  135. throw std::runtime_error("load_component_transform(): Invalid parameter count.");
  136. }
  137. std::stringstream stream;
  138. for (std::size_t i = 1; i < parameters.size(); ++i)
  139. {
  140. stream << parameters[i];
  141. if (i < parameters.size() - 1)
  142. stream << ' ';
  143. }
  144. entity::component::transform component;
  145. stream >> component.local.translation.x;
  146. stream >> component.local.translation.y;
  147. stream >> component.local.translation.z;
  148. stream >> component.local.rotation.w;
  149. stream >> component.local.rotation.x;
  150. stream >> component.local.rotation.y;
  151. stream >> component.local.rotation.z;
  152. stream >> component.local.scale.x;
  153. stream >> component.local.scale.y;
  154. stream >> component.local.scale.z;
  155. component.warp = true;
  156. archetype.set<entity::component::transform>(component);
  157. return true;
  158. }
  159. static bool load_component(entity::archetype& archetype, resource_manager& resource_manager, const std::vector<std::string>& parameters)
  160. {
  161. if (parameters[0] == "behavior") return load_component_behavior(archetype, resource_manager, parameters);
  162. if (parameters[0] == "collision") return load_component_collision(archetype, resource_manager, parameters);
  163. if (parameters[0] == "model") return load_component_model(archetype, resource_manager, parameters);
  164. if (parameters[0] == "nest") return load_component_nest(archetype, parameters);
  165. if (parameters[0] == "tool") return load_component_tool(archetype, parameters);
  166. if (parameters[0] == "transform") return load_component_transform(archetype, parameters);
  167. if (parameters[0] == "marker") return load_component_marker(archetype, parameters);
  168. if (parameters[0] == "brush") return load_component_brush(archetype, parameters);
  169. std::string message = std::string("load_component(): Unknown component type \"") + parameters[0] + std::string("\"");
  170. throw std::runtime_error(message);
  171. }
  172. template <>
  173. entity::archetype* resource_loader<entity::archetype>::load(resource_manager* resource_manager, PHYSFS_File* file)
  174. {
  175. entity::archetype* archetype = new entity::archetype(resource_manager->get_archetype_registry());
  176. // Load string table from input stream
  177. string_table* table = resource_loader<string_table>::load(resource_manager, file);
  178. // Ensure table is not empty.
  179. if (!table || table->empty())
  180. {
  181. delete table;
  182. return nullptr;
  183. }
  184. // Load components from table rows
  185. for (const string_table_row& row: *table)
  186. {
  187. // Skip empty rows and comments
  188. if (row.empty() || row[0].empty() || row[0][0] == '#')
  189. {
  190. continue;
  191. }
  192. load_component(*archetype, *resource_manager, row);
  193. }
  194. return archetype;
  195. }