From 639db84a0bb3f91e629e91ad7f4727c560948b3e Mon Sep 17 00:00:00 2001 From: Ed <96445749+TheShuEd@users.noreply.github.com> Date: Sun, 5 Jan 2025 19:08:35 +0300 Subject: [PATCH] dayCycle prototipization (#694) --- .../_CP14/DayCycle/CP14DayCycleSystem.cs | 36 +++++++++++++------ .../Commands/CP14AddTimeEntryCommand.cs | 1 + .../Commands/CP14InitDayCycleCommand.cs | 5 ++- .../Commands/CP14SetTimeEntryCommand.cs | 7 ++-- .../DayCycle/CP14SharedDayCycleSystem.cs | 1 + .../Components/CP14DayCycleComponent.cs | 35 +++++++++++------- .../CP14DayCyclePeriodPrototype.cs | 2 +- .../Prototypes/CP14DayCyclePrototype.cs | 14 ++++++++ .../MagicRitual/Requirements/RequiredTime.cs | 1 + .../_CP14/Random/Rules/IsDaylight.cs | 6 +++- Resources/Maps/_CP14/comoss.yml | 21 ----------- Resources/Maps/_CP14/dev_map.yml | 21 ----------- Resources/Maps/_CP14/island.yml | 21 ----------- Resources/Maps/_CP14/tavern.yml | 21 ----------- Resources/Maps/_CP14/template.yml | 21 ----------- .../Prototypes/_CP14/DayCycle/periods.yml | 21 +++++++++++ .../Demiplane/Modifiers/MapLight/mapLight.yml | 11 ++++++ 17 files changed, 113 insertions(+), 132 deletions(-) rename Content.Shared/_CP14/DayCycle/{ => Prototypes}/CP14DayCyclePeriodPrototype.cs (83%) create mode 100644 Content.Shared/_CP14/DayCycle/Prototypes/CP14DayCyclePrototype.cs diff --git a/Content.Server/_CP14/DayCycle/CP14DayCycleSystem.cs b/Content.Server/_CP14/DayCycle/CP14DayCycleSystem.cs index e9dcc7006c..655d3aaed5 100644 --- a/Content.Server/_CP14/DayCycle/CP14DayCycleSystem.cs +++ b/Content.Server/_CP14/DayCycle/CP14DayCycleSystem.cs @@ -1,6 +1,7 @@ using Content.Shared._CP14.DayCycle; using Content.Shared._CP14.DayCycle.Components; using Robust.Shared.Map.Components; +using Robust.Shared.Prototypes; using Robust.Shared.Random; using Robust.Shared.Timing; @@ -13,6 +14,7 @@ public sealed partial class CP14DayCycleSystem : CP14SharedDayCycleSystem [Dependency] private readonly IGameTiming _timing = default!; [Dependency] private readonly IRobustRandom _random = default!; + [Dependency] private readonly IPrototypeManager _proto = default!; public override void Initialize() { @@ -23,10 +25,12 @@ public sealed partial class CP14DayCycleSystem : CP14SharedDayCycleSystem private void OnMapInitDayCycle(Entity dayCycle, ref MapInitEvent args) { + dayCycle.Comp.IndexedCycle = _proto.Index(dayCycle.Comp.CycleProto); + Init(dayCycle); - if (dayCycle.Comp.StartWithRandomEntry && dayCycle.Comp.TimeEntries.Count > 1) - SetTimeEntry(dayCycle, _random.Next(dayCycle.Comp.TimeEntries.Count)); + if (dayCycle.Comp.StartWithRandomEntry && dayCycle.Comp.IndexedCycle.TimeEntries.Count > 1) + SetTimeEntry(dayCycle, _random.Next(dayCycle.Comp.IndexedCycle.TimeEntries.Count)); } public override void Update(float frameTime) @@ -38,7 +42,10 @@ public sealed partial class CP14DayCycleSystem : CP14SharedDayCycleSystem { var entity = new Entity(uid, dayCycle, mapLight); - if (dayCycle.TimeEntries.Count < MinTimeEntryCount) + if (dayCycle.IndexedCycle is null) + continue; + + if (dayCycle.IndexedCycle.TimeEntries.Count < MinTimeEntryCount) continue; SetAmbientColor((entity, entity), GetCurrentColor(entity, _timing.CurTime.TotalSeconds)); @@ -52,32 +59,41 @@ public sealed partial class CP14DayCycleSystem : CP14SharedDayCycleSystem public void Init(Entity dayCycle) { - if (dayCycle.Comp.TimeEntries.Count < MinTimeEntryCount) + if (dayCycle.Comp.IndexedCycle is null) + return; + + if (dayCycle.Comp.IndexedCycle.TimeEntries.Count < MinTimeEntryCount) { - Log.Warning($"Attempting to init a daily cycle with the number of time entries less than {MinTimeEntryCount}"); + Log.Warning($"Attempting to init a day/night cycle with the number of time entries less than {MinTimeEntryCount}"); return; } dayCycle.Comp.CurrentTimeEntryIndex = 0; dayCycle.Comp.EntryStartTime = _timing.CurTime; - dayCycle.Comp.EntryEndTime = _timing.CurTime + dayCycle.Comp.CurrentTimeEntry.Duration; + dayCycle.Comp.EntryEndTime = _timing.CurTime + dayCycle.Comp.CurrentTimeEntry!.Value.Duration; Dirty(dayCycle); } public void AddTimeEntry(Entity dayCycle, DayCycleEntry entry) { - dayCycle.Comp.TimeEntries.Add(entry); + if (dayCycle.Comp.IndexedCycle is null) + return; + + dayCycle.Comp.IndexedCycle.TimeEntries.Add(entry); Dirty(dayCycle); } public void SetTimeEntry(Entity dayCycle, int nextEntry) { - nextEntry = Math.Clamp(nextEntry, 0, dayCycle.Comp.TimeEntries.Count - 1); + if (dayCycle.Comp.IndexedCycle is null) + return; + + nextEntry = Math.Clamp(nextEntry, 0, dayCycle.Comp.IndexedCycle.TimeEntries.Count - 1); dayCycle.Comp.CurrentTimeEntryIndex = nextEntry; dayCycle.Comp.EntryStartTime = dayCycle.Comp.EntryEndTime; - dayCycle.Comp.EntryEndTime += dayCycle.Comp.CurrentTimeEntry.Duration; + dayCycle.Comp.EntryEndTime += dayCycle.Comp.CurrentTimeEntry!.Value.Duration; var ev = new DayCycleChangedEvent(dayCycle.Comp.CurrentTimeEntry); RaiseLocalEvent(dayCycle, ref ev, true); @@ -97,7 +113,7 @@ public sealed partial class CP14DayCycleSystem : CP14SharedDayCycleSystem private Color GetCurrentColor(Entity dayCycle, double totalSeconds) { var timeScale = GetTimeScale(dayCycle, totalSeconds); - return Color.InterpolateBetween(dayCycle.Comp.StartColor, dayCycle.Comp.EndColor, timeScale); + return Color.InterpolateBetween(dayCycle.Comp.StartColor ?? Color.Black, dayCycle.Comp.EndColor ?? Color.Black, timeScale); } private float GetTimeScale(Entity dayCycle, double totalSeconds) diff --git a/Content.Server/_CP14/DayCycle/Commands/CP14AddTimeEntryCommand.cs b/Content.Server/_CP14/DayCycle/Commands/CP14AddTimeEntryCommand.cs index 011b814f2d..ba0d700dde 100644 --- a/Content.Server/_CP14/DayCycle/Commands/CP14AddTimeEntryCommand.cs +++ b/Content.Server/_CP14/DayCycle/Commands/CP14AddTimeEntryCommand.cs @@ -1,6 +1,7 @@ using Content.Server.Administration; using Content.Shared._CP14.DayCycle; using Content.Shared._CP14.DayCycle.Components; +using Content.Shared._CP14.DayCycle.Prototypes; using Content.Shared.Administration; using Robust.Shared.Console; using Robust.Shared.Prototypes; diff --git a/Content.Server/_CP14/DayCycle/Commands/CP14InitDayCycleCommand.cs b/Content.Server/_CP14/DayCycle/Commands/CP14InitDayCycleCommand.cs index 56de466d51..ca489bd2a8 100644 --- a/Content.Server/_CP14/DayCycle/Commands/CP14InitDayCycleCommand.cs +++ b/Content.Server/_CP14/DayCycle/Commands/CP14InitDayCycleCommand.cs @@ -41,7 +41,10 @@ public sealed class CP14InitDayCycleCommand : LocalizedCommands return; } - if (dayCycle.TimeEntries.Count < CP14DayCycleSystem.MinTimeEntryCount) + if (dayCycle.IndexedCycle is null) + return; + + if (dayCycle.IndexedCycle.TimeEntries.Count < CP14DayCycleSystem.MinTimeEntryCount) { shell.WriteError($"Attempting to init a daily cycle with the number of time entries less than {CP14DayCycleSystem.MinTimeEntryCount}"); return; diff --git a/Content.Server/_CP14/DayCycle/Commands/CP14SetTimeEntryCommand.cs b/Content.Server/_CP14/DayCycle/Commands/CP14SetTimeEntryCommand.cs index 9cf8bc5f0b..1e6335e26e 100644 --- a/Content.Server/_CP14/DayCycle/Commands/CP14SetTimeEntryCommand.cs +++ b/Content.Server/_CP14/DayCycle/Commands/CP14SetTimeEntryCommand.cs @@ -64,10 +64,13 @@ public sealed class CP14SetTimeEntryCommand : LocalizedCommands if (!entityManager.TryGetComponent(entityManager.GetEntity(mapUid), out var component)) return CompletionResult.Empty; - if (component.TimeEntries.Count - 1 < 0) + if (component.IndexedCycle is null) return CompletionResult.Empty; - var indices = new string[component.TimeEntries.Count - 1]; + if (component.IndexedCycle.TimeEntries.Count - 1 < 0) + return CompletionResult.Empty; + + var indices = new string[component.IndexedCycle.TimeEntries.Count - 1]; for (var i = 0; i < indices.Length; i++) { indices[i] = i.ToString(); diff --git a/Content.Shared/_CP14/DayCycle/CP14SharedDayCycleSystem.cs b/Content.Shared/_CP14/DayCycle/CP14SharedDayCycleSystem.cs index ec8a0e0c23..1f025eb347 100644 --- a/Content.Shared/_CP14/DayCycle/CP14SharedDayCycleSystem.cs +++ b/Content.Shared/_CP14/DayCycle/CP14SharedDayCycleSystem.cs @@ -1,4 +1,5 @@ using Content.Shared._CP14.DayCycle.Components; +using Content.Shared._CP14.DayCycle.Prototypes; using Content.Shared.Maps; using Robust.Shared.Map; using Robust.Shared.Map.Components; diff --git a/Content.Shared/_CP14/DayCycle/Components/CP14DayCycleComponent.cs b/Content.Shared/_CP14/DayCycle/Components/CP14DayCycleComponent.cs index 4a2dbb2c39..6d320f0af4 100644 --- a/Content.Shared/_CP14/DayCycle/Components/CP14DayCycleComponent.cs +++ b/Content.Shared/_CP14/DayCycle/Components/CP14DayCycleComponent.cs @@ -1,3 +1,4 @@ +using Content.Shared._CP14.DayCycle.Prototypes; using Robust.Shared.GameStates; using Robust.Shared.Prototypes; using Robust.Shared.Serialization; @@ -7,37 +8,47 @@ namespace Content.Shared._CP14.DayCycle.Components; /// /// Stores all the necessary data for the day and night cycle system to work. /// -[RegisterComponent, NetworkedComponent, AutoGenerateComponentState, Access(typeof(CP14SharedDayCycleSystem))] +[RegisterComponent, NetworkedComponent, AutoGenerateComponentState, AutoGenerateComponentPause, Access(typeof(CP14SharedDayCycleSystem))] public sealed partial class CP14DayCycleComponent : Component { [ViewVariables] - public int NextTimeEntryIndex => CurrentTimeEntryIndex + 1 >= TimeEntries.Count ? 0 : CurrentTimeEntryIndex + 1; + public int NextTimeEntryIndex + { + get + { + if (IndexedCycle is null) + return 0; + return CurrentTimeEntryIndex + 1 >= IndexedCycle.TimeEntries.Count ? 0 : CurrentTimeEntryIndex + 1; + } + } [ViewVariables] - public DayCycleEntry CurrentTimeEntry => TimeEntries[CurrentTimeEntryIndex]; + public DayCycleEntry? CurrentTimeEntry => IndexedCycle?.TimeEntries[CurrentTimeEntryIndex]; [ViewVariables] - public DayCycleEntry NextCurrentTimeEntry => TimeEntries[NextTimeEntryIndex]; + public DayCycleEntry? NextCurrentTimeEntry => IndexedCycle?.TimeEntries[NextTimeEntryIndex]; [ViewVariables] - public Color StartColor => CurrentTimeEntry.Color; + public Color? StartColor => CurrentTimeEntry?.Color; [ViewVariables] - public Color EndColor => NextCurrentTimeEntry.Color; + public Color? EndColor => NextCurrentTimeEntry?.Color; [ViewVariables] - public ProtoId CurrentPeriod => CurrentTimeEntry.Period; + public ProtoId? CurrentPeriod => CurrentTimeEntry?.Period; - [DataField(required: true), ViewVariables, AutoNetworkedField] - public List TimeEntries = new(); + public CP14DayCyclePrototype? IndexedCycle; + + [DataField, AutoNetworkedField] + public ProtoId CycleProto = "Default"; [DataField, ViewVariables, AutoNetworkedField] public int CurrentTimeEntryIndex; - [DataField, ViewVariables, AutoNetworkedField] + [DataField, ViewVariables, AutoNetworkedField, AutoPausedField] public TimeSpan EntryStartTime; - [DataField, ViewVariables, AutoNetworkedField] + [DataField, ViewVariables, AutoNetworkedField, AutoPausedField] public TimeSpan EntryEndTime; [DataField] @@ -73,4 +84,4 @@ public readonly partial record struct DayCycleEntry() /// Event raised on map entity, wen day cycle changed. /// [ByRefEvent] -public readonly record struct DayCycleChangedEvent(DayCycleEntry Entry); +public readonly record struct DayCycleChangedEvent(DayCycleEntry? Entry); diff --git a/Content.Shared/_CP14/DayCycle/CP14DayCyclePeriodPrototype.cs b/Content.Shared/_CP14/DayCycle/Prototypes/CP14DayCyclePeriodPrototype.cs similarity index 83% rename from Content.Shared/_CP14/DayCycle/CP14DayCyclePeriodPrototype.cs rename to Content.Shared/_CP14/DayCycle/Prototypes/CP14DayCyclePeriodPrototype.cs index 5fdc58c4d2..04e0e3330a 100644 --- a/Content.Shared/_CP14/DayCycle/CP14DayCyclePeriodPrototype.cs +++ b/Content.Shared/_CP14/DayCycle/Prototypes/CP14DayCyclePeriodPrototype.cs @@ -1,6 +1,6 @@ using Robust.Shared.Prototypes; -namespace Content.Shared._CP14.DayCycle; +namespace Content.Shared._CP14.DayCycle.Prototypes; [Prototype("CP14DayCyclePeriod")] public sealed class CP14DayCyclePeriodPrototype : IPrototype diff --git a/Content.Shared/_CP14/DayCycle/Prototypes/CP14DayCyclePrototype.cs b/Content.Shared/_CP14/DayCycle/Prototypes/CP14DayCyclePrototype.cs new file mode 100644 index 0000000000..c9c85aca77 --- /dev/null +++ b/Content.Shared/_CP14/DayCycle/Prototypes/CP14DayCyclePrototype.cs @@ -0,0 +1,14 @@ +using Content.Shared._CP14.DayCycle.Components; +using Robust.Shared.Prototypes; + +namespace Content.Shared._CP14.DayCycle.Prototypes; + +[Prototype("CP14DayCycle")] +public sealed class CP14DayCyclePrototype : IPrototype +{ + [IdDataField] + public string ID { get; } = string.Empty; + + [DataField(required: true), ViewVariables] + public List TimeEntries = new(); +} diff --git a/Content.Shared/_CP14/MagicRitual/Requirements/RequiredTime.cs b/Content.Shared/_CP14/MagicRitual/Requirements/RequiredTime.cs index 4263d66534..9a53c7928a 100644 --- a/Content.Shared/_CP14/MagicRitual/Requirements/RequiredTime.cs +++ b/Content.Shared/_CP14/MagicRitual/Requirements/RequiredTime.cs @@ -1,5 +1,6 @@ using Content.Shared._CP14.DayCycle; using Content.Shared._CP14.DayCycle.Components; +using Content.Shared._CP14.DayCycle.Prototypes; using Robust.Shared.Prototypes; namespace Content.Shared._CP14.MagicRitual.Requirements; diff --git a/Content.Shared/_CP14/Random/Rules/IsDaylight.cs b/Content.Shared/_CP14/Random/Rules/IsDaylight.cs index 79dbde4d3b..3418dd2830 100644 --- a/Content.Shared/_CP14/Random/Rules/IsDaylight.cs +++ b/Content.Shared/_CP14/Random/Rules/IsDaylight.cs @@ -1,5 +1,6 @@ using Content.Shared._CP14.DayCycle; using Content.Shared._CP14.DayCycle.Components; +using Content.Shared._CP14.DayCycle.Prototypes; using Content.Shared.Random.Rules; using Robust.Shared.Prototypes; @@ -17,6 +18,9 @@ public sealed partial class CP14TimePeriod : RulesRule var transform = entManager.System(); var map = transform.GetMap(uid); - return entManager.TryGetComponent(map, out var dayCycle) && Periods.Contains(dayCycle.CurrentPeriod); + if (!entManager.TryGetComponent(map, out var dayCycle)) + return false; + + return dayCycle.CurrentPeriod is not null && Periods.Contains(dayCycle.CurrentPeriod.Value); } } diff --git a/Resources/Maps/_CP14/comoss.yml b/Resources/Maps/_CP14/comoss.yml index cd7deebd59..c02ae3f5b5 100644 --- a/Resources/Maps/_CP14/comoss.yml +++ b/Resources/Maps/_CP14/comoss.yml @@ -69,27 +69,6 @@ entities: - 0 - 0 - type: CP14DayCycle - timeEntries: - - duration: 80 - color: '#754A4AFF' - - duration: 80 - color: '#E0BA87FF' - - duration: 80 - color: '#BFEEFFFF' - - period: Night - duration: 80 - color: '#385163FF' - - period: Night - duration: 80 - color: '#060D12FF' - - period: Night - duration: 80 - color: '#000000FF' - - period: Night - duration: 80 - color: '#000000FF' - - duration: 80 - color: '#120906FF' - type: CP14WeatherController entries: - visuals: CP14Snow diff --git a/Resources/Maps/_CP14/dev_map.yml b/Resources/Maps/_CP14/dev_map.yml index 8e1553fa65..154c0d05a3 100644 --- a/Resources/Maps/_CP14/dev_map.yml +++ b/Resources/Maps/_CP14/dev_map.yml @@ -36,27 +36,6 @@ entities: - visuals: CP14Storm - visuals: CP14Mist - type: CP14DayCycle - timeEntries: - - duration: 80 - color: '#754A4AFF' - - duration: 80 - color: '#E0BA87FF' - - duration: 80 - color: '#BFEEFFFF' - - period: Night - duration: 80 - color: '#385163FF' - - period: Night - duration: 80 - color: '#060D12FF' - - period: Night - duration: 80 - color: '#000000FF' - - period: Night - duration: 80 - color: '#000000FF' - - duration: 80 - color: '#120906FF' - type: Broadphase - type: OccluderTree - type: MapGrid diff --git a/Resources/Maps/_CP14/island.yml b/Resources/Maps/_CP14/island.yml index 3b2e87b6b3..3904cbc8a7 100644 --- a/Resources/Maps/_CP14/island.yml +++ b/Resources/Maps/_CP14/island.yml @@ -47,27 +47,6 @@ entities: - 0 - 0 - type: CP14DayCycle - timeEntries: - - duration: 80 - color: '#754A4AFF' - - duration: 80 - color: '#E0BA87FF' - - duration: 80 - color: '#BFEEFFFF' - - period: Night - duration: 80 - color: '#385163FF' - - period: Night - duration: 80 - color: '#060D12FF' - - period: Night - duration: 80 - color: '#000000FF' - - period: Night - duration: 80 - color: '#000000FF' - - duration: 80 - color: '#120906FF' - type: Gravity gravityShakeSound: !type:SoundPathSpecifier path: /Audio/Effects/alert.ogg diff --git a/Resources/Maps/_CP14/tavern.yml b/Resources/Maps/_CP14/tavern.yml index 31d44578d6..1565596d0b 100644 --- a/Resources/Maps/_CP14/tavern.yml +++ b/Resources/Maps/_CP14/tavern.yml @@ -53,27 +53,6 @@ entities: - visuals: CP14Storm - visuals: CP14Mist - type: CP14DayCycle - timeEntries: - - duration: 80 - color: '#754A4AFF' - - duration: 80 - color: '#E0BA87FF' - - duration: 80 - color: '#BFEEFFFF' - - period: Night - duration: 80 - color: '#385163FF' - - period: Night - duration: 80 - color: '#060D12FF' - - period: Night - duration: 80 - color: '#000000FF' - - period: Night - duration: 80 - color: '#000000FF' - - duration: 80 - color: '#120906FF' - type: Broadphase - type: OccluderTree - type: MapGrid diff --git a/Resources/Maps/_CP14/template.yml b/Resources/Maps/_CP14/template.yml index 0f49d44803..8f87cc9598 100644 --- a/Resources/Maps/_CP14/template.yml +++ b/Resources/Maps/_CP14/template.yml @@ -28,27 +28,6 @@ entities: range: 10 - type: MapLight - type: CP14DayCycle - timeEntries: - - duration: 80 - color: '#754A4AFF' - - duration: 80 - color: '#E0BA87FF' - - duration: 80 - color: '#BFEEFFFF' - - period: Night - duration: 80 - color: '#385163FF' - - period: Night - duration: 80 - color: '#060D12FF' - - period: Night - duration: 80 - color: '#000000FF' - - period: Night - duration: 80 - color: '#000000FF' - - duration: 80 - color: '#120906FF' - type: Gravity gravityShakeSound: !type:SoundPathSpecifier path: /Audio/Effects/alert.ogg diff --git a/Resources/Prototypes/_CP14/DayCycle/periods.yml b/Resources/Prototypes/_CP14/DayCycle/periods.yml index 6045d5c4ba..2844a9090d 100644 --- a/Resources/Prototypes/_CP14/DayCycle/periods.yml +++ b/Resources/Prototypes/_CP14/DayCycle/periods.yml @@ -13,3 +13,24 @@ - type: CP14DayCyclePeriod id: Evening name: cp14-daycycle-evening + +- type: CP14DayCycle # + id: Default + timeEntries: + - duration: 145 + color: '#754A4A' #Dawn + - duration: 145 + color: '#E0BA87' # + - duration: 145 + color: '#BFEEFF' #Day + - period: Night + duration: 145 + color: '#385163' #Evening + - period: Night + duration: 80 + color: '#060D12' #Night + - period: Night + duration: 160 + color: '#000000' #BLACK NIGHT + - duration: 80 + color: '#120906' #Night \ No newline at end of file diff --git a/Resources/Prototypes/_CP14/Procedural/Demiplane/Modifiers/MapLight/mapLight.yml b/Resources/Prototypes/_CP14/Procedural/Demiplane/Modifiers/MapLight/mapLight.yml index b23b514f43..994df56e72 100644 --- a/Resources/Prototypes/_CP14/Procedural/Demiplane/Modifiers/MapLight/mapLight.yml +++ b/Resources/Prototypes/_CP14/Procedural/Demiplane/Modifiers/MapLight/mapLight.yml @@ -11,6 +11,17 @@ - type: MapLight ambientLightColor: "#000000" +- type: cp14DemiplaneModifier + id: MapLightCycleDefault + tiers: + - 1 + - 2 + - 3 + categories: + MapLight: 1 + components: + - type: CP14DayCycle + - type: cp14DemiplaneModifier id: MapLightDarkRed tiers: