From 656ef74ae69d79d2f09dc3d137fd164e6c7ecb21 Mon Sep 17 00:00:00 2001 From: metalgearsloth Date: Wed, 18 May 2022 14:08:35 +1000 Subject: [PATCH 1/5] Lots of pathfinder bugfixes Not the big refactor I want but: - Non-hard bodies now no longer block - Bodytype changes (AKA anchoring) now accounted for --- .../AI/Pathfinding/PathfindingNode.cs | 3 ++- .../AI/Pathfinding/PathfindingSystem.Grid.cs | 26 +++++++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Content.Server/AI/Pathfinding/PathfindingNode.cs b/Content.Server/AI/Pathfinding/PathfindingNode.cs index d99bdc3511ab..ecaec0b72cdd 100644 --- a/Content.Server/AI/Pathfinding/PathfindingNode.cs +++ b/Content.Server/AI/Pathfinding/PathfindingNode.cs @@ -263,7 +263,8 @@ public void AddEntity(EntityUid entity, IPhysBody physicsComponent, IEntityManag DebugTools.Assert((PathfindingSystem.TrackedCollisionLayers & physicsComponent.CollisionLayer) != 0); - if (physicsComponent.BodyType != BodyType.Static) + if (physicsComponent.BodyType != BodyType.Static || + !physicsComponent.Hard) { _physicsLayers.Add(entity, physicsComponent.CollisionLayer); } diff --git a/Content.Server/AI/Pathfinding/PathfindingSystem.Grid.cs b/Content.Server/AI/Pathfinding/PathfindingSystem.Grid.cs index 4c41f4d4a49a..8a9b14048102 100644 --- a/Content.Server/AI/Pathfinding/PathfindingSystem.Grid.cs +++ b/Content.Server/AI/Pathfinding/PathfindingSystem.Grid.cs @@ -28,11 +28,17 @@ public override void Initialize() SubscribeLocalEvent(OnAccessChange); SubscribeLocalEvent(OnGridAdd); SubscribeLocalEvent(OnTileChange); + SubscribeLocalEvent(OnBodyTypeChange); // Handle all the base grid changes // Anything that affects traversal (i.e. collision layer) is handled separately. } + private void OnBodyTypeChange(PhysicsBodyTypeChangedEvent ev) + { + // var node = Transform(ev.) + } + private void OnGridAdd(GridAddEvent ev) { EnsureComp(ev.EntityUid); @@ -149,6 +155,15 @@ private void OnEntityRemove(EntityUid entity, TransformComponent? xform = null) node.RemoveEntity(entity); } + private void OnEntityRemove(EntityUid entity, EntityCoordinates coordinates) + { + var gridId = coordinates.GetGridId(EntityManager); + if (!_mapManager.TryGetGrid(gridId, out var grid)) return; + + var node = GetNode(grid.GetTileRef(coordinates)); + node.RemoveEntity(entity); + } + /// /// When an entity moves around we'll remove it from its old node and add it to its new node (if applicable) /// @@ -162,22 +177,17 @@ private void OnEntityMove(MoveEvent moveEvent) !IsRelevant(xform, physics) || moveEvent.NewPosition.GetGridId(EntityManager) == GridId.Invalid) { - OnEntityRemove(moveEvent.Sender, xform); + OnEntityRemove(moveEvent.Sender, moveEvent.OldPosition); return; } - var oldGridId = moveEvent.OldPosition.GetGridId(EntityManager); var gridId = moveEvent.NewPosition.GetGridId(EntityManager); - if (_mapManager.TryGetGrid(oldGridId, out var oldGrid)) - { - var oldNode = GetNode(oldGrid.GetTileRef(moveEvent.OldPosition)); - oldNode.RemoveEntity(moveEvent.Sender); - } + OnEntityRemove(moveEvent.Sender, moveEvent.OldPosition); if (_mapManager.TryGetGrid(gridId, out var grid)) { - var newNode = GetNode(grid.GetTileRef(moveEvent.OldPosition)); + var newNode = GetNode(grid.GetTileRef(moveEvent.NewPosition)); newNode.AddEntity(moveEvent.Sender, physics, EntityManager); } } From 3117f5189e1fad3081383fcf77c04c53597bdafc Mon Sep 17 00:00:00 2001 From: metalgearsloth Date: Wed, 18 May 2022 18:16:57 +1000 Subject: [PATCH 2/5] Bodytype changes --- .../AI/Pathfinding/PathfindingNode.cs | 13 +++++----- .../AI/Pathfinding/PathfindingSystem.Grid.cs | 24 +++++++++++++++++-- .../Disposal/Tube/DisposalTubeSystem.cs | 2 +- .../ParticleAcceleratorSystem.Parts.cs | 2 +- .../ContainmentFieldGeneratorSystem.cs | 2 +- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/Content.Server/AI/Pathfinding/PathfindingNode.cs b/Content.Server/AI/Pathfinding/PathfindingNode.cs index ecaec0b72cdd..7fc02b76bdb3 100644 --- a/Content.Server/AI/Pathfinding/PathfindingNode.cs +++ b/Content.Server/AI/Pathfinding/PathfindingNode.cs @@ -286,18 +286,19 @@ public void RemoveEntity(EntityUid entity) // There's no guarantee that the entity isn't deleted // 90% of updates are probably entities moving around // Entity can't be under multiple categories so just checking each once is fine. - if (_physicsLayers.ContainsKey(entity)) + if (_physicsLayers.Remove(entity)) { - _physicsLayers.Remove(entity); + return; } - else if (_accessReaders.ContainsKey(entity)) + + if (_accessReaders.Remove(entity)) { - _accessReaders.Remove(entity); ParentChunk.Dirty(); + return; } - else if (_blockedCollidables.ContainsKey(entity)) + + if (_blockedCollidables.Remove(entity)) { - _blockedCollidables.Remove(entity); GenerateMask(); ParentChunk.Dirty(); } diff --git a/Content.Server/AI/Pathfinding/PathfindingSystem.Grid.cs b/Content.Server/AI/Pathfinding/PathfindingSystem.Grid.cs index 8a9b14048102..3237f3a0d4e9 100644 --- a/Content.Server/AI/Pathfinding/PathfindingSystem.Grid.cs +++ b/Content.Server/AI/Pathfinding/PathfindingSystem.Grid.cs @@ -34,9 +34,15 @@ public override void Initialize() // Anything that affects traversal (i.e. collision layer) is handled separately. } - private void OnBodyTypeChange(PhysicsBodyTypeChangedEvent ev) + private void OnBodyTypeChange(ref PhysicsBodyTypeChangedEvent ev) { - // var node = Transform(ev.) + var xform = Transform(ev.Entity); + + if (!IsRelevant(xform, ev.Component)) return; + + var node = GetNode(xform); + node?.RemoveEntity(ev.Entity); + node?.AddEntity(ev.Entity, ev.Component, EntityManager); } private void OnGridAdd(GridAddEvent ev) @@ -97,6 +103,8 @@ private PathfindingChunk CreateChunk(GridPathfindingComponent comp, Vector2i ind return newChunk; } + + /// /// Return the corresponding PathfindingNode for this tile /// @@ -164,6 +172,18 @@ private void OnEntityRemove(EntityUid entity, EntityCoordinates coordinates) node.RemoveEntity(entity); } + private PathfindingNode? GetNode(TransformComponent xform) + { + if (!_mapManager.TryGetGrid(xform.GridID, out var grid)) return null; + return GetNode(grid.GetTileRef(xform.Coordinates)); + } + + private PathfindingNode? GetNode(EntityCoordinates coordinates) + { + if (!_mapManager.TryGetGrid(coordinates.GetGridId(EntityManager), out var grid)) return null; + return GetNode(grid.GetTileRef(coordinates)); + } + /// /// When an entity moves around we'll remove it from its old node and add it to its new node (if applicable) /// diff --git a/Content.Server/Disposal/Tube/DisposalTubeSystem.cs b/Content.Server/Disposal/Tube/DisposalTubeSystem.cs index 126de2783f9c..bb9509af1bc6 100644 --- a/Content.Server/Disposal/Tube/DisposalTubeSystem.cs +++ b/Content.Server/Disposal/Tube/DisposalTubeSystem.cs @@ -114,7 +114,7 @@ private void OnOpenTaggerUIAttempt(EntityUid uid, DisposalTaggerComponent router private static void BodyTypeChanged( EntityUid uid, DisposalTubeComponent component, - PhysicsBodyTypeChangedEvent args) + ref PhysicsBodyTypeChangedEvent args) { component.AnchoredChanged(); } diff --git a/Content.Server/ParticleAccelerator/EntitySystems/ParticleAcceleratorSystem.Parts.cs b/Content.Server/ParticleAccelerator/EntitySystems/ParticleAcceleratorSystem.Parts.cs index d087bab9d2be..dcfeff598548 100644 --- a/Content.Server/ParticleAccelerator/EntitySystems/ParticleAcceleratorSystem.Parts.cs +++ b/Content.Server/ParticleAccelerator/EntitySystems/ParticleAcceleratorSystem.Parts.cs @@ -15,7 +15,7 @@ private void InitializePartSystem() private static void BodyTypeChanged( EntityUid uid, ParticleAcceleratorPartComponent component, - PhysicsBodyTypeChangedEvent args) + ref PhysicsBodyTypeChangedEvent args) { component.OnAnchorChanged(); } diff --git a/Content.Server/Singularity/EntitySystems/ContainmentFieldGeneratorSystem.cs b/Content.Server/Singularity/EntitySystems/ContainmentFieldGeneratorSystem.cs index 0e8eba054754..67e92dc16385 100644 --- a/Content.Server/Singularity/EntitySystems/ContainmentFieldGeneratorSystem.cs +++ b/Content.Server/Singularity/EntitySystems/ContainmentFieldGeneratorSystem.cs @@ -57,7 +57,7 @@ private void HandleFieldCollide(EntityUid uid, ContainmentFieldComponent compone private static void BodyTypeChanged( EntityUid uid, ContainmentFieldGeneratorComponent component, - PhysicsBodyTypeChangedEvent args) + ref PhysicsBodyTypeChangedEvent args) { component.OnAnchoredChanged(); } From d3a7d48394c52318704bfd152369b19506ec9004 Mon Sep 17 00:00:00 2001 From: metalgearsloth Date: Wed, 18 May 2022 18:54:40 +1000 Subject: [PATCH 3/5] Door bumps --- Resources/Prototypes/Entities/Mobs/NPCs/simplemob.yml | 3 +++ Resources/Prototypes/Entities/Mobs/NPCs/xeno.yml | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Resources/Prototypes/Entities/Mobs/NPCs/simplemob.yml b/Resources/Prototypes/Entities/Mobs/NPCs/simplemob.yml index 0ce3d0d10045..afdfc3448e3c 100644 --- a/Resources/Prototypes/Entities/Mobs/NPCs/simplemob.yml +++ b/Resources/Prototypes/Entities/Mobs/NPCs/simplemob.yml @@ -4,6 +4,9 @@ id: SimpleSpaceMobBase # Mob without barotrauma, freezing and asphyxiation (for space carps!?) suffix: AI components: + - type: Tag + tags: + - DoorBumpOpener - type: Reactive groups: Flammable: [Touch] diff --git a/Resources/Prototypes/Entities/Mobs/NPCs/xeno.yml b/Resources/Prototypes/Entities/Mobs/NPCs/xeno.yml index 8c355b9442e5..916fd6d779c8 100644 --- a/Resources/Prototypes/Entities/Mobs/NPCs/xeno.yml +++ b/Resources/Prototypes/Entities/Mobs/NPCs/xeno.yml @@ -31,7 +31,7 @@ fixtures: - shape: !type:PhysShapeCircle - radius: 0.35 + radius: 0.25 mass: 120 mask: - MobMask @@ -88,6 +88,8 @@ - type: Tag tags: - CannotSuicide + - DoorBumpOpener + - FootstepSound - type: NoSlip - type: entity From 5e2fbff57a9bfaba1d0ce67b9f7730853c21a36a Mon Sep 17 00:00:00 2001 From: metalgearsloth Date: Wed, 18 May 2022 19:49:58 +1000 Subject: [PATCH 4/5] a --- .../AI/Pathfinding/PathfindingSystem.Grid.cs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/Content.Server/AI/Pathfinding/PathfindingSystem.Grid.cs b/Content.Server/AI/Pathfinding/PathfindingSystem.Grid.cs index 3237f3a0d4e9..0b9ddc9de6cb 100644 --- a/Content.Server/AI/Pathfinding/PathfindingSystem.Grid.cs +++ b/Content.Server/AI/Pathfinding/PathfindingSystem.Grid.cs @@ -194,22 +194,19 @@ private void OnEntityMove(MoveEvent moveEvent) // If we've moved to space or the likes then remove us. if (!TryComp(moveEvent.Sender, out var physics) || - !IsRelevant(xform, physics) || - moveEvent.NewPosition.GetGridId(EntityManager) == GridId.Invalid) + !IsRelevant(xform, physics)) { OnEntityRemove(moveEvent.Sender, moveEvent.OldPosition); return; } - var gridId = moveEvent.NewPosition.GetGridId(EntityManager); + var oldNode = GetNode(moveEvent.OldPosition); + var newNode = GetNode(moveEvent.NewPosition); - OnEntityRemove(moveEvent.Sender, moveEvent.OldPosition); + if (oldNode?.Equals(newNode) == true) return; - if (_mapManager.TryGetGrid(gridId, out var grid)) - { - var newNode = GetNode(grid.GetTileRef(moveEvent.NewPosition)); - newNode.AddEntity(moveEvent.Sender, physics, EntityManager); - } + oldNode?.RemoveEntity(moveEvent.Sender); + newNode?.AddEntity(moveEvent.Sender, physics, EntityManager); } // TODO: Need to rethink the pathfinder utils (traversable etc.). Maybe just chuck them all in PathfindingSystem From e49e7c0a349dd4313665d21105e639d47dd271f0 Mon Sep 17 00:00:00 2001 From: metalgearsloth Date: Wed, 18 May 2022 19:50:45 +1000 Subject: [PATCH 5/5] comment --- Content.Server/AI/Pathfinding/PathfindingSystem.Grid.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Content.Server/AI/Pathfinding/PathfindingSystem.Grid.cs b/Content.Server/AI/Pathfinding/PathfindingSystem.Grid.cs index 0b9ddc9de6cb..037ac965ed22 100644 --- a/Content.Server/AI/Pathfinding/PathfindingSystem.Grid.cs +++ b/Content.Server/AI/Pathfinding/PathfindingSystem.Grid.cs @@ -7,11 +7,12 @@ namespace Content.Server.AI.Pathfinding; -/// -/// Handles pathfinding while on a grid. -/// public sealed partial class PathfindingSystem { + /* + * Handles pathfinding while on a grid. + */ + [Dependency] private readonly AccessReaderSystem _accessReader = default!; [Dependency] private readonly IMapManager _mapManager = default!;