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

442 lines
17 KiB

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/blackbody-component.hpp"
  35. #include "ecs/components/atmosphere-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/orbit-system.hpp"
  60. #include "ecs/systems/astronomy-system.hpp"
  61. #include "game/biome.hpp"
  62. #include "utility/fundamental-types.hpp"
  63. #include "utility/bit-math.hpp"
  64. #include "genetics/genetics.hpp"
  65. #include <iostream>
  66. #include <bitset>
  67. #include <ctime>
  68. void play_state_enter(game_context* ctx)
  69. {
  70. debug::logger* logger = ctx->logger;
  71. logger->push_task("Entering play state");
  72. resource_manager* resource_manager = ctx->resource_manager;
  73. entt::registry& ecs_registry = *ctx->ecs_registry;
  74. // Load biome
  75. if (ctx->option_biome.has_value())
  76. {
  77. ctx->biome = resource_manager->load<biome>(ctx->option_biome.value() + ".bio");
  78. }
  79. else
  80. {
  81. ctx->biome = 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.1f, 1.1f, 1.1f});
  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(4.26352e-5, ctx->biome->location[0], ctx->biome->location[1]);
  93. sky_pass->set_moon_angular_radius(math::radians(1.0f));
  94. sky_pass->set_sun_angular_radius(math::radians(1.0f));
  95. sky_pass->set_sky_gradient(resource_manager->load<gl::texture_2d>("sky-gradient.tex"), resource_manager->load<gl::texture_2d>("sky-gradient2.tex"));
  96. // Create sun
  97. auto sun_entity = ecs_registry.create();
  98. {
  99. ecs::orbit_component orbit;
  100. orbit.elements.a = 0.0;
  101. orbit.elements.e = 0.0;
  102. orbit.elements.i = math::radians(0.0);
  103. orbit.elements.raan = math::radians(0.0);
  104. orbit.elements.w = math::radians(0.0);
  105. orbit.elements.ta = math::radians(0.0);
  106. ecs::blackbody_component blackbody;
  107. blackbody.temperature = 5777.0;
  108. blackbody.radius = 6.957e+8;
  109. ecs::transform_component transform;
  110. transform.local = math::identity_transform<float>;
  111. transform.warp = true;
  112. ecs_registry.assign<ecs::orbit_component>(sun_entity, orbit);
  113. ecs_registry.assign<ecs::blackbody_component>(sun_entity, blackbody);
  114. ecs_registry.assign<ecs::transform_component>(sun_entity, transform);
  115. }
  116. // Create Earth
  117. auto earth_entity = ecs_registry.create();
  118. {
  119. ecs::orbit_component orbit;
  120. orbit.elements.a = 1.00000261;
  121. orbit.elements.e = 0.01671123;
  122. orbit.elements.i = math::radians(-0.00001531);
  123. orbit.elements.raan = math::radians(0.0);
  124. const double longitude_periapsis = math::radians(102.93768193);
  125. orbit.elements.w = longitude_periapsis - orbit.elements.raan;
  126. orbit.elements.ta = math::radians(100.46457166) - longitude_periapsis;
  127. const double earth_radius_m = 6378e3;
  128. ecs::atmosphere_component atmosphere;
  129. atmosphere.exosphere_altitude = 80e3;
  130. atmosphere.rayleigh_scale_height = 8000.0;
  131. atmosphere.mie_scale_height = 1200.0;
  132. ecs::transform_component transform;
  133. transform.local = math::identity_transform<float>;
  134. transform.warp = true;
  135. ecs_registry.assign<ecs::orbit_component>(earth_entity, orbit);
  136. ecs_registry.assign<ecs::atmosphere_component>(earth_entity, atmosphere);
  137. ecs_registry.assign<ecs::transform_component>(earth_entity, transform);
  138. }
  139. scene::ambient_light* ambient = new scene::ambient_light();
  140. ambient->set_color({1, 1, 1});
  141. ambient->set_intensity(0.0f);
  142. ambient->update_tweens();
  143. ctx->overworld_scene->add_object(ambient);
  144. scene::directional_light* sun = new scene::directional_light();
  145. sun->set_intensity(1000.0f);
  146. sun->set_light_texture(resource_manager->load<gl::texture_2d>("forest-gobo.tex"));
  147. sun->set_light_texture_scale({2000, 2000});
  148. sun->set_light_texture_opacity(0.925f);
  149. sun->look_at({2, 1, 0}, {0, 0, 0}, {0, 0, 1});
  150. sun->update_tweens();
  151. ctx->overworld_scene->add_object(sun);
  152. ctx->overworld_shadow_map_pass->set_light(sun);
  153. // Set universal time
  154. const double universal_time = 0.0;
  155. ctx->astronomy_system->set_universal_time(universal_time);
  156. ctx->orbit_system->set_universal_time(universal_time);
  157. // Set astronomy system observation parameters
  158. const double earth_radius_au = 4.2635e-5;
  159. ctx->astronomy_system->set_reference_body(earth_entity);
  160. ctx->astronomy_system->set_reference_body_axial_tilt(math::radians(23.4393));
  161. ctx->astronomy_system->set_observer_location(double3{4.26352e-5, math::radians(0.0f), math::radians(0.0f)});
  162. ctx->astronomy_system->set_sun_light(sun);
  163. ctx->astronomy_system->set_sky_pass(ctx->overworld_sky_pass);
  164. // Load entity archetypes
  165. ecs::archetype* ant_hill_archetype = resource_manager->load<ecs::archetype>("ant-hill.ent");
  166. ecs::archetype* nest_archetype = resource_manager->load<ecs::archetype>("harvester-nest.ent");
  167. ecs::archetype* redwood_archetype = resource_manager->load<ecs::archetype>("redwood.ent");
  168. ecs::archetype* forceps_archetype = resource_manager->load<ecs::archetype>("forceps.ent");
  169. ecs::archetype* lens_archetype = resource_manager->load<ecs::archetype>("lens.ent");
  170. ecs::archetype* brush_archetype = resource_manager->load<ecs::archetype>("brush.ent");
  171. ecs::archetype* marker_archetype = resource_manager->load<ecs::archetype>("marker.ent");
  172. ecs::archetype* container_archetype = resource_manager->load<ecs::archetype>("container.ent");
  173. ecs::archetype* twig_archetype = resource_manager->load<ecs::archetype>("twig.ent");
  174. ecs::archetype* larva_archetype = resource_manager->load<ecs::archetype>("ant-larva.ent");
  175. ecs::archetype* flashlight_archetype = resource_manager->load<ecs::archetype>("flashlight.ent");
  176. ecs::archetype* flashlight_light_cone_archetype = resource_manager->load<ecs::archetype>("flashlight-light-cone.ent");
  177. ecs::archetype* lens_light_cone_archetype = resource_manager->load<ecs::archetype>("lens-light-cone.ent");
  178. ecs::archetype* cube_archetype = resource_manager->load<ecs::archetype>("unit-cube.ent");
  179. ecs::archetype* color_checker_archetype = resource_manager->load<ecs::archetype>("color-checker.ent");
  180. // Create tools
  181. forceps_archetype->assign(ecs_registry, ctx->forceps_entity);
  182. lens_archetype->assign(ecs_registry, ctx->lens_entity);
  183. brush_archetype->assign(ecs_registry, ctx->brush_entity);
  184. marker_archetype->assign(ecs_registry, ctx->marker_entity);
  185. container_archetype->assign(ecs_registry, ctx->container_entity);
  186. twig_archetype->assign(ecs_registry, ctx->twig_entity);
  187. // Create flashlight and light cone, set light cone parent to flashlight, and move both to underworld scene
  188. flashlight_archetype->assign(ecs_registry, ctx->flashlight_entity);
  189. auto flashlight_light_cone = flashlight_light_cone_archetype->create(ecs_registry);
  190. ecs::command::parent(ecs_registry, flashlight_light_cone, ctx->flashlight_entity);
  191. ecs::command::assign_render_layers(ecs_registry, ctx->flashlight_entity, 2);
  192. ecs::command::assign_render_layers(ecs_registry, flashlight_light_cone, 2);
  193. // Make lens tool's model instance unculled, so its shadow is always visible.
  194. scene::model_instance* lens_model_instance = ctx->render_system->get_model_instance(ctx->lens_entity);
  195. if (lens_model_instance)
  196. {
  197. //lens_model_instance->set_culling_mask(&ctx->no_cull);
  198. }
  199. // Create lens light cone and set its parent to lens
  200. auto lens_light_cone = lens_light_cone_archetype->create(ecs_registry);
  201. ecs::command::bind_transform(ecs_registry, lens_light_cone, ctx->lens_entity);
  202. ecs::command::parent(ecs_registry, lens_light_cone, ctx->lens_entity);
  203. // Hide inactive tools
  204. ecs::command::assign_render_layers(ecs_registry, ctx->forceps_entity, 0);
  205. ecs::command::assign_render_layers(ecs_registry, ctx->brush_entity, 0);
  206. ecs::command::assign_render_layers(ecs_registry, ctx->lens_entity, 0);
  207. ecs::command::assign_render_layers(ecs_registry, ctx->marker_entity, 0);
  208. ecs::command::assign_render_layers(ecs_registry, ctx->container_entity, 0);
  209. ecs::command::assign_render_layers(ecs_registry, ctx->twig_entity, 0);
  210. // Activate brush tool
  211. ctx->tool_system->set_active_tool(ctx->brush_entity);
  212. // Create ant-hill
  213. auto ant_hill_entity = ant_hill_archetype->create(ecs_registry);
  214. ecs::command::place(ecs_registry, ant_hill_entity, {0, -40});
  215. // Creat nest
  216. auto nest_entity = nest_archetype->create(ecs_registry);
  217. // Create terrain
  218. int terrain_radius = 0;//6;
  219. for (int x = -terrain_radius; x <= terrain_radius; ++x)
  220. {
  221. for (int z = -terrain_radius; z <= terrain_radius; ++z)
  222. {
  223. ecs::terrain_component terrain_component;
  224. terrain_component.subdivisions = TERRAIN_PATCH_RESOLUTION;
  225. terrain_component.x = x;
  226. terrain_component.z = z;
  227. auto terrain_entity = ecs_registry.create();
  228. ecs_registry.assign<ecs::terrain_component>(terrain_entity, terrain_component);
  229. }
  230. }
  231. // Create trees
  232. for (int i = 0; i < 0; ++i)
  233. {
  234. auto redwood = redwood_archetype->create(ecs_registry);
  235. auto& transform = ecs_registry.get<ecs::transform_component>(redwood);
  236. float zone = 500.0f;
  237. ecs::command::place(ecs_registry, redwood, {math::random(-zone, zone), math::random(-zone, zone)});
  238. }
  239. // Create unit cube
  240. auto cube = cube_archetype->create(ecs_registry);
  241. ecs::command::place(ecs_registry, cube, {10, 10});
  242. // Create color checker
  243. auto color_checker = color_checker_archetype->create(ecs_registry);
  244. ecs::command::place(ecs_registry, color_checker, {-10, -10});
  245. auto& cc_transform = ecs_registry.get<ecs::transform_component>(color_checker);
  246. cc_transform.local.scale *= 10.0f;
  247. cc_transform.local.rotation = math::angle_axis(math::radians(-90.0f), {1, 0, 0});
  248. // Setup camera focal point
  249. ecs::transform_component focal_point_transform;
  250. focal_point_transform.local = math::identity_transform<float>;
  251. focal_point_transform.warp = true;
  252. ecs::camera_follow_component focal_point_follow;
  253. ecs::snap_component focal_point_snap;
  254. focal_point_snap.ray = {float3{0, 10000, 0}, float3{0, -1, 0}};
  255. focal_point_snap.warp = false;
  256. focal_point_snap.relative = true;
  257. focal_point_snap.autoremove = false;
  258. ecs_registry.assign_or_replace<ecs::transform_component>(ctx->focal_point_entity, focal_point_transform);
  259. ecs_registry.assign_or_replace<ecs::camera_follow_component>(ctx->focal_point_entity, focal_point_follow);
  260. ecs_registry.assign_or_replace<ecs::snap_component>(ctx->focal_point_entity, focal_point_snap);
  261. // Setup camera
  262. ctx->overworld_camera->look_at({0, 0, 1}, {0, 0, 0}, {0, 1, 0});
  263. ctx->camera_system->set_camera(ctx->overworld_camera);
  264. ctx->overworld_scene->update_tweens();
  265. // Allocate a nest
  266. nest* nest = new ::nest();
  267. // Setup initial nest parameters
  268. float tunnel_radius = 1.15f;
  269. nest->set_tunnel_radius(tunnel_radius);
  270. nest::shaft* central_shaft = nest->get_central_shaft();
  271. central_shaft->chirality = 1.0f;
  272. central_shaft->rotation = math::radians(0.0f);
  273. central_shaft->depth = {0.0f, 200.0f};
  274. central_shaft->radius = {15.0f, 15.0f};
  275. central_shaft->pitch = {40.0f, 40.0f};
  276. central_shaft->translation = {{{0.0f, 0.0f}, {0.0f, 0.0f}}};
  277. central_shaft->current_depth = 0.0f;
  278. for (std::size_t i = 0; i < 4; ++i)
  279. {
  280. nest::chamber chamber;
  281. chamber.shaft = central_shaft;
  282. chamber.depth = (i + 1) * 50.0f;
  283. chamber.rotation = math::radians(0.0f);
  284. chamber.inner_radius = 4.0f;
  285. chamber.outer_radius = 10.0f;
  286. central_shaft->chambers.push_back(chamber);
  287. }
  288. // Dig nest shafts
  289. float shift = 0.1f;
  290. for (int i = 0; i < 800; ++i)
  291. {
  292. ecs::cavity_component cavity;
  293. cavity.position = nest->extend_shaft(*nest->get_central_shaft());
  294. cavity.position += float3{math::random(-shift, shift), math::random(-shift, shift), math::random(-shift, shift)};
  295. cavity.radius = tunnel_radius * math::random(1.0f, 1.1f);
  296. ecs_registry.assign<ecs::cavity_component>(ecs_registry.create(), cavity);
  297. }
  298. // Dig nest chambers
  299. /*
  300. for (int i = 0; i < central_shaft->chambers.size(); ++i)
  301. {
  302. for (int j = 0; j < 150; ++j)
  303. {
  304. ecs::cavity_component cavity;
  305. cavity.position = nest->expand_chamber(central_shaft->chambers[i]);
  306. cavity.position += float3{math::random(-shift, shift), math::random(-shift, shift), math::random(-shift, shift)};
  307. cavity.radius = tunnel_radius * math::random(1.0f, 1.1f);
  308. ecs_registry.assign<ecs::cavity_component>(ecs_registry.create(), cavity);
  309. }
  310. }
  311. */
  312. // Place larva in chamber
  313. {
  314. auto larva = larva_archetype->create(ecs_registry);
  315. ecs::command::assign_render_layers(ecs_registry, larva, 1);
  316. ecs::command::warp_to(ecs_registry, larva, {50, 0.1935f, 0});
  317. //auto& transform = ecs_registry.get<ecs::transform_component>(larva_entity);
  318. //transform.transform = math::identity_transform<float>;
  319. //transform.transform.translation = nest->get_shaft_position(*central_shaft, central_shaft->depth[1]);
  320. //transform.transform.translation.y -= 1.0f;
  321. }
  322. ecs::control_system* control_system = ctx->control_system;
  323. control_system->update(0.0, 0.0);
  324. control_system->set_nest(nest);
  325. // Start fade in
  326. ctx->fade_transition->transition(1.0f, true, ease<float>::in_quad);
  327. logger->pop_task(EXIT_SUCCESS);
  328. std::string biome_name = (*ctx->strings)[ctx->biome->name];
  329. logger->log("Entered biome \"" + biome_name + "\"");
  330. std::srand(std::time(nullptr));
  331. //auto rng = [](){ return std::rand(); };
  332. std::random_device rd;
  333. std::mt19937 rng(rd());
  334. std::string sequence_a = "CCTTGCCCTTTGGGTCGCCCCCCTAG";
  335. std::string sequence_b = "ATGTTTCCCGAAGGGTAG";
  336. std::string sequence_c = "AAATGCCCCCCCCCCCCCCCCCCCCCCCCCCCTAGAAAAAAAAA";
  337. std::string orf_a;
  338. std::string protein_a;
  339. std::string protein_b;
  340. std::string protein_c;
  341. std::cout << "sequence a: " << sequence_a << std::endl;
  342. genetics::sequence::transcribe(sequence_a.begin(), sequence_a.end(), sequence_a.begin());
  343. std::cout << "sequence a: " << sequence_a << std::endl;
  344. std::string complement;
  345. genetics::sequence::rna::complement(sequence_a.begin(), sequence_a.end(), std::back_inserter(complement));
  346. std::cout << "complement: " << complement << std::endl;
  347. auto orf = genetics::sequence::find_orf(sequence_a.begin(), sequence_a.end(), genetics::standard_code);
  348. if (orf.start != sequence_a.end())
  349. {
  350. std::copy(orf.start, orf.stop, std::back_inserter(orf_a));
  351. std::cout << "orf a: " << orf_a << std::endl;
  352. genetics::sequence::translate(orf.start, orf.stop, std::back_inserter(protein_a), genetics::standard_code);
  353. std::cout << "protein a: " << protein_a << std::endl;
  354. }
  355. protein_b = "MFFFFP";
  356. protein_c = "MFFFYP";
  357. int score;
  358. std::cout << std::endl;
  359. std::cout << "protein_b: " << protein_b << std::endl;
  360. std::cout << "protein_c: " << protein_c << std::endl;
  361. score = genetics::protein::score(protein_b.begin(), protein_b.end(), protein_c.begin(), genetics::matrix::blosum62<int>);
  362. std::cout << "score blosum62: " << score << std::endl;
  363. score = genetics::protein::score(protein_b.begin(), protein_b.end(), protein_c.begin(), genetics::matrix::blosum80<int>);
  364. std::cout << "score blosum80: " << score << std::endl;
  365. std::cout << "identity : " << genetics::protein::identity<float>(protein_b.begin(), protein_b.end(), protein_c.begin()) << std::endl;
  366. std::cout << "similarity62: " << genetics::protein::similarity<float>(protein_b.begin(), protein_b.end(), protein_c.begin(), genetics::matrix::blosum62<int>) << std::endl;
  367. std::cout << "similarity80: " << genetics::protein::similarity<float>(protein_b.begin(), protein_b.end(), protein_c.begin(), genetics::matrix::blosum80<int>) << std::endl;
  368. }
  369. void play_state_exit(game_context* ctx)
  370. {
  371. debug::logger* logger = ctx->logger;
  372. logger->push_task("Exiting play state");
  373. logger->pop_task(EXIT_SUCCESS);
  374. }