use std::cmp::Reverse;
use std::collections::BinaryHeap;
use log::info;
use serde::{Deserialize, Serialize};
use crate::trigger::{ScriptedAction, TriggerCondition};
#[cfg(test)]
const PLACEHOLDER_THRESHOLD: usize = 4;
#[cfg(not(test))]
const PLACEHOLDER_THRESHOLD: usize = 64;
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Scheduler {
pub heap: BinaryHeap<Reverse<(usize, usize)>>,
pub events: Vec<ScheduledEvent>,
}
impl Scheduler {
pub fn schedule_in(&mut self, now: usize, turns_ahead: usize, actions: Vec<ScriptedAction>, note: Option<String>) {
let idx = self.events.len();
let on_turn = now + turns_ahead;
let log_msg = match ¬e {
Some(msg) => msg.as_str(),
None => "<no note provided>",
};
info!("scheduling event (turn now/due = {now}/{on_turn}): \"{log_msg}\"");
self.heap.push(Reverse((on_turn, idx)));
self.events.push(ScheduledEvent {
on_turn,
actions,
note,
condition: None,
on_false: OnFalsePolicy::Cancel,
});
}
pub fn schedule_on(&mut self, on_turn: usize, actions: Vec<ScriptedAction>, note: Option<String>) {
let idx = self.events.len();
let log_msg = match ¬e {
Some(note) => note.as_str(),
None => "<no note provided>",
};
info!("scheduling event (turn due = {on_turn}): \"{log_msg}\"");
self.heap.push(Reverse((on_turn, idx)));
self.events.push(ScheduledEvent {
on_turn,
actions,
note,
condition: None,
on_false: OnFalsePolicy::Cancel,
});
}
pub fn schedule_in_if(
&mut self,
now: usize,
turns_ahead: usize,
condition: Option<EventCondition>,
on_false: OnFalsePolicy,
actions: Vec<ScriptedAction>,
note: Option<String>,
) {
let idx = self.events.len();
let on_turn = now + turns_ahead;
let log_msg = match ¬e {
Some(msg) => msg.as_str(),
None => "<no note provided>",
};
info!("scheduling conditional event (turn now/due = {now}/{on_turn}): \"{log_msg}\"");
self.heap.push(Reverse((on_turn, idx)));
self.events.push(ScheduledEvent {
on_turn,
actions,
note,
condition,
on_false,
});
}
pub fn schedule_on_if(
&mut self,
on_turn: usize,
condition: Option<EventCondition>,
on_false: OnFalsePolicy,
actions: Vec<ScriptedAction>,
note: Option<String>,
) {
let idx = self.events.len();
let log_msg = match ¬e {
Some(note) => note.as_str(),
None => "<no note provided>",
};
info!("scheduling conditional event (turn due = {on_turn}): \"{log_msg}\"");
self.heap.push(Reverse((on_turn, idx)));
self.events.push(ScheduledEvent {
on_turn,
actions,
note,
condition,
on_false,
});
}
pub fn pop_due(&mut self, now: usize) -> Option<ScheduledEvent> {
if let Some(Reverse((turn_due, idx))) = self.heap.peek().copied()
&& now >= turn_due
{
self.heap.pop();
let event = std::mem::take(&mut self.events[idx]);
self.compact_if_needed();
return Some(event);
}
None
}
fn compact_if_needed(&mut self) {
let placeholder_count = self.events.iter().filter(|e| e.is_placeholder()).count();
if placeholder_count > PLACEHOLDER_THRESHOLD {
let old_events = std::mem::take(&mut self.events);
let mut index_map = vec![0; old_events.len()];
for (old_idx, event) in old_events.into_iter().enumerate() {
if event.is_placeholder() {
continue;
}
let new_idx = self.events.len();
index_map[old_idx] = new_idx;
self.events.push(event);
}
let mut new_heap = BinaryHeap::with_capacity(self.heap.len());
while let Some(Reverse((turn_due, old_idx))) = self.heap.pop() {
let new_idx = index_map[old_idx];
new_heap.push(Reverse((turn_due, new_idx)));
}
self.heap = new_heap;
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct ScheduledEvent {
pub on_turn: usize,
pub actions: Vec<ScriptedAction>,
pub note: Option<String>,
pub condition: Option<EventCondition>,
pub on_false: OnFalsePolicy,
}
impl ScheduledEvent {
fn is_placeholder(&self) -> bool {
*self == ScheduledEvent::default()
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub enum OnFalsePolicy {
#[default]
Cancel,
RetryAfter(usize),
RetryNextTurn,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum EventCondition {
Trigger(TriggerCondition),
All(Vec<EventCondition>),
Any(Vec<EventCondition>),
}
impl EventCondition {
pub fn eval_with_events(&self, world: &crate::world::AmbleWorld, events: &[TriggerCondition]) -> bool {
match self {
EventCondition::Trigger(tc) => tc.matches_event_in(events) || tc.is_ongoing(world),
EventCondition::All(conds) => conds.iter().all(|c| c.eval_with_events(world, events)),
EventCondition::Any(conds) => conds.iter().any(|c| c.eval_with_events(world, events)),
}
}
pub fn eval(&self, world: &crate::world::AmbleWorld) -> bool {
self.eval_with_events(world, &[])
}
pub fn eval_ambient(&self, world: &crate::world::AmbleWorld) -> bool {
match self {
EventCondition::Trigger(tc) => match tc {
TriggerCondition::Ambient { room_ids, .. } => world
.player
.location
.room_id()
.is_ok_and(|room_id| room_ids.is_empty() || room_ids.contains(&room_id)),
_ => tc.is_ongoing(world),
},
EventCondition::All(conds) => conds.iter().all(|c| c.eval_ambient(world)),
EventCondition::Any(conds) => conds.iter().any(|c| c.eval_ambient(world)),
}
}
pub fn any_trigger<F>(&self, matcher: F) -> bool
where
F: FnMut(&TriggerCondition) -> bool,
{
let mut matcher = matcher;
self.any_trigger_inner(&mut matcher)
}
pub fn for_each_condition<F>(&self, visitor: F)
where
F: FnMut(&TriggerCondition),
{
let mut visitor = visitor;
self.for_each_trigger_inner(&mut visitor);
}
fn any_trigger_inner<F>(&self, matcher: &mut F) -> bool
where
F: FnMut(&TriggerCondition) -> bool,
{
match self {
EventCondition::Trigger(tc) => matcher(tc),
EventCondition::All(conds) | EventCondition::Any(conds) => {
conds.iter().any(|c| c.any_trigger_inner(matcher))
},
}
}
fn for_each_trigger_inner<F>(&self, visitor: &mut F)
where
F: FnMut(&TriggerCondition),
{
match self {
EventCondition::Trigger(tc) => visitor(tc),
EventCondition::All(conds) | EventCondition::Any(conds) => {
for cond in conds {
cond.for_each_trigger_inner(visitor);
}
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::trigger::TriggerAction;
fn create_test_action() -> ScriptedAction {
ScriptedAction::new(TriggerAction::ShowMessage("Test message".to_string()))
}
fn create_test_actions(count: usize) -> Vec<ScriptedAction> {
(0..count)
.map(|i| ScriptedAction::new(TriggerAction::ShowMessage(format!("Message {i}"))))
.collect()
}
#[test]
fn scheduler_new_is_empty() {
let scheduler = Scheduler::default();
assert!(scheduler.heap.is_empty());
assert!(scheduler.events.is_empty());
}
#[test]
fn schedule_in_adds_event_correctly() {
let mut scheduler = Scheduler::default();
let actions = vec![create_test_action()];
let note = Some("Test event".to_string());
scheduler.schedule_in(5, 3, actions.clone(), note.clone());
assert_eq!(scheduler.events.len(), 1);
assert_eq!(scheduler.heap.len(), 1);
let event = &scheduler.events[0];
assert_eq!(event.on_turn, 8); assert_eq!(event.actions.len(), 1);
assert_eq!(event.note, note);
}
#[test]
fn schedule_on_adds_event_correctly() {
let mut scheduler = Scheduler::default();
let actions = vec![create_test_action()];
let note = Some("Direct schedule test".to_string());
scheduler.schedule_on(10, actions.clone(), note.clone());
assert_eq!(scheduler.events.len(), 1);
assert_eq!(scheduler.heap.len(), 1);
let event = &scheduler.events[0];
assert_eq!(event.on_turn, 10);
assert_eq!(event.actions.len(), 1);
assert_eq!(event.note, note);
}
#[test]
fn schedule_multiple_events() {
let mut scheduler = Scheduler::default();
scheduler.schedule_in(0, 5, vec![create_test_action()], Some("Event 1".to_string()));
scheduler.schedule_in(0, 3, vec![create_test_action()], Some("Event 2".to_string()));
scheduler.schedule_on(10, vec![create_test_action()], Some("Event 3".to_string()));
assert_eq!(scheduler.events.len(), 3);
assert_eq!(scheduler.heap.len(), 3);
}
#[test]
fn pop_due_returns_none_when_nothing_due() {
let mut scheduler = Scheduler::default();
scheduler.schedule_in(5, 5, vec![create_test_action()], None);
let result = scheduler.pop_due(8); assert!(result.is_none());
assert_eq!(scheduler.heap.len(), 1); }
#[test]
fn pop_due_returns_event_when_due() {
let mut scheduler = Scheduler::default();
let actions = vec![create_test_action()];
let note = Some("Due event".to_string());
scheduler.schedule_in(5, 3, actions.clone(), note.clone());
let result = scheduler.pop_due(8); assert!(result.is_some());
let event = result.unwrap();
assert_eq!(event.on_turn, 8);
assert_eq!(event.note, note);
assert_eq!(event.actions.len(), 1);
assert!(scheduler.heap.is_empty());
}
#[test]
fn pop_due_returns_event_when_overdue() {
let mut scheduler = Scheduler::default();
scheduler.schedule_in(5, 3, vec![create_test_action()], Some("Overdue event".to_string()));
let result = scheduler.pop_due(10); assert!(result.is_some());
let event = result.unwrap();
assert_eq!(event.on_turn, 8);
}
#[test]
fn events_fire_in_correct_order() {
let mut scheduler = Scheduler::default();
scheduler.schedule_on(15, create_test_actions(1), Some("Third".to_string()));
scheduler.schedule_on(5, create_test_actions(1), Some("First".to_string()));
scheduler.schedule_on(10, create_test_actions(1), Some("Second".to_string()));
let first = scheduler.pop_due(5).unwrap();
assert_eq!(first.note, Some("First".to_string()));
assert_eq!(first.on_turn, 5);
let second = scheduler.pop_due(10).unwrap();
assert_eq!(second.note, Some("Second".to_string()));
assert_eq!(second.on_turn, 10);
let third = scheduler.pop_due(15).unwrap();
assert_eq!(third.note, Some("Third".to_string()));
assert_eq!(third.on_turn, 15);
assert!(scheduler.pop_due(20).is_none());
}
#[test]
fn events_with_same_turn_fire_in_fifo_order() {
let mut scheduler = Scheduler::default();
scheduler.schedule_on(10, create_test_actions(1), Some("First scheduled".to_string()));
scheduler.schedule_on(10, create_test_actions(1), Some("Second scheduled".to_string()));
scheduler.schedule_on(10, create_test_actions(1), Some("Third scheduled".to_string()));
let first = scheduler.pop_due(10).unwrap();
assert_eq!(first.note, Some("First scheduled".to_string()));
let second = scheduler.pop_due(10).unwrap();
assert_eq!(second.note, Some("Second scheduled".to_string()));
let third = scheduler.pop_due(10).unwrap();
assert_eq!(third.note, Some("Third scheduled".to_string()));
}
#[test]
fn pop_due_multiple_events_same_turn() {
let mut scheduler = Scheduler::default();
scheduler.schedule_on(5, create_test_actions(1), Some("Event A".to_string()));
scheduler.schedule_on(5, create_test_actions(1), Some("Event B".to_string()));
scheduler.schedule_on(10, create_test_actions(1), Some("Event C".to_string()));
let mut events_turn_5 = Vec::new();
while let Some(event) = scheduler.pop_due(5) {
if event.on_turn == 5 {
events_turn_5.push(event);
} else {
break;
}
}
assert_eq!(events_turn_5.len(), 2);
assert!(events_turn_5.iter().any(|e| e.note == Some("Event A".to_string())));
assert!(events_turn_5.iter().any(|e| e.note == Some("Event B".to_string())));
let event_c = scheduler.pop_due(10).unwrap();
assert_eq!(event_c.note, Some("Event C".to_string()));
}
#[test]
fn schedule_with_no_note() {
let mut scheduler = Scheduler::default();
scheduler.schedule_in(0, 5, vec![create_test_action()], None);
let event = scheduler.pop_due(5).unwrap();
assert_eq!(event.note, None);
}
#[test]
fn schedule_with_empty_actions() {
let mut scheduler = Scheduler::default();
scheduler.schedule_in(0, 5, vec![], Some("Empty actions".to_string()));
let event = scheduler.pop_due(5).unwrap();
assert!(event.actions.is_empty());
assert_eq!(event.note, Some("Empty actions".to_string()));
}
#[test]
fn schedule_with_multiple_actions() {
let mut scheduler = Scheduler::default();
let actions = create_test_actions(5);
scheduler.schedule_in(0, 3, actions.clone(), Some("Multi-action event".to_string()));
let event = scheduler.pop_due(3).unwrap();
assert_eq!(event.actions.len(), 5);
}
#[test]
fn scheduled_event_default() {
let event = ScheduledEvent::default();
assert_eq!(event.on_turn, 0);
assert!(event.actions.is_empty());
assert_eq!(event.note, None);
}
#[test]
fn mem_take_leaves_default_placeholder() {
let mut scheduler = Scheduler::default();
scheduler.schedule_in(0, 5, vec![create_test_action()], Some("Test".to_string()));
let _event = scheduler.pop_due(5).unwrap();
assert_eq!(scheduler.events.len(), 1);
let placeholder = &scheduler.events[0];
assert_eq!(placeholder.on_turn, 0);
assert!(placeholder.actions.is_empty());
assert_eq!(placeholder.note, None);
}
#[test]
fn compact_events_when_placeholder_threshold_exceeded() {
let mut scheduler = Scheduler::default();
for i in 1..=6 {
scheduler.schedule_on(i, create_test_actions(1), Some(format!("Event {i}")));
}
for turn in 1..=5 {
let ev = scheduler.pop_due(turn).unwrap();
assert_eq!(ev.note, Some(format!("Event {turn}")));
}
assert_eq!(scheduler.events.len(), 1);
assert_eq!(scheduler.heap.len(), 1);
assert_eq!(scheduler.events[0].note, Some("Event 6".to_string()));
let final_event = scheduler.pop_due(6).unwrap();
assert_eq!(final_event.note, Some("Event 6".to_string()));
}
#[test]
fn edge_case_turn_zero() {
let mut scheduler = Scheduler::default();
scheduler.schedule_on(0, vec![create_test_action()], Some("Turn zero".to_string()));
let event = scheduler.pop_due(0).unwrap();
assert_eq!(event.on_turn, 0);
}
#[test]
fn edge_case_large_turn_numbers() {
let mut scheduler = Scheduler::default();
let large_turn = usize::MAX - 1000;
scheduler.schedule_on(large_turn, vec![create_test_action()], Some("Large turn".to_string()));
let event = scheduler.pop_due(large_turn).unwrap();
assert_eq!(event.on_turn, large_turn);
}
#[test]
fn serialization_roundtrip() {
let mut scheduler = Scheduler::default();
scheduler.schedule_in(5, 10, create_test_actions(3), Some("Serialization test".to_string()));
scheduler.schedule_on(20, create_test_actions(2), None);
let serialized = serde_json::to_string(&scheduler).expect("Failed to serialize");
let deserialized: Scheduler = serde_json::from_str(&serialized).expect("Failed to deserialize");
assert_eq!(deserialized.events.len(), scheduler.events.len());
assert_eq!(deserialized.heap.len(), scheduler.heap.len());
let mut des_scheduler = deserialized;
let event1 = des_scheduler.pop_due(15).unwrap();
assert_eq!(event1.on_turn, 15);
assert_eq!(event1.actions.len(), 3);
let event2 = des_scheduler.pop_due(20).unwrap();
assert_eq!(event2.on_turn, 20);
assert_eq!(event2.actions.len(), 2);
assert_eq!(event2.note, None);
}
}