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

182 lines
5.6 KiB

7 years ago
7 years ago
  1. /*
  2. * Copyright (C) 2017 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. #ifndef NEST_HPP
  20. #define NEST_HPP
  21. #include <random>
  22. #include <vector>
  23. #include <emergent/emergent.hpp>
  24. using namespace Emergent;
  25. /**
  26. * General algorithm: Draw a series of lines straight down. Select multiple elevation levels on each line at which to create a chamber. Create helixes around lines. At the selected elevation levels on each line, create a corresponding chambers on the helixes at the same elevations, in the direction of the outside of the helix. Check for intersections between chambers and tunnels, and merge as necessary.
  27. */
  28. struct Chamber;
  29. /**
  30. * A vertically-directed helical shaft with one or more connected chambers.
  31. */
  32. struct Shaft
  33. {
  34. Chamber* parent; // The parent chamber from which this shaft begins
  35. std::vector<Chamber*> children; // A list of chambers which are formed from this shaft
  36. int generation; // The generation index of this shaft. The root shaft is gen 0, the shafts from its chambers are gen 1, and so on.
  37. Vector3 entrance; // Position of the entrance point of this shaft
  38. float shaftRadius; // Radius of the shaft
  39. float shaftDepth; // Total depth of this shaft
  40. float initialHelixAngle; // Angle at which the shaft helix begins
  41. float helixRadius; // Radius of the shaft helix
  42. float helixPitch; // Pitch of the shaft helix
  43. /**
  44. * Returns the position on the shaft's helix at the specified depth.
  45. *
  46. * @param depth Depth relative to the entrance of this shaft.
  47. * @return Point on the helix at the specified depth.
  48. */
  49. Vector3 getHelixPosition(float depth) const;
  50. /**
  51. * Returns the angle to the helix at the specified depth.
  52. *
  53. * @param depth Depth relative to the entrance of this shaft.
  54. * @return Angle to the helix at the specified depth.
  55. */
  56. float getHelixAngle(float depth) const;
  57. };
  58. /**
  59. * A horizontal annular chamber with one parent shaft and a max of one child shaft. Chambers always face toward the outside of the parent shaft's helix.
  60. */
  61. struct Chamber
  62. {
  63. Shaft* parent; // Parent shaft from which this chamber was formed
  64. Shaft* child; // Child shaft which begins in this chamber
  65. int generation; // The number of chambers from this chamber to the root shaft.
  66. float relativeDepth; // Depth from the entrance of the parent shaft to this chamber
  67. float absoluteDepth; // Depth from the entrance of the root shaft to this chamber
  68. float innerRadius; // Inner radius of the annulus
  69. float outerRadius; // Outer radius of the annulus
  70. float centralAngle; // Angle of the annular sector
  71. float height; // Height of the annular sector
  72. float childAngle; // The angle on the annulus at which the child shaft begins
  73. };
  74. /**
  75. * Describes the parameters required to generate a nest.
  76. */
  77. struct NestParameters
  78. {
  79. // Random params
  80. unsigned int randomSeed;
  81. // Shaft params
  82. int maxShaftGeneration;
  83. float minShaftRadius;
  84. float maxShaftRadius;
  85. float minShaftDepth;
  86. float maxShaftDepth;
  87. float minShaftHelixRadius;
  88. float maxShaftHelixRadius;
  89. float minShaftHelixPitch;
  90. float maxShaftHelixPitch;
  91. int minShaftChamberCount;
  92. int maxShaftChamberCount;
  93. float minShaftChamberPitch;
  94. float maxShaftChamberPitch;
  95. // Chamber params
  96. float minChamberInnerRadius;
  97. float maxChamberInnerRadius;
  98. float minChamberOuterRadius;
  99. float maxChamberOuterRadius;
  100. float minChamberCentralAngle;
  101. float maxChamberCentralAngle;
  102. };
  103. class Nest
  104. {
  105. public:
  106. Nest();
  107. ~Nest();
  108. /**
  109. * Generates the nest and all of its shafts and chambers.
  110. */
  111. void generate();
  112. /**
  113. * Sets the nest generation parameters.
  114. */
  115. void setParameters(const NestParameters& parameters);
  116. /**
  117. * Returns the nest generation parameters.
  118. */
  119. const NestParameters& getParameters() const;
  120. /**
  121. * Returns a pointer to the root shaft of the nest.
  122. */
  123. const Shaft* getRootShaft() const;
  124. private:
  125. /**
  126. * Recursive function which generates a connected system of shafts and chambers.
  127. *
  128. * @param parent Specifies the parent chamber of the shaft. A value of `nullptr` indicates the root shaft.
  129. * @param generation The index of the shaft's generation. The root shaft is generation 0, the shafts formed from the root shaft's chambers are generation 1, and so on.
  130. * @return The newly created shaft.
  131. */
  132. Shaft* dig(Chamber* parent) const;
  133. /**
  134. * Determines intersections between chambers and connects them.
  135. */
  136. void merge();
  137. /**
  138. * Creates a map (interconnected system of nodes) with which can be used to navigate the nest.
  139. */
  140. void map();
  141. /**
  142. * Recursive function which deletes a shaft and all of its connect chambers and subshafts.
  143. *
  144. * @param Specifies a shaft to delete.
  145. */
  146. void free(Shaft* shaft);
  147. float random(float minValue, float maxValue) const;
  148. int random(int minValue, int maxValue) const;
  149. NestParameters parameters;
  150. Shaft* root;
  151. mutable std::default_random_engine rng;
  152. };
  153. inline const Shaft* Nest::getRootShaft() const
  154. {
  155. return root;
  156. }
  157. #endif // NEST_HPP