Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Z-Levels #1

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Content.Client/Entry/IgnoredComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static class IgnoredComponents
"Construction",
"PoweredLight",
"Smes",
"Ladder",
"LightBulb",
"Healing",
"Material",
Expand Down
4 changes: 4 additions & 0 deletions Content.Server/GameTicking/GameTicker.CVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public partial class GameTicker
[ViewVariables]
public float MaxStationOffset { get; private set; } = 0f;

[ViewVariables]
public string ZLevels { get; private set; } = CCVars.ZLevels.DefaultValue;

private void InitializeCVars()
{
_configurationManager.OnValueChanged(CCVars.GameLobbyEnabled, value => LobbyEnabled = value, true);
Expand All @@ -37,6 +40,7 @@ private void InitializeCVars()
value => { DisallowLateJoin = value; UpdateLateJoinStatus(); UpdateJobsAvailable(); }, true);
_configurationManager.OnValueChanged(CCVars.StationOffset, value => StationOffset = value, true);
_configurationManager.OnValueChanged(CCVars.MaxStationOffset, value => MaxStationOffset = value, true);
_configurationManager.OnValueChanged(CCVars.ZLevels, value => ZLevels = value, true);
}
}
}
85 changes: 69 additions & 16 deletions Content.Server/GameTicking/GameTicker.RoundFlow.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Content.Server.Players;
using Content.Shared.CCVar;
Expand All @@ -17,6 +18,7 @@
using Robust.Shared.Timing;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
using YamlDotNet.RepresentationModel;

namespace Content.Server.GameTicking
{
Expand Down Expand Up @@ -53,29 +55,80 @@ private set

private void PreRoundSetup()
{
DefaultMap = _mapManager.CreateMap();

var startTime = _gameTiming.RealTime;
var map = ChosenMap;
var grid = _mapLoader.LoadBlueprint(DefaultMap, map);

var zLevels = ZLevels;

if (grid == null)
var zLevelsDoc = new YamlDocument(zLevels);
TextReader reader;
var resPath = new ResourcePath(zLevels).ToRootedPath();
if (_resMan.TryContentFileRead(resPath, out var contentReader))
{
throw new InvalidOperationException($"No grid found for map {map}");
}
reader = new StreamReader(contentReader);
var stream = new YamlStream();
stream.Load(reader);

if (StationOffset)
{
// Apply a random offset to the station grid entity.
var x = _robustRandom.NextFloat() * MaxStationOffset * 2 - MaxStationOffset;
var y = _robustRandom.NextFloat() * MaxStationOffset * 2 - MaxStationOffset;
EntityManager.GetEntity(grid.GridEntityId).Transform.LocalPosition = new Vector2(x, y);
var root = stream.Documents[0].RootNode as YamlMappingNode;
if (root != null)
{
var levels = root.GetNode<YamlMappingNode>("levels");
int counter = 0;
while (levels.TryGetNode<YamlScalarNode>(counter.ToString(), out var node)) {
var mapName = node.AsString();

var mapZ = _mapManager.CreateMap();
var gridZ = _mapLoader.LoadBlueprint(mapZ, mapName);

if (gridZ == null)
{
throw new InvalidOperationException("No grid found for the Z-Level map");
}

MapsZ.Add(mapZ);
GridsZ.Add(gridZ.Index);

counter++;
}

var firstGrid = _mapManager.GetGrid(GridsZ[0]);

_spawnPoint = firstGrid.ToCoordinates();
}
else
{
throw new InvalidOperationException("Please fill your ZLevels file. Location: " + zLevels);
}
}
else
{
Logger.Warning("Z LEVELS NOT FOUND! Loading default map...");

DefaultMap = _mapManager.CreateMap();
var map = ChosenMap;

DefaultGridId = grid.Index;
_spawnPoint = grid.ToCoordinates();

var timeSpan = _gameTiming.RealTime - startTime;
Logger.InfoS("ticker", $"Loaded map in {timeSpan.TotalMilliseconds:N2}ms.");
var grid = _mapLoader.LoadBlueprint(DefaultMap, map);

if (grid == null)
{
throw new InvalidOperationException($"No grid found for map {map}");
}

if (StationOffset)
{
// Apply a random offset to the station grid entity.
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();

var timeSpan = _gameTiming.RealTime - startTime;
Logger.InfoS("ticker", $"Loaded map in {timeSpan.TotalMilliseconds:N2}ms.");
}
}

public void StartRound(bool force = false)
Expand Down
6 changes: 6 additions & 0 deletions Content.Server/GameTicking/GameTicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Robust.Server.Maps;
using Robust.Server.ServerStatus;
using Robust.Shared.Configuration;
using Robust.Shared.ContentPack;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
Expand All @@ -17,6 +18,7 @@
using Robust.Shared.Timing;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
using System.Collections.Generic;

namespace Content.Server.GameTicking
{
Expand All @@ -28,6 +30,9 @@ public partial class GameTicker : SharedGameTicker
[ViewVariables] public MapId DefaultMap { get; private set; }
[ViewVariables] public GridId DefaultGridId { get; private set; }

[ViewVariables] public List<MapId> MapsZ { get; private set; } = new();
[ViewVariables] public List<GridId> GridsZ { get; private set; } = new();

public override void Initialize()
{
base.Initialize();
Expand Down Expand Up @@ -85,5 +90,6 @@ public override void Update(float frameTime)
[Dependency] private readonly IBaseServer _baseServer = default!;
[Dependency] private readonly IWatchdogApi _watchdogApi = default!;
[Dependency] private readonly IReflectionManager _reflectionManager = default!;
[Dependency] private readonly IResourceManager _resMan = default!;
}
}
36 changes: 35 additions & 1 deletion Content.Server/Gravity/EntitySystems/GravitySystem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using Content.Server.Camera;
using Content.Server.GameTicking;
using Content.Shared.Gravity;
using Content.Shared.Sound;
using JetBrains.Annotations;
Expand Down Expand Up @@ -42,13 +43,29 @@ private void HandleGeneratorUpdate(GravityGeneratorUpdateEvent ev)
if (ev.GridId == GridId.Invalid) return;

var gravity = EntityManager.GetComponent<GravityComponent>(_mapManager.GetGrid(ev.GridId).GridEntityId);
var gameTicker = EntitySystem.Get<GameTicker>();

if (ev.Status == GravityGeneratorStatus.On)
{
EnableGravity(gravity);
}
else
{
if (gameTicker.GridsZ.Contains(ev.GridId))
{
foreach (var grid in gameTicker.GridsZ)
{
var gridEnt = EntityManager.GetEntity(_mapManager.GetGrid(grid).GridEntityId);
if (gridEnt.TryGetComponent<GravityComponent>(out var grav))
{
if (grav.Enabled)
{
EnableGravity(gravity);
return;
}
}
}
}
DisableGravity(gravity);
}
}
Expand All @@ -61,7 +78,24 @@ private void HandleGravityInitialize(EntityUid uid, GravityComponent component,

foreach (var generator in EntityManager.EntityQuery<GravityGeneratorComponent>(true))
{
if (generator.Owner.Transform.GridID == gridId && generator.Status == GravityGeneratorStatus.On)
var gameTicker = EntitySystem.Get<GameTicker>();
var enable = false;
if (gameTicker.GridsZ.Contains(generator.Owner.Transform.GridID))
{
foreach (var grid in gameTicker.GridsZ)
{
var gridEnt = EntityManager.GetEntity(_mapManager.GetGrid(grid).GridEntityId);
if (gridEnt.TryGetComponent<GravityComponent>(out var grav))
{
if (grav.Enabled)
{
enable = true;
break;
}
}
}
}
if ((generator.Owner.Transform.GridID == gridId) && generator.Status == GravityGeneratorStatus.On)
{
component.Enabled = true;
message = new GravityChangedMessage(gridId, true);
Expand Down
67 changes: 67 additions & 0 deletions Content.Server/Maps/LadderComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Content.Shared.Interaction;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.IoC;
using Content.Server.GameTicking;
using Robust.Shared.Maths;
using Robust.Shared.Map;
using Content.Server.DoAfter;

namespace Content.Server.Maps
{
[RegisterComponent]
class LadderComponent : Component, IInteractHand
{
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IEntitySystemManager _sysManager = default!;
public override string Name => "Ladder";

[DataField("down", required: true)]
private bool _down = false;

bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
{
eventArgs.User.Transform.WorldPosition = Owner.Transform.WorldPosition;
var gameTicker = _sysManager.GetEntitySystem<GameTicker>();

if (!gameTicker.MapsZ.Contains(Owner.Transform.MapID)) return false;

// RIP DoAfter. No more async :'(
/*var doAfterSystem = EntitySystem.Get<DoAfterSystem>();

var doAfterArgs = new DoAfterEventArgs(eventArgs.User, 1.5f, default, Owner)
{
BreakOnTargetMove = true,
BreakOnUserMove = true,
BreakOnDamage = true,
BreakOnStun = true,
NeedHand = true,
};

var result = await doAfterSystem.WaitDoAfter(doAfterArgs);

if (result == DoAfterStatus.Cancelled) return false;*/

var index = gameTicker.MapsZ.IndexOf(Owner.Transform.MapID);
var mapChange = Owner.Transform.MapID;
if (_down)
{
if (index + 1 > gameTicker.MapsZ.Count) return false;
mapChange = gameTicker.MapsZ[index + 1];
}
else
{
if (index - 1 < 0) return false;
mapChange = gameTicker.MapsZ[index - 1];
}
eventArgs.User.Transform.AttachParent(_mapManager.GetMapEntity(mapChange));

return true;
}
}
}
2 changes: 1 addition & 1 deletion Content.Server/NodeContainer/Nodes/AdjacentNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override IEnumerable<Node> GetReachableNodes()
var grid = IoCManager.Resolve<IMapManager>().GetGrid(Owner.Transform.GridID);
var gridIndex = grid.TileIndicesFor(Owner.Transform.Coordinates);

foreach (var (_, node) in NodeHelpers.GetCardinalNeighborNodes(entMan, grid, gridIndex))
foreach (var (_, node) in NodeHelpers.GetCardinalNeighborNodes(entMan, Owner.Transform.GridID, gridIndex))
{
if (node != this)
yield return node;
Expand Down
21 changes: 18 additions & 3 deletions Content.Server/NodeContainer/Nodes/NodeHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Content.Server.GameTicking;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;

Expand All @@ -28,7 +30,7 @@ public static IEnumerable<Node> GetNodesInTile(IEntityManager entMan, IMapGrid g

public static IEnumerable<(Direction dir, Node node)> GetCardinalNeighborNodes(
IEntityManager entMan,
IMapGrid grid,
GridId grid,
Vector2i coords,
bool includeSameTile = true)
{
Expand All @@ -46,10 +48,12 @@ public static IEnumerable<Node> GetNodesInTile(IEntityManager entMan, IMapGrid g

[SuppressMessage("ReSharper", "EnforceForeachStatementBraces")]
public static IEnumerable<(Direction dir, EntityUid entity)> GetCardinalNeighborCells(
IMapGrid grid,
GridId gridid,
Vector2i coords,
bool includeSameTile = true)
{
var mapMan = IoCManager.Resolve<IMapManager>();
var grid = mapMan.GetGrid(gridid);
if (includeSameTile)
{
foreach (var uid in grid.GetAnchoredEntities(coords))
Expand All @@ -67,6 +71,17 @@ public static IEnumerable<Node> GetNodesInTile(IEntityManager entMan, IMapGrid g

foreach (var uid in grid.GetAnchoredEntities(coords + (-1, 0)))
yield return (Direction.West, uid);

var ticker = EntitySystem.Get<GameTicker>();
var index = ticker.GridsZ.IndexOf(gridid);
if (index - 1 > 0)
{
var upperGrid = mapMan.GetGrid(ticker.GridsZ[index - 1]);
foreach (var uid in upperGrid.GetAnchoredEntities(coords))
{
yield return (Direction.Invalid, uid);
}
}
}

public static Vector2i TileOffsetForDir(Direction dir)
Expand Down
1 change: 1 addition & 0 deletions Content.Server/Power/Components/CableComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Robust.Shared.ViewVariables;
using Robust.Shared.Map;

namespace Content.Server.Power.Components
{
Expand Down
4 changes: 2 additions & 2 deletions Content.Server/Power/Nodes/CableNode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using Content.Server.NodeContainer.Nodes;
using Content.Server.Power.EntitySystems;
using Robust.Shared.GameObjects;
Expand Down Expand Up @@ -26,7 +26,7 @@ public override IEnumerable<Node> GetReachableNodes()
var terminalDirs = 0;
List<(Direction, Node)> nodeDirs = new();

foreach (var (dir, node) in NodeHelpers.GetCardinalNeighborNodes(entMan, grid, gridIndex))
foreach (var (dir, node) in NodeHelpers.GetCardinalNeighborNodes(entMan, Owner.Transform.GridID, gridIndex))
{
if (node is CableNode && node != this)
{
Expand Down
Loading