use crate::flags::impl_flags;
use alloc::rc::Rc;
use core::{mem, ptr};
use crate::basic_types::Vector3;
use crate::colors::RgbColor;
use crate::events::{Event, EventList, EventListPlayer};
use crate::lights::{LightPlayer, LightProgram};
use crate::screenplay::{Screenplay, ScreenplayScene};
use crate::trajectory::{Trajectory, TrajectoryPlayer};
use crate::yaw_control::{YawControl, YawPlayer};
const MAX_LIGHT_TIME_SEC: f32 = 86_400.0;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct ControlComponents(u8);
impl ControlComponents {
pub const NONE: Self = Self(0);
pub const POSITION: Self = Self(1);
pub const VELOCITY: Self = Self(2);
pub const LIGHTS: Self = Self(4);
pub const YAW: Self = Self(8);
pub const YAW_RATE: Self = Self(16);
pub const ALL: Self = Self(31);
}
impl_flags!(ControlComponents);
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ControlOutput {
mask: ControlComponents,
position: Vector3,
velocity: Vector3,
color: RgbColor,
yaw: f32,
yaw_rate: f32,
}
impl ControlOutput {
#[must_use]
pub const fn new() -> Self {
Self {
mask: ControlComponents::NONE,
position: Vector3::ZERO,
velocity: Vector3::ZERO,
color: RgbColor::BLACK,
yaw: 0.0,
yaw_rate: 0.0,
}
}
#[must_use]
pub const fn components(&self) -> ControlComponents {
self.mask
}
pub const fn clear(&mut self) {
self.mask = ControlComponents::NONE;
}
pub const fn clear_components(&mut self, components: ControlComponents) {
self.mask.remove(components);
}
#[must_use]
pub const fn has_any_component_in(&self, components: ControlComponents) -> bool {
self.mask.intersects(components)
}
#[must_use]
pub const fn has_all_components_in(&self, components: ControlComponents) -> bool {
self.mask.contains(components)
}
#[must_use]
pub const fn position(&self) -> Option<Vector3> {
self.component(ControlComponents::POSITION, self.position)
}
#[must_use]
pub const fn velocity(&self) -> Option<Vector3> {
self.component(ControlComponents::VELOCITY, self.velocity)
}
#[must_use]
pub const fn color(&self) -> Option<RgbColor> {
self.component(ControlComponents::LIGHTS, self.color)
}
#[must_use]
pub const fn yaw(&self) -> Option<f32> {
self.component(ControlComponents::YAW, self.yaw)
}
#[must_use]
pub const fn yaw_rate(&self) -> Option<f32> {
self.component(ControlComponents::YAW_RATE, self.yaw_rate)
}
pub const fn set_position(&mut self, position: Vector3) {
self.position = position;
self.mask.insert(ControlComponents::POSITION);
}
pub const fn set_velocity(&mut self, velocity: Vector3) {
self.velocity = velocity;
self.mask.insert(ControlComponents::VELOCITY);
}
pub const fn set_color(&mut self, color: RgbColor) {
self.color = color;
self.mask.insert(ControlComponents::LIGHTS);
}
pub const fn set_yaw(&mut self, yaw: f32) {
self.yaw = yaw;
self.mask.insert(ControlComponents::YAW);
}
pub const fn set_yaw_rate(&mut self, yaw_rate: f32) {
self.yaw_rate = yaw_rate;
self.mask.insert(ControlComponents::YAW_RATE);
}
const fn component<T: Copy>(&self, component: ControlComponents, value: T) -> Option<T> {
if self.has_any_component_in(component) {
Some(value)
} else {
None
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ControlOutputTime {
pub time_msec: u32,
pub scene: Option<usize>,
pub time_in_scene_msec: u32,
pub warped_time_in_scene_sec: f32,
}
#[derive(Debug)]
struct Players<'a> {
trajectory: Option<TrajectoryPlayer<Rc<Trajectory<'a>>>>,
light: Option<LightPlayer<Rc<LightProgram<'a>>>>,
yaw: Option<YawPlayer<Rc<YawControl<'a>>>>,
events: Option<EventListPlayer<Rc<EventList>>>,
}
impl<'a> Players<'a> {
const fn none() -> Self {
Self {
trajectory: None,
light: None,
yaw: None,
events: None,
}
}
fn sync(&mut self, scene: &ScreenplayScene<'a>) {
sync_player(
&mut self.trajectory,
scene.trajectory(),
TrajectoryPlayer::trajectory,
TrajectoryPlayer::new,
);
sync_player(
&mut self.light,
scene.light_program(),
LightPlayer::program,
LightPlayer::new,
);
sync_player(
&mut self.yaw,
scene.yaw_control(),
YawPlayer::yaw_control,
YawPlayer::new,
);
sync_player(
&mut self.events,
scene.events(),
EventListPlayer::events,
EventListPlayer::new,
);
}
}
fn sync_player<P, C>(
player: &mut Option<P>,
component: Option<&Rc<C>>,
played: impl Fn(&P) -> &C,
new: impl FnOnce(Rc<C>) -> P,
) {
let up_to_date = match (player.as_ref(), component) {
(Some(player), Some(component)) => ptr::eq(played(player), Rc::as_ptr(component)),
(None, None) => true,
_ => false,
};
if !up_to_date {
*player = component.map(|component| new(Rc::clone(component)));
}
}
#[derive(Debug)]
pub struct ShowController<'a> {
screenplay: Screenplay<'a>,
current_scene: Option<usize>,
players: Players<'a>,
default_output: ControlOutput,
output: ControlOutput,
output_time: Option<ControlOutputTime>,
reached_end: bool,
}
impl<'a> ShowController<'a> {
#[must_use]
pub fn new(screenplay: Screenplay<'a>) -> Self {
let mut default_output = ControlOutput::new();
default_output.set_velocity(Vector3::ZERO);
default_output.set_yaw_rate(0.0);
Self {
screenplay,
current_scene: None,
players: Players::none(),
default_output,
output: default_output,
output_time: None,
reached_end: false,
}
}
#[must_use]
pub const fn screenplay(&self) -> &Screenplay<'a> {
&self.screenplay
}
pub fn screenplay_mut(&mut self) -> &mut Screenplay<'a> {
self.invalidate_output();
&mut self.screenplay
}
pub fn set_screenplay(&mut self, screenplay: Screenplay<'a>) -> Screenplay<'a> {
mem::replace(self.screenplay_mut(), screenplay)
}
#[must_use]
pub fn into_screenplay(self) -> Screenplay<'a> {
self.screenplay
}
#[must_use]
pub fn current_scene(&self) -> Option<&ScreenplayScene<'a>> {
self.screenplay.scene(self.current_scene?)
}
#[must_use]
pub const fn current_output(&self) -> &ControlOutput {
&self.output
}
#[must_use]
pub const fn current_output_time(&self) -> Option<ControlOutputTime> {
self.output_time
}
#[must_use]
pub const fn has_reached_end(&self) -> bool {
self.reached_end
}
#[must_use]
pub const fn is_output_valid(&self) -> bool {
self.output_time.is_some()
}
pub fn update_time_msec(&mut self, time_msec: u32) {
if self
.output_time
.is_some_and(|output_time| output_time.time_msec == time_msec)
{
return;
}
let found = i32::try_from(time_msec)
.ok()
.and_then(|wall_clock_time_msec| {
let (index, scene) = self.screenplay.scene_at_time_msec(time_msec)?;
Some((index, scene, wall_clock_time_msec))
});
let scene_index = found.map(|(index, _, _)| index);
if scene_index != self.current_scene {
self.players = Players::none();
self.current_scene = scene_index;
}
self.output = self.default_output;
self.output_time = None;
self.reached_end = false;
let Some((_, scene, wall_clock_time_msec)) = found else {
self.output = self.default_output;
self.reached_end = true;
self.output_time = Some(ControlOutputTime {
time_msec,
scene: None,
time_in_scene_msec: 0,
warped_time_in_scene_sec: 0.0,
});
return;
};
self.players.sync(scene);
let time_in_scene_msec = time_msec.wrapping_sub_signed(scene.origin_msec());
let (warped_time_sec, warped_rate) = scene.time_axis().map_with_rate(wall_clock_time_msec);
let output = &mut self.output;
output.clear();
if let Some(player) = &mut self.players.trajectory {
output.set_position(player.position_at(warped_time_sec).xyz());
output.set_velocity(player.velocity_at(warped_time_sec).xyz() * warped_rate);
}
if let Some(player) = &mut self.players.light {
let light_time_sec = if warped_time_sec < 0.0 || !warped_time_sec.is_finite() {
0.0
} else {
warped_time_sec.min(MAX_LIGHT_TIME_SEC)
};
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "a time between zero and a day, truncated as in the C library"
)]
let light_time_msec = (light_time_sec * 1000.0) as u64;
output.set_color(player.color_at(light_time_msec));
}
if let Some(player) = &mut self.players.yaw {
output.set_yaw(player.yaw_at(warped_time_sec));
output.set_yaw_rate(player.yaw_rate_at(warped_time_sec) * warped_rate);
}
self.output_time = Some(ControlOutputTime {
time_msec,
scene: scene_index,
time_in_scene_msec,
warped_time_in_scene_sec: warped_time_sec,
});
}
pub fn next_event(&mut self) -> Option<&Event> {
let time_sec = self
.output_time
.map_or(0.0, |output_time| output_time.warped_time_in_scene_sec);
self.players
.events
.as_mut()?
.next_event_not_later_than(time_sec)
}
fn invalidate_output(&mut self) {
self.output = self.default_output;
self.output_time = None;
self.reached_end = false;
}
}