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

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