Skip to main content

ForestGeneration

A class in Root.Township, Alta.Forests.

A forest's generated layout: outline, entrances and a path graph of nodes, edges and triangular spaces, plus lookups by chunk and grid. Cached as an ICacheable; get it from the forest generator's GetGenerationAsync or GetGenerated.

Constructors​

MemberWhat it is
ForestGeneration()Creates a ForestGeneration with default values.

Fields​

20 private and internal fields
MemberWhat it is
private float areaBacking field for Area: the area inside the outline, worked out when the perimeter is triangulated.
private Dictionary<LocationChunk, HashSet<Edge>> chunkEdgesEdges grouped by the location chunk that holds their midpoint; read by GetEdgesWithinChunk.
private Dictionary<LocationChunk, HashSet<Node>> chunkNodesNodes grouped by the location chunk that holds them; read by GetNodesWithinChunk.
private Mesh delaunayMeshBacking field for Delaunay: the Delaunay triangulation of the node positions, rebuilt by UpdateDelaunay.
private List<Edge> edgesBacking field for Edges and WorkingEdges: the paths between nodes.
private List<Node> entranceNodesBacking field for EntranceNodes: the first nodes, one for each entrance position.
private Vector3[] entrancePositionsBacking field for Entrances: world positions of the forest entrances.
private uint groupSizeXWidth in world units of one cell of the lookup grid.
private uint groupSizeZDepth in world units of one cell of the lookup grid.
private int[] indicesBacking field for OutlineIndices: triangle indices of the triangulated perimeter, used by Contains.
private Otri lastResultTriangle found by the last GetSpace call; the next search starts from it.
private static Logger loggerNLog logger for this class.
private List<Node> nodesBacking field for Nodes and WorkingNodes: the forest's path nodes.
private SimplexNoise noiseBacking field for SimplexNoise, seeded from Seed plus one in GenerateNoise.
private Vector3[] outlinePositionsBacking field for Outline: world positions of the forest border points.
private Vector2[] perimeterThe outline points flattened to XZ, used for containment and border distance checks.
private uint seedBacking field for Seed; saved with the forest.
private HashSet<Edge>[,] sortedEdges51 by 51 lookup grid of the edges near each cell, filled by SortNodes and used by EvaluateNearest.
private HashSet<Node>[,] sortedNodes51 by 51 lookup grid of the nodes near each cell, filled by SortNodes.
private List<Space> spacesBacking field for WorkingSpaces (and Spaces, which skips nulls): triangles between three nodes, matched to Delaunay triangles by index.

Properties​

MemberWhat it is
float Area { get; }Area of the forest outline, worked out when the outline is set.
BiomeSphereManager BiomeSphereManager { get; }The biome areas inside the forest. Saved with the layout.
Bounds Bounds { get; private set; }Bounds around the outline points, grown by 1 unit.
Mesh Delaunay { get; }The TriangleNet triangulation of the node positions, used by GetSpace and GetNodeHeight.
IReadOnlyList<Edge> Edges { get; }All path edges between nodes.
ICollection<Node> EntranceNodes { get; }The nodes at the forest entrances, set by CollectEntrances.
ICollection<Vector3> Entrances { get; }World positions of the entrance points.
ForestGenerator Generator { get; set; }The ForestGenerator this layout belongs to.
float LongestPath { get; private set; }Not set anywhere in this build, so always 0.
int NodeCount { get; }How many path nodes there are.
IReadOnlyList<Node> Nodes { get; }All path nodes, read-only.
ICollection<Vector3> Outline { get; }World positions of the outline points.
int[] OutlineIndices { get; }Triangle indices from triangulating the outline. Used by Contains.
uint Seed { get; }The seed the layout was generated from.
SimplexNoise SimplexNoise { get; }Noise seeded from the forest seed, set by GenerateNoise.
IEnumerable<Space> Spaces { get; }The triangular spaces between edges, skipping empty slots.
uint Version { get; }Cache data version, 1.
List<Edge> WorkingEdges { get; }The editable edge list, used while generating.
List<Node> WorkingNodes { get; }The editable node list, used while generating.
List<Space> WorkingSpaces { get; }The editable space list (may hold nulls), used while generating.

Methods​

MemberWhat it is
Node ClosestNode(Vector2 point, out bool isWithinRadius)Nearest path node to an x/z point from the grid lookup. Stops early and reports when the point is inside a node's radius.
void CollectEntrances()Takes the first nodes (one per entrance position) as the entrance nodes.
bool Contains(Vector2 point)Whether an x/z point is inside the forest outline.
Edge CreateEdge(Node a, Node b, EdgeType type)Adds an edge of a given type between two nodes, links paths both ways and files it under its location chunk.
float DistanceToBorderSquared(Vector2 point)Squared distance from an x/z point to the nearest outline segment.
float DistanceToPath(Vector2 point, Predicate<Edge> predicate, bool isIncludingRadius = true)Distance from an x/z point to the nearest edge that passes the filter, checking every edge. Minus the path radius by default.
float DistanceToPath(Vector2 point, Predicate<Edge> predicate, out Edge nearest, out float nearestProgress, bool isIncludingRadius = true)Distance from an x/z point to the nearest edge that passes the filter, checking every edge. Minus the path radius by default.
float DistanceToPathOrNode(Vector2 point, Predicate<Edge> edgePredicate, out Edge nearestEdge, out Node nearestNode, out float nearestProgress, bool isIncludingRadius = true)Like DistanceToPath but checks the grid group first and falls back to all edges. The node output is always null.
void EvaluateGroup(float worldX, float worldZ, out int groupX, out int groupZ)Turns a world x/z into grid group indices, from the terrain offset and the group sizes.
ForestGenerator.NearestInfo EvaluateNearest(Vector2 position, int xGroup, int zGroup, Predicate<Edge> condition)Up to the five nearest edges (passing a filter) to a point within one grid group, nearest first.
void GenerateNoise()Creates the forest's simplex noise from the seed.
Edge GetEdge(int fromIndex, int toIndex)The edge between two node indices, or null (with an error) if an index is out of range.
IEnumerable<Edge> GetEdgesWithinChunk(LocationChunk chunk)The edges filed under a location chunk. Empty if none.
float GetNodeHeight(Vector2 point)Height at an x/z point, blended from the heights of the surrounding nodes. -1 if outside the triangulation.
IEnumerable<Node> GetNodesWithinChunk(LocationChunk chunk)The nodes filed under a location chunk. Empty if none.
Space GetSpace(Vector2 point)The triangular space containing an x/z point, found through the triangulation. Null if outside.
async Task SerializeAsync(uint version, Stream stream)Reads or writes the layout for the cache. After reading it rebuilds the chunk and grid lookups, triangulation, entrances and noise.
void SetGroupSizes(uint sizeX, uint sizeZ)Sets the x and z size of the grid groups used for lookups.
void SetOutline(Transform[] outline, Transform[] entrances)Copies the outline and entrance positions from transforms and works out bounds and area.
void SetSeed(uint seed)Sets the generation seed.
async Task SortNodes()Async: files nodes and edges under their location chunks and a 51 by 51 grid for fast lookups. Also adds those chunks to LocationChunk.ContentLocationChunks.
void UpdateDelaunay(List<Vector3> overridePoints = null)Rebuilds the triangulation from the node positions, or from given points.
4 private and internal methods
MemberWhat it is
private void EvaluateBounds()Builds Bounds from the outline (grown by 1) and triangulates the perimeter into indices, storing the forest area.
private Vector2 GetGroup(int groupX, int groupZ, out float distanceToCornerSquared)Returns the world XZ corner of a lookup grid cell and the squared length of a cell's diagonal.
private void SerializeNodes(Stream stream)Saves or loads the nodes (position and radius), the edges between them (by node index and type) and the spaces (triangles of three nodes).
private void SerializeOutline(Stream stream)Saves or loads the outline and entrance positions; on load it rebuilds the perimeter and bounds.

Used in​

Members of other types that take, return or hold a ForestGeneration:

ForestDefinedAtmosphere.generation, CutoutPathModifier.Modify, CutoutPathModifier.ModifyHeight, CutoutPathModifier.ModifySplat, ForestAttributeEvaluator.EvaluateEdge, ForestAttributeEvaluator.EvaluateNode, ForestAttributeEvaluator.Modify, ForestBridges.GenerateDynamic, ForestBridges.GenerateGlobal, ForestBridges.GenerateStatic, ForestCliffEvaluator.EvaluateCliff, ForestCliffEvaluator.IsLegitimateEdge, ForestCliffEvaluator.Modify, ForestConnectivityIncreaser.Modify, ForestEncounters.GenerateDynamic, ForestEncounters.GenerateGlobal, ForestEncounters.GenerateStatic, ForestFillCore.Forest, ForestFillCore.GenerateDynamic, ForestFillCore.GenerateGlobal, ForestFillCore.GenerateStatic, ForestFillCore.Setup.Forest, ForestFillCore.Setup.Setup, ForestGenerator.AddRandomInTriangle, ForestGenerator.AddRandomPoint, ForestGenerator.AreaStartingAt, ForestGenerator.Carve, ForestGenerator.CreateRandomPoints, ForestGenerator.EvaluateHeights, ForestGenerator.EvaluateSplatData, ForestGenerator.EvaluateTargetHeight, ForestGenerator.GenerateAsync, ForestGenerator.GenerateInternalAsync, ForestGenerator.GenerateTerrain, ForestGenerator.generation, ForestGenerator.GetAllGCores, ForestGenerator.GetChunkInstructionsAsync, ForestGenerator.GetGenerated, ForestGenerator.GetGenerationAsync, ForestGenerator.ModifyHeights, ForestGenerator.OutlineGizmo, ForestGenerator.PathGizmos, ForestGenerator.Reproduce, ForestGenerator.SpaceGizmos, ForestGenerator.Traverse, ForestGenerator.WeightToEdge, ForestPathAttributeLerper.Modify, ForestSpaceEvaluator.Modify, IForestGeneration.GenerateDynamic, IForestGeneration.GenerateGlobal, IForestGeneration.GenerateStatic, ISplatPainter.Paint, ITerrainModifier.ModifyHeight, ITerrainModifier.ModifySplat, LandBridgeModifier.Modify, NodeMapModifier.Modify, PointEdge.EvaluateEdge, PointEdge.GetEdge, PointEdge.GetEdgeDistance, PointNormal.GetEdge and 24 more.