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

main
hut 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) => {
if vars.evaluate(condition, &chat.talker.actor_id) {
if vars.evaluate_condition(condition, &chat.talker.actor_id) {
chat.position = goto.clone();
}
}

View File

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

View File

@ -237,6 +237,7 @@ impl GameVars {
// This method ensures that the variable name contains a scope separator,
// 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="$":
//
@ -278,9 +279,11 @@ impl GameVars {
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();
if parts.len() == 1 {
// Got something like "if $somevar:".
// Check whether the variable evaluates to true.
let part = parts[0];
if part.contains(SCOPE_SEPARATOR) {
let part = Self::normalize_varname(scope, part);
@ -290,6 +293,26 @@ impl GameVars {
else {
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 {
return false;
}