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

93 lines
3.0 KiB

  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 "nest.hpp"
  20. #include "math/math.hpp"
  21. nest::nest()
  22. {
  23. dig_radius = 1.25f;
  24. }
  25. float3 nest::extend_shaft(shaft& shaft)
  26. {
  27. float3 dig_position = get_shaft_position(shaft, shaft.current_depth);
  28. float dr = math::random(dig_radius * 0.75f, dig_radius * 1.25f);
  29. shaft.current_depth += dr * 0.1f;
  30. return dig_position;
  31. }
  32. float3 nest::expand_chamber(chamber& chamber)
  33. {
  34. float dig_angle = math::random(0.0f, math::two_pi<float>);
  35. float2 dig_direction = math::normalize(float2{std::cos(dig_angle), std::sin(dig_angle)});
  36. float3 chamber_center = get_shaft_position(*chamber.shaft, chamber.depth);
  37. float3 dig_position = chamber_center;
  38. float dr = math::random(dig_radius * 0.75f, dig_radius * 1.25f);
  39. float t = math::random(0.0f, 1.0f);
  40. dig_position.x += dig_direction.x * (chamber.outer_radius - dr) * t;
  41. dig_position.z += dig_direction.y * (chamber.outer_radius - dr) * t;
  42. return dig_position;
  43. }
  44. void nest::set_tunnel_radius(float radius)
  45. {
  46. tunnel_radius = radius;
  47. }
  48. float nest::get_shaft_angle(const shaft& shaft, float depth) const
  49. {
  50. float shaft_length = shaft.depth[1] - shaft.depth[0];
  51. float depth_factor = (depth - shaft.depth[0]) / shaft_length;
  52. float pitch = math::lerp<float>(shaft.pitch[0], shaft.pitch[1], depth_factor);
  53. return shaft.rotation + (depth / pitch) * shaft.chirality * math::two_pi<float>;
  54. }
  55. float nest::get_shaft_depth(const shaft& shaft, float turns) const
  56. {
  57. return shaft.pitch[0] * turns;
  58. }
  59. float3 nest::get_shaft_position(const shaft& shaft, float depth) const
  60. {
  61. float shaft_length = shaft.depth[1] - shaft.depth[0];
  62. float depth_factor = (depth - shaft.depth[0]) / shaft_length;
  63. float pitch = math::lerp<float>(shaft.pitch[0], shaft.pitch[1], depth_factor);
  64. float radius = math::lerp<float>(shaft.radius[0], shaft.radius[1], depth_factor);
  65. float translation_x = math::lerp<float>(shaft.translation[0][0], shaft.translation[1][0], depth_factor);
  66. float translation_z = math::lerp<float>(shaft.translation[0][1], shaft.translation[1][1], depth_factor);
  67. float angle = shaft.rotation + (depth / pitch) * shaft.chirality * math::two_pi<float>;
  68. float3 position;
  69. position[0] = std::cos(angle) * radius + translation_x;
  70. position[1] = -std::max<float>(shaft.depth[0], std::min<float>(shaft.depth[1], depth));
  71. position[2] = std::sin(angle) * radius + translation_z;
  72. return position;
  73. }