From c4d2478d89be8aaa0cda1e2f3d929e1a6bd844d4 Mon Sep 17 00:00:00 2001 From: Ed Date: Mon, 7 Oct 2024 13:55:47 +0300 Subject: [PATCH] cut out old zlevel system --- .../Components/CP14StationAbyssComponent.cs | 1 - .../Components/CP14StationZLevelsComponent.cs | 28 ++-- .../EntitySystems/CP14StationAbyssSystem.cs | 76 +++++----- .../CP14StationZLevelsSystem.AutoPortal.cs | 44 ++++-- .../EntitySystems/CP14StationZLevelsSystem.cs | 142 +++++++----------- 5 files changed, 128 insertions(+), 163 deletions(-) diff --git a/Content.Server/_CP14/StationDungeonMap/Components/CP14StationAbyssComponent.cs b/Content.Server/_CP14/StationDungeonMap/Components/CP14StationAbyssComponent.cs index 0e08207b3d..e07e8c3b6a 100644 --- a/Content.Server/_CP14/StationDungeonMap/Components/CP14StationAbyssComponent.cs +++ b/Content.Server/_CP14/StationDungeonMap/Components/CP14StationAbyssComponent.cs @@ -1,6 +1,5 @@ using Content.Server._CP14.StationDungeonMap.EntitySystems; using Content.Shared._CP14.StationZLevels; -using Content.Shared.Destructible.Thresholds; namespace Content.Server._CP14.StationDungeonMap.Components; diff --git a/Content.Server/_CP14/StationDungeonMap/Components/CP14StationZLevelsComponent.cs b/Content.Server/_CP14/StationDungeonMap/Components/CP14StationZLevelsComponent.cs index de408cd316..1c16129166 100644 --- a/Content.Server/_CP14/StationDungeonMap/Components/CP14StationZLevelsComponent.cs +++ b/Content.Server/_CP14/StationDungeonMap/Components/CP14StationZLevelsComponent.cs @@ -5,24 +5,22 @@ using Robust.Shared.Utility; namespace Content.Server._CP14.StationDungeonMap.Components; /// -/// Initializes the z-level system by creating a series of linked maps +/// allows you to control an array of maps linked to each other by z-levels /// -[RegisterComponent, Access(typeof(CP14StationZLevelsSystem))] -public sealed partial class CP14StationZLevelsComponent : Component +[RegisterComponent] +public sealed partial class CP14ZLevelGroupComponent : Component { - [DataField(required: true)] - public int DefaultMapLevel = 0; - - [DataField(required: true)] - public Dictionary Levels = new(); - - public bool Initialized = false; - - public Dictionary LevelEntities = new(); + [DataField] + public Dictionary Levels = new(); } -[DataRecord, Serializable] -public sealed class CP14ZLevelEntry +/// +/// +/// +[RegisterComponent] +public sealed partial class CP14ZLevelElementComponent : Component { - public ResPath? Path { get; set; } = null; + [DataField] + public Entity? Group = null; } + diff --git a/Content.Server/_CP14/StationDungeonMap/EntitySystems/CP14StationAbyssSystem.cs b/Content.Server/_CP14/StationDungeonMap/EntitySystems/CP14StationAbyssSystem.cs index a38a6170e2..fcdd519283 100644 --- a/Content.Server/_CP14/StationDungeonMap/EntitySystems/CP14StationAbyssSystem.cs +++ b/Content.Server/_CP14/StationDungeonMap/EntitySystems/CP14StationAbyssSystem.cs @@ -18,48 +18,44 @@ public sealed partial class CP14StationAbyssSystem : EntitySystem base.Initialize(); - SubscribeLocalEvent(OnStationPostInit); + //SubscribeLocalEvent(OnStationPostInit); } - public override void Update(float frameTime) - { - base.Update(frameTime); + //public override void Update(float frameTime) + //{ + // base.Update(frameTime); +// + // var query = new EntityQueryEnumerator(); + // while (query.MoveNext(out var uid, out var abyss, out var zLevel)) + // { + // if (_timing.CurTime < abyss.NextReloadTime && abyss.NextReloadTime != TimeSpan.Zero) + // continue; +// + // abyss.NextReloadTime = _timing.CurTime + _random.Next(abyss.MinReloadTime, abyss.MaxReloadTime); +// + // ReloadAbyssNow((uid, abyss)); + // } + //} - var query = new EntityQueryEnumerator(); - while (query.MoveNext(out var uid, out var abyss, out var zLevel)) - { - if (_timing.CurTime < abyss.NextReloadTime && abyss.NextReloadTime != TimeSpan.Zero) - continue; + //private void OnStationPostInit(Entity abyss, ref StationPostInitEvent args) + //{ + // abyss.Comp.NextReloadTime = _timing.CurTime + _random.Next(abyss.Comp.MinReloadTime, abyss.Comp.MaxReloadTime); + // ReloadAbyssNow(abyss); + //} - abyss.NextReloadTime = _timing.CurTime + _random.Next(abyss.MinReloadTime, abyss.MaxReloadTime); - - ReloadAbyssNow((uid, abyss)); - } - } - - private void OnStationPostInit(Entity abyss, ref StationPostInitEvent args) - { - abyss.Comp.NextReloadTime = _timing.CurTime + _random.Next(abyss.Comp.MinReloadTime, abyss.Comp.MaxReloadTime); - ReloadAbyssNow(abyss); - } - - /// - /// Start process of removing old layers, and then generate new - /// - /// - public void ReloadAbyssNow(Entity abyss) - { - if (!TryComp(abyss, out var zLevelComp)) - return; - - foreach (var level in abyss.Comp.Levels) - { - var floor = _random.Pick(level.Value); - if (!_proto.TryIndex(floor, out var indexedZLevel)) - return; - var resPath = _random.Pick(indexedZLevel.Maps); - - _zLevels.SetZLevel((abyss, zLevelComp), level.Key, resPath, true); - } - } + //public void ReloadAbyssNow(Entity abyss) + //{ + // if (!TryComp(abyss, out var zLevelComp)) + // return; +// + // foreach (var level in abyss.Comp.Levels) + // { + // var floor = _random.Pick(level.Value); + // if (!_proto.TryIndex(floor, out var indexedZLevel)) + // return; + // var resPath = _random.Pick(indexedZLevel.Maps); +// + // _zLevels.SetZLevel((abyss, zLevelComp), level.Key, resPath, true); + // } + //} } diff --git a/Content.Server/_CP14/StationDungeonMap/EntitySystems/CP14StationZLevelsSystem.AutoPortal.cs b/Content.Server/_CP14/StationDungeonMap/EntitySystems/CP14StationZLevelsSystem.AutoPortal.cs index bb7b23cf04..cc4738befe 100644 --- a/Content.Server/_CP14/StationDungeonMap/EntitySystems/CP14StationZLevelsSystem.AutoPortal.cs +++ b/Content.Server/_CP14/StationDungeonMap/EntitySystems/CP14StationZLevelsSystem.AutoPortal.cs @@ -1,4 +1,5 @@ using Content.Server._CP14.StationDungeonMap.Components; +using Content.Server.GameTicking.Events; using Robust.Shared.Map; namespace Content.Server._CP14.StationDungeonMap.EntitySystems; @@ -7,9 +8,19 @@ public sealed partial class CP14StationZLevelsSystem { private void AutoPortalInitialize() { + SubscribeLocalEvent(OnRoundStart); SubscribeLocalEvent(OnPortalMapInit); } + private void OnRoundStart(RoundStartingEvent ev) + { + var query = EntityQueryEnumerator(); + while (query.MoveNext(out var uid, out var portal)) + { + InitPortal((uid, portal)); + } + } + private void OnPortalMapInit(Entity autoPortal, ref MapInitEvent args) { InitPortal(autoPortal); @@ -17,21 +28,22 @@ public sealed partial class CP14StationZLevelsSystem private void InitPortal(Entity autoPortal) { - var mapId = Transform(autoPortal).MapUid; - if (mapId is null) - return; - - var offsetMap = GetMapOffset(mapId.Value, autoPortal.Comp.ZLevelOffset); - - if (offsetMap is null) - return; - - var currentWorldPos = _transform.GetWorldPosition(autoPortal); - var targetMapPos = new MapCoordinates(currentWorldPos, offsetMap.Value); - - var otherSidePortal = Spawn(autoPortal.Comp.OtherSideProto, targetMapPos); - - if (_linkedEntity.TryLink(autoPortal, otherSidePortal, true)) - RemComp(autoPortal); + QueueDel(autoPortal); + //var mapId = Transform(autoPortal).MapUid; + //if (mapId is null) + // return; +// + //var offsetMap = GetMapOffset(mapId.Value, autoPortal.Comp.ZLevelOffset); +// + //if (offsetMap is null) + // return; +// + //var currentWorldPos = _transform.GetWorldPosition(autoPortal); + //var targetMapPos = new MapCoordinates(currentWorldPos, offsetMap.Value); +// + //var otherSidePortal = Spawn(autoPortal.Comp.OtherSideProto, targetMapPos); +// + //if (_linkedEntity.TryLink(autoPortal, otherSidePortal, true)) + // RemComp(autoPortal); } } diff --git a/Content.Server/_CP14/StationDungeonMap/EntitySystems/CP14StationZLevelsSystem.cs b/Content.Server/_CP14/StationDungeonMap/EntitySystems/CP14StationZLevelsSystem.cs index c35e30c063..99176f00ad 100644 --- a/Content.Server/_CP14/StationDungeonMap/EntitySystems/CP14StationZLevelsSystem.cs +++ b/Content.Server/_CP14/StationDungeonMap/EntitySystems/CP14StationZLevelsSystem.cs @@ -1,15 +1,10 @@ using Content.Server._CP14.StationDungeonMap.Components; -using Content.Server.GameTicking.Events; using Content.Server.Station.Components; -using Content.Server.Station.Events; using Content.Server.Station.Systems; -using Content.Shared.Maps; -using Content.Shared.Station.Components; using Content.Shared.Teleportation.Systems; using Robust.Server.GameObjects; using Robust.Server.Maps; using Robust.Shared.Map; -using Robust.Shared.Random; using Robust.Shared.Utility; namespace Content.Server._CP14.StationDungeonMap.EntitySystems; @@ -26,114 +21,79 @@ public sealed partial class CP14StationZLevelsSystem : EntitySystem { base.Initialize(); AutoPortalInitialize(); - - SubscribeLocalEvent(OnRoundStart); - SubscribeLocalEvent(OnStationPostInit); } - private void OnRoundStart(RoundStartingEvent ev) + private bool TryAddMapInZLevelGroup(Entity zLevelGroup, int zLevel, MapId mapId) { - var query = EntityQueryEnumerator(); - while (query.MoveNext(out var uid, out var portal)) - { - InitPortal((uid, portal)); - } + if (!_map.MapExists(mapId)) + return false; + + var mapUid = _map.GetMap(mapId); + if (!zLevelGroup.Comp.Levels.TryAdd(zLevel, mapId)) + return false; + + var lvlElement = EnsureComp(mapUid); + + lvlElement.Group = zLevelGroup; + return true; } - private void OnStationPostInit(Entity ent, ref StationPostInitEvent args) + private void RemoveMapFromZLevelGroup(Entity zLevelGroup, int zLevel) { - if (ent.Comp.Initialized) + if (!zLevelGroup.Comp.Levels.ContainsKey(zLevel)) return; - if (!TryComp(ent, out StationDataComponent? dataComp)) - { - Log.Error($"Failed to init CP14StationZLevelsSystem: no StationData"); - return; - } + var map = zLevelGroup.Comp.Levels[zLevel]; - var defaultMap = _station.GetLargestGrid(dataComp); + var mapUid = _map.GetMap(map); - if (defaultMap is null) - { - Log.Error($"Failed to init CP14StationZLevelsSystem: defaultMap is null"); - return; - } - - ent.Comp.LevelEntities.Add(Transform(defaultMap.Value).MapID, ent.Comp.DefaultMapLevel); - - ent.Comp.Initialized = true; - - foreach (var (zLevel, level) in ent.Comp.Levels) - { - var path = level.Path.ToString(); - if (level.Path is null) - { - Log.Error($"path {path} for CP14StationZLevelsSystem at level {zLevel} don't exist!"); - continue; - } - - SetZLevel(ent, zLevel, level.Path.Value); - } + RemCompDeferred(mapUid); + zLevelGroup.Comp.Levels.Remove(zLevel); } - public MapId? SetZLevel(Entity ent, int zLevel, ResPath resPath, bool force = false) + private MapId? GetZLevelMap(Entity zLevelGroup, int zLevel) { - if (ent.Comp.LevelEntities.ContainsValue(zLevel)) - { - if (!force) - { - Log.Error($"Key duplication for CP14StationZLevelsSystem at level {zLevel}!"); - return null; - } - - foreach (var (key, value) in ent.Comp.LevelEntities) - { - if (value == zLevel && _map.MapExists(key)) - { - ent.Comp.LevelEntities.Remove(key); - Del(_map.GetMap(key)); - } - } - } - - var mapUid = _map.CreateMap(out var mapId); - var member = EnsureComp(mapUid); - member.Station = ent; - - Log.Info($"Created map {mapId} for CP14StationZLevelsSystem at level {zLevel}"); - - var options = new MapLoadOptions { LoadMap = true }; - - if (!_mapLoader.TryLoad(mapId, resPath.ToString(), out var grids, options)) - { - Log.Error($"Failed to load map for CP14StationZLevelsSystem at level {zLevel}!"); - Del(mapUid); + if (!zLevelGroup.Comp.Levels.TryGetValue(zLevel, out var mapId)) return null; - } - ent.Comp.LevelEntities.TryAdd(mapId, zLevel); return mapId; } - public MapId? GetMapOffset(EntityUid mapUid, int offset) + private Entity CreateZLevelGroup(Dictionary? maps = null) { - var query = EntityQueryEnumerator(); - while (query.MoveNext(out var uid, out var zLevel, out _)) + var groupEntity = Spawn(null, MapCoordinates.Nullspace); + var groupComp = AddComp(groupEntity); + + if (maps is not null) { - if (!zLevel.LevelEntities.TryGetValue(Transform(mapUid).MapID, out var currentLevel)) - continue; - - var targetLevel = currentLevel + offset; - - if (!zLevel.LevelEntities.ContainsValue(targetLevel)) - continue; - - foreach (var (key, value) in zLevel.LevelEntities) + foreach (var (level, map) in maps) { - if (value == targetLevel && _map.MapExists(key)) - return key; + TryAddMapInZLevelGroup((groupEntity, groupComp), level, map); } } - return null; + + return (groupEntity, groupComp); } + + //public MapId? GetMapOffset(EntityUid mapUid, int offset) + //{ + // var query = EntityQueryEnumerator(); + // while (query.MoveNext(out var uid, out var zLevel, out _)) + // { + // if (!zLevel.LevelEntities.TryGetValue(Transform(mapUid).MapID, out var currentLevel)) + // continue; +// + // var targetLevel = currentLevel + offset; +// + // if (!zLevel.LevelEntities.ContainsValue(targetLevel)) + // continue; +// + // foreach (var (key, value) in zLevel.LevelEntities) + // { + // if (value == targetLevel && _map.MapExists(key)) + // return key; + // } + // } + // return null; + //} }