implement "if" statements with 2 operands (comparing for equality)

This commit is contained in:
yuni 2024-04-14 19:23:43 +02:00
parent d51333274b
commit 00df7bc711
3 changed files with 31 additions and 8 deletions

View file

@ -679,7 +679,7 @@ pub fn handle_chat_events(
} }
} }
ChatEvent::GotoIf(condition, goto) => { ChatEvent::GotoIf(condition, goto) => {
if vars.evaluate(condition, &chat.talker.actor_id) { if vars.evaluate_condition(condition, &chat.talker.actor_id) {
chat.position = goto.clone(); chat.position = goto.clone();
} }
} }

View file

@ -7,7 +7,7 @@
- chat: ClippyTransSerenity - chat: ClippyTransSerenity
- set: busstop serenity - set: $busstop serenity
- Welcome at StarTrans Cargo Services! You have reached Serenity Station. - Welcome at StarTrans Cargo Services! You have reached Serenity Station.
- "Ready for a trip? Available bus stops: Oscillation Station, Metis Prime Station" - "Ready for a trip? Available bus stops: Oscillation Station, Metis Prime Station"
- label: startransbusstop - label: startransbusstop
@ -21,7 +21,7 @@
- chat: ClippyTransMetis - chat: ClippyTransMetis
- set: busstop metis - set: $busstop metis
- Welcome at StarTrans Cargo Services! You have reached Metis Prime Station. - Welcome at StarTrans Cargo Services! You have reached Metis Prime Station.
- "Ready for a trip? Available bus stops: Oscillation Station, Serenity Station" - "Ready for a trip? Available bus stops: Oscillation Station, Serenity Station"
- label: startransbusstop - label: startransbusstop
@ -36,7 +36,7 @@
- chat: ClippyTransOscillation - chat: ClippyTransOscillation
- set: busstop oscillation - set: $busstop oscillation
- Welcome at StarTrans Cargo Services! You have reached Oscillation Station. - Welcome at StarTrans Cargo Services! You have reached Oscillation Station.
- "Ready for a trip? Available bus stops: Serenity Station, Metis Prime Station" - "Ready for a trip? Available bus stops: Serenity Station, Metis Prime Station"
- label: startransbusstop - label: startransbusstop
@ -57,21 +57,21 @@
- Can I take a spacecraft with me?: - Can I take a spacecraft with me?:
- Absolutely. - Absolutely.
- goto: startransbusstop - goto: startransbusstop
- if: busstop != "oscillation" - if: $busstop != oscillation
Take me to Oscillation Station, please.: Take me to Oscillation Station, please.:
- StarTrans wishes you a pleasant journey. - StarTrans wishes you a pleasant journey.
- script: cryofadeout - script: cryofadeout
- sleep: 5 - sleep: 5
- script: cryotrip oscillation - script: cryotrip oscillation
- goto: EXIT - goto: EXIT
- if: busstop != "metis" - if: $busstop != metis
Take me to Metis Prime Station, please.: Take me to Metis Prime Station, please.:
- StarTrans wishes you a pleasant journey. - StarTrans wishes you a pleasant journey.
- script: cryofadeout - script: cryofadeout
- sleep: 5 - sleep: 5
- script: cryotrip metisprime - script: cryotrip metisprime
- goto: EXIT - goto: EXIT
- if: busstop != "serenity" - if: $busstop != serenity
Take me to Serenity Station, please.: Take me to Serenity Station, please.:
- StarTrans wishes you a pleasant journey. - StarTrans wishes you a pleasant journey.
- script: cryofadeout - script: cryofadeout

View file

@ -237,6 +237,7 @@ impl GameVars {
// This method ensures that the variable name contains a scope separator, // This method ensures that the variable name contains a scope separator,
// and if a scope is missing, it prefixes the fallback scope. // and if a scope is missing, it prefixes the fallback scope.
// Should NOT be used on non-variable values, like plain strings.
// //
// Some examples, assuming fallback_scope="Clippy", SCOPE_SEPARATOR="$": // Some examples, assuming fallback_scope="Clippy", SCOPE_SEPARATOR="$":
// //
@ -278,9 +279,11 @@ impl GameVars {
self.db.insert(key, value); self.db.insert(key, value);
} }
pub fn evaluate(&self, condition: &str, scope: &str) -> bool { pub fn evaluate_condition(&self, condition: &str, scope: &str) -> bool {
let parts: Vec<&str> = condition.split(" ").collect(); let parts: Vec<&str> = condition.split(" ").collect();
if parts.len() == 1 { if parts.len() == 1 {
// Got something like "if $somevar:".
// Check whether the variable evaluates to true.
let part = parts[0]; let part = parts[0];
if part.contains(SCOPE_SEPARATOR) { if part.contains(SCOPE_SEPARATOR) {
let part = Self::normalize_varname(scope, part); let part = Self::normalize_varname(scope, part);
@ -290,6 +293,26 @@ impl GameVars {
else { else {
return Self::evaluate_str_as_bool(part); return Self::evaluate_str_as_bool(part);
} }
} else if parts.len() == 2 {
// Got something like "if $something somethingelse"
// Check whether the two are identical.
let mut left: String = parts[0].to_string();
if left.contains(SCOPE_SEPARATOR) {
left = self
.get(Self::normalize_varname(scope, left.as_str()).as_str())
.unwrap_or("".to_string());
}
let mut right: String = parts[1].to_string();
if right.contains(SCOPE_SEPARATOR) {
right = self
.get(Self::normalize_varname(scope, right.as_str()).as_str())
.unwrap_or("".to_string());
}
dbg!(&left);
dbg!(&right);
dbg!(left == right);
return left == right;
} else { } else {
return false; return false;
} }