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

356 lines
11 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 "render/model.hpp"
  22. #include "game/component/atmosphere.hpp"
  23. #include "game/component/behavior.hpp"
  24. #include "game/component/collision.hpp"
  25. #include "game/component/diffuse-reflector.hpp"
  26. #include "game/component/terrain.hpp"
  27. #include "game/component/transform.hpp"
  28. #include "game/component/model.hpp"
  29. #include "game/component/orbit.hpp"
  30. #include "game/component/blackbody.hpp"
  31. #include "game/component/celestial-body.hpp"
  32. #include "entity/archetype.hpp"
  33. #include "entity/ebt.hpp"
  34. #include "physics/orbit/elements.hpp"
  35. #include "resources/json.hpp"
  36. #include <stdexcept>
  37. static bool load_component_atmosphere(entity::archetype& archetype, const json& element)
  38. {
  39. game::component::atmosphere component;
  40. if (element.contains("upper_limit"))
  41. component.upper_limit = element["upper_limit"].get<double>();
  42. if (element.contains("index_of_refraction"))
  43. component.index_of_refraction = element["index_of_refraction"].get<double>();
  44. if (element.contains("rayleigh_concentration"))
  45. component.rayleigh_concentration = element["rayleigh_concentration"].get<double>();
  46. if (element.contains("rayleigh_scale_height"))
  47. component.rayleigh_scale_height = element["rayleigh_scale_height"].get<double>();
  48. if (element.contains("mie_concentration"))
  49. component.mie_concentration = element["mie_concentration"].get<double>();
  50. if (element.contains("mie_scale_height"))
  51. component.mie_scale_height = element["mie_scale_height"].get<double>();
  52. if (element.contains("mie_anisotropy"))
  53. component.mie_anisotropy = element["mie_anisotropy"].get<double>();
  54. if (element.contains("mie_albedo"))
  55. component.mie_albedo = element["mie_albedo"].get<double>();
  56. if (element.contains("ozone_concentration"))
  57. component.ozone_concentration = element["ozone_concentration"].get<double>();
  58. if (element.contains("ozone_lower_limit"))
  59. component.ozone_lower_limit = element["ozone_lower_limit"].get<double>();
  60. if (element.contains("ozone_upper_limit"))
  61. component.ozone_upper_limit = element["ozone_upper_limit"].get<double>();
  62. if (element.contains("ozone_mode"))
  63. component.ozone_mode = element["ozone_mode"].get<double>();
  64. if (element.contains("airglow_illuminance"))
  65. {
  66. const auto& airglow_illuminance = element["airglow_illuminance"];
  67. component.airglow_illuminance.x() = airglow_illuminance[0].get<double>();
  68. component.airglow_illuminance.y() = airglow_illuminance[1].get<double>();
  69. component.airglow_illuminance.z() = airglow_illuminance[2].get<double>();
  70. }
  71. archetype.stamps.push_back
  72. (
  73. [component](entt::handle& handle)
  74. {
  75. handle.emplace_or_replace<decltype(component)>(component);
  76. }
  77. );
  78. return true;
  79. }
  80. static bool load_component_behavior(entity::archetype& archetype, resource_manager& resource_manager, const json& element)
  81. {
  82. game::component::behavior component;
  83. component.behavior_tree = nullptr;
  84. if (element.contains("file"))
  85. {
  86. component.behavior_tree = resource_manager.load<entity::ebt::node>(element["file"].get<std::string>());
  87. }
  88. archetype.stamps.push_back
  89. (
  90. [component](entt::handle& handle)
  91. {
  92. handle.emplace_or_replace<decltype(component)>(component);
  93. }
  94. );
  95. return (component.behavior_tree != nullptr);
  96. }
  97. static bool load_component_blackbody(entity::archetype& archetype, const json& element)
  98. {
  99. game::component::blackbody component;
  100. component.temperature = 0.0;
  101. if (element.contains("temperature"))
  102. component.temperature = element["temperature"].get<double>();
  103. archetype.stamps.push_back
  104. (
  105. [component](entt::handle& handle)
  106. {
  107. handle.emplace_or_replace<decltype(component)>(component);
  108. }
  109. );
  110. return true;
  111. }
  112. static bool load_component_celestial_body(entity::archetype& archetype, const json& element)
  113. {
  114. game::component::celestial_body component;
  115. if (element.contains("radius"))
  116. component.radius = element["radius"].get<double>();
  117. if (element.contains("mass"))
  118. component.mass = element["mass"].get<double>();
  119. if (element.contains("pole_ra"))
  120. {
  121. component.pole_ra.clear();
  122. auto& pole_ra_element = element["pole_ra"];
  123. for (auto it = pole_ra_element.rbegin(); it != pole_ra_element.rend(); ++it)
  124. component.pole_ra.push_back(math::radians(it->get<double>()));
  125. }
  126. if (element.contains("pole_dec"))
  127. {
  128. component.pole_dec.clear();
  129. auto& pole_dec_element = element["pole_dec"];
  130. for (auto it = pole_dec_element.rbegin(); it != pole_dec_element.rend(); ++it)
  131. component.pole_dec.push_back(math::radians(it->get<double>()));
  132. }
  133. if (element.contains("prime_meridian"))
  134. {
  135. component.prime_meridian.clear();
  136. auto& prime_meridian_element = element["prime_meridian"];
  137. for (auto it = prime_meridian_element.rbegin(); it != prime_meridian_element.rend(); ++it)
  138. component.prime_meridian.push_back(math::radians(it->get<double>()));
  139. }
  140. if (element.contains("albedo"))
  141. component.albedo = element["albedo"].get<double>();
  142. archetype.stamps.push_back
  143. (
  144. [component](entt::handle& handle)
  145. {
  146. handle.emplace_or_replace<decltype(component)>(component);
  147. }
  148. );
  149. return true;
  150. }
  151. static bool load_component_collision(entity::archetype& archetype, resource_manager& resource_manager, const json& element)
  152. {
  153. game::component::collision component;
  154. component.mesh = nullptr;
  155. if (element.contains("file"))
  156. {
  157. component.mesh = resource_manager.load<geom::mesh>(element["file"].get<std::string>());
  158. }
  159. archetype.stamps.push_back
  160. (
  161. [component](entt::handle& handle)
  162. {
  163. handle.emplace_or_replace<decltype(component)>(component);
  164. }
  165. );
  166. return (component.mesh != nullptr);
  167. }
  168. static bool load_component_diffuse_reflector(entity::archetype& archetype, const json& element)
  169. {
  170. game::component::diffuse_reflector component;
  171. component.albedo = 0.0;
  172. if (element.contains("albedo"))
  173. component.albedo = element["albedo"].get<double>();
  174. archetype.stamps.push_back
  175. (
  176. [component](entt::handle& handle)
  177. {
  178. handle.emplace_or_replace<decltype(component)>(component);
  179. }
  180. );
  181. return true;
  182. }
  183. static bool load_component_model(entity::archetype& archetype, resource_manager& resource_manager, const json& element)
  184. {
  185. game::component::model component;
  186. component.instance_count = 0;
  187. //component.layers = ~0;
  188. component.layers = 1;
  189. if (element.contains("file"))
  190. {
  191. component.render_model = resource_manager.load<render::model>(element["file"].get<std::string>());
  192. }
  193. archetype.stamps.push_back
  194. (
  195. [component](entt::handle& handle)
  196. {
  197. handle.emplace_or_replace<decltype(component)>(component);
  198. }
  199. );
  200. return true;
  201. }
  202. static bool load_component_orbit(entity::archetype& archetype, const json& element)
  203. {
  204. game::component::orbit component;
  205. component.parent = entt::null;
  206. component.ephemeris_index = -1;
  207. component.scale = 1.0;
  208. component.position = {0, 0, 0};
  209. if (element.contains("ephemeris_index"))
  210. component.ephemeris_index = element["ephemeris_index"].get<int>();
  211. if (element.contains("scale"))
  212. component.scale = element["scale"].get<double>();
  213. archetype.stamps.push_back
  214. (
  215. [component](entt::handle& handle)
  216. {
  217. handle.emplace_or_replace<decltype(component)>(component);
  218. }
  219. );
  220. return true;
  221. }
  222. static bool load_component_transform(entity::archetype& archetype, const json& element)
  223. {
  224. game::component::transform component;
  225. component.local = math::transform<float>::identity;
  226. component.warp = true;
  227. if (element.contains("translation"))
  228. {
  229. auto translation = element["translation"];
  230. component.local.translation.x() = translation[0].get<float>();
  231. component.local.translation.y() = translation[1].get<float>();
  232. component.local.translation.z() = translation[2].get<float>();
  233. }
  234. if (element.contains("rotation"))
  235. {
  236. auto translation = element["rotation"];
  237. component.local.rotation.w = translation[0].get<float>();
  238. component.local.rotation.x = translation[1].get<float>();
  239. component.local.rotation.y = translation[2].get<float>();
  240. component.local.rotation.z = translation[3].get<float>();
  241. }
  242. if (element.contains("scale"))
  243. {
  244. auto translation = element["scale"];
  245. component.local.scale.x() = translation[0].get<float>();
  246. component.local.scale.y() = translation[1].get<float>();
  247. component.local.scale.z() = translation[2].get<float>();
  248. }
  249. component.world = component.local;
  250. archetype.stamps.push_back
  251. (
  252. [component](entt::handle& handle)
  253. {
  254. handle.emplace_or_replace<decltype(component)>(component);
  255. }
  256. );
  257. return true;
  258. }
  259. static bool load_component(entity::archetype& archetype, resource_manager& resource_manager, json::const_iterator element)
  260. {
  261. if (element.key() == "atmosphere")
  262. return load_component_atmosphere(archetype, element.value());
  263. if (element.key() == "behavior")
  264. return load_component_behavior(archetype, resource_manager, element.value());
  265. if (element.key() == "blackbody")
  266. return load_component_blackbody(archetype, element.value());
  267. if (element.key() == "celestial_body")
  268. return load_component_celestial_body(archetype, element.value());
  269. if (element.key() == "collision")
  270. return load_component_collision(archetype, resource_manager, element.value());
  271. if (element.key() == "diffuse_reflector")
  272. return load_component_diffuse_reflector(archetype, element.value());
  273. if (element.key() == "model")
  274. return load_component_model(archetype, resource_manager, element.value());
  275. if (element.key() == "orbit")
  276. return load_component_orbit(archetype, element.value());
  277. if (element.key() == "transform")
  278. return load_component_transform(archetype, element.value());
  279. //throw std::runtime_error("Unknown component type \"" + element.key() + "\"");
  280. return false;
  281. }
  282. template <>
  283. entity::archetype* resource_loader<entity::archetype>::load(resource_manager* resource_manager, PHYSFS_File* file, const std::filesystem::path& path)
  284. {
  285. // Allocate archetype
  286. entity::archetype* archetype = new entity::archetype();
  287. // Read file into buffer
  288. std::size_t size = static_cast<int>(PHYSFS_fileLength(file));
  289. std::string buffer;
  290. buffer.resize(size);
  291. PHYSFS_readBytes(file, &buffer[0], size);
  292. // Parse JSON data from file buffer
  293. json data = nlohmann::json::parse(buffer, nullptr, true, true);
  294. // Load components from table rows
  295. for (json::const_iterator element = data.cbegin(); element != data.cend(); ++element)
  296. {
  297. if (!load_component(*archetype, *resource_manager, element))
  298. {
  299. throw std::runtime_error("Failed to load component \"" + element.key() + "\"");
  300. }
  301. }
  302. return archetype;
  303. }