Compare commits

...

3 Commits

Author SHA1 Message Date
Ed
c4d2478d89 cut out old zlevel system 2024-10-07 13:55:47 +03:00
Ed
52d8e8b00f brute zlevel reloading 2024-10-07 00:07:47 +03:00
Ed
5475758e65 start random zlevel 2024-10-06 22:49:30 +03:00
6 changed files with 250 additions and 127 deletions

View File

@@ -0,0 +1,38 @@
using Content.Server._CP14.StationDungeonMap.EntitySystems;
using Content.Shared._CP14.StationZLevels;
namespace Content.Server._CP14.StationDungeonMap.Components;
/// <summary>
/// Creates a chain of z-levels overloading with time
/// </summary>
[RegisterComponent, Access(typeof(CP14StationAbyssSystem))]
public sealed partial class CP14StationAbyssComponent : Component
{
[DataField]
public Dictionary<int, HashSet<CP14ZLevelPrototype>> Levels = new();
[DataField]
public TimeSpan MinReloadTime = TimeSpan.FromMinutes(5);
[DataField]
public TimeSpan MaxReloadTime = TimeSpan.FromMinutes(30);
[DataField]
public TimeSpan NextReloadTime = TimeSpan.Zero;
// Telegraphy and delays
[DataField]
public TimeSpan PreReloadAlertTime = TimeSpan.FromSeconds(120f);
//[DataField]
//public AbyssState Status = AbyssState.Generating;
}
public enum AbyssState : byte
{
Generating = 0,
Waiting = 1,
Destroying = 2,
}

View File

@@ -5,24 +5,22 @@ using Robust.Shared.Utility;
namespace Content.Server._CP14.StationDungeonMap.Components;
/// <summary>
/// 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
/// </summary>
[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<int, CP14ZLevelEntry> Levels = new();
public bool Initialized = false;
public Dictionary<MapId, int> LevelEntities = new();
[DataField]
public Dictionary<int, MapId> Levels = new();
}
[DataRecord, Serializable]
public sealed class CP14ZLevelEntry
/// <summary>
///
/// </summary>
[RegisterComponent]
public sealed partial class CP14ZLevelElementComponent : Component
{
public ResPath? Path { get; set; } = null;
[DataField]
public Entity<CP14ZLevelGroupComponent>? Group = null;
}

View File

@@ -0,0 +1,61 @@
using Content.Server._CP14.StationDungeonMap.Components;
using Content.Server.Station.Events;
using Content.Shared._CP14.StationZLevels;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Timing;
namespace Content.Server._CP14.StationDungeonMap.EntitySystems;
public sealed partial class CP14StationAbyssSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly CP14StationZLevelsSystem _zLevels = default!;
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly IRobustRandom _random = default!;
public override void Initialize()
{
base.Initialize();
//SubscribeLocalEvent<CP14StationAbyssComponent, StationPostInitEvent>(OnStationPostInit);
}
//public override void Update(float frameTime)
//{
// base.Update(frameTime);
//
// var query = new EntityQueryEnumerator<CP14StationAbyssComponent, CP14StationZLevelsComponent>();
// 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));
// }
//}
//private void OnStationPostInit(Entity<CP14StationAbyssComponent> abyss, ref StationPostInitEvent args)
//{
// abyss.Comp.NextReloadTime = _timing.CurTime + _random.Next(abyss.Comp.MinReloadTime, abyss.Comp.MaxReloadTime);
// ReloadAbyssNow(abyss);
//}
//public void ReloadAbyssNow(Entity<CP14StationAbyssComponent> abyss)
//{
// if (!TryComp<CP14StationZLevelsComponent>(abyss, out var zLevelComp))
// return;
//
// foreach (var level in abyss.Comp.Levels)
// {
// var floor = _random.Pick(level.Value);
// if (!_proto.TryIndex<CP14ZLevelPrototype>(floor, out var indexedZLevel))
// return;
// var resPath = _random.Pick(indexedZLevel.Maps);
//
// _zLevels.SetZLevel((abyss, zLevelComp), level.Key, resPath, true);
// }
//}
}

View File

@@ -0,0 +1,49 @@
using Content.Server._CP14.StationDungeonMap.Components;
using Content.Server.GameTicking.Events;
using Robust.Shared.Map;
namespace Content.Server._CP14.StationDungeonMap.EntitySystems;
public sealed partial class CP14StationZLevelsSystem
{
private void AutoPortalInitialize()
{
SubscribeLocalEvent<RoundStartingEvent>(OnRoundStart);
SubscribeLocalEvent<CP14ZLevelAutoPortalComponent, MapInitEvent>(OnPortalMapInit);
}
private void OnRoundStart(RoundStartingEvent ev)
{
var query = EntityQueryEnumerator<CP14ZLevelAutoPortalComponent>();
while (query.MoveNext(out var uid, out var portal))
{
InitPortal((uid, portal));
}
}
private void OnPortalMapInit(Entity<CP14ZLevelAutoPortalComponent> autoPortal, ref MapInitEvent args)
{
InitPortal(autoPortal);
}
private void InitPortal(Entity<CP14ZLevelAutoPortalComponent> 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<CP14ZLevelAutoPortalComponent>(autoPortal);
}
}

View File

@@ -1,15 +1,11 @@
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;
@@ -19,129 +15,85 @@ public sealed partial class CP14StationZLevelsSystem : EntitySystem
[Dependency] private readonly StationSystem _station = default!;
[Dependency] private readonly MapLoaderSystem _mapLoader = default!;
[Dependency] private readonly TransformSystem _transform = default!;
[Dependency] private readonly ITileDefinitionManager _tileDefManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly TileSystem _tile = default!;
[Dependency] private readonly SharedMapSystem _maps = default!;
[Dependency] private readonly LinkedEntitySystem _linkedEntity = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<CP14ZLevelAutoPortalComponent, MapInitEvent>(OnPortalMapInit);
SubscribeLocalEvent<RoundStartingEvent>(OnRoundStart);
SubscribeLocalEvent<CP14StationZLevelsComponent, StationPostInitEvent>(OnStationPostInit);
AutoPortalInitialize();
}
private void OnRoundStart(RoundStartingEvent ev)
private bool TryAddMapInZLevelGroup(Entity<CP14ZLevelGroupComponent> zLevelGroup, int zLevel, MapId mapId)
{
var query = EntityQueryEnumerator<CP14ZLevelAutoPortalComponent>();
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<CP14ZLevelElementComponent>(mapUid);
lvlElement.Group = zLevelGroup;
return true;
}
private void OnStationPostInit(Entity<CP14StationZLevelsComponent> ent, ref StationPostInitEvent args)
private void RemoveMapFromZLevelGroup(Entity<CP14ZLevelGroupComponent> zLevelGroup, int zLevel)
{
if (ent.Comp.Initialized)
if (!zLevelGroup.Comp.Levels.ContainsKey(zLevel))
return;
if (!TryComp(ent, out StationDataComponent? dataComp))
var map = zLevelGroup.Comp.Levels[zLevel];
var mapUid = _map.GetMap(map);
RemCompDeferred<CP14ZLevelElementComponent>(mapUid);
zLevelGroup.Comp.Levels.Remove(zLevel);
}
private MapId? GetZLevelMap(Entity<CP14ZLevelGroupComponent> zLevelGroup, int zLevel)
{
if (!zLevelGroup.Comp.Levels.TryGetValue(zLevel, out var mapId))
return null;
return mapId;
}
private Entity<CP14ZLevelGroupComponent> CreateZLevelGroup(Dictionary<int, MapId>? maps = null)
{
var groupEntity = Spawn(null, MapCoordinates.Nullspace);
var groupComp = AddComp<CP14ZLevelGroupComponent>(groupEntity);
if (maps is not null)
{
Log.Error($"Failed to init CP14StationZLevelsSystem: no StationData");
return;
}
var defaultMap = _station.GetLargestGrid(dataComp);
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 (map, level) in ent.Comp.Levels)
{
if (ent.Comp.LevelEntities.ContainsValue(map))
foreach (var (level, map) in maps)
{
Log.Error($"Key duplication for CP14StationZLevelsSystem at level {map}!");
continue;
}
var path = level.Path.ToString();
if (path is null)
{
Log.Error($"path {path} for CP14StationZLevelsSystem at level {map} don't exist!");
continue;
}
var mapUid = _map.CreateMap(out var mapId);
var member = EnsureComp<StationMemberComponent>(mapUid);
member.Station = ent;
Log.Info($"Created map {mapId} for CP14StationZLevelsSystem at level {map}");
var options = new MapLoadOptions { LoadMap = true };
if (!_mapLoader.TryLoad(mapId, path, out var grids, options))
{
Log.Error($"Failed to load map for CP14StationZLevelsSystem at level {map}!");
Del(mapUid);
continue;
}
ent.Comp.LevelEntities.Add(mapId, map);
}
}
private void OnPortalMapInit(Entity<CP14ZLevelAutoPortalComponent> autoPortal, ref MapInitEvent args)
{
InitPortal(autoPortal);
}
private void InitPortal(Entity<CP14ZLevelAutoPortalComponent> 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<CP14ZLevelAutoPortalComponent>(autoPortal);
}
public MapId? GetMapOffset(EntityUid mapUid, int offset)
{
var query = EntityQueryEnumerator<CP14StationZLevelsComponent, StationDataComponent>();
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;
TryAddMapInZLevelGroup((groupEntity, groupComp), level, map);
}
}
return null;
return (groupEntity, groupComp);
}
//public MapId? GetMapOffset(EntityUid mapUid, int offset)
//{
// var query = EntityQueryEnumerator<CP14StationZLevelsComponent, StationDataComponent>();
// 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;
//}
}

View File

@@ -0,0 +1,25 @@
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Shared._CP14.StationZLevels;
/// <summary>
/// Prototype “floor type”. Refers to a certain group of maps united by a common style and some common data
/// </summary>
[Prototype("zLevel")]
public sealed partial class CP14ZLevelPrototype : IPrototype
{
[IdDataField] public string ID { get; } = default!;
/// <summary>
/// the name of the floor that players can see.
/// </summary>
[DataField(required: true)]
public LocId Name = string.Empty;
/// <summary>
/// all possible floor layouts
/// </summary>
[DataField]
public HashSet<ResPath> Maps = new();
}