Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Rework na iluminação e ciclo de dia e noite.
Browse files Browse the repository at this point in the history
  • Loading branch information
DoutorWhite committed Dec 11, 2023
1 parent e2d459a commit 44f8db1
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Content.Client/Time/TimeSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public TimeSpan GetRoundDuration()
}
public TimeSpan GetStationTime()
{
return TimeSpan.FromSeconds(_curTime + _cfg.GetCVar(CCVars.InitialTime));
return TimeSpan.FromSeconds(_curTime + _cfg.GetCVar(CCVars.InitialTime) * _cfg.GetCVar(CCVars.TimeScale));
}
public DateTime GetStationDate()
{
Expand Down
66 changes: 45 additions & 21 deletions Content.Server/Light/EntitySystems/PoweredLightSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using System.Linq;
using System.Text.RegularExpressions;
using FastAccessors;
using System.Globalization;

namespace Content.Server.Light.EntitySystems
{
Expand All @@ -54,12 +55,19 @@ public sealed class PoweredLightSystem : EntitySystem
[Dependency] private readonly PointLightSystem _pointLight = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly InventorySystem _inventory = default!;
[Dependency] private readonly IEntitySystemManager _entitySystem = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly ChatSystem _chatSystem = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
private static readonly TimeSpan ThunkDelay = TimeSpan.FromSeconds(2);
private DayCycleSystem? _dayCycleSystem;
private bool _isNight;
private List<EntityUid>? _stationList;
private double _lightLevel;
private double _redLevel;
private double _greenLevel;
private double _blueLevel;

public const string LightBulbContainer = "light_bulb";

public override void Initialize()
Expand All @@ -84,7 +92,12 @@ public override void Initialize()
SubscribeLocalEvent<ShiftChangeEvent>(OnShiftChange);

_isNight = false;
_dayCycleSystem = _entitySystem.GetEntitySystem<DayCycleSystem>();
_stationList = new List<EntityUid>();
_lightLevel = _dayCycleSystem.CalculateDayLightLevel();
_redLevel = _dayCycleSystem.CalculateColorLevel(1);
_greenLevel = _dayCycleSystem.CalculateColorLevel(2);
_blueLevel = _dayCycleSystem.CalculateColorLevel(3);
}

private void OnInit(EntityUid uid, PoweredLightComponent light, ComponentInit args)
Expand Down Expand Up @@ -270,6 +283,7 @@ public bool TryDestroyBulb(EntityUid uid, PoweredLightComponent? light = null)

private void UpdateLight(EntityUid uid,
PoweredLightComponent? light = null,
bool isLightCycle = false,
ApcPowerReceiverComponent? powerReceiver = null,
AppearanceComponent? appearance = null)
{
Expand All @@ -293,33 +307,29 @@ private void UpdateLight(EntityUid uid,
case LightBulbState.Normal:
if (powerReceiver.Powered && light.On)
{
var regex = new Regex(@"^#(\d+.?\d*)-(\d+.?\d*)-(\d+.?\d*)$");
var rgb_energy = _cfg.GetCVar(CCVars.NightLightRGBIntensity);
var result = regex.Match(rgb_energy);
var regex = new Regex(@"^#([A-Fa-f0-9]){6}$");
var color_hex = _cfg.GetCVar(CCVars.LightRGB);
var match = regex.Match(color_hex);
var energy = lightBulb.LightEnergy;
var radius = lightBulb.LightRadius;
var rbyte = lightBulb.Color.RByte;
var gbyte = lightBulb.Color.GByte;
var bbyte = lightBulb.Color.BByte;
if (_isNight && _stationList!.Contains(uid.ToCoordinates().GetGridUid(_entityManager).GetValueOrDefault()))
var color = lightBulb.Color;
if (_cfg.GetCVar(CCVars.ColorOverride) && match.Success)
color = System.Drawing.Color.FromArgb(int.Parse(match.Value.Replace("#", ""), NumberStyles.HexNumber));
if (_cfg.GetCVar(CCVars.DayNightCycle) && _stationList!.Contains(uid.ToCoordinates().GetGridUid(_entityManager).GetValueOrDefault()))
{
energy *= _cfg.GetCVar(CCVars.NightLightIntensity);
radius *= _cfg.GetCVar(CCVars.NightLightRadius);
if (result.Success)
{
rbyte = (byte) Math.Min(255, rbyte * float.Parse(result.Groups[1].Value));
gbyte = (byte) Math.Min(255, gbyte * float.Parse(result.Groups[2].Value));
bbyte = (byte) Math.Min(255, bbyte * float.Parse(result.Groups[3].Value));
}
else
energy *= (float) _lightLevel;
if (_cfg.GetCVar(CCVars.ColorCycle))
{
_cfg.SetCVar(CCVars.NightLightRGBIntensity, "#1.0-1.0-1.0");
var red = (int) Math.Min(255, color.RByte * _redLevel + 1);
var green = (int) Math.Min(255, color.BByte * _greenLevel + 1);
var blue = (int) Math.Min(255, color.GByte * _blueLevel + 1);
color = System.Drawing.Color.FromArgb(red, green, blue);
}
}
SetLight(uid, true, System.Drawing.Color.FromArgb(rbyte, gbyte, bbyte), light, radius, energy, lightBulb.LightSoftness);
SetLight(uid, true, color, light, radius, (float) energy, lightBulb.LightSoftness);
_appearance.SetData(uid, PoweredLightVisuals.BulbState, PoweredLightState.On, appearance);
var time = _gameTiming.CurTime;
if (time > light.LastThunk + ThunkDelay)
if (time > light.LastThunk + ThunkDelay && !isLightCycle)
{
light.LastThunk = time;
_audio.Play(light.TurnOnSound, Filter.Pvs(uid), uid, true, AudioParams.Default.WithVolume(-10f));
Expand Down Expand Up @@ -490,10 +500,24 @@ private void OnShiftChange(ShiftChangeEvent args)
_stationList!.First(), args.Message, Loc.GetString("comms-console-announcement-title-centcom"), true, args.Sound, colorOverride: args.Color);
}
}

public override void Update(float frameTime)
{
base.Update(frameTime);
if (_cfg.GetCVar(CCVars.DayNightCycle))
{
var curLightLevel = _dayCycleSystem!.CalculateDayLightLevel();
if (Math.Abs(curLightLevel - _lightLevel) > _cfg.GetCVar(CCVars.DeltaAdjust))
{
if (_cfg.GetCVar(CCVars.ColorCycle))
{
_redLevel = _dayCycleSystem.CalculateColorLevel(1);
_greenLevel = _dayCycleSystem.CalculateColorLevel(2);
_blueLevel = _dayCycleSystem.CalculateColorLevel(3);
}
_lightLevel = curLightLevel;
UpdateAll();
}
}
}
public void UpdateAll()
{
Expand All @@ -503,7 +527,7 @@ public void UpdateAll()
}
foreach (var bulb in EntityQuery<PoweredLightComponent>())
{
UpdateLight(bulb.Owner, bulb);
UpdateLight(bulb.Owner, bulb, isLightCycle: true);
}
}
}
Expand Down
52 changes: 52 additions & 0 deletions Content.Server/Time/DayCycleSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Content.Server.Light.EntitySystems;
using Content.Shared.CCVar;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Robust.Shared.Audio;
using Robust.Shared.Configuration;

Expand All @@ -22,6 +24,7 @@ public override void Initialize()
NightAlert = new SoundPathSpecifier("/Audio/Announcements/nightshift.ogg");
DayAlert = new SoundPathSpecifier("/Audio/Announcements/dayshift.ogg");
_timeSystem = _entitySystem.GetEntitySystem<TimeSystem>();
_currentHour = _timeSystem!.GetStationTime().Hours;
_isNight = false;
}
public override void Update(float frameTime)
Expand Down Expand Up @@ -49,5 +52,54 @@ private void ShiftChange(bool isNight, SoundSpecifier alertSound, string message
var ev = new ShiftChangeEvent(isNight, _cfg.GetCVar(CCVars.ShiftAnnouncement), alertSound, message, color);
RaiseLocalEvent(ev);
}

public double CalculateDayLightLevel()
{
var time = _timeSystem!.GetStationTime().TotalSeconds;
var wave_lenght = Math.Max(0, _cfg.GetCVar(CCVars.LightCycleDuration)) * 24;
var crest = Math.Max(1, _cfg.GetCVar(CCVars.MaxLight));
var shift = Math.Max(0, _cfg.GetCVar(CCVars.MinLight));
var amplitude = Math.Max(0, _cfg.GetCVar(CCVars.LightAmplitude));
var exponential = Math.Max(1, _cfg.GetCVar(CCVars.ExponentialConstant));
return CalculateCurve(time, wave_lenght, crest, shift, amplitude, 2 * exponential);
}

public double CalculateColorLevel(int color)
{
double crest = 2;
double shift = 0;
var exponent = 2;
var time = _timeSystem!.GetStationTime().TotalSeconds;
var wave_lenght = Math.Max(0, _cfg.GetCVar(CCVars.LightCycleDuration)) * 24;
var phase = 0d;
var amplification = 1d;
switch (color)
{
case 1:
shift = 0.8;
break;
case 2:
shift = 0.75;
crest = 1;
amplification = 1.35;
exponent = 8;
break;
case 3:
shift = 0.65;
crest = 1;
amplification = 1.25;
wave_lenght /= 2;
phase = wave_lenght / 2;
break;
}
return Math.Min(1.5, CalculateCurve(time, wave_lenght, crest, shift, amplification, exponent, phase));
}

public static double CalculateCurve(double x, double wave_lenght, double crest, double shift, double amplitude, double exponent, double phase = 0)
{
var sen = Math.Pow(Math.Sin((Math.PI * (phase + x)) / wave_lenght), exponent);
var function = amplitude * (Math.Pow(crest - shift + 1, sen) - 1) + shift;
return function;
}
}
}
2 changes: 1 addition & 1 deletion Content.Server/Time/TimeSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public double GetRoundDuration()
}
public TimeSpan GetStationTime()
{
return TimeSpan.FromSeconds(_curTime + _cfg.GetCVar(CCVars.InitialTime));
return TimeSpan.FromSeconds(_curTime + _cfg.GetCVar(CCVars.InitialTime) * _cfg.GetCVar(CCVars.TimeScale));
}
private void SetStationTime(double time)
{
Expand Down
43 changes: 24 additions & 19 deletions Content.Shared/CCVar/CCVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1823,29 +1823,34 @@ public static readonly CVarDef<float>
CVarDef.Create("replay.auto_record_temp_dir", "", CVar.SERVERONLY);

public static readonly CVarDef<int> InitialTime =
CVarDef.Create("time.initial", 28800, desc: "Valor inicial do contador de tempo relativo. (segundos)", flag: CVar.REPLICATED);

CVarDef.Create("time.initial", 1200, desc: "{seconds} (int)", flag: CVar.REPLICATED);
public static readonly CVarDef<int> TimeScale =
CVarDef.Create("time.scale", 24, desc: "Multiplicador do tempo relativo em relação ao tempo real (fator)", flag: CVar.REPLICATED);

CVarDef.Create("time.scale", 24, desc: "{factor} (int)", flag: CVar.REPLICATED);
public static readonly CVarDef<bool> DayNightCycle =
CVarDef.Create("time.day_night_cycle", false, desc: "Define se o sistema de iluminação noturna estará ativado na estação. (true/false)", flag: CVar.SERVERONLY);

CVarDef.Create("time.day_night_cycle", false, desc: "{state} (true/false)", flag: CVar.SERVERONLY);
public static readonly CVarDef<bool> ShiftAnnouncement =
CVarDef.Create("time.shift_announcement", false, desc: "Define se serão emitidos anúncios após a mudança de turno. (true/false)", flag: CVar.SERVERONLY);

public static readonly CVarDef<float> NightLightIntensity =
CVarDef.Create("time.night_light_intensity", 0.33f, desc: "Define o multiplicador da intensidade das luzes durante o modo noturno. (fator)", flag: CVar.SERVERONLY);

public static readonly CVarDef<string> NightLightRGBIntensity =
CVarDef.Create("time.night_rgb_intensity", "#1.0-1.0-1.0", desc: "Altera o color shifting das luzes durante o modo noturno. (#R.R-G.G-B.B)", flag: CVar.SERVERONLY);

public static readonly CVarDef<float> NightLightRadius =
CVarDef.Create("time.night_light_radius", 0.66f, desc: "Define o multiplicador da queda do radio das luzes durante o modo noturno. (fator)", flag: CVar.SERVERONLY);

CVarDef.Create("time.shift_announcement", false, desc: "{state} (true/false)", flag: CVar.SERVERONLY);
public static readonly CVarDef<float> LightCycleDuration =
CVarDef.Create("time.cycle_duration", 3600f, desc: "{seconds} (true/false)", flag: CVar.SERVERONLY);
public static readonly CVarDef<int> NightStartTime =
CVarDef.Create("time.night_start_time", 20, desc: "Horário em que a estação entrará em modo noturno (se estiver ativo). (horário)", flag: CVar.SERVERONLY);
CVarDef.Create("time.night_start_time", 20, desc: "{hour}. (int)", flag: CVar.SERVERONLY);
public static readonly CVarDef<int> NightDuration =
CVarDef.Create("time.night_duration", 8, desc: "Duração da noite. (horas)", flag: CVar.SERVERONLY);
CVarDef.Create("time.night_duration", 8, desc: "{hour}. (int)", flag: CVar.SERVERONLY);
public static readonly CVarDef<float> MaxLight =
CVarDef.Create("lights.max_light", 1.5f, desc: "{constant} (float)", flag: CVar.SERVERONLY);
public static readonly CVarDef<float> MinLight =
CVarDef.Create("lights.min_light", 0.3f, desc: "{constant} (float)", flag: CVar.SERVERONLY);
public static readonly CVarDef<int> ExponentialConstant =
CVarDef.Create("lights.exponential_c", 1, desc: "{constant} (int)", flag: CVar.SERVERONLY);
public static readonly CVarDef<float> LightAmplitude =
CVarDef.Create("lights.amplitude", 1f, desc: "{constant} (float)", flag: CVar.SERVERONLY);
public static readonly CVarDef<bool> ColorCycle =
CVarDef.Create("lights.color_cycle", false, desc: "{state} (true/false)", flag: CVar.SERVERONLY);
public static readonly CVarDef<float> DeltaAdjust =
CVarDef.Create("lights.delta_adjust", 0.0005f, desc: "{constant} (float)", flag: CVar.SERVERONLY);
public static readonly CVarDef<string> LightRGB =
CVarDef.Create("lights.color", "#FFFFFF", desc: "(#RRGGBB)", flag: CVar.SERVERONLY);
public static readonly CVarDef<bool> ColorOverride =
CVarDef.Create("lights.color_override", false, desc: "{state} (true/false)", flag: CVar.SERVERONLY);
}
}

0 comments on commit 44f8db1

Please sign in to comment.