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

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