Ignore non-hard bodies for npc steering (#12990)

This commit is contained in:
metalgearsloth
2022-12-12 21:20:13 +11:00
committed by GitHub
parent bbfa8b5ae6
commit 666d81a869
3 changed files with 16 additions and 13 deletions

View File

@@ -39,7 +39,7 @@ public sealed partial class NPCCombatSystem
var obstacleDirection = pointB - args.WorldPosition; var obstacleDirection = pointB - args.WorldPosition;
var obstacleDistance = obstacleDirection.Length; var obstacleDistance = obstacleDirection.Length;
if (obstacleDistance > idealDistance) if (obstacleDistance > idealDistance || obstacleDistance == 0f)
{ {
// Don't want to get too far. // Don't want to get too far.
return; return;

View File

@@ -306,7 +306,8 @@ public sealed partial class NPCSteeringSystem
Vector2 worldPos, Vector2 worldPos,
float agentRadius, float agentRadius,
float moveSpeed, float moveSpeed,
PhysicsComponent body, int layer,
int mask,
TransformComponent xform, TransformComponent xform,
float[] danger, float[] danger,
List<Vector2> dangerPoints, List<Vector2> dangerPoints,
@@ -322,8 +323,8 @@ public sealed partial class NPCSteeringSystem
!bodyQuery.TryGetComponent(ent, out var otherBody) || !bodyQuery.TryGetComponent(ent, out var otherBody) ||
!otherBody.Hard || !otherBody.Hard ||
!otherBody.CanCollide || !otherBody.CanCollide ||
((body.CollisionMask & otherBody.CollisionLayer) == 0x0 && (mask & otherBody.CollisionLayer) == 0x0 &&
(body.CollisionLayer & otherBody.CollisionMask) == 0x0)) (layer & otherBody.CollisionMask) == 0x0)
{ {
continue; 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))) if (!_physics.TryGetNearestPoints(uid, ent, out var pointA, out var pointB, xform, xformQuery.GetComponent(ent)))
continue; continue;
var obstacleDirection = (pointB - worldPos); var obstacleDirection = pointB - worldPos;
var obstableDistance = obstacleDirection.Length; var obstableDistance = obstacleDirection.Length;
if (obstableDistance > detectionRadius) if (obstableDistance > detectionRadius || obstableDistance == 0f)
continue; continue;
dangerPoints.Add(pointB); dangerPoints.Add(pointB);
@@ -363,9 +364,10 @@ public sealed partial class NPCSteeringSystem
Angle offsetRot, Angle offsetRot,
Vector2 worldPos, Vector2 worldPos,
float agentRadius, float agentRadius,
int layer,
int mask,
PhysicsComponent body, PhysicsComponent body,
TransformComponent xform, TransformComponent xform,
float[] interest,
float[] danger, float[] danger,
EntityQuery<PhysicsComponent> bodyQuery, EntityQuery<PhysicsComponent> bodyQuery,
EntityQuery<TransformComponent> xformQuery) EntityQuery<TransformComponent> xformQuery)
@@ -382,8 +384,8 @@ public sealed partial class NPCSteeringSystem
!bodyQuery.TryGetComponent(ent, out var otherBody) || !bodyQuery.TryGetComponent(ent, out var otherBody) ||
!otherBody.Hard || !otherBody.Hard ||
!otherBody.CanCollide || !otherBody.CanCollide ||
(body.CollisionMask & otherBody.CollisionLayer) == 0x0 && (mask & otherBody.CollisionLayer) == 0x0 &&
(body.CollisionLayer & otherBody.CollisionMask) == 0x0 || (layer & otherBody.CollisionMask) == 0x0 ||
!factionQuery.TryGetComponent(ent, out var otherFaction) || !factionQuery.TryGetComponent(ent, out var otherFaction) ||
!_faction.IsFriendly(uid, ent, ourFaction, otherFaction) || !_faction.IsFriendly(uid, ent, ourFaction, otherFaction) ||
Vector2.Dot(otherBody.LinearVelocity, ourVelocity) < 0f) Vector2.Dot(otherBody.LinearVelocity, ourVelocity) < 0f)
@@ -398,10 +400,10 @@ public sealed partial class NPCSteeringSystem
continue; continue;
} }
var obstacleDirection = (pointB - worldPos); var obstacleDirection = pointB - worldPos;
var obstableDistance = obstacleDirection.Length; var obstableDistance = obstacleDirection.Length;
if (obstableDistance > detectionRadius) if (obstableDistance > detectionRadius || obstableDistance == 0f)
continue; continue;
obstacleDirection = offsetRot.RotateVec(obstacleDirection); obstacleDirection = offsetRot.RotateVec(obstacleDirection);

View File

@@ -299,6 +299,7 @@ namespace Content.Server.NPC.Systems
var danger = steering.Danger; var danger = steering.Danger;
var agentRadius = steering.Radius; var agentRadius = steering.Radius;
var worldPos = xform.WorldPosition; var worldPos = xform.WorldPosition;
var (layer, mask) = _physics.GetHardCollision(uid);
// Use rotation relative to parent to rotate our context vectors by. // Use rotation relative to parent to rotate our context vectors by.
var offsetRot = -_mover.GetParentGridAngle(mover); var offsetRot = -_mover.GetParentGridAngle(mover);
@@ -326,10 +327,10 @@ namespace Content.Server.NPC.Systems
DebugTools.Assert(!float.IsNaN(interest[0])); DebugTools.Assert(!float.IsNaN(interest[0]));
// Avoid static objects like walls // 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])); 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. // Remove the danger map from the interest map.
var desiredDirection = -1; var desiredDirection = -1;