use super::{CASCADES, LIGHT_SLOTS, TILE_LEVELS, TILES};
pub const GROUPS: usize = CASCADES + LIGHT_SLOTS;
const TIERS: usize = if TILE_LEVELS > CASCADES {
TILE_LEVELS
} else {
CASCADES
};
crcbl_console::convar! {
pub static r_shadow_cadence: i64 in 1 ..= 8 = 1;
}
crcbl_console::convar! {
pub static r_shadow_faces: i64 in 1 ..= 16 = 16;
}
const _: () = assert!(
TILES == 16,
"r_shadow_faces' range is the atlas's tile count spelled out, because a \
console variable's bounds are part of its declaration and cannot be an \
expression; an atlas of a different size needs that literal moved with it"
);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Group {
pub faces: usize,
pub tier: usize,
pub forced: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Cadence {
pub hold: u32,
pub faces: usize,
}
impl Cadence {
pub const EVERY_FRAME: Self = Self {
hold: 1,
faces: TILES,
};
#[must_use]
pub fn from_console() -> Self {
let hold = r_shadow_cadence.get_i64().clamp(1, 8);
let faces = r_shadow_faces.get_i64().clamp(1, TILES as i64);
Self {
hold: u32::try_from(hold).unwrap_or(1),
faces: usize::try_from(faces).unwrap_or(TILES),
}
}
fn period(self, tier: usize) -> u32 {
let tier = tier.min(TIERS - 1);
let rung = 1u32 << tier;
rung.min(self.hold.max(1))
}
#[must_use]
pub fn schedule(self, frame: u64, wanted: &[Option<Group>; GROUPS]) -> [bool; GROUPS] {
let mut due = [(0usize, 0usize, 0usize); GROUPS];
let mut count = 0;
for (group, want) in wanted.iter().enumerate() {
let Some(want) = want else {
continue;
};
let period = u64::from(self.period(want.tier));
if !want.forced && !(frame + group as u64).is_multiple_of(period) {
continue;
}
let rotated = (group + (frame % GROUPS as u64) as usize) % GROUPS;
due[count] = (usize::from(!want.forced), rotated, group);
count += 1;
}
due[..count].sort_unstable();
let mut redraw = [false; GROUPS];
let mut spent = 0;
for &(_, _, group) in &due[..count] {
let faces = wanted[group].map_or(0, |want| want.faces);
if spent + faces > self.faces {
continue;
}
spent += faces;
redraw[group] = true;
}
if spent == 0
&& let Some(&(_, _, group)) = due[..count].first()
{
redraw[group] = true;
}
redraw
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::shadow::POINT_FACES;
fn all_on(tier: usize) -> [Option<Group>; GROUPS] {
[Some(Group {
faces: 1,
tier,
forced: false,
}); GROUPS]
}
#[test]
fn the_shipped_limits_redraw_every_group_that_wants_it() {
let cadence = Cadence {
hold: match r_shadow_cadence.default() {
crcbl_console::Value::Int(hold) => u32::try_from(*hold).expect("a small count"),
other => panic!("`r_shadow_cadence` is declared as {other:?}"),
},
faces: match r_shadow_faces.default() {
crcbl_console::Value::Int(faces) => usize::try_from(*faces).expect("a small count"),
other => panic!("`r_shadow_faces` is declared as {other:?}"),
},
};
assert_eq!(
cadence,
Cadence::EVERY_FRAME,
"the console's defaults are the schedule that shipped"
);
for frame in 0..8 {
for tier in 0..TIERS {
assert_eq!(
cadence.schedule(frame, &all_on(tier)),
[true; GROUPS],
"frame {frame}, tier {tier}"
);
}
}
}
#[test]
fn the_console_moves_both_limits() {
let _guard = CONSOLE
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
r_shadow_cadence
.set(&crcbl_console::Value::Int(4))
.expect("`r_shadow_cadence` is a writable int in range");
r_shadow_faces
.set(&crcbl_console::Value::Int(3))
.expect("`r_shadow_faces` is a writable int in range");
let moved = Cadence::from_console();
r_shadow_cadence
.set(r_shadow_cadence.default())
.expect("back to what it was declared holding");
r_shadow_faces
.set(r_shadow_faces.default())
.expect("back to what it was declared holding");
assert_eq!(moved, Cadence { hold: 4, faces: 3 });
assert_eq!(
Cadence::from_console(),
Cadence::EVERY_FRAME,
"and the restore above put the process back where it found it"
);
}
static CONSOLE: std::sync::Mutex<()> = std::sync::Mutex::new(());
#[test]
fn a_group_that_wants_nothing_is_never_redrawn() {
let cadence = Cadence {
hold: 8,
faces: TILES,
};
let mut wanted = all_on(0);
wanted[2] = None;
for frame in 0..16 {
assert!(
!cadence.schedule(frame, &wanted)[2],
"frame {frame} redrew a group that holds what it should"
);
}
}
#[test]
fn each_rung_of_the_ladder_halves_how_often_a_group_is_redrawn() {
let cadence = Cadence {
hold: 8,
faces: TILES,
};
const FRAMES: u64 = 64;
let counts: Vec<usize> = (0..TIERS)
.map(|tier| {
(0..FRAMES)
.filter(|frame| cadence.schedule(*frame, &all_on(tier))[0])
.count()
})
.collect();
for (tier, drawn) in counts.iter().enumerate() {
let expected = FRAMES as usize >> tier;
assert_eq!(
*drawn, expected,
"tier {tier} was redrawn {drawn} times in {FRAMES} frames"
);
}
}
#[test]
fn the_hold_caps_the_period_the_ladder_asks_for() {
let cadence = Cadence {
hold: 2,
faces: TILES,
};
for tier in 1..TIERS {
assert_eq!(cadence.period(tier), 2, "tier {tier}");
}
assert_eq!(cadence.period(0), 1, "the top of the ladder is every frame");
}
#[test]
fn a_frame_never_redraws_more_tiles_than_the_budget() {
let cadence = Cadence { hold: 1, faces: 7 };
let mut wanted = [None; GROUPS];
wanted[0] = Some(Group {
faces: 1,
tier: 0,
forced: false,
});
wanted[1] = Some(Group {
faces: 1,
tier: 0,
forced: false,
});
wanted[2] = Some(Group {
faces: 6,
tier: 0,
forced: false,
});
wanted[3] = Some(Group {
faces: 6,
tier: 0,
forced: false,
});
for frame in 0..8 {
let redraw = cadence.schedule(frame, &wanted);
let spent: usize = redraw
.iter()
.enumerate()
.filter(|(_, drawn)| **drawn)
.filter_map(|(group, _)| wanted[group])
.map(|want| want.faces)
.sum();
assert!(spent <= 7, "frame {frame} redrew {spent} tiles");
}
}
#[test]
fn a_group_the_budget_refuses_does_not_take_the_ones_behind_it_with_it() {
let cadence = Cadence { hold: 1, faces: 2 };
let mut wanted = [None; GROUPS];
wanted[0] = Some(Group {
faces: 6,
tier: 0,
forced: false,
});
wanted[1] = Some(Group {
faces: 1,
tier: 0,
forced: false,
});
wanted[2] = Some(Group {
faces: 1,
tier: 0,
forced: false,
});
let redraw = cadence.schedule(0, &wanted);
assert!(!redraw[0], "six tiles do not fit in a budget of two");
assert!(
redraw[1] && redraw[2],
"the two that do fit were refused with it: {redraw:?}"
);
}
#[test]
fn a_budget_smaller_than_every_map_still_draws_one_of_them() {
let cadence = Cadence { hold: 1, faces: 1 };
let mut wanted = [None; GROUPS];
wanted[0] = Some(Group {
faces: POINT_FACES,
tier: 0,
forced: false,
});
wanted[3] = Some(Group {
faces: POINT_FACES,
tier: 0,
forced: false,
});
for frame in 0..8 {
let redraw = cadence.schedule(frame, &wanted);
assert_eq!(
redraw.iter().filter(|drawn| **drawn).count(),
1,
"frame {frame} drew {redraw:?} against a budget no cube fits in"
);
}
}
#[test]
fn every_group_that_keeps_asking_is_redrawn_within_a_bounded_run() {
let cadence = Cadence { hold: 1, faces: 2 };
let wanted = all_on(0);
let mut last = [None; GROUPS];
for frame in 0..64u64 {
for (group, drawn) in cadence.schedule(frame, &wanted).iter().enumerate() {
if *drawn {
last[group] = Some(frame);
}
}
}
for (group, seen) in last.iter().enumerate() {
let seen = seen.unwrap_or_else(|| panic!("group {group} was never redrawn"));
assert!(
seen + u64::try_from(GROUPS).expect("a handful of groups") >= 63,
"group {group} was last redrawn at frame {seen} of 63"
);
}
}
#[test]
fn a_forced_group_is_due_on_every_frame_however_coarse_its_tier() {
let cadence = Cadence {
hold: 8,
faces: TILES,
};
let mut wanted = [None; GROUPS];
wanted[0] = Some(Group {
faces: 1,
tier: TIERS - 1,
forced: true,
});
for frame in 0..16 {
assert!(
cadence.schedule(frame, &wanted)[0],
"frame {frame} held a map that is about somewhere else"
);
}
}
#[test]
fn a_forced_group_takes_the_budget_before_one_that_is_only_stale() {
let cadence = Cadence { hold: 1, faces: 1 };
let mut wanted = [None; GROUPS];
wanted[0] = Some(Group {
faces: 1,
tier: 0,
forced: false,
});
wanted[5] = Some(Group {
faces: 1,
tier: 0,
forced: true,
});
let redraw = cadence.schedule(0, &wanted);
assert!(
redraw[5],
"the forced group was refused the only tile spare"
);
assert!(!redraw[0], "the budget of one was spent twice: {redraw:?}");
}
#[test]
fn one_frame_index_gives_one_answer() {
let cadence = Cadence { hold: 4, faces: 3 };
let wanted = all_on(1);
for frame in 0..32 {
let first = cadence.schedule(frame, &wanted);
for _ in 0..4 {
assert_eq!(cadence.schedule(frame, &wanted), first, "frame {frame}");
}
}
}
}