Skip to main content

euv_core/game/algorithms/procgen/
struct.rs

1use super::*;
2
3/// Portable deterministic Xoshiro256** pseudo-random number generator seeded via SplitMix64.
4#[derive(Clone, Copy, Data, Debug, Eq, PartialEq)]
5pub struct DeterministicRng {
6    /// Internal 256-bit Xoshiro256** generator state.
7    #[get(type(copy))]
8    pub(crate) state: [u64; 4],
9}
10
11/// Seeded multi-octave procedural noise evaluator supporting Perlin, Simplex, Worley, fBm, Ridged, and Curl noise.
12#[derive(Clone, Data, Debug)]
13pub struct NoiseField {
14    /// Original deterministic seed.
15    #[get(type(copy))]
16    pub(crate) seed: u64,
17    /// Doubled 512-entry permutation lookup table built deterministically from `seed`.
18    #[get(pub(crate))]
19    #[get_mut(pub(crate))]
20    #[set(pub(crate))]
21    pub(crate) perm: [u8; 512],
22}
23
24/// Compact 128-bit tile domain bitmask supporting zero-allocation WFC propagation.
25#[derive(Clone, Copy, Debug, Default, Eq, Getter, PartialEq)]
26pub(crate) struct WfcMask {
27    /// Lower 64 tile bits (`0..64`) and upper 64 tile bits (`64..128`).
28    #[get(type(copy))]
29    pub(crate) words: [u64; 2],
30}
31
32/// Tile weights and four-way adjacency compatibility rules for `WfcSolver2D`.
33#[derive(Clone, Data, Debug)]
34pub struct WfcRuleSet {
35    /// Number of valid tile indices in `0..tile_count` (at most `128`).
36    #[get(type(copy))]
37    pub(crate) tile_count: usize,
38    /// Positive selection weights per tile index.
39    pub(crate) weights: Vec<f32>,
40    /// Per-tile allowed neighbor masks indexed by `[tile][dir_index]`.
41    #[get(pub(crate))]
42    #[get_mut(pub(crate))]
43    #[set(pub(crate))]
44    pub(crate) compat: Vec<[WfcMask; 4]>,
45}
46
47/// Backtracking snapshot saved prior to speculative WFC tile collapse.
48#[derive(Clone, Debug)]
49pub(crate) struct WfcBacktrackFrame {
50    /// Domain bitmask grid snapshot prior to speculative collapse.
51    pub(crate) cells: Vec<WfcMask>,
52    /// Index of the cell being speculatively collapsed.
53    pub(crate) cell_index: usize,
54    /// Remaining untried candidate tiles for `cell_index`.
55    pub(crate) remaining: WfcMask,
56}
57
58/// Wave Function Collapse 2D solver with Shannon-entropy selection and deterministic backtracking.
59#[derive(Clone, Data, Debug)]
60pub struct WfcSolver2D {
61    /// Tile weights and symmetric directional compatibility rules.
62    pub(crate) rules: WfcRuleSet,
63    /// Maximum backtracking steps before reporting unsatisfiable.
64    #[get(type(copy))]
65    pub(crate) max_backtracks: usize,
66}
67
68/// Solved 2D tile grid produced by `WfcSolver2D`.
69#[derive(Clone, Data, Debug, PartialEq)]
70pub struct WfcResult2D {
71    /// Solved grid width in tiles.
72    #[get(type(copy))]
73    pub(crate) width: usize,
74    /// Solved grid height in tiles.
75    #[get(type(copy))]
76    pub(crate) height: usize,
77    /// Row-major collapsed tile indices.
78    pub(crate) tiles: Vec<usize>,
79    /// Number of contradiction backtracks executed during solving.
80    #[get(type(copy))]
81    pub(crate) backtracks: usize,
82}
83
84/// Bridson's fast O(N) Poisson-disk blue-noise sampler in 2D and 3D.
85#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
86pub struct PoissonDiskSampler {
87    /// Candidate attempts per active sample point (typically `30`).
88    #[get(type(copy))]
89    pub(crate) max_attempts: usize,
90}
91
92/// Configuration parameters for procedural BSP dungeon and interior generation.
93#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
94pub struct BspDungeonConfig {
95    /// Total dungeon tile grid width.
96    #[get(type(copy))]
97    pub(crate) width: usize,
98    /// Total dungeon tile grid height.
99    #[get(type(copy))]
100    pub(crate) height: usize,
101    /// Minimum BSP leaf partition width and height.
102    #[get(type(copy))]
103    pub(crate) min_leaf_size: usize,
104    /// Maximum BSP leaf partition width and height before forced splitting.
105    #[get(type(copy))]
106    pub(crate) max_leaf_size: usize,
107    /// Minimum interior room width and height.
108    #[get(type(copy))]
109    pub(crate) min_room_size: usize,
110    /// Margin reserved around rooms inside each BSP leaf.
111    #[get(type(copy))]
112    pub(crate) room_padding: usize,
113    /// Width of carved connecting corridors in tiles.
114    #[get(type(copy))]
115    pub(crate) corridor_width: usize,
116}
117
118/// Axis-aligned integer room rectangle carved inside a BSP dungeon leaf.
119#[derive(Clone, Copy, Data, Debug, Eq, Hash, New, PartialEq)]
120pub struct BspRoomRect {
121    /// Leftmost interior X tile coordinate.
122    #[get(type(copy))]
123    pub(crate) x: usize,
124    /// Topmost interior Y tile coordinate.
125    #[get(type(copy))]
126    pub(crate) y: usize,
127    /// Interior width in tiles.
128    #[get(type(copy))]
129    pub(crate) width: usize,
130    /// Interior height in tiles.
131    #[get(type(copy))]
132    pub(crate) height: usize,
133}
134
135/// Straight orthogonal corridor segment linking two rooms or bends.
136#[derive(Clone, Copy, Data, Debug, Eq, Hash, New, PartialEq)]
137pub struct BspCorridorSegment {
138    /// Starting tile coordinate.
139    #[get(type(copy))]
140    pub(crate) start: GridCoord,
141    /// Ending tile coordinate.
142    #[get(type(copy))]
143    pub(crate) end: GridCoord,
144    /// Corridor tile width.
145    #[get(type(copy))]
146    pub(crate) width: usize,
147}
148
149/// Doorway threshold connecting a carved corridor to a room perimeter.
150#[derive(Clone, Copy, Data, Debug, Eq, Hash, New, PartialEq)]
151pub struct BspDoorway {
152    /// Tile coordinate of the doorway.
153    #[get(type(copy))]
154    pub(crate) position: GridCoord,
155    /// Index into `BspDungeonMap::rooms` owning this doorway.
156    #[get(type(copy))]
157    pub(crate) room_index: usize,
158}
159
160/// Generated procedural dungeon containing tile grid, rooms, corridors, and doorways.
161#[derive(Clone, Data, Debug, PartialEq)]
162pub struct BspDungeonMap {
163    /// Map width in tiles.
164    #[get(type(copy))]
165    pub(crate) width: usize,
166    /// Map height in tiles.
167    #[get(type(copy))]
168    pub(crate) height: usize,
169    /// Row-major semantic tiles.
170    pub(crate) tiles: Vec<BspTileKind>,
171    /// Carved interior rooms.
172    pub(crate) rooms: Vec<BspRoomRect>,
173    /// Orthogonal corridor segments linking rooms.
174    pub(crate) corridors: Vec<BspCorridorSegment>,
175    /// Doorway tiles at room-corridor transitions.
176    pub(crate) doors: Vec<BspDoorway>,
177}
178
179/// Internal binary space partition tree node used during dungeon construction.
180#[derive(Clone, Debug)]
181pub(crate) struct BspLeafNode {
182    /// Partition region origin X.
183    pub(crate) x: usize,
184    /// Partition region origin Y.
185    pub(crate) y: usize,
186    /// Partition width.
187    pub(crate) width: usize,
188    /// Partition height.
189    pub(crate) height: usize,
190    /// Left child index (`usize::MAX` if leaf).
191    pub(crate) left: usize,
192    /// Right child index (`usize::MAX` if leaf).
193    pub(crate) right: usize,
194    /// Carved room index (`usize::MAX` if internal node).
195    pub(crate) room_index: usize,
196}
197
198/// Stateless Binary Space Partitioning dungeon, interior, and city-block layout generator.
199#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
200pub struct BspDungeonGenerator {
201    /// Active dungeon generation parameters.
202    #[get(type(copy))]
203    pub(crate) config: BspDungeonConfig,
204}