diff --git a/Robust.Client/Graphics/ClientEye/EyeManager.cs b/Robust.Client/Graphics/ClientEye/EyeManager.cs index 9e09b06b59..23ac33497f 100644 --- a/Robust.Client/Graphics/ClientEye/EyeManager.cs +++ b/Robust.Client/Graphics/ClientEye/EyeManager.cs @@ -85,19 +85,14 @@ namespace Robust.Client.Graphics.ClientEye public GridCoordinates ScreenToWorld(Vector2 point) { - var mapPos = ScreenToMap(point).Position; + var mapCoords = ScreenToMap(point); - IMapGrid grid; - var mapId = currentEye.Position.MapId; - if (_mapManager.MapExists(mapId)) + if (!_mapManager.TryFindGridAt(mapCoords, out var grid)) { - grid = _mapManager.FindGridAt(mapId, mapPos); + grid = _mapManager.GetDefaultGrid(mapCoords.MapId); } - else - { - grid = _mapManager.GetGrid(GridId.Invalid); - } - return new GridCoordinates(grid.WorldToLocal(mapPos), grid); + + return new GridCoordinates(grid.WorldToLocal(mapCoords.Position), grid.Index); } public MapCoordinates ScreenToMap(Vector2 point) diff --git a/Robust.Server/Console/Commands/PlayerCommands.cs b/Robust.Server/Console/Commands/PlayerCommands.cs index 67889e5214..10daf49883 100644 --- a/Robust.Server/Console/Commands/PlayerCommands.cs +++ b/Robust.Server/Console/Commands/PlayerCommands.cs @@ -46,11 +46,21 @@ namespace Robust.Server.Console.Commands else mapId = transform.MapID; - var grid = mapMgr.FindGridAt(mapId, position); - var gridPos = grid.WorldToLocal(position); - transform.GridPosition = new GridCoordinates(gridPos, grid); + if (mapMgr.TryFindGridAt(mapId, position, out var grid)) + { + var gridPos = grid.WorldToLocal(position); - shell.SendText(player, $"Teleported {player} to {grid.ParentMapId}:{posX},{posY}."); + transform.GridPosition = new GridCoordinates(gridPos, grid); + } + else + { + var mapEnt = mapMgr.GetMapEntity(mapId); + + transform.AttachParent(mapEnt); + transform.WorldPosition = position; + } + + shell.SendText(player, $"Teleported {player} to {mapId}:{posX},{posY}."); } } diff --git a/Robust.Shared/GameObjects/Components/Transform/TransformComponent.cs b/Robust.Shared/GameObjects/Components/Transform/TransformComponent.cs index daeb07392d..ec5e631160 100644 --- a/Robust.Shared/GameObjects/Components/Transform/TransformComponent.cs +++ b/Robust.Shared/GameObjects/Components/Transform/TransformComponent.cs @@ -386,14 +386,12 @@ namespace Robust.Shared.GameObjects.Components.Transform } var mapPos = MapPosition; - var mapGrid = _mapManager.FindGridAt(mapPos.MapId, mapPos.Position); - var newMapEntity = _entityManager.GetEntity(mapGrid.GridEntityId); - /* // TODO: reinstate this once entities can be picked up from the map - var newMapEntity = mapGrid.IsDefaultGrid - ? _mapManager.GetMapEntity(mapPos.MapId) - : _entityManager.GetEntity(mapGrid.GridEntityId); - */ + IEntity newMapEntity; + if (_mapManager.TryFindGridAt(mapPos, out var mapGrid)) + newMapEntity = _entityManager.GetEntity(mapGrid.GridEntityId); + else + newMapEntity = _mapManager.GetMapEntity(mapPos.MapId); // this would be a no-op var oldParentEnt = oldParent.Owner; diff --git a/Robust.Shared/Interfaces/Map/IMapManager.cs b/Robust.Shared/Interfaces/Map/IMapManager.cs index 2991b0ec94..5e0f44e6a1 100644 --- a/Robust.Shared/Interfaces/Map/IMapManager.cs +++ b/Robust.Shared/Interfaces/Map/IMapManager.cs @@ -103,8 +103,30 @@ namespace Robust.Shared.Interfaces.Map bool TryGetGrid(GridId gridId, out IMapGrid grid); bool GridExists(GridId gridID); IEnumerable GetAllMapGrids(MapId mapId); - IMapGrid FindGridAt(MapId mapId, Vector2 worldPos); - IMapGrid FindGridAt(MapCoordinates mapCoords); + + /// + /// Attempts to find the map grid under the map location. + /// + /// + /// This method will never return the map's default grid. + /// + /// Map to search. + /// Location on the map to check for a grid. + /// Grid that was found, if any. + /// Returns true when a grid was found under the location. + bool TryFindGridAt(MapId mapId, Vector2 worldPos, out IMapGrid grid); + + /// + /// Attempts to find the map grid under the map location. + /// + /// + /// This method will never return the map's default grid. + /// + /// Location on the map to check for a grid. + /// Grid that was found, if any. + /// Returns true when a grid was found under the location. + bool TryFindGridAt(MapCoordinates mapCoordinates, out IMapGrid grid); + IEnumerable FindGridsIntersecting(MapId mapId, Box2 worldArea); void DeleteGrid(GridId gridID); diff --git a/Robust.Shared/Map/MapManager.cs b/Robust.Shared/Map/MapManager.cs index a994440609..e381a7c842 100644 --- a/Robust.Shared/Map/MapManager.cs +++ b/Robust.Shared/Map/MapManager.cs @@ -519,23 +519,34 @@ namespace Robust.Shared.Map return _grids.Values.Where(m => m.ParentMapId == mapId); } - public IMapGrid FindGridAt(MapId mapId, Vector2 worldPos) + /// + public bool TryFindGridAt(MapId mapId, Vector2 worldPos, out IMapGrid grid) { - var defaultGrid = GetDefaultGrid(mapId); - foreach (var grid in GetAllMapGrids(mapId)) - if (grid.WorldBounds.Contains(worldPos) && grid != defaultGrid) - return grid; - return defaultGrid; + foreach (var mapGrid in _grids.Values) + { + if(mapGrid.ParentMapId != mapId || mapGrid.IsDefaultGrid) + continue; + + if(!mapGrid.WorldBounds.Contains(worldPos)) + continue; + + grid = mapGrid; + return true; + } + + grid = default; + return false; } - public IMapGrid FindGridAt(MapCoordinates mapCoords) + /// + public bool TryFindGridAt(MapCoordinates mapCoordinates, out IMapGrid grid) { - return FindGridAt(mapCoords.MapId, mapCoords.Position); + return TryFindGridAt(mapCoordinates.MapId, mapCoordinates.Position, out grid); } public IEnumerable FindGridsIntersecting(MapId mapId, Box2 worldArea) { - return GetAllMapGrids(mapId).Where(grid => grid.WorldBounds.Intersects(worldArea)); + return _grids.Values.Where(g => g.ParentMapId == mapId && !g.IsDefaultGrid && g.WorldBounds.Intersects(worldArea)); } public void DeleteGrid(GridId gridID)