aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/script_host.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-runtime/src/script_host.rs')
-rw-r--r--crates/fparkan-runtime/src/script_host.rs115
1 files changed, 115 insertions, 0 deletions
diff --git a/crates/fparkan-runtime/src/script_host.rs b/crates/fparkan-runtime/src/script_host.rs
new file mode 100644
index 00000000..d87f6348
--- /dev/null
+++ b/crates/fparkan-runtime/src/script_host.rs
@@ -0,0 +1,115 @@
+use crate::{
+ apply_recovered_script_host_callback, mission_events::MissionEvents, MissionMode,
+ MissionNotice, MissionScriptHostEffect, MissionScriptOutcome,
+};
+use fparkan_script::VmHostCallbackCommand;
+
+pub(super) fn apply(
+ mode: MissionMode,
+ status: &mut u32,
+ outcome: &mut MissionScriptOutcome,
+ events: &mut MissionEvents,
+ command: VmHostCallbackCommand,
+ notices: &mut Vec<MissionNotice>,
+) -> Result<(), String> {
+ if mode == MissionMode::AutoDemo {
+ return Ok(());
+ }
+ let outcome_command = match command.first {
+ 0 => Some(command),
+ 1 => {
+ // shortcut: present the first message until the native repeated-message UI is wired.
+ if events.voiced_messages.insert(command.second) {
+ notices.push(MissionNotice::Information(command.second));
+ }
+ None
+ }
+ 3..=5 => events
+ .objectives
+ .apply(command.first, command.second, notices)?
+ .then_some(VmHostCallbackCommand {
+ mode: 0,
+ first: 0,
+ second: 1,
+ }),
+ _ => return Err(format!("Handler(30) command {}", command.first)),
+ };
+ if let Some(command) = outcome_command {
+ match apply_recovered_script_host_callback(mode, status, outcome, command) {
+ MissionScriptHostEffect::MissionCompleted => notices.push(MissionNotice::Completed),
+ MissionScriptHostEffect::MissionFailed => notices.push(MissionNotice::Failed),
+ MissionScriptHostEffect::NoChange => {}
+ MissionScriptHostEffect::Unhandled => {
+ return Err(format!("Handler(30) outcome {}", command.second));
+ }
+ }
+ }
+ Ok(())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn information_is_presented_once_across_script_callers() {
+ let mut events = MissionEvents::default();
+ let mut status = 5;
+ let mut outcome = MissionScriptOutcome::Active;
+ let mut notices = Vec::new();
+ for index in [0, 0, 1, 0, 1] {
+ apply(
+ MissionMode::SingleMission,
+ &mut status,
+ &mut outcome,
+ &mut events,
+ VmHostCallbackCommand {
+ mode: 0,
+ first: 1,
+ second: index,
+ },
+ &mut notices,
+ )
+ .unwrap();
+ }
+ assert_eq!(
+ notices,
+ [MissionNotice::Information(0), MissionNotice::Information(1)]
+ );
+ assert_eq!(outcome, MissionScriptOutcome::Active);
+ assert_eq!(status, 5);
+ }
+
+ #[test]
+ fn autodemo_does_not_consume_an_information_message() {
+ let mut events = MissionEvents::default();
+ let mut status = 5;
+ let mut outcome = MissionScriptOutcome::Active;
+ let mut notices = Vec::new();
+ let command = VmHostCallbackCommand {
+ mode: 0,
+ first: 1,
+ second: 0,
+ };
+ apply(
+ MissionMode::AutoDemo,
+ &mut status,
+ &mut outcome,
+ &mut events,
+ command,
+ &mut notices,
+ )
+ .unwrap();
+ assert!(notices.is_empty());
+ apply(
+ MissionMode::SingleMission,
+ &mut status,
+ &mut outcome,
+ &mut events,
+ command,
+ &mut notices,
+ )
+ .unwrap();
+ assert_eq!(notices, [MissionNotice::Information(0)]);
+ }
+}