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

401 lines
17 KiB

3 years ago
  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 "animation/ease.hpp"
  20. #include "animation/screen-transition.hpp"
  21. #include "configuration.hpp"
  22. #include "debug/logger.hpp"
  23. #include "entity/archetype.hpp"
  24. #include "entity/components/cavity.hpp"
  25. #include "entity/components/copy-transform.hpp"
  26. #include "entity/components/copy-translation.hpp"
  27. #include "entity/components/model.hpp"
  28. #include "entity/components/genome.hpp"
  29. #include "entity/components/snap.hpp"
  30. #include "entity/components/terrain.hpp"
  31. #include "entity/components/tool.hpp"
  32. #include "entity/components/transform.hpp"
  33. #include "entity/components/camera-follow.hpp"
  34. #include "entity/components/orbit.hpp"
  35. #include "entity/components/blackbody.hpp"
  36. #include "entity/components/celestial-body.hpp"
  37. #include "entity/components/atmosphere.hpp"
  38. #include "entity/components/light.hpp"
  39. #include "entity/components/observer.hpp"
  40. #include "entity/commands.hpp"
  41. #include "game/game-context.hpp"
  42. #include "game/states/game-states.hpp"
  43. #include "math/math.hpp"
  44. #include "nest.hpp"
  45. #include "renderer/material.hpp"
  46. #include "gl/texture-2d.hpp"
  47. #include "gl/texture-filter.hpp"
  48. #include "gl/texture-wrapping.hpp"
  49. #include "renderer/model.hpp"
  50. #include "renderer/passes/sky-pass.hpp"
  51. #include "renderer/passes/shadow-map-pass.hpp"
  52. #include "resources/resource-manager.hpp"
  53. #include "scene/model-instance.hpp"
  54. #include "scene/collection.hpp"
  55. #include "scene/camera.hpp"
  56. #include "scene/ambient-light.hpp"
  57. #include "scene/directional-light.hpp"
  58. #include "entity/systems/control.hpp"
  59. #include "entity/systems/camera.hpp"
  60. #include "entity/systems/render.hpp"
  61. #include "entity/systems/tool.hpp"
  62. #include "entity/systems/orbit.hpp"
  63. #include "entity/systems/astronomy.hpp"
  64. #include "game/biome.hpp"
  65. #include "utility/fundamental-types.hpp"
  66. #include "utility/bit-math.hpp"
  67. #include "genetics/genetics.hpp"
  68. #include "math/random.hpp"
  69. #include <iostream>
  70. #include <bitset>
  71. #include <ctime>
  72. void play_state_enter(game_context* ctx)
  73. {
  74. debug::logger* logger = ctx->logger;
  75. logger->push_task("Entering play state");
  76. resource_manager* resource_manager = ctx->resource_manager;
  77. entt::registry& entity_registry = *ctx->entity_registry;
  78. // Load biome
  79. if (ctx->option_biome.has_value())
  80. {
  81. ctx->biome = resource_manager->load<biome>(ctx->option_biome.value() + ".bio");
  82. }
  83. else
  84. {
  85. ctx->biome = resource_manager->load<biome>("forest.bio");
  86. }
  87. // Apply biome parameters to scene
  88. sky_pass* sky_pass = ctx->overworld_sky_pass;
  89. sky_pass->set_enabled(true);
  90. sky_pass->set_sky_model(ctx->resource_manager->load<model>("sky-dome.mdl"));
  91. sky_pass->set_moon_model(ctx->resource_manager->load<model>("moon.mdl"));
  92. // Create sun
  93. auto sun_entity = entity_registry.create();
  94. {
  95. entity::component::celestial_body body;
  96. body.radius = 6.957e+8;
  97. body.axial_tilt = math::radians(0.0);
  98. body.axial_rotation = math::radians(0.0);
  99. body.angular_frequency = math::radians(0.0);
  100. entity::component::orbit orbit;
  101. orbit.elements.a = 0.0;
  102. orbit.elements.e = 0.0;
  103. orbit.elements.i = math::radians(0.0);
  104. orbit.elements.raan = math::radians(0.0);
  105. orbit.elements.w = math::radians(0.0);
  106. orbit.elements.ta = math::radians(0.0);
  107. entity::component::blackbody blackbody;
  108. blackbody.temperature = 5778.0;
  109. entity::component::transform transform;
  110. transform.local = math::identity_transform<float>;
  111. transform.warp = true;
  112. entity_registry.assign<entity::component::celestial_body>(sun_entity, body);
  113. entity_registry.assign<entity::component::orbit>(sun_entity, orbit);
  114. entity_registry.assign<entity::component::blackbody>(sun_entity, blackbody);
  115. entity_registry.assign<entity::component::transform>(sun_entity, transform);
  116. }
  117. // Create Earth
  118. auto earth_entity = entity_registry.create();
  119. {
  120. entity::component::celestial_body body;
  121. body.radius = 6.3781e6;
  122. body.axial_tilt = math::radians(23.4393);
  123. body.axial_rotation = math::radians(280.46061837504);
  124. body.angular_frequency = math::radians(360.9856122880876128);
  125. entity::component::orbit orbit;
  126. orbit.elements.a = 1.496e+11;
  127. orbit.elements.e = 0.01671123;
  128. orbit.elements.i = math::radians(-0.00001531);
  129. orbit.elements.raan = math::radians(0.0);
  130. const double longitude_periapsis = math::radians(102.93768193);
  131. orbit.elements.w = longitude_periapsis - orbit.elements.raan;
  132. orbit.elements.ta = math::radians(100.46457166) - longitude_periapsis;
  133. entity::component::terrain terrain;
  134. terrain.elevation = [](double, double) -> double
  135. {
  136. //return math::random<double>(0.0, 1.0);
  137. return 0.0;
  138. };
  139. terrain.max_lod = 18;
  140. terrain.patch_material = resource_manager->load<material>("desert-terrain.mtl");
  141. entity::component::atmosphere atmosphere;
  142. atmosphere.exosphere_altitude = 65e3;
  143. atmosphere.index_of_refraction = 1.000293;
  144. atmosphere.rayleigh_density = 2.545e25;
  145. atmosphere.rayleigh_scale_height = 8000.0;
  146. atmosphere.mie_density = 14.8875;
  147. atmosphere.mie_scale_height = 1200.0;
  148. atmosphere.mie_anisotropy = 0.8;
  149. entity::component::transform transform;
  150. transform.local = math::identity_transform<float>;
  151. transform.warp = true;
  152. entity_registry.assign<entity::component::celestial_body>(earth_entity, body);
  153. entity_registry.assign<entity::component::orbit>(earth_entity, orbit);
  154. entity_registry.assign<entity::component::atmosphere>(earth_entity, atmosphere);
  155. entity_registry.assign<entity::component::terrain>(earth_entity, terrain);
  156. entity_registry.assign<entity::component::transform>(earth_entity, transform);
  157. }
  158. // Create observer
  159. auto observer_eid = entity_registry.create();
  160. {
  161. entity::component::observer observer;
  162. observer.reference_body_eid = earth_entity;
  163. observer.elevation = 0.0;
  164. observer.latitude = 0.0;
  165. observer.longitude = 0.0;
  166. observer.camera = ctx->overworld_camera;
  167. entity_registry.assign<entity::component::observer>(observer_eid, observer);
  168. }
  169. scene::ambient_light* ambient = new scene::ambient_light();
  170. ambient->set_color({1, 1, 1});
  171. ambient->set_intensity(0.0f);
  172. ambient->update_tweens();
  173. ctx->overworld_scene->add_object(ambient);
  174. scene::directional_light* sun = new scene::directional_light();
  175. //sun->set_intensity(1000.0f);
  176. //sun->set_light_texture(resource_manager->load<gl::texture_2d>("forest-gobo.tex"));
  177. //sun->set_light_texture_scale({2000, 2000});
  178. //sun->set_light_texture_opacity(0.925f);
  179. //sun->look_at({2, 1, 0}, {0, 0, 0}, {0, 0, 1});
  180. //sun->update_tweens();
  181. ctx->overworld_scene->add_object(sun);
  182. ctx->overworld_shadow_map_pass->set_light(sun);
  183. // Set universal time
  184. const double universal_time = 0.0;
  185. ctx->astronomy_system->set_universal_time(universal_time);
  186. ctx->orbit_system->set_universal_time(universal_time);
  187. // Set astronomy system observation parameters
  188. ctx->astronomy_system->set_reference_body(earth_entity);
  189. ctx->astronomy_system->set_observer_location(double3{0.0, math::radians(0.0f), math::radians(0.0f)});
  190. ctx->astronomy_system->set_sun_light(sun);
  191. ctx->astronomy_system->set_sky_pass(ctx->overworld_sky_pass);
  192. // Load entity archetypes
  193. entity::archetype* ant_hill_archetype = resource_manager->load<entity::archetype>("ant-hill.ent");
  194. entity::archetype* nest_archetype = resource_manager->load<entity::archetype>("harvester-nest.ent");
  195. entity::archetype* redwood_archetype = resource_manager->load<entity::archetype>("redwood.ent");
  196. entity::archetype* forceps_archetype = resource_manager->load<entity::archetype>("forceps.ent");
  197. entity::archetype* lens_archetype = resource_manager->load<entity::archetype>("lens.ent");
  198. entity::archetype* brush_archetype = resource_manager->load<entity::archetype>("brush.ent");
  199. entity::archetype* marker_archetype = resource_manager->load<entity::archetype>("marker.ent");
  200. entity::archetype* container_archetype = resource_manager->load<entity::archetype>("container.ent");
  201. entity::archetype* twig_archetype = resource_manager->load<entity::archetype>("twig.ent");
  202. entity::archetype* larva_archetype = resource_manager->load<entity::archetype>("ant-larva.ent");
  203. entity::archetype* flashlight_archetype = resource_manager->load<entity::archetype>("flashlight.ent");
  204. entity::archetype* flashlight_light_cone_archetype = resource_manager->load<entity::archetype>("flashlight-light-cone.ent");
  205. entity::archetype* lens_light_cone_archetype = resource_manager->load<entity::archetype>("lens-light-cone.ent");
  206. entity::archetype* cube_archetype = resource_manager->load<entity::archetype>("unit-cube.ent");
  207. entity::archetype* color_checker_archetype = resource_manager->load<entity::archetype>("color-checker.ent");
  208. // Create tools
  209. /*
  210. forceps_archetype->assign(entity_registry, ctx->forceps_entity);
  211. lens_archetype->assign(entity_registry, ctx->lens_entity);
  212. brush_archetype->assign(entity_registry, ctx->brush_entity);
  213. marker_archetype->assign(entity_registry, ctx->marker_entity);
  214. container_archetype->assign(entity_registry, ctx->container_entity);
  215. twig_archetype->assign(entity_registry, ctx->twig_entity);
  216. */
  217. // Create flashlight and light cone, set light cone parent to flashlight, and move both to underworld scene
  218. /*
  219. flashlight_archetype->assign(entity_registry, ctx->flashlight_entity);
  220. auto flashlight_light_cone = flashlight_light_cone_archetype->create(entity_registry);
  221. entity::command::parent(entity_registry, flashlight_light_cone, ctx->flashlight_entity);
  222. entity::command::assign_render_layers(entity_registry, ctx->flashlight_entity, 2);
  223. entity::command::assign_render_layers(entity_registry, flashlight_light_cone, 2);
  224. */
  225. // Make lens tool's model instance unculled, so its shadow is always visible.
  226. //scene::model_instance* lens_model_instance = ctx->render_system->get_model_instance(ctx->lens_entity);
  227. //if (lens_model_instance)
  228. //{
  229. //lens_model_instance->set_culling_mask(&ctx->no_cull);
  230. //}
  231. // Create lens light cone and set its parent to lens
  232. //auto lens_light_cone = lens_light_cone_archetype->create(entity_registry);
  233. //entity::command::bind_transform(entity_registry, lens_light_cone, ctx->lens_entity);
  234. //entity::command::parent(entity_registry, lens_light_cone, ctx->lens_entity);
  235. // Hide inactive tools
  236. /*
  237. entity::command::assign_render_layers(entity_registry, ctx->forceps_entity, 0);
  238. entity::command::assign_render_layers(entity_registry, ctx->brush_entity, 0);
  239. entity::command::assign_render_layers(entity_registry, ctx->lens_entity, 0);
  240. entity::command::assign_render_layers(entity_registry, ctx->marker_entity, 0);
  241. entity::command::assign_render_layers(entity_registry, ctx->container_entity, 0);
  242. entity::command::assign_render_layers(entity_registry, ctx->twig_entity, 0);
  243. */
  244. // Activate brush tool
  245. //ctx->tool_system->set_active_tool(ctx->brush_entity);
  246. // Create ant-hill
  247. //auto ant_hill_entity = ant_hill_archetype->create(entity_registry);
  248. //entity::command::place(entity_registry, ant_hill_entity, earth_entity, 0.0, 0.0, 0.0);
  249. // Create color checker
  250. /*
  251. auto color_checker = color_checker_archetype->create(entity_registry);
  252. entity::command::place(entity_registry, color_checker, {-10, -10});
  253. auto& cc_transform = entity_registry.get<entity::component::transform>(color_checker);
  254. cc_transform.local.scale *= 10.0f;
  255. cc_transform.local.rotation = math::angle_axis(math::radians(-90.0f), {1, 0, 0});
  256. */
  257. // Setup camera focal point
  258. entity::component::transform focal_point_transform;
  259. focal_point_transform.local = math::identity_transform<float>;
  260. //focal_point_transform.local.translation = {0, 6.3781e6, 0};
  261. focal_point_transform.warp = true;
  262. entity::component::camera_follow focal_point_follow;
  263. entity::component::snap focal_point_snap;
  264. focal_point_snap.ray = {float3{0, 10000, 0}, float3{0, -1, 0}};
  265. focal_point_snap.warp = false;
  266. focal_point_snap.relative = true;
  267. focal_point_snap.autoremove = false;
  268. entity_registry.assign_or_replace<entity::component::transform>(ctx->focal_point_entity, focal_point_transform);
  269. entity_registry.assign_or_replace<entity::component::camera_follow>(ctx->focal_point_entity, focal_point_follow);
  270. //entity_registry.assign_or_replace<entity::component::snap>(ctx->focal_point_entity, focal_point_snap);
  271. // Setup camera
  272. ctx->overworld_camera->look_at({0, 0, 1}, {0, 0, 0}, {0, 1, 0});
  273. ctx->overworld_camera->set_exposure(-14.5f);
  274. ctx->camera_system->set_camera(ctx->overworld_camera);
  275. ctx->overworld_scene->update_tweens();
  276. // Allocate a nest
  277. nest* nest = new ::nest();
  278. // Setup initial nest parameters
  279. float tunnel_radius = 1.15f;
  280. nest->set_tunnel_radius(tunnel_radius);
  281. nest::shaft* central_shaft = nest->get_central_shaft();
  282. central_shaft->chirality = 1.0f;
  283. central_shaft->rotation = math::radians(0.0f);
  284. central_shaft->depth = {0.0f, 200.0f};
  285. central_shaft->radius = {15.0f, 15.0f};
  286. central_shaft->pitch = {40.0f, 40.0f};
  287. central_shaft->translation = {{{0.0f, 0.0f}, {0.0f, 0.0f}}};
  288. central_shaft->current_depth = 0.0f;
  289. for (std::size_t i = 0; i < 4; ++i)
  290. {
  291. nest::chamber chamber;
  292. chamber.shaft = central_shaft;
  293. chamber.depth = (i + 1) * 50.0f;
  294. chamber.rotation = math::radians(0.0f);
  295. chamber.inner_radius = 4.0f;
  296. chamber.outer_radius = 10.0f;
  297. central_shaft->chambers.push_back(chamber);
  298. }
  299. // Dig nest shafts
  300. float shift = 0.1f;
  301. for (int i = 0; i < 800; ++i)
  302. {
  303. entity::component::cavity cavity;
  304. cavity.position = nest->extend_shaft(*nest->get_central_shaft());
  305. cavity.position += float3{math::random(-shift, shift), math::random(-shift, shift), math::random(-shift, shift)};
  306. cavity.radius = tunnel_radius * math::random(1.0f, 1.1f);
  307. entity_registry.assign<entity::component::cavity>(entity_registry.create(), cavity);
  308. }
  309. // Dig nest chambers
  310. /*
  311. for (int i = 0; i < central_shaft->chambers.size(); ++i)
  312. {
  313. for (int j = 0; j < 150; ++j)
  314. {
  315. entity::component::cavity cavity;
  316. cavity.position = nest->expand_chamber(central_shaft->chambers[i]);
  317. cavity.position += float3{math::random(-shift, shift), math::random(-shift, shift), math::random(-shift, shift)};
  318. cavity.radius = tunnel_radius * math::random(1.0f, 1.1f);
  319. entity_registry.assign<entity::component::cavity>(entity_registry.create(), cavity);
  320. }
  321. }
  322. */
  323. // Place larva in chamber
  324. {
  325. auto larva_eid = larva_archetype->create(entity_registry);
  326. entity::command::assign_render_layers(entity_registry, larva_eid, 1);
  327. entity::command::warp_to(entity_registry, larva_eid, {50, 0.1935f, 0});
  328. //auto& transform = entity_registry.get<entity::component::transform>(larva_entity);
  329. //transform.transform = math::identity_transform<float>;
  330. //transform.transform.translation = nest->get_shaft_position(*central_shaft, central_shaft->depth[1]);
  331. //transform.transform.translation.y -= 1.0f;
  332. // Construct larva genome
  333. entity::component::genome genome;
  334. genome.ploidy = 2;
  335. const std::string antp = "ATGACCATGAGCACCAACAACTGCGAAAGCATGACCAGCTATTTTACCAACAGCTATATGGGCGCGGATATGCATCATGGCCATTATCCGGGCAACGGCGTGACCGATCTGGATGCGCAGCAGATGCATCATTATAGCCAGAACGCGAACCATCAGGGCAACATGCCGTATCCGCGCTTTCCGCCGTATGATCGCATGCCGTATTATAACGGCCAGGGCATGGATCAGCAGCAGCAGCATCAGGTGTATAGCCGCCCGGATAGCCCGAGCAGCCAGGTGGGCGGCGTGATGCCGCAGGCGCAGACCAACGGCCAGCTGGGCGTGCCGCAGCAGCAGCAGCAGCAGCAGCAGCAGCCGAGCCAGAACCAGCAGCAGCAGCAGGCGCAGCAGGCGCCGCAGCAGCTGCAGCAGCAGCTGCCGCAGGTGACCCAGCAGGTGACCCATCCGCAGCAGCAGCAGCAGCAGCCGGTGGTGTATGCGAGCTGCAAACTGCAGGCGGCGGTGGGCGGCCTGGGCATGGTGCCGGAAGGCGGCAGCCCGCCGCTGGTGGATCAGATGAGCGGCCATCATATGAACGCGCAGATGACCCTGCCGCATCATATGGGCCATCCGCAGGCGCAGGTGCATCAGAACCATCATAACATGGGCATGTATCAGCAGCAGAGCGGCGTGCCGCCGGTGGGCGCGCCGCCGCAGGGCATGATGCATCAGGGCCAGGGCCCGCCGCAGATGCATCAGGGCCATCCGGGCCAGCATACCCCGCCGAGCCAGAACCCGAACAGCCAGAGCAGCGGCATGCCGAGCCCGCTGTATCCGTGGATGCGCAGCCAGTTTGAACGCAAACGCGGCCGCCAGACCTATACCCGCTATCAGACCCTGGAACTGGAAAAAGAATTTCATTTTAACCGCTATCTGACCCGCCGCCGCCGCATTGAAATTGCGCATGCGCTGTGCCTGACCGAACGCCAGATTAAAATTTGGTTTCAGAACCGCCGCATGAAATGGAAAAAAGAAAACAAAACCAAAGGCGAACCGGGCAGCGGCGGCGAAGGCGATGAAATTACCCCGCCGAACAGCCCGCAGTAG";
  336. genome.chromosomes.push_back(antp);
  337. entity_registry.assign<entity::component::genome>(larva_eid, genome);
  338. }
  339. entity::system::control* control_system = ctx->control_system;
  340. control_system->update(0.0, 0.0);
  341. control_system->set_nest(nest);
  342. // Start fade in
  343. ctx->fade_transition->transition(1.0f, true, ease<float>::in_quad);
  344. logger->pop_task(EXIT_SUCCESS);
  345. std::string biome_name = (*ctx->strings)[ctx->biome->name];
  346. logger->log("Entered biome \"" + biome_name + "\"");
  347. }
  348. void play_state_exit(game_context* ctx)
  349. {
  350. debug::logger* logger = ctx->logger;
  351. logger->push_task("Exiting play state");
  352. logger->pop_task(EXIT_SUCCESS);
  353. }