Skip to main content

MiningBaseGenerator

A class in Root.Township, the global namespace.

Server-side generator that builds a random mine base from StructurePart tiles on a grid and spawns them as child entities. It checks the space with physics first so parts avoid existing terrain.

Constructors​

MemberWhat it is
MiningBaseGenerator()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

28 private and internal fields
MemberWhat it is
private const StructureSide AllSidesConstant flag set holding all six StructureSide directions.
private MiningBaseGenerator.PartOption[] centerPiecesWeighted parts the generation can start from.
private LayerMask collideWithLayers that count as blocked when scanning the space.
private int depthGrid size along Z, in tiles.
private float floorEdgeChanceChance that each bottom-layer cell starts with open gates on all four horizontal sides.
private int halfContinuationCountOnce more parts than this are placed, the chance of spreading further is halved.
private int heightGrid size along Y, in tiles.
private int[,] heightsPer column height array; reset each attempt.
private const int HorizontalMaskBit mask (15) for the four horizontal StructureSide flags.
private StructureSide[,,] incomingAirPer cell sides that neighbours say are open air.
private StructureSide[,,] incomingGatesPer cell sides where a neighbour has an open gate.
private StructureSide[,,] incomingRequireAirPer cell sides where a neighbour needs air next to it.
private StructureSide[,,] incomingRequireSolidPer cell sides where a neighbour needs solid next to it.
private StructureSide[,,] incomingSolidPer cell sides that neighbours say are solid.
private bool isRegeneratingInspector button flag labelled Regenerate.
private int maximumAttemptsHow many generation attempts to make before giving up.
private int maximumSpawnedAn attempt fails if it places this many parts or more.
private int minimumSpawnedAn attempt fails if it places fewer parts than this.
private MiningBaseGenerator.PartOption[] partsWeighted parts used to fill out the base after the center piece.
private List<MiningBaseGenerator.Point> priorityCells to process before the normal queue.
private List<MiningBaseGenerator.Point> queueCells waiting to have a part placed at them.
private List<MiningBaseGenerator.Possibility> resultsPlacements chosen in the current attempt.
private bool[,,] spacialAwarenessPer cell flags for cells already blocked by world geometry.
private bool[,] spacialAwarenessFloorPer column flags for whether ground was found below the bottom layer.
private StructureType structureTypeStructure type that supplies the tile size.
private bool[,,] takenPer cell flags for cells already filled.
private float verticalCheckOffsetHeight added to each cell's centre before the space checks.
private int widthGrid size along X, in tiles.

Methods​

MemberWhat it is
bool CheckPossible(MiningBaseGenerator.Possibility possibility, out int pathCount, bool isForced = false)Checks whether a part at a given offset and rotation fits the grid and matches the gate, solid and air rules of its neighbours; outputs how many gates connect.
MiningBaseGenerator.PartOption GetRandom(MiningBaseGenerator.PartOption[] parts, SpawnData spawnData)Weighted random pick: either a PartOption (chance plus biome attribute bonus) or an index into a possibility list.
int GetRandom(List<MiningBaseGenerator.Possibility> possibilities)Weighted random pick: either a PartOption (chance plus biome attribute bonus) or an index into a possibility list.
StructureSide Rotate(StructureSide side, int rotations)Rotates the horizontal bits of a StructureSide mask by a number of 90 degree steps, leaving up and down alone.
protected override void Generate(SpawnData spawnData)Server: scans the space, retries Attempt up to maximumAttempts times, then spawns the result if one succeeded.
15 private and internal methods
MemberWhat it is
private void ApplySpacialAwareness()Marks grid cells blocked by world geometry as taken and clears the floor flag where no ground was found below. Called at the start of each attempt.
private bool Attempt(SpawnData spawnData)Runs one full generation pass from a random center piece. Returns true if the part count lands between minimumSpawned and maximumSpawned.
private void ChoosePossibility(MiningBaseGenerator.Point point, List<MiningBaseGenerator.Possibility> possibilities)Picks a weighted random placement for a queued cell, discarding ones that don't fit, and marks the first one that does.
private void CullResults()Removes placed parts whose cubes connect to fewer gates than their MinimumGatesForCulling value.
private List<MiningBaseGenerator.Possibility> GeneratePossibilities(MiningBaseGenerator.Point point, SpawnData spawnData)Lists every part, rotation, drop and offset that could cover a grid cell, weighted by the part's chance and biome attribute.
private void GenerateSpacialAwareness()Box-casts each grid cell and raycasts down from the bottom layer to record which cells are blocked and which have ground under them.
private void Mark(MiningBaseGenerator.Possibility possibility, float continuationChance = 1)Records a placed part: marks its cells as taken and pushes its gate, solid and air sides onto neighbouring cells, maybe queueing them.
private void MarkCenterPiece(SpawnData spawnData)Places a random center piece at a random spot on the bottom layer, ignoring neighbour rules.
private void MarkIncoming(int x, int y, int z, StructureSide outgoingSides, StructureSide[,,] map, float queueChance = -1)Sets or clears the matching side flag on each neighbour of a cell in one of the incoming maps, and may queue that neighbour.
private bool MatchAll(int x, int y, int z, StructureSide sides, StructureSide[,,] map)True if every bit in one side mask is also set in the other (either the given sides or the map cell is the required set).
private bool MatchAll(int x, int y, int z, StructureSide[,,] map, StructureSide sides)True if every bit in one side mask is also set in the other (either the given sides or the map cell is the required set).
private bool MatchAny(int x, int y, int z, StructureSide sides, StructureSide[,,] map)True if any of the given sides is set in the map cell.
private int NumberOfSetBits(int i)Counts the set bits in an integer. Used to count connected gates when culling.
private void Reset()Clears results and queues and rebuilds the grid maps, with solid floor below, air inside and random floor-edge gates.
private void SpawnResults(SpawnData spawnData)Spawns each chosen part as a child entity and places it on the grid using the structure type's tile size.