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

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