diff --git a/Robust.Benchmarks/Transform/RecursiveMoveBenchmark.cs b/Robust.Benchmarks/Transform/RecursiveMoveBenchmark.cs index 5b2655861..ed6bda92a 100644 --- a/Robust.Benchmarks/Transform/RecursiveMoveBenchmark.cs +++ b/Robust.Benchmarks/Transform/RecursiveMoveBenchmark.cs @@ -54,7 +54,7 @@ public class RecursiveMoveBenchmark var mapSys = _entMan.System(); var mapId = mapMan.CreateMap(); var map = mapMan.GetMapEntityId(mapId); - var gridComp = mapMan.CreateGrid(mapId); + var gridComp = mapMan.CreateGridEntity(mapId); var grid = gridComp.Owner; _gridCoords = new EntityCoordinates(grid, .5f, .5f); _mapCoords = new EntityCoordinates(map, 100, 100); diff --git a/Robust.Client/Console/Commands/AddCompCommand.cs b/Robust.Client/Console/Commands/AddCompCommand.cs index 01f7e40a6..b4e638441 100644 --- a/Robust.Client/Console/Commands/AddCompCommand.cs +++ b/Robust.Client/Console/Commands/AddCompCommand.cs @@ -27,9 +27,6 @@ namespace Robust.Client.Console.Commands var componentName = args[1]; var component = _componentFactory.GetComponent(componentName); - - component.Owner = entity; - _entityManager.AddComponent(entity, component); } } diff --git a/Robust.Client/Debugging/DebugAnchoringSystem.cs b/Robust.Client/Debugging/DebugAnchoringSystem.cs index 858ff052f..5dd603d84 100644 --- a/Robust.Client/Debugging/DebugAnchoringSystem.cs +++ b/Robust.Client/Debugging/DebugAnchoringSystem.cs @@ -1,6 +1,7 @@ #if DEBUG using System.Numerics; using System.Text; +using Robust.Client.GameObjects; using Robust.Client.Graphics; using Robust.Client.Input; using Robust.Client.UserInterface; @@ -8,7 +9,6 @@ using Robust.Client.UserInterface.Controls; using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Map; -using Robust.Shared.Maths; using Robust.Shared.Utility; namespace Robust.Client.Debugging @@ -19,6 +19,7 @@ namespace Robust.Client.Debugging [Dependency] private readonly IInputManager _inputManager = default!; [Dependency] private readonly IMapManager _mapManager = default!; [Dependency] private readonly IUserInterfaceManager _userInterface = default!; + [Dependency] private readonly MapSystem _mapSystem = default!; private Label? _label; @@ -70,7 +71,7 @@ namespace Robust.Client.Debugging return; } - var tile = grid.GetTileRef(spot); + var tile = _mapSystem.GetTileRef(gridUid, grid, spot); _label.Position = mouseSpot.Position + new Vector2(32, 0); if (_hovered?.GridId == gridUid && _hovered?.Tile == tile) return; @@ -79,7 +80,7 @@ namespace Robust.Client.Debugging var text = new StringBuilder(); - foreach (var ent in grid.GetAnchoredEntities(spot)) + foreach (var ent in _mapSystem.GetAnchoredEntities(gridUid, grid, spot)) { if (EntityManager.TryGetComponent(ent, out var meta)) { diff --git a/Robust.Client/Debugging/DebugPhysicsSystem.cs b/Robust.Client/Debugging/DebugPhysicsSystem.cs index dc5f10cc2..f1fe77a72 100644 --- a/Robust.Client/Debugging/DebugPhysicsSystem.cs +++ b/Robust.Client/Debugging/DebugPhysicsSystem.cs @@ -46,7 +46,6 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Numerics; using Robust.Client.Graphics; using Robust.Client.Input; @@ -207,6 +206,7 @@ namespace Robust.Client.Debugging private readonly Font _font; private HashSet _drawnJoints = new(); + private List> _grids = new(); public PhysicsDebugOverlay(IEntityManager entityManager, IEyeManager eyeManager, IInputManager inputManager, IMapManager mapManager, IPlayerManager playerManager, IResourceCache cache, DebugPhysicsSystem system, EntityLookupSystem lookup, SharedPhysicsSystem physicsSystem) { @@ -231,32 +231,33 @@ namespace Robust.Client.Debugging { foreach (var physBody in _physicsSystem.GetCollidingEntities(mapId, viewBounds)) { - if (_entityManager.HasComponent(physBody.Owner)) continue; + if (_entityManager.HasComponent(physBody)) continue; - var xform = _physicsSystem.GetPhysicsTransform(physBody.Owner); + var xform = _physicsSystem.GetPhysicsTransform(physBody); + var comp = physBody.Comp; const float AlphaModifier = 0.2f; - foreach (var fixture in _entityManager.GetComponent(physBody.Owner).Fixtures.Values) + foreach (var fixture in _entityManager.GetComponent(physBody).Fixtures.Values) { // Invalid shape - Box2D doesn't check for IsSensor but we will for sanity. - if (physBody.BodyType == BodyType.Dynamic && fixture.Density == 0f && fixture.Hard) + if (comp.BodyType == BodyType.Dynamic && fixture.Density == 0f && fixture.Hard) { DrawShape(worldHandle, fixture, xform, Color.Red.WithAlpha(AlphaModifier)); } - else if (!physBody.CanCollide) + else if (!comp.CanCollide) { DrawShape(worldHandle, fixture, xform, new Color(0.5f, 0.5f, 0.3f).WithAlpha(AlphaModifier)); } - else if (physBody.BodyType == BodyType.Static) + else if (comp.BodyType == BodyType.Static) { DrawShape(worldHandle, fixture, xform, new Color(0.5f, 0.9f, 0.5f).WithAlpha(AlphaModifier)); } - else if ((physBody.BodyType & (BodyType.Kinematic | BodyType.KinematicController)) != 0x0) + else if ((comp.BodyType & (BodyType.Kinematic | BodyType.KinematicController)) != 0x0) { DrawShape(worldHandle, fixture, xform, new Color(0.5f, 0.5f, 0.9f).WithAlpha(AlphaModifier)); } - else if (!physBody.Awake) + else if (!comp.Awake) { DrawShape(worldHandle, fixture, xform, new Color(0.6f, 0.6f, 0.6f).WithAlpha(AlphaModifier)); } @@ -275,15 +276,18 @@ namespace Robust.Client.Debugging foreach (var physBody in _physicsSystem.GetCollidingEntities(mapId, viewBounds)) { var color = Color.Purple.WithAlpha(Alpha); - var transform = _physicsSystem.GetPhysicsTransform(physBody.Owner); - worldHandle.DrawCircle(Transform.Mul(transform, physBody.LocalCenter), 0.2f, color); + var transform = _physicsSystem.GetPhysicsTransform(physBody); + worldHandle.DrawCircle(Transform.Mul(transform, physBody.Comp.LocalCenter), 0.2f, color); } - foreach (var grid in _mapManager.FindGridsIntersecting(mapId, viewBounds)) + _grids.Clear(); + _mapManager.FindGridsIntersecting(mapId, viewBounds, ref _grids); + + foreach (var grid in _grids) { - var physBody = _entityManager.GetComponent(grid.Owner); + var physBody = _entityManager.GetComponent(grid); var color = Color.Orange.WithAlpha(Alpha); - var transform = _physicsSystem.GetPhysicsTransform(grid.Owner); + var transform = _physicsSystem.GetPhysicsTransform(grid); worldHandle.DrawCircle(Transform.Mul(transform, physBody.LocalCenter), 1f, color); } } @@ -292,14 +296,14 @@ namespace Robust.Client.Debugging { foreach (var physBody in _physicsSystem.GetCollidingEntities(mapId, viewBounds)) { - if (_entityManager.HasComponent(physBody.Owner)) continue; + if (_entityManager.HasComponent(physBody)) continue; - var xform = _physicsSystem.GetPhysicsTransform(physBody.Owner); + var xform = _physicsSystem.GetPhysicsTransform(physBody); const float AlphaModifier = 0.2f; Box2? aabb = null; - foreach (var fixture in _entityManager.GetComponent(physBody.Owner).Fixtures.Values) + foreach (var fixture in _entityManager.GetComponent(physBody).Fixtures.Values) { for (var i = 0; i < fixture.Shape.ChildCount; i++) { @@ -318,10 +322,11 @@ namespace Robust.Client.Debugging { _drawnJoints.Clear(); - foreach (var jointComponent in _entityManager.EntityQuery(true)) + var query = _entityManager.AllEntityQueryEnumerator(); + while (query.MoveNext(out var uid, out var jointComponent)) { if (jointComponent.JointCount == 0 || - !_entityManager.TryGetComponent(jointComponent.Owner, out TransformComponent? xf1) || + !_entityManager.TryGetComponent(uid, out TransformComponent? xf1) || !viewAABB.Contains(xf1.WorldPosition)) continue; foreach (var (_, joint) in jointComponent.Joints) @@ -370,28 +375,31 @@ namespace Robust.Client.Debugging if ((_debugPhysicsSystem.Flags & PhysicsDebugFlags.ShapeInfo) != 0x0) { - var hoverBodies = new List(); + var hoverBodies = new List>(); var bounds = Box2.UnitCentered.Translated(_eyeManager.PixelToMap(mousePos.Position).Position); foreach (var physBody in _physicsSystem.GetCollidingEntities(mapId, bounds)) { - if (_entityManager.HasComponent(physBody.Owner)) continue; - hoverBodies.Add(physBody); + var uid = physBody.Owner; + if (_entityManager.HasComponent(uid)) continue; + hoverBodies.Add((uid, physBody)); } var lineHeight = _font.GetLineHeight(1f); var drawPos = mousePos.Position + new Vector2(20, 0) + new Vector2(0, -(hoverBodies.Count * 4 * lineHeight / 2f)); int row = 0; - foreach (var body in hoverBodies) + foreach (var bodyEnt in hoverBodies) { - if (body != hoverBodies[0]) + if (bodyEnt != hoverBodies[0]) { screenHandle.DrawString(_font, drawPos + new Vector2(0, row * lineHeight), "------"); row++; } - screenHandle.DrawString(_font, drawPos + new Vector2(0, row * lineHeight), $"Ent: {body.Owner}"); + var body = bodyEnt.Comp; + + screenHandle.DrawString(_font, drawPos + new Vector2(0, row * lineHeight), $"Ent: {bodyEnt.Owner}"); row++; screenHandle.DrawString(_font, drawPos + new Vector2(0, row * lineHeight), $"Layer: {Convert.ToString(body.CollisionLayer, 2)}"); row++; diff --git a/Robust.Client/GameObjects/ClientEntityManager.cs b/Robust.Client/GameObjects/ClientEntityManager.cs index d6d2008eb..ab7498b65 100644 --- a/Robust.Client/GameObjects/ClientEntityManager.cs +++ b/Robust.Client/GameObjects/ClientEntityManager.cs @@ -86,10 +86,16 @@ namespace Robust.Client.GameObjects /// public override void Dirty(EntityUid uid, IComponent component, MetaDataComponent? meta = null) + { + Dirty(new Entity(uid, component), meta); + } + + /// + public override void Dirty(Entity ent, MetaDataComponent? meta = null) { // Client only dirties during prediction if (_gameTiming.InPrediction) - base.Dirty(uid, component, meta); + base.Dirty(ent, meta); } public override EntityStringRepresentation ToPrettyString(EntityUid uid, MetaDataComponent? metaDataComponent = null) diff --git a/Robust.Client/GameObjects/Components/Animations/AnimationPlayerComponent.cs b/Robust.Client/GameObjects/Components/Animations/AnimationPlayerComponent.cs index ed8aa5156..a8bf1de71 100644 --- a/Robust.Client/GameObjects/Components/Animations/AnimationPlayerComponent.cs +++ b/Robust.Client/GameObjects/Components/Animations/AnimationPlayerComponent.cs @@ -1,8 +1,6 @@ -using System; using System.Collections.Generic; using Robust.Client.Animations; using Robust.Shared.GameObjects; -using Robust.Shared.IoC; using static Robust.Client.Animations.AnimationPlaybackShared; namespace Robust.Client.GameObjects @@ -21,42 +19,5 @@ namespace Robust.Client.GameObjects = new(); internal bool HasPlayingAnimation = false; - - /// - /// Start playing an animation. - /// - /// The animation to play. - /// - /// The key for this animation play. This key can be used to stop playback short later. - /// - [Obsolete("Use AnimationPlayerSystem.Play() instead")] - public void Play(Animation animation, string key) - { - IoCManager.Resolve().GetEntitySystem().AddComponent(this); - var playback = new AnimationPlayback(animation); - - PlayingAnimations.Add(key, playback); - } - - [Obsolete("Use AnimationPlayerSystem.HasRunningAnimation() instead")] - public bool HasRunningAnimation(string key) - { - return PlayingAnimations.ContainsKey(key); - } - - [Obsolete("Use AnimationPlayerSystem.Stop() instead")] - public void Stop(string key) - { - PlayingAnimations.Remove(key); - } - - [Obsolete("Temporary method until the event is replaced with eventbus")] - internal void AnimationComplete(string key) - { - AnimationCompleted?.Invoke(key); - } - - [Obsolete("Use AnimationCompletedEvent instead")] - public event Action? AnimationCompleted; } } diff --git a/Robust.Client/GameObjects/EntitySystems/AnimationPlayerSystem.cs b/Robust.Client/GameObjects/EntitySystems/AnimationPlayerSystem.cs index 07bf13335..5ef0e1102 100644 --- a/Robust.Client/GameObjects/EntitySystems/AnimationPlayerSystem.cs +++ b/Robust.Client/GameObjects/EntitySystems/AnimationPlayerSystem.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using Robust.Client.Animations; using Robust.Shared.GameObjects; @@ -9,7 +10,7 @@ namespace Robust.Client.GameObjects { public sealed class AnimationPlayerSystem : EntitySystem, IPostInjectInit { - private readonly List _activeAnimations = new(); + private readonly List> _activeAnimations = new(); private EntityQuery _metaQuery; @@ -38,22 +39,22 @@ namespace Robust.Client.GameObjects continue; } - if (!Update(uid, anim, frameTime)) + if (!Update(uid, anim.Comp, frameTime)) { continue; } _activeAnimations.RemoveSwap(i); i--; - anim.HasPlayingAnimation = false; + anim.Comp.HasPlayingAnimation = false; } } - internal void AddComponent(AnimationPlayerComponent component) + internal void AddComponent(Entity ent) { - if (component.HasPlayingAnimation) return; - _activeAnimations.Add(component); - component.HasPlayingAnimation = true; + if (ent.Comp.HasPlayingAnimation) return; + _activeAnimations.Add(ent); + ent.Comp.HasPlayingAnimation = true; } private bool Update(EntityUid uid, AnimationPlayerComponent component, float frameTime) @@ -78,7 +79,6 @@ namespace Robust.Client.GameObjects { component.PlayingAnimations.Remove(key); EntityManager.EventBus.RaiseLocalEvent(uid, new AnimationCompletedEvent {Uid = uid, Key = key}, true); - component.AnimationComplete(key); } return false; @@ -89,22 +89,29 @@ namespace Robust.Client.GameObjects /// public void Play(EntityUid uid, Animation animation, string key) { - var component = EntityManager.EnsureComponent(uid); - Play(component, animation, key); + var component = EnsureComp(uid); + Play(new Entity(uid, component), animation, key); } + [Obsolete("Use Play(EntityUid ent, Animation animation, string key) instead")] public void Play(EntityUid uid, AnimationPlayerComponent? component, Animation animation, string key) { component ??= EntityManager.EnsureComponent(uid); - Play(component, animation, key); + Play(new Entity(uid, component), animation, key); } /// /// Start playing an animation. /// + [Obsolete("Use Play(EntityUid ent, Animation animation, string key) instead")] public void Play(AnimationPlayerComponent component, Animation animation, string key) { - AddComponent(component); + Play(new Entity(component.Owner, component), animation, key); + } + + public void Play(Entity ent, Animation animation, string key) + { + AddComponent(ent); var playback = new AnimationPlaybackShared.AnimationPlayback(animation); #if DEBUG @@ -120,14 +127,14 @@ namespace Robust.Client.GameObjects return; } - if (!EntityManager.TryGetComponent(component.Owner, compTrack.ComponentType, out var animatedComp)) + if (!EntityManager.TryGetComponent(ent, compTrack.ComponentType, out var animatedComp)) { _sawmill.Error( - $"Attempted to play a component animation, but the entity {ToPrettyString(component.Owner)} does not have the component to be animated: {compTrack.ComponentType}."); + $"Attempted to play a component animation, but the entity {ToPrettyString(ent)} does not have the component to be animated: {compTrack.ComponentType}."); return; } - if (IsClientSide(component.Owner) || !animatedComp.NetSyncEnabled) + if (IsClientSide(ent) || !animatedComp.NetSyncEnabled) continue; var reg = _compFact.GetRegistration(animatedComp); @@ -140,13 +147,13 @@ namespace Robust.Client.GameObjects if (animatedComp.GetType().GetProperty(compTrack.Property) is { } property && property.HasCustomAttribute()) { - _sawmill.Warning($"Playing a component animation on a networked component {reg.Name} belonging to {ToPrettyString(component.Owner)}"); + _sawmill.Warning($"Playing a component animation on a networked component {reg.Name} belonging to {ToPrettyString(ent)}"); } } } #endif - component.PlayingAnimations.Add(key, playback); + ent.Comp.PlayingAnimations.Add(key, playback); } public bool HasRunningAnimation(EntityUid uid, string key) diff --git a/Robust.Client/GameObjects/EntitySystems/GridChunkBoundsDebugSystem.cs b/Robust.Client/GameObjects/EntitySystems/GridChunkBoundsDebugSystem.cs index 7d8a17b38..b7e1e8b2d 100644 --- a/Robust.Client/GameObjects/EntitySystems/GridChunkBoundsDebugSystem.cs +++ b/Robust.Client/GameObjects/EntitySystems/GridChunkBoundsDebugSystem.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.Numerics; using Robust.Client.Graphics; using Robust.Shared.Enums; @@ -58,6 +59,8 @@ namespace Robust.Client.GameObjects public override OverlaySpace Space => OverlaySpace.WorldSpace; + private List> _grids = new(); + public GridChunkBoundsOverlay(IEntityManager entManager, IEyeManager eyeManager, IMapManager mapManager) { _entityManager = entManager; @@ -71,13 +74,15 @@ namespace Robust.Client.GameObjects var viewport = args.WorldBounds; var worldHandle = args.WorldHandle; - foreach (var grid in _mapManager.FindGridsIntersecting(currentMap, viewport)) + _grids.Clear(); + _mapManager.FindGridsIntersecting(currentMap, viewport, ref _grids); + foreach (var grid in _grids) { - var worldMatrix = _entityManager.GetComponent(grid.Owner).WorldMatrix; + var worldMatrix = _entityManager.GetComponent(grid).WorldMatrix; worldHandle.SetTransform(worldMatrix); var transform = new Transform(Vector2.Zero, Angle.Zero); - var chunkEnumerator = grid.GetMapChunks(viewport); + var chunkEnumerator = grid.Comp.GetMapChunks(viewport); while (chunkEnumerator.MoveNext(out var chunk)) { diff --git a/Robust.Client/GameObjects/EntitySystems/TransformSystem.cs b/Robust.Client/GameObjects/EntitySystems/TransformSystem.cs index 7ff762d7e..91540803b 100644 --- a/Robust.Client/GameObjects/EntitySystems/TransformSystem.cs +++ b/Robust.Client/GameObjects/EntitySystems/TransformSystem.cs @@ -28,11 +28,11 @@ namespace Robust.Client.GameObjects // Only keep track of transforms actively lerping. // Much faster than iterating 3000+ transforms every frame. - [ViewVariables] private readonly List _lerpingTransforms = new(); + [ViewVariables] private readonly List> _lerpingTransforms = new(); public void Reset() { - foreach (var xform in _lerpingTransforms) + foreach (var (_, xform) in _lerpingTransforms) { xform.ActivelyLerping = false; xform.NextPosition = null; @@ -77,7 +77,7 @@ namespace Robust.Client.GameObjects return; } - _lerpingTransforms.Add(xform); + _lerpingTransforms.Add((uid, xform)); xform.ActivelyLerping = true; xform.PredictedLerp = false; xform.LerpParent = xform.ParentUid; @@ -96,7 +96,7 @@ namespace Robust.Client.GameObjects if (!xform.ActivelyLerping) { - _lerpingTransforms.Add(xform); + _lerpingTransforms.Add((uid, xform)); xform.ActivelyLerping = true; xform.PredictedLerp = true; xform.PrevRotation = xform._localRotation; @@ -123,8 +123,7 @@ namespace Robust.Client.GameObjects for (var i = 0; i < _lerpingTransforms.Count; i++) { - var transform = _lerpingTransforms[i]; - var uid = transform.Owner; + var (uid, transform) = _lerpingTransforms[i]; var found = false; // Only lerp if parent didn't change. diff --git a/Robust.Client/GameStates/ClientGameStateManager.cs b/Robust.Client/GameStates/ClientGameStateManager.cs index 6abcea0e2..4f46e6798 100644 --- a/Robust.Client/GameStates/ClientGameStateManager.cs +++ b/Robust.Client/GameStates/ClientGameStateManager.cs @@ -604,7 +604,7 @@ namespace Robust.Client.GameStates /// Whenever a new entity is created, the server doesn't send full state data, given that much of the data /// can simply be obtained from the entity prototype information. This function basically creates a fake /// initial server state for any newly created entity. It does this by simply using the standard . + /// cref="IEntityManager.GetComponentState"/>. /// private void MergeImplicitData(IEnumerable createdEntities) { @@ -1188,7 +1188,6 @@ namespace Robust.Client.GameStates if (!meta.NetComponents.TryGetValue(id, out var comp)) { comp = _compFactory.GetComponent(id); - comp.Owner = uid; _entityManager.AddComponent(uid, comp, true, metadata: meta); } @@ -1202,7 +1201,6 @@ namespace Robust.Client.GameStates if (!meta.NetComponents.TryGetValue(compChange.NetID, out var comp)) { comp = _compFactory.GetComponent(compChange.NetID); - comp.Owner = uid; _entityManager.AddComponent(uid, comp, true, metadata:meta); } else if (compChange.LastModifiedTick <= lastApplied && lastApplied != GameTick.Zero) @@ -1272,7 +1270,9 @@ namespace Robust.Client.GameStates var handleState = new ComponentHandleState(cur, next); bus.RaiseComponentEvent(comp, ref handleState); } +#pragma warning disable CS0168 // Variable is declared but never used catch (Exception e) +#pragma warning restore CS0168 // Variable is declared but never used { #if EXCEPTION_TOLERANCE _sawmill.Error($"Failed to apply comp state: entity={_entities.ToPrettyString(uid)}, comp={comp.GetType()}"); @@ -1429,7 +1429,6 @@ namespace Robust.Client.GameStates if (!meta.NetComponents.TryGetValue(id, out var comp)) { comp = _compFactory.GetComponent(id); - comp.Owner = uid; _entityManager.AddComponent(uid, comp, true, meta); } diff --git a/Robust.Client/Graphics/Clyde/Clyde.GridRendering.cs b/Robust.Client/Graphics/Clyde/Clyde.GridRendering.cs index 7e6a09140..8c87f3f3a 100644 --- a/Robust.Client/Graphics/Clyde/Clyde.GridRendering.cs +++ b/Robust.Client/Graphics/Clyde/Clyde.GridRendering.cs @@ -4,7 +4,6 @@ using OpenToolkit.Graphics.OpenGL4; using Robust.Shared.GameObjects; using Robust.Shared.Graphics; using Robust.Shared.IoC; -using Robust.Shared.Log; using Robust.Shared.Map; using Robust.Shared.Map.Components; using Robust.Shared.Maths; @@ -41,21 +40,23 @@ namespace Robust.Client.Graphics.Clyde gridProgram.SetUniformTextureMaybe(UniILightTexture, TextureUnit.Texture1); gridProgram.SetUniform(UniIModUV, new Vector4(0, 0, 1, 1)); - foreach (var mapGrid in _mapManager.FindGridsIntersecting(mapId, worldBounds)) + var grids = new List>(); + _mapManager.FindGridsIntersecting(mapId, worldBounds, ref grids); + foreach (var mapGrid in grids) { - if (!_mapChunkData.ContainsKey(mapGrid.Owner)) + if (!_mapChunkData.ContainsKey(mapGrid)) continue; - var transform = _entityManager.GetComponent(mapGrid.Owner); + var transform = _entityManager.GetComponent(mapGrid); gridProgram.SetUniform(UniIModelMatrix, transform.WorldMatrix); - var enumerator = mapGrid.GetMapChunks(worldBounds); + var enumerator = mapGrid.Comp.GetMapChunks(worldBounds); while (enumerator.MoveNext(out var chunk)) { if (_isChunkDirty(mapGrid, chunk)) _updateChunkMesh(mapGrid, chunk); - var datum = _mapChunkData[mapGrid.Owner][chunk.Indices]; + var datum = _mapChunkData[mapGrid][chunk.Indices]; if (datum.TileCount == 0) continue; @@ -70,9 +71,9 @@ namespace Robust.Client.Graphics.Clyde } } - private void _updateChunkMesh(MapGridComponent grid, MapChunk chunk) + private void _updateChunkMesh(Entity grid, MapChunk chunk) { - var data = _mapChunkData[grid.Owner]; + var data = _mapChunkData[grid]; if (!data.TryGetValue(chunk.Indices, out var datum)) { @@ -83,7 +84,7 @@ namespace Robust.Client.Graphics.Clyde Span vertexBuffer = stackalloc Vertex2D[_verticesPerChunk(chunk)]; var i = 0; - var cSz = grid.ChunkSize; + var cSz = grid.Comp.ChunkSize; var cScaled = chunk.Indices * cSz; for (ushort x = 0; x < cSz; x++) { @@ -130,7 +131,7 @@ namespace Robust.Client.Graphics.Clyde datum.TileCount = i; } - private unsafe MapChunkData _initChunkBuffers(MapGridComponent grid, MapChunk chunk) + private unsafe MapChunkData _initChunkBuffers(Entity grid, MapChunk chunk) { var vao = GenVertexArray(); BindVertexArray(vao); @@ -158,19 +159,19 @@ namespace Robust.Client.Graphics.Clyde Dirty = true }; - _mapChunkData[grid.Owner].Add(chunk.Indices, datum); + _mapChunkData[grid].Add(chunk.Indices, datum); return datum; } - private bool _isChunkDirty(MapGridComponent grid, MapChunk chunk) + private bool _isChunkDirty(Entity grid, MapChunk chunk) { - var data = _mapChunkData[grid.Owner]; + var data = _mapChunkData[grid]; return !data.TryGetValue(chunk.Indices, out var datum) || datum.Dirty; } - public void _setChunkDirty(MapGridComponent grid, Vector2i chunk) + public void _setChunkDirty(Entity grid, Vector2i chunk) { - var data = _mapChunkData.GetOrNew(grid.Owner); + var data = _mapChunkData.GetOrNew(grid); if (data.TryGetValue(chunk, out var datum)) { datum.Dirty = true; @@ -184,7 +185,7 @@ namespace Robust.Client.Graphics.Clyde { var grid = args.Grid; var chunk = grid.GridTileToChunkIndices(pos); - _setChunkDirty(grid, chunk); + _setChunkDirty((args.GridEnt, grid), chunk); } } @@ -192,7 +193,7 @@ namespace Robust.Client.Graphics.Clyde { var grid = _mapManager.GetGrid(args.NewTile.GridUid); var chunk = grid.GridTileToChunkIndices(new Vector2i(args.NewTile.X, args.NewTile.Y)); - _setChunkDirty(grid, chunk); + _setChunkDirty((args.NewTile.GridUid, grid), chunk); } private void _updateOnGridCreated(GridStartupEvent ev) diff --git a/Robust.Client/Graphics/Clyde/Clyde.HLR.cs b/Robust.Client/Graphics/Clyde/Clyde.HLR.cs index f857c0233..36b7a0154 100644 --- a/Robust.Client/Graphics/Clyde/Clyde.HLR.cs +++ b/Robust.Client/Graphics/Clyde/Clyde.HLR.cs @@ -350,7 +350,7 @@ namespace Robust.Client.Graphics.Clyde _renderHandle.Viewport(Box2i.FromDimensions(-flippedPos, screenSize)); if (entry.Sprite.RaiseShaderEvent) - _entityManager.EventBus.RaiseLocalEvent(entry.Sprite.Owner, + _entityManager.EventBus.RaiseLocalEvent(entry.Uid, new BeforePostShaderRenderEvent(entry.Sprite, viewport), false); } } diff --git a/Robust.Client/Graphics/Clyde/Clyde.Sprite.cs b/Robust.Client/Graphics/Clyde/Clyde.Sprite.cs index b51583a47..cad95db8f 100644 --- a/Robust.Client/Graphics/Clyde/Clyde.Sprite.cs +++ b/Robust.Client/Graphics/Clyde/Clyde.Sprite.cs @@ -1,11 +1,3 @@ -using Robust.Client.ComponentTrees; -using Robust.Client.GameObjects; -using Robust.Shared.GameObjects; -using Robust.Shared.Map; -using Robust.Shared.Maths; -using Robust.Shared.Physics; -using Robust.Shared.Threading; -using Robust.Shared.Utility; using System; using System.Buffers; using System.Collections.Generic; @@ -14,7 +6,15 @@ using System.Runtime.CompilerServices; using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; using System.Threading.Tasks; +using Robust.Client.ComponentTrees; +using Robust.Client.GameObjects; +using Robust.Shared.GameObjects; using Robust.Shared.Graphics; +using Robust.Shared.Map; +using Robust.Shared.Maths; +using Robust.Shared.Physics; +using Robust.Shared.Threading; +using Robust.Shared.Utility; namespace Robust.Client.Graphics.Clyde; @@ -260,7 +260,7 @@ internal partial class Clyde if (cmp != 0) return cmp; - return a.Sprite.Owner.CompareTo(b.Sprite.Owner); + return a.Uid.CompareTo(b.Uid); } } } diff --git a/Robust.Client/Graphics/Clyde/Clyde.cs b/Robust.Client/Graphics/Clyde/Clyde.cs index f4416c193..81852f34c 100644 --- a/Robust.Client/Graphics/Clyde/Clyde.cs +++ b/Robust.Client/Graphics/Clyde/Clyde.cs @@ -17,11 +17,9 @@ using Robust.Shared.IoC; using Robust.Shared.Localization; using Robust.Shared.Log; using Robust.Shared.Map; +using Robust.Shared.Maths; using Robust.Shared.Profiling; using Robust.Shared.Timing; -using SixLabors.ImageSharp; -using Color = Robust.Shared.Maths.Color; -using DependencyAttribute = Robust.Shared.IoC.DependencyAttribute; using TextureWrapMode = Robust.Shared.Graphics.TextureWrapMode; namespace Robust.Client.Graphics.Clyde diff --git a/Robust.Client/Map/TileEdgeOverlay.cs b/Robust.Client/Map/TileEdgeOverlay.cs index 456680d3d..b27290d2a 100644 --- a/Robust.Client/Map/TileEdgeOverlay.cs +++ b/Robust.Client/Map/TileEdgeOverlay.cs @@ -1,12 +1,13 @@ using System; +using System.Collections.Generic; using System.Numerics; using Robust.Client.Graphics; using Robust.Client.ResourceManagement; using Robust.Shared.Enums; using Robust.Shared.GameObjects; using Robust.Shared.Map; +using Robust.Shared.Map.Components; using Robust.Shared.Maths; -using Direction = Robust.Shared.Maths.Direction; namespace Robust.Client.Map; @@ -22,6 +23,8 @@ public sealed class TileEdgeOverlay : Overlay public override OverlaySpace Space => OverlaySpace.WorldSpaceBelowEntities; + private List> _grids = new(); + public TileEdgeOverlay(IEntityManager entManager, IMapManager mapManager, IResourceCache resource, ITileDefinitionManager tileDefManager) { _entManager = entManager; @@ -36,16 +39,18 @@ public sealed class TileEdgeOverlay : Overlay if (args.MapId == MapId.Nullspace) return; - var xformQuery = _entManager.GetEntityQuery(); + _grids.Clear(); + _mapManager.FindGridsIntersecting(args.MapId, args.WorldBounds, ref _grids); - foreach (var grid in _mapManager.FindGridsIntersecting(args.MapId, args.WorldBounds)) + var xformQuery = _entManager.GetEntityQuery(); + foreach (var grid in _grids) { - var tileSize = grid.TileSize; + var tileSize = grid.Comp.TileSize; var tileDimensions = new Vector2(tileSize, tileSize); - var xform = xformQuery.GetComponent(grid.Owner); + var xform = xformQuery.GetComponent(grid); args.WorldHandle.SetTransform(xform.WorldMatrix); - foreach (var tileRef in grid.GetTilesIntersecting(args.WorldBounds, false)) + foreach (var tileRef in grid.Comp.GetTilesIntersecting(args.WorldBounds, false)) { var tileDef = _tileDefManager[tileRef.Tile.TypeId]; @@ -61,7 +66,7 @@ public sealed class TileEdgeOverlay : Overlay continue; var neighborIndices = new Vector2i(tileRef.GridIndices.X + x, tileRef.GridIndices.Y + y); - var neighborTile = grid.GetTileRef(neighborIndices); + var neighborTile = grid.Comp.GetTileRef(neighborIndices); var neighborDef = _tileDefManager[neighborTile.Tile.TypeId]; // If it's the same tile then no edge to be drawn. diff --git a/Robust.Client/Physics/PhysicsSystem.cs b/Robust.Client/Physics/PhysicsSystem.cs index d84e884de..0c756ebfc 100644 --- a/Robust.Client/Physics/PhysicsSystem.cs +++ b/Robust.Client/Physics/PhysicsSystem.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; using JetBrains.Annotations; -using Robust.Client.Player; using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Physics; @@ -26,7 +25,7 @@ namespace Robust.Client.Physics protected override void Cleanup(PhysicsMapComponent component, float frameTime) { - var toRemove = new List(); + var toRemove = new List>(); // Because we're not predicting 99% of bodies its sleep timer never gets incremented so we'll just do it ourselves. // (and serializing it over the network isn't necessary?) @@ -38,13 +37,13 @@ namespace Robust.Client.Physics body.SleepTime += frameTime; if (body.SleepTime > TimeToSleep) { - toRemove.Add(body); + toRemove.Add(new Entity(body.Owner, body)); } } foreach (var body in toRemove) { - SetAwake(body.Owner, body, false); + SetAwake(body, false); } base.Cleanup(component, frameTime); diff --git a/Robust.Client/UserInterface/Controls/SpriteView.cs b/Robust.Client/UserInterface/Controls/SpriteView.cs index 71505c38f..3ac01c370 100644 --- a/Robust.Client/UserInterface/Controls/SpriteView.cs +++ b/Robust.Client/UserInterface/Controls/SpriteView.cs @@ -7,7 +7,6 @@ using Robust.Shared.IoC; using Robust.Shared.Maths; using Robust.Shared.Utility; using Robust.Shared.ViewVariables; -using Direction = Robust.Shared.Maths.Direction; namespace Robust.Client.UserInterface.Controls { @@ -18,18 +17,14 @@ namespace Robust.Client.UserInterface.Controls IEntityManager _entMan; [ViewVariables] - private SpriteComponent? _sprite; - public SpriteComponent? Sprite - { - get => _sprite; - [Obsolete("Use SetEntity()")] - set => SetEntity(value?.Owner); - } + public SpriteComponent? Sprite { get; private set; } [ViewVariables] public EntityUid? Entity { get; private set; } + public Entity? Ent => Entity == null || Sprite == null ? null : (Entity.Value, Sprite); + /// /// This field configures automatic scaling of the sprite. This automatic scaling is done before /// applying the explicitly set scale . @@ -124,14 +119,22 @@ namespace Robust.Client.UserInterface.Controls public SpriteView() { _entMan = IoCManager.Resolve(); - _entMan.TryGetComponent(Entity, out _sprite); + if (_entMan.TryGetComponent(Entity, out SpriteComponent? sprite)) + { + Sprite = sprite; + } + RectClipContent = true; } public void SetEntity(EntityUid? uid) { Entity = uid; - _entMan.TryGetComponent(Entity, out _sprite); + + if (_entMan.TryGetComponent(Entity, out SpriteComponent? sprite)) + { + Sprite = sprite; + } } protected override Vector2 MeasureOverride(Vector2 availableSize) @@ -143,13 +146,13 @@ namespace Robust.Client.UserInterface.Controls private void UpdateSize() { - if (Entity == null || _sprite == null) + if (Entity == null || Sprite == null) { _spriteSize = default; return; } - var spriteBox = _sprite.CalculateRotatedBoundingBox(default, _worldRotation ?? Angle.Zero, _eyeRotation) + var spriteBox = Sprite.CalculateRotatedBoundingBox(default, _worldRotation ?? Angle.Zero, _eyeRotation) .CalcBoundingBox(); if (!SpriteOffset) @@ -191,10 +194,10 @@ namespace Robust.Client.UserInterface.Controls internal override void DrawInternal(IRenderHandle renderHandle) { - if (Entity is not {} uid || _sprite == null) + if (Entity is not {} uid || Sprite == null) return; - if (_sprite.Deleted) + if (Sprite.Deleted) { SetEntity(null); return; @@ -214,11 +217,11 @@ namespace Robust.Client.UserInterface.Controls var offset = SpriteOffset ? Vector2.Zero - : - (-_eyeRotation).RotateVec(_sprite.Offset) * new Vector2(1, -1) * EyeManager.PixelsPerMeter; + : - (-_eyeRotation).RotateVec(Sprite.Offset) * new Vector2(1, -1) * EyeManager.PixelsPerMeter; var position = PixelSize / 2 + offset * stretch * UIScale; var scale = Scale * UIScale * stretch; - renderHandle.DrawEntity(uid, position, scale, _worldRotation, _eyeRotation, OverrideDirection, _sprite); + renderHandle.DrawEntity(uid, position, scale, _worldRotation, _eyeRotation, OverrideDirection, Sprite); } } } diff --git a/Robust.Client/UserInterface/CustomControls/DebugMonitorControls/DebugCoordsPanel.cs b/Robust.Client/UserInterface/CustomControls/DebugMonitorControls/DebugCoordsPanel.cs index 6641efede..5e1783ccc 100644 --- a/Robust.Client/UserInterface/CustomControls/DebugMonitorControls/DebugCoordsPanel.cs +++ b/Robust.Client/UserInterface/CustomControls/DebugMonitorControls/DebugCoordsPanel.cs @@ -117,7 +117,7 @@ Mouse Pos: {playerWorldOffset} {playerCoordinates} Rotation: {playerRotation.Degrees:F2}° - EntId: {entityTransform.Owner} + EntId: {controlledEntity} GridUid: {entityTransform.GridUid} Grid Rotation: {gridRotation.Degrees:F2}°"); } diff --git a/Robust.Client/ViewVariables/Editors/VVPropEditorEntityCoordinates.cs b/Robust.Client/ViewVariables/Editors/VVPropEditorEntityCoordinates.cs index 116d7de81..91620f5b5 100644 --- a/Robust.Client/ViewVariables/Editors/VVPropEditorEntityCoordinates.cs +++ b/Robust.Client/ViewVariables/Editors/VVPropEditorEntityCoordinates.cs @@ -5,7 +5,7 @@ using Robust.Client.UserInterface.Controls; using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Map; -using Robust.Shared.Maths; +using Robust.Shared.Map.Components; using static Robust.Client.UserInterface.Controls.BoxContainer; namespace Robust.Client.ViewVariables.Editors @@ -67,13 +67,13 @@ namespace Robust.Client.ViewVariables.Editors var xVal = float.Parse(x.Text, CultureInfo.InvariantCulture); var yVal = float.Parse(y.Text, CultureInfo.InvariantCulture); - if (!mapManager.TryGetGrid(gridVal, out var grid)) + if (!entityManager.HasComponent(gridVal)) { ValueChanged(new EntityCoordinates(EntityUid.Invalid, new(xVal, yVal))); return; } - ValueChanged(new EntityCoordinates(grid.Owner, new(xVal, yVal))); + ValueChanged(new EntityCoordinates(gridVal, new(xVal, yVal))); } if (!ReadOnly) diff --git a/Robust.Client/ViewVariables/Instances/ViewVariablesInstanceEntity.cs b/Robust.Client/ViewVariables/Instances/ViewVariablesInstanceEntity.cs index bacd5a376..057213418 100644 --- a/Robust.Client/ViewVariables/Instances/ViewVariablesInstanceEntity.cs +++ b/Robust.Client/ViewVariables/Instances/ViewVariablesInstanceEntity.cs @@ -120,7 +120,11 @@ namespace Robust.Client.ViewVariables.Instances }; top.HorizontalExpand = true; hBox.AddChild(top); - hBox.AddChild(new SpriteView {Sprite = sprite, OverrideDirection = Direction.South}); + + var view = new SpriteView { OverrideDirection = Direction.South }; + view.SetEntity(_entity); + hBox.AddChild(view); + vBoxContainer.AddChild(hBox); } else @@ -432,7 +436,6 @@ namespace Robust.Client.ViewVariables.Instances try { var comp = componentFactory.GetComponent(registration.Type); - comp.Owner = _entity; _entityManager.AddComponent(_entity, comp); } catch (Exception e) diff --git a/Robust.Server/Console/Commands/AddComponentCommand.cs b/Robust.Server/Console/Commands/AddComponentCommand.cs index 15da99a65..c50c985ff 100644 --- a/Robust.Server/Console/Commands/AddComponentCommand.cs +++ b/Robust.Server/Console/Commands/AddComponentCommand.cs @@ -47,10 +47,6 @@ namespace Robust.Server.Console.Commands } var component = _componentFactory.GetComponent(registration.Type); - -#pragma warning disable CS0618 - component.Owner = uid.Value; -#pragma warning restore CS0618 _entityManager.AddComponent(uid.Value, component); shell.WriteLine($"Added {componentName} component to entity {_entityManager.GetComponent(uid.Value).EntityName}."); diff --git a/Robust.Server/GameObjects/Components/VisibilityComponent.cs b/Robust.Server/GameObjects/Components/VisibilityComponent.cs index b1fefdb05..05c52d614 100644 --- a/Robust.Server/GameObjects/Components/VisibilityComponent.cs +++ b/Robust.Server/GameObjects/Components/VisibilityComponent.cs @@ -1,5 +1,4 @@ using System; -using Robust.Shared.Analyzers; using Robust.Shared.GameObjects; using Robust.Shared.Serialization.Manager.Attributes; using Robust.Shared.ViewVariables; @@ -22,7 +21,7 @@ namespace Robust.Server.GameObjects public int LayerVV { get => Layer; - set => EntitySystem.Get().SetLayer(this, value); + set => EntitySystem.Get().SetLayer(Owner, this, value); } } } diff --git a/Robust.Server/GameObjects/EntitySystems/MapLoaderSystem.cs b/Robust.Server/GameObjects/EntitySystems/MapLoaderSystem.cs index 723acac05..12c14b1c3 100644 --- a/Robust.Server/GameObjects/EntitySystems/MapLoaderSystem.cs +++ b/Robust.Server/GameObjects/EntitySystems/MapLoaderSystem.cs @@ -6,7 +6,6 @@ using System.IO; using System.Linq; using System.Numerics; using Robust.Server.Maps; -using Robust.Shared.Collections; using Robust.Shared.ContentPack; using Robust.Shared.GameObjects; using Robust.Shared.IoC; @@ -733,7 +732,7 @@ public sealed class MapLoaderSystem : EntitySystem return; // get ents that the grids will bind to - var gridComps = new MapGridComponent[yamlGrids.Count]; + var gridComps = new Entity[yamlGrids.Count]; var gridQuery = _serverEntityManager.GetEntityQuery(); // linear search for new grid comps @@ -745,7 +744,7 @@ public sealed class MapLoaderSystem : EntitySystem // These should actually be new, pre-init DebugTools.Assert(gridComp.LifeStage == ComponentLifeStage.Added); - gridComps[gridComp.GridIndex] = gridComp; + gridComps[gridComp.GridIndex] = new Entity(uid, gridComp); } for (var index = 0; index < yamlGrids.Count; index++) @@ -764,18 +763,18 @@ public sealed class MapLoaderSystem : EntitySystem MappingDataNode yamlGridInfo = (MappingDataNode)yamlGrid["settings"]; SequenceDataNode yamlGridChunks = (SequenceDataNode)yamlGrid["chunks"]; - var grid = AllocateMapGrid(gridComp, yamlGridInfo); - var gridUid = grid.Owner; + AllocateMapGrid(gridComp, yamlGridInfo); + var gridUid = gridComp.Owner; foreach (var chunkNode in yamlGridChunks.Cast()) { var (chunkOffsetX, chunkOffsetY) = _serManager.Read(chunkNode["ind"]); - _serManager.Read(chunkNode, _context, instanceProvider: () => _mapSystem.GetOrAddChunk(gridUid, grid, chunkOffsetX, chunkOffsetY), notNullableOverride: true); + _serManager.Read(chunkNode, _context, instanceProvider: () => _mapSystem.GetOrAddChunk(gridUid, gridComp, chunkOffsetX, chunkOffsetY), notNullableOverride: true); } } } - private static MapGridComponent AllocateMapGrid(MapGridComponent gridComp, MappingDataNode yamlGridInfo) + private static void AllocateMapGrid(MapGridComponent gridComp, MappingDataNode yamlGridInfo) { // sane defaults ushort csz = 16; @@ -795,8 +794,6 @@ public sealed class MapLoaderSystem : EntitySystem gridComp.ChunkSize = csz; gridComp.TileSize = tsz; - - return gridComp; } private void StartupEntities(MapData data) diff --git a/Robust.Server/Physics/GridFixtureSystem.cs b/Robust.Server/Physics/GridFixtureSystem.cs index 22cf6c2bc..cacf322a6 100644 --- a/Robust.Server/Physics/GridFixtureSystem.cs +++ b/Robust.Server/Physics/GridFixtureSystem.cs @@ -260,7 +260,7 @@ namespace Robust.Server.Physics for (var i = 0; i < grids.Count - 1; i++) { var group = grids[i]; - var newGrid = _mapManager.CreateGrid(mapId); + var newGrid = _mapManager.CreateGridEntity(mapId); var newGridUid = newGrid.Owner; var newGridXform = xformQuery.GetComponent(newGridUid); newGrids[i] = newGridUid; @@ -287,7 +287,7 @@ namespace Robust.Server.Physics } } - newGrid.SetTiles(tileData); + newGrid.Comp.SetTiles(tileData); DebugTools.Assert(_mapManager.IsGrid(newGridUid), "A split grid had no tiles?"); // Set tiles on new grid + update anchored entities diff --git a/Robust.Server/Placement/PlacementManager.cs b/Robust.Server/Placement/PlacementManager.cs index b18970d9a..ad310886f 100644 --- a/Robust.Server/Placement/PlacementManager.cs +++ b/Robust.Server/Placement/PlacementManager.cs @@ -199,11 +199,11 @@ namespace Robust.Server.Placement } else if (tileType != 0) // create a new grid { - var newGrid = _mapManager.CreateGrid(coordinates.GetMapId(_entityManager)); - var newGridXform = _entityManager.GetComponent(newGrid.Owner); - newGridXform.WorldPosition = coordinates.Position - newGrid.TileSizeHalfVector; // assume bottom left tile origin - var tilePos = newGrid.WorldToTile(coordinates.Position); - newGrid.SetTile(tilePos, new Tile(tileType)); + var newGrid = _mapManager.CreateGridEntity(coordinates.GetMapId(_entityManager)); + var newGridXform = _entityManager.GetComponent(newGrid); + newGridXform.WorldPosition = coordinates.Position - newGrid.Comp.TileSizeHalfVector; // assume bottom left tile origin + var tilePos = newGrid.Comp.WorldToTile(coordinates.Position); + newGrid.Comp.SetTile(tilePos, new Tile(tileType)); var placementEraseEvent = new PlacementTileEvent(tileType, coordinates, placingUserId); _entityManager.EventBus.RaiseEvent(EventSource.Local, placementEraseEvent); diff --git a/Robust.Shared/Console/Commands/MapCommands.cs b/Robust.Shared/Console/Commands/MapCommands.cs index 17d9a671d..e0ff05b29 100644 --- a/Robust.Shared/Console/Commands/MapCommands.cs +++ b/Robust.Shared/Console/Commands/MapCommands.cs @@ -148,7 +148,7 @@ internal sealed class ListMapsCommand : LocalizedCommands mapId, _map.IsMapInitialized(mapId), _map.IsMapPaused(mapId), _map.GetMapEntityId(mapId), - string.Join(",", _map.GetAllMapGrids(mapId).Select(grid => grid.Owner))); + string.Join(",", _map.GetAllGrids(mapId).Select(grid => grid.Owner))); } shell.WriteLine(msg.ToString()); diff --git a/Robust.Shared/Containers/BaseContainer.cs b/Robust.Shared/Containers/BaseContainer.cs index 1d918f19a..a80f40413 100644 --- a/Robust.Shared/Containers/BaseContainer.cs +++ b/Robust.Shared/Containers/BaseContainer.cs @@ -1,5 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; using Robust.Shared.GameObjects; using Robust.Shared.IoC; +using Robust.Shared.Log; using Robust.Shared.Map; using Robust.Shared.Maths; using Robust.Shared.Network; @@ -9,12 +14,6 @@ using Robust.Shared.Physics.Systems; using Robust.Shared.Serialization.Manager.Attributes; using Robust.Shared.Utility; using Robust.Shared.ViewVariables; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Numerics; -using Robust.Shared.Log; -using Robust.Shared.Serialization; namespace Robust.Shared.Containers { @@ -62,7 +61,7 @@ namespace Robust.Shared.Containers /// The entity that owns this container. /// [ViewVariables] - public EntityUid Owner => Manager.Owner; + public EntityUid Owner { get; internal set; } /// /// Should the contents of this container be shown? False for closed containers like lockers, true for @@ -77,10 +76,7 @@ namespace Robust.Shared.Containers DebugTools.AssertNull(ID); ID = id; Manager = component; - - // TODO fix container init. - // Eventually, we want an owner field, but currently it needs to use component.Owner - // Owner = owner; + Owner = owner; } /// @@ -107,10 +103,9 @@ namespace Robust.Shared.Containers bool force = false) { IoCManager.Resolve(ref entMan); - DebugTools.Assert(transform == null || transform.Owner == toinsert); - DebugTools.Assert(ownerTransform == null || ownerTransform.Owner == Owner); - DebugTools.Assert(ownerTransform == null || ownerTransform.Owner == Owner); - DebugTools.Assert(physics == null || physics.Owner == toinsert); + DebugTools.AssertOwner(toinsert, transform); + DebugTools.AssertOwner(Owner, ownerTransform); + DebugTools.AssertOwner(toinsert, physics); DebugTools.Assert(!ExpectedEntities.Contains(entMan.GetNetEntity(toinsert))); DebugTools.Assert(Manager.Containers.ContainsKey(ID)); @@ -298,8 +293,8 @@ namespace Robust.Shared.Containers IoCManager.Resolve(ref entMan); DebugTools.AssertNotNull(Manager); DebugTools.Assert(entMan.EntityExists(toRemove)); - DebugTools.Assert(xform == null || xform.Owner == toRemove); - DebugTools.Assert(meta == null || meta.Owner == toRemove); + DebugTools.AssertOwner(toRemove, xform); + DebugTools.AssertOwner(toRemove, meta); xform ??= entMan.GetComponent(toRemove); meta ??= entMan.GetComponent(toRemove); @@ -337,7 +332,7 @@ namespace Robust.Shared.Containers else if (reparent) { // Container ECS when. - sys.AttachParentToContainerOrGrid(xform); + sys.AttachParentToContainerOrGrid((toRemove, xform)); if (localRotation != null) entMan.EntitySysManager.GetEntitySystem().SetLocalRotation(xform, localRotation.Value); } diff --git a/Robust.Shared/Containers/ContainerManagerComponent.cs b/Robust.Shared/Containers/ContainerManagerComponent.cs index 72bfe1a42..739599ef4 100644 --- a/Robust.Shared/Containers/ContainerManagerComponent.cs +++ b/Robust.Shared/Containers/ContainerManagerComponent.cs @@ -7,7 +7,6 @@ using Robust.Shared.GameStates; using Robust.Shared.IoC; using Robust.Shared.Map; using Robust.Shared.Maths; -using Robust.Shared.Network; using Robust.Shared.Serialization; using Robust.Shared.Serialization.Manager.Attributes; @@ -22,7 +21,6 @@ namespace Robust.Shared.Containers { [Dependency] private readonly IDynamicTypeFactoryInternal _dynFactory = default!; [Dependency] private readonly IEntityManager _entMan = default!; - [Dependency] private readonly INetManager _netMan = default!; [DataField("containers")] public Dictionary Containers = new(); @@ -30,41 +28,37 @@ namespace Robust.Shared.Containers void ISerializationHooks.AfterDeserialization() { // TODO custom type serializer - // TODO set owner uid on init. foreach (var (id, container) in Containers) { container.Manager = this; + container.Owner = Owner; container.ID = id; } } - /// - public T MakeContainer(string id) + public T MakeContainer(EntityUid uid, string id) where T : BaseContainer { if (HasContainer(id)) throw new ArgumentException($"Container with specified ID already exists: '{id}'"); var container = _dynFactory.CreateInstanceUnchecked(typeof(T), inject: false); - container.Init(id, Owner, this); + container.Init(id, uid, this); Containers[id] = container; _entMan.Dirty(this); return container; } - /// public BaseContainer GetContainer(string id) { return Containers[id]; } - /// public bool HasContainer(string id) { return Containers.ContainsKey(id); } - /// public bool TryGetContainer(string id, [NotNullWhen(true)] out BaseContainer? container) { var ret = Containers.TryGetValue(id, out var cont); @@ -72,7 +66,6 @@ namespace Robust.Shared.Containers return ret; } - /// public bool TryGetContainer(EntityUid entity, [NotNullWhen(true)] out BaseContainer? container) { foreach (var contain in Containers.Values) @@ -88,7 +81,6 @@ namespace Robust.Shared.Containers return false; } - /// public bool ContainsEntity(EntityUid entity) { foreach (var container in Containers.Values) @@ -99,7 +91,6 @@ namespace Robust.Shared.Containers return false; } - /// public bool Remove(EntityUid toremove, TransformComponent? xform = null, MetaDataComponent? meta = null, @@ -214,7 +205,9 @@ namespace Robust.Shared.Containers object IEnumerator.Current => Current; - public void Dispose() { } + public void Dispose() + { + } } } } diff --git a/Robust.Shared/Containers/SharedContainerSystem.cs b/Robust.Shared/Containers/SharedContainerSystem.cs index aa840ca70..4e4967f9b 100644 --- a/Robust.Shared/Containers/SharedContainerSystem.cs +++ b/Robust.Shared/Containers/SharedContainerSystem.cs @@ -83,16 +83,16 @@ namespace Robust.Shared.Containers where T : BaseContainer { if (!Resolve(uid, ref containerManager, false)) - containerManager = EntityManager.AddComponent(uid); // Happy Vera. + containerManager = AddComp(uid); // Happy Vera. - return containerManager.MakeContainer(id); + return containerManager.MakeContainer(uid, id); } public T EnsureContainer(EntityUid uid, string id, out bool alreadyExisted, ContainerManagerComponent? containerManager = null) where T : BaseContainer { if (!Resolve(uid, ref containerManager, false)) - containerManager = EntityManager.AddComponent(uid); + containerManager = AddComp(uid); if (TryGetContainer(uid, id, out var container, containerManager)) { @@ -141,7 +141,7 @@ namespace Robust.Shared.Containers public bool TryGetContainingContainer(EntityUid uid, EntityUid containedUid, [NotNullWhen(true)] out BaseContainer? container, ContainerManagerComponent? containerManager = null, bool skipExistCheck = false) { - if (Resolve(uid, ref containerManager, false) && (skipExistCheck || EntityManager.EntityExists(containedUid))) + if (Resolve(uid, ref containerManager, false) && (skipExistCheck || Exists(containedUid))) return containerManager.TryGetContainer(containedUid, out container); container = null; @@ -150,7 +150,7 @@ namespace Robust.Shared.Containers public bool ContainsEntity(EntityUid uid, EntityUid containedUid, ContainerManagerComponent? containerManager = null) { - if (!Resolve(uid, ref containerManager, false) || !EntityManager.EntityExists(containedUid)) + if (!Resolve(uid, ref containerManager, false) || !Exists(containedUid)) return false; return containerManager.ContainsEntity(containedUid); @@ -228,7 +228,7 @@ namespace Robust.Shared.Containers { if (meta == null) { - metas ??= EntityManager.GetEntityQuery(); + metas ??= GetEntityQuery(); meta = metas.Value.GetComponent(uid); } @@ -237,7 +237,7 @@ namespace Robust.Shared.Containers if (xform == null) { - xforms ??= EntityManager.GetEntityQuery(); + xforms ??= GetEntityQuery(); xform = xforms.Value.GetComponent(uid); } @@ -297,7 +297,7 @@ namespace Robust.Shared.Containers if (!xform.ParentUid.Valid) return foundComponents.Any(); - if (EntityManager.TryGetComponent(xform.ParentUid, out T? foundComponent)) + if (TryComp(xform.ParentUid, out T? foundComponent)) foundComponents.Add(foundComponent); return TryFindComponentsOnEntityContainerOrParent(xform.ParentUid, entityQuery, foundComponents); @@ -394,7 +394,7 @@ namespace Robust.Shared.Containers /// public bool TryGetOuterContainer(EntityUid uid, TransformComponent xform, [NotNullWhen(true)] out BaseContainer? container) { - var xformQuery = EntityManager.GetEntityQuery(); + var xformQuery = GetEntityQuery(); return TryGetOuterContainer(uid, xform, out container, xformQuery); } @@ -406,8 +406,8 @@ namespace Robust.Shared.Containers if (!uid.IsValid()) return false; - var conQuery = EntityManager.GetEntityQuery(); - var metaQuery = EntityManager.GetEntityQuery(); + var conQuery = GetEntityQuery(); + var metaQuery = GetEntityQuery(); var child = uid; var parent = xform.ParentUid; @@ -501,20 +501,26 @@ namespace Robust.Shared.Containers } } + [Obsolete("Use AttachParentToContainerOrGrid(EntityUid) instead")] public void AttachParentToContainerOrGrid(TransformComponent transform) + { + AttachParentToContainerOrGrid(new Entity(transform.Owner, transform)); + } + + public void AttachParentToContainerOrGrid(Entity transform) { // TODO make this check upwards for any container, and parent to that. // Currently this just checks the direct parent, so entities will still teleport through containers. - if (!transform.ParentUid.IsValid() - || !TryGetContainingContainer(transform.ParentUid, out var container) + if (!transform.Comp.ParentUid.IsValid() + || !TryGetContainingContainer(transform.Comp.ParentUid, out var container) || !TryInsertIntoContainer(transform, container)) - transform.AttachToGridOrMap(); + transform.Comp.AttachToGridOrMap(); } - private bool TryInsertIntoContainer(TransformComponent transform, BaseContainer container) + private bool TryInsertIntoContainer(Entity transform, BaseContainer container) { - if (container.Insert(transform.Owner)) return true; + if (container.Insert(transform)) return true; if (Transform(container.Owner).ParentUid.IsValid() && TryGetContainingContainer(container.Owner, out var newContainer)) diff --git a/Robust.Shared/GameObjects/Components/Appearance/AppearanceComponent.cs b/Robust.Shared/GameObjects/Components/Appearance/AppearanceComponent.cs index 15d6bd0f0..8816d0552 100644 --- a/Robust.Shared/GameObjects/Components/Appearance/AppearanceComponent.cs +++ b/Robust.Shared/GameObjects/Components/Appearance/AppearanceComponent.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Robust.Shared.GameStates; -using Robust.Shared.IoC; using Robust.Shared.ViewVariables; namespace Robust.Shared.GameObjects; @@ -33,14 +32,6 @@ public sealed partial class AppearanceComponent : Component [ViewVariables] internal Dictionary AppearanceData = new(); - [Dependency] private readonly IEntitySystemManager _sysMan = default!; - - [Obsolete("Use SharedAppearanceSystem instead")] - public void SetData(Enum key, object value) - { - _sysMan.GetEntitySystem().SetData(Owner, key, value, this); - } - [Obsolete("Use SharedAppearanceSystem instead")] public bool TryGetData(Enum key, [NotNullWhen(true)] out T data) { diff --git a/Robust.Shared/GameObjects/Entity.cs b/Robust.Shared/GameObjects/Entity.cs new file mode 100644 index 000000000..5115d0a86 --- /dev/null +++ b/Robust.Shared/GameObjects/Entity.cs @@ -0,0 +1,584 @@ +using Robust.Shared.Utility; + +namespace Robust.Shared.GameObjects; + +public record struct Entity + where T : IComponent? +{ + public EntityUid Owner; + public T Comp; + + public Entity(EntityUid owner, T comp) + { + DebugTools.AssertOwner(owner, comp); + + Owner = owner; + Comp = comp; + } + + public static implicit operator Entity((EntityUid Owner, T Comp) tuple) + { + return new Entity(tuple.Owner, tuple.Comp); + } + + public static implicit operator Entity(EntityUid owner) + { + return new Entity(owner, default); + } + + public static implicit operator EntityUid(Entity ent) + { + return ent.Owner; + } + + public static implicit operator T(Entity ent) + { + return ent.Comp; + } + + public readonly void Deconstruct(out EntityUid owner, out T comp) + { + owner = Owner; + comp = Comp; + } +} + +public record struct Entity + where T1 : IComponent? where T2 : IComponent? +{ + public EntityUid Owner; + public T1 Comp1; + public T2 Comp2; + + public Entity(EntityUid owner, T1 comp1, T2 comp2) + { + DebugTools.AssertOwner(owner, comp1); + DebugTools.AssertOwner(owner, comp2); + + Owner = owner; + Comp1 = comp1; + Comp2 = comp2; + } + + public static implicit operator Entity((EntityUid Owner, T1 Comp1, T2 Comp2) tuple) + { + return new Entity(tuple.Owner, tuple.Comp1, tuple.Comp2); + } + + public static implicit operator Entity(EntityUid owner) + { + return new Entity(owner, default, default); + } + + public static implicit operator EntityUid(Entity ent) + { + return ent.Owner; + } + + public static implicit operator T1(Entity ent) + { + return ent.Comp1; + } + + public static implicit operator T2(Entity ent) + { + return ent.Comp2; + } + + public readonly void Deconstruct(out EntityUid owner, out T1 comp1, out T2 comp2) + { + owner = Owner; + comp1 = Comp1; + comp2 = Comp2; + } +} + +public record struct Entity + where T1 : IComponent? where T2 : IComponent? where T3 : IComponent? +{ + public EntityUid Owner; + public T1 Comp1; + public T2 Comp2; + public T3 Comp3; + + public Entity(EntityUid owner, T1 comp1, T2 comp2, T3 comp3) + { + DebugTools.AssertOwner(owner, comp1); + DebugTools.AssertOwner(owner, comp2); + DebugTools.AssertOwner(owner, comp3); + + Owner = owner; + Comp1 = comp1; + Comp2 = comp2; + Comp3 = comp3; + } + + public static implicit operator Entity((EntityUid Owner, T1 Comp1, T2 Comp2, T3 Comp3) tuple) + { + return new Entity(tuple.Owner, tuple.Comp1, tuple.Comp2, tuple.Comp3); + } + + public static implicit operator Entity(EntityUid owner) + { + return new Entity(owner, default, default, default); + } + + public static implicit operator EntityUid(Entity ent) + { + return ent.Owner; + } + + public static implicit operator T1(Entity ent) + { + return ent.Comp1; + } + + public static implicit operator T2(Entity ent) + { + return ent.Comp2; + } + + public static implicit operator T3(Entity ent) + { + return ent.Comp3; + } + + public readonly void Deconstruct(out EntityUid owner, out T1 comp1, out T2 comp2, out T3 comp3) + { + owner = Owner; + comp1 = Comp1; + comp2 = Comp2; + comp3 = Comp3; + } +} + +public record struct Entity + where T1 : IComponent? where T2 : IComponent? where T3 : IComponent? where T4 : IComponent? +{ + public EntityUid Owner; + public T1 Comp1; + public T2 Comp2; + public T3 Comp3; + public T4 Comp4; + + public Entity(EntityUid owner, T1 comp1, T2 comp2, T3 comp3, T4 comp4) + { + DebugTools.AssertOwner(owner, comp1); + DebugTools.AssertOwner(owner, comp2); + DebugTools.AssertOwner(owner, comp3); + DebugTools.AssertOwner(owner, comp4); + + Owner = owner; + Comp1 = comp1; + Comp2 = comp2; + Comp3 = comp3; + Comp4 = comp4; + } + + public static implicit operator Entity((EntityUid Owner, T1 Comp1, T2 Comp2, T3 Comp3, T4 Comp4) tuple) + { + return new Entity(tuple.Owner, tuple.Comp1, tuple.Comp2, tuple.Comp3, tuple.Comp4); + } + + public static implicit operator Entity(EntityUid owner) + { + return new Entity(owner, default, default, default, default); + } + + public static implicit operator EntityUid(Entity ent) + { + return ent.Owner; + } + + public static implicit operator T1(Entity ent) + { + return ent.Comp1; + } + + public static implicit operator T2(Entity ent) + { + return ent.Comp2; + } + + public static implicit operator T3(Entity ent) + { + return ent.Comp3; + } + + public static implicit operator T4(Entity ent) + { + return ent.Comp4; + } + + public readonly void Deconstruct(out EntityUid owner, out T1 comp1, out T2 comp2, out T3 comp3, out T4 comp4) + { + owner = Owner; + comp1 = Comp1; + comp2 = Comp2; + comp3 = Comp3; + comp4 = Comp4; + } +} + +public record struct Entity + where T1 : IComponent? where T2 : IComponent? where T3 : IComponent? where T4 : IComponent? where T5 : IComponent? +{ + public EntityUid Owner; + public T1 Comp1; + public T2 Comp2; + public T3 Comp3; + public T4 Comp4; + public T5 Comp5; + + public Entity(EntityUid owner, T1 comp1, T2 comp2, T3 comp3, T4 comp4, T5 comp5) + { + DebugTools.AssertOwner(owner, comp1); + DebugTools.AssertOwner(owner, comp2); + DebugTools.AssertOwner(owner, comp3); + DebugTools.AssertOwner(owner, comp4); + DebugTools.AssertOwner(owner, comp5); + + Owner = owner; + Comp1 = comp1; + Comp2 = comp2; + Comp3 = comp3; + Comp4 = comp4; + Comp5 = comp5; + } + + public static implicit operator Entity((EntityUid Owner, T1 Comp1, T2 Comp2, T3 Comp3, T4 Comp4, T5 Comp5) tuple) + { + return new Entity(tuple.Owner, tuple.Comp1, tuple.Comp2, tuple.Comp3, tuple.Comp4, tuple.Comp5); + } + + public static implicit operator Entity(EntityUid owner) + { + return new Entity(owner, default, default, default, default, default); + } + + public static implicit operator EntityUid(Entity ent) + { + return ent.Owner; + } + + public static implicit operator T1(Entity ent) + { + return ent.Comp1; + } + + public static implicit operator T2(Entity ent) + { + return ent.Comp2; + } + + public static implicit operator T3(Entity ent) + { + return ent.Comp3; + } + + public static implicit operator T4(Entity ent) + { + return ent.Comp4; + } + + public static implicit operator T5(Entity ent) + { + return ent.Comp5; + } + + public readonly void Deconstruct(out EntityUid owner, out T1 comp1, out T2 comp2, out T3 comp3, out T4 comp4, out T5 comp5) + { + owner = Owner; + comp1 = Comp1; + comp2 = Comp2; + comp3 = Comp3; + comp4 = Comp4; + comp5 = Comp5; + } +} + +public record struct Entity + where T1 : IComponent? where T2 : IComponent? where T3 : IComponent? where T4 : IComponent? where T5 : IComponent? where T6 : IComponent? +{ + public EntityUid Owner; + public T1 Comp1; + public T2 Comp2; + public T3 Comp3; + public T4 Comp4; + public T5 Comp5; + public T6 Comp6; + + public Entity(EntityUid owner, T1 comp1, T2 comp2, T3 comp3, T4 comp4, T5 comp5, T6 comp6) + { + DebugTools.AssertOwner(owner, comp1); + DebugTools.AssertOwner(owner, comp2); + DebugTools.AssertOwner(owner, comp3); + DebugTools.AssertOwner(owner, comp4); + DebugTools.AssertOwner(owner, comp5); + DebugTools.AssertOwner(owner, comp6); + + Owner = owner; + Comp1 = comp1; + Comp2 = comp2; + Comp3 = comp3; + Comp4 = comp4; + Comp5 = comp5; + Comp6 = comp6; + } + + public static implicit operator Entity((EntityUid Owner, T1 Comp1, T2 Comp2, T3 Comp3, T4 Comp4, T5 Comp5, T6 Comp6) tuple) + { + return new Entity(tuple.Owner, tuple.Comp1, tuple.Comp2, tuple.Comp3, tuple.Comp4, tuple.Comp5, tuple.Comp6); + } + + public static implicit operator Entity(EntityUid owner) + { + return new Entity(owner, default, default, default, default, default, default); + } + + public static implicit operator EntityUid(Entity ent) + { + return ent.Owner; + } + + public static implicit operator T1(Entity ent) + { + return ent.Comp1; + } + + public static implicit operator T2(Entity ent) + { + return ent.Comp2; + } + + public static implicit operator T3(Entity ent) + { + return ent.Comp3; + } + + public static implicit operator T4(Entity ent) + { + return ent.Comp4; + } + + public static implicit operator T5(Entity ent) + { + return ent.Comp5; + } + + public static implicit operator T6(Entity ent) + { + return ent.Comp6; + } + + public readonly void Deconstruct(out EntityUid owner, out T1 comp1, out T2 comp2, out T3 comp3, out T4 comp4, out T5 comp5, out T6 comp6) + { + owner = Owner; + comp1 = Comp1; + comp2 = Comp2; + comp3 = Comp3; + comp4 = Comp4; + comp5 = Comp5; + comp6 = Comp6; + } +} + +public record struct Entity + where T1 : IComponent? where T2 : IComponent? where T3 : IComponent? where T4 : IComponent? where T5 : IComponent? where T6 : IComponent? where T7 : IComponent? +{ + public EntityUid Owner; + public T1 Comp1; + public T2 Comp2; + public T3 Comp3; + public T4 Comp4; + public T5 Comp5; + public T6 Comp6; + public T7 Comp7; + + public Entity(EntityUid owner, T1 comp1, T2 comp2, T3 comp3, T4 comp4, T5 comp5, T6 comp6, T7 comp7) + { + DebugTools.AssertOwner(owner, comp1); + DebugTools.AssertOwner(owner, comp2); + DebugTools.AssertOwner(owner, comp3); + DebugTools.AssertOwner(owner, comp4); + DebugTools.AssertOwner(owner, comp5); + DebugTools.AssertOwner(owner, comp6); + DebugTools.AssertOwner(owner, comp7); + + Owner = owner; + Comp1 = comp1; + Comp2 = comp2; + Comp3 = comp3; + Comp4 = comp4; + Comp5 = comp5; + Comp6 = comp6; + Comp7 = comp7; + } + + public static implicit operator Entity((EntityUid Owner, T1 Comp1, T2 Comp2, T3 Comp3, T4 Comp4, T5 Comp5, T6 Comp6, T7 Comp7) tuple) + { + return new Entity(tuple.Owner, tuple.Comp1, tuple.Comp2, tuple.Comp3, tuple.Comp4, tuple.Comp5, tuple.Comp6, tuple.Comp7); + } + + public static implicit operator Entity(EntityUid owner) + { + return new Entity(owner, default, default, default, default, default, default, default); + } + + public static implicit operator EntityUid(Entity ent) + { + return ent.Owner; + } + + public static implicit operator T1(Entity ent) + { + return ent.Comp1; + } + + public static implicit operator T2(Entity ent) + { + return ent.Comp2; + } + + public static implicit operator T3(Entity ent) + { + return ent.Comp3; + } + + public static implicit operator T4(Entity ent) + { + return ent.Comp4; + } + + public static implicit operator T5(Entity ent) + { + return ent.Comp5; + } + + public static implicit operator T6(Entity ent) + { + return ent.Comp6; + } + + public static implicit operator T7(Entity ent) + { + return ent.Comp7; + } + + public readonly void Deconstruct(out EntityUid owner, out T1 comp1, out T2 comp2, out T3 comp3, out T4 comp4, out T5 comp5, out T6 comp6, out T7 comp7) + { + owner = Owner; + comp1 = Comp1; + comp2 = Comp2; + comp3 = Comp3; + comp4 = Comp4; + comp5 = Comp5; + comp6 = Comp6; + comp7 = Comp7; + } +} + +public record struct Entity + where T1 : IComponent? where T2 : IComponent? where T3 : IComponent? where T4 : IComponent? where T5 : IComponent? where T6 : IComponent? where T7 : IComponent? where T8 : IComponent? +{ + public EntityUid Owner; + public T1 Comp1; + public T2 Comp2; + public T3 Comp3; + public T4 Comp4; + public T5 Comp5; + public T6 Comp6; + public T7 Comp7; + public T8 Comp8; + + public Entity(EntityUid owner, T1 comp1, T2 comp2, T3 comp3, T4 comp4, T5 comp5, T6 comp6, T7 comp7, T8 comp8) + { + DebugTools.AssertOwner(owner, comp1); + DebugTools.AssertOwner(owner, comp2); + DebugTools.AssertOwner(owner, comp3); + DebugTools.AssertOwner(owner, comp4); + DebugTools.AssertOwner(owner, comp5); + DebugTools.AssertOwner(owner, comp6); + DebugTools.AssertOwner(owner, comp7); + DebugTools.AssertOwner(owner, comp8); + + Owner = owner; + Comp1 = comp1; + Comp2 = comp2; + Comp3 = comp3; + Comp4 = comp4; + Comp5 = comp5; + Comp6 = comp6; + Comp7 = comp7; + Comp8 = comp8; + } + + public static implicit operator Entity((EntityUid Owner, T1 Comp1, T2 Comp2, T3 Comp3, T4 Comp4, T5 Comp5, T6 Comp6, T7 Comp7, T8 Comp8) tuple) + { + return new Entity(tuple.Owner, tuple.Comp1, tuple.Comp2, tuple.Comp3, tuple.Comp4, tuple.Comp5, tuple.Comp6, tuple.Comp7, tuple.Comp8); + } + + public static implicit operator Entity(EntityUid owner) + { + return new Entity(owner, default, default, default, default, default, default, default, default); + } + + public static implicit operator EntityUid(Entity ent) + { + return ent.Owner; + } + + public static implicit operator T1(Entity ent) + { + return ent.Comp1; + } + + public static implicit operator T2(Entity ent) + { + return ent.Comp2; + } + + public static implicit operator T3(Entity ent) + { + return ent.Comp3; + } + + public static implicit operator T4(Entity ent) + { + return ent.Comp4; + } + + public static implicit operator T5(Entity ent) + { + return ent.Comp5; + } + + public static implicit operator T6(Entity ent) + { + return ent.Comp6; + } + + public static implicit operator T7(Entity ent) + { + return ent.Comp7; + } + + public static implicit operator T8(Entity ent) + { + return ent.Comp8; + } + + public readonly void Deconstruct(out EntityUid owner, out T1 comp1, out T2 comp2, out T3 comp3, out T4 comp4, out T5 comp5, out T6 comp6, out T7 comp7, out T8 comp8) + { + owner = Owner; + comp1 = Comp1; + comp2 = Comp2; + comp3 = Comp3; + comp4 = Comp4; + comp5 = Comp5; + comp6 = Comp6; + comp7 = Comp7; + comp8 = Comp8; + } +} + diff --git a/Robust.Shared/GameObjects/EntityEventBus.Directed.cs b/Robust.Shared/GameObjects/EntityEventBus.Directed.cs index b32209840..bf495ad42 100644 --- a/Robust.Shared/GameObjects/EntityEventBus.Directed.cs +++ b/Robust.Shared/GameObjects/EntityEventBus.Directed.cs @@ -46,6 +46,12 @@ namespace Robust.Shared.GameObjects where TComp : IComponent where TEvent : notnull; + void SubscribeLocalEvent( + EntityEventRefHandler handler, + Type orderType, Type[]? before = null, Type[]? after = null) + where TComp : IComponent + where TEvent : notnull; + #endregion void UnsubscribeLocalEvent() @@ -294,6 +300,25 @@ namespace Robust.Shared.GameObjects RegisterCommon(typeof(TEvent), orderData, out _); } + public void SubscribeLocalEvent(EntityEventRefHandler handler, Type orderType, + Type[]? before = null, + Type[]? after = null) where TComp : IComponent where TEvent : notnull + { + void EventHandler(EntityUid uid, IComponent comp, ref TEvent args) + => handler(new Entity(uid, (TComp) comp), ref args); + + var orderData = new OrderingData(orderType, before ?? Array.Empty(), after ?? Array.Empty()); + + EntSubscribe( + CompIdx.Index(), + typeof(TComp), + typeof(TEvent), + EventHandler, + orderData); + + RegisterCommon(typeof(TEvent), orderData, out _); + } + /// public void UnsubscribeLocalEvent() where TComp : IComponent @@ -718,4 +743,8 @@ namespace Robust.Shared.GameObjects public delegate void ComponentEventRefHandler(EntityUid uid, TComp component, ref TEvent args) where TComp : IComponent where TEvent : notnull; + + public delegate void EntityEventRefHandler(Entity ent, ref TEvent args) + where TComp : IComponent + where TEvent : notnull; } diff --git a/Robust.Shared/GameObjects/EntityManager.Components.cs b/Robust.Shared/GameObjects/EntityManager.Components.cs index 664ad1945..17acd727a 100644 --- a/Robust.Shared/GameObjects/EntityManager.Components.cs +++ b/Robust.Shared/GameObjects/EntityManager.Components.cs @@ -106,9 +106,7 @@ namespace Robust.Shared.GameObjects public void InitializeComponents(EntityUid uid, MetaDataComponent? metadata = null) { -#pragma warning disable CS0618 // Type or member is obsolete - DebugTools.Assert(metadata == null || metadata.Owner == uid); -#pragma warning restore CS0618 // Type or member is obsolete + DebugTools.AssertOwner(uid, metadata); metadata ??= GetComponent(uid); DebugTools.Assert(metadata.EntityLifeStage == EntityLifeStage.PreInit); metadata.EntityLifeStage = EntityLifeStage.Initializing; @@ -175,9 +173,6 @@ namespace Robust.Shared.GameObjects public IComponent AddComponent(EntityUid uid, ushort netId, MetaDataComponent? meta = null) { var newComponent = _componentFactory.GetComponent(netId); -#pragma warning disable CS0618 // Type or member is obsolete - newComponent.Owner = uid; -#pragma warning restore CS0618 // Type or member is obsolete AddComponent(uid, newComponent, metadata: meta); return newComponent; } @@ -185,9 +180,6 @@ namespace Robust.Shared.GameObjects public T AddComponent(EntityUid uid) where T : IComponent, new() { var newComponent = _componentFactory.GetComponent(); -#pragma warning disable CS0618 // Type or member is obsolete - newComponent.Owner = uid; -#pragma warning restore CS0618 // Type or member is obsolete AddComponent(uid, newComponent); return newComponent; } @@ -486,7 +478,9 @@ namespace Robust.Shared.GameObjects { if (component == null) throw new ArgumentNullException(nameof(component)); +#pragma warning disable CS0618 // Type or member is obsolete if (component.Owner != uid) +#pragma warning restore CS0618 // Type or member is obsolete throw new InvalidOperationException("Component is not owned by entity."); if (component.Deleted) return; @@ -1121,9 +1115,9 @@ namespace Robust.Shared.GameObjects } else { - foreach (var t1Comp in comps.Values) + foreach (var (uid, t1Comp) in comps) { - if (t1Comp.Deleted || !MetaQuery.TryGetComponentInternal(t1Comp.Owner, out var metaComp)) continue; + if (t1Comp.Deleted || !MetaQuery.TryGetComponentInternal(uid, out var metaComp)) continue; if (metaComp.EntityPaused) continue; @@ -1164,7 +1158,7 @@ namespace Robust.Shared.GameObjects if (!trait2.TryGetValue(uid, out var t2Comp) || t2Comp.Deleted) continue; - if (t1Comp.Deleted || !metaComps.TryGetValue(t1Comp.Owner, out var metaComp)) continue; + if (t1Comp.Deleted || !metaComps.TryGetValue(uid, out var metaComp)) continue; var meta = (MetaDataComponent)metaComp; @@ -1216,7 +1210,7 @@ namespace Robust.Shared.GameObjects if (!trait3.TryGetValue(uid, out var t3Comp) || t3Comp.Deleted) continue; - if (t1Comp.Deleted || !metaComps.TryGetValue(t1Comp.Owner, out var metaComp)) continue; + if (t1Comp.Deleted || !metaComps.TryGetValue(uid, out var metaComp)) continue; var meta = (MetaDataComponent)metaComp; @@ -1279,7 +1273,7 @@ namespace Robust.Shared.GameObjects if (!trait4.TryGetValue(uid, out var t4Comp) || t4Comp.Deleted) continue; - if (t1Comp.Deleted || !metaComps.TryGetValue(t1Comp.Owner, out var metaComp)) continue; + if (t1Comp.Deleted || !metaComps.TryGetValue(uid, out var metaComp)) continue; var meta = (MetaDataComponent)metaComp; @@ -1449,9 +1443,7 @@ namespace Robust.Shared.GameObjects { if (component != null) { -#pragma warning disable CS0618 // Type or member is obsolete - DebugTools.Assert(uid == component.Owner, "Specified Entity is not the component's Owner!"); -#pragma warning restore CS0618 // Type or member is obsolete + DebugTools.AssertOwner(uid, component); return true; } @@ -1544,9 +1536,7 @@ namespace Robust.Shared.GameObjects { if (component != null) { -#pragma warning disable CS0618 // Type or member is obsolete - DebugTools.Assert(uid == component.Owner, "Specified Entity is not the component's Owner!"); -#pragma warning restore CS0618 // Type or member is obsolete + DebugTools.AssertOwner(uid, component); return true; } diff --git a/Robust.Shared/GameObjects/EntityManager.cs b/Robust.Shared/GameObjects/EntityManager.cs index 9821b5ea1..f0457ed01 100644 --- a/Robust.Shared/GameObjects/EntityManager.cs +++ b/Robust.Shared/GameObjects/EntityManager.cs @@ -395,12 +395,19 @@ namespace Robust.Shared.GameObjects /// public virtual void Dirty(EntityUid uid, IComponent component, MetaDataComponent? meta = null) { - if (component.LifeStage >= ComponentLifeStage.Removing || !component.NetSyncEnabled) + Dirty(new Entity(uid, component), meta); + } + + public virtual void Dirty(Entity ent, MetaDataComponent? meta = null) + { + if (ent.Comp.LifeStage >= ComponentLifeStage.Removing || !ent.Comp.NetSyncEnabled) return; - DebugTools.AssertOwner(uid, component); - DirtyEntity(uid, meta); - component.LastModifiedTick = CurrentTick; + DebugTools.AssertOwner(ent, ent.Comp); + DirtyEntity(ent, meta); +#pragma warning disable CS0618 // Type or member is obsolete + ent.Comp.LastModifiedTick = CurrentTick; +#pragma warning restore CS0618 // Type or member is obsolete } /// @@ -659,7 +666,9 @@ namespace Robust.Shared.GameObjects // allocate the required TransformComponent var xformComp = Unsafe.As(_componentFactory.GetComponent(_xformReg)); +#pragma warning disable CS0618 // Type or member is obsolete xformComp.Owner = uid; +#pragma warning restore CS0618 // Type or member is obsolete AddComponentInternal(uid, xformComp, false, true, metadata); return uid; diff --git a/Robust.Shared/GameObjects/EntitySystem.Proxy.cs b/Robust.Shared/GameObjects/EntitySystem.Proxy.cs index 06a00557b..07bb2c4da 100644 --- a/Robust.Shared/GameObjects/EntitySystem.Proxy.cs +++ b/Robust.Shared/GameObjects/EntitySystem.Proxy.cs @@ -220,6 +220,15 @@ public partial class EntitySystem EntityManager.Dirty(uid, component, meta); } + /// + /// Marks a component as dirty. This also implicitly dirties the entity this component belongs to. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + protected void Dirty(Entity ent, MetaDataComponent? meta = null) where T : IComponent + { + EntityManager.Dirty(ent.Owner, ent.Comp, meta); + } + /// /// Retrieves the name of an entity. /// @@ -396,7 +405,7 @@ public partial class EntitySystem return true; } - /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] [return: NotNullIfNotNull("uid")] protected EntityStringRepresentation? ToPrettyString(EntityUid? uid, MetaDataComponent? metadata = null) @@ -404,7 +413,7 @@ public partial class EntitySystem return EntityManager.ToPrettyString(uid, metadata); } - /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] [return: NotNullIfNotNull("netEntity")] protected EntityStringRepresentation? ToPrettyString(NetEntity? netEntity) @@ -412,17 +421,17 @@ public partial class EntitySystem return EntityManager.ToPrettyString(netEntity); } - /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] protected EntityStringRepresentation ToPrettyString(EntityUid uid, MetaDataComponent? metadata) => EntityManager.ToPrettyString(uid, metadata); - /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] protected EntityStringRepresentation ToPrettyString(EntityUid uid) => EntityManager.ToPrettyString(uid); - /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] protected EntityStringRepresentation ToPrettyString(NetEntity netEntity) => EntityManager.ToPrettyString(netEntity); @@ -458,7 +467,7 @@ public partial class EntitySystem /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - protected bool TryComp(EntityUid uid, [NotNullWhen(true)] out T? comp) + protected bool TryComp(EntityUid uid, [NotNullWhen(true)] out T? comp) where T : IComponent { return EntityManager.TryGetComponent(uid, out comp); } @@ -479,7 +488,7 @@ public partial class EntitySystem /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - protected bool TryComp([NotNullWhen(true)] EntityUid? uid, [NotNullWhen(true)] out T? comp) + protected bool TryComp([NotNullWhen(true)] EntityUid? uid, [NotNullWhen(true)] out T? comp) where T : IComponent { if (!uid.HasValue) { diff --git a/Robust.Shared/GameObjects/EntitySystem.Resolve.cs b/Robust.Shared/GameObjects/EntitySystem.Resolve.cs index d34a2ce6c..c1750ee09 100644 --- a/Robust.Shared/GameObjects/EntitySystem.Resolve.cs +++ b/Robust.Shared/GameObjects/EntitySystem.Resolve.cs @@ -19,7 +19,7 @@ namespace Robust.Shared.GameObjects protected bool Resolve(EntityUid uid, [NotNullWhen(true)] ref TComp? component, bool logMissing = true) where TComp : IComponent { - DebugTools.Assert(component == null || uid == component.Owner, "Specified Entity is not the component's Owner!"); + DebugTools.AssertOwner(uid, component); if (component != null && !component.Deleted) return true; diff --git a/Robust.Shared/GameObjects/EntitySystem.Subscriptions.cs b/Robust.Shared/GameObjects/EntitySystem.Subscriptions.cs index bce2aae34..16e3dbbcf 100644 --- a/Robust.Shared/GameObjects/EntitySystem.Subscriptions.cs +++ b/Robust.Shared/GameObjects/EntitySystem.Subscriptions.cs @@ -138,6 +138,18 @@ namespace Robust.Shared.GameObjects _subscriptions.Add(new SubLocal()); } + protected void SubscribeLocalEvent( + EntityEventRefHandler handler, + Type[]? before = null, Type[]? after = null) + where TComp : IComponent + where TEvent : notnull + { + EntityManager.EventBus.SubscribeLocalEvent(handler, GetType(), before, after); + + _subscriptions ??= new(); + _subscriptions.Add(new SubLocal()); + } + private void ShutdownSubscriptions() { if (_subscriptions == null) diff --git a/Robust.Shared/GameObjects/EntityUid.cs b/Robust.Shared/GameObjects/EntityUid.cs index 3695258c6..3cce6c28a 100644 --- a/Robust.Shared/GameObjects/EntityUid.cs +++ b/Robust.Shared/GameObjects/EntityUid.cs @@ -4,7 +4,6 @@ using Robust.Shared.IoC; using Robust.Shared.Prototypes; using Robust.Shared.Serialization.Manager.Attributes; using Robust.Shared.Timing; -using Robust.Shared.Utility; using Robust.Shared.ViewVariables; namespace Robust.Shared.GameObjects @@ -153,7 +152,7 @@ namespace Robust.Shared.GameObjects set { if (MetaData is {} metaData) - metaData.EntityName = value; + IoCManager.Resolve().System().SetEntityName(this, value, metaData); } } @@ -163,8 +162,11 @@ namespace Robust.Shared.GameObjects get => MetaData?.EntityDescription ?? string.Empty; set { - if (MetaData is {} metaData) - metaData.EntityDescription = value; + if (MetaData is { } metaData) + { + var entManager = IoCManager.Resolve(); + entManager.System().SetEntityDescription(this, value, metaData); + } } } diff --git a/Robust.Shared/GameObjects/IEntityManager.cs b/Robust.Shared/GameObjects/IEntityManager.cs index bbd6a524b..1a659dd5c 100644 --- a/Robust.Shared/GameObjects/IEntityManager.cs +++ b/Robust.Shared/GameObjects/IEntityManager.cs @@ -87,6 +87,8 @@ namespace Robust.Shared.GameObjects public void Dirty(EntityUid uid, IComponent component, MetaDataComponent? meta = null); + public void Dirty(Entity ent, MetaDataComponent? meta = null); + public void QueueDeleteEntity(EntityUid? uid); public bool IsQueuedForDeletion(EntityUid uid); diff --git a/Robust.Shared/GameObjects/NetEntity.cs b/Robust.Shared/GameObjects/NetEntity.cs index 5bc034d00..cc0be0370 100644 --- a/Robust.Shared/GameObjects/NetEntity.cs +++ b/Robust.Shared/GameObjects/NetEntity.cs @@ -184,8 +184,11 @@ public readonly struct NetEntity : IEquatable, IComparable get => MetaData?.EntityName ?? string.Empty; set { - if (MetaData is {} metaData) - metaData.EntityName = value; + if (MetaData is { } metaData) + { + var entManager = IoCManager.Resolve(); + entManager.System().SetEntityName(entManager.GetEntity(this), value, metaData); + } } } @@ -195,8 +198,11 @@ public readonly struct NetEntity : IEquatable, IComparable get => MetaData?.EntityDescription ?? string.Empty; set { - if (MetaData is {} metaData) - metaData.EntityDescription = value; + if (MetaData is { } metaData) + { + var entManager = IoCManager.Resolve(); + entManager.System().SetEntityDescription(entManager.GetEntity(this), value, metaData); + } } } diff --git a/Robust.Shared/GameObjects/Systems/EntityLookup.Queries.cs b/Robust.Shared/GameObjects/Systems/EntityLookup.Queries.cs index 2207b3857..c75b3bd02 100644 --- a/Robust.Shared/GameObjects/Systems/EntityLookup.Queries.cs +++ b/Robust.Shared/GameObjects/Systems/EntityLookup.Queries.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Numerics; using System.Runtime.CompilerServices; using Robust.Shared.Collections; -using Robust.Shared.Containers; using Robust.Shared.Map; using Robust.Shared.Map.Components; using Robust.Shared.Maths; @@ -273,9 +272,14 @@ public sealed partial class EntityLookupSystem public HashSet GetEntitiesIntersecting(MapId mapId, Box2 worldAABB, LookupFlags flags = DefaultFlags) { - if (mapId == MapId.Nullspace) return new HashSet(); - var intersecting = new HashSet(); + GetEntitiesIntersecting(mapId, worldAABB, intersecting, flags); + return intersecting; + } + + public void GetEntitiesIntersecting(MapId mapId, Box2 worldAABB, HashSet intersecting, LookupFlags flags = DefaultFlags) + { + if (mapId == MapId.Nullspace) return; // Get grid entities var state = (this, _map, intersecting, worldAABB, _transform, flags); @@ -309,8 +313,6 @@ public sealed partial class EntityLookupSystem var localAABB = _transform.GetInvWorldMatrix(mapUid).TransformBox(worldAABB); AddEntitiesIntersecting(mapUid, intersecting, localAABB, flags); AddContained(intersecting, flags); - - return intersecting; } #endregion @@ -546,16 +548,23 @@ public sealed partial class EntityLookupSystem public HashSet GetEntitiesInRange(MapId mapId, Vector2 worldPos, float range, LookupFlags flags = DefaultFlags) + { + var entities = new HashSet(); + GetEntitiesInRange(mapId, worldPos, range, entities, flags); + return entities; + } + + public void GetEntitiesInRange(MapId mapId, Vector2 worldPos, float range, HashSet entities, LookupFlags flags = DefaultFlags) { DebugTools.Assert(range > 0, "Range must be a positive float"); if (mapId == MapId.Nullspace) - return new HashSet(); + return; // TODO: Actual circles var rangeVec = new Vector2(range, range); var worldAABB = new Box2(worldPos - rangeVec, worldPos + rangeVec); - return GetEntitiesIntersecting(mapId, worldAABB, flags); + GetEntitiesIntersecting(mapId, worldAABB, entities, flags); } #endregion diff --git a/Robust.Shared/GameObjects/Systems/EntityLookupSystem.ComponentQueries.cs b/Robust.Shared/GameObjects/Systems/EntityLookupSystem.ComponentQueries.cs index e5c5ec822..83d0aaaaa 100644 --- a/Robust.Shared/GameObjects/Systems/EntityLookupSystem.ComponentQueries.cs +++ b/Robust.Shared/GameObjects/Systems/EntityLookupSystem.ComponentQueries.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Numerics; using Robust.Shared.Collections; using Robust.Shared.Map; @@ -20,6 +21,18 @@ public sealed partial class EntityLookupSystem Box2 worldAABB, LookupFlags flags, EntityQuery query) where T : IComponent + { + var intersectingEntities = new HashSet>(); + AddEntitiesIntersecting(lookupUid, intersectingEntities, worldAABB, flags, query); + intersecting.UnionWith(intersectingEntities.Select(e => e.Comp)); + } + + private void AddEntitiesIntersecting( + EntityUid lookupUid, + HashSet> intersecting, + Box2 worldAABB, + LookupFlags flags, + EntityQuery query) where T : IComponent { var lookup = _broadQuery.GetComponent(lookupUid); var invMatrix = _transform.GetInvWorldMatrix(lookupUid); @@ -28,48 +41,48 @@ public sealed partial class EntityLookupSystem if ((flags & LookupFlags.Dynamic) != 0x0) { - lookup.DynamicTree.QueryAabb(ref state, static (ref (HashSet intersecting, EntityQuery query) tuple, in FixtureProxy value) => + lookup.DynamicTree.QueryAabb(ref state, static (ref (HashSet> intersecting, EntityQuery query) tuple, in FixtureProxy value) => { if (!tuple.query.TryGetComponent(value.Entity, out var comp)) return true; - tuple.intersecting.Add(comp); + tuple.intersecting.Add((value.Entity, comp)); return true; }, localAABB, (flags & LookupFlags.Approximate) != 0x0); } if ((flags & (LookupFlags.Static)) != 0x0) { - lookup.StaticTree.QueryAabb(ref state, static (ref (HashSet intersecting, EntityQuery query) tuple, in FixtureProxy value) => + lookup.StaticTree.QueryAabb(ref state, static (ref (HashSet> intersecting, EntityQuery query) tuple, in FixtureProxy value) => { if (!tuple.query.TryGetComponent(value.Entity, out var comp)) return true; - tuple.intersecting.Add(comp); + tuple.intersecting.Add((value.Entity, comp)); return true; }, localAABB, (flags & LookupFlags.Approximate) != 0x0); } if ((flags & LookupFlags.StaticSundries) == LookupFlags.StaticSundries) { - lookup.StaticSundriesTree.QueryAabb(ref state, static (ref (HashSet intersecting, EntityQuery query) tuple, in EntityUid value) => + lookup.StaticSundriesTree.QueryAabb(ref state, static (ref (HashSet> intersecting, EntityQuery query) tuple, in EntityUid value) => { if (!tuple.query.TryGetComponent(value, out var comp)) return true; - tuple.intersecting.Add(comp); + tuple.intersecting.Add((value, comp)); return true; }, localAABB, (flags & LookupFlags.Approximate) != 0x0); } if ((flags & LookupFlags.Sundries) != 0x0) { - lookup.SundriesTree.QueryAabb(ref state, static (ref (HashSet intersecting, EntityQuery query) tuple, in EntityUid value) => + lookup.SundriesTree.QueryAabb(ref state, static (ref (HashSet> intersecting, EntityQuery query) tuple, in EntityUid value) => { if (!tuple.query.TryGetComponent(value, out var comp)) return true; - tuple.intersecting.Add(comp); + tuple.intersecting.Add((value, comp)); return true; }, localAABB, (flags & LookupFlags.Approximate) != 0x0); } @@ -166,15 +179,38 @@ public sealed partial class EntityLookupSystem } } + private void RecursiveAdd(EntityUid uid, ref ValueList> toAdd, EntityQuery query) where T : IComponent + { + var childEnumerator = _xformQuery.GetComponent(uid).ChildEnumerator; + + while (childEnumerator.MoveNext(out var child)) + { + if (query.TryGetComponent(child.Value, out var compies)) + { + toAdd.Add((child.Value, compies)); + } + + RecursiveAdd(child.Value, ref toAdd, query); + } + } + + [Obsolete] private void AddContained(HashSet intersecting, LookupFlags flags, EntityQuery query) where T : IComponent + { + var intersectingEntities = new HashSet>(); + AddContained(intersectingEntities, flags, query); + intersecting.UnionWith(intersectingEntities.Select(e => e.Comp)); + } + + private void AddContained(HashSet> intersecting, LookupFlags flags, EntityQuery query) where T : IComponent { if ((flags & LookupFlags.Contained) == 0x0) return; - var toAdd = new ValueList(); + var toAdd = new ValueList>(); foreach (var comp in intersecting) { - if (!_containerQuery.TryGetComponent(comp.Owner, out var conManager)) continue; + if (!_containerQuery.TryGetComponent(comp, out var conManager)) continue; foreach (var con in conManager.GetAllContainers()) { @@ -182,7 +218,7 @@ public sealed partial class EntityLookupSystem { if (query.TryGetComponent(contained, out var compies)) { - toAdd.Add(compies); + toAdd.Add((contained, compies)); } RecursiveAdd(contained, ref toAdd, query); @@ -272,13 +308,20 @@ public sealed partial class EntityLookupSystem return false; } + [Obsolete] public HashSet GetComponentsIntersecting(Type type, MapId mapId, Box2 worldAABB, LookupFlags flags = DefaultFlags) + { + var intersectingEntities = new HashSet>(); + GetEntitiesIntersecting(type, mapId, worldAABB, intersectingEntities, flags); + var intersecting = new HashSet(intersectingEntities.Select(e => e.Comp)); + return intersecting; + } + + public void GetEntitiesIntersecting(Type type, MapId mapId, Box2 worldAABB, HashSet> intersecting, LookupFlags flags = DefaultFlags) { DebugTools.Assert(typeof(IComponent).IsAssignableFrom(type)); if (mapId == MapId.Nullspace) - return new HashSet(); - - var intersecting = new HashSet(); + return; if (!UseBoundsQuery(type, worldAABB.Height * worldAABB.Width)) { @@ -294,7 +337,7 @@ public sealed partial class EntityLookupSystem continue; } - intersecting.Add(comp); + intersecting.Add((uid, comp)); } } else @@ -310,35 +353,39 @@ public sealed partial class EntityLookupSystem Box2 worldAABB, LookupFlags flags, EntityQuery query, - HashSet intersecting) tuple) => + HashSet> intersecting) tuple) => { - tuple.system.AddComponentsIntersecting(uid, tuple.intersecting, tuple.worldAABB, tuple.flags, tuple.query); + tuple.system.AddEntitiesIntersecting(uid, tuple.intersecting, tuple.worldAABB, tuple.flags, tuple.query); return true; }, (flags & LookupFlags.Approximate) != 0x0); // Get map entities var mapUid = _mapManager.GetMapEntityId(mapId); - AddComponentsIntersecting(mapUid, intersecting, worldAABB, flags, query); + AddEntitiesIntersecting(mapUid, intersecting, worldAABB, flags, query); AddContained(intersecting, flags, query); } - - return intersecting; } + [Obsolete] public HashSet GetComponentsIntersecting(MapId mapId, Box2 worldAABB, LookupFlags flags = DefaultFlags) where T : IComponent { - if (mapId == MapId.Nullspace) return new HashSet(); + var intersectingEntities = new HashSet>(); + GetEntitiesIntersecting(mapId, worldAABB, intersectingEntities, flags); + return new HashSet(intersectingEntities.Select(e => e.Comp)); + } - var intersecting = new HashSet(); + public void GetEntitiesIntersecting(MapId mapId, Box2 worldAABB, HashSet> entities, LookupFlags flags = DefaultFlags) where T : IComponent + { + if (mapId == MapId.Nullspace) return; if (!UseBoundsQuery(worldAABB.Height * worldAABB.Width)) { var query = AllEntityQuery(); - while (query.MoveNext(out var comp, out var xform)) + while (query.MoveNext(out var uid, out var comp, out var xform)) { if (xform.MapID != mapId || !worldAABB.Contains(_transform.GetWorldPosition(xform))) continue; - intersecting.Add(comp); + entities.Add((uid, comp)); } } else @@ -346,27 +393,25 @@ public sealed partial class EntityLookupSystem var query = GetEntityQuery(); // Get grid entities - var state = (this, worldAABB, flags, query, intersecting); + var state = (this, worldAABB, flags, query, entities); _mapManager.FindGridsIntersecting(mapId, worldAABB, ref state, static (EntityUid uid, MapGridComponent grid, - ref (EntityLookupSystem system, - Box2 worldAABB, - LookupFlags flags, - EntityQuery query, - HashSet intersecting) tuple) => - { - tuple.system.AddComponentsIntersecting(uid, tuple.intersecting, tuple.worldAABB, tuple.flags, tuple.query); - return true; - }, (flags & LookupFlags.Approximate) != 0x0); + ref (EntityLookupSystem system, + Box2 worldAABB, + LookupFlags flags, + EntityQuery query, + HashSet> intersecting) tuple) => + { + tuple.system.AddEntitiesIntersecting(uid, tuple.intersecting, tuple.worldAABB, tuple.flags, tuple.query); + return true; + }, (flags & LookupFlags.Approximate) != 0x0); // Get map entities var mapUid = _mapManager.GetMapEntityId(mapId); - AddComponentsIntersecting(mapUid, intersecting, worldAABB, flags, query); - AddContained(intersecting, flags, query); + AddEntitiesIntersecting(mapUid, entities, worldAABB, flags, query); + AddContained(entities, flags, query); } - - return intersecting; } #endregion @@ -378,21 +423,56 @@ public sealed partial class EntityLookupSystem var mapPos = coordinates.ToMap(EntityManager, _transform); return GetComponentsInRange(mapPos, range); } + + public void GetEntitiesInRange(EntityCoordinates coordinates, float range, HashSet> entities) where T : IComponent + { + var mapPos = coordinates.ToMap(EntityManager, _transform); + GetEntitiesInRange(mapPos, range, entities); + } + + public HashSet> GetEntitiesInRange(EntityCoordinates coordinates, float range) where T : IComponent + { + var entities = new HashSet>(); + GetEntitiesInRange(coordinates, range, entities); + return entities; + } + #endregion #region MapCoordinates + [Obsolete] public HashSet GetComponentsInRange(Type type, MapCoordinates coordinates, float range) { DebugTools.Assert(typeof(IComponent).IsAssignableFrom(type)); return GetComponentsInRange(type, coordinates.MapId, coordinates.Position, range); } + public HashSet> GetEntitiesInRange(Type type, MapCoordinates coordinates, float range) + { + DebugTools.Assert(typeof(IComponent).IsAssignableFrom(type)); + var entities = new HashSet>(); + GetEntitiesInRange(type, coordinates.MapId, coordinates.Position, range, entities); + return entities; + } + public HashSet GetComponentsInRange(MapCoordinates coordinates, float range) where T : IComponent { return GetComponentsInRange(coordinates.MapId, coordinates.Position, range); } + public void GetEntitiesInRange(MapCoordinates coordinates, float range, HashSet> entities) where T : IComponent + { + GetEntitiesInRange(coordinates.MapId, coordinates.Position, range, entities); + } + + public HashSet> GetEntitiesInRange(MapCoordinates coordinates, float range) where T : IComponent + { + var entities = new HashSet>(); + GetEntitiesInRange(coordinates.MapId, coordinates.Position, range, entities); + return entities; + } + #endregion #region MapId @@ -411,31 +491,47 @@ public sealed partial class EntityLookupSystem return AnyComponentsIntersecting(type, mapId, worldAABB); } + [Obsolete] public HashSet GetComponentsInRange(Type type, MapId mapId, Vector2 worldPos, float range) + { + var entities = new HashSet>(); + GetEntitiesInRange(type, mapId, worldPos, range, entities); + return new HashSet(entities.Select(e => e.Comp)); + } + + public void GetEntitiesInRange(Type type, MapId mapId, Vector2 worldPos, float range, HashSet> entities) { DebugTools.Assert(typeof(IComponent).IsAssignableFrom(type)); DebugTools.Assert(range > 0, "Range must be a positive float"); - if (mapId == MapId.Nullspace) return new HashSet(); + if (mapId == MapId.Nullspace) return; // TODO: Actual circles var rangeVec = new Vector2(range, range); var worldAABB = new Box2(worldPos - rangeVec, worldPos + rangeVec); - return GetComponentsIntersecting(type, mapId, worldAABB); + GetEntitiesIntersecting(type, mapId, worldAABB, entities); } + [Obsolete] public HashSet GetComponentsInRange(MapId mapId, Vector2 worldPos, float range) where T : IComponent + { + var entities = new HashSet>(); + GetEntitiesInRange(mapId, worldPos, range, entities); + return new HashSet(entities.Select(e => e.Comp)); + } + + public void GetEntitiesInRange(MapId mapId, Vector2 worldPos, float range, HashSet> entities) where T : IComponent { DebugTools.Assert(range > 0, "Range must be a positive float"); - if (mapId == MapId.Nullspace) return new HashSet(); + if (mapId == MapId.Nullspace) return; // TODO: Actual circles var rangeVec = new Vector2(range, range); var worldAABB = new Box2(worldPos - rangeVec, worldPos + rangeVec); - return GetComponentsIntersecting(mapId, worldAABB); + GetEntitiesIntersecting(mapId, worldAABB, entities); } #endregion diff --git a/Robust.Shared/GameObjects/Systems/PrototypeReloadSystem.cs b/Robust.Shared/GameObjects/Systems/PrototypeReloadSystem.cs index a91ce3821..06de539e5 100644 --- a/Robust.Shared/GameObjects/Systems/PrototypeReloadSystem.cs +++ b/Robust.Shared/GameObjects/Systems/PrototypeReloadSystem.cs @@ -31,14 +31,15 @@ internal sealed class PrototypeReloadSystem : EntitySystem if (!eventArgs.ByType.TryGetValue(typeof(EntityPrototype), out var set)) return; - foreach (var metadata in EntityQuery()) + var query = EntityQueryEnumerator(); + while (query.MoveNext(out var uid, out var metadata)) { var id = metadata.EntityPrototype?.ID; if (id == null || !set.Modified.ContainsKey(id)) continue; var proto = _prototypes.Index(id); - UpdateEntity(metadata.Owner, metadata, proto); + UpdateEntity(uid, metadata, proto); } } @@ -78,7 +79,6 @@ internal sealed class PrototypeReloadSystem : EntitySystem { var data = newPrototype.Components[name]; var component = _componentFactory.GetComponent(name); - component.Owner = entity; EntityManager.AddComponent(entity, component); } diff --git a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs index 1af7cbf20..4a258c4d3 100644 --- a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs +++ b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs @@ -6,12 +6,12 @@ using System.Numerics; using System.Runtime.CompilerServices; using Robust.Shared.GameStates; using Robust.Shared.Map; -using Robust.Shared.Physics.Components; using Robust.Shared.Map.Components; using Robust.Shared.Map.Enumerators; using Robust.Shared.Map.Events; using Robust.Shared.Maths; using Robust.Shared.Physics; +using Robust.Shared.Physics.Components; using Robust.Shared.Timing; using Robust.Shared.Utility; @@ -296,7 +296,7 @@ public abstract partial class SharedMapSystem MapManager.SuppressOnTileChanged = false; if (modified.Count != 0) - RaiseLocalEvent(uid, new GridModifiedEvent(component, modified), true); + RaiseLocalEvent(uid, new GridModifiedEvent(uid, component, modified), true); } private void OnGridGetState(EntityUid uid, MapGridComponent component, ref ComponentGetState args) diff --git a/Robust.Shared/GameObjects/Systems/SharedMapSystem.cs b/Robust.Shared/GameObjects/Systems/SharedMapSystem.cs index e9e3860ac..5e79bb7c3 100644 --- a/Robust.Shared/GameObjects/Systems/SharedMapSystem.cs +++ b/Robust.Shared/GameObjects/Systems/SharedMapSystem.cs @@ -1,11 +1,9 @@ -using JetBrains.Annotations; -using Robust.Shared.IoC; -using Robust.Shared.Map; -using Robust.Shared.Maths; using System.Collections.Generic; using Robust.Shared.GameStates; +using Robust.Shared.IoC; +using Robust.Shared.Map; using Robust.Shared.Map.Components; -using System.Linq; +using Robust.Shared.Maths; using Robust.Shared.Network; using Robust.Shared.Physics.Systems; using Robust.Shared.Timing; @@ -156,6 +154,11 @@ namespace Robust.Shared.GameObjects /// public sealed class GridModifiedEvent : EntityEventArgs { + /// + /// The id of the grid being changed. + /// + public EntityUid GridEnt { get; } + /// /// Grid being changed. /// @@ -169,8 +172,9 @@ namespace Robust.Shared.GameObjects /// /// Creates a new instance of this class. /// - public GridModifiedEvent(MapGridComponent grid, IReadOnlyCollection<(Vector2i position, Tile tile)> modified) + public GridModifiedEvent(EntityUid gridEnt, MapGridComponent grid, IReadOnlyCollection<(Vector2i position, Tile tile)> modified) { + GridEnt = gridEnt; Grid = grid; Modified = modified; } diff --git a/Robust.Shared/Map/Components/MapGridComponent.cs b/Robust.Shared/Map/Components/MapGridComponent.cs index 4d4427d96..9d0f40a4c 100644 --- a/Robust.Shared/Map/Components/MapGridComponent.cs +++ b/Robust.Shared/Map/Components/MapGridComponent.cs @@ -25,7 +25,7 @@ namespace Robust.Shared.Map.Components // This field is used for deserialization internally in the map loader. // If you want to remove this, you would have to restructure the map save file. - [DataField("index")] internal int GridIndex = 0; + [DataField("index")] internal int GridIndex; // the grid section now writes the grid's EntityUID. as long as existing maps get updated (just a load+save), // this can be removed @@ -75,70 +75,83 @@ namespace Robust.Shared.Map.Components #region TileAccess + [Obsolete("Use the MapSystem method")] public TileRef GetTileRef(MapCoordinates coords) { return MapSystem.GetTileRef(Owner, this, coords); } + [Obsolete("Use the MapSystem method")] public TileRef GetTileRef(EntityCoordinates coords) { return MapSystem.GetTileRef(Owner, this, coords); } + [Obsolete("Use the MapSystem method")] public TileRef GetTileRef(Vector2i tileCoordinates) { return MapSystem.GetTileRef(Owner, this, tileCoordinates); } + [Obsolete("Use the MapSystem method")] public IEnumerable GetAllTiles(bool ignoreEmpty = true) { return MapSystem.GetAllTiles(Owner, this, ignoreEmpty); } + [Obsolete("Use the MapSystem method")] public GridTileEnumerator GetAllTilesEnumerator(bool ignoreEmpty = true) { return MapSystem.GetAllTilesEnumerator(Owner, this, ignoreEmpty); } + [Obsolete("Use the MapSystem method")] public void SetTile(EntityCoordinates coords, Tile tile) { MapSystem.SetTile(Owner, this, coords, tile); } + [Obsolete("Use the MapSystem method")] public void SetTile(Vector2i gridIndices, Tile tile) { MapSystem.SetTile(Owner, this, gridIndices, tile); } + [Obsolete("Use the MapSystem method")] public void SetTiles(List<(Vector2i GridIndices, Tile Tile)> tiles) { MapSystem.SetTiles(Owner, this, tiles); } + [Obsolete("Use the MapSystem method")] public IEnumerable GetLocalTilesIntersecting(Box2Rotated localArea, bool ignoreEmpty = true, Predicate? predicate = null) { return MapSystem.GetLocalTilesIntersecting(Owner, this, localArea, ignoreEmpty, predicate); } + [Obsolete("Use the MapSystem method")] public IEnumerable GetTilesIntersecting(Box2Rotated worldArea, bool ignoreEmpty = true, Predicate? predicate = null) { return MapSystem.GetTilesIntersecting(Owner, this, worldArea, ignoreEmpty, predicate); } + [Obsolete("Use the MapSystem method")] public IEnumerable GetTilesIntersecting(Box2 worldArea, bool ignoreEmpty = true, Predicate? predicate = null) { return MapSystem.GetTilesIntersecting(Owner, this, worldArea, ignoreEmpty, predicate); } + [Obsolete("Use the MapSystem method")] public IEnumerable GetLocalTilesIntersecting(Box2 localArea, bool ignoreEmpty = true, Predicate? predicate = null) { return MapSystem.GetLocalTilesIntersecting(Owner, this, localArea, ignoreEmpty, predicate); } + [Obsolete("Use the MapSystem method")] public IEnumerable GetTilesIntersecting(Circle worldArea, bool ignoreEmpty = true, Predicate? predicate = null) { @@ -149,16 +162,19 @@ namespace Robust.Shared.Map.Components #region ChunkAccess + [Obsolete("Use the MapSystem method")] internal bool TryGetChunk(Vector2i chunkIndices, [NotNullWhen(true)] out MapChunk? chunk) { return MapSystem.TryGetChunk(Owner, this, chunkIndices, out chunk); } + [Obsolete("Use the MapSystem method")] internal IReadOnlyDictionary GetMapChunks() { return MapSystem.GetMapChunks(Owner, this); } + [Obsolete("Use the MapSystem method")] internal ChunkEnumerator GetMapChunks(Box2Rotated worldArea) { return MapSystem.GetMapChunks(Owner, this, worldArea); @@ -168,56 +184,67 @@ namespace Robust.Shared.Map.Components #region SnapGridAccess + [Obsolete("Use the MapSystem method")] public IEnumerable GetAnchoredEntities(MapCoordinates coords) { return MapSystem.GetAnchoredEntities(Owner, this, coords); } + [Obsolete("Use the MapSystem method")] public IEnumerable GetAnchoredEntities(Vector2i pos) { return MapSystem.GetAnchoredEntities(Owner, this, pos); } + [Obsolete("Use the MapSystem method")] public AnchoredEntitiesEnumerator GetAnchoredEntitiesEnumerator(Vector2i pos) { return MapSystem.GetAnchoredEntitiesEnumerator(Owner, this, pos); } + [Obsolete("Use the MapSystem method")] public IEnumerable GetLocalAnchoredEntities(Box2 localAABB) { return MapSystem.GetLocalAnchoredEntities(Owner, this, localAABB); } + [Obsolete("Use the MapSystem method")] public IEnumerable GetAnchoredEntities(Box2 worldAABB) { return MapSystem.GetAnchoredEntities(Owner, this, worldAABB); } + [Obsolete("Use the MapSystem method")] public Vector2i TileIndicesFor(EntityCoordinates coords) { return MapSystem.TileIndicesFor(Owner, this, coords); } + [Obsolete("Use the MapSystem method")] public Vector2i TileIndicesFor(MapCoordinates worldPos) { return MapSystem.TileIndicesFor(Owner, this, worldPos); } + [Obsolete("Use the MapSystem method")] public IEnumerable GetInDir(EntityCoordinates position, Direction dir) { return MapSystem.GetInDir(Owner, this, position, dir); } + [Obsolete("Use the MapSystem method")] public IEnumerable GetLocal(EntityCoordinates coords) { return MapSystem.GetLocal(Owner, this, coords); } + [Obsolete("Use the MapSystem method")] public IEnumerable GetCardinalNeighborCells(EntityCoordinates coords) { return MapSystem.GetCardinalNeighborCells(Owner, this, coords); } + [Obsolete("Use the MapSystem method")] public IEnumerable GetCellsInSquareArea(EntityCoordinates coords, int n) { return MapSystem.GetCellsInSquareArea(Owner, this, coords, n); @@ -225,66 +252,79 @@ namespace Robust.Shared.Map.Components #endregion + [Obsolete("Use the MapSystem method")] public Vector2 WorldToLocal(Vector2 posWorld) { return MapSystem.WorldToLocal(Owner, this, posWorld); } + [Obsolete("Use the MapSystem method")] public EntityCoordinates MapToGrid(MapCoordinates posWorld) { return MapSystem.MapToGrid(Owner, posWorld); } + [Obsolete("Use the MapSystem method")] public Vector2 LocalToWorld(Vector2 posLocal) { return MapSystem.LocalToWorld(Owner, this, posLocal); } + [Obsolete("Use the MapSystem method")] public Vector2i WorldToTile(Vector2 posWorld) { return MapSystem.WorldToTile(Owner, this, posWorld); } + [Obsolete("Use the MapSystem method")] public Vector2i LocalToTile(EntityCoordinates coordinates) { return MapSystem.LocalToTile(Owner, this, coordinates); } + [Obsolete("Use the MapSystem method")] public Vector2i CoordinatesToTile(EntityCoordinates coords) { return MapSystem.CoordinatesToTile(Owner, this, coords); } + [Obsolete("Use the MapSystem method")] public bool CollidesWithGrid(Vector2i indices) { return MapSystem.CollidesWithGrid(Owner, this, indices); } + [Obsolete("Use the MapSystem method")] public Vector2i GridTileToChunkIndices(Vector2i gridTile) { return MapSystem.GridTileToChunkIndices(Owner, this, gridTile); } + [Obsolete("Use the MapSystem method")] public EntityCoordinates GridTileToLocal(Vector2i gridTile) { return MapSystem.GridTileToLocal(Owner, this, gridTile); } + [Obsolete("Use the MapSystem method")] public Vector2 GridTileToWorldPos(Vector2i gridTile) { return MapSystem.GridTileToWorldPos(Owner, this, gridTile); } + [Obsolete("Use the MapSystem method")] public MapCoordinates GridTileToWorld(Vector2i gridTile) { return MapSystem.GridTileToWorld(Owner, this, gridTile); } + [Obsolete("Use the MapSystem method")] public bool TryGetTileRef(Vector2i indices, out TileRef tile) { return MapSystem.TryGetTileRef(Owner, this, indices, out tile); } + [Obsolete("Use the MapSystem method")] public bool TryGetTileRef(EntityCoordinates coords, out TileRef tile) { return MapSystem.TryGetTileRef(Owner, this, coords, out tile); diff --git a/Robust.Shared/Map/IMapManager.cs b/Robust.Shared/Map/IMapManager.cs index 6697eff34..9ec0a0f0f 100644 --- a/Robust.Shared/Map/IMapManager.cs +++ b/Robust.Shared/Map/IMapManager.cs @@ -87,11 +87,21 @@ namespace Robust.Shared.Map MapGridComponent CreateGrid(MapId currentMapId, ushort chunkSize = 16); MapGridComponent CreateGrid(MapId currentMapId, in GridCreateOptions options); MapGridComponent CreateGrid(MapId currentMapId); + Entity CreateGridEntity(MapId currentMapId, GridCreateOptions? options = null); + + [Obsolete("Use GetComponent(uid)")] MapGridComponent GetGrid(EntityUid gridId); + + [Obsolete("Use TryGetComponent(uid, out MapGridComponent? grid)")] bool TryGetGrid([NotNullWhen(true)] EntityUid? euid, [NotNullWhen(true)] out MapGridComponent? grid); + + [Obsolete("Use HasComponent(uid)")] bool GridExists([NotNullWhen(true)] EntityUid? euid); + IEnumerable GetAllMapGrids(MapId mapId); + IEnumerable> GetAllGrids(MapId mapId); + /// /// Attempts to find the map grid under the map location. /// @@ -131,10 +141,13 @@ namespace Robust.Shared.Map void FindGridsIntersecting(MapId mapId, Box2 worldAABB, ref TState state, GridCallback callback, bool approx = false, bool includeMap = true); + void FindGridsIntersecting(MapId mapId, Box2 worldAABB, ref List> state, bool approx = false, bool includeMap = true); + void FindGridsIntersecting(MapId mapId, Box2Rotated worldBounds, GridCallback callback, bool approx = false, bool includeMap = true); void FindGridsIntersecting(MapId mapId, Box2Rotated worldBounds, ref TState state, GridCallback callback, bool approx = false, bool includeMap = true); + void FindGridsIntersecting(MapId mapId, Box2Rotated worldBounds, ref List> state, bool approx = false, bool includeMap = true); /// /// Returns the grids intersecting this AABB. diff --git a/Robust.Shared/Map/MapManager.GridCollection.cs b/Robust.Shared/Map/MapManager.GridCollection.cs index e9c60e820..0b2b50126 100644 --- a/Robust.Shared/Map/MapManager.GridCollection.cs +++ b/Robust.Shared/Map/MapManager.GridCollection.cs @@ -1,11 +1,8 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Linq; using Robust.Shared.GameObjects; -using Robust.Shared.Log; using Robust.Shared.Map.Components; -using Robust.Shared.Maths; using Robust.Shared.Utility; // All the obsolete warnings about GridId are probably useless here. @@ -47,6 +44,12 @@ internal partial class MapManager return CreateGrid(currentMapId, GridCreateOptions.Default); } + public Entity CreateGridEntity(MapId currentMapId, GridCreateOptions? options = null) + { + options ??= GridCreateOptions.Default; + return CreateGrid(currentMapId, options.Value.ChunkSize, default); + } + [Obsolete("Use GetComponent(uid)")] public MapGridComponent GetGrid(EntityUid gridId) { @@ -61,6 +64,7 @@ internal partial class MapManager return EntityManager.HasComponent(uid); } + [Obsolete("Use TryGetComponent(uid, out MapGridComponent? grid)")] public bool TryGetGrid([NotNullWhen(true)] EntityUid? euid, [MaybeNullWhen(false)] out MapGridComponent grid) { if (EntityManager.TryGetComponent(euid, out MapGridComponent? comp)) @@ -81,11 +85,24 @@ internal partial class MapManager public IEnumerable GetAllMapGrids(MapId mapId) { - var xformQuery = EntityManager.GetEntityQuery(); + var query = EntityManager.AllEntityQueryEnumerator(); + while (query.MoveNext(out var grid, out var xform)) + { + if (xform.MapID != mapId) + yield return grid; + } + } - return EntityManager.EntityQuery(true) - .Where(c => xformQuery.GetComponent(c.Owner).MapID == mapId) - .Select(c => c); + public IEnumerable> GetAllGrids(MapId mapId) + { + var query = EntityManager.AllEntityQueryEnumerator(); + while (query.MoveNext(out var uid, out var grid, out var xform)) + { + if (xform.MapID != mapId) + continue; + + yield return (uid, grid); + } } public virtual void DeleteGrid(EntityUid euid) @@ -135,7 +152,7 @@ internal partial class MapManager EntityManager.EventBus.RaiseLocalEvent(euid, ref ev, true); } - protected MapGridComponent CreateGrid(MapId currentMapId, ushort chunkSize, EntityUid forcedGridEuid) + protected Entity CreateGrid(MapId currentMapId, ushort chunkSize, EntityUid forcedGridEuid) { var gridEnt = EntityManager.CreateEntityUninitialized(null, forcedGridEuid); @@ -155,6 +172,6 @@ internal partial class MapManager EntityManager.System().SetEntityName(gridEnt, $"grid", meta); EntityManager.InitializeComponents(gridEnt, meta); EntityManager.StartComponents(gridEnt); - return grid; + return (gridEnt, grid); } } diff --git a/Robust.Shared/Map/MapManager.MapCollection.cs b/Robust.Shared/Map/MapManager.MapCollection.cs index 68b9b56e2..f8bedd642 100644 --- a/Robust.Shared/Map/MapManager.MapCollection.cs +++ b/Robust.Shared/Map/MapManager.MapCollection.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; -using System.Linq; using Robust.Shared.GameObjects; -using Robust.Shared.Log; using Robust.Shared.Map.Components; using Robust.Shared.Utility; @@ -211,22 +209,21 @@ internal partial class MapManager if (actualId != MapId.Nullspace) // nullspace isn't bound to an entity { - var mapComps = EntityManager.EntityQuery(true); - - MapComponent? result = null; - foreach (var mapComp in mapComps) + Entity result = default; + var query = EntityManager.AllEntityQueryEnumerator(); + while (query.MoveNext(out var uid, out var map)) { - if (mapComp.MapId != actualId) + if (map.MapId != actualId) continue; - result = mapComp; + result = (uid, map); break; } - if (result != null) + if (result != default) { DebugTools.Assert(mapId != null); - _mapEntities.Add(actualId, result.Owner); + _mapEntities.Add(actualId, result); _sawmill.Debug($"Rebinding map {actualId} to entity {result.Owner}"); } else diff --git a/Robust.Shared/Map/MapManager.Queries.cs b/Robust.Shared/Map/MapManager.Queries.cs index 85ecda9a7..35cc744da 100644 --- a/Robust.Shared/Map/MapManager.Queries.cs +++ b/Robust.Shared/Map/MapManager.Queries.cs @@ -96,6 +96,17 @@ internal partial class MapManager state = state2.state; } + public void FindGridsIntersecting(MapId mapId, Box2 worldAABB, ref List> state, + bool approx = false, bool includeMap = true) + { + FindGridsIntersecting(mapId, worldAABB, ref state, static (EntityUid uid, MapGridComponent grid, + ref List> list) => + { + list.Add((uid, grid)); + return true; + }, approx, includeMap); + } + public void FindGridsIntersecting(MapId mapId, Box2Rotated worldBounds, GridCallback callback, bool approx = false, bool includeMap = true) { @@ -108,6 +119,17 @@ internal partial class MapManager FindGridsIntersecting(mapId, worldBounds.CalcBoundingBox(), ref state, callback, approx, includeMap); } + public void FindGridsIntersecting(MapId mapId, Box2Rotated worldBounds, ref List> state, + bool approx = false, bool includeMap = true) + { + FindGridsIntersecting(mapId, worldBounds, ref state, static (EntityUid uid, MapGridComponent grid, + ref List> list) => + { + list.Add((uid, grid)); + return true; + }, approx, includeMap); + } + private bool IsIntersecting( Box2 aabb, EntityUid gridUid, diff --git a/Robust.Shared/Physics/Events/CollisionLayerChangeEvent.cs b/Robust.Shared/Physics/Events/CollisionLayerChangeEvent.cs index 0e8d0b2c2..91bae63d8 100644 --- a/Robust.Shared/Physics/Events/CollisionLayerChangeEvent.cs +++ b/Robust.Shared/Physics/Events/CollisionLayerChangeEvent.cs @@ -9,9 +9,9 @@ namespace Robust.Shared.Physics.Events [ByRefEvent] public readonly struct CollisionLayerChangeEvent { - public readonly PhysicsComponent Body; + public readonly Entity Body; - public CollisionLayerChangeEvent(PhysicsComponent body) + public CollisionLayerChangeEvent(Entity body) { Body = body; } diff --git a/Robust.Shared/Physics/Systems/FixtureSystem.cs b/Robust.Shared/Physics/Systems/FixtureSystem.cs index dd11c1926..a50d060ad 100644 --- a/Robust.Shared/Physics/Systems/FixtureSystem.cs +++ b/Robust.Shared/Physics/Systems/FixtureSystem.cs @@ -5,7 +5,6 @@ using Robust.Shared.Collections; using Robust.Shared.GameObjects; using Robust.Shared.GameStates; using Robust.Shared.IoC; -using Robust.Shared.Log; using Robust.Shared.Physics.Collision.Shapes; using Robust.Shared.Physics.Components; using Robust.Shared.Physics.Dynamics; @@ -354,7 +353,7 @@ namespace Robust.Shared.Physics.Systems if (oldLayer != layer) { - var ev = new CollisionLayerChangeEvent(body); + var ev = new CollisionLayerChangeEvent((uid, body)); RaiseLocalEvent(ref ev); } diff --git a/Robust.Shared/Physics/Systems/SharedJointSystem.cs b/Robust.Shared/Physics/Systems/SharedJointSystem.cs index 6e9ff60b8..9e7be843f 100644 --- a/Robust.Shared/Physics/Systems/SharedJointSystem.cs +++ b/Robust.Shared/Physics/Systems/SharedJointSystem.cs @@ -26,7 +26,7 @@ public abstract partial class SharedJointSystem : EntitySystem // To avoid issues with component states we'll queue up all dirty joints and check it every tick to see if // we can delete the component. - private readonly HashSet _dirtyJoints = new(); + private readonly HashSet> _dirtyJoints = new(); protected readonly HashSet AddedJoints = new(); protected readonly List ToRemove = new(); @@ -115,8 +115,8 @@ public abstract partial class SharedJointSystem : EntitySystem foreach (var joint in _dirtyJoints) { - if (joint.Deleted || joint.JointCount != 0) continue; - EntityManager.RemoveComponent(joint.Owner); + if (joint.Comp.Deleted || joint.Comp.JointCount != 0) continue; + EntityManager.RemoveComponent(joint); } _dirtyJoints.Clear(); @@ -139,7 +139,8 @@ public abstract partial class SharedJointSystem : EntitySystem jointComponentA ??= EnsureComp(aUid); jointComponentB ??= EnsureComp(bUid); - DebugTools.Assert(jointComponentA.Owner == aUid && jointComponentB.Owner == bUid); + DebugTools.AssertOwner(aUid, jointComponentA); + DebugTools.AssertOwner(bUid, jointComponentB); DebugTools.AssertNotEqual(jointComponentA.Relay, bUid); DebugTools.AssertNotEqual(jointComponentB.Relay, aUid); @@ -198,8 +199,8 @@ public abstract partial class SharedJointSystem : EntitySystem Dirty(bUid, jointComponentB); // Also flag these for checking juusssttt in case. - _dirtyJoints.Add(jointComponentA); - _dirtyJoints.Add(jointComponentB); + _dirtyJoints.Add((aUid, jointComponentA)); + _dirtyJoints.Add((bUid, jointComponentB)); // Note: creating a joint doesn't wake the bodies. // Raise broadcast last so we can do both sides of directed first. @@ -522,12 +523,12 @@ public abstract partial class SharedJointSystem : EntitySystem // Originally I logged these but because of prediction the client can just nuke them multiple times in a row // because each body has its own JointComponent, bleh. - if (!EntityManager.TryGetComponent(bodyAUid, out var jointComponentA)) + if (!TryComp(bodyAUid, out var jointComponentA)) { return; } - if (!EntityManager.TryGetComponent(bodyBUid, out var jointComponentB)) + if (!TryComp(bodyBUid, out var jointComponentB)) { return; } @@ -559,12 +560,12 @@ public abstract partial class SharedJointSystem : EntitySystem if (!jointComponentA.Deleted) { - Dirty(jointComponentA); + Dirty(bodyAUid, jointComponentA); } if (!jointComponentB.Deleted) { - Dirty(jointComponentB); + Dirty(bodyBUid, jointComponentB); } if (jointComponentA.Deleted && jointComponentB.Deleted) @@ -599,8 +600,8 @@ public abstract partial class SharedJointSystem : EntitySystem } // We can't just check up front due to how prediction works. - _dirtyJoints.Add(jointComponentA); - _dirtyJoints.Add(jointComponentB); + _dirtyJoints.Add((bodyAUid, jointComponentA)); + _dirtyJoints.Add((bodyBUid, jointComponentB)); } #endregion diff --git a/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Components.cs b/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Components.cs index 50d9cd0b0..304185499 100644 --- a/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Components.cs +++ b/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Components.cs @@ -22,6 +22,7 @@ * PhysicsComponent is heavily modified from Box2D. */ +using System; using System.Numerics; using Robust.Shared.GameObjects; using Robust.Shared.GameStates; @@ -371,8 +372,16 @@ public partial class SharedPhysicsSystem Dirty(body); } + [Obsolete("Use SetAwake with EntityUid")] public void SetAwake(EntityUid uid, PhysicsComponent body, bool value, bool updateSleepTime = true) { + SetAwake(new Entity(uid, body), value, updateSleepTime); + } + + public void SetAwake(Entity ent, bool value, bool updateSleepTime = true) + { + var uid = ent.Owner; + var body = ent.Comp; if (body.Awake == value) return; @@ -396,7 +405,7 @@ public partial class SharedPhysicsSystem if (updateSleepTime) SetSleepTime(body, 0); - Dirty(body); + Dirty(ent); } public void TrySetBodyType(EntityUid uid, BodyType value, FixturesComponent? manager = null, PhysicsComponent? body = null, TransformComponent? xform = null) diff --git a/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Queries.cs b/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Queries.cs index 553e11320..b9418f5cf 100644 --- a/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Queries.cs +++ b/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Queries.cs @@ -150,11 +150,12 @@ namespace Robust.Shared.Physics.Systems /// /// Get all entities colliding with a certain body. /// - public IEnumerable GetCollidingEntities(MapId mapId, in Box2Rotated worldBounds) + public IEnumerable> GetCollidingEntities(MapId mapId, in Box2Rotated worldBounds) { - if (mapId == MapId.Nullspace) return Array.Empty(); + if (mapId == MapId.Nullspace) + return Array.Empty>(); - var bodies = new HashSet(); + var bodies = new HashSet>(); foreach (var (uid, broadphase) in _broadphase.GetBroadphases(mapId, worldBounds.CalcBoundingBox())) { @@ -162,12 +163,12 @@ namespace Robust.Shared.Physics.Systems foreach (var proxy in broadphase.StaticTree.QueryAabb(gridAABB, false)) { - bodies.Add(proxy.Body); + bodies.Add(new Entity(proxy.Entity, proxy.Body)); } foreach (var proxy in broadphase.DynamicTree.QueryAabb(gridAABB, false)) { - bodies.Add(proxy.Body); + bodies.Add(new Entity(proxy.Entity, proxy.Body)); } } diff --git a/Robust.Shared/Prototypes/EntityPrototype.cs b/Robust.Shared/Prototypes/EntityPrototype.cs index a0f67ae7f..22c9b6b4d 100644 --- a/Robust.Shared/Prototypes/EntityPrototype.cs +++ b/Robust.Shared/Prototypes/EntityPrototype.cs @@ -250,7 +250,6 @@ namespace Robust.Shared.Prototypes if (!entityManager.TryGetComponent(entity, compReg.Idx, out var component)) { var newComponent = factory.GetComponent(compName); - newComponent.Owner = entity; entityManager.AddComponent(entity, newComponent); component = newComponent; } diff --git a/Robust.Shared/Utility/DebugTools.cs b/Robust.Shared/Utility/DebugTools.cs index 83210c86f..f6157318b 100644 --- a/Robust.Shared/Utility/DebugTools.cs +++ b/Robust.Shared/Utility/DebugTools.cs @@ -1,5 +1,4 @@ using System; -using System.Data; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; @@ -98,7 +97,9 @@ namespace Robust.Shared.Utility // Whenever .owner is removed this will need to be replaced by something. // As long as components are just reference types, we could just get the component and check if the references are equal? +#pragma warning disable CS0618 // Type or member is obsolete if (component.Owner != uid) +#pragma warning restore CS0618 // Type or member is obsolete throw new DebugAssertException($"Entity {uid} is not the owner of the component. Component: {component.GetType().Name}"); } diff --git a/Robust.UnitTesting/Client/GameObjects/Components/TransformComponentTests.cs b/Robust.UnitTesting/Client/GameObjects/Components/TransformComponentTests.cs index bbfcf91a3..7190e01d4 100644 --- a/Robust.UnitTesting/Client/GameObjects/Components/TransformComponentTests.cs +++ b/Robust.UnitTesting/Client/GameObjects/Components/TransformComponentTests.cs @@ -26,10 +26,10 @@ namespace Robust.UnitTesting.Client.GameObjects.Components mapManager.CreateMap(TestMapId); // Adds two grids to use in tests. - var gridA = mapManager.CreateGrid(TestMapId); - var gridB = mapManager.CreateGrid(TestMapId); + var gridA = mapManager.CreateGridEntity(TestMapId); + var gridB = mapManager.CreateGridEntity(TestMapId); - return (sim, gridA.Owner, gridB.Owner); + return (sim, gridA, gridB); } /// @@ -47,18 +47,18 @@ namespace Robust.UnitTesting.Client.GameObjects.Components var gridB = mapMan.GetGrid(gridIdB); // Arrange - var initialPos = new EntityCoordinates(gridA.Owner, new Vector2(0, 0)); + var initialPos = new EntityCoordinates(gridIdA, new Vector2(0, 0)); var parent = entMan.SpawnEntity(null, initialPos); var child = entMan.SpawnEntity(null, initialPos); var parentTrans = entMan.GetComponent(parent); var childTrans = entMan.GetComponent(child); ComponentHandleState handleState; - var compState = new TransformComponentState(new Vector2(5, 5), new Angle(0), entMan.GetNetEntity(gridB.Owner), false, false); + var compState = new TransformComponentState(new Vector2(5, 5), new Angle(0), entMan.GetNetEntity(gridIdB), false, false); handleState = new ComponentHandleState(compState, null); xformSystem.OnHandleState(parent, parentTrans, ref handleState); - compState = new TransformComponentState(new Vector2(6, 6), new Angle(0), entMan.GetNetEntity(gridB.Owner), false, false); + compState = new TransformComponentState(new Vector2(6, 6), new Angle(0), entMan.GetNetEntity(gridIdB), false, false); handleState = new ComponentHandleState(compState, null); xformSystem.OnHandleState(child, childTrans, ref handleState); // World pos should be 6, 6 now. @@ -90,7 +90,7 @@ namespace Robust.UnitTesting.Client.GameObjects.Components var gridB = mapMan.GetGrid(gridIdB); // Arrange - var initalPos = new EntityCoordinates(gridA.Owner, new Vector2(0, 0)); + var initalPos = new EntityCoordinates(gridIdA, new Vector2(0, 0)); var node1 = entMan.SpawnEntity(null, initalPos); var node2 = entMan.SpawnEntity(null, initalPos); var node3 = entMan.SpawnEntity(null, initalPos); @@ -103,7 +103,7 @@ namespace Robust.UnitTesting.Client.GameObjects.Components var node2Trans = entMan.GetComponent(node2); var node3Trans = entMan.GetComponent(node3); - var compState = new TransformComponentState(new Vector2(6, 6), Angle.FromDegrees(135), entMan.GetNetEntity(gridB.Owner), false, false); + var compState = new TransformComponentState(new Vector2(6, 6), Angle.FromDegrees(135), entMan.GetNetEntity(gridIdB), false, false); var handleState = new ComponentHandleState(compState, null); xformSystem.OnHandleState(node1, node1Trans, ref handleState); diff --git a/Robust.UnitTesting/Server/GameObjects/Components/Container_Test.cs b/Robust.UnitTesting/Server/GameObjects/Components/Container_Test.cs index fffe3760a..de84c0d55 100644 --- a/Robust.UnitTesting/Server/GameObjects/Components/Container_Test.cs +++ b/Robust.UnitTesting/Server/GameObjects/Components/Container_Test.cs @@ -1,8 +1,6 @@ -using System; using System.Collections.Generic; using System.Linq; using System.Numerics; -using JetBrains.Annotations; using NUnit.Framework; using Robust.Server.Containers; using Robust.Shared.Containers; @@ -195,7 +193,7 @@ namespace Robust.UnitTesting.Server.GameObjects.Components var sim = SimulationFactory(); var containerSys = sim.Resolve().GetEntitySystem(); - var grid = sim.Resolve().CreateGrid(new MapId(1)).Owner; + var grid = sim.Resolve().CreateGridEntity(new MapId(1)).Owner; var entity = sim.SpawnEntity(null, new EntityCoordinates(new EntityUid(1), new Vector2(0, 0))); var container = containerSys.MakeContainer(entity, "dummy"); diff --git a/Robust.UnitTesting/Server/GameObjects/Components/Transform_Test.cs b/Robust.UnitTesting/Server/GameObjects/Components/Transform_Test.cs index 7f5cf9ab6..d9e449b72 100644 --- a/Robust.UnitTesting/Server/GameObjects/Components/Transform_Test.cs +++ b/Robust.UnitTesting/Server/GameObjects/Components/Transform_Test.cs @@ -1,17 +1,12 @@ using System.IO; using System.Numerics; -using System.Reflection; -using Moq; using NUnit.Framework; -using Robust.Server.Containers; using Robust.Server.GameObjects; -using Robust.Server.Physics; using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Map; using Robust.Shared.Map.Components; using Robust.Shared.Maths; -using Robust.Shared.Physics; using Robust.Shared.Prototypes; using Robust.Shared.Serialization.Manager; using Robust.Shared.Timing; @@ -40,9 +35,9 @@ namespace Robust.UnitTesting.Server.GameObjects.Components "; private MapId MapA; - private MapGridComponent GridA = default!; + private Entity GridA = default!; private MapId MapB; - private MapGridComponent GridB = default!; + private Entity GridB = default!; private static readonly EntityCoordinates InitialPos = new(new EntityUid(1), new Vector2(0, 0)); @@ -63,10 +58,10 @@ namespace Robust.UnitTesting.Server.GameObjects.Components // build the net dream MapA = MapManager.CreateMap(); - GridA = MapManager.CreateGrid(MapA); + GridA = MapManager.CreateGridEntity(MapA); MapB = MapManager.CreateMap(); - GridB = MapManager.CreateGrid(MapB); + GridB = MapManager.CreateGridEntity(MapB); //NOTE: The grids have not moved, so we can assert worldpos == localpos for the test } @@ -89,8 +84,8 @@ namespace Robust.UnitTesting.Server.GameObjects.Components var childTrans = EntityManager.GetComponent(child); // that are not on the same map - parentTrans.Coordinates = new EntityCoordinates(GridA.Owner, new Vector2(5, 5)); - childTrans.Coordinates = new EntityCoordinates(GridB.Owner, new Vector2(4, 4)); + parentTrans.Coordinates = new EntityCoordinates(GridA, new Vector2(5, 5)); + childTrans.Coordinates = new EntityCoordinates(GridB, new Vector2(4, 4)); // if they are parented, the child keeps its world position, but moves to the parents map childTrans.AttachParent(parentTrans); @@ -100,7 +95,7 @@ namespace Robust.UnitTesting.Server.GameObjects.Components { Assert.That(childTrans.MapID, Is.EqualTo(parentTrans.MapID)); Assert.That(childTrans.GridUid, Is.EqualTo(parentTrans.GridUid)); - Assert.That(childTrans.Coordinates, Is.EqualTo(new EntityCoordinates(parentTrans.Owner, new Vector2(-1, -1)))); + Assert.That(childTrans.Coordinates, Is.EqualTo(new EntityCoordinates(parent, new Vector2(-1, -1)))); Assert.That(childTrans.WorldPosition, Is.EqualTo(new Vector2(4, 4))); }); diff --git a/Robust.UnitTesting/Server/GameStates/PvsSystemTests.cs b/Robust.UnitTesting/Server/GameStates/PvsSystemTests.cs index 02587dd43..8aa3050e8 100644 --- a/Robust.UnitTesting/Server/GameStates/PvsSystemTests.cs +++ b/Robust.UnitTesting/Server/GameStates/PvsSystemTests.cs @@ -54,8 +54,8 @@ public sealed class PvsSystemTests : RobustIntegrationTest { var mapId = mapMan.CreateMap(); map = mapMan.GetMapEntityId(mapId); - var gridComp = mapMan.CreateGrid(mapId); - gridComp.SetTile(Vector2i.Zero, new Tile(1)); + var gridComp = mapMan.CreateGridEntity(mapId); + gridComp.Comp.SetTile(Vector2i.Zero, new Tile(1)); grid = gridComp.Owner; }); diff --git a/Robust.UnitTesting/Shared/EntityLookup_Test.cs b/Robust.UnitTesting/Shared/EntityLookup_Test.cs index 1eab0668a..8994cd86c 100644 --- a/Robust.UnitTesting/Shared/EntityLookup_Test.cs +++ b/Robust.UnitTesting/Shared/EntityLookup_Test.cs @@ -44,10 +44,10 @@ namespace Robust.UnitTesting.Shared var mapManager = server.Resolve(); var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); + var grid = mapManager.CreateGridEntity(mapId); var theMapSpotBeingUsed = new Box2(Vector2.Zero, Vector2.One); - grid.SetTile(new Vector2i(), new Tile(1)); + grid.Comp.SetTile(new Vector2i(), new Tile(1)); Assert.That(lookup.GetEntitiesIntersecting(mapId, theMapSpotBeingUsed).ToList().Count, Is.EqualTo(0)); @@ -66,7 +66,7 @@ namespace Robust.UnitTesting.Shared Assert.That(lookup.GetEntitiesIntersecting(mapId, theMapSpotBeingUsed).ToList().Count, Is.EqualTo(1)); entManager.DeleteEntity(dummy); - mapManager.DeleteGrid(grid.Owner); + mapManager.DeleteGrid(grid); mapManager.DeleteMap(mapId); } } diff --git a/Robust.UnitTesting/Shared/GameObjects/ContainerTests.cs b/Robust.UnitTesting/Shared/GameObjects/ContainerTests.cs index b4943b693..15ae17789 100644 --- a/Robust.UnitTesting/Shared/GameObjects/ContainerTests.cs +++ b/Robust.UnitTesting/Shared/GameObjects/ContainerTests.cs @@ -8,7 +8,6 @@ using Robust.Server.GameObjects; using Robust.Server.Player; using Robust.Shared.Containers; using Robust.Shared.GameObjects; -using Robust.Shared.IoC; using Robust.Shared.Map; using Robust.Shared.Network; using Robust.Shared.Timing; @@ -42,7 +41,6 @@ namespace Robust.UnitTesting.Shared.GameObjects Assert.DoesNotThrow(() => client.SetConnectTarget(server)); client.Post(() => { - clientNetManager.ClientConnect(null!, 0, null!); }); @@ -58,16 +56,18 @@ namespace Robust.UnitTesting.Shared.GameObjects EntityUid entityUid = default!; + var cContainerSys = cEntManager.System(); + var sContainerSys = sEntManager.System(); + var sMetadataSys = sEntManager.System(); + await server.WaitAssertion(() => { - var containerSys = sEntManager.System(); - mapId = sMapManager.CreateMap(); mapPos = new MapCoordinates(new Vector2(0, 0), mapId); entityUid = sEntManager.SpawnEntity(null, mapPos); - sEntManager.GetComponent(entityUid).EntityName = "Container"; - containerSys.EnsureContainer(entityUid, "dummy"); + sMetadataSys.SetEntityName(entityUid, "Container"); + sContainerSys.EnsureContainer(entityUid, "dummy"); // Setup PVS sEntManager.AddComponent(entityUid); @@ -85,11 +85,9 @@ namespace Robust.UnitTesting.Shared.GameObjects EntityUid itemUid = default!; await server.WaitAssertion(() => { - var containerSys = sEntManager.System(); - itemUid = sEntManager.SpawnEntity(null, mapPos); - sEntManager.GetComponent(itemUid).EntityName = "Item"; - var container = containerSys.EnsureContainer(entityUid, "dummy"); + sMetadataSys.SetEntityName(itemUid, "Item"); + var container = sContainerSys.EnsureContainer(entityUid, "dummy"); Assert.That(container.Insert(itemUid)); // Move item out of PVS so that it doesn't get sent to the client @@ -114,9 +112,8 @@ namespace Robust.UnitTesting.Shared.GameObjects Assert.That(container.ContainedEntities.Count, Is.EqualTo(0)); Assert.That(container.ExpectedEntities.Count, Is.EqualTo(1)); - var containerSystem = cEntManager.System(); - Assert.That(containerSystem.ExpectedEntities.ContainsKey(sEntManager.GetNetEntity(itemUid))); - Assert.That(containerSystem.ExpectedEntities.Count, Is.EqualTo(1)); + Assert.That(cContainerSys.ExpectedEntities.ContainsKey(sEntManager.GetNetEntity(itemUid))); + Assert.That(cContainerSys.ExpectedEntities.Count, Is.EqualTo(1)); }); await server.WaitAssertion(() => @@ -140,9 +137,8 @@ namespace Robust.UnitTesting.Shared.GameObjects Assert.That(container.ContainedEntities.Count, Is.EqualTo(1)); Assert.That(container.ExpectedEntities.Count, Is.EqualTo(0)); - var containerSystem = cEntManager.System(); - Assert.That(!containerSystem.ExpectedEntities.ContainsKey(sEntManager.GetNetEntity(itemUid))); - Assert.That(containerSystem.ExpectedEntities, Is.Empty); + Assert.That(!cContainerSys.ExpectedEntities.ContainsKey(sEntManager.GetNetEntity(itemUid))); + Assert.That(cContainerSys.ExpectedEntities, Is.Empty); }); } @@ -188,16 +184,18 @@ namespace Robust.UnitTesting.Shared.GameObjects EntityUid sItemUid = default!; NetEntity netEnt = default; + var cContainerSys = cEntManager.System(); + var sContainerSys = sEntManager.System(); + var sMetadataSys = sEntManager.System(); + await server.WaitAssertion(() => { - var containerSys = sEntManager.System(); - mapId = sMapManager.CreateMap(); mapPos = new MapCoordinates(new Vector2(0, 0), mapId); sEntityUid = sEntManager.SpawnEntity(null, mapPos); - sEntManager.GetComponent(sEntityUid).EntityName = "Container"; - containerSys.EnsureContainer(sEntityUid, "dummy"); + sMetadataSys.SetEntityName(sEntityUid, "Container"); + sContainerSys.EnsureContainer(sEntityUid, "dummy"); // Setup PVS sEntManager.AddComponent(sEntityUid); @@ -214,12 +212,10 @@ namespace Robust.UnitTesting.Shared.GameObjects await server.WaitAssertion(() => { - var containerSys = sEntManager.System(); - sItemUid = sEntManager.SpawnEntity(null, mapPos); netEnt = sEntManager.GetNetEntity(sItemUid); - sEntManager.GetComponent(sItemUid).EntityName = "Item"; - var container = containerSys.GetContainer(sEntityUid, "dummy"); + sMetadataSys.SetEntityName(sItemUid, "Item"); + var container = sContainerSys.GetContainer(sEntityUid, "dummy"); container.Insert(sItemUid); // Move item out of PVS so that it doesn't get sent to the client @@ -247,20 +243,17 @@ namespace Robust.UnitTesting.Shared.GameObjects Assert.That(container.ContainedEntities.Count, Is.EqualTo(0)); Assert.That(container.ExpectedEntities.Count, Is.EqualTo(1)); - var containerSystem = cEntManager.System(); - Assert.That(containerSystem.ExpectedEntities.ContainsKey(netEnt)); - Assert.That(containerSystem.ExpectedEntities.Count, Is.EqualTo(1)); + Assert.That(cContainerSys.ExpectedEntities.ContainsKey(netEnt)); + Assert.That(cContainerSys.ExpectedEntities.Count, Is.EqualTo(1)); }); await server.WaitAssertion(() => { - var containerSystem = sEntManager.System(); - // If possible it'd be best to only have the DeleteEntity, but right now // the entity deleted event is not played on the client if the entity does not exist on the client. if (sEntManager.EntityExists(sItemUid) // && itemUid.TryGetContainer(out var container)) - && containerSystem.TryGetContainingContainer(sItemUid, out var container)) + && sContainerSys.TryGetContainingContainer(sItemUid, out var container)) { container.ForceRemove(sItemUid); } @@ -283,9 +276,8 @@ namespace Robust.UnitTesting.Shared.GameObjects Assert.That(container.ContainedEntities.Count, Is.EqualTo(0)); Assert.That(container.ExpectedEntities.Count, Is.EqualTo(0)); - var containerSystem = cEntManager.System(); - Assert.That(!containerSystem.ExpectedEntities.ContainsKey(netEnt)); - Assert.That(containerSystem.ExpectedEntities.Count, Is.EqualTo(0)); + Assert.That(!cContainerSys.ExpectedEntities.ContainsKey(netEnt)); + Assert.That(cContainerSys.ExpectedEntities.Count, Is.EqualTo(0)); }); } @@ -302,22 +294,22 @@ namespace Robust.UnitTesting.Shared.GameObjects var sEntManager = server.ResolveDependency(); var mapManager = server.ResolveDependency(); + var sContainerSys = sEntManager.System(); + var sMetadataSys = sEntManager.System(); await server.WaitAssertion(() => { - var containerSys = sEntManager.EntitySysManager.GetEntitySystem(); - // build the map var mapIdOne = mapManager.CreateMap(); Assert.That(mapManager.IsMapInitialized(mapIdOne), Is.True); var containerEnt = sEntManager.SpawnEntity(null, new MapCoordinates(1, 1, mapIdOne)); - sEntManager.GetComponent(containerEnt).EntityName = "ContainerEnt"; + sMetadataSys.SetEntityName(containerEnt, "ContainerEnt"); var containeeEnt = sEntManager.SpawnEntity(null, new MapCoordinates(2, 2, mapIdOne)); - sEntManager.GetComponent(containeeEnt).EntityName = "ContaineeEnt"; + sMetadataSys.SetEntityName(containeeEnt, "ContaineeEnt"); - var container = containerSys.MakeContainer(containerEnt, "testContainer"); + var container = sContainerSys.MakeContainer(containerEnt, "testContainer"); container.OccludesLight = true; container.ShowContents = true; container.Insert(containeeEnt); @@ -347,15 +339,21 @@ namespace Robust.UnitTesting.Shared.GameObjects await server.WaitAssertion(() => { // verify container - var containerQuery = sEntManager.EntityQuery(); - var containerComp = containerQuery.First(); - var containerEnt = containerComp.Owner; + Entity container = default; + var query = sEntManager.EntityQueryEnumerator(); + while (query.MoveNext(out var uid, out var containerComp)) + { + container = (uid, containerComp); + } + + var containerEnt = container.Owner; + Assert.NotNull(container.Comp); Assert.That(sEntManager.GetComponent(containerEnt).EntityName, Is.EqualTo("ContainerEnt")); - Assert.That(containerComp.Containers.ContainsKey("testContainer")); + Assert.That(container.Comp!.Containers.ContainsKey("testContainer")); - var baseContainer = containerComp.GetContainer("testContainer"); + var baseContainer = container.Comp.GetContainer("testContainer"); Assert.That(baseContainer.ContainedEntities, Has.Count.EqualTo(1)); var containeeEnt = baseContainer.ContainedEntities[0]; diff --git a/Robust.UnitTesting/Shared/GameObjects/GenericEntityPrint.cs b/Robust.UnitTesting/Shared/GameObjects/GenericEntityPrint.cs new file mode 100644 index 000000000..2574c8073 --- /dev/null +++ b/Robust.UnitTesting/Shared/GameObjects/GenericEntityPrint.cs @@ -0,0 +1,120 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Robust.UnitTesting.Shared.GameObjects; + +public sealed class GenericEntityPrint +{ + // [Test] + public void Print() + { + // Using the test framework for things it was not meant for is my passion + var i = 8; + + IEnumerable Generics(int n, bool nullable) + { + for (var j = 1; j <= n; j++) + { + var jStr = n == 1 ? string.Empty : j.ToString(); + yield return $"T{jStr}{(nullable ? "?" : string.Empty)}"; + } + } + + var structs = new StringBuilder(); + var constraints = new StringBuilder(); + var fields = new StringBuilder(); + var parameters = new StringBuilder(); + var asserts = new StringBuilder(); + var assignments = new StringBuilder(); + var tupleParameters = new StringBuilder(); + var tupleAccess = new StringBuilder(); + var defaults = new StringBuilder(); + var compOperators = new StringBuilder(); + var deConstructorParameters = new StringBuilder(); + var deConstructorAccess = new StringBuilder(); + + for (var j = 1; j <= i; j++) + { + constraints.Clear(); + fields.Clear(); + parameters.Clear(); + asserts.Clear(); + assignments.Clear(); + tupleParameters.Clear(); + tupleAccess.Clear(); + defaults.Clear(); + compOperators.Clear(); + deConstructorParameters.Clear(); + deConstructorAccess.Clear(); + + var generics = string.Join(", ", Generics(j, false)); + var nullableGenerics = string.Join(", ", Generics(j, true)); + + for (var k = 1; k <= j; k++) + { + var kStr = j == 1 ? string.Empty : k.ToString(); + fields.AppendLine($" public T{kStr} Comp{kStr};"); + constraints.Append($"where T{kStr} : IComponent? "); + parameters.Append($", T{kStr} comp{kStr}"); + asserts.AppendLine($" DebugTools.AssertOwner(owner, comp{kStr});"); + assignments.AppendLine($" Comp{kStr} = comp{kStr};"); + tupleParameters.Append($", T{kStr} Comp{kStr}"); + tupleAccess.Append($", tuple.Comp{kStr}"); + defaults.Append(", default"); + compOperators.AppendLine($$""" + public static implicit operator T{{kStr}}(Entity<{{generics}}> ent) + { + return ent.Comp{{kStr}}; + } + + """); + deConstructorParameters.Append($", out T{kStr} comp{kStr}"); + deConstructorAccess.AppendLine($" comp{kStr} = Comp{kStr};"); + } + + structs.Append($$""" + public record struct Entity<{{generics}}> + {{constraints.ToString().TrimEnd()}} + { + public EntityUid Owner; + {{fields.ToString().TrimEnd()}} + + public Entity(EntityUid owner{{parameters}}) + { + {{asserts}} + Owner = owner; + {{assignments.ToString().TrimEnd()}} + } + + public static implicit operator Entity<{{generics}}>((EntityUid Owner{{tupleParameters}}) tuple) + { + return new Entity<{{generics}}>(tuple.Owner{{tupleAccess}}); + } + + public static implicit operator Entity<{{nullableGenerics}}>(EntityUid owner) + { + return new Entity<{{nullableGenerics}}>(owner{{defaults}}); + } + + public static implicit operator EntityUid(Entity<{{generics}}> ent) + { + return ent.Owner; + } + + {{compOperators.ToString().TrimEnd()}} + + public readonly void Deconstruct(out EntityUid owner{{deConstructorParameters}}) + { + owner = Owner; + {{deConstructorAccess.ToString().TrimEnd()}} + } + } + + + """); + } + + Console.WriteLine(structs); + } +} diff --git a/Robust.UnitTesting/Shared/GameObjects/Systems/AnchoredSystemTests.cs b/Robust.UnitTesting/Shared/GameObjects/Systems/AnchoredSystemTests.cs index 79261dc5b..c28a2fdd5 100644 --- a/Robust.UnitTesting/Shared/GameObjects/Systems/AnchoredSystemTests.cs +++ b/Robust.UnitTesting/Shared/GameObjects/Systems/AnchoredSystemTests.cs @@ -1,15 +1,14 @@ +using System.Linq; +using System.Numerics; using NUnit.Framework; using Robust.Shared.Containers; using Robust.Shared.GameObjects; -using Robust.Shared.IoC; using Robust.Shared.Map; using Robust.Shared.Maths; using Robust.Shared.Physics; using Robust.Shared.Physics.Components; using Robust.Shared.Physics.Systems; using Robust.UnitTesting.Server; -using System.Linq; -using System.Numerics; // ReSharper disable AccessToStaticMemberViaDerivedType @@ -46,9 +45,9 @@ namespace Robust.UnitTesting.Shared.GameObjects.Systems mapManager.CreateMap(TestMapId); // Add grid 1, as the default grid to anchor things to. - var grid = mapManager.CreateGrid(TestMapId); + var grid = mapManager.CreateGridEntity(TestMapId); - return (sim, grid.Owner); + return (sim, grid); } // An entity is anchored to the tile it is over on the target grid. @@ -130,7 +129,7 @@ namespace Robust.UnitTesting.Shared.GameObjects.Systems var ent1 = entMan.SpawnEntity(null, coordinates); Assert.False(entMan.GetComponent(ent1).Anchored); - Assert.That(grid.GetAnchoredEntities(pos).Count() == 0); + Assert.That(!grid.GetAnchoredEntities(pos).Any()); entMan.DeleteEntity(ent1); var ent2 = entMan.CreateEntityUninitialized(null, coordinates); @@ -359,8 +358,8 @@ namespace Robust.UnitTesting.Shared.GameObjects.Systems // Act // We purposefully use the grid as container so parent stays the same, reparent will unanchor - var containerMan = entMan.AddComponent(grid.Owner); - var container = containerMan.MakeContainer("TestContainer"); + var containerMan = entMan.AddComponent(gridId); + var container = containerMan.MakeContainer(gridId, "TestContainer"); container.Insert(ent1); Assert.That(entMan.GetComponent(ent1).Anchored, Is.False); @@ -478,8 +477,8 @@ namespace Robust.UnitTesting.Shared.GameObjects.Systems var tileIndices = grid.TileIndicesFor(entMan.GetComponent(ent1).Coordinates); grid.SetTile(tileIndices, new Tile(1)); - var containerMan = entMan.AddComponent(grid.Owner); - var container = containerMan.MakeContainer("TestContainer"); + var containerMan = entMan.AddComponent(gridId); + var container = containerMan.MakeContainer(gridId, "TestContainer"); container.Insert(ent1); // Act diff --git a/Robust.UnitTesting/Shared/GameObjects/TransformComponent_Tests.cs b/Robust.UnitTesting/Shared/GameObjects/TransformComponent_Tests.cs index e72ebde82..7039f8ff7 100644 --- a/Robust.UnitTesting/Shared/GameObjects/TransformComponent_Tests.cs +++ b/Robust.UnitTesting/Shared/GameObjects/TransformComponent_Tests.cs @@ -57,19 +57,19 @@ namespace Robust.UnitTesting.Shared.GameObjects var mapManager = server.Resolve(); var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); - grid.SetTile(new Vector2i(0, 0), new Tile(1)); - var gridXform = entManager.GetComponent(grid.Owner); + var grid = mapManager.CreateGridEntity(mapId); + grid.Comp.SetTile(new Vector2i(0, 0), new Tile(1)); + var gridXform = entManager.GetComponent(grid); gridXform.LocalPosition = new Vector2(0f, 100f); - var ent1 = entManager.SpawnEntity(null, new EntityCoordinates(grid.Owner, Vector2.One * grid.TileSize / 2)); + var ent1 = entManager.SpawnEntity(null, new EntityCoordinates(grid, Vector2.One * grid.Comp.TileSize / 2)); var ent2 = entManager.SpawnEntity(null, new EntityCoordinates(ent1, Vector2.Zero)); var xform2 = entManager.GetComponent(ent2); Assert.That(xform2.WorldPosition, Is.EqualTo(new Vector2(0.5f, 100.5f))); xform2.AttachToGridOrMap(); - Assert.That(xform2.LocalPosition, Is.EqualTo(Vector2.One * grid.TileSize / 2)); + Assert.That(xform2.LocalPosition, Is.EqualTo(Vector2.One * grid.Comp.TileSize / 2)); } } } diff --git a/Robust.UnitTesting/Shared/GameState/DeletionNetworkingTests.cs b/Robust.UnitTesting/Shared/GameState/DeletionNetworkingTests.cs index 236094899..427c9d40c 100644 --- a/Robust.UnitTesting/Shared/GameState/DeletionNetworkingTests.cs +++ b/Robust.UnitTesting/Shared/GameState/DeletionNetworkingTests.cs @@ -9,11 +9,6 @@ using Robust.Shared.GameObjects; using Robust.Shared.Map; using Robust.Shared.Maths; using Robust.Shared.Network; -using Robust.Shared.Physics; -using Robust.Shared.Physics.Collision.Shapes; -using Robust.Shared.Physics.Components; -using Robust.Shared.Physics.Dynamics; -using Robust.Shared.Physics.Systems; using cIPlayerManager = Robust.Client.Player.IPlayerManager; using sIPlayerManager = Robust.Server.Player.IPlayerManager; @@ -70,14 +65,14 @@ public sealed class DeletionNetworkingTests : RobustIntegrationTest { var mapId = mapMan.CreateMap(); mapMan.GetMapEntityId(mapId); - var gridComp = mapMan.CreateGrid(mapId); - gridComp.SetTile(Vector2i.Zero, new Tile(1)); + var gridComp = mapMan.CreateGridEntity(mapId); + gridComp.Comp.SetTile(Vector2i.Zero, new Tile(1)); grid1 = gridComp.Owner; xformSys.SetLocalPosition(grid1, new Vector2(-2,0)); grid1Net = sEntMan.GetNetEntity(grid1); - gridComp = mapMan.CreateGrid(mapId); - gridComp.SetTile(Vector2i.Zero, new Tile(1)); + gridComp = mapMan.CreateGridEntity(mapId); + gridComp.Comp.SetTile(Vector2i.Zero, new Tile(1)); grid2 = gridComp.Owner; xformSys.SetLocalPosition(grid2, new Vector2(2,0)); grid2Net = sEntMan.GetNetEntity(grid2); diff --git a/Robust.UnitTesting/Shared/Map/EntityCoordinates_Tests.cs b/Robust.UnitTesting/Shared/Map/EntityCoordinates_Tests.cs index 9cb8c81f3..3984d3856 100644 --- a/Robust.UnitTesting/Shared/Map/EntityCoordinates_Tests.cs +++ b/Robust.UnitTesting/Shared/Map/EntityCoordinates_Tests.cs @@ -1,14 +1,8 @@ -using System.IO; using System.Numerics; -using Moq; using NUnit.Framework; -using Robust.Server.GameObjects; -using Robust.Server.Physics; using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Map; -using Robust.Shared.Maths; -using Robust.Shared.Physics; using Robust.Shared.Prototypes; using Robust.Shared.Serialization.Manager; @@ -142,7 +136,7 @@ namespace Robust.UnitTesting.Shared.Map var mapManager = IoCManager.Resolve(); var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); + var grid = mapManager.CreateGridEntity(mapId); var gridEnt = grid.Owner; var newEnt = entityManager.CreateEntityUninitialized(null, new EntityCoordinates(gridEnt, Vector2.Zero)); @@ -173,7 +167,7 @@ namespace Robust.UnitTesting.Shared.Map var mapManager = IoCManager.Resolve(); var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); + var grid = mapManager.CreateGridEntity(mapId); var gridEnt = grid.Owner; var newEnt = entityManager.CreateEntityUninitialized(null, new EntityCoordinates(gridEnt, Vector2.Zero)); @@ -188,10 +182,10 @@ namespace Robust.UnitTesting.Shared.Map var mapManager = IoCManager.Resolve(); var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); + var grid = mapManager.CreateGridEntity(mapId); var mapEnt = mapManager.GetMapEntityId(mapId); var gridEnt = grid.Owner; - var newEnt = entityManager.CreateEntityUninitialized(null, new EntityCoordinates(grid.Owner, Vector2.Zero)); + var newEnt = entityManager.CreateEntityUninitialized(null, new EntityCoordinates(grid, Vector2.Zero)); Assert.That(entityManager.GetComponent(mapEnt).Coordinates.EntityId, Is.EqualTo(mapEnt)); Assert.That(entityManager.GetComponent(gridEnt).Coordinates.EntityId, Is.EqualTo(mapEnt)); @@ -210,10 +204,10 @@ namespace Robust.UnitTesting.Shared.Map var mapManager = IoCManager.Resolve(); var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); + var grid = mapManager.CreateGridEntity(mapId); var mapEnt = mapManager.GetMapEntityId(mapId); var gridEnt = grid.Owner; - var newEnt = entityManager.CreateEntityUninitialized(null, new EntityCoordinates(grid.Owner, Vector2.Zero)); + var newEnt = entityManager.CreateEntityUninitialized(null, new EntityCoordinates(grid, Vector2.Zero)); var mapCoords = entityManager.GetComponent(mapEnt).Coordinates; Assert.That(mapCoords.IsValid(entityManager), Is.EqualTo(true)); @@ -261,9 +255,9 @@ namespace Robust.UnitTesting.Shared.Map var mapManager = IoCManager.Resolve(); var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); + var grid = mapManager.CreateGridEntity(mapId); var gridEnt = grid.Owner; - var newEnt = entityManager.CreateEntityUninitialized(null, new EntityCoordinates(grid.Owner, entPos)); + var newEnt = entityManager.CreateEntityUninitialized(null, new EntityCoordinates(grid, entPos)); Assert.That(IoCManager.Resolve().GetComponent(newEnt).Coordinates.ToMap(entityManager), Is.EqualTo(new MapCoordinates(entPos, mapId))); @@ -280,9 +274,9 @@ namespace Robust.UnitTesting.Shared.Map var mapId = mapManager.CreateMap(); var mapEnt = mapManager.GetMapEntityId(mapId); - var grid = mapManager.CreateGrid(mapId); + var grid = mapManager.CreateGridEntity(mapId); var gridEnt = grid.Owner; - var newEnt = entityManager.CreateEntityUninitialized(null, new EntityCoordinates(grid.Owner, Vector2.Zero)); + var newEnt = entityManager.CreateEntityUninitialized(null, new EntityCoordinates(grid, Vector2.Zero)); Assert.That(IoCManager.Resolve().GetComponent(newEnt).Coordinates.WithEntityId(mapEnt).Position, Is.EqualTo(Vector2.Zero)); diff --git a/Robust.UnitTesting/Shared/Map/GridCollision_Test.cs b/Robust.UnitTesting/Shared/Map/GridCollision_Test.cs index 0718eb18a..da5ff311b 100644 --- a/Robust.UnitTesting/Shared/Map/GridCollision_Test.cs +++ b/Robust.UnitTesting/Shared/Map/GridCollision_Test.cs @@ -1,7 +1,6 @@ using System.Threading.Tasks; using NUnit.Framework; using Robust.Shared.GameObjects; -using Robust.Shared.IoC; using Robust.Shared.Map; using Robust.Shared.Map.Components; using Robust.Shared.Maths; @@ -25,8 +24,8 @@ namespace Robust.UnitTesting.Shared.Map var physSystem = server.ResolveDependency().GetEntitySystem(); MapId mapId; - MapGridComponent? gridId1 = null; - MapGridComponent? gridId2 = null; + Entity? gridId1 = null; + Entity? gridId2 = null; PhysicsComponent? physics1 = null; PhysicsComponent? physics2 = null; EntityUid? gridEnt1; @@ -35,10 +34,10 @@ namespace Robust.UnitTesting.Shared.Map await server.WaitPost(() => { mapId = mapManager.CreateMap(); - gridId1 = mapManager.CreateGrid(mapId); - gridId2 = mapManager.CreateGrid(mapId); - gridEnt1 = gridId1.Owner; - gridEnt2 = gridId2.Owner; + gridId1 = mapManager.CreateGridEntity(mapId); + gridId2 = mapManager.CreateGridEntity(mapId); + gridEnt1 = gridId1.Value.Owner; + gridEnt2 = gridId2.Value.Owner; physics1 = entManager.GetComponent(gridEnt1.Value); physics2 = entManager.GetComponent(gridEnt2.Value); // Can't collide static bodies and grids (at time of this writing) start as static @@ -70,8 +69,8 @@ namespace Robust.UnitTesting.Shared.Map await server.WaitAssertion(() => { - gridId1?.SetTile(new Vector2i(0, 0), new Tile(1)); - gridId2?.SetTile(new Vector2i(0, 0), new Tile(1)); + gridId1?.Comp.SetTile(new Vector2i(0, 0), new Tile(1)); + gridId2?.Comp.SetTile(new Vector2i(0, 0), new Tile(1)); }); await server.WaitRunTicks(1); diff --git a/Robust.UnitTesting/Shared/Map/GridContraction_Test.cs b/Robust.UnitTesting/Shared/Map/GridContraction_Test.cs index b39b41394..0fcb868db 100644 --- a/Robust.UnitTesting/Shared/Map/GridContraction_Test.cs +++ b/Robust.UnitTesting/Shared/Map/GridContraction_Test.cs @@ -22,17 +22,17 @@ namespace Robust.UnitTesting.Shared.Map await server.WaitAssertion(() => { var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); + var grid = mapManager.CreateGridEntity(mapId); var gridEntity = grid.Owner; for (var i = 0; i < 10; i++) { - grid.SetTile(new Vector2i(i, 0), new Tile(1)); + grid.Comp.SetTile(new Vector2i(i, 0), new Tile(1)); } for (var i = 10; i >= 0; i--) { - grid.SetTile(new Vector2i(i, 0), Tile.Empty); + grid.Comp.SetTile(new Vector2i(i, 0), Tile.Empty); } Assert.That(entManager.Deleted(gridEntity)); @@ -60,19 +60,19 @@ namespace Robust.UnitTesting.Shared.Map await server.WaitAssertion(() => { var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); + var grid = mapManager.CreateGridEntity(mapId); for (var i = 0; i < 10; i++) { - grid.SetTile(new Vector2i(i, 0), new Tile(1)); + grid.Comp.SetTile(new Vector2i(i, 0), new Tile(1)); } for (var i = 10; i >= 0; i--) { - grid.SetTile(new Vector2i(i, 0), Tile.Empty); + grid.Comp.SetTile(new Vector2i(i, 0), Tile.Empty); } - Assert.That(!((!entManager.EntityExists(grid.Owner) ? EntityLifeStage.Deleted : entManager.GetComponent(grid.Owner).EntityLifeStage) >= EntityLifeStage.Deleted)); + Assert.That(!((!entManager.EntityExists(grid) ? EntityLifeStage.Deleted : entManager.GetComponent(grid).EntityLifeStage) >= EntityLifeStage.Deleted)); }); } } diff --git a/Robust.UnitTesting/Shared/Map/GridFixtures_Tests.cs b/Robust.UnitTesting/Shared/Map/GridFixtures_Tests.cs index 2e5cb147e..28e081c1b 100644 --- a/Robust.UnitTesting/Shared/Map/GridFixtures_Tests.cs +++ b/Robust.UnitTesting/Shared/Map/GridFixtures_Tests.cs @@ -30,16 +30,16 @@ namespace Robust.UnitTesting.Shared.Map await server.WaitAssertion(() => { var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); + var grid = mapManager.CreateGridEntity(mapId); // Should be nothing if grid empty - Assert.That(entManager.TryGetComponent(grid.Owner, out PhysicsComponent? gridBody)); - Assert.That(entManager.TryGetComponent(grid.Owner, out FixturesComponent? manager)); + Assert.That(entManager.TryGetComponent(grid, out PhysicsComponent? gridBody)); + Assert.That(entManager.TryGetComponent(grid, out FixturesComponent? manager)); Assert.That(manager!.FixtureCount, Is.EqualTo(0)); Assert.That(gridBody!.BodyType, Is.EqualTo(BodyType.Static)); // 1 fixture if we only ever update the 1 chunk - grid.SetTile(Vector2i.Zero, new Tile(1)); + grid.Comp.SetTile(Vector2i.Zero, new Tile(1)); Assert.That(manager.FixtureCount, Is.EqualTo(1)); // Also should only be a single tile. @@ -48,7 +48,7 @@ namespace Robust.UnitTesting.Shared.Map Assert.That(MathHelper.CloseToPercent(Box2.Area(bounds), 1.0f, 0.1f)); // Now do 2 tiles (same chunk) - grid.SetTile(new Vector2i(0, 1), new Tile(1)); + grid.Comp.SetTile(new Vector2i(0, 1), new Tile(1)); Assert.That(manager.FixtureCount, Is.EqualTo(1)); bounds = manager.Fixtures.First().Value.Shape.ComputeAABB(new Transform(Vector2.Zero, (float) Angle.Zero.Theta), 0); @@ -57,10 +57,10 @@ namespace Robust.UnitTesting.Shared.Map Assert.That(MathHelper.CloseToPercent(Box2.Area(bounds), 2.0f, 0.1f)); // If we add a new chunk should be 2 now - grid.SetTile(new Vector2i(0, -1), new Tile(1)); + grid.Comp.SetTile(new Vector2i(0, -1), new Tile(1)); Assert.That(manager.FixtureCount, Is.EqualTo(2)); - physSystem.SetLinearVelocity(grid.Owner, Vector2.One, manager: manager, body: gridBody); + physSystem.SetLinearVelocity(grid, Vector2.One, manager: manager, body: gridBody); Assert.That(gridBody.LinearVelocity.Length, Is.EqualTo(0f)); }); } diff --git a/Robust.UnitTesting/Shared/Map/GridRotation_Tests.cs b/Robust.UnitTesting/Shared/Map/GridRotation_Tests.cs index 9f717342a..958329d96 100644 --- a/Robust.UnitTesting/Shared/Map/GridRotation_Tests.cs +++ b/Robust.UnitTesting/Shared/Map/GridRotation_Tests.cs @@ -4,9 +4,7 @@ using System.Numerics; using System.Threading.Tasks; using NUnit.Framework; using Robust.Shared.GameObjects; -using Robust.Shared.IoC; using Robust.Shared.Map; -using Robust.Shared.Map.Components; using Robust.Shared.Maths; namespace Robust.UnitTesting.Shared.Map @@ -31,27 +29,27 @@ namespace Robust.UnitTesting.Shared.Map await server.WaitAssertion(() => { var mapId = mapMan.CreateMap(); - var grid = mapMan.CreateGrid(mapId); + var grid = mapMan.CreateGridEntity(mapId); var gridEnt = grid.Owner; var coordinates = new EntityCoordinates(gridEnt, new Vector2(10, 0)); // if no rotation and 0,0 position should just be the same coordinate. Assert.That(entMan.GetComponent(gridEnt).WorldRotation, Is.EqualTo(Angle.Zero)); - Assert.That(grid.WorldToLocal(coordinates.Position), Is.EqualTo(coordinates.Position)); + Assert.That(grid.Comp.WorldToLocal(coordinates.Position), Is.EqualTo(coordinates.Position)); // Rotate 180 degrees should show -10, 0 for the position in map-terms and 10, 0 for the position in entity terms (i.e. no change). entMan.GetComponent(gridEnt).WorldRotation += new Angle(MathF.PI); Assert.That(entMan.GetComponent(gridEnt).WorldRotation, Is.EqualTo(new Angle(MathF.PI))); // Check the map coordinate rotates correctly - Assert.That(grid.WorldToLocal(new Vector2(10, 0)).EqualsApprox(new Vector2(-10, 0), 0.01f)); - Assert.That(grid.LocalToWorld(coordinates.Position).EqualsApprox(new Vector2(-10, 0), 0.01f)); + Assert.That(grid.Comp.WorldToLocal(new Vector2(10, 0)).EqualsApprox(new Vector2(-10, 0), 0.01f)); + Assert.That(grid.Comp.LocalToWorld(coordinates.Position).EqualsApprox(new Vector2(-10, 0), 0.01f)); // Now we'll do the same for 180 degrees. entMan.GetComponent(gridEnt).WorldRotation += MathF.PI / 2f; // If grid facing down then worldpos of 10, 0 gets rotated 90 degrees CCW and hence should be 0, 10 - Assert.That(grid.WorldToLocal(new Vector2(10, 0)).EqualsApprox(new Vector2(0, 10), 0.01f)); + Assert.That(grid.Comp.WorldToLocal(new Vector2(10, 0)).EqualsApprox(new Vector2(0, 10), 0.01f)); // If grid facing down then local 10,0 pos should just return 0, -10 given it's aligned with the rotation. - Assert.That(grid.LocalToWorld(coordinates.Position).EqualsApprox(new Vector2(0, -10), 0.01f)); + Assert.That(grid.Comp.LocalToWorld(coordinates.Position).EqualsApprox(new Vector2(0, -10), 0.01f)); }); } @@ -70,7 +68,7 @@ namespace Robust.UnitTesting.Shared.Map await server.WaitAssertion(() => { var mapId = mapMan.CreateMap(); - var grid = mapMan.CreateGrid(mapId); + var grid = mapMan.CreateGridEntity(mapId); var gridEnt = grid.Owner; /* Test for map chunk rotations */ @@ -80,11 +78,11 @@ namespace Robust.UnitTesting.Shared.Map { for (var y = 0; y < 10; y++) { - grid.SetTile(new Vector2i(x, y), tile); + grid.Comp.SetTile(new Vector2i(x, y), tile); } } - var chunks = grid.GetMapChunks().Select(c => c.Value).ToList(); + var chunks = grid.Comp.GetMapChunks().Select(c => c.Value).ToList(); Assert.That(chunks.Count, Is.EqualTo(1)); var chunk = chunks[0]; diff --git a/Robust.UnitTesting/Shared/Map/GridSplit_Tests.cs b/Robust.UnitTesting/Shared/Map/GridSplit_Tests.cs index 42a190717..aee9e4caf 100644 --- a/Robust.UnitTesting/Shared/Map/GridSplit_Tests.cs +++ b/Robust.UnitTesting/Shared/Map/GridSplit_Tests.cs @@ -32,7 +32,8 @@ public sealed class GridSplit_Tests var sim = GetSim(); var mapManager = sim.Resolve(); var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); + var gridEnt = mapManager.CreateGridEntity(mapId); + var grid = gridEnt.Comp; grid.CanSplit = false; for (var x = 0; x < 5; x++) @@ -40,14 +41,14 @@ public sealed class GridSplit_Tests grid.SetTile(new Vector2i(x, 0), new Tile(1)); } - Assert.That(mapManager.GetAllMapGrids(mapId).Count(), Is.EqualTo(1)); + Assert.That(mapManager.GetAllGrids(mapId).Count(), Is.EqualTo(1)); grid.SetTile(new Vector2i(1, 0), Tile.Empty); - Assert.That(mapManager.GetAllMapGrids(mapId).Count(), Is.EqualTo(1)); + Assert.That(mapManager.GetAllGrids(mapId).Count(), Is.EqualTo(1)); grid.CanSplit = true; grid.SetTile(new Vector2i(2, 0), Tile.Empty); - Assert.That(mapManager.GetAllMapGrids(mapId).Count(), Is.EqualTo(2)); + Assert.That(mapManager.GetAllGrids(mapId).Count(), Is.EqualTo(2)); mapManager.DeleteMap(mapId); } @@ -58,17 +59,18 @@ public sealed class GridSplit_Tests var sim = GetSim(); var mapManager = sim.Resolve(); var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); + var gridEnt = mapManager.CreateGridEntity(mapId); + var grid = gridEnt.Comp; for (var x = 0; x < 3; x++) { grid.SetTile(new Vector2i(x, 0), new Tile(1)); } - Assert.That(mapManager.GetAllMapGrids(mapId).Count(), Is.EqualTo(1)); + Assert.That(mapManager.GetAllGrids(mapId).Count(), Is.EqualTo(1)); grid.SetTile(new Vector2i(1, 0), Tile.Empty); - Assert.That(mapManager.GetAllMapGrids(mapId).Count(), Is.EqualTo(2)); + Assert.That(mapManager.GetAllGrids(mapId).Count(), Is.EqualTo(2)); mapManager.DeleteMap(mapId); } @@ -79,7 +81,8 @@ public sealed class GridSplit_Tests var sim = GetSim(); var mapManager = sim.Resolve(); var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); + var gridEnt = mapManager.CreateGridEntity(mapId); + var grid = gridEnt.Comp; for (var x = 0; x < 3; x++) { @@ -89,16 +92,16 @@ public sealed class GridSplit_Tests } } - Assert.That(mapManager.GetAllMapGrids(mapId).Count(), Is.EqualTo(1)); + Assert.That(mapManager.GetAllGrids(mapId).Count(), Is.EqualTo(1)); grid.SetTile(Vector2i.One, Tile.Empty); - Assert.That(mapManager.GetAllMapGrids(mapId).Count(), Is.EqualTo(1)); + Assert.That(mapManager.GetAllGrids(mapId).Count(), Is.EqualTo(1)); grid.SetTile(new Vector2i(1, 2), Tile.Empty); - Assert.That(mapManager.GetAllMapGrids(mapId).Count(), Is.EqualTo(1)); + Assert.That(mapManager.GetAllGrids(mapId).Count(), Is.EqualTo(1)); grid.SetTile(new Vector2i(1, 0), Tile.Empty); - Assert.That(mapManager.GetAllMapGrids(mapId).Count(), Is.EqualTo(2)); + Assert.That(mapManager.GetAllGrids(mapId).Count(), Is.EqualTo(2)); mapManager.DeleteMap(mapId); } @@ -109,7 +112,8 @@ public sealed class GridSplit_Tests var sim = GetSim(); var mapManager = sim.Resolve(); var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); + var gridEnt = mapManager.CreateGridEntity(mapId); + var grid = gridEnt.Comp; for (var x = 0; x < 3; x++) { @@ -118,10 +122,10 @@ public sealed class GridSplit_Tests grid.SetTile(Vector2i.One, new Tile(1)); - Assert.That(mapManager.GetAllMapGrids(mapId).Count(), Is.EqualTo(1)); + Assert.That(mapManager.GetAllGrids(mapId).Count(), Is.EqualTo(1)); grid.SetTile(new Vector2i(1, 0), Tile.Empty); - Assert.That(mapManager.GetAllMapGrids(mapId).Count(), Is.EqualTo(3)); + Assert.That(mapManager.GetAllGrids(mapId).Count(), Is.EqualTo(3)); mapManager.DeleteMap(mapId); } @@ -136,27 +140,28 @@ public sealed class GridSplit_Tests var entManager = sim.Resolve(); var mapManager = sim.Resolve(); var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); + var gridEnt = mapManager.CreateGridEntity(mapId); + var grid = gridEnt.Comp; for (var x = 0; x < 4; x++) { grid.SetTile(new Vector2i(x, 0), new Tile(1)); } - Assert.That(mapManager.GetAllMapGrids(mapId).Count(), Is.EqualTo(1)); + Assert.That(mapManager.GetAllGrids(mapId).Count(), Is.EqualTo(1)); - var dummy = entManager.SpawnEntity(null, new EntityCoordinates(grid.Owner, new Vector2(3.5f, 0.5f))); + var dummy = entManager.SpawnEntity(null, new EntityCoordinates(gridEnt, new Vector2(3.5f, 0.5f))); var dummyXform = entManager.GetComponent(dummy); - var anchored = entManager.SpawnEntity(null, new EntityCoordinates(grid.Owner, new Vector2(3.5f, 0.5f))); + var anchored = entManager.SpawnEntity(null, new EntityCoordinates(gridEnt, new Vector2(3.5f, 0.5f))); var anchoredXform = entManager.GetComponent(anchored); anchoredXform.Anchored = true; Assert.That(anchoredXform.Anchored); grid.SetTile(new Vector2i(2, 0), Tile.Empty); - Assert.That(mapManager.GetAllMapGrids(mapId).Count(), Is.EqualTo(2)); + Assert.That(mapManager.GetAllGrids(mapId).Count(), Is.EqualTo(2)); - var newGrid = mapManager.GetAllMapGrids(mapId).Last(); - var newGridXform = entManager.GetComponent(newGrid.Owner); + var newGrid = mapManager.GetAllGrids(mapId).Last(); + var newGridXform = entManager.GetComponent(newGrid); Assert.Multiple(() => { diff --git a/Robust.UnitTesting/Shared/Map/MapGridMap_Tests.cs b/Robust.UnitTesting/Shared/Map/MapGridMap_Tests.cs index 0fd055214..5ebd9cc62 100644 --- a/Robust.UnitTesting/Shared/Map/MapGridMap_Tests.cs +++ b/Robust.UnitTesting/Shared/Map/MapGridMap_Tests.cs @@ -41,7 +41,7 @@ public sealed class MapGridMap_Tests var mapManager = sim.Resolve(); var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); + mapManager.CreateGridEntity(mapId); Assert.DoesNotThrow(() => { diff --git a/Robust.UnitTesting/Shared/Map/MapManager_Tests.cs b/Robust.UnitTesting/Shared/Map/MapManager_Tests.cs index b0c5e5bf4..f8463e009 100644 --- a/Robust.UnitTesting/Shared/Map/MapManager_Tests.cs +++ b/Robust.UnitTesting/Shared/Map/MapManager_Tests.cs @@ -1,11 +1,9 @@ +using System.Numerics; using NUnit.Framework; using Robust.Shared.GameObjects; using Robust.Shared.Map; -using Robust.Shared.Maths; -using Robust.UnitTesting.Server; -using System.Management; -using System.Numerics; using Robust.Shared.Map.Components; +using Robust.UnitTesting.Server; namespace Robust.UnitTesting.Shared.Map { @@ -49,11 +47,11 @@ namespace Robust.UnitTesting.Shared.Map var mapID = new MapId(11); mapMan.CreateMap(mapID); - var grid = mapMan.CreateGrid(mapID); + var grid = mapMan.CreateGridEntity(mapID); mapMan.Restart(); - Assert.That(mapMan.GridExists(grid.Owner), Is.False); + Assert.That(mapMan.GridExists(grid), Is.False); } /// diff --git a/Robust.UnitTesting/Shared/Map/MapPauseTests.cs b/Robust.UnitTesting/Shared/Map/MapPauseTests.cs index 317b88c15..7ece14055 100644 --- a/Robust.UnitTesting/Shared/Map/MapPauseTests.cs +++ b/Robust.UnitTesting/Shared/Map/MapPauseTests.cs @@ -141,10 +141,10 @@ internal sealed class MapPauseTests mapMan.SetMapPaused(mapId, true); // act - var newGrid = mapMan.CreateGrid(mapId); + var newGrid = mapMan.CreateGridEntity(mapId); // assert - var metaData = entMan.GetComponent(newGrid.Owner); + var metaData = entMan.GetComponent(newGrid); Assert.That(metaData.EntityPaused, Is.True); } diff --git a/Robust.UnitTesting/Shared/Physics/BroadphaseNetworkingTest.cs b/Robust.UnitTesting/Shared/Physics/BroadphaseNetworkingTest.cs index fede28915..63f17ae8c 100644 --- a/Robust.UnitTesting/Shared/Physics/BroadphaseNetworkingTest.cs +++ b/Robust.UnitTesting/Shared/Physics/BroadphaseNetworkingTest.cs @@ -64,9 +64,9 @@ public sealed class BroadphaseNetworkingTest : RobustIntegrationTest { var mapId = mapMan.CreateMap(); map1 = mapMan.GetMapEntityId(mapId); - var gridComp = mapMan.CreateGrid(mapId); - gridComp.SetTile(Vector2i.Zero, new Tile(1)); - grid1 = gridComp.Owner; + var gridEnt = mapMan.CreateGridEntity(mapId); + gridEnt.Comp.SetTile(Vector2i.Zero, new Tile(1)); + grid1 = gridEnt.Owner; }); var map1Net = sEntMan.GetNetEntity(map1); @@ -136,9 +136,9 @@ public sealed class BroadphaseNetworkingTest : RobustIntegrationTest // Create grid var mapId = mapMan.CreateMap(); map2 = mapMan.GetMapEntityId(mapId); - var gridComp = mapMan.CreateGrid(mapId); - gridComp.SetTile(Vector2i.Zero, new Tile(1)); - grid2 = gridComp.Owner; + var gridEnt = mapMan.CreateGridEntity(mapId); + gridEnt.Comp.SetTile(Vector2i.Zero, new Tile(1)); + grid2 = gridEnt.Owner; // Move player var coords = new EntityCoordinates(grid2, Vector2.Zero); diff --git a/Robust.UnitTesting/Shared/Physics/Broadphase_Test.cs b/Robust.UnitTesting/Shared/Physics/Broadphase_Test.cs index 7a32abfa5..5143efb8f 100644 --- a/Robust.UnitTesting/Shared/Physics/Broadphase_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/Broadphase_Test.cs @@ -1,8 +1,6 @@ using System.Linq; using System.Numerics; using NUnit.Framework; -using Robust.Client.UserInterface.CustomControls; -using Robust.Server.GameStates; using Robust.Shared.GameObjects; using Robust.Shared.Map; using Robust.Shared.Maths; @@ -30,13 +28,13 @@ public sealed class Broadphase_Test var mapId = mapManager.CreateMap(); var mapEnt = mapManager.GetMapEntityId(mapId); - var grid = mapManager.CreateGrid(mapId); + var grid = mapManager.CreateGridEntity(mapId); - grid.SetTile(Vector2i.Zero, new Tile(1)); - Assert.That(entManager.HasComponent(grid.Owner)); - var broadphase = entManager.GetComponent(grid.Owner); + grid.Comp.SetTile(Vector2i.Zero, new Tile(1)); + Assert.That(entManager.HasComponent(grid)); + var broadphase = entManager.GetComponent(grid); - var ent = entManager.SpawnEntity(null, new EntityCoordinates(grid.Owner, new Vector2(0.5f, 0.5f))); + var ent = entManager.SpawnEntity(null, new EntityCoordinates(grid, new Vector2(0.5f, 0.5f))); var xform = entManager.GetComponent(ent); Assert.That(broadphase.SundriesTree, Does.Contain(ent)); @@ -65,10 +63,10 @@ public sealed class Broadphase_Test var mapId = mapManager.CreateMap(); var mapEnt = mapManager.GetMapEntityId(mapId); - var grid = mapManager.CreateGrid(mapId); + var grid = mapManager.CreateGridEntity(mapId); var gridUid = grid.Owner; - grid.SetTile(Vector2i.Zero, new Tile(1)); + grid.Comp.SetTile(Vector2i.Zero, new Tile(1)); Assert.That(entManager.HasComponent(gridUid)); var broadphase = entManager.GetComponent(gridUid); @@ -114,10 +112,10 @@ public sealed class Broadphase_Test var mapId1 = mapManager.CreateMap(); var mapId2 = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId1); - var xform = entManager.GetComponent(grid.Owner); + var grid = mapManager.CreateGridEntity(mapId1); + var xform = entManager.GetComponent(grid); - grid.SetTile(Vector2i.Zero, new Tile(1)); + grid.Comp.SetTile(Vector2i.Zero, new Tile(1)); var mapBroadphase1 = entManager.GetComponent(mapManager.GetMapEntityId(mapId1)); var mapBroadphase2 = entManager.GetComponent(mapManager.GetMapEntityId(mapId2)); entManager.TickUpdate(0.016f, false); @@ -146,15 +144,15 @@ public sealed class Broadphase_Test var lookup = system.GetEntitySystem(); var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); + var grid = mapManager.CreateGridEntity(mapId); - grid.SetTile(Vector2i.Zero, new Tile(1)); - var gridBroadphase = entManager.GetComponent(grid.Owner); + grid.Comp.SetTile(Vector2i.Zero, new Tile(1)); + var gridBroadphase = entManager.GetComponent(grid); var mapBroadphase = entManager.GetComponent(mapManager.GetMapEntityId(mapId)); Assert.That(entManager.EntityQuery(true).Count(), Is.EqualTo(2)); - var parent = entManager.SpawnEntity(null, new EntityCoordinates(grid.Owner, new Vector2(0.5f, 0.5f))); + var parent = entManager.SpawnEntity(null, new EntityCoordinates(grid, new Vector2(0.5f, 0.5f))); var child1 = entManager.SpawnEntity(null, new EntityCoordinates(parent, Vector2.Zero)); var child1Xform = entManager.GetComponent(child1); @@ -173,7 +171,7 @@ public sealed class Broadphase_Test Assert.That(lookup.FindBroadphase(child1), Is.EqualTo(gridBroadphase)); // They should get deparented to the map and updated to the map's broadphase instead. - grid.SetTile(Vector2i.Zero, Tile.Empty); + grid.Comp.SetTile(Vector2i.Zero, Tile.Empty); Assert.That(lookup.FindBroadphase(parent), Is.EqualTo(mapBroadphase)); Assert.That(lookup.FindBroadphase(child1), Is.EqualTo(mapBroadphase)); Assert.That(lookup.FindBroadphase(child2), Is.EqualTo(mapBroadphase)); @@ -203,16 +201,16 @@ public sealed class Broadphase_Test var mapB = mapManager.GetMapEntityId(mapBId); // setup grids - var gridAComp = mapManager.CreateGrid(mapAId); - var gridBComp = mapManager.CreateGrid(mapBId); - var gridCComp = mapManager.CreateGrid(mapAId); + var gridAComp = mapManager.CreateGridEntity(mapAId); + var gridBComp = mapManager.CreateGridEntity(mapBId); + var gridCComp = mapManager.CreateGridEntity(mapAId); var gridA = gridAComp.Owner; var gridB = gridBComp.Owner; var gridC = gridCComp.Owner; xforms.SetLocalPosition(gridC, new Vector2(10, 10)); - gridAComp.SetTile(Vector2i.Zero, new Tile(1)); - gridBComp.SetTile(Vector2i.Zero, new Tile(1)); - gridCComp.SetTile(Vector2i.Zero, new Tile(1)); + gridAComp.Comp.SetTile(Vector2i.Zero, new Tile(1)); + gridBComp.Comp.SetTile(Vector2i.Zero, new Tile(1)); + gridCComp.Comp.SetTile(Vector2i.Zero, new Tile(1)); // set up test entities var parent = entManager.SpawnEntity(null, new EntityCoordinates(mapA, new Vector2(200,200))); diff --git a/Robust.UnitTesting/Shared/Physics/CollisionWake_Test.cs b/Robust.UnitTesting/Shared/Physics/CollisionWake_Test.cs index aa3cd7a7c..d2d7aa246 100644 --- a/Robust.UnitTesting/Shared/Physics/CollisionWake_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/CollisionWake_Test.cs @@ -1,12 +1,11 @@ using System.Numerics; +using System.Threading.Tasks; using NUnit.Framework; -using Robust.Client.UserInterface.CustomControls; using Robust.Shared.GameObjects; using Robust.Shared.Map; +using Robust.Shared.Map.Components; using Robust.Shared.Maths; using Robust.Shared.Physics.Components; -using System.Threading.Tasks; -using Robust.Shared.Map.Components; namespace Robust.UnitTesting.Shared.Physics { @@ -44,7 +43,7 @@ namespace Robust.UnitTesting.Shared.Physics var entManager = server.ResolveDependency(); var mapManager = server.ResolveDependency(); - MapGridComponent grid = default!; + Entity grid = default!; MapId mapId = default!; PhysicsComponent entityOnePhysics = default!; TransformComponent xform = default!; @@ -53,15 +52,15 @@ namespace Robust.UnitTesting.Shared.Physics await server.WaitPost(() => { mapId = mapManager.CreateMap(); - grid = mapManager.CreateGrid(mapId); - grid.SetTile(Vector2i.Zero, new Tile(1)); + grid = mapManager.CreateGridEntity(mapId); + grid.Comp.SetTile(Vector2i.Zero, new Tile(1)); var entityOne = entManager.SpawnEntity("CollisionWakeTestItem", new MapCoordinates(Vector2.One * 2f, mapId)); entityOnePhysics = entManager.GetComponent(entityOne); xform = entManager.GetComponent(entityOne); Assert.That(xform.ParentUid == mapManager.GetMapEntityId(mapId)); - var entityTwo = entManager.SpawnEntity("CollisionWakeTestItem", new EntityCoordinates(grid.Owner, new Vector2(0.5f, 0.5f))); + var entityTwo = entManager.SpawnEntity("CollisionWakeTestItem", new EntityCoordinates(grid, new Vector2(0.5f, 0.5f))); entityTwoPhysics = entManager.GetComponent(entityTwo); Assert.That(entManager.GetComponent(entityTwo).ParentUid == grid.Owner); @@ -76,7 +75,7 @@ namespace Robust.UnitTesting.Shared.Physics Assert.That(entityOnePhysics.CanCollide, Is.EqualTo(true)); xform.LocalPosition = new Vector2(0.5f, 0.5f); - xform.AttachParent(grid.Owner); + xform.AttachParent(grid); // Entity 2 should immediately not be collidable on spawn Assert.That(entityTwoPhysics.Awake, Is.EqualTo(false)); diff --git a/Robust.UnitTesting/Shared/Physics/GridDeletion_Test.cs b/Robust.UnitTesting/Shared/Physics/GridDeletion_Test.cs index 99ffe9d6d..27d3121c6 100644 --- a/Robust.UnitTesting/Shared/Physics/GridDeletion_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridDeletion_Test.cs @@ -31,17 +31,17 @@ public sealed class GridDeletion_Test : RobustIntegrationTest PhysicsComponent physics = default!; - MapGridComponent grid = default!; + Entity grid = default!; MapId mapId = default!; await server.WaitAssertion(() => { mapId = mapManager.CreateMap(); - grid = mapManager.CreateGrid(mapId); + grid = mapManager.CreateGridEntity(mapId); - physics = entManager.GetComponent(grid.Owner); - physSystem.SetBodyType(grid.Owner, BodyType.Dynamic, body: physics); - physSystem.SetLinearVelocity(grid.Owner, new Vector2(50f, 0f), body: physics); + physics = entManager.GetComponent(grid); + physSystem.SetBodyType(grid, BodyType.Dynamic, body: physics); + physSystem.SetLinearVelocity(grid, new Vector2(50f, 0f), body: physics); Assert.That(physics.LinearVelocity.Length, NUnit.Framework.Is.GreaterThan(0f)); }); @@ -50,7 +50,7 @@ public sealed class GridDeletion_Test : RobustIntegrationTest await server.WaitAssertion(() => { Assert.That(physics.LinearVelocity.Length, NUnit.Framework.Is.GreaterThan(0f)); - entManager.DeleteEntity(grid.Owner); + entManager.DeleteEntity(grid); // So if gridtree is fucky then this SHOULD throw. foreach (var _ in mapManager.FindGridsIntersecting(mapId, diff --git a/Robust.UnitTesting/Shared/Physics/GridMovement_Test.cs b/Robust.UnitTesting/Shared/Physics/GridMovement_Test.cs index 4f9cafa66..d8ccf5896 100644 --- a/Robust.UnitTesting/Shared/Physics/GridMovement_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridMovement_Test.cs @@ -32,14 +32,14 @@ public sealed class GridMovement_Test : RobustIntegrationTest await server.WaitAssertion(() => { var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); + var grid = mapManager.CreateGridEntity(mapId); // Setup 1 body on grid, 1 body off grid, and assert that it's all gucci. - grid.SetTile(Vector2i.Zero, new Tile(1)); - var fixtures = entManager.GetComponent(grid.Owner); + grid.Comp.SetTile(Vector2i.Zero, new Tile(1)); + var fixtures = entManager.GetComponent(grid); Assert.That(fixtures.FixtureCount, Is.EqualTo(1)); - var onGrid = entManager.SpawnEntity(null, new EntityCoordinates(grid.Owner, 0.5f, 0.5f )); + var onGrid = entManager.SpawnEntity(null, new EntityCoordinates(grid, 0.5f, 0.5f )); var onGridBody = entManager.AddComponent(onGrid); physSystem.SetBodyType(onGrid, BodyType.Dynamic, body: onGridBody); var shapeA = new PolygonShape(); diff --git a/Robust.UnitTesting/Shared/Physics/MapVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/MapVelocity_Test.cs index af01ffa3c..4dd3ea6a0 100644 --- a/Robust.UnitTesting/Shared/Physics/MapVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/MapVelocity_Test.cs @@ -1,12 +1,11 @@ using System.Numerics; +using System.Threading.Tasks; using NUnit.Framework; using Robust.Shared.GameObjects; using Robust.Shared.Map; -using Robust.Shared.Maths; using Robust.Shared.Physics; using Robust.Shared.Physics.Components; using Robust.Shared.Physics.Systems; -using System.Threading.Tasks; namespace Robust.UnitTesting.Shared.Physics { @@ -43,8 +42,8 @@ namespace Robust.UnitTesting.Shared.Physics await server.WaitAssertion(() => { var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); - var grid2 = mapManager.CreateGrid(mapId); + var grid = mapManager.CreateGridEntity(mapId); + var grid2 = mapManager.CreateGridEntity(mapId); var gridUidA = grid.Owner; Assert.That(entityManager.TryGetComponent(gridUidA, out var gridPhysics)); @@ -53,7 +52,7 @@ namespace Robust.UnitTesting.Shared.Physics Vector2 offset = new(3, 4); Vector2 expectedFinalVelocity = new Vector2(-4, 3) * 2 + Vector2.One; - var dummy = entityManager.SpawnEntity(DummyEntity, new EntityCoordinates(grid.Owner, offset)); + var dummy = entityManager.SpawnEntity(DummyEntity, new EntityCoordinates(grid, offset)); Assert.That(entityManager.TryGetComponent(dummy, out PhysicsComponent? body)); Assert.That(entityManager.TryGetComponent(dummy, out TransformComponent? xform)); xformSystem.SetParent(dummy, xform!, gridUidA); @@ -85,7 +84,7 @@ namespace Robust.UnitTesting.Shared.Physics Assert.That(velocities.Item2, Is.Approximately(angularVelocity, 1e-6)); // Check that velocity does not change when changing parent - xformSystem.SetParent(dummy, xform!, grid2.Owner); + xformSystem.SetParent(dummy, xform!, grid2); linearVelocity = physicsSys.GetMapLinearVelocity(dummy, body); angularVelocity = physicsSys.GetMapAngularVelocity(dummy, body); velocities = physicsSys.GetMapVelocities(dummy, body); @@ -112,7 +111,7 @@ namespace Robust.UnitTesting.Shared.Physics await server.WaitAssertion(() => { var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); + var grid = mapManager.CreateGridEntity(mapId); var gridUid = grid.Owner; Assert.That(entityManager.TryGetComponent(gridUid, out var gridPhysics)); diff --git a/Robust.UnitTesting/Shared/Physics/RecursiveUpdateTest.cs b/Robust.UnitTesting/Shared/Physics/RecursiveUpdateTest.cs index 6e97fd567..e9cb8bb60 100644 --- a/Robust.UnitTesting/Shared/Physics/RecursiveUpdateTest.cs +++ b/Robust.UnitTesting/Shared/Physics/RecursiveUpdateTest.cs @@ -8,6 +8,8 @@ using Robust.Shared.Maths; using Robust.Shared.Physics; using Robust.UnitTesting.Server; +// ReSharper disable AccessToStaticMemberViaDerivedType + namespace Robust.UnitTesting.Shared.Physics; [TestFixture] @@ -26,9 +28,9 @@ public sealed class RecursiveUpdateTest var containers = entManager.System(); var mapId = mapManager.CreateMap(); - var grid = mapManager.CreateGrid(mapId); + var grid = mapManager.CreateGridEntity(mapId); var guid = grid.Owner; - grid.SetTile(Vector2i.Zero, new Tile(1)); + grid.Comp.SetTile(Vector2i.Zero, new Tile(1)); Assert.That(entManager.HasComponent(guid)); var broadphase = entManager.GetComponent(guid); @@ -214,9 +216,9 @@ public sealed class RecursiveUpdateTest Assert.That(ents, Does.Contain(child)); // Try again, but this time with a parent change. - var grid = mapManager.CreateGrid(mapId); + var grid = mapManager.CreateGridEntity(mapId); var guid = grid.Owner; - grid.SetTile(Vector2i.Zero, new Tile(1)); + grid.Comp.SetTile(Vector2i.Zero, new Tile(1)); var gridBroadphase = entManager.GetComponent(guid); var gridBroadData = new BroadphaseData(guid, EntityUid.Invalid, false, false);