From b4ff95c3bed03bcc7fd0eca04990c6f2be61a82f Mon Sep 17 00:00:00 2001 From: hut Date: Sun, 14 Apr 2024 17:29:35 +0200 Subject: [PATCH] WIP if-branches in chats --- src/chat.rs | 20 ++++++++++++++++++-- src/chats/serenity.yaml | 6 ++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index 28fc22d..e219fa3 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -18,6 +18,7 @@ pub const TOKEN_WARN: &str = "warn"; pub const TOKEN_SLEEP: &str = "sleep"; pub const TOKEN_SET: &str = "set"; pub const TOKEN_IF: &str = "if"; +pub const TOKEN_THEN: &str = "then"; pub const TOKEN_GOTO: &str = "goto"; pub const TOKEN_LABEL: &str = "label"; pub const TOKEN_SCRIPT: &str = "script"; @@ -43,6 +44,7 @@ pub const NON_CHOICE_TOKENS: &[&str] = &[ TOKEN_SLEEP, TOKEN_SET, TOKEN_IF, + TOKEN_THEN, TOKEN_GOTO, TOKEN_LABEL, TOKEN_SCRIPT, @@ -118,6 +120,7 @@ pub enum ChatEvent { RunScript(String), SleepSeconds(f64), SetVariable(String), + GotoIf(String, ChatPos), } // This is the only place where any YAML interaction should be happening. @@ -351,7 +354,11 @@ impl ChatDB { result = Some(Value::String(value_string.into())); } Some(Value::Mapping(mapping)) => { - if let Some((_choicetext, subconversation, _)) = self.search_choice(value) { + if let Some(branch) = mapping.get(TOKEN_THEN) { + result = Some(Value::Mapping(mapping.clone())); + next_pointer = Some(branch.clone()); + } + else if let Some((_choicetext, subconversation, _)) = self.search_choice(value) { result = Some(Value::Mapping(mapping.clone())); next_pointer = Some(subconversation); } @@ -430,7 +437,11 @@ impl ChatDB { for (key, value) in &map { let key = key.as_str(); match (key, value) { - (Some(TOKEN_IF), _) => {} // TODO + (Some(TOKEN_IF), Value::String(condition)) => { + let mut pos = chat.position.clone(); + pos.push(0); + event.send(ChatEvent::GotoIf(condition.into(), pos)); + } (Some(TOKEN_SOUND), Value::String(sound_name)) => { sound = sound_name.clone(); } @@ -667,6 +678,11 @@ pub fn handle_chat_events( vars.set_in_scope(&chat.talker.actor_id, string, "".into()); } } + ChatEvent::GotoIf(condition, goto) => { + if condition != "" { + chat.position = goto.clone(); + } + } } } } diff --git a/src/chats/serenity.yaml b/src/chats/serenity.yaml index 2892c1e..4116438 100644 --- a/src/chats/serenity.yaml +++ b/src/chats/serenity.yaml @@ -17,8 +17,14 @@ - chat: Icarus +- if: + then: + - Oh hey, you're back! + - Nice to see you again! + - goto: back - Oh hey, you're awake! - set: talked_before +- label: back - I found you drifting out cold, and thought, I better watch over you. - Took us here behind that moonlet, to shield you from the micros. - Thank you!: