Mushroom groves: unclaimable moss where giant mushrooms grow slowly and glow when ready to fell

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Richard ZinkandClaude Opus 5.5 committed 2026-11-06 10:25:24 +01:00
1 parent 368a338ccf
commit f8eda88bb3
26 files changed
+321 -43

No files matched your search

+121 -33
View File
@@ -16,62 +16,106 @@ pub const WOOD_TREE: f32 = 3.0;
pub const MUSHROOM_HP: f32 = 4.0;
#[allow(dead_code)] // trees come with the surface world
pub const TREE_HP: f32 = 6.0;
// seconds until a felled giant mushroom grows back
pub const REGROW: f32 = 240.0;
// seconds until a felled giant mushroom sprouts again, and from sprout to fully grown
pub const REGROW: f32 = 90.0;
pub const GROW_TIME: f32 = 300.0;
// wood a wooden bridge tile, a wooden drawbridge tile and a wooden door cost
pub const BRIDGE_WOOD: f32 = 2.0;
pub const DRAWBRIDGE_WOOD: f32 = 4.0;
pub const DOOR_WOOD: f32 = 3.0;
// giant mushrooms in a cavern of a random map: one per this many open tiles
const MUSHROOM_SHARE: usize = 70;
// mushroom groves of a random map: one per this many map tiles (at least 2, at most 6)
const GROVE_SHARE: usize = 2200;
const GROVE_RADIUS: i32 = 4;
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct WoodWork {
pub regrow: Vec<(usize, f32)>, // felled mushroom tile, seconds until it grows back
pub regrow: Vec<(usize, f32)>, // felled mushroom tile, seconds until it sprouts again
#[serde(default)]
pub growing: Vec<usize>, // giant mushrooms still growing (Tile::food: growth left)
}
// a tile where a plant stands alone: all eight neighbours open ground, no other plant next to
// it (so a plant never closes a corridor)
// open ground of nobody (or a grove's moss)
fn open_ground(t: &Tile) -> bool {
(t.kind == Kind::Floor && t.owner == Owner::None) || t.kind == Kind::Grove
}
// a tile where a plant may stand: all eight neighbours open, no other plant next to it (so a
// plant never closes a corridor)
fn lonely_spot(map: &Map, x: i32, y: i32) -> bool {
if !inb(x, y) || map.t[idx(x, y)].kind != Kind::Floor || map.t[idx(x, y)].owner != Owner::None {
if !inb(x, y) || !open_ground(&map.t[idx(x, y)]) {
return false;
}
DIR8.iter().all(|&(dx, dy)| map.get(x + dx, y + dy).map_or(false, |n| n.walkable() && !n.bridge() && n.kind != Kind::Door))
&& (-2..=2).all(|dy| (-2..=2).all(|dx| !map.get(x + dx, y + dy).map_or(false, |n| n.plant())))
DIR8.iter().all(|&(dx, dy)| map.get(x + dx, y + dy).map_or(false, |n| n.walkable() && !n.bridge() && n.kind != Kind::Door && !n.plant()))
}
impl Game {
// giant mushrooms in the open caverns of a random map (deterministic, from the seed)
// mushroom groves in the open caverns of a random map (deterministic, from the seed): mossy
// ground nobody can claim, giant mushrooms in a loose grid that grow back when felled
pub fn grow_mushrooms(&mut self, seed: u32) {
let mut rng = Rng(seed.wrapping_mul(0x9e37_79b9) ^ 0x6d2b_79f5 | 1);
let (cx, cy) = (self.map.core.0 + 1, self.map.core.1 + 1);
let open: Vec<usize> = (0..self.map.t.len())
let open = |g: &Game, x: i32, y: i32| inb(x, y) && !g.map.sky(idx(x, y)) && open_ground(&g.map.t[idx(x, y)]) && !g.map.t[idx(x, y)].bridge();
// centres: open ground with open ground all around, away from the heart
let r = GROVE_RADIUS;
let centres: Vec<usize> = (0..self.map.t.len())
.filter(|&i| {
let (x, y) = xy(i);
(x - cx).abs().max((y - cy).abs()) > 10 && lonely_spot(&self.map, x, y)
(x - cx).abs().max((y - cy).abs()) > 12 && (-2..=2).all(|dy| (-2..=2).all(|dx| open(self, x + dx, y + dy)))
})
.collect();
let want = (open.len() / MUSHROOM_SHARE).max(6);
let mut placed = 0;
let want = (self.map.t.len() / GROVE_SHARE).clamp(2, 6);
let mut groves: Vec<(i32, i32)> = Vec::new();
let mut tries = 0;
while placed < want && tries < want * 20 && !open.is_empty() {
while groves.len() < want && tries < 200 && !centres.is_empty() {
tries += 1;
let i = open[(rng.next() as usize) % open.len()];
let (x, y) = xy(i);
if self.ents.iter().any(|e| e.alive && e.tile() == i) || !lonely_spot(&self.map, x, y) {
let (x, y) = xy(centres[(rng.next() as usize) % centres.len()]);
if groves.iter().any(|&(gx, gy)| (gx - x).abs().max((gy - y).abs()) < 3 * r) {
continue;
}
self.plant_mushroom(i);
placed += 1;
groves.push((x, y));
}
for &(gx, gy) in &groves {
// the moss: the open ground of a rough disc
for y in gy - r..=gy + r {
for x in gx - r..=gx + r {
let d2 = (x - gx).pow(2) + (y - gy).pow(2);
if d2 <= r * r + (hash32(x, y) % 3) as i32 - 1 && open(self, x, y) {
let t = &mut self.map.t[idx(x, y)];
t.kind = Kind::Grove;
t.owner = Owner::None;
}
}
}
// the mushrooms: every other tile in both directions, some still growing
for y in gy - r + 1..gy + r {
for x in gx - r + 1..gx + r {
if (x - gx) % 2 != 0 || (y - gy) % 2 != 0 {
continue;
}
let i = idx(x, y);
if self.map.t[i].kind != Kind::Grove || !lonely_spot(&self.map, x, y) || self.ents.iter().any(|e| e.alive && e.tile() == i) {
continue;
}
let left = if rng.f() < 0.6 { 0.0 } else { rng.f() };
self.plant_mushroom(i, left);
}
}
}
}
pub fn plant_mushroom(&mut self, i: usize) {
// a giant mushroom on tile i; `left`: how much of its growth is still to come (0 = grown)
pub fn plant_mushroom(&mut self, i: usize, left: f32) {
let grove = self.map.t[i].kind == Kind::Grove || self.map.t[i].res & MUSH_GROVE != 0;
let t = &mut self.map.t[i];
t.kind = Kind::Mushroom;
t.owner = Owner::None;
t.hp = MUSHROOM_HP;
t.marked = false;
t.food = left.clamp(0.0, 1.0);
t.res = if grove { MUSH_GROVE } else { 0 };
if t.food > 0.0 && !self.woodwork.growing.contains(&i) {
self.woodwork.growing.push(i);
}
}
// an imp felled the plant at tile i: wood for its master, a mushroom grows back later
@@ -96,8 +140,23 @@ impl Game {
true
}
// the shared step: felled mushrooms grow back where there is room
// the shared step: young mushrooms grow, felled ones sprout again where there is room
pub(crate) fn update_wood(&mut self, dt: f32) {
let mut k = 0;
while k < self.woodwork.growing.len() {
let i = self.woodwork.growing[k];
let t = &mut self.map.t[i];
if t.kind != Kind::Mushroom {
self.woodwork.growing.swap_remove(k);
continue;
}
t.food = (t.food - dt / GROW_TIME).max(0.0);
if t.food <= 0.0 {
self.woodwork.growing.swap_remove(k);
} else {
k += 1;
}
}
let mut k = 0;
while k < self.woodwork.regrow.len() {
self.woodwork.regrow[k].1 -= dt;
@@ -108,9 +167,10 @@ impl Game {
let i = self.woodwork.regrow[k].0;
let (x, y) = xy(i);
if lonely_spot(&self.map, x, y) && !self.ents.iter().any(|e| e.alive && e.tile() == i) {
self.plant_mushroom(i);
// a sprout: it takes GROW_TIME until it can be felled again
self.plant_mushroom(i, 1.0);
self.woodwork.regrow.swap_remove(k);
} else if self.map.t[i].kind != Kind::Floor || self.map.t[i].owner != Owner::None {
} else if !open_ground(&self.map.t[i]) {
// built over or claimed: it does not come back
self.woodwork.regrow.swap_remove(k);
} else {
@@ -121,37 +181,65 @@ impl Game {
}
}
fn hash32(x: i32, y: i32) -> u32 {
let h = (x as u32).wrapping_mul(2654435761) ^ (y as u32).wrapping_mul(2246822519);
(h ^ (h >> 15)).wrapping_mul(2246822519) >> 7
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn random_maps_have_lonely_giant_mushrooms() {
fn random_maps_have_mushroom_groves() {
let g = Game::new(9, 96, 96);
let groves = g.map.t.iter().filter(|t| t.kind == Kind::Grove).count();
let n = g.map.t.iter().filter(|t| t.kind == Kind::Mushroom).count();
assert!(groves >= 30, "{} grove tiles", groves);
assert!(n >= 6, "{} mushrooms", n);
assert!((g.wood - WOOD_START).abs() < 1e-3);
// none closes a corridor: every neighbour is open ground
// none closes a corridor: every neighbour is open ground; all stand in a grove
for i in 0..g.map.t.len() {
if g.map.t[i].kind == Kind::Mushroom {
let (x, y) = xy(i);
assert!(DIR8.iter().all(|&(dx, dy)| g.map.get(x + dx, y + dy).map_or(false, |n| n.walkable() || n.plant())));
assert!(DIR8.iter().all(|&(dx, dy)| g.map.get(x + dx, y + dy).map_or(false, |n| n.walkable())));
assert!(g.map.t[i].res & MUSH_GROVE != 0);
}
}
}
#[test]
fn felling_gives_wood_and_mushrooms_grow_back() {
fn grove_mushrooms_grow_slowly_are_felled_only_when_grown_and_come_back() {
let mut g = Game::new(9, 96, 96);
let i = g.map.t.iter().position(|t| t.kind == Kind::Mushroom).unwrap();
let i = g.map.t.iter().position(|t| t.kind == Kind::Mushroom && t.grown()).unwrap();
assert!(g.map.t[i].diggable());
let w0 = g.wood;
g.felled(i);
g.dig_out(i);
assert_eq!(g.wood, w0 + WOOD_MUSHROOM);
assert_eq!(g.map.t[i].kind, Kind::Floor);
g.woodwork.regrow[0].1 = 0.01;
assert_eq!(g.map.t[i].kind, Kind::Grove, "the moss stays");
// the grove can't be claimed or marked
let (x, y) = xy(i);
g.mark_rect(x, y, x, y, true);
assert!(!g.map.t[i].marked);
assert!(!g.claimable(i, 1));
// it sprouts again: small, and not to be felled yet
let idx_r = g.woodwork.regrow.iter().position(|r| r.0 == i).unwrap();
g.woodwork.regrow[idx_r].1 = 0.01;
g.update_wood(0.05);
assert_eq!(g.map.t[i].kind, Kind::Mushroom, "grown back");
assert_eq!(g.map.t[i].kind, Kind::Mushroom, "a sprout");
assert!(!g.map.t[i].grown() && !g.map.t[i].diggable());
g.mark_rect(x, y, x, y, true);
assert!(!g.map.t[i].marked, "a young mushroom can't be marked");
// halfway
for _ in 0..(GROW_TIME as usize / 2) {
g.update_wood(1.0);
}
assert!(!g.map.t[i].grown() && g.map.t[i].growth() > 0.4);
for _ in 0..(GROW_TIME as usize / 2 + 2) {
g.update_wood(1.0);
}
assert!(g.map.t[i].grown() && g.map.t[i].diggable(), "fully grown: ready to fell");
assert!(g.spend_wood(3.0) && !g.spend_wood(1000.0));
}
}