diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-14 19:24:14 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-14 19:24:14 +0300 |
| commit | 1912d5a0c6aa405f8e17d6b065eed33a7f687fbb (patch) | |
| tree | 3203d680b1b8dc58bcaa1f61031baca00018aeb1 /crates/fparkan-script/src/lib.rs | |
| parent | 71ae57fb95805e6262aa70fc4890dcad91ab3ea1 (diff) | |
| download | fparkan-1912d5a0c6aa405f8e17d6b065eed33a7f687fbb.tar.xz fparkan-1912d5a0c6aa405f8e17d6b065eed33a7f687fbb.zip | |
fix(script): evaluate products before adding formula terms
Reuses the original operand formats and rounds each product before adding or subtracting. The original ai.dll agreed on three campaign difficulty expressions. Mission 19 now reaches comparison branches in Init.
Diffstat (limited to 'crates/fparkan-script/src/lib.rs')
| -rw-r--r-- | crates/fparkan-script/src/lib.rs | 56 |
1 files changed, 49 insertions, 7 deletions
diff --git a/crates/fparkan-script/src/lib.rs b/crates/fparkan-script/src/lib.rs index 60ddc826..d28a3d19 100644 --- a/crates/fparkan-script/src/lib.rs +++ b/crates/fparkan-script/src/lib.rs @@ -277,7 +277,7 @@ impl std::fmt::Display for ScriptInitResolveError { ), Self::UnsupportedFormulaExpression { index, expression } => write!( formatter, - "formula {index} expression {:?} is outside the supported atom and binary +/− subset", + "formula {index} expression {:?} is outside the supported arithmetic subset", String::from_utf8_lossy(expression) ), Self::DwordConversionOutOfRange { index, value_bits } => write!( @@ -1643,7 +1643,7 @@ fn parse_init_formula_expression( } expression = &expression[start + 1..close]; } - let (left, left_end) = parse_init_formula_atom(expression, 0, declarations, values)?; + let (left, left_end) = parse_init_formula_product(expression, 0, declarations, values)?; let operator_index = skip_native_formula_space(expression, left_end); let Some(&operator @ (b'+' | b'-')) = expression.get(operator_index) else { return expression[left_end..] @@ -1652,7 +1652,7 @@ fn parse_init_formula_expression( .then_some(left); }; let (right, right_end) = - parse_init_formula_atom(expression, operator_index + 1, declarations, values)?; + parse_init_formula_product(expression, operator_index + 1, declarations, values)?; if !expression[right_end..] .iter() .all(|byte| *byte == 0 || *byte <= 32) @@ -1666,6 +1666,21 @@ fn parse_init_formula_expression( }) } +fn parse_init_formula_product( + expression: &[u8], + start: usize, + declarations: &[VarSetDeclaration], + values: &[VarSetDefault], +) -> Option<(f32, usize)> { + let (left, end) = parse_init_formula_atom(expression, start, declarations, values)?; + let index = skip_native_formula_space(expression, end); + if expression.get(index) != Some(&b'*') { + return Some((left, end)); + } + let (right, end) = parse_init_formula_atom(expression, index + 1, declarations, values)?; + Some((left * right, end)) +} + fn parse_init_formula_atom( expression: &[u8], start: usize, @@ -3320,13 +3335,40 @@ STRING( 8, ignored, ignored, ignored)\r\n"; } #[test] + fn script_init_multiplies_before_addition_and_subtraction() { + let varset = + parse_varset(b"VAR(float, fDifficulty, 0.75)\nVAR(float, target, 0)\n").unwrap(); + for (expression, expected) in [ + (&b"20 + 55*fDifficulty"[..], 61.25_f32), + (b"100 - 40*fDifficulty", 70.0), + (b"700 - 100*fDifficulty", 625.0), + (b"2*fDifficulty + 4*fDifficulty", 4.5), + ] { + let formulas = one_formula(expression); + let package = init_package(vec![init_formula_assignment(1, 0)]); + let prefix = varset + .resolve_init_prefix(&package, init_input(), Some(&formulas)) + .unwrap() + .unwrap(); + assert_eq!(prefix.stopped_at, None); + assert_eq!( + prefix.writes, + [ScriptInitWrite { + index: 1, + value: VarSetDefault::FloatBits(expected.to_bits()), + }] + ); + } + } + + #[test] fn script_init_rejects_unsupported_formula_variable_forms() { let varset = parse_varset( b"VAR(float, target, 1)\nVAR(float, A, 2)\nVAR(float, A1, 3)\nVAR(float, ^name, 4)\n", ) .expect("numeric declarations"); for expression in [ - &b"target * 2"[..], + &b"target / 2"[..], b"-1", b"target + 1 + 2", b"missing + 1", @@ -3441,7 +3483,7 @@ STRING( 8, ignored, ignored, ignored)\r\n"; let varset = parse_varset(b"VAR(DWORD, target, 0)\nVAR(DWORD, integer, 0)\n") .expect("numeric declaration"); let formulas = - FormulaTable::parse(b"FUNCTION(unsupported, target * 2, )\nFUNCTION(literal, 7.5, )\n"); + FormulaTable::parse(b"FUNCTION(unsupported, target / 2, )\nFUNCTION(literal, 7.5, )\n"); let action = |formula_index| ScriptInstruction { header_words: [u32::MAX, 1, u32::MAX, u32::MAX, 6, 0, formula_index], references: Vec::new(), @@ -3481,7 +3523,7 @@ STRING( 8, ignored, ignored, ignored)\r\n"; instruction_index: 0, error: ScriptInitResolveError::UnsupportedFormulaExpression { index: 0, - expression: b" target * 2".to_vec(), + expression: b" target / 2".to_vec(), }, }) ); @@ -3495,7 +3537,7 @@ STRING( 8, ignored, ignored, ignored)\r\n"; instruction_index: 1, error: ScriptInitResolveError::UnsupportedFormulaExpression { index: 0, - expression: b" target * 2".to_vec(), + expression: b" target / 2".to_vec(), }, }) ); |
