diff --git a/Content.Server/GameObjects/EntitySystems/SpawnAfterInteractSystem.cs b/Content.Server/GameObjects/EntitySystems/SpawnAfterInteractSystem.cs index 13ddee9d83..07821913a4 100644 --- a/Content.Server/GameObjects/EntitySystems/SpawnAfterInteractSystem.cs +++ b/Content.Server/GameObjects/EntitySystems/SpawnAfterInteractSystem.cs @@ -36,12 +36,14 @@ namespace Content.Server.GameObjects.EntitySystems { if (string.IsNullOrEmpty(component.Prototype)) return; - if (!args.ClickLocation.TryGetTileRef(out var tileRef, EntityManager, _mapManager)) + if (!_mapManager.TryGetGrid(args.ClickLocation.GetGridId(EntityManager), out var grid)) + return; + if (!grid.TryGetTileRef(args.ClickLocation, out var tileRef)) return; bool IsTileClear() { - return tileRef.Value.Tile.IsEmpty == false && args.User.InRangeUnobstructed(args.ClickLocation, popup: true); + return tileRef.Tile.IsEmpty == false && args.User.InRangeUnobstructed(args.ClickLocation, popup: true); } if (!IsTileClear()) @@ -68,7 +70,7 @@ namespace Content.Server.GameObjects.EntitySystems if (component.RemoveOnInteract && component.Owner.TryGetComponent(out stack) && !stack.Use(1)) return; - EntityManager.SpawnEntity(component.Prototype, tileRef.Value.GridPosition(_mapManager)); + EntityManager.SpawnEntity(component.Prototype, args.ClickLocation.SnapToGrid(grid, SnapGridOffset.Center)); if (component.RemoveOnInteract && stack == null && !component.Owner.Deleted) component.Owner.Delete(); diff --git a/Content.Server/Utility/SnapgridHelper.cs b/Content.Server/Utility/SnapgridHelper.cs index 414c2d9a54..54655944f0 100644 --- a/Content.Server/Utility/SnapgridHelper.cs +++ b/Content.Server/Utility/SnapgridHelper.cs @@ -1,4 +1,4 @@ -using System; +using System; using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Map; @@ -35,5 +35,18 @@ namespace Content.Server.Utility return new EntityCoordinates(coordinates.EntityId, x, y); } + + public static EntityCoordinates SnapToGrid(this EntityCoordinates coordinates, IMapGrid grid, + SnapGridOffset offset = SnapGridOffset.Center) + { + var tileSize = grid.TileSize; + + var localPos = coordinates.Position; + + var x = (int)Math.Floor(localPos.X / tileSize) + tileSize / (offset == SnapGridOffset.Center ? 2f : 0f); + var y = (int)Math.Floor(localPos.Y / tileSize) + tileSize / (offset == SnapGridOffset.Center ? 2f : 0f); + + return new EntityCoordinates(coordinates.EntityId, x, y); + } } }