From 666d81a8691a1adfe9215545f3650e400e386192 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Mon, 12 Dec 2022 21:20:13 +1100 Subject: [PATCH] Ignore non-hard bodies for npc steering (#12990) --- .../NPC/Systems/NPCCombatSystem.Melee.cs | 2 +- .../NPC/Systems/NPCSteeringSystem.Context.cs | 22 ++++++++++--------- .../NPC/Systems/NPCSteeringSystem.cs | 5 +++-- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/Content.Server/NPC/Systems/NPCCombatSystem.Melee.cs b/Content.Server/NPC/Systems/NPCCombatSystem.Melee.cs index 514130eaac..b29bd52b26 100644 --- a/Content.Server/NPC/Systems/NPCCombatSystem.Melee.cs +++ b/Content.Server/NPC/Systems/NPCCombatSystem.Melee.cs @@ -39,7 +39,7 @@ public sealed partial class NPCCombatSystem var obstacleDirection = pointB - args.WorldPosition; var obstacleDistance = obstacleDirection.Length; - if (obstacleDistance > idealDistance) + if (obstacleDistance > idealDistance || obstacleDistance == 0f) { // Don't want to get too far. return; diff --git a/Content.Server/NPC/Systems/NPCSteeringSystem.Context.cs b/Content.Server/NPC/Systems/NPCSteeringSystem.Context.cs index ff99b0fd0c..14a087b6f3 100644 --- a/Content.Server/NPC/Systems/NPCSteeringSystem.Context.cs +++ b/Content.Server/NPC/Systems/NPCSteeringSystem.Context.cs @@ -306,7 +306,8 @@ public sealed partial class NPCSteeringSystem Vector2 worldPos, float agentRadius, float moveSpeed, - PhysicsComponent body, + int layer, + int mask, TransformComponent xform, float[] danger, List dangerPoints, @@ -322,8 +323,8 @@ public sealed partial class NPCSteeringSystem !bodyQuery.TryGetComponent(ent, out var otherBody) || !otherBody.Hard || !otherBody.CanCollide || - ((body.CollisionMask & otherBody.CollisionLayer) == 0x0 && - (body.CollisionLayer & otherBody.CollisionMask) == 0x0)) + (mask & otherBody.CollisionLayer) == 0x0 && + (layer & otherBody.CollisionMask) == 0x0) { continue; } @@ -331,10 +332,10 @@ public sealed partial class NPCSteeringSystem if (!_physics.TryGetNearestPoints(uid, ent, out var pointA, out var pointB, xform, xformQuery.GetComponent(ent))) continue; - var obstacleDirection = (pointB - worldPos); + var obstacleDirection = pointB - worldPos; var obstableDistance = obstacleDirection.Length; - if (obstableDistance > detectionRadius) + if (obstableDistance > detectionRadius || obstableDistance == 0f) continue; dangerPoints.Add(pointB); @@ -363,9 +364,10 @@ public sealed partial class NPCSteeringSystem Angle offsetRot, Vector2 worldPos, float agentRadius, + int layer, + int mask, PhysicsComponent body, TransformComponent xform, - float[] interest, float[] danger, EntityQuery bodyQuery, EntityQuery xformQuery) @@ -382,8 +384,8 @@ public sealed partial class NPCSteeringSystem !bodyQuery.TryGetComponent(ent, out var otherBody) || !otherBody.Hard || !otherBody.CanCollide || - (body.CollisionMask & otherBody.CollisionLayer) == 0x0 && - (body.CollisionLayer & otherBody.CollisionMask) == 0x0 || + (mask & otherBody.CollisionLayer) == 0x0 && + (layer & otherBody.CollisionMask) == 0x0 || !factionQuery.TryGetComponent(ent, out var otherFaction) || !_faction.IsFriendly(uid, ent, ourFaction, otherFaction) || Vector2.Dot(otherBody.LinearVelocity, ourVelocity) < 0f) @@ -398,10 +400,10 @@ public sealed partial class NPCSteeringSystem continue; } - var obstacleDirection = (pointB - worldPos); + var obstacleDirection = pointB - worldPos; var obstableDistance = obstacleDirection.Length; - if (obstableDistance > detectionRadius) + if (obstableDistance > detectionRadius || obstableDistance == 0f) continue; obstacleDirection = offsetRot.RotateVec(obstacleDirection); diff --git a/Content.Server/NPC/Systems/NPCSteeringSystem.cs b/Content.Server/NPC/Systems/NPCSteeringSystem.cs index 37ca616f55..1f5a369188 100644 --- a/Content.Server/NPC/Systems/NPCSteeringSystem.cs +++ b/Content.Server/NPC/Systems/NPCSteeringSystem.cs @@ -299,6 +299,7 @@ namespace Content.Server.NPC.Systems var danger = steering.Danger; var agentRadius = steering.Radius; var worldPos = xform.WorldPosition; + var (layer, mask) = _physics.GetHardCollision(uid); // Use rotation relative to parent to rotate our context vectors by. var offsetRot = -_mover.GetParentGridAngle(mover); @@ -326,10 +327,10 @@ namespace Content.Server.NPC.Systems DebugTools.Assert(!float.IsNaN(interest[0])); // Avoid static objects like walls - CollisionAvoidance(uid, offsetRot, worldPos, agentRadius, tickMove, body, xform, danger, dangerPoints, bodyQuery, xformQuery); + CollisionAvoidance(uid, offsetRot, worldPos, agentRadius, tickMove, layer, mask, xform, danger, dangerPoints, bodyQuery, xformQuery); DebugTools.Assert(!float.IsNaN(danger[0])); - Separation(uid, offsetRot, worldPos, agentRadius, body, xform, interest, danger, bodyQuery, xformQuery); + Separation(uid, offsetRot, worldPos, agentRadius, layer, mask, body, xform, danger, bodyQuery, xformQuery); // Remove the danger map from the interest map. var desiredDirection = -1;