From d3daa83b8287f61bbf53321d4d063c2721079af0 Mon Sep 17 00:00:00 2001 From: Acruid Date: Sat, 20 Apr 2019 16:20:18 -0700 Subject: [PATCH] Map System Code Refactor (#204) * Removes static `IoCManager` service locator calls from `Robust.Shared.Map` namespace. * Misc code cleanup and filling out doc comments for Map classes. * Added Union and Intersect methods to Box2. * Any touched component was converted from static IoC calls to field injection. Sibling PR to https://github.com/space-wizards/RobustToolbox/pull/796. --- .../EntitySystems/SubFloorHideSystem.cs | 6 ++-- Content.Client/Parallax/ParallaxOverlay.cs | 4 ++- .../Construction/ConstructorComponent.cs | 29 ++++++++++--------- .../Interactable/Tools/CrowbarComponent.cs | 13 ++++----- .../Items/Storage/ServerStorageComponent.cs | 18 +++++++----- .../Weapon/Melee/MeleeWeaponComponent.cs | 23 +++++++-------- .../EntitySystems/Click/InteractionSystem.cs | 12 ++++++-- .../GameObjects/EntitySystems/HandsSystem.cs | 11 +++++-- .../GameObjects/EntitySystems/MoverSystem.cs | 19 ++++++------ Resources/Groups/groups.yml | 2 ++ 10 files changed, 77 insertions(+), 60 deletions(-) diff --git a/Content.Client/GameObjects/EntitySystems/SubFloorHideSystem.cs b/Content.Client/GameObjects/EntitySystems/SubFloorHideSystem.cs index d340f5a645..9bddb28577 100644 --- a/Content.Client/GameObjects/EntitySystems/SubFloorHideSystem.cs +++ b/Content.Client/GameObjects/EntitySystems/SubFloorHideSystem.cs @@ -43,13 +43,13 @@ namespace Content.Client.GameObjects.EntitySystems var grid = _mapManager.GetGrid(senderEnt.Transform.GridID); var position = senderEnt.Transform.GridPosition; var tileRef = grid.GetTile(position); - var tileDef = (ContentTileDefinition) _tileDefinitionManager[tileRef.Tile.TileId]; + var tileDef = (ContentTileDefinition) _tileDefinitionManager[tileRef.Tile.TypeId]; sprite.Visible = tileDef.IsSubFloor; } private void MapManagerOnTileChanged(object sender, TileChangedEventArgs e) { - UpdateTile(_mapManager.GetGrid(e.NewTile.GridIndex), e.NewTile.GridTile); + UpdateTile(_mapManager.GetGrid(e.NewTile.GridIndex), e.NewTile.GridIndices); } private void MapManagerOnGridChanged(object sender, GridChangedEventArgs e) @@ -63,7 +63,7 @@ namespace Content.Client.GameObjects.EntitySystems private void UpdateTile(IMapGrid grid, MapIndices position) { var tile = grid.GetTile(position); - var tileDef = (ContentTileDefinition) _tileDefinitionManager[tile.Tile.TileId]; + var tileDef = (ContentTileDefinition) _tileDefinitionManager[tile.Tile.TypeId]; foreach (var snapGridComponent in grid.GetSnapGridCell(position, SnapGridOffset.Center)) { var entity = snapGridComponent.Owner; diff --git a/Content.Client/Parallax/ParallaxOverlay.cs b/Content.Client/Parallax/ParallaxOverlay.cs index c4266073a5..4e961b1151 100644 --- a/Content.Client/Parallax/ParallaxOverlay.cs +++ b/Content.Client/Parallax/ParallaxOverlay.cs @@ -5,6 +5,7 @@ using Robust.Client.Graphics.Overlays; using Robust.Client.Graphics.Shaders; using Robust.Client.Interfaces.Graphics.ClientEye; using Robust.Client.Interfaces.Graphics.Overlays; +using Robust.Shared.Interfaces.Map; using Robust.Shared.IoC; using Robust.Shared.Maths; using Robust.Shared.Prototypes; @@ -17,6 +18,7 @@ namespace Content.Client.Parallax [Dependency] private readonly IParallaxManager _parallaxManager; [Dependency] private readonly IEyeManager _eyeManager; [Dependency] private readonly IPrototypeManager _prototypeManager; + [Dependency] private readonly IMapManager _mapManager; #pragma warning restore 649 public override bool AlwaysDirty => true; @@ -49,7 +51,7 @@ namespace Content.Client.Parallax } var (sizeX, sizeY) = _parallaxTexture.Size; - var (posX, posY) = _eyeManager.ScreenToWorld(Vector2.Zero).ToWorld().Position; + var (posX, posY) = _eyeManager.ScreenToWorld(Vector2.Zero).ToWorld(_mapManager).Position; var (ox, oy) = (Vector2i) new Vector2(-posX / Slowness, posY / Slowness); ox = MathHelper.Mod(ox, sizeX); oy = MathHelper.Mod(oy, sizeY); diff --git a/Content.Server/GameObjects/Components/Construction/ConstructorComponent.cs b/Content.Server/GameObjects/Components/Construction/ConstructorComponent.cs index 8d7e9491c6..2a27c6ccdb 100644 --- a/Content.Server/GameObjects/Components/Construction/ConstructorComponent.cs +++ b/Content.Server/GameObjects/Components/Construction/ConstructorComponent.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using Content.Server.GameObjects.Components.Materials; -using Content.Server.GameObjects.Components.Sound; using Content.Server.GameObjects.Components.Stack; using Content.Server.GameObjects.EntitySystems; using Content.Shared.Construction; @@ -10,6 +7,7 @@ using Robust.Server.GameObjects; using Robust.Server.GameObjects.EntitySystems; using Robust.Server.Interfaces.GameObjects; using Robust.Shared.GameObjects; +using Robust.Shared.Interfaces.Map; using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects.Components; using Robust.Shared.Interfaces.Network; @@ -22,6 +20,13 @@ namespace Content.Server.GameObjects.Components.Construction { public class ConstructorComponent : SharedConstructorComponent { +#pragma warning disable 649 + [Dependency] private readonly IPrototypeManager _prototypeManager; + [Dependency] private readonly IMapManager _mapManager; + [Dependency] private readonly IServerEntityManager _serverEntityManager; + [Dependency] private readonly IEntitySystemManager _entitySystemManager; +#pragma warning restore 649 + public override void HandleMessage(ComponentMessage message, INetChannel netChannel = null, IComponent component = null) { base.HandleMessage(message, netChannel, component); @@ -36,11 +41,10 @@ namespace Content.Server.GameObjects.Components.Construction void TryStartStructureConstruction(GridCoordinates loc, string prototypeName, Angle angle, int ack) { - var protoMan = IoCManager.Resolve(); - var prototype = protoMan.Index(prototypeName); + var prototype = _prototypeManager.Index(prototypeName); - var transform = Owner.GetComponent(); - if (!loc.InRange(transform.GridPosition, InteractionSystem.INTERACTION_RANGE)) + var transform = Owner.Transform; + if (!loc.InRange(_mapManager, transform.GridPosition, InteractionSystem.INTERACTION_RANGE)) { return; } @@ -75,20 +79,19 @@ namespace Content.Server.GameObjects.Components.Construction } // OK WE'RE GOOD CONSTRUCTION STARTED. - var entMgr = IoCManager.Resolve(); - IoCManager.Resolve().GetEntitySystem().Play("/Audio/items/deconstruct.ogg", loc); + _entitySystemManager.GetEntitySystem().Play("/Audio/items/deconstruct.ogg", loc); if (prototype.Stages.Count == 2) { // Exactly 2 stages, so don't make an intermediate frame. - var ent = entMgr.ForceSpawnEntityAt(prototype.Result, loc); - ent.GetComponent().LocalRotation = angle; + var ent = _serverEntityManager.ForceSpawnEntityAt(prototype.Result, loc); + ent.Transform.LocalRotation = angle; } else { - var frame = entMgr.ForceSpawnEntityAt("structureconstructionframe", loc); + var frame = _serverEntityManager.ForceSpawnEntityAt("structureconstructionframe", loc); var construction = frame.GetComponent(); construction.Init(prototype); - frame.GetComponent().LocalRotation = angle; + frame.Transform.LocalRotation = angle; } var msg = new AckStructureConstructionMessage(ack); diff --git a/Content.Server/GameObjects/Components/Interactable/Tools/CrowbarComponent.cs b/Content.Server/GameObjects/Components/Interactable/Tools/CrowbarComponent.cs index ed61174afc..6732437a47 100644 --- a/Content.Server/GameObjects/Components/Interactable/Tools/CrowbarComponent.cs +++ b/Content.Server/GameObjects/Components/Interactable/Tools/CrowbarComponent.cs @@ -13,6 +13,7 @@ namespace Content.Server.GameObjects.Components.Interactable.Tools #pragma warning disable 649 [Dependency] private readonly ITileDefinitionManager _tileDefinitionManager; [Dependency] private readonly IEntitySystemManager _entitySystemManager; + [Dependency] private readonly IMapManager _mapManager; #pragma warning restore 649 /// @@ -20,19 +21,15 @@ namespace Content.Server.GameObjects.Components.Interactable.Tools /// public override string Name => "Crowbar"; - public CrowbarComponent() - { - IoCManager.InjectDependencies(this); - } - public void AfterAttack(AfterAttackEventArgs eventArgs) { - var tile = eventArgs.ClickLocation.Grid.GetTile(eventArgs.ClickLocation); - var tileDef = (ContentTileDefinition) tile.TileDef; + var mapGrid = _mapManager.GetGrid(eventArgs.ClickLocation.GridID); + var tile = mapGrid.GetTile(eventArgs.ClickLocation); + var tileDef = (ContentTileDefinition)_tileDefinitionManager[tile.Tile.TypeId]; if (tileDef.CanCrowbar) { var underplating = _tileDefinitionManager["underplating"]; - eventArgs.ClickLocation.Grid.SetTile(eventArgs.ClickLocation, underplating.TileId); + mapGrid.SetTile(eventArgs.ClickLocation, underplating.TileId); _entitySystemManager.GetEntitySystem().Play("/Audio/items/crowbar.ogg", Owner); } } diff --git a/Content.Server/GameObjects/Components/Items/Storage/ServerStorageComponent.cs b/Content.Server/GameObjects/Components/Items/Storage/ServerStorageComponent.cs index 4c28e07c8f..35180c683f 100644 --- a/Content.Server/GameObjects/Components/Items/Storage/ServerStorageComponent.cs +++ b/Content.Server/GameObjects/Components/Items/Storage/ServerStorageComponent.cs @@ -1,5 +1,4 @@ using Content.Server.GameObjects.EntitySystems; -using Content.Server.Interfaces.GameObjects; using Content.Shared.GameObjects.Components.Storage; using Robust.Server.GameObjects; using Robust.Server.GameObjects.Components.Container; @@ -16,6 +15,7 @@ using Robust.Shared.Serialization; using System.Collections.Generic; using Content.Shared.Interfaces; using Robust.Shared.GameObjects.EntitySystemMessages; +using Robust.Shared.Interfaces.Map; using Robust.Shared.ViewVariables; using Content.Server.GameObjects.Components; @@ -26,6 +26,12 @@ namespace Content.Server.GameObjects /// public class ServerStorageComponent : SharedStorageComponent, IAttackBy, IUse, IActivate { +#pragma warning disable 649 + [Dependency] private readonly IMapManager _mapManager; + [Dependency] private readonly IPlayerManager _playerManager; + [Dependency] private readonly IEntityManager _entityManager; +#pragma warning restore 649 + private Container storage; private bool _storageInitialCalculated = false; @@ -269,18 +275,17 @@ namespace Content.Server.GameObjects case RemoveEntityMessage _: { _ensureInitialCalculated(); - var playerMan = IoCManager.Resolve(); - var session = playerMan.GetSessionByChannel(netChannel); + var session = _playerManager.GetSessionByChannel(netChannel); var playerentity = session.AttachedEntity; var ourtransform = Owner.GetComponent(); var playertransform = playerentity.GetComponent(); - if (playertransform.GridPosition.InRange(ourtransform.GridPosition, 2) + if (playertransform.GridPosition.InRange(_mapManager, ourtransform.GridPosition, 2) && (ourtransform.IsMapTransform || playertransform.ContainsEntity(ourtransform))) { var remove = (RemoveEntityMessage)message; - var entity = IoCManager.Resolve().GetEntity(remove.EntityUid); + var entity = _entityManager.GetEntity(remove.EntityUid); if (entity != null && storage.Contains(entity)) { Remove(entity); @@ -300,8 +305,7 @@ namespace Content.Server.GameObjects case CloseStorageUIMessage _: { - var playerMan = IoCManager.Resolve(); - var session = playerMan.GetSessionByChannel(netChannel); + var session = _playerManager.GetSessionByChannel(netChannel); UnsubscribeSession(session); } diff --git a/Content.Server/GameObjects/Components/Weapon/Melee/MeleeWeaponComponent.cs b/Content.Server/GameObjects/Components/Weapon/Melee/MeleeWeaponComponent.cs index a36dff18c1..18b3304576 100644 --- a/Content.Server/GameObjects/Components/Weapon/Melee/MeleeWeaponComponent.cs +++ b/Content.Server/GameObjects/Components/Weapon/Melee/MeleeWeaponComponent.cs @@ -1,22 +1,21 @@ -using System; -using Robust.Shared.GameObjects; +using Robust.Shared.GameObjects; using Content.Server.GameObjects.EntitySystems; -using Robust.Shared.Interfaces.GameObjects; -using Robust.Shared.Map; using Robust.Shared.IoC; -using Robust.Server.GameObjects; using Robust.Shared.Maths; using Robust.Server.Interfaces.GameObjects; -using Robust.Shared.Interfaces.Timing; -using Robust.Shared.GameObjects.EntitySystemMessages; using Robust.Shared.Serialization; -using Robust.Shared.Interfaces.GameObjects.Components; using Content.Shared.GameObjects; +using Robust.Shared.Interfaces.Map; namespace Content.Server.GameObjects.Components.Weapon.Melee { public class MeleeWeaponComponent : Component, IAfterAttack { +#pragma warning disable 649 + [Dependency] private readonly IMapManager _mapManager; + [Dependency] private readonly IServerEntityManager _serverEntityManager; +#pragma warning restore 649 + public override string Name => "MeleeWeapon"; public int Damage = 1; @@ -34,13 +33,13 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee void IAfterAttack.AfterAttack(AfterAttackEventArgs eventArgs) { - var location = eventArgs.User.GetComponent().GridPosition; - var angle = new Angle(eventArgs.ClickLocation.ToWorld().Position - location.ToWorld().Position); - var entities = IoCManager.Resolve().GetEntitiesInArc(eventArgs.User.GetComponent().GridPosition, Range, angle, ArcWidth); + var location = eventArgs.User.Transform.GridPosition; + var angle = new Angle(eventArgs.ClickLocation.ToWorld(_mapManager).Position - location.ToWorld(_mapManager).Position); + var entities = _serverEntityManager.GetEntitiesInArc(eventArgs.User.Transform.GridPosition, Range, angle, ArcWidth); foreach (var entity in entities) { - if (!entity.GetComponent().IsMapTransform || entity == eventArgs.User) + if (!entity.Transform.IsMapTransform || entity == eventArgs.User) continue; if (entity.TryGetComponent(out DamageableComponent damagecomponent)) diff --git a/Content.Server/GameObjects/EntitySystems/Click/InteractionSystem.cs b/Content.Server/GameObjects/EntitySystems/Click/InteractionSystem.cs index e1de64f8b2..45436dd0e1 100644 --- a/Content.Server/GameObjects/EntitySystems/Click/InteractionSystem.cs +++ b/Content.Server/GameObjects/EntitySystems/Click/InteractionSystem.cs @@ -12,6 +12,8 @@ using Robust.Shared.Map; using Robust.Server.GameObjects.EntitySystems; using Robust.Server.Interfaces.Player; using Robust.Shared.Interfaces.GameObjects.Components; +using Robust.Shared.Interfaces.Map; +using Robust.Shared.IoC; using Robust.Shared.Players; namespace Content.Server.GameObjects.EntitySystems @@ -138,6 +140,10 @@ namespace Content.Server.GameObjects.EntitySystems /// public class InteractionSystem : EntitySystem { +#pragma warning disable 649 + [Dependency] private readonly IMapManager _mapManager; +#pragma warning restore 649 + public const float INTERACTION_RANGE = 2; public const float INTERACTION_RANGE_SQUARED = INTERACTION_RANGE * INTERACTION_RANGE; @@ -158,7 +164,7 @@ namespace Content.Server.GameObjects.EntitySystems if(playerEnt == null || !playerEnt.IsValid()) return; - if (!playerEnt.Transform.GridPosition.InRange(used.Transform.GridPosition, INTERACTION_RANGE)) + if (!playerEnt.Transform.GridPosition.InRange(_mapManager, used.Transform.GridPosition, INTERACTION_RANGE)) return; var activateMsg = new ActivateInWorldMessage(playerEnt, used); @@ -175,7 +181,7 @@ namespace Content.Server.GameObjects.EntitySystems private void HandleUseItemInHand(ICommonSession session, GridCoordinates coords, EntityUid uid) { // client sanitization - if(!coords.IsValidLocation()) + if(!_mapManager.GridExists(coords.GridID)) { Logger.InfoS("system.interaction", $"Invalid Coordinates: client={session}, coords={coords}"); return; @@ -202,7 +208,7 @@ namespace Content.Server.GameObjects.EntitySystems return; } //Verify player is on the same map as the entity he clicked on - else if (coordinates.MapID != playerTransform.MapID) + else if (_mapManager.GetGrid(coordinates.GridID).ParentMap.Index != playerTransform.MapID) { Logger.Warning(string.Format("Player named {0} clicked on a map he isn't located on", player.Name)); return; diff --git a/Content.Server/GameObjects/EntitySystems/HandsSystem.cs b/Content.Server/GameObjects/EntitySystems/HandsSystem.cs index 37d92c7006..a2dd32a0e9 100644 --- a/Content.Server/GameObjects/EntitySystems/HandsSystem.cs +++ b/Content.Server/GameObjects/EntitySystems/HandsSystem.cs @@ -12,6 +12,7 @@ using Robust.Shared.GameObjects.EntitySystemMessages; using Robust.Shared.GameObjects.Systems; using Robust.Shared.Input; using Robust.Shared.Interfaces.GameObjects.Components; +using Robust.Shared.Interfaces.Map; using Robust.Shared.Interfaces.Timing; using Robust.Shared.IoC; using Robust.Shared.Map; @@ -22,6 +23,10 @@ namespace Content.Server.GameObjects.EntitySystems { internal class HandsSystem : EntitySystem { +#pragma warning disable 649 + [Dependency] private readonly IMapManager _mapManager; +#pragma warning restore 649 + private const float ThrowForce = 1.5f; // Throwing force of mobs in Newtons /// @@ -105,7 +110,7 @@ namespace Content.Server.GameObjects.EntitySystems handsComp.SwapHands(); } - private static void HandleDrop(ICommonSession session, GridCoordinates coords, EntityUid uid) + private void HandleDrop(ICommonSession session, GridCoordinates coords, EntityUid uid) { var ent = ((IPlayerSession) session).AttachedEntity; @@ -129,7 +134,7 @@ namespace Content.Server.GameObjects.EntitySystems handsComp.ActivateItem(); } - private static void HandleThrowItem(ICommonSession session, GridCoordinates coords, EntityUid uid) + private void HandleThrowItem(ICommonSession session, GridCoordinates coords, EntityUid uid) { var plyEnt = ((IPlayerSession)session).AttachedEntity; @@ -173,7 +178,7 @@ namespace Content.Server.GameObjects.EntitySystems projComp.IgnoreEntity(plyEnt); var transform = plyEnt.Transform; - var dirVec = (coords.ToWorld().Position - transform.WorldPosition).Normalized; + var dirVec = (coords.ToWorld(_mapManager).Position - transform.WorldPosition).Normalized; if (!throwEnt.TryGetComponent(out PhysicsComponent physComp)) { diff --git a/Content.Server/GameObjects/EntitySystems/MoverSystem.cs b/Content.Server/GameObjects/EntitySystems/MoverSystem.cs index 37d36a582a..e505f7c446 100644 --- a/Content.Server/GameObjects/EntitySystems/MoverSystem.cs +++ b/Content.Server/GameObjects/EntitySystems/MoverSystem.cs @@ -15,6 +15,7 @@ using Robust.Shared.GameObjects.Components.Transform; using Robust.Shared.GameObjects.Systems; using Robust.Shared.Input; using Robust.Shared.Interfaces.GameObjects.Components; +using Robust.Shared.Interfaces.Map; using Robust.Shared.IoC; using Robust.Shared.Map; using Robust.Shared.Maths; @@ -27,10 +28,10 @@ namespace Content.Server.GameObjects.EntitySystems internal class MoverSystem : EntitySystem { #pragma warning disable 649 - [Dependency] - private IPauseManager _pauseManager; - [Dependency] - private IPrototypeManager _prototypeManager; + [Dependency] private readonly IPauseManager _pauseManager; + [Dependency] private readonly IPrototypeManager _prototypeManager; + [Dependency] private readonly ITileDefinitionManager _tileDefinitionManager; + [Dependency] private readonly IMapManager _mapManager; #pragma warning restore 649 private AudioSystem _audioSystem; @@ -42,8 +43,6 @@ namespace Content.Server.GameObjects.EntitySystems /// public override void Initialize() { - IoCManager.InjectDependencies(this); - EntityQuery = new TypeEntityQuery(typeof(PlayerInputMoverComponent)); var moveUpCmdHandler = InputCmdHandler.FromDelegate( @@ -135,7 +134,7 @@ namespace Content.Server.GameObjects.EntitySystems transform.LocalRotation = mover.VelocityDir.GetDir().ToAngle(); // Handle footsteps. - var distance = transform.GridPosition.Distance(mover.LastPosition); + var distance = transform.GridPosition.Distance(_mapManager, mover.LastPosition); mover.StepSoundDistance += distance; mover.LastPosition = transform.GridPosition; float distanceNeeded; @@ -191,13 +190,13 @@ namespace Content.Server.GameObjects.EntitySystems private void PlayFootstepSound(GridCoordinates coordinates) { // Step one: figure out sound collection prototype. - var grid = coordinates.Grid; + var grid = _mapManager.GetGrid(coordinates.GridID); var tile = grid.GetTile(coordinates); // If the coordinates have a catwalk, it's always catwalk. string soundCollectionName; var catwalk = false; - foreach (var maybeCatwalk in grid.GetSnapGridCell(tile.GridTile, SnapGridOffset.Center)) + foreach (var maybeCatwalk in grid.GetSnapGridCell(tile.GridIndices, SnapGridOffset.Center)) { if (maybeCatwalk.Owner.HasComponent()) { @@ -214,7 +213,7 @@ namespace Content.Server.GameObjects.EntitySystems else { // Walking on a tile. - var def = (ContentTileDefinition)tile.TileDef; + var def = (ContentTileDefinition)_tileDefinitionManager[tile.Tile.TypeId]; if (def.FootstepSounds == null) { // Nothing to play, oh well. diff --git a/Resources/Groups/groups.yml b/Resources/Groups/groups.yml index cf4c0f9a6e..1df54bd807 100644 --- a/Resources/Groups/groups.yml +++ b/Resources/Groups/groups.yml @@ -43,4 +43,6 @@ - toggleready - spawn - delete + - tp + - tpgrid CanViewVar: true