Skip to content

Commit 449ca23

Browse files
committed
prevent spirit light amounts snowballing out of control
1 parent 283bfc1 commit 449ca23

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

wotw_seedgen/src/generator/placement.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,20 @@ impl<'graph, 'settings> Context<'graph, 'settings> {
361361
let batch = origin_world
362362
.spirit_light_provider
363363
.take(spirit_light_placements_remaining);
364+
365+
spirit_light_placements_remaining -= 1;
366+
364367
(
365368
origin_world_index,
366369
compile::spirit_light((batch as i32).into(), &mut self.rng),
367370
)
368371
} else {
369372
let target_world_index = self.choose_target_world_for_random_placement();
373+
374+
if origin_world_index != target_world_index {
375+
spirit_light_placements_remaining -= 1;
376+
}
377+
370378
(
371379
target_world_index,
372380
self.worlds[target_world_index]
@@ -380,8 +388,6 @@ impl<'graph, 'settings> Context<'graph, 'settings> {
380388
self.place_command_at(command, name, node, origin_world_index, target_world_index);
381389

382390
placements_remaining -= 1;
383-
spirit_light_placements_remaining =
384-
spirit_light_placements_remaining.saturating_sub(1);
385391
}
386392
}
387393
any_placed

wotw_seedgen/src/generator/spirit_light.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use rand_pcg::Pcg64Mcg;
44
use std::mem;
55

66
const MIN_SPIRIT_LIGHT: f32 = 50.;
7+
const NOISE: f32 = 0.25;
78

8-
// TODO the last couple spirit light placements are weird
99
pub struct SpiritLightProvider {
1010
rng: Pcg64Mcg,
1111
pub amount: f32,
@@ -18,11 +18,10 @@ impl SpiritLightProvider {
1818
rng: Pcg64Mcg::from_rng(rng).expect(SEED_FAILED_MESSAGE),
1919
amount: amount as f32,
2020
next_amount: MIN_SPIRIT_LIGHT,
21-
noise: Uniform::new_inclusive(0.75, 1.25),
21+
noise: Uniform::new_inclusive(1. - NOISE, 1. + NOISE),
2222
}
2323
}
2424

25-
// TODO in a single world seed, the reported spirit_light_placements_remaining have some jumps, not sure that should be the case
2625
pub fn take(&mut self, spirit_light_placements_remaining: usize) -> usize {
2726
// For brevity, spirit_light_placements_remaining is referred to as remaining in this comment
2827
//
@@ -46,9 +45,12 @@ impl SpiritLightProvider {
4645
let a = (2. * self.amount / (remaining - 1.) - 2. * self.next_amount)
4746
/ (remaining + 1. - 2. * remaining);
4847
let b = self.next_amount - a * remaining;
49-
let mut next = a * (remaining - 1.) + b;
50-
self.amount -= self.next_amount;
51-
next *= self.rng.sample(self.noise);
52-
mem::replace(&mut self.next_amount, next).round() as usize
48+
let next = a * (remaining - 1.) + b;
49+
let mut amount = mem::replace(&mut self.next_amount, next);
50+
51+
// Apply random noise after setting the next amount to prevent snowballing
52+
amount = (amount * self.rng.sample(self.noise)).round();
53+
self.amount -= amount;
54+
amount as usize
5355
}
5456
}

0 commit comments

Comments
 (0)