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

433 lines
17 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
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 "ecs/archetype.hpp"
  24. #include "ecs/components/cavity-component.hpp"
  25. #include "ecs/components/copy-transform-component.hpp"
  26. #include "ecs/components/copy-translation-component.hpp"
  27. #include "ecs/components/model-component.hpp"
  28. #include "ecs/components/snap-component.hpp"
  29. #include "ecs/components/terrain-component.hpp"
  30. #include "ecs/components/tool-component.hpp"
  31. #include "ecs/components/transform-component.hpp"
  32. #include "ecs/components/camera-follow-component.hpp"
  33. #include "ecs/components/orbit-component.hpp"
  34. #include "ecs/components/celestial-body-component.hpp"
  35. #include "ecs/components/light-component.hpp"
  36. #include "ecs/commands.hpp"
  37. #include "game/game-context.hpp"
  38. #include "game/states/game-states.hpp"
  39. #include "math/math.hpp"
  40. #include "nest.hpp"
  41. #include "renderer/material.hpp"
  42. #include "gl/texture-2d.hpp"
  43. #include "gl/texture-filter.hpp"
  44. #include "gl/texture-wrapping.hpp"
  45. #include "renderer/model.hpp"
  46. #include "renderer/passes/sky-pass.hpp"
  47. #include "renderer/passes/shadow-map-pass.hpp"
  48. #include "resources/resource-manager.hpp"
  49. #include "scene/model-instance.hpp"
  50. #include "scene/collection.hpp"
  51. #include "scene/camera.hpp"
  52. #include "scene/ambient-light.hpp"
  53. #include "scene/directional-light.hpp"
  54. #include "ecs/systems/control-system.hpp"
  55. #include "ecs/systems/camera-system.hpp"
  56. #include "ecs/systems/render-system.hpp"
  57. #include "ecs/systems/tool-system.hpp"
  58. #include "ecs/systems/weather-system.hpp"
  59. #include "ecs/systems/solar-system.hpp"
  60. #include "ecs/systems/astronomy-system.hpp"
  61. #include "game/biome.hpp"
  62. #include "utility/fundamental-types.hpp"
  63. #include "utility/gamma.hpp"
  64. #include "astro/blackbody.hpp"
  65. #include "utility/bit-math.hpp"
  66. #include "genetics/genetics.hpp"
  67. #include <iostream>
  68. #include <bitset>
  69. #include <ctime>
  70. void play_state_enter(game_context* ctx)
  71. {
  72. debug::logger* logger = ctx->logger;
  73. logger->push_task("Entering play state");
  74. resource_manager* resource_manager = ctx->resource_manager;
  75. entt::registry& ecs_registry = *ctx->ecs_registry;
  76. // Load biome
  77. if (ctx->option_biome.has_value())
  78. {
  79. ctx->biome = resource_manager->load<biome>(ctx->option_biome.value() + ".bio");
  80. }
  81. else
  82. {
  83. ctx->biome = resource_manager->load<biome>("forest.bio");
  84. }
  85. // Apply biome parameters to scene
  86. sky_pass* sky_pass = ctx->overworld_sky_pass;
  87. sky_pass->set_enabled(true);
  88. sky_pass->set_sky_model(ctx->resource_manager->load<model>("sky-dome.mdl"));
  89. sky_pass->set_moon_model(ctx->resource_manager->load<model>("moon.mdl"));
  90. sky_pass->set_horizon_color({0.1f, 0.1f, 0.1f});
  91. sky_pass->set_zenith_color({0.0f, 0.0f, 0.0f});
  92. sky_pass->set_time_of_day(0.0f);
  93. sky_pass->set_julian_day(0.0f);
  94. sky_pass->set_observer_location(4.26352e-5, ctx->biome->location[0], ctx->biome->location[1]);
  95. sky_pass->set_moon_angular_radius(math::radians(1.0f));
  96. sky_pass->set_sun_angular_radius(math::radians(1.0f));
  97. sky_pass->set_sun_coordinates({0, 1, 0}, {0, 3.1415f / 2.0f});
  98. sky_pass->set_moon_coordinates({1, 0, 0}, {0, 0});
  99. sky_pass->set_moon_rotation(math::identity_quaternion<float>);
  100. scene::ambient_light* ambient = new scene::ambient_light();
  101. ambient->set_color({1, 1, 1});
  102. ambient->set_intensity(10.0f);
  103. ambient->update_tweens();
  104. ctx->overworld_scene->add_object(ambient);
  105. // Create sun
  106. {
  107. ecs::celestial_body_component sun_body;
  108. sun_body.orbital_elements.a = 1.0;
  109. sun_body.orbital_elements.ec = 0.016709;
  110. sun_body.orbital_elements.w = math::radians(282.9404);
  111. sun_body.orbital_elements.ma = math::radians(356.0470);
  112. sun_body.orbital_elements.i = 0.0;
  113. sun_body.orbital_elements.om = 0.0;
  114. sun_body.orbital_rate.a = 0.0;
  115. sun_body.orbital_rate.ec = -1.151e-9;
  116. sun_body.orbital_rate.w = math::radians(4.70935e-5);
  117. sun_body.orbital_rate.ma = math::radians(0.9856002585);
  118. sun_body.orbital_rate.i = 0.0;
  119. sun_body.orbital_rate.om = 0.0;
  120. ecs::transform_component sun_transform;
  121. sun_transform.local = math::identity_transform<float>;
  122. sun_transform.warp = true;
  123. auto sun_entity = ecs_registry.create();
  124. ecs_registry.assign<ecs::transform_component>(sun_entity, sun_transform);
  125. ecs_registry.assign<ecs::celestial_body_component>(sun_entity, sun_body);
  126. //ctx->astronomy_system->set_sun(sun_entity);
  127. }
  128. scene::directional_light* sun = new scene::directional_light();
  129. float3 sun_color = math::type_cast<float>(astro::blackbody(6000.0)); // NOTE: this is linear sRGB, should be ACEScg
  130. sun->set_color(sun_color);
  131. sun->set_intensity(1000.0f);
  132. sun->set_light_texture(resource_manager->load<gl::texture_2d>("forest-gobo.tex"));
  133. sun->set_light_texture_scale({2000, 2000});
  134. sun->set_light_texture_opacity(0.925f);
  135. sun->look_at({2, 1, 0}, {0, 0, 0}, {0, 0, 1});
  136. sun->update_tweens();
  137. ctx->overworld_scene->add_object(sun);
  138. ctx->overworld_shadow_map_pass->set_light(sun);
  139. ctx->weather_system->set_universal_time(0.0);
  140. ctx->solar_system->set_universal_time(0.0);
  141. ctx->astronomy_system->set_observer_location(double3{4.26352e-5, ctx->biome->location[0], ctx->biome->location[1]});
  142. ctx->astronomy_system->set_universal_time(0.0);
  143. ctx->astronomy_system->set_obliquity(math::radians(23.4393));
  144. ctx->astronomy_system->set_axial_rotation_at_epoch(math::radians(280.4606));
  145. ctx->astronomy_system->set_axial_rotation_speed(math::radians(360.9856));
  146. ctx->astronomy_system->set_sun_light(sun);
  147. // Load entity archetypes
  148. ecs::archetype* ant_hill_archetype = resource_manager->load<ecs::archetype>("ant-hill.ent");
  149. ecs::archetype* nest_archetype = resource_manager->load<ecs::archetype>("harvester-nest.ent");
  150. ecs::archetype* redwood_archetype = resource_manager->load<ecs::archetype>("redwood.ent");
  151. ecs::archetype* forceps_archetype = resource_manager->load<ecs::archetype>("forceps.ent");
  152. ecs::archetype* lens_archetype = resource_manager->load<ecs::archetype>("lens.ent");
  153. ecs::archetype* brush_archetype = resource_manager->load<ecs::archetype>("brush.ent");
  154. ecs::archetype* marker_archetype = resource_manager->load<ecs::archetype>("marker.ent");
  155. ecs::archetype* container_archetype = resource_manager->load<ecs::archetype>("container.ent");
  156. ecs::archetype* twig_archetype = resource_manager->load<ecs::archetype>("twig.ent");
  157. ecs::archetype* larva_archetype = resource_manager->load<ecs::archetype>("ant-larva.ent");
  158. ecs::archetype* flashlight_archetype = resource_manager->load<ecs::archetype>("flashlight.ent");
  159. ecs::archetype* flashlight_light_cone_archetype = resource_manager->load<ecs::archetype>("flashlight-light-cone.ent");
  160. ecs::archetype* lens_light_cone_archetype = resource_manager->load<ecs::archetype>("lens-light-cone.ent");
  161. ecs::archetype* cube_archetype = resource_manager->load<ecs::archetype>("unit-cube.ent");
  162. ecs::archetype* color_checker_archetype = resource_manager->load<ecs::archetype>("color-checker.ent");
  163. // Create tools
  164. forceps_archetype->assign(ecs_registry, ctx->forceps_entity);
  165. lens_archetype->assign(ecs_registry, ctx->lens_entity);
  166. brush_archetype->assign(ecs_registry, ctx->brush_entity);
  167. marker_archetype->assign(ecs_registry, ctx->marker_entity);
  168. container_archetype->assign(ecs_registry, ctx->container_entity);
  169. twig_archetype->assign(ecs_registry, ctx->twig_entity);
  170. // Create flashlight and light cone, set light cone parent to flashlight, and move both to underworld scene
  171. flashlight_archetype->assign(ecs_registry, ctx->flashlight_entity);
  172. auto flashlight_light_cone = flashlight_light_cone_archetype->create(ecs_registry);
  173. ecs::command::parent(ecs_registry, flashlight_light_cone, ctx->flashlight_entity);
  174. ecs::command::assign_render_layers(ecs_registry, ctx->flashlight_entity, 2);
  175. ecs::command::assign_render_layers(ecs_registry, flashlight_light_cone, 2);
  176. // Make lens tool's model instance unculled, so its shadow is always visible.
  177. scene::model_instance* lens_model_instance = ctx->render_system->get_model_instance(ctx->lens_entity);
  178. if (lens_model_instance)
  179. {
  180. //lens_model_instance->set_culling_mask(&ctx->no_cull);
  181. }
  182. // Create lens light cone and set its parent to lens
  183. auto lens_light_cone = lens_light_cone_archetype->create(ecs_registry);
  184. ecs::command::bind_transform(ecs_registry, lens_light_cone, ctx->lens_entity);
  185. ecs::command::parent(ecs_registry, lens_light_cone, ctx->lens_entity);
  186. // Hide inactive tools
  187. ecs::command::assign_render_layers(ecs_registry, ctx->forceps_entity, 0);
  188. ecs::command::assign_render_layers(ecs_registry, ctx->brush_entity, 0);
  189. ecs::command::assign_render_layers(ecs_registry, ctx->lens_entity, 0);
  190. ecs::command::assign_render_layers(ecs_registry, ctx->marker_entity, 0);
  191. ecs::command::assign_render_layers(ecs_registry, ctx->container_entity, 0);
  192. ecs::command::assign_render_layers(ecs_registry, ctx->twig_entity, 0);
  193. // Activate brush tool
  194. ctx->tool_system->set_active_tool(ctx->brush_entity);
  195. // Create ant-hill
  196. auto ant_hill_entity = ant_hill_archetype->create(ecs_registry);
  197. ecs::command::place(ecs_registry, ant_hill_entity, {0, 0});
  198. // Creat nest
  199. auto nest_entity = nest_archetype->create(ecs_registry);
  200. // Create terrain
  201. int terrain_radius = 6;
  202. for (int x = -terrain_radius; x <= terrain_radius; ++x)
  203. {
  204. for (int z = -terrain_radius; z <= terrain_radius; ++z)
  205. {
  206. ecs::terrain_component terrain_component;
  207. terrain_component.subdivisions = TERRAIN_PATCH_RESOLUTION;
  208. terrain_component.x = x;
  209. terrain_component.z = z;
  210. auto terrain_entity = ecs_registry.create();
  211. ecs_registry.assign<ecs::terrain_component>(terrain_entity, terrain_component);
  212. }
  213. }
  214. // Create trees
  215. for (int i = 0; i < 30; ++i)
  216. {
  217. auto redwood = redwood_archetype->create(ecs_registry);
  218. auto& transform = ecs_registry.get<ecs::transform_component>(redwood);
  219. float zone = 500.0f;
  220. ecs::command::place(ecs_registry, redwood, {math::random(-zone, zone), math::random(-zone, zone)});
  221. }
  222. // Create unit cube
  223. auto cube = cube_archetype->create(ecs_registry);
  224. ecs::command::place(ecs_registry, cube, {10, 10});
  225. // Create color checker
  226. auto color_checker = color_checker_archetype->create(ecs_registry);
  227. ecs::command::place(ecs_registry, color_checker, {-10, -10});
  228. auto& cc_transform = ecs_registry.get<ecs::transform_component>(color_checker);
  229. cc_transform.local.scale *= 10.0f;
  230. cc_transform.local.rotation = math::angle_axis(math::radians(-90.0f), {1, 0, 0});
  231. // Setup camera focal point
  232. ecs::transform_component focal_point_transform;
  233. focal_point_transform.local = math::identity_transform<float>;
  234. focal_point_transform.warp = true;
  235. ecs::camera_follow_component focal_point_follow;
  236. ecs::snap_component focal_point_snap;
  237. focal_point_snap.ray = {float3{0, 10000, 0}, float3{0, -1, 0}};
  238. focal_point_snap.warp = false;
  239. focal_point_snap.relative = true;
  240. focal_point_snap.autoremove = false;
  241. ecs_registry.assign_or_replace<ecs::transform_component>(ctx->focal_point_entity, focal_point_transform);
  242. ecs_registry.assign_or_replace<ecs::camera_follow_component>(ctx->focal_point_entity, focal_point_follow);
  243. ecs_registry.assign_or_replace<ecs::snap_component>(ctx->focal_point_entity, focal_point_snap);
  244. // Setup camera
  245. ctx->overworld_camera->look_at({0, 0, 1}, {0, 0, 0}, {0, 1, 0});
  246. ctx->camera_system->set_camera(ctx->overworld_camera);
  247. ctx->overworld_scene->update_tweens();
  248. // Allocate a nest
  249. nest* nest = new ::nest();
  250. // Setup initial nest parameters
  251. float tunnel_radius = 1.15f;
  252. nest->set_tunnel_radius(tunnel_radius);
  253. nest::shaft* central_shaft = nest->get_central_shaft();
  254. central_shaft->chirality = 1.0f;
  255. central_shaft->rotation = math::radians(0.0f);
  256. central_shaft->depth = {0.0f, 200.0f};
  257. central_shaft->radius = {15.0f, 15.0f};
  258. central_shaft->pitch = {40.0f, 40.0f};
  259. central_shaft->translation = {{{0.0f, 0.0f}, {0.0f, 0.0f}}};
  260. central_shaft->current_depth = 0.0f;
  261. for (std::size_t i = 0; i < 4; ++i)
  262. {
  263. nest::chamber chamber;
  264. chamber.shaft = central_shaft;
  265. chamber.depth = (i + 1) * 50.0f;
  266. chamber.rotation = math::radians(0.0f);
  267. chamber.inner_radius = 4.0f;
  268. chamber.outer_radius = 10.0f;
  269. central_shaft->chambers.push_back(chamber);
  270. }
  271. // Dig nest shafts
  272. float shift = 0.1f;
  273. for (int i = 0; i < 800; ++i)
  274. {
  275. ecs::cavity_component cavity;
  276. cavity.position = nest->extend_shaft(*nest->get_central_shaft());
  277. cavity.position += float3{math::random(-shift, shift), math::random(-shift, shift), math::random(-shift, shift)};
  278. cavity.radius = tunnel_radius * math::random(1.0f, 1.1f);
  279. ecs_registry.assign<ecs::cavity_component>(ecs_registry.create(), cavity);
  280. }
  281. // Dig nest chambers
  282. /*
  283. for (int i = 0; i < central_shaft->chambers.size(); ++i)
  284. {
  285. for (int j = 0; j < 150; ++j)
  286. {
  287. ecs::cavity_component cavity;
  288. cavity.position = nest->expand_chamber(central_shaft->chambers[i]);
  289. cavity.position += float3{math::random(-shift, shift), math::random(-shift, shift), math::random(-shift, shift)};
  290. cavity.radius = tunnel_radius * math::random(1.0f, 1.1f);
  291. ecs_registry.assign<ecs::cavity_component>(ecs_registry.create(), cavity);
  292. }
  293. }
  294. */
  295. // Place larva in chamber
  296. {
  297. auto larva = larva_archetype->create(ecs_registry);
  298. ecs::command::assign_render_layers(ecs_registry, larva, 1);
  299. ecs::command::warp_to(ecs_registry, larva, {50, 0.1935f, 10});
  300. //auto& transform = ecs_registry.get<ecs::transform_component>(larva_entity);
  301. //transform.transform = math::identity_transform<float>;
  302. //transform.transform.translation = nest->get_shaft_position(*central_shaft, central_shaft->depth[1]);
  303. //transform.transform.translation.y -= 1.0f;
  304. }
  305. ecs::control_system* control_system = ctx->control_system;
  306. control_system->update(0.0, 0.0);
  307. control_system->set_nest(nest);
  308. // Start fade in
  309. ctx->fade_transition->transition(1.0f, true, ease<float>::in_quad);
  310. logger->pop_task(EXIT_SUCCESS);
  311. std::string biome_name = (*ctx->strings)[ctx->biome->name];
  312. logger->log("Entered biome \"" + biome_name + "\"");
  313. std::srand(std::time(nullptr));
  314. //auto rng = [](){ return std::rand(); };
  315. std::random_device rd;
  316. std::mt19937 rng(rd());
  317. std::string sequence_a = "CCTTGCCCTTTGGGTCGCCCCCCTAG";
  318. std::string sequence_b = "ATGTTTCCCGAAGGGTAG";
  319. std::string sequence_c = "AAATGCCCCCCCCCCCCCCCCCCCCCCCCCCCTAGAAAAAAAAA";
  320. std::string orf_a;
  321. std::string protein_a;
  322. std::string protein_b;
  323. std::string protein_c;
  324. std::cout << "sequence a: " << sequence_a << std::endl;
  325. genetics::sequence::transcribe(sequence_a.begin(), sequence_a.end(), sequence_a.begin());
  326. std::cout << "sequence a: " << sequence_a << std::endl;
  327. std::string complement;
  328. genetics::sequence::rna::complement(sequence_a.begin(), sequence_a.end(), std::back_inserter(complement));
  329. std::cout << "complement: " << complement << std::endl;
  330. auto orf = genetics::sequence::find_orf(sequence_a.begin(), sequence_a.end(), genetics::standard_code);
  331. if (orf.start != sequence_a.end())
  332. {
  333. std::copy(orf.start, orf.stop, std::back_inserter(orf_a));
  334. std::cout << "orf a: " << orf_a << std::endl;
  335. genetics::sequence::translate(orf.start, orf.stop, std::back_inserter(protein_a), genetics::standard_code);
  336. std::cout << "protein a: " << protein_a << std::endl;
  337. }
  338. protein_b = "MFFFFP";
  339. protein_c = "MFFFYP";
  340. int score;
  341. std::cout << std::endl;
  342. std::cout << "protein_b: " << protein_b << std::endl;
  343. std::cout << "protein_c: " << protein_c << std::endl;
  344. score = genetics::protein::score(protein_b.begin(), protein_b.end(), protein_c.begin(), genetics::matrix::blosum62<int>);
  345. std::cout << "score blosum62: " << score << std::endl;
  346. score = genetics::protein::score(protein_b.begin(), protein_b.end(), protein_c.begin(), genetics::matrix::blosum80<int>);
  347. std::cout << "score blosum80: " << score << std::endl;
  348. std::cout << "identity : " << genetics::protein::identity<float>(protein_b.begin(), protein_b.end(), protein_c.begin()) << std::endl;
  349. std::cout << "similarity62: " << genetics::protein::similarity<float>(protein_b.begin(), protein_b.end(), protein_c.begin(), genetics::matrix::blosum62<int>) << std::endl;
  350. std::cout << "similarity80: " << genetics::protein::similarity<float>(protein_b.begin(), protein_b.end(), protein_c.begin(), genetics::matrix::blosum80<int>) << std::endl;
  351. }
  352. void play_state_exit(game_context* ctx)
  353. {
  354. debug::logger* logger = ctx->logger;
  355. logger->push_task("Exiting play state");
  356. logger->pop_task(EXIT_SUCCESS);
  357. }