From af05332b36a0b05314ac57fd32b88cec7fdcc14a Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Tue, 3 Aug 2021 18:49:25 +1000 Subject: [PATCH] Random offset for DefaultGrid every round (#4411) * Random offset for DefaultGrid every round This is useful to make coders aware of entitycoordinates and mapcoordinates being different and to help spot problems early. It also puts the onus of fixing positioning bugs back onto the original coder rather than someone else if they happen to spot it. * Fix clickable test * Fix entitysystemextensions --- .../Tests/ClickableTest.cs | 22 +++++++++++++------ .../Utility/EntitySystemExtensionsTest.cs | 4 +++- .../GameTicking/GameTicker.RoundFlow.cs | 6 +++++ Content.Shared/CCVar/CCVars.cs | 6 +++++ .../Spawning/EntitySystemExtensions.cs | 2 +- 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/Content.IntegrationTests/Tests/ClickableTest.cs b/Content.IntegrationTests/Tests/ClickableTest.cs index ed29ec9d5b..7ac1f71eef 100644 --- a/Content.IntegrationTests/Tests/ClickableTest.cs +++ b/Content.IntegrationTests/Tests/ClickableTest.cs @@ -1,11 +1,14 @@ using System; using System.Threading.Tasks; using Content.Client.Clickable; +using Content.Server.GameTicking; using NUnit.Framework; using Robust.Server.GameObjects; using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Map; +using Robust.Shared.Maths; +using Robust.Shared.Timing; namespace Content.IntegrationTests.Tests { @@ -57,12 +60,19 @@ namespace Content.IntegrationTests.Tests [TestCase("ClickTestRotatingCornerInvisibleNoRot", 0.25f, 0.25f, DirSouthEastJustShy, 1, ExpectedResult = true)] public async Task Test(string prototype, float clickPosX, float clickPosY, double angle, float scale) { + Vector2? worldPos = null; EntityUid entity = default; + var clientEntManager = _client.ResolveDependency(); + var serverEntManager = _server.ResolveDependency(); + var mapManager = _server.ResolveDependency(); + var gameTicker = _server.ResolveDependency().GetEntitySystem(); await _server.WaitPost(() => { - var entMgr = IoCManager.Resolve(); - var ent = entMgr.SpawnEntity(prototype, new MapCoordinates(0, 0, new MapId(1))); + var gridEnt = mapManager.GetGrid(gameTicker.DefaultGridId).GridEntityId; + worldPos = serverEntManager.GetEntity(gridEnt).Transform.WorldPosition; + + var ent = serverEntManager.SpawnEntity(prototype, new EntityCoordinates(gridEnt, 0f, 0f)); ent.Transform.LocalRotation = angle; ent.GetComponent().Scale = (scale, scale); entity = ent.Uid; @@ -75,17 +85,15 @@ namespace Content.IntegrationTests.Tests await _client.WaitPost(() => { - var entMgr = IoCManager.Resolve(); - var ent = entMgr.GetEntity(entity); + var ent = clientEntManager.GetEntity(entity); var clickable = ent.GetComponent(); - hit = clickable.CheckClick((clickPosX, clickPosY), out _, out _); + hit = clickable.CheckClick((clickPosX, clickPosY) + worldPos!.Value, out _, out _); }); await _server.WaitPost(() => { - var entMgr = IoCManager.Resolve(); - entMgr.DeleteEntity(entity); + serverEntManager.DeleteEntity(entity); }); return hit; diff --git a/Content.IntegrationTests/Tests/Utility/EntitySystemExtensionsTest.cs b/Content.IntegrationTests/Tests/Utility/EntitySystemExtensionsTest.cs index b33ccc3a57..b810b42e5d 100644 --- a/Content.IntegrationTests/Tests/Utility/EntitySystemExtensionsTest.cs +++ b/Content.IntegrationTests/Tests/Utility/EntitySystemExtensionsTest.cs @@ -48,6 +48,8 @@ namespace Content.IntegrationTests.Tests.Utility var mapId = new MapId(1); var grid = sMapManager.GetGrid(new GridId(1)); grid.SetTile(new Vector2i(0, 0), new Tile(1)); + var gridEnt = sEntityManager.GetEntity(grid.GridEntityId); + var gridPos = gridEnt.Transform.WorldPosition; var entityCoordinates = new EntityCoordinates(grid.GridEntityId, 0, 0); // Nothing blocking it, only entity is the grid @@ -55,7 +57,7 @@ namespace Content.IntegrationTests.Tests.Utility Assert.True(sEntityManager.TrySpawnIfUnobstructed(null, entityCoordinates, CollisionGroup.Impassable, out var entity)); Assert.NotNull(entity); - var mapCoordinates = new MapCoordinates(0, 0, mapId); + var mapCoordinates = new MapCoordinates(gridPos.X, gridPos.Y, mapId); // Nothing blocking it, only entity is the grid Assert.NotNull(sEntityManager.SpawnIfUnobstructed(null, mapCoordinates, CollisionGroup.Impassable)); diff --git a/Content.Server/GameTicking/GameTicker.RoundFlow.cs b/Content.Server/GameTicking/GameTicker.RoundFlow.cs index e47c36c58a..a7e8223bb9 100644 --- a/Content.Server/GameTicking/GameTicker.RoundFlow.cs +++ b/Content.Server/GameTicking/GameTicker.RoundFlow.cs @@ -11,6 +11,7 @@ using Robust.Server.Player; using Robust.Shared.IoC; using Robust.Shared.Localization; using Robust.Shared.Log; +using Robust.Shared.Maths; using Robust.Shared.Player; using Robust.Shared.Random; using Robust.Shared.Timing; @@ -62,6 +63,11 @@ namespace Content.Server.GameTicking throw new InvalidOperationException($"No grid found for map {map}"); } + var maxStationOffset = _configurationManager.GetCVar(CCVars.MaxStationOffset); + var x = _robustRandom.NextFloat() * maxStationOffset * 2 - maxStationOffset; + var y = _robustRandom.NextFloat() * maxStationOffset * 2 - maxStationOffset; + _entityManager.GetEntity(grid.GridEntityId).Transform.LocalPosition = new Vector2(x, y); + DefaultGridId = grid.Index; _spawnPoint = grid.ToCoordinates(); diff --git a/Content.Shared/CCVar/CCVars.cs b/Content.Shared/CCVar/CCVars.cs index 3b2091492c..c4b1cd403c 100644 --- a/Content.Shared/CCVar/CCVars.cs +++ b/Content.Shared/CCVar/CCVars.cs @@ -55,6 +55,12 @@ namespace Content.Shared.CCVar public static readonly CVarDef GameMap = CVarDef.Create("game.map", "Maps/saltern.yml", CVar.SERVERONLY); + /// + /// When the default blueprint is loaded what is the maximum amount it can be offset from 0,0. + /// + public static readonly CVarDef MaxStationOffset = + CVarDef.Create("game.maxstationoffset", 1000.0f); + /// /// When enabled, guests will be assigned permanent UIDs and will have their preferences stored. /// diff --git a/Content.Shared/Spawning/EntitySystemExtensions.cs b/Content.Shared/Spawning/EntitySystemExtensions.cs index 6ec0226358..a096d0f4d9 100644 --- a/Content.Shared/Spawning/EntitySystemExtensions.cs +++ b/Content.Shared/Spawning/EntitySystemExtensions.cs @@ -32,7 +32,7 @@ namespace Content.Shared.Spawning in Box2? box = null, SharedBroadphaseSystem? collision = null) { - var boxOrDefault = box.GetValueOrDefault(Box2.UnitCentered); + var boxOrDefault = box.GetValueOrDefault(Box2.UnitCentered).Translated(coordinates.Position); collision ??= EntitySystem.Get(); foreach (var body in collision.GetCollidingEntities(coordinates.MapId, in boxOrDefault))