diff --git a/crates/benilla-app/src/lanes/unit_live_tests.rs b/crates/benilla-app/src/lanes/unit_live_tests.rs index 9347fbda7..9e7dc3ff4 100644 --- a/crates/benilla-app/src/lanes/unit_live_tests.rs +++ b/crates/benilla-app/src/lanes/unit_live_tests.rs @@ -3,7 +3,9 @@ //! answer and the frame's snapshot agree on every getter wherever the snapshot is current, and the //! object answers where it is not. -use benilla_protocol::field::{FIELD_UNIT_HEALTH, FIELD_UNIT_LEVEL, FIELD_UNIT_MAXHEALTH}; +use benilla_protocol::field::{ + FIELD_UNIT_FLAGS, FIELD_UNIT_HEALTH, FIELD_UNIT_LEVEL, FIELD_UNIT_MAXHEALTH, +}; use benilla_protocol::messages::GroupMemberEntry; use benilla_protocol::{EntityKind, SessionEvent}; use benilla_ui::script::UiScript; @@ -35,7 +37,7 @@ const TOKENS: [&str; 6] = [ ]; /// The one-token getters of the `Unit*` family. -const GETTERS: [&str; 39] = [ +const GETTERS: [&str; 40] = [ "UnitExists", "UnitIsVisible", "UnitIsTapped", @@ -75,6 +77,7 @@ const GETTERS: [&str; 39] = [ "UnitManaMax", "UnitIsCharmed", "UnitXP", + "UnitOnTaxi", ]; /// The whole client, headless, in the world with its frame run. @@ -252,3 +255,23 @@ fn a_getter_reads_the_object_as_it_runs() { let snapshot: (i64, i64, i64) = crate::script_calls::eval_in_vm(c.app.world(), chunk); assert_eq!(snapshot, (80, 90, 90)); } + +/// `UnitOnTaxi` tests bit 20 of the resolved object's flags as it runs (`0x517a80`-`0x517a8c`): a +/// party member whose store gained the flag after the frame's pushes answers 1 live, the snapshot +/// still nil, and the flag cleared answers nil again. +#[test] +fn unit_on_taxi_reads_a_party_members_flag_off_the_object() { + let mut c = client(); + let ask = |c: &mut Client| -> (Option, Option) { + let chunk = "return UnitOnTaxi('party1')"; + let live = crate::script_calls::with_hosted_vm(c.app.world_mut(), |s| { + s.expect("a VM").eval(chunk).expect("runs") + }); + (live, crate::script_calls::eval_in_vm(c.app.world(), chunk)) + }; + assert_eq!(ask(&mut c), (None, None)); + c.write_store(MATE, &[(FIELD_UNIT_FLAGS, 1 << 20)]); + assert_eq!(ask(&mut c), (Some(1), None)); + c.write_store(MATE, &[(FIELD_UNIT_FLAGS, 0)]); + assert_eq!(ask(&mut c), (None, None)); +} diff --git a/crates/benilla-app/src/ui_taxi/mod.rs b/crates/benilla-app/src/ui_taxi/mod.rs index 3bb596185..f36e29af5 100644 --- a/crates/benilla-app/src/ui_taxi/mod.rs +++ b/crates/benilla-app/src/ui_taxi/mod.rs @@ -354,9 +354,10 @@ mod tests { use benilla_protocol::field::FIELD_UNIT_FLAGS; use benilla_protocol::ObjectFields; - /// `1` with the bit, `nil` without it; the feed reads nothing about server splines. + /// The feed pushes the ride with the bit and clears it without; it reads nothing about server + /// splines. #[test] - fn unit_on_taxi_reads_the_taxi_flight_flag() { + fn the_feed_pushes_the_taxi_flight_flag() { let mut app = App::new(); app.add_systems(Update, feed_on_taxi); app.insert_non_send_resource(UiScript::new().unwrap()); @@ -369,10 +370,7 @@ mod tests { .id(); let on_taxi = |app: &mut App| -> bool { app.update(); - let s = app.world_mut().non_send_resource_mut::(); - s.eval::>(r#"return UnitOnTaxi("player")"#) - .unwrap() - .is_some() + app.world().non_send_resource::().on_taxi() }; assert!(!on_taxi(&mut app), "no TAXI_FLIGHT bit: nil"); diff --git a/crates/benilla-ui/src/script/talent.rs b/crates/benilla-ui/src/script/talent.rs index 8e16eefd6..b23d9adc8 100644 --- a/crates/benilla-ui/src/script/talent.rs +++ b/crates/benilla-ui/src/script/talent.rs @@ -320,13 +320,22 @@ pub(super) fn install(lua: &Lua) -> mlua::Result<()> { })?, )?; - // Every unit token answers the player's own pair: `PLAYER_CHARACTER_POINTS1/2` are private - // fields only we receive. + // UnitCharacterPoints(unit) (`0x519420`): the token resolved (`0x6f3690`, `0x515940`), then the + // two counters (`+0xe48`, `+0xe4c`) of the player block, which the object holds for the active + // player alone (`0x519463`-`0x51949e`: the guid must equal the active player's); any other + // unit, or none, answers 0, 0 (`0x5194da`). An unrecognised token raises in the resolver. g.set( "UnitCharacterPoints", - lua.create_function(|lua, _unit: String| { + lua.create_function(|lua, unit: mlua::Value| { + let unit = + super::binding_abi::string_arg(lua, unit, r#"Usage: UnitCharacterPoints("unit")"#)?; + super::unit::check_unit_token(&Some(unit.clone()))?; let model = lua.app_data_ref::().expect("model app_data"); - let (cp1, cp2) = model.talents.points; + let (cp1, cp2) = if super::unit::names_self(&model, &unit) { + model.talents.points + } else { + (0, 0) + }; Ok((i64::from(cp1), i64::from(cp2))) })?, )?; diff --git a/crates/benilla-ui/src/script/taxi.rs b/crates/benilla-ui/src/script/taxi.rs index 712c0e108..9450fe6eb 100644 --- a/crates/benilla-ui/src/script/taxi.rs +++ b/crates/benilla-ui/src/script/taxi.rs @@ -91,11 +91,17 @@ impl super::UiScript { } } - /// Whether the player is riding a taxi, for `UnitOnTaxi("player")`. + /// Whether the player is riding a taxi (`UNIT_FLAG_TAXI_FLIGHT`), for the equip control gate + /// (`0x494ba6`); `UnitOnTaxi` reads the flag off the unit itself. pub fn set_on_taxi(&mut self, riding: bool) { self.model_mut().taxi_riding = riding; } + /// What [`Self::set_on_taxi`] last pushed. + pub fn on_taxi(&self) -> bool { + self.model_ref().taxi_riding + } + /// The taxi calls a harness's VM kept ([`Self::keep_calls`]), in call order. pub fn take_taxi_calls(&mut self) -> Vec { self.take_calls_where(|call| match call { @@ -291,23 +297,30 @@ pub(super) fn install(lua: &Lua) -> mlua::Result<()> { )?; // `0x517a40`: the number 1 or nil, never a boolean; an argument neither string nor number - // raises the reference's usage (`0x517a48`). Only the player is tracked, as the stock UI asks - // about no other unit. + // raises the reference's usage (`0x517a48`). The token resolves (`0x515940`) and bit 20 of the + // unit's `UNIT_FIELD_FLAGS` (`0x517a80`-`0x517a8c`, `UNIT_FLAG_TAXI_FLIGHT`) answers, for any + // unit it names; a token naming nothing is nil. Read off the object as the call runs, else off + // the snapshot's flags ([`crate::script::unit`]'s `with_object`). g.set( "UnitOnTaxi", lua.create_function(|lua, unit: mlua::Value| { let unit = crate::script::binding_abi::string_arg(lua, unit, r#"Usage: UnitOnTaxi("unit")"#)?; - let model = lua.app_data_ref::().expect("model"); - Ok(crate::script::binding_abi::flag( - unit.eq_ignore_ascii_case("player") && model.taxi_riding, - )) + Ok(crate::script::binding_abi::flag(super::unit::with_object( + lua, + &Some(unit), + false, + |_, o| o.flags() & TAXI_FLIGHT != 0, + )?)) })?, )?; Ok(()) } +/// `UNIT_FIELD_FLAGS` bit 20 (`0x517a86`), vmangos `UNIT_FLAG_TAXI_FLIGHT`. +const TAXI_FLIGHT: u32 = 1 << 20; + #[cfg(test)] mod tests { use crate::script::UiScript; diff --git a/crates/benilla-ui/src/script/tests/talent.rs b/crates/benilla-ui/src/script/tests/talent.rs index f17ec504a..c54c52a79 100644 --- a/crates/benilla-ui/src/script/tests/talent.rs +++ b/crates/benilla-ui/src/script/tests/talent.rs @@ -103,6 +103,69 @@ fn bindings_read_the_pushed_snapshot() { assert!(s.take_errors().is_empty()); } +/// `UnitCharacterPoints` resolves its token and reads the two counters (`+0xe48`, `+0xe4c`) only +/// off the active player's object; any other unit, or none, answers 0, 0 (`0x519420`-`0x5194f8`). +#[test] +fn unit_character_points_answer_the_active_player_only() { + const ME: u64 = 0x10; + const OTHER: u64 = 0x20; + let mut s = script(); + s.set_talents(one_tab_state()); + let seat = |s: &mut UiScript, target: u64| { + s.set_unit_guids(&UnitGuids { + player: ME, + target, + party: [OTHER, 0, 0, 0], + held: [(ME, 0), (OTHER, 0)].into_iter().collect(), + ..Default::default() + }); + }; + let points = |s: &mut UiScript, token: &str| { + s.eval::<(i64, i64)>(&format!("return UnitCharacterPoints({token:?})")) + .unwrap() + }; + + seat(&mut s, OTHER); + assert_eq!(points(&mut s, "player"), (2, 1)); + assert_eq!(points(&mut s, "PLAYER"), (2, 1)); + assert_eq!(points(&mut s, "target"), (0, 0), "another player"); + assert_eq!(points(&mut s, "party1"), (0, 0), "a party member"); + assert_eq!( + points(&mut s, "pet"), + (0, 0), + "a recognised token naming nothing" + ); + + seat(&mut s, ME); + assert_eq!(points(&mut s, "target"), (2, 1), "the target is the player"); + + let err = s + .run(r#"UnitCharacterPoints("bogus")"#) + .unwrap_err() + .to_string(); + assert!(err.contains("Unknown unit name: bogus"), "{err}"); +} + +/// A token that is neither string nor number raises the reference's usage (`0x51943a`, +/// `0x851294`), not a type error; a number is stringified and then fails the resolver. +#[test] +fn unit_character_points_raises_the_reference_usage() { + let s = script(); + for call in [ + "UnitCharacterPoints()", + "UnitCharacterPoints(nil)", + "UnitCharacterPoints({})", + ] { + let err = s.run(call).unwrap_err().to_string(); + assert!( + err.contains(r#"Usage: UnitCharacterPoints("unit")"#), + "{call}: {err}" + ); + } + let err = s.run("UnitCharacterPoints(5)").unwrap_err().to_string(); + assert!(err.contains("Unknown unit name: 5"), "{err}"); +} + /// `isExceptional` is `1.0` when `[rec+0x4c] & 1` and nil otherwise (`0x4f33e8`): never `0`, which /// Lua reads as true, and `meetsPrereq` and each prereq's `isLearnable` are 1 or nil too /// (`0x4f3403`-`0x4f3443`, `0x4f3676`, `0x4f3644`). `GetTalentPrereqs` answers three values per edge. diff --git a/crates/benilla-ui/src/script/tests/taxi.rs b/crates/benilla-ui/src/script/tests/taxi.rs index 0d3c376a9..ebed736f7 100644 --- a/crates/benilla-ui/src/script/tests/taxi.rs +++ b/crates/benilla-ui/src/script/tests/taxi.rs @@ -69,7 +69,14 @@ fn taxi_snapshot_surfaces_and_calls() { "the continent art draws on the SetTaxiMap target" ); - s.set_on_taxi(true); + s.set_unit( + "player", + Some(UnitState { + exists: true, + flags: 1 << 20, + ..Default::default() + }), + ); s.run(r#"assert(UnitOnTaxi("player") == 1)"#).unwrap(); assert!(s.errors().is_empty(), "script errors: {:?}", s.errors()); } diff --git a/crates/benilla-ui/src/script/unit/bindings.rs b/crates/benilla-ui/src/script/unit/bindings.rs index 6e64646b9..235074c03 100644 --- a/crates/benilla-ui/src/script/unit/bindings.rs +++ b/crates/benilla-ui/src/script/unit/bindings.rs @@ -11,9 +11,9 @@ use super::super::protected::select_may_cast; use super::super::stat_read::read_self; use super::super::Model; use super::{ - check_unit_token, classification_word, grey_band, level_is_hidden_at, object_predicate, - read_object, unit_is_grey, unit_predicate, unknownobject, with_object, with_unit, PlayerRecord, - SelectionRequest, + check_unit_token, classification_word, grey_band, level_is_hidden_at, names_self, + object_predicate, read_object, unit_is_grey, unit_predicate, unknownobject, with_object, + with_unit, PlayerRecord, SelectionRequest, }; /// The `"player"` fast path of `UnitName`, `UnitRace`, `UnitClass` and `UnitSex`, never @@ -789,11 +789,6 @@ pub(in crate::script) fn install(lua: &Lua) -> mlua::Result<()> { // UnitXP/UnitXPMax(unit): the token resolved (`0x51738e`), then the active player's private // `PLAYER_XP` pair for its object by whatever token names it (`0x5173ad`-`0x5173c9`), read // off the object as the call runs ([`read_self`]), 0 for any other unit or none. - fn names_self(model: &Model, token: &str) -> bool { - token.eq_ignore_ascii_case("player") - || (model.unit_guids.player != 0 - && model.unit_guids.resolve(token).ok().flatten() == Some(model.unit_guids.player)) - } g.set( "UnitXP", lua.create_function(|lua, token: Value| { diff --git a/crates/benilla-ui/src/script/unit/mod.rs b/crates/benilla-ui/src/script/unit/mod.rs index 8fff55c27..0983ad04e 100644 --- a/crates/benilla-ui/src/script/unit/mod.rs +++ b/crates/benilla-ui/src/script/unit/mod.rs @@ -601,8 +601,17 @@ fn with_unit( }) } +/// Whether `token` names the active player: `"player"`, or any token the resolver turns into the +/// player's guid (`target` on yourself, `0x5173ad`-`0x5173c9`). The private player blocks +/// (`UnitXP`, `UnitCharacterPoints`) answer for this unit alone. +pub(super) fn names_self(model: &Model, token: &str) -> bool { + token.eq_ignore_ascii_case("player") + || (model.unit_guids.player != 0 + && model.unit_guids.resolve(token).ok().flatten() == Some(model.unit_guids.player)) +} + /// [`read_object`] on `token`, or `default` for an absent unit, after the token check. -fn with_object( +pub(super) fn with_object( lua: &Lua, token: &Option, default: T, diff --git a/crates/benilla-ui/src/script/unit/tests.rs b/crates/benilla-ui/src/script/unit/tests.rs index 02cf0f6e1..0832c1226 100644 --- a/crates/benilla-ui/src/script/unit/tests.rs +++ b/crates/benilla-ui/src/script/unit/tests.rs @@ -2027,8 +2027,8 @@ fn the_equals_one_idiom_branches_correctly_on_a_live_and_a_dead_token() { ); } -/// `UnitOnTaxi` (`0x517a40`), registered with the taxi module's ride flag, has the family's shape -/// and raises `Usage:` from its gate (`0x517a48`). +/// `UnitOnTaxi` (`0x517a40`) has the family's shape: the number 1 or nil, and `Usage:` from its +/// gate (`0x517a48`). #[test] fn unit_on_taxi_is_one_or_nil_and_raises_the_reference_usage() { let mut s = UiScript::new().unwrap(); @@ -2037,7 +2037,13 @@ fn unit_on_taxi_is_one_or_nil_and_raises_the_reference_usage() { assert!(s .eval::(r#"return UnitOnTaxi("player") == nil"#) .unwrap()); - s.set_on_taxi(true); + s.set_unit( + "player", + Some(UnitState { + flags: TAXI_FLIGHT, + ..player() + }), + ); assert_eq!( s.eval::(r#"return type(UnitOnTaxi("player"))"#) .unwrap(), @@ -2047,10 +2053,6 @@ fn unit_on_taxi_is_one_or_nil_and_raises_the_reference_usage() { assert!(!s .eval::(r#"return UnitOnTaxi("player") == true"#) .unwrap()); - // Only the player's ride is tracked: any other token is nil. - assert!(s - .eval::(r#"return UnitOnTaxi("target") == nil"#) - .unwrap()); let err = s.eval::("return UnitOnTaxi()").unwrap_err(); assert!( @@ -2059,6 +2061,49 @@ fn unit_on_taxi_is_one_or_nil_and_raises_the_reference_usage() { ); } +/// `UNIT_FIELD_FLAGS` bit 20, `UNIT_FLAG_TAXI_FLIGHT` (`0x517a86`). +const TAXI_FLIGHT: u32 = 1 << 20; + +/// `UnitOnTaxi` resolves the token and tests bit 20 of that unit's flags (`0x517a76`-`0x517a8c`), +/// so a party member or the target answers, a neighbouring bit does not, and a token naming +/// nothing is nil; an unrecognised token raises in the resolver (`0x515c14`). +#[test] +fn unit_on_taxi_reads_the_flag_of_any_resolved_unit() { + let mut s = UiScript::new().unwrap(); + let with = |flags: u32| { + Some(UnitState { + exists: true, + flags, + ..Default::default() + }) + }; + s.set_unit("party1", with(TAXI_FLIGHT | 0x8)); + s.set_unit("party2", with(!TAXI_FLIGHT)); + s.set_unit("target", with(TAXI_FLIGHT)); + for token in ["party1", "PARTY1", "target"] { + assert_eq!( + s.eval::(&format!("return UnitOnTaxi({token:?})")) + .unwrap(), + 1, + "{token}" + ); + } + assert!( + s.eval::>(r#"return UnitOnTaxi("party2")"#) + .unwrap() + .is_none(), + "every other bit set" + ); + assert!( + s.eval::>(r#"return UnitOnTaxi("party3")"#) + .unwrap() + .is_none(), + "recognised but naming nothing" + ); + let err = s.run(r#"UnitOnTaxi("bogus")"#).unwrap_err().to_string(); + assert!(err.contains("Unknown unit name: bogus"), "{err}"); +} + /// The reference's `"player"` arm reads the record (`0x51708c` reads `0xc27d88`), never the /// snapshot, so a nameless player push cannot blank the name. #[test] diff --git a/docs/DEVIATIONS.md b/docs/DEVIATIONS.md index ed6b2787f..3c8eca5fb 100644 --- a/docs/DEVIATIONS.md +++ b/docs/DEVIATIONS.md @@ -792,7 +792,7 @@ Each entry is the first sentence of a `Deviation:` comment, linking to where it - Ours answers milliseconds, since a cycle count of unknown rate is no use to an addon timing its code. ([`src/script/stdlib.rs:77`](../crates/benilla-ui/src/script/stdlib.rs#L77)) - A format that ends in `%` reads past its terminator in the client (`0x4135c3`), into whatever memory follows the string; here the output ends there. ([`src/script/stdlib.rs:441`](../crates/benilla-ui/src/script/stdlib.rs#L441)) - The reference reads uninitialised fields before the seed; we answer the first legal design because that value is undefined. ([`src/script/tabard.rs:98`](../crates/benilla-ui/src/script/tabard.rs#L98)) -- Returns nothing. The handler ends `mov eax,1` with nothing pushed (`0x4dcd7c`), so Lua 5.0 hands back the stack top, the node argument: an accident of the handler, not an API, and copying a stack-top leak would copy a bug. ([`src/script/taxi.rs:210`](../crates/benilla-ui/src/script/taxi.rs#L210)) +- Returns nothing. The handler ends `mov eax,1` with nothing pushed (`0x4dcd7c`), so Lua 5.0 hands back the stack top, the node argument: an accident of the handler, not an API, and copying a stack-top leak would copy a bug. ([`src/script/taxi.rs:216`](../crates/benilla-ui/src/script/taxi.rs#L216)) - A handler that unregisters the saved next ends the dispatch, because the reference then reads a freed node. ([`src/script/tick.rs:29`](../crates/benilla-ui/src/script/tick.rs#L29)) - `0x530a52` lays it out at once; getters resolve first, so only OnSizeChanged fires later. ([`src/script/tooltip/size.rs:180`](../crates/benilla-ui/src/script/tooltip/size.rs#L180)) - No time line follows, where the reference's rides the record's `+0xc` (`0x532bdf`), a byte an empty record keeps from its history. 0 after `ClearBuffRecord 0x4e4490` prints a lone "0 seconds remaining" (`0x4e447e`); else it is whatever the last occupant left, as `ResetBuffCache 0x4e40c0` and the compaction's tail reset (`0x4e4237`) call `0x4e44b0` with slot -1, which writes `+0x0` and `+0x4` alone (`0x4e44bc`-`0x4e44c0`). ([`src/script/tooltip_aura.rs:234`](../crates/benilla-ui/src/script/tooltip_aura.rs#L234)) @@ -805,7 +805,7 @@ Each entry is the first sentence of a `Deviation:` comment, linking to where it - A header clears it too, because it has no spell id; the reference stores its `0xffffffff` (`0x4d7b05`) and reads back 1, which the stock window's `> 1` test treats the same as 0. ([`src/script/trainer/mod.rs:1224`](../crates/benilla-ui/src/script/trainer/mod.rs#L1224)) - Index 0 is a no-op, because one stray call would spend the purse; the reference buys every visible service for any index at or below 0 (`0x4da244`, `0x4d8a70`). ([`src/script/trainer/mod.rs:1248`](../crates/benilla-ui/src/script/trainer/mod.rs#L1248)) - Kept apart from `Self::vertex_color`, which the reference's `Texture:SetAlpha` (`0x79b580`) rewrites, because the drawn product differs only on a region that sets both, or a gradient and an alpha, which no stock code does, and a FontString's `SetAlpha` then keeps its font colour. ([`src/script/types.rs:421`](../crates/benilla-ui/src/script/types.rs#L421)) -- Among whole-name matches ours picks the nearest, because the reference's first-walked pick is order-dependent and reads as a bug. ([`src/script/unit/bindings.rs:995`](../crates/benilla-ui/src/script/unit/bindings.rs#L995)) +- Among whole-name matches ours picks the nearest, because the reference's first-walked pick is order-dependent and reads as a bug. ([`src/script/unit/bindings.rs:990`](../crates/benilla-ui/src/script/unit/bindings.rs#L990)) - The reference's matcher takes a whole-string hit at once only when it is nearer than the best so far (`0x493c88`-`0x493ca2`); an exact hit that is not nearer competes on prefix length alone (`0x493cf8`-`0x493d09`), so a nearer "Bobby" walked first beats an exact "Bob", and the pick follows the object list's order. ([`src/script/unit/by_name.rs:19`](../crates/benilla-ui/src/script/unit/by_name.rs#L19)) - A full tie goes to the lower guid, where the reference keeps the first unit its walk (`0x493aa0`) reaches, an order neither of ours has; a guid keeps the two picks equal. ([`src/script/unit/by_name.rs:61`](../crates/benilla-ui/src/script/unit/by_name.rs#L61)) - `0x4e81d0` has no bound and reads past its four-entry table, which is stale memory, not a mechanism; past it, nobody. ([`src/script/unit/resolve.rs:198`](../crates/benilla-ui/src/script/unit/resolve.rs#L198))