From 1a11ef8319d3298a5c10c077ccf97c8a7e40d69c Mon Sep 17 00:00:00 2001 From: Sam Sung <51686767+samwhosung@users.noreply.github.com> Date: Sat, 31 Oct 2026 04:58:29 +0700 Subject: [PATCH] A display rebuild ends a unit's spell effects before its body goes (#2864) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A display swap despawned a unit's spell effects with its old visual while their instances still named them, so a later teardown despawned a dead entity. 1.12.1's rebuild ends every effect on the unit first (`0x60abe0`, `0x60ac10`), then replays the auras' and the channel's kits. **Changed** - A rebuild runs only when `0x60ae10` says the display changed. It ends the effects started before it on the wire, and replays the auras' and the channel's kits on the new body. - Kit sounds fade over 0.15 s as they stop (`0x6204b6`), and each loop is tracked per kit. - benilla's rig heal keeps what the unit had. **Tests:** a swap and a heal with effects up, the wire order within a frame, the gate, the replays, the fade. The swap tests fail on main. Fixes #2843 Found in Adrinalin4ik/manila@cd8790bc 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 5.5 --- crates/benilla-app/src/creature_anim.rs | 11 +- crates/benilla-app/src/creature_anim/blood.rs | 2 + .../benilla-app/src/creature_anim/breath.rs | 2 + crates/benilla-app/src/creature_anim/edits.rs | 1 + .../src/creature_anim/env_damage.rs | 2 + crates/benilla-app/src/creature_anim/net.rs | 2 + .../src/creature_anim/spell_visual.rs | 185 +++- .../src/creature_anim/spell_visual/tests.rs | 280 +++++- crates/benilla-app/src/entities.rs | 34 +- crates/benilla-app/src/entities/chain_beam.rs | 1 + crates/benilla-app/src/entities/dest_fx.rs | 3 + .../benilla-app/src/entities/live_display.rs | 335 ++++++- crates/benilla-app/src/entities/missile.rs | 1 + .../benilla-app/src/entities/spell_fx/mod.rs | 822 ++++++++++++++++-- crates/benilla-app/src/net.rs | 1 + crates/benilla-app/src/net/objects.rs | 70 ++ crates/benilla-app/src/net/objects/update.rs | 77 +- crates/benilla-app/src/sound/kit.rs | 17 +- crates/benilla-app/src/sound/spell.rs | 425 +++++++-- crates/benilla-app/src/spell/net.rs | 8 + crates/benilla-app/src/ui_chat/input/mod.rs | 3 + crates/benilla-world/src/particles.rs | 5 + docs/DEVIATIONS.md | 4 +- 23 files changed, 2026 insertions(+), 265 deletions(-) diff --git a/crates/benilla-app/src/creature_anim.rs b/crates/benilla-app/src/creature_anim.rs index 346cdefd5..d15a3b703 100644 --- a/crates/benilla-app/src/creature_anim.rs +++ b/crates/benilla-app/src/creature_anim.rs @@ -710,6 +710,9 @@ pub(crate) struct CastEvent { pub(crate) kind: CastEventKind, /// `PlaySeq` stamp, carried to the kit's `EmoteAnim`. pub(crate) seq: u64, + /// The cast packet's place among what the packets queue ([`crate::lanes::stamp`]); `None` for + /// an edge the frame raised (a missile's arrival, a probe line), after every packet. + pub(crate) at: Option, } /// Which cast edge fired. @@ -889,6 +892,9 @@ pub(crate) struct SpellGoTargets { pub(crate) ammo_display_id: Option, /// `PlaySeq` stamp, the GO's `CastEvent`'s. pub(crate) seq: u64, + /// The GO packet's place, its `CastEvent`'s, which its impact kits carry; `None` for a GO the + /// frame raised, after every packet. + pub(crate) at: Option, } /// The sheath policy: requests, ceremony overlays, the `AnimationData.dbc` table. @@ -956,8 +962,9 @@ use spell_visual::{ #[cfg(test)] pub(crate) use spell_visual::arm_aura_state_fx as arm_aura_state_fx_for_test; pub(crate) use spell_visual::{ - held_strike_sound, ChainProcPlay, FxClass, FxStage, GroundList, KitPush, MissileSpawn, - PrecastTables, PrecastVisuals, SpellKitFx, SpellKitShake, SpellKitSound, SpellVisuals, + held_strike_sound, replay_rebuilt_kit_sounds, ChainProcPlay, FxClass, FxStage, GroundList, + KitPush, MissileSpawn, PrecastTables, PrecastVisuals, SpellKitFx, SpellKitShake, SpellKitSound, + SpellVisuals, }; /// The per-unit animation state machine; `Transform` is required, its scale divides the rate, and diff --git a/crates/benilla-app/src/creature_anim/blood.rs b/crates/benilla-app/src/creature_anim/blood.rs index 81a041da1..c4aaccfdd 100644 --- a/crates/benilla-app/src/creature_anim/blood.rs +++ b/crates/benilla-app/src/creature_anim/blood.rs @@ -130,6 +130,8 @@ pub(super) fn blood_spurts( effect, path: path.to_string(), }], + // A swing's impact, presented in the frame after every packet. + at: None, }); } } diff --git a/crates/benilla-app/src/creature_anim/breath.rs b/crates/benilla-app/src/creature_anim/breath.rs index 7b615798d..fbc6b2a08 100644 --- a/crates/benilla-app/src/creature_anim/breath.rs +++ b/crates/benilla-app/src/creature_anim/breath.rs @@ -180,6 +180,8 @@ pub(super) fn fire_breath( effect, path: path.clone(), }], + // A model event of the frame, after every packet. + at: None, }); } // Forget despawned units. diff --git a/crates/benilla-app/src/creature_anim/edits.rs b/crates/benilla-app/src/creature_anim/edits.rs index 00a0444da..849d75e32 100644 --- a/crates/benilla-app/src/creature_anim/edits.rs +++ b/crates/benilla-app/src/creature_anim/edits.rs @@ -437,6 +437,7 @@ fn cast_event(world: &mut World, entity: Entity, spell_id: u32, kind: CastEventK spell_id, kind, seq, + at: None, }); } } diff --git a/crates/benilla-app/src/creature_anim/env_damage.rs b/crates/benilla-app/src/creature_anim/env_damage.rs index a5cd74ec8..8f234b195 100644 --- a/crates/benilla-app/src/creature_anim/env_damage.rs +++ b/crates/benilla-app/src/creature_anim/env_damage.rs @@ -63,6 +63,8 @@ pub(super) fn hard_landing_dust( entity: l.entity, kit_id, seq: seq.next(), + // The landing frame's own prediction, after every packet. + at: None, }); } } diff --git a/crates/benilla-app/src/creature_anim/net.rs b/crates/benilla-app/src/creature_anim/net.rs index 6b458e731..6937185bd 100644 --- a/crates/benilla-app/src/creature_anim/net.rs +++ b/crates/benilla-app/src/creature_anim/net.rs @@ -449,6 +449,7 @@ fn play_spell_visual( entity: e, kit_id, seq: play_seq.next(), + at: Some(crate::lanes::stamp()), }); } } @@ -473,6 +474,7 @@ fn environmental_damage_log( entity: ent, kit_id, seq: play_seq.next(), + at: Some(crate::lanes::stamp()), }); } } diff --git a/crates/benilla-app/src/creature_anim/spell_visual.rs b/crates/benilla-app/src/creature_anim/spell_visual.rs index b2de59bcc..e2ba7f543 100644 --- a/crates/benilla-app/src/creature_anim/spell_visual.rs +++ b/crates/benilla-app/src/creature_anim/spell_visual.rs @@ -58,6 +58,7 @@ pub(crate) fn meeting_stone_join_fx(visuals: &SpellVisuals, entity: Entity) -> O effect, path: path.to_string(), }], + at: None, }) } @@ -77,10 +78,16 @@ pub(crate) enum SpellKitSound { entity: Entity, spell_id: u32, kit_sound: u32, + /// Rung by a cast's hold, a precast's or a channel's kit, which a replacing hold stops + /// ([`Self::StopHold`] with no spell). + cast_hold: bool, + /// Its packet's place ([`SpellKitFx::at`]), which a hold loop keeps for + /// [`Self::Rebuilt`]. + at: Option, }, - /// Stop the unit's hold loop: for `Some(spell)`, only a loop that spell's kit rang, as - /// `0x614150(spell, 0)` stops only the effects whose spell matches (`0x614167`); for `None`, - /// whatever loop it holds, as a new hold replaces the old. + /// Stop the unit's hold loops: for `Some(spell)`, every loop that spell's kits rang, as + /// `0x614150(spell, 0)` stops every effect whose spell matches (`0x614167`); for `None`, every + /// loop a cast's hold rang, as a new hold replaces the old. StopHold { entity: Entity, spell_id: Option, @@ -90,6 +97,24 @@ pub(crate) enum SpellKitSound { PlayAt { pos: Vec3, kit_sound: u32 }, /// Stop this kit's sound on the unit as its aura leaves (`0x614150`, 0.15 s fade). StopKit { entity: Entity, kit_sound: u32 }, + /// A display rebuild at `at` (`0x60abe0`): its loop `0x60ac10` stops each of the unit's + /// effects, fading each one's sound over 0.15 s (`0x6203e0`, `0x6204b6` → `0x7a5a10`), so the + /// unit's hold loop fades if it rang before `at`. + Rebuilt { + entity: Entity, + at: crate::lanes::Stamp, + }, + /// The rebuild's replay at its place `at` of a kit it plays again, an aura slot's state kit + /// (`0x5ff130` at `0x60ada6`) or the channel's (`0x612a30` at `0x60adad`): the unit's sound + /// on that kit fades as the rebuild stopped its effect, and the kit rings afresh. + Replay { + entity: Entity, + spell_id: u32, + kit_sound: u32, + /// The channel's, a cast's hold ([`Self::Play`]). + cast_hold: bool, + at: crate::lanes::Stamp, + }, } /// A kit's camera shake (kit field 14 → `SpellEffectCameraShakes.dbc`), consumed by @@ -156,6 +181,8 @@ pub(crate) enum SpellKitFx { /// The models' animation lifecycle ([`FxStage`]). stage: FxStage, effects: Vec, + /// Its packet's place ([`SpellKitFx::at`]). + at: Option, }, /// Despawn the unit's persistent effects matching `(spell_id, class)` (`0x614150`). Reap { @@ -165,9 +192,29 @@ pub(crate) enum SpellKitFx { /// `0x614150`'s `force`: the cast edges stop unforced, sparing the spell's state effects /// (kind 2), which the aura's removal and the channel's change stop forced. force: bool, + /// Its packet's place ([`SpellKitFx::at`]). + at: Option, }, } +impl SpellKitFx { + /// The unit the edge plays on. + pub(crate) fn entity(&self) -> Entity { + match self { + Self::Begin { entity, .. } | Self::Reap { entity, .. } => *entity, + } + } + + /// The place of the packet that raised the edge among what the packets queue (a field + /// edge's or a cast packet's [`crate::lanes::Stamp`]), by which a display rebuild in the same + /// frame ends it or not; `None` for an edge the frame raised, after every packet. + pub(crate) fn at(&self) -> Option { + match self { + Self::Begin { at, .. } | Self::Reap { at, .. } => *at, + } + } +} + /// A kit play whose `CharProc` slots name a beam, the dispatcher's chain case (`0x60da79`), run at /// the kit tail (`0x60f35c`) and from the channel poll (`0x612b18`). Consumed by /// `crate::entities::chain_beam`. @@ -493,6 +540,9 @@ pub(crate) struct KitPush { pub(crate) kit_id: u32, /// [`super::PlaySeq`] stamp at the wire drain, in packet order. pub(crate) seq: u64, + /// The packet's place ([`SpellKitFx::at`]); `None` for a push the frame raised, after every + /// packet. + pub(crate) at: Option, } /// The kit lanes [`route_cast_visuals`] writes but does not own, bundled into one param because @@ -519,6 +569,8 @@ struct KitOut<'a, 'w1, 'w2, 'w3, 'w4, 'w5, 'w6, 'w7, 'w8> { trails: &'a mut MessageWriter<'w7, crate::weapon_trail::TrailArm>, /// The base recompute a stage-2 kit's anim id spends itself on. recomputes: &'a mut MessageWriter<'w8, BaseAnimRecompute>, + /// The place of the packet the plays come from ([`SpellKitFx::at`]). + at: Option, } /// One kit play's policy on the reference's `PlaySpellVisualKit` (`0x60edf0`): @@ -583,6 +635,8 @@ fn play_kit( entity, spell_id, kit_sound, + cast_hold: false, + at: out.at, }); } // The camera shake plays at every stage, gated on nothing. @@ -615,6 +669,7 @@ fn play_kit( class: FxClass::Hold, stage: play.stage, effects, + at: out.at, }); } } @@ -790,6 +845,7 @@ pub(super) fn route_cast_visuals( shakes, trails, recomputes, + at: None, }; // `holds` lags one command flush and an instant cast's START and GO drain together, so @@ -803,6 +859,7 @@ pub(super) fn route_cast_visuals( }; for p in pushes.read() { + out.at = p.at; // No kit-level scan, but each slot's same-(record, tag) replace (`0x6208e0`) swaps the old // batch out; the driver's same-id dedup (`0x5fdba0`) keeps a looping clip running. if let Some(kit) = visuals.0.kit(p.kit_id) { @@ -824,6 +881,7 @@ pub(super) fn route_cast_visuals( if commands.get_entity(ev.entity).is_err() { continue; } + out.at = ev.at; match ev.kind { CastEventKind::Start => { // A replacing cast reaps the prior hold's loop and persistent models first. @@ -837,6 +895,7 @@ pub(super) fn route_cast_visuals( spell_id: prior, class: FxClass::Hold, force: true, + at: ev.at, }); } // A ranged-attribute START draws the ranged weapon (`0x6e78f3`); vmangos sends @@ -881,6 +940,8 @@ pub(super) fn route_cast_visuals( entity: ev.entity, spell_id: ev.spell_id, kit_sound, + cast_hold: true, + at: ev.at, }); } // Persistent until the cast resolves, re-armed forever (stage 4). @@ -893,6 +954,7 @@ pub(super) fn route_cast_visuals( class: FxClass::Hold, stage: FxStage::Relive, effects, + at: ev.at, }); } } @@ -925,6 +987,7 @@ pub(super) fn route_cast_visuals( spell_id: ev.spell_id, class: FxClass::Hold, force: false, + at: ev.at, }); } // Then the running auto-repeat spell's on the same unit, `0x614150([0xceac30], 0)` @@ -944,6 +1007,7 @@ pub(super) fn route_cast_visuals( spell_id: repeat, class: FxClass::Hold, force: false, + at: ev.at, }); } // A ranged-slot GO redraws ranged before the cast kit (`0x60f34c`, gate @@ -1027,6 +1091,7 @@ pub(super) fn route_cast_visuals( spell_id: ev.spell_id, class: FxClass::Hold, force: false, + at: ev.at, }); } } @@ -1035,6 +1100,8 @@ pub(super) fn route_cast_visuals( // Speed 0 plays every impact now (`0x6e8bf0`), with or without a caster unit (`0x6e816b`); // Speed > 0 flies the launcher's missiles from a caster unit ([`launcher_shots`]). for go in go_targets.read() { + // The GO's own place, not the frame's last cast edge's. + out.at = go.at; let Some(display) = spells.catalog.get(go.spell_id) else { continue; }; @@ -1123,7 +1190,11 @@ pub(super) fn route_cast_visuals( } // The channel poll: `UNIT_CHANNEL_SPELL` edges drive an observed unit's channel (the self-only - // `MSG_CHANNEL_*` do not); a cleared field drops the hold even with no other wire trace. + // `MSG_CHANNEL_*` do not); a cleared field drops the hold even with no other wire trace. Its + // kits carry no place, falling after every packet's: the channel's watcher (field 144) runs + // after the display's (131) in an update, and across packets the rebuild replays the + // channel's kit (`0x612a30`), so either side of a rebuild leaves it on the new body once. + out.at = None; for (entity, store) in &units { let cur = store.0.unit_channel_spell(); let prev = channel_cache.get(&entity).copied().unwrap_or(0); @@ -1143,6 +1214,7 @@ pub(super) fn route_cast_visuals( spell_id: prev, class: FxClass::Hold, force: true, + at: None, }); } // No ranged fallback on the channel poll (`0x612a30`). @@ -1162,6 +1234,8 @@ pub(super) fn route_cast_visuals( entity, spell_id: cur, kit_sound, + cast_hold: true, + at: None, }); } // The CharProc dispatcher's second caller (`0x612b18`): Drain Life's beam is @@ -1188,6 +1262,7 @@ pub(super) fn route_cast_visuals( class: FxClass::Hold, stage: FxStage::State, effects, + at: None, }); } } @@ -1206,6 +1281,7 @@ pub(super) fn route_cast_visuals( spell_id: prev, class: FxClass::Hold, force: true, + at: None, }); } } @@ -1249,19 +1325,19 @@ pub(crate) fn arm_aura_state_fx( let (Some(visuals), Some(spells)) = (visuals.as_deref(), spells.as_deref()) else { return; // no client data }; + // Each unit's kits carry the aura watcher's place, its first edge in the span (`0x604d00`). + let mut aura_at: EntityHashMap = EntityHashMap::default(); let scan = if full_sweep { units.iter().collect::>() } else { let mut due: EntityHashSet = arrived.iter().collect(); - due.extend( - edges - .read() - .filter(|e| { - e.unit_array_slot(AURA_WATCH_BASE, AURA_WATCH_SPAN) - .is_some() - }) - .map(|e| e.entity), - ); + for e in edges.read().filter(|e| { + e.unit_array_slot(AURA_WATCH_BASE, AURA_WATCH_SPAN) + .is_some() + }) { + due.insert(e.entity); + aura_at.entry(e.entity).or_insert(e.at); + } due.into_iter().filter_map(|e| units.get(e).ok()).collect() }; for (entity, store) in scan { @@ -1290,6 +1366,7 @@ pub(crate) fn arm_aura_state_fx( spell_id, class: FxClass::AuraState, force: true, + at: aura_at.get(&entity).copied(), }); procs.write(AuraProc::Reap { entity, spell_id }); // The forced stop takes the 0x1000 effect too (`0x614179`). @@ -1332,6 +1409,7 @@ pub(crate) fn arm_aura_state_fx( class: FxClass::AuraState, stage: FxStage::State, effects, + at: aura_at.get(&entity).copied(), }); } if !nodes.is_empty() { @@ -1347,6 +1425,8 @@ pub(crate) fn arm_aura_state_fx( entity, spell_id, kit_sound, + cast_hold: false, + at: aura_at.get(&entity).copied(), }); } // The stage-2 anim leg, on the add edge only: an aura refreshed in place keeps its id, @@ -1395,6 +1475,7 @@ pub(super) fn arm_loot_fx( let Some((effect, path)) = visuals.as_ref().and_then(|v| v.0.loot_art_effect()) else { return; // no client data or no such row }; + let mut loot_at: EntityHashMap = EntityHashMap::default(); let scan = if full_sweep { units.iter().collect::>() } else { @@ -1403,18 +1484,18 @@ pub(super) fn arm_loot_fx( FIELD_UNIT_MAXHEALTH, }; let mut due: EntityHashSet = arrived.iter().collect(); - due.extend( - edges - .read() - .filter(|e| { - e.unit_field(FIELD_UNIT_DYNAMIC_FLAGS) - || e.unit_field(FIELD_UNIT_HEALTH) - || e.unit_field(FIELD_UNIT_MAXHEALTH) - || (e.kind == benilla_protocol::messages::ObjectType::Corpse - && e.index == FIELD_CORPSE_DYNAMIC_FLAGS) - }) - .map(|e| e.entity), - ); + for e in edges.read().filter(|e| { + e.unit_field(FIELD_UNIT_DYNAMIC_FLAGS) + || e.unit_field(FIELD_UNIT_HEALTH) + || e.unit_field(FIELD_UNIT_MAXHEALTH) + || (e.kind == benilla_protocol::messages::ObjectType::Corpse + && e.index == FIELD_CORPSE_DYNAMIC_FLAGS) + }) { + due.insert(e.entity); + // The last of the fields to move, after which the unit reads dead and lootable. + let at = loot_at.entry(e.entity).or_insert(e.at); + *at = (*at).max(e.at); + } due.into_iter().filter_map(|e| units.get(e).ok()).collect() }; for (entity, store, net) in scan { @@ -1443,6 +1524,7 @@ pub(super) fn arm_loot_fx( effect, path: path.to_string(), }], + at: loot_at.get(&entity).copied(), }); } else if !lootable && armed.remove(&entity) { fx.write(SpellKitFx::Reap { @@ -1450,6 +1532,7 @@ pub(super) fn arm_loot_fx( spell_id: LOOT_FX_KEY, class: FxClass::Hold, force: true, + at: loot_at.get(&entity).copied(), }); } } @@ -1491,6 +1574,7 @@ pub(super) fn arm_level_up_fx( effect, path: path.to_string(), }], + at: Some(e.at), }); } } @@ -1531,6 +1615,7 @@ pub(super) fn arm_mount_poof_fx( effect, path: path.to_string(), }], + at: Some(e.at), }); } } @@ -1587,6 +1672,54 @@ pub(super) fn arm_morph_latch( latch.0.retain(|e, _| units.contains(*e)); } +/// The sound half of the rebuild's replay (`0x60abe0`): the aura slots' state kits (`0x5ff130` at +/// `0x60ada6`), then the channel's kit (`0x612a30` at `0x60adad`), played again as kind 2, ring +/// afresh at the rebuild's place over the sounds its loop faded. Their models re-arm in +/// `crate::entities::spell_fx`. +pub(crate) fn replay_rebuilt_kit_sounds( + mut swaps: MessageReader, + units: Query<&ObjectStore>, + visuals: Option>, + spells: Option>, + mut sounds: MessageWriter, +) { + for swap in swaps.read() { + let (Some(at), Ok(store)) = (swap.at, units.get(swap.entity)) else { + continue; // a game object's or corpse's new id, or a unit gone + }; + let (Some(visuals), Some(spells)) = (visuals.as_deref(), spells.as_deref()) else { + continue; // no client data + }; + let mut auras: Vec = Vec::new(); + for spell_id in store.0.unit_aura_ids().filter(|&s| s != 0) { + if !auras.contains(&spell_id) { + auras.push(spell_id); + } + } + let state = auras.into_iter().map(|spell_id| { + let kit = resolve_kit(spells, &visuals.0, spell_id, |s| s.state, || None); + (spell_id, kit, false) + }); + let channel = Some(store.0.unit_channel_spell()) + .filter(|&c| c != 0) + .map(|spell_id| { + let kit = resolve_kit(spells, &visuals.0, spell_id, |s| s.channel, || None); + (spell_id, kit, true) + }); + for (spell_id, kit, cast_hold) in state.chain(channel) { + if let Some(kit_sound) = kit.and_then(|k| k.sound) { + sounds.write(SpellKitSound::Replay { + entity: swap.entity, + spell_id, + kit_sound, + cast_hold, + at, + }); + } + } + } +} + /// The rebuild's impact-kit replay (the tail of `0x60abe0`): a display swap with the latch armed /// plays the latched impact kit and clears it, a frame later on the rebuilt body. The same-record, /// same-tag replace (`0x6208e0`) is not built, so a cold-cache shift-in briefly shows two clouds. @@ -1626,6 +1759,8 @@ pub(super) fn replay_morph_kit( shakes: &mut shakes, trails: &mut trails, recomputes: &mut recomputes, + // The replay rides the rebuild that was the frame before: after every packet. + at: None, }; play_kit( swap.entity, diff --git a/crates/benilla-app/src/creature_anim/spell_visual/tests.rs b/crates/benilla-app/src/creature_anim/spell_visual/tests.rs index 20b8587df..79385d3d3 100644 --- a/crates/benilla-app/src/creature_anim/spell_visual/tests.rs +++ b/crates/benilla-app/src/creature_anim/spell_visual/tests.rs @@ -108,6 +108,7 @@ fn cast_event(entity: Entity, spell_id: u32, kind: CastEventKind) -> CastEvent { spell_id, kind, seq: 1, + at: None, } } @@ -529,6 +530,7 @@ fn a_go_with_no_caster_unit_plays_its_instant_impacts() { target_mask: 0x2, ammo_display_id: None, seq: 1, + at: None, }); app.update(); let wounds: Vec = app @@ -540,6 +542,259 @@ fn a_go_with_no_caster_unit_plays_its_instant_impacts() { assert_eq!(wounds, [target]); } +/// A GO's impact kits carry the GO packet's place, not the frame's last cast edge's: a display +/// rebuild between the two (`0x60abe0`) ends the impact the wire put before it and spares the +/// cast after it, with or without a caster unit. +#[test] +fn a_gos_impact_kits_carry_the_gos_own_place() { + const BOLT: u32 = 5143; + const BOLT_VISUAL: u32 = 140; + const IMPACT_KIT: u32 = 301; + const IMPACT_SOUND: u32 = 3001; + let mut app = app(); + app.insert_resource(SpellVisuals(SpellVisualCatalog::from_tables( + HashMap::from([ + ( + BOLT_VISUAL, + VisualStages { + impact: IMPACT_KIT, + ..Default::default() + }, + ), + ( + VISUAL, + VisualStages { + precast: PRECAST_KIT, + ..Default::default() + }, + ), + ]), + HashMap::from([ + ( + IMPACT_KIT, + VisualKit { + sound: Some(IMPACT_SOUND), + ..Default::default() + }, + ), + (PRECAST_KIT, VisualKit::default()), + ]), + ))); + app.insert_resource(crate::ui_action::Spells { + catalog: SpellCatalog::from_displays(HashMap::from([ + ( + BOLT, + SpellDisplay { + visual: BOLT_VISUAL, + ..Default::default() + }, + ), + ( + SPELL, + SpellDisplay { + visual: VISUAL, + ..Default::default() + }, + ), + ])), + ..crate::ui_action::Spells::empty_for_tests() + }); + let target = app.world_mut().spawn_empty().id(); + let later_caster = app.world_mut().spawn_empty().id(); + let go = crate::lanes::stamp(); + let later = crate::lanes::stamp(); + app.world_mut().write_message(SpellGoTargets { + caster: None, + spell_id: BOLT, + hits: vec![target], + misses: Vec::new(), + source: None, + dest: None, + target_mask: 0x2, + ammo_display_id: None, + seq: 1, + at: Some(go), + }); + app.world_mut().write_message(CastEvent { + at: Some(later), + ..cast_event(later_caster, SPELL, CastEventKind::Start) + }); + app.update(); + let impacts: Vec<_> = app + .world_mut() + .resource_mut::>() + .drain() + .filter_map(|ev| match ev { + SpellKitSound::Play { + entity, + kit_sound: IMPACT_SOUND, + at, + .. + } => Some((entity, at)), + _ => None, + }) + .collect(); + assert_eq!(impacts, [(target, Some(go))]); +} + +/// A pushed kit (`SMSG_PLAY_SPELL_VISUAL`, an environmental hit's) carries its packet's place, so +/// a display rebuild after it (`0x60abe0`) ends it, and one before it spares it. +#[test] +fn a_pushed_kit_carries_its_packets_place() { + const PUSHED_KIT: u32 = 302; + const PUSHED_SOUND: u32 = 3002; + let mut app = app(); + app.insert_resource(SpellVisuals(SpellVisualCatalog::from_tables( + HashMap::new(), + HashMap::from([( + PUSHED_KIT, + VisualKit { + sound: Some(PUSHED_SOUND), + ..Default::default() + }, + )]), + ))); + let unit = app.world_mut().spawn_empty().id(); + let at = crate::lanes::stamp(); + app.world_mut().write_message(KitPush { + entity: unit, + kit_id: PUSHED_KIT, + seq: 1, + at: Some(at), + }); + app.update(); + let places: Vec<_> = app + .world_mut() + .resource_mut::>() + .drain() + .filter_map(|ev| match ev { + SpellKitSound::Play { + entity, + kit_sound: PUSHED_SOUND, + at, + .. + } => Some((entity, at)), + _ => None, + }) + .collect(); + assert_eq!(places, [(unit, Some(at))]); +} + +/// The rebuild replays the aura slots' state kits (`0x5ff130` at `0x60ada6`), then the channel's +/// (`0x612a30` at `0x60adad`), as kind 2: each sound rings afresh at the rebuild's place, an aura +/// in two slots once. A game object's new id replays nothing. +#[test] +fn a_rebuild_rings_the_aura_and_channel_kits_again() { + use benilla_protocol::field::{FIELD_UNIT_AURA, FIELD_UNIT_CHANNEL_SPELL}; + const AURA: u32 = 1459; // Arcane Intellect + const AURA_VISUAL: u32 = 150; + const AURA_KIT: u32 = 401; + const AURA_SOUND: u32 = 4001; + const CHANNEL: u32 = 5143; // Arcane Missiles + const CHANNEL_VISUAL: u32 = 151; + const CHANNEL_KIT: u32 = 402; + const AM_HUM: u32 = 3136; + let mut app = App::new(); + app.add_message::() + .add_message::() + .insert_resource(SpellVisuals(SpellVisualCatalog::from_tables( + HashMap::from([ + ( + AURA_VISUAL, + VisualStages { + state: AURA_KIT, + ..Default::default() + }, + ), + ( + CHANNEL_VISUAL, + VisualStages { + channel: CHANNEL_KIT, + ..Default::default() + }, + ), + ]), + HashMap::from([ + ( + AURA_KIT, + VisualKit { + sound: Some(AURA_SOUND), + ..Default::default() + }, + ), + ( + CHANNEL_KIT, + VisualKit { + sound: Some(AM_HUM), + ..Default::default() + }, + ), + ]), + ))) + .insert_resource(crate::ui_action::Spells { + catalog: SpellCatalog::from_displays(HashMap::from([ + ( + AURA, + SpellDisplay { + visual: AURA_VISUAL, + ..Default::default() + }, + ), + ( + CHANNEL, + SpellDisplay { + visual: CHANNEL_VISUAL, + ..Default::default() + }, + ), + ])), + ..crate::ui_action::Spells::empty_for_tests() + }) + .add_systems(Update, super::replay_rebuilt_kit_sounds); + let unit = app + .world_mut() + .spawn(crate::net::ObjectStore( + benilla_protocol::ObjectFields::from_pairs(&[ + (FIELD_UNIT_AURA, AURA), + (FIELD_UNIT_AURA + 1, AURA), + (FIELD_UNIT_CHANNEL_SPELL, CHANNEL), + ]), + )) + .id(); + let at = crate::lanes::stamp(); + let mut swaps = app + .world_mut() + .resource_mut::>(); + swaps.write(crate::entities::DisplaySwapped { + entity: unit, + at: Some(at), + }); + swaps.write(crate::entities::DisplaySwapped { + entity: unit, + at: None, + }); + app.update(); + let replays: Vec<_> = app + .world_mut() + .resource_mut::>() + .drain() + .filter_map(|ev| match ev { + SpellKitSound::Replay { + entity, + spell_id, + kit_sound, + at, + .. + } => Some((entity, spell_id, kit_sound, at)), + _ => None, + }) + .collect(); + assert_eq!( + replays, + [(unit, AURA, AURA_SOUND, at), (unit, CHANNEL, AM_HUM, at)] + ); +} + /// The ranged weapon-visual merge (`0x60d450`): a spell with `Attributes & 0x2` fills every zero /// slot of its own row from the caster's weapon visual; a non-ranged spell never looks. #[test] @@ -897,6 +1152,7 @@ fn a_harmful_go_wounds_each_hit_once_and_a_missile_arrival_always() { target_mask: 0x2, ammo_display_id: None, seq: 1, + at: None, }); app.update(); assert_eq!(wounds(&mut app), expected, "instant GO of spell {spell_id}"); @@ -1040,6 +1296,7 @@ fn missile_spawn_defers_iff_the_cast_kit_animates() { target_mask: 0x2, ammo_display_id: None, seq: 1, + at: None, }); } app.update(); @@ -1126,6 +1383,7 @@ fn a_targetless_dest_go_spawns_a_ground_missile_whose_arrival_sounds_at_the_poin target_mask: 0x40, ammo_display_id: None, seq: 1, + at: None, }); app.update(); let spawns: Vec<_> = app @@ -1157,6 +1415,7 @@ fn a_targetless_dest_go_spawns_a_ground_missile_whose_arrival_sounds_at_the_poin spell_id: GROUND, kind: CastEventKind::GroundImpact { pos: at }, seq: 2, + at: None, }); app.update(); let sounds: Vec<_> = app @@ -1950,6 +2209,7 @@ fn a_state_kits_anim_is_a_recompute_and_never_a_second_play() { target_mask: 0x2, ammo_display_id: None, seq: 1, + at: None, }); app.update(); @@ -2485,7 +2745,9 @@ fn esc_with_a_cursor_up_leaves_the_running_channels_hold_and_loop() { entity, spell_id, kit_sound: AM_HUM, - } => loops.rang(entity, spell_id, AM_HUM), + cast_hold, + at, + } => loops.rang(entity, (spell_id, AM_HUM), cast_hold, at), SpellKitSound::StopHold { entity, spell_id } => { loops.stop_hold(entity, spell_id); } @@ -2494,7 +2756,7 @@ fn esc_with_a_cursor_up_leaves_the_running_channels_hold_and_loop() { } assert_eq!( loops.held(me), - Some((ARCANE_MISSILES, AM_HUM)), + [(ARCANE_MISSILES, AM_HUM)], "Blizzard's reap stops none of the channel's sound (got {after:?})" ); } @@ -2531,7 +2793,9 @@ fn a_go_stops_the_running_auto_repeat_spells_hold_and_loop_on_its_caster() { entity, spell_id, kit_sound: AUTO_SHOT_LOOP, - } => loops.rang(entity, spell_id, AUTO_SHOT_LOOP), + cast_hold, + at, + } => loops.rang(entity, (spell_id, AUTO_SHOT_LOOP), cast_hold, at), SpellKitSound::StopHold { entity, spell_id } => { loops.stop_hold(entity, spell_id); } @@ -2595,7 +2859,7 @@ fn a_go_stops_the_running_auto_repeat_spells_hold_and_loop_on_its_caster() { ], ); assert_eq!(hold(&app, hunter), Some(AUTO_SHOT)); - assert_eq!(loops.held(hunter), Some((AUTO_SHOT, AUTO_SHOT_LOOP))); + assert_eq!(loops.held(hunter), [(AUTO_SHOT, AUTO_SHOT_LOOP)]); let reaped = frame( &mut app, @@ -2603,7 +2867,7 @@ fn a_go_stops_the_running_auto_repeat_spells_hold_and_loop_on_its_caster() { &[cast_event(hunter, ARCANE_SHOT, CastEventKind::Go)], ); assert_eq!(hold(&app, hunter), None, "the caster's Auto Shot hold"); - assert_eq!(loops.held(hunter), None, "and its loop"); + assert_eq!(loops.held(hunter), [], "and its loop"); assert_eq!( reaped, vec![(hunter, ARCANE_SHOT), (hunter, AUTO_SHOT)], @@ -2612,7 +2876,7 @@ fn a_go_stops_the_running_auto_repeat_spells_hold_and_loop_on_its_caster() { assert_eq!(hold(&app, other), Some(AUTO_SHOT), "another unit's hold"); assert_eq!( loops.held(other), - Some((AUTO_SHOT, AUTO_SHOT_LOOP)), + [(AUTO_SHOT, AUTO_SHOT_LOOP)], "and its loop" ); @@ -2629,7 +2893,7 @@ fn a_go_stops_the_running_auto_repeat_spells_hold_and_loop_on_its_caster() { &[cast_event(hunter, ARCANE_SHOT, CastEventKind::Go)], ); assert_eq!(hold(&app, hunter), Some(AUTO_SHOT)); - assert_eq!(loops.held(hunter), Some((AUTO_SHOT, AUTO_SHOT_LOOP))); + assert_eq!(loops.held(hunter), [(AUTO_SHOT, AUTO_SHOT_LOOP)]); } /// A GO of `hits` and `misses` (target, code, reflect byte) under `target_mask`, at `dest`. @@ -2649,6 +2913,7 @@ fn launcher_go( target_mask, ammo_display_id: None, seq: 1, + at: None, } } @@ -2789,6 +3054,7 @@ fn a_travelling_spell_without_a_missile_or_ammo_flies_nothing() { target_mask: 0x2, ammo_display_id, seq: 1, + at: None, }); app.update(); let spawns = app diff --git a/crates/benilla-app/src/entities.rs b/crates/benilla-app/src/entities.rs index 19674c46d..128727aac 100644 --- a/crates/benilla-app/src/entities.rs +++ b/crates/benilla-app/src/entities.rs @@ -10,6 +10,8 @@ use benilla_formats::{ CharSections, CharacterGeosets, CreatureCatalog, GameObjectCatalog, }; use benilla_protocol::EntityKind; +use bevy::ecs::schedule::ScheduleConfigs; +use bevy::ecs::system::ScheduleSystem; use bevy::prelude::*; use crate::net::NetEntity; @@ -54,7 +56,7 @@ pub(crate) mod corpse; mod live_display; use live_display::{refresh_live_display, tick_scale_ease}; -pub(crate) use live_display::{AppliedDisplay, DisplaySwapped}; +pub(crate) use live_display::{AppliedDisplay, DisplayRebuilt, DisplaySwapped}; /// Terrain conform: a model flagged `GlobalModelFlags & 3 ∈ {1,3}` tilts to the ground. mod conform; @@ -646,6 +648,22 @@ fn publish_world_units( } } +/// A display swap and a rig heal, then the frame's kit starts. A swap or heal ends what the unit +/// held before its visual goes, so none of this frame's starts spawns on the body it takes; the +/// starts then go in wire order, and those the packets put before the rebuild end at its place +/// ([`spell_fx::resolve_spell_fx`]). The rebuild's replayed kits ring in the same frame, after the +/// stop it raised ([`crate::creature_anim::replay_rebuilt_kit_sounds`]). +fn rebuild_then_start_kits() -> ScheduleConfigs { + ( + (refresh_live_display, live_display::heal_rig_starved).chain(), + ( + resolve_spell_fx, + crate::creature_anim::replay_rebuilt_kit_sounds, + ), + ) + .chain() +} + /// Loads the display catalogs at startup and each frame gives every net entity its visual. pub(crate) struct EntitiesPlugin; @@ -671,6 +689,7 @@ impl Plugin for EntitiesPlugin { // A display swap's rebuild edge, for the impact-kit replay the reference runs at the end // of `0x60abe0`. .add_message::() + .add_message::() .add_systems(Startup, setup_entities.after(AssetSet::Open)) // The eviction ahead of the VM pass, whose sends read the item displays for a ranged // spell's precast visual (`0x60d450`). @@ -709,7 +728,7 @@ impl Plugin for EntitiesPlugin { // 20-element ceiling. (resolve_equipment, resolve_corpse_equipment), // Spell effects and missiles create `SpellFx` entries too. - resolve_spell_fx, + rebuild_then_start_kits(), move_missiles, spawn_missiles, // The dest-anchored lane's three independent producers also create entries. @@ -745,14 +764,9 @@ impl Plugin for EntitiesPlugin { // A mount change re-seats the rig on the mount's attachment 0 or back, as the // reference re-parents the body model (`0x712f70`, `0x713020`). reseat_mounts, - // A display swap rebuilds, a scale change eases over the reference's 2 s, and a - // unit denied a palette rig rebuilds once the table has room. - ( - refresh_live_display, - tick_scale_ease, - live_display::heal_rig_starved, - ) - .chain(), + // A scale change eases over the reference's 2 s, after attach so a rebuild's snap + // never draws. + tick_scale_ease, ) .chain() .in_set(EntityVisualsSet) diff --git a/crates/benilla-app/src/entities/chain_beam.rs b/crates/benilla-app/src/entities/chain_beam.rs index 78180608c..f58ffb702 100644 --- a/crates/benilla-app/src/entities/chain_beam.rs +++ b/crates/benilla-app/src/entities/chain_beam.rs @@ -891,6 +891,7 @@ mod tests { spell_id: CHAIN_SPELL, class: FxClass::Hold, force: true, + at: None, }); app.update(); assert_eq!( diff --git a/crates/benilla-app/src/entities/dest_fx.rs b/crates/benilla-app/src/entities/dest_fx.rs index f3e13f0fd..3b765db43 100644 --- a/crates/benilla-app/src/entities/dest_fx.rs +++ b/crates/benilla-app/src/entities/dest_fx.rs @@ -166,6 +166,9 @@ pub(super) fn arm_ground_effects( entity: anchor, spell_id, kit_sound, + cast_hold: false, + // A ground arrival of the frame, after every packet. + at: None, }); } } diff --git a/crates/benilla-app/src/entities/live_display.rs b/crates/benilla-app/src/entities/live_display.rs index b9ebe7396..0f37c5906 100644 --- a/crates/benilla-app/src/entities/live_display.rs +++ b/crates/benilla-app/src/entities/live_display.rs @@ -5,10 +5,12 @@ //! The reference's display handler rebuilds the model (`0x60abe0`) when the display record changed //! (`0x60ae10`, against `[unit+0xb34]`), re-resolving its model facts (`0x60afb0`) and the stand or //! ride animation (`0x60ce70`): an instant swap, a ghost's revive too. Its scale handler eases -//! over 2 s with a cosine smoothstep (`0x614bbf`). Here a swap tears the visual down for -//! `attach_entity_visuals` to rebuild without the spawn fade; the unit's -//! [`super::spell_fx::FxAttached`] list outlives it, as the reference's per-unit `+0xb4` effect -//! list does, so persistent effects re-spawn on the new body. +//! over 2 s with a cosine smoothstep (`0x614bbf`). A unit swaps on the update's own rebuild +//! ([`DisplayRebuilt`]), so a new display naming the same records keeps the body, as `0x60ae10` +//! keeps it; a game object or corpse swaps on any new id. Here a swap tears the visual down for +//! `attach_entity_visuals` to rebuild without the spawn fade, first ending the unit's spell effects +//! as the rebuild's `0x60ac10` loop does ([`super::spell_fx::RebuildFx`]); the auras' and the +//! channel's re-spawn on the new body. use benilla_protocol::EntityKind; use bevy::prelude::*; @@ -21,16 +23,32 @@ use super::{Creatures, VisualAttached}; /// The reference's scale-ease window: 2 s, cosine smoothstep (`0x614bbf`). const SCALE_EASE_SECS: f32 = 2.0; -/// The display id the visual was built with, cube included: [`refresh_live_display`]'s diff key. +/// The display id the visual was built with, cube included: [`refresh_live_display`]'s diff key +/// for a game object or corpse. #[derive(Component)] pub(crate) struct AppliedDisplay(pub(crate) Option); -/// A live display swap tore this entity's visual down. The reference's rebuild ends by replaying -/// the pending morph's impact kit (`0x60ad67`), which `crate::creature_anim`'s morph-latch watcher -/// does on this edge. +/// The reference rebuilt this unit's display (`0x60abe0`): the display watcher (`0x6049b5`), the +/// health watcher's return to life (`0x604835`) or slot 3 (`0x5fbe28`) found a new record set +/// (`0x60ae10`). `at` is that watcher's place among the packet's edges. +#[derive(Message, Clone, Copy, Debug)] +pub(crate) struct DisplayRebuilt { + pub(crate) entity: Entity, + pub(crate) at: crate::lanes::Stamp, + /// The row it resolved (`+0xb34`, `0x60afb0`), which the body is built from: a later + /// packet's id naming the same records (`0x60ae10`) leaves it. + pub(crate) display_id: u32, +} + +/// A unit's display rebuild, or a game object's or corpse's new display id, which tore down the +/// visual it had. The reference's rebuild ends by replaying the pending morph's impact kit +/// (`0x60ad67`), which `crate::creature_anim`'s morph-latch watcher does on this edge. #[derive(Message, Clone, Copy)] pub(crate) struct DisplaySwapped { pub(crate) entity: Entity, + /// A unit's rebuild's place ([`DisplayRebuilt::at`]), at which `spell_fx` ends the kit starts + /// the wire put before it; `None` for a game object's or corpse's new id. + pub(crate) at: Option, } /// A render-scale ease toward [`NetEntity::scale`], written absolutely each tick so a mid-ease @@ -64,10 +82,31 @@ fn live_display_id(kind: EntityKind, store: &ObjectStore) -> Option { } } -/// Diff each attached entity's live appearance against what its visual was built with: a display -/// move swaps the model, a scale move arms the 2 s ease, and a change of the native display or -/// `SCALE_X` restamps [`CollisionHeight`]. Mount children carry no [`ObjectStore`]: their display -/// is the host's field, diffed by `mount::reseat_mounts`. +/// A unit's rebuild at `at` (`0x60abe0`), short of its body: the loop `0x60ac10` stops every +/// effect the unit holds, kit sounds included (`0x61fedc`), and the tail replays the morph latch +/// (`0x60ad67`) off [`DisplaySwapped`]. +fn rebuild_edges( + commands: &mut Commands, + fx: &mut super::spell_fx::RebuildFx, + swapped: &mut MessageWriter, + kit_sounds: &mut MessageWriter, + entity: Entity, + at: crate::lanes::Stamp, +) { + swapped.write(DisplaySwapped { + entity, + at: Some(at), + }); + fx.end_all(commands, entity, super::spell_fx::Rebuild::Display); + kit_sounds.write(crate::creature_anim::SpellKitSound::Rebuilt { entity, at }); +} + +/// Each attached entity's live appearance against what its visual was built with: a unit's +/// rebuild ([`DisplayRebuilt`]) or a game object's or corpse's new display id swaps the model, a +/// scale move arms the 2 s ease, and a change of the native display or `SCALE_X` restamps +/// [`CollisionHeight`]. A unit rebuilt before its visual attached builds the new display. +/// Mount children carry no [`ObjectStore`]: their display is the host's field, diffed by +/// `mount::reseat_mounts`. #[allow(clippy::type_complexity)] pub(super) fn refresh_live_display( mut commands: Commands, @@ -88,45 +127,98 @@ pub(super) fn refresh_live_display( Without, ), >, + mut unattached: Query< + &mut NetEntity, + ( + Without, + Without, + ), + >, + mut rebuilds: MessageReader, mut swapped: MessageWriter, + mut fx: super::spell_fx::RebuildFx, + mut kit_sounds: MessageWriter, ) { + // This frame's unit rebuilds, the latest of each unit's with the row it resolved: it ends + // whatever came before it. + let mut rebuilt: bevy::ecs::entity::EntityHashMap<(crate::lanes::Stamp, u32)> = default(); + for r in rebuilds.read() { + let latest = rebuilt.entry(r.entity).or_insert((r.at, r.display_id)); + if r.at >= latest.0 { + *latest = (r.at, r.display_id); + } + } + for (&entity, &(at, row)) in &rebuilt { + // A unit with no body yet rebuilds all the same, short of the teardown: what it held + // ends, the morph latch replays, and the body built later is the row's. + if let Ok(mut net) = unattached.get_mut(entity) { + net.display_id = Some(row); + rebuild_edges( + &mut commands, + &mut fx, + &mut swapped, + &mut kit_sounds, + entity, + at, + ); + } + } for (entity, guid, mut net, store, applied, height, tf) in &mut entities { - // ── The display-id swap ────────────────────────────────────────────────────────────── - let live = live_display_id(net.kind, store); - if let Some(live) = live { - if applied.0 != Some(live) { - info!( - "display swap: guid {:016x} {:?} {:?} -> {} (instant, the 0x60abe0 rebuild)", - guid.0, net.kind, applied.0, live - ); - net.display_id = Some(live); - swapped.write(DisplaySwapped { entity }); - // `attach_entity_visuals` rebuilds from scratch, fade-skipped by `Reattached`. - commands - .entity(entity) - .despawn_related::() - .remove::<( - VisualAttached, - AppliedDisplay, - super::equipment::AppliedEquipment, - super::mount::AppliedMount, - super::mount::MountChild, - AnimationPlayer, - bevy::animation::transition::AnimationTransitions, - AnimationGraphHandle, - benilla_assets::ModelAnimations, - crate::creature_anim::AnimDriver, - ( - benilla_world::rig_anim::RigPose, - crate::creature_anim::BodyTwist, - benilla_world::rig_anim::GlobalSeqDrive, - ), - benilla_world::rig_palette::RigSkin, - super::BoneAttach, - super::equipment::HeldAttached, - )>() - .insert(super::equipment::Reattached); + // ── The display swap ───────────────────────────────────────────────────────────────── + // A unit takes the row its update's rebuild resolved; any other kind, a new field. + let swap = match net.kind { + EntityKind::Unit | EntityKind::Player => { + rebuilt.get(&entity).map(|&(at, row)| (row, Some(at))) } + _ => live_display_id(net.kind, store) + .filter(|&live| applied.0 != Some(live)) + .map(|live| (live, None)), + }; + if let Some((to, at)) = swap { + info!( + "display swap: guid {:016x} {:?} {:?} -> {} (instant, the 0x60abe0 rebuild)", + guid.0, net.kind, applied.0, to + ); + net.display_id = Some(to); + match at { + Some(at) => rebuild_edges( + &mut commands, + &mut fx, + &mut swapped, + &mut kit_sounds, + entity, + at, + ), + None => { + swapped.write(DisplaySwapped { entity, at: None }); + fx.end_all(&mut commands, entity, super::spell_fx::Rebuild::Display); + } + } + // `attach_entity_visuals` rebuilds from scratch, fade-skipped by `Reattached`. + commands + .entity(entity) + .despawn_related::() + .remove::<( + VisualAttached, + AppliedDisplay, + super::equipment::AppliedEquipment, + super::mount::AppliedMount, + super::mount::MountChild, + AnimationPlayer, + bevy::animation::transition::AnimationTransitions, + AnimationGraphHandle, + benilla_assets::ModelAnimations, + crate::creature_anim::AnimDriver, + ( + benilla_world::rig_anim::RigPose, + crate::creature_anim::BodyTwist, + benilla_world::rig_anim::GlobalSeqDrive, + ), + benilla_world::rig_palette::RigSkin, + super::BoneAttach, + super::equipment::HeldAttached, + )>() + .insert(super::equipment::Reattached); } // ── The scale ease ─────────────────────────────────────────────────────────────────── @@ -218,6 +310,7 @@ pub(super) fn heal_rig_starved( Without, ), >, + mut fx: super::spell_fx::RebuildFx, ) { if starved.is_empty() || palettes.slot_headroom() < HEAL_MIN_HEADROOM { return; @@ -227,6 +320,7 @@ pub(super) fn heal_rig_starved( "rig heal: guid {:016x} was denied a palette rig at attach — rebuilding (0x60abe0 shape)", guid.0 ); + fx.end_all(&mut commands, entity, super::spell_fx::Rebuild::Heal); commands .entity(entity) .despawn_related::() @@ -331,6 +425,8 @@ mod tests { let mut app = App::new(); app.init_resource::(); app.add_message::(); + app.add_message::(); + app.add_message::(); app.add_systems(Update, (refresh_live_display, heal_rig_starved)); let unit = |app: &mut App, display: u32| { app.world_mut() @@ -356,6 +452,7 @@ mod tests { app.world_mut() .entity_mut(healed) .insert(benilla_world::rig_palette::RigStarved); + rebuild(&mut app, swapped); app.update(); for (e, what) in [(swapped, "the swap"), (healed, "the heal")] { let e = app.world().entity(e); @@ -374,6 +471,150 @@ mod tests { } } + /// The update's rebuild of `unit` to the descriptor's display, as its display watcher + /// announces it, at its stamp. + fn rebuild(app: &mut App, unit: Entity) -> crate::lanes::Stamp { + rebuild_to(app, unit, 8) + } + + /// [`rebuild`] to the row `display_id`. + fn rebuild_to(app: &mut App, unit: Entity, display_id: u32) -> crate::lanes::Stamp { + let at = crate::lanes::stamp(); + app.world_mut() + .resource_mut::>() + .write(DisplayRebuilt { + entity: unit, + at, + display_id, + }); + at + } + + /// An app running the swap, and a unit built in display 7 whose descriptor now says 8. + fn displayed_unit(attached: bool) -> (App, Entity) { + let mut app = App::new(); + app.init_resource::(); + app.add_message::(); + app.add_message::(); + app.add_message::(); + app.add_systems(Update, refresh_live_display); + let unit = app + .world_mut() + .spawn(( + crate::net::Guid(0xB0B), + NetEntity { + kind: EntityKind::Unit, + display_id: Some(7), + scale: 1.0, + }, + ObjectStore(benilla_protocol::ObjectFields::from_pairs(&[( + benilla_protocol::field::FIELD_UNIT_DISPLAYID, + 8, + )])), + Transform::default(), + )) + .id(); + if attached { + app.world_mut() + .entity_mut(unit) + .insert((VisualAttached, AppliedDisplay(Some(7)))); + } + (app, unit) + } + + /// `0x60abe0` rebuilds only when `0x60ae10` finds a new record set (`0x60abeb`): a display + /// naming the same extra, model and sound rows keeps the body, so a new id alone swaps nothing. + #[test] + fn a_unit_swaps_only_on_the_updates_rebuild_not_on_a_new_id() { + let (mut app, unit) = displayed_unit(true); + app.update(); + assert!( + app.world().entity(unit).contains::(), + "a new id the rebuild's gate passed over keeps the body" + ); + rebuild(&mut app, unit); + app.update(); + let e = app.world().entity(unit); + assert!(!e.contains::(), "the rebuild swaps it"); + assert_eq!(e.get::().unwrap().display_id, Some(8)); + } + + /// The rebuild's loop `0x60ac10` stops the kit sounds it links (`0x61fedc`) with the rest: + /// the swap hands the sound side the rebuild's stamp, to stop the hold loops rung before it. + #[test] + fn a_units_rebuild_hands_its_stamp_to_the_kit_sounds() { + use crate::creature_anim::SpellKitSound; + let (mut app, unit) = displayed_unit(true); + let at = rebuild(&mut app, unit); + app.update(); + let edges: Vec<_> = app + .world_mut() + .resource_mut::>() + .drain() + .filter_map(|ev| match ev { + SpellKitSound::Rebuilt { entity, at } => Some((entity, at)), + _ => None, + }) + .collect(); + assert_eq!(edges, [(unit, at)]); + } + + /// The rebuild resolves its row at its watcher (`0x60afb0`), and a later packet's id naming + /// the same records leaves it (`0x60ae10`): the body is the rebuilt row's, not the frame's + /// last field's, built or not. + #[test] + fn a_rebuild_builds_the_row_it_resolved_not_the_frames_last_field() { + for attached in [true, false] { + let (mut app, unit) = displayed_unit(attached); + rebuild_to(&mut app, unit, 9); + app.update(); + assert_eq!( + app.world().get::(unit).unwrap().display_id, + Some(9), + "attached: {attached}" + ); + } + } + + /// The reference rebuilds the unit, built or not (`0x60abe0`): with no body yet only the + /// teardown is moot, so the effects end, the kit sounds stop and the morph latch replays. + #[test] + fn a_unit_with_no_body_yet_rebuilds_short_of_the_teardown() { + use crate::creature_anim::SpellKitSound; + let (mut app, unit) = displayed_unit(false); + let at = rebuild(&mut app, unit); + app.update(); + let swaps: Vec<_> = app + .world_mut() + .resource_mut::>() + .drain() + .map(|s| (s.entity, s.at)) + .collect(); + assert_eq!(swaps, [(unit, Some(at))], "the morph latch's replay edge"); + let stops: Vec<_> = app + .world_mut() + .resource_mut::>() + .drain() + .filter_map(|ev| match ev { + SpellKitSound::Rebuilt { entity, at } => Some((entity, at)), + _ => None, + }) + .collect(); + assert_eq!(stops, [(unit, at)], "the kit sounds' stop"); + } + + /// A unit rebuilt before its visual attached builds the new display, not its create's. + #[test] + fn a_unit_rebuilt_before_its_visual_attached_builds_the_new_display() { + let (mut app, unit) = displayed_unit(false); + rebuild(&mut app, unit); + app.update(); + assert_eq!( + app.world().get::(unit).unwrap().display_id, + Some(8) + ); + } + #[test] fn a_torn_down_starved_unit_is_not_rebuilt() { let mut app = App::new(); diff --git a/crates/benilla-app/src/entities/missile.rs b/crates/benilla-app/src/entities/missile.rs index c71b1c783..c95af7ebc 100644 --- a/crates/benilla-app/src/entities/missile.rs +++ b/crates/benilla-app/src/entities/missile.rs @@ -446,6 +446,7 @@ fn arrival_handoff( spell_id, kind, seq: play_seq.next(), + at: None, }; match missile.aim { Aim::Unit { diff --git a/crates/benilla-app/src/entities/spell_fx/mod.rs b/crates/benilla-app/src/entities/spell_fx/mod.rs index 77837979c..47281ca2e 100644 --- a/crates/benilla-app/src/entities/spell_fx/mod.rs +++ b/crates/benilla-app/src/entities/spell_fx/mod.rs @@ -9,7 +9,8 @@ //! //! A persistent instance lives until its spell's [`SpellKitFx::Reap`] (`0x614150`); a //! self-terminating one despawns after one pass of its model's sequence 0, a span clock standing in -//! for the client's completion callback. +//! for the client's completion callback. A rebuild of the unit's visual ends both first +//! ([`RebuildFx`]). mod lifecycle; @@ -527,11 +528,17 @@ pub(super) fn tend_world_plants( } } -/// Apply the router's [`SpellKitFx`] edges per unit in emission order: `Begin` records instances, -/// `Reap` decays the spell's persistent ones, so a GO's precast dies before its release flash. +/// Apply the routers' [`SpellKitFx`] edges per unit in the order the packets raised them +/// ([`SpellKitFx::at`], the frame's own last): `Begin` records instances, `Reap` decays the +/// spell's persistent ones, so a GO's precast dies before its release flash. A display rebuild +/// this frame ([`crate::entities::DisplaySwapped::at`]) has already ended what the unit held; the +/// edges the wire put before it end there too, as the rebuild's loop `0x60ac10` stops what was +/// started before the display's watcher ran ([`Rebuild::Display`]), and those after it stand. +#[allow(clippy::too_many_arguments)] // one Bevy system's full input set pub(super) fn resolve_spell_fx( mut commands: Commands, mut events: MessageReader, + mut swaps: MessageReader, mut units: Query<&mut FxAttached>, fx: Option>, time: Res