diff --git a/rust/crates/aaemu-game/PORTING.md b/rust/crates/aaemu-game/PORTING.md index 9ec47922e9..b25572cda7 100644 --- a/rust/crates/aaemu-game/PORTING.md +++ b/rust/crates/aaemu-game/PORTING.md @@ -1224,6 +1224,24 @@ is no longer mirrored (see "Known deviations", dead NPC returned and healed). fabricated. RedeemBuff's second and third value slots (the damage / cap slots of other special actions), recipe/account/cinema placeholders, several other special actions and generic Anchor/Flowgraph still need authoritative behavior or protocol evidence. Passing covered tests does not certify every data-defined skill. +* systems::skills (2026-10-27, `check_obstacle`): the C# tree had no obstacle check at all + (`git show a2ab80abd^:AAEmu.Game/Models/Game/Skills/Skill.cs`), but the 1.2 skills data has + the retail column. The 1.2 client is the oracle (`x2game_unpacked.dll`; Ghidra `aa12u`): + `FUN_390B5540` calls `RayWorldIntersection(Game)` with objtypes **7** (static + sleeping rigid + + rigid), or **0x107** when `check_terrain` is set, ray flags **0x2048F** (colltype-any, + collision class 2, ignore back faces, pierceability 15) and one hit; a hit maps to skill + result 0xC (`skill_obstacle`). `FUN_390B5650` shortens the end point by the target radius and + casts no ray when `distance <= radius`. Unit targets (`FUN_390B5C60` -> `FUN_3989C0D0`) run at + unit position height and skip the source and target colliders; position (ground) targets + (`FUN_390B5A30` -> `FUN_390B5790`) use the constant 0.5 (`DAT_3999C47C`) and skip only the + source. The former Rust ray was 1 m above both ends, used static+terrain (0x101) and the + default collision class, and had no radius gate; it rejected ground-anchored doodad + interactions through nearby doodad colliders (entrance doodad template 3674 / skill 10999, + exit 4535 / skill 16560) and re-checked at fire time with the same wrong rule. Rejections are + logged at `warn` with character, skill, target and result key. The flow tests in + `cast_validation_tests.rs` cover the height/mask/class rule, the radius gate, the 0.5 m + position radius and the terrain gate. Not modelled: the multi-part target variant + `FUN_3989DAE0` (loops the target's sub-shapes and passes if any part passes). * systems::skills: the labor margin requirement uses `GetMaxLabor(Payment.PremiumState)`; every connection has the default C# `AccountPayment` (premium until 2030), so the cap is 5000. * systems::skills (in-place resurrection, 2026-10-20): `CSResurrectCharacterPacket(in_place = diff --git a/rust/crates/aaemu-game/src/systems/ai/collision/flow_tests.rs b/rust/crates/aaemu-game/src/systems/ai/collision/flow_tests.rs index 6de946d870..045ba0d512 100644 --- a/rust/crates/aaemu-game/src/systems/ai/collision/flow_tests.rs +++ b/rust/crates/aaemu-game/src/systems/ai/collision/flow_tests.rs @@ -142,6 +142,42 @@ async fn queries_and_doodad_colliders() { assert!((g - 100.0).abs() < 1e-3); } +#[tokio::test] +async fn unit_colliders_are_skippable_by_obj_id() { + let s = server(100.0); + let pw = super::phys_world(&s, 0).unwrap(); + // A living proxy is stored under `living`, not `objects`: the ignore list must find it + // there, or a caster's or target's own collider can block its interaction ray. + pw.lock().sync_living( + 7, + &super::data::LivingParams::default(), + Vec3::new(500.0, 500.0, 100.0), + ); + let from = [495.0, 500.0, 100.5]; + let to = [505.0, 500.0, 100.5]; + let query = |ignored| { + super::raycast_ignoring_objects( + &s, + 0, + from, + to, + super::query::mask::ALL_TERRAIN_WATER, + super::query::ray_flags::WZ_RAYCASTING, + ignored, + ) + }; + assert!(matches!( + query(&[]), + Some(Some(hit)) + if hit.owner + == Some(Owner::Object { + kind: ObjKind::Unit, + obj_id: 7, + }) + )); + assert_eq!(query(&[(ObjKind::Unit, 7)]), Some(None)); +} + #[tokio::test] async fn zone_fall_gate_in_the_movement_path() { // ground above the synthetic world's ocean level (100): no swimming diff --git a/rust/crates/aaemu-game/src/systems/ai/collision/mod.rs b/rust/crates/aaemu-game/src/systems/ai/collision/mod.rs index 0a104ff0b6..694df73176 100644 --- a/rust/crates/aaemu-game/src/systems/ai/collision/mod.rs +++ b/rust/crates/aaemu-game/src/systems/ai/collision/mod.rs @@ -392,7 +392,11 @@ pub fn raycast_ignoring_objects( let mut st = pw.lock(); let skip: Vec<_> = ignored .iter() - .filter_map(|key| st.objects.get(key).map(|c| c.ent)) + .filter_map(|key| match key.0 { + // Living proxies are the units' colliders; they are not in `objects`. + world::ObjKind::Unit => st.living.get(&key.1).copied(), + _ => st.objects.get(key).map(|c| c.ent), + }) .collect(); Some(st.raycast(a, b, objtypes, flags, &skip)) } diff --git a/rust/crates/aaemu-game/src/systems/ai/collision/query.rs b/rust/crates/aaemu-game/src/systems/ai/collision/query.rs index 40ea01c492..d842b493d8 100644 --- a/rust/crates/aaemu-game/src/systems/ai/collision/query.rs +++ b/rust/crates/aaemu-game/src/systems/ai/collision/query.rs @@ -18,6 +18,9 @@ pub mod mask { /// static + sleeping + rigid + terrain: unmount check `sub_3972C690`, building /// `sub_398351E0`. pub const SOLID_TERRAIN: u32 = 0x107; + /// static + sleeping + rigid: the 1.2 skill obstacle ray without `check_terrain` + /// (`FUN_390B5540`, `x2game.dll.c:149916`). + pub const SOLID: u32 = 0x007; /// all entities + terrain: tornado `sub_3920FE10`, surface id `sub_394B6660`. pub const ALL_TERRAIN: u32 = 0x11F; /// all + terrain + water: `WZRayCasting` `sub_3936F340`. @@ -34,6 +37,9 @@ pub mod mask { pub mod ray_flags { /// `rwi_stop_at_pierceable | rwi_ignore_back_faces` (0x8F): most x2game rays. pub const DEFAULT: u32 = 0x8F; + /// Skill obstacle ray (`FUN_390B5540`, `x2game.dll.c:149930`): colltype-any with + /// collision class 2, ignore back faces, pierceability 15. + pub const SKILL_OBSTACLE: u32 = 0x0002_048F; /// `WZRayCasting` (0xFFFE048F: colltype-any with every class but 0). pub const WZ_RAYCASTING: u32 = 0xFFFE_048F; } diff --git a/rust/crates/aaemu-game/src/systems/skills/cast_validation_tests.rs b/rust/crates/aaemu-game/src/systems/skills/cast_validation_tests.rs index b29d862658..6b45d4f35a 100644 --- a/rust/crates/aaemu-game/src/systems/skills/cast_validation_tests.rs +++ b/rust/crates/aaemu-game/src/systems/skills/cast_validation_tests.rs @@ -917,6 +917,326 @@ async fn obstacle_checks_reject_a_physical_wall_before_any_cast_mutation() { assert_eq!(cast(&server, &skill, &player, &npc).0, 0); } +/// 1.2 client `FUN_390B5540` (`x2game.dll.c:149877`): the obstacle ray runs at unit +/// position height, uses the static/sleeping/rigid classes with collision class 2 (terrain +/// only with `check_terrain`), and skips the source and target colliders. +#[tokio::test] +async fn obstacle_ray_matches_the_retail_height_mask_and_collision_class() { + use crate::systems::ai::collision::models::{ModelPart, ModelPhys}; + use crate::systems::ai::collision::world::{CellContent, StaticInstance}; + use aaemu_physics::geometry::primitives::{BoxPrim, Primitive}; + use aaemu_physics::math::{Quat, Vec3}; + + struct Flat(f32); + impl crate::systems::ai::collision::terrain::TerrainSource for Flat { + fn sample(&self, _x: i32, _y: i32) -> f32 { + self.0 + } + fn size(&self) -> (i32, i32) { + (1024, 1024) + } + } + + fn check( + terrain_z: f32, + wall: Vec3, + wall_flags: u32, + check_terrain: bool, + source_z: f32, + target_z: f32, + ) -> Result<(), (u8, u32)> { + let (server, _client, player, _npc) = fixture(GameData::empty()); + server + .systems + .ai + .collision + .test_files + .set(Arc::new(std::collections::HashMap::new())) + .unwrap(); + server + .systems + .ai + .collision + .test_terrain + .set(Arc::new(Flat(terrain_z))) + .unwrap_or_else(|_| panic!("terrain")); + let physics = crate::systems::ai::collision::phys_world(&server, 0).unwrap(); + let geometry = physics + .lock() + .world + .register_primitive(&Primitive::Box(BoxPrim::aligned( + Vec3::ZERO, + Vec3::new(1.0, 0.5, 0.5), + ))) + .unwrap(); + physics.lock().insert_cell(CellContent { + cell: (0, 0), + instances: vec![StaticInstance { + model: Arc::new(ModelPhys { + path: "test/skill-obstacle.cgf".into(), + parts: vec![ModelPart { + slot: 0, + geom: geometry, + }], + blob_bytes: 0, + }), + pos: wall, + q: Quat::IDENTITY, + scale: 1.0, + flags_default: wall_flags, + density: 0.0, + ent_flags_or: 0x8000, + vegetation: false, + }], + ..Default::default() + }); + player.data().unit.transform.set_xyz(100.0, 100.0, source_z); + let mut crop = crate::models::doodad::Doodad::new(1); + crop.transform.set_xyz(100.0, 110.0, target_z); + let crop = Entity::new(0x300, crop); + let crop_target = Target::Obj(WorldObject::from(&crop)); + let skill = SkillRun::new( + Arc::new(SkillTemplate { + check_obstacle: true, + check_terrain, + target_type: super::types::SkillTargetType::Doodad as i32, + ..super::tests::skill_template(3) + }), + 1, + ); + super::validation::check(&server, &skill, &WorldObject::from(&player), &crop_target) + } + + // The old +1 m ray hit a wall above the feet; the retail ray at position height does not. + assert!(check( + 100.1, + Vec3::new(100.0, 105.0, 101.5), + 0x18FFF, + false, + 100.0, + 100.0 + ) + .is_ok()); + // A part without collision class 2 is not an obstacle to the skill ray (0x8000 only). + assert!(check( + 100.1, + Vec3::new(100.0, 105.0, 100.0), + 0x18FFD, + false, + 100.0, + 100.0 + ) + .is_ok()); + // A class-2 static part still blocks. + assert_eq!( + check( + 100.1, + Vec3::new(100.0, 105.0, 100.0), + 0x18FFF, + false, + 100.0, + 100.0 + ) + .unwrap_err() + .0, + skill_result::OBSTACLE + ); + // Terrain joins the mask only with `check_terrain`. + assert!(check( + 101.0, + Vec3::new(100.0, 50.0, 50.0), + 0x18FFF, + false, + 102.0, + 100.0 + ) + .is_ok()); + assert_eq!( + check( + 101.0, + Vec3::new(100.0, 50.0, 50.0), + 0x18FFF, + true, + 102.0, + 100.0 + ) + .unwrap_err() + .0, + skill_result::OBSTACLE + ); +} + +/// 1.2 `FUN_390B5650` (`x2game.dll.c:149951`) casts no ray at all when `dist <= radius`; +/// position (ground) targets (`FUN_390B5A30` -> `FUN_390B5790`) use radius 0.5 +/// (`DAT_3999C47C`) and skip only the source. +#[tokio::test] +async fn obstacle_ray_skips_inside_radius_and_uses_the_position_radius() { + use crate::models::transform::PositionAndRotation; + use crate::systems::ai::collision::models::{ModelPart, ModelPhys}; + use crate::systems::ai::collision::world::{CellContent, StaticInstance}; + use aaemu_physics::geometry::primitives::{BoxPrim, Primitive}; + use aaemu_physics::math::{Quat, Vec3}; + + struct Flat; + impl crate::systems::ai::collision::terrain::TerrainSource for Flat { + fn sample(&self, _x: i32, _y: i32) -> f32 { + 100.0 + } + fn size(&self) -> (i32, i32) { + (1024, 1024) + } + } + + fn setup( + data: GameData, + wall: Vec3, + half: Vec3, + ) -> (Arc, TestClient, PlayerRef, NpcRef) { + let (server, client, player, npc) = fixture(data); + server + .systems + .ai + .collision + .test_files + .set(Arc::new(std::collections::HashMap::new())) + .unwrap(); + server + .systems + .ai + .collision + .test_terrain + .set(Arc::new(Flat)) + .unwrap_or_else(|_| panic!("terrain")); + let physics = crate::systems::ai::collision::phys_world(&server, 0).unwrap(); + let geometry = physics + .lock() + .world + .register_primitive(&Primitive::Box(BoxPrim::aligned(Vec3::ZERO, half))) + .unwrap(); + physics.lock().insert_cell(CellContent { + cell: (0, 0), + instances: vec![StaticInstance { + model: Arc::new(ModelPhys { + path: "test/skill-obstacle-radius.cgf".into(), + parts: vec![ModelPart { + slot: 0, + geom: geometry, + }], + blob_bytes: 0, + }), + pos: wall, + q: Quat::IDENTITY, + scale: 1.0, + flags_default: 0x18FFF, + density: 0.0, + ent_flags_or: 0x8000, + vegetation: false, + }], + ..Default::default() + }); + (server, client, player, npc) + } + + fn position(x: f32, y: f32, z: f32) -> Target { + Target::Pos(super::geometry::PosUnit { + instance_id: 0, + zone_id: 0, + pos: PositionAndRotation { + x, + y, + z, + ..Default::default() + }, + }) + } + + fn check( + server: &crate::GameServer, + player: &PlayerRef, + target: &Target, + ) -> Result<(), (u8, u32)> { + let skill = SkillRun::new( + Arc::new(SkillTemplate { + check_obstacle: true, + ..super::tests::skill_template(3) + }), + 1, + ); + super::validation::check(server, &skill, &WorldObject::from(player), target) + } + + // A unit target inside its own radius: no ray, even with a wall behind the caster. + let mut data = GameData::empty(); + data.systems.skills.model_radii.insert(1, 1.0); + let (server, _client, player, npc) = setup( + data, + Vec3::new(100.0, 99.0, 100.0), + Vec3::new(1.0, 0.5, 0.5), + ); + npc.data().unit.model_id = 1; + npc.data().unit.transform.set_xyz(100.0, 100.2, 100.0); + assert!(check(&server, &player, &Target::Obj(WorldObject::from(&npc))).is_ok()); + + // A thin obstacle just inside the target radius is not on the ray: the end point + // stops 1 m short of a target at 110. + let mut data = GameData::empty(); + data.systems.skills.model_radii.insert(1, 1.0); + let (server, _client1b, player, npc) = setup( + data, + Vec3::new(100.0, 109.5, 101.0), + Vec3::new(1.0, 0.05, 1.0), + ); + npc.data().unit.model_id = 1; + npc.data().unit.transform.set_xyz(100.0, 110.0, 100.0); + assert!(check(&server, &player, &Target::Obj(WorldObject::from(&npc))).is_ok()); + + // A wall further back still blocks the unit target. + let mut data = GameData::empty(); + data.systems.skills.model_radii.insert(1, 1.0); + let (server, _client1c, player, npc) = setup( + data, + Vec3::new(100.0, 105.0, 101.0), + Vec3::new(1.0, 0.5, 1.0), + ); + npc.data().unit.model_id = 1; + npc.data().unit.transform.set_xyz(100.0, 110.0, 100.0); + assert_eq!( + check(&server, &player, &Target::Obj(WorldObject::from(&npc))) + .unwrap_err() + .0, + skill_result::OBSTACLE + ); + + // A position target inside 0.5 m: no ray, even with a wall behind the caster. + let (server, _client2, player, _npc2) = setup( + GameData::empty(), + Vec3::new(100.0, 99.0, 100.0), + Vec3::new(1.0, 0.5, 0.5), + ); + assert!(check(&server, &player, &position(100.0, 100.2, 100.0)).is_ok()); + + // The 0.5 m position radius stops the ray 0.2 m before a wall that sits at 109.75. + let (server, _client3, player, _npc3) = setup( + GameData::empty(), + Vec3::new(100.0, 109.75, 100.0), + Vec3::new(1.0, 0.05, 0.5), + ); + assert!(check(&server, &player, &position(100.0, 110.0, 100.0)).is_ok()); + + // A wall on the way still blocks a position target. + let (server, _client4, player, _npc4) = setup( + GameData::empty(), + Vec3::new(100.0, 105.0, 100.0), + Vec3::new(1.0, 0.5, 0.5), + ); + assert_eq!( + check(&server, &player, &position(100.0, 110.0, 100.0)) + .unwrap_err() + .0, + skill_result::OBSTACLE + ); +} + #[tokio::test] async fn insufficient_mana_keeps_ongoing_buffs_and_cast_timing_untouched() { let mut data = GameData::empty(); diff --git a/rust/crates/aaemu-game/src/systems/skills/validation.rs b/rust/crates/aaemu-game/src/systems/skills/validation.rs index 665aa0a0cc..80332daaa4 100644 --- a/rust/crates/aaemu-game/src/systems/skills/validation.rs +++ b/rust/crates/aaemu-game/src/systems/skills/validation.rs @@ -41,6 +41,18 @@ pub(super) fn check_item_zone( Ok(()) } +/// The physics collider kind of a world object (matches the keys `ai::collision` registers). +fn collision_kind(obj: &WorldObject) -> crate::systems::ai::collision::world::ObjKind { + use crate::systems::ai::collision::world::ObjKind; + match obj { + WorldObject::Doodad(_) => ObjKind::Doodad, + WorldObject::House(_) => ObjKind::House, + WorldObject::Slave(_) | WorldObject::Shipyard(_) => ObjKind::Ship, + WorldObject::Gimmick(_) => ObjKind::Gimmick, + _ => ObjKind::Unit, + } +} + fn in_water(server: &GameServer, target: &Target) -> bool { if target .as_obj() @@ -305,38 +317,66 @@ pub(super) fn check( return fail("skill_too_far_range"); } if template.check_obstacle && !target.as_obj().is_some_and(|o| o.same(caster)) { - // Doodad transforms anchor crops/furniture at the ground. A ray ending at that - // anchor hits the terrain itself and prevents normal harvesting and recovery. - let end_z = if target - .as_obj() - .is_some_and(|o| o.is_unit() || o.as_doodad().is_some()) - { - destination.z + 1.0 - } else { - destination.z + // 1.2 client `FUN_390B5650` (x2game.dll.c:149951): shorten the end point by the + // target radius and cast no ray when `dist <= radius`; `FUN_390B5540` + // (x2game.dll.c:149877) uses objtypes 7 (0x107 with `check_terrain`), ray flags + // 0x2048F (collision class 2) and one hit, mapping a hit to skill result 0xC. + // Unit targets (`FUN_390B5C60` -> `FUN_3989C0D0`) use the target radius at target + // +0x80 and skip the source and target colliders; position targets + // (`FUN_390B5A30` -> `FUN_390B5790`) use the constant 0.5 (DAT_3999C47C) and skip + // only the source. + let dx = destination.x - source.x; + let dy = destination.y - source.y; + let dz = destination.z - source.z; + let len = (dx * dx + dy * dy + dz * dz).sqrt(); + let (target_radius, skip_target) = match target { + Target::Pos(_) => (0.5, false), + Target::Obj(_) => (super::geometry::model_size(server, target), true), }; - let ignored: Vec<_> = target - .as_obj() - .and_then(WorldObject::as_doodad) - .map(|d| { - ( - crate::systems::ai::collision::world::ObjKind::Doodad, - d.obj_id, - ) - }) - .into_iter() - .collect(); - if let Some(Some(hit)) = crate::systems::ai::collision::raycast_ignoring_objects( - server, - caster.transform().instance_id, - [source.x, source.y, source.z + 1.0], - [destination.x, destination.y, end_z], - crate::systems::ai::collision::query::mask::STATIC_TERRAIN, - crate::systems::ai::collision::query::ray_flags::DEFAULT, - &ignored, - ) { - tracing::debug!(skill = template.id, from = ?source, to = ?destination, ?hit, "Skill line of sight blocked"); - return fail("skill_obstacle"); + if len > target_radius { + let mut to = [destination.x, destination.y, destination.z]; + if target_radius > 0.0 { + to[0] -= dx / len * target_radius; + to[1] -= dy / len * target_radius; + to[2] -= dz / len * target_radius; + } + let mut ignored = vec![(collision_kind(caster), caster.obj_id())]; + if skip_target { + if let Some(obj) = target.as_obj() { + ignored.push((collision_kind(obj), obj.obj_id())); + } + } + let objtypes = if template.check_terrain { + crate::systems::ai::collision::query::mask::SOLID_TERRAIN + } else { + crate::systems::ai::collision::query::mask::SOLID + }; + if let Some(Some(hit)) = crate::systems::ai::collision::raycast_ignoring_objects( + server, + caster.transform().instance_id, + [source.x, source.y, source.z], + to, + objtypes, + crate::systems::ai::collision::query::ray_flags::SKILL_OBSTACLE, + &ignored, + ) { + let character = caster + .as_player() + .map(|p| p.data().unit.name.clone()) + .unwrap_or_else(|| caster.with_base_unit(|b| b.name().to_string())); + tracing::warn!( + character = %character, + skill = template.id, + target = target.obj_id(), + result_key = "skill_obstacle", + check_terrain = template.check_terrain, + from = ?source, + to = ?to, + ?hit, + "Skill line of sight blocked" + ); + return fail("skill_obstacle"); + } } } Ok(())