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

Commit

Permalink
clean up weather systems (space-wizards#28792)
Browse files Browse the repository at this point in the history
* clean up weather systems

* Update WeatherComponent.cs

* Update SharedWeatherSystem.cs

* some fix

* Update SharedWeatherSystem.cs

* Update WeatherComponent.cs

* Update WeatherComponent.cs

* revert autoPause

* Update SharedWeatherSystem.cs
  • Loading branch information
TheShuEd authored Jun 18, 2024
1 parent 82c4db6 commit a1e66cf
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Content.Client/Overlays/StencilOverlay.Weather.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private void DrawWeather(in OverlayDrawArgs args, WeatherPrototype weatherProto,
foreach (var tile in grid.Comp.GetTilesIntersecting(worldAABB))
{
// Ignored tiles for stencil
if (_weather.CanWeatherAffect(grid, tile))
if (_weather.CanWeatherAffect(grid.Owner, grid, tile))
{
continue;
}
Expand Down
11 changes: 3 additions & 8 deletions Content.Client/Weather/WeatherSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@
using Content.Shared.Weather;
using Robust.Client.Audio;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.Player;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.GameStates;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Player;
using AudioComponent = Robust.Shared.Audio.Components.AudioComponent;

Expand Down Expand Up @@ -62,7 +57,7 @@ protected override void Run(EntityUid uid, WeatherData weather, WeatherPrototype
if (TryComp<MapGridComponent>(entXform.GridUid, out var grid))
{
var gridId = entXform.GridUid.Value;
// Floodfill to the nearest tile and use that for audio.
// FloodFill to the nearest tile and use that for audio.
var seed = _mapSystem.GetTileRef(gridId, grid, entXform.Coordinates);
var frontier = new Queue<TileRef>();
frontier.Enqueue(seed);
Expand All @@ -75,7 +70,7 @@ protected override void Run(EntityUid uid, WeatherData weather, WeatherPrototype
if (!visited.Add(node.GridIndices))
continue;

if (!CanWeatherAffect(grid, node))
if (!CanWeatherAffect(entXform.GridUid.Value, grid, node))
{
// Add neighbors
// TODO: Ideally we pick some deterministically random direction and use that
Expand Down Expand Up @@ -107,7 +102,7 @@ protected override void Run(EntityUid uid, WeatherData weather, WeatherPrototype
if (nearestNode != null)
{
var entPos = _transform.GetMapCoordinates(entXform);
var nodePosition = nearestNode.Value.ToMap(EntityManager, _transform).Position;
var nodePosition = _transform.ToMapCoordinates(nearestNode.Value).Position;
var delta = nodePosition - entPos.Position;
var distance = delta.Length();
occlusion = _audio.GetOcclusion(entPos, delta, distance);
Expand Down
8 changes: 4 additions & 4 deletions Content.Server/Weather/WeatherSystem.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using System.Linq;
using Content.Server.Administration;
using Content.Shared.Administration;
using Content.Shared.Weather;
using Robust.Shared.Console;
using Robust.Shared.GameStates;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;

namespace Content.Server.Weather;

public sealed class WeatherSystem : SharedWeatherSystem
{
[Dependency] private readonly IConsoleHost _console = default!;
[Dependency] private readonly SharedMapSystem _mapSystem = default!;

public override void Initialize()
{
Expand All @@ -30,7 +29,7 @@ private void OnWeatherGetState(EntityUid uid, WeatherComponent component, ref Co
}

[AdminCommand(AdminFlags.Fun)]
private void WeatherTwo(IConsoleShell shell, string argstr, string[] args)
private void WeatherTwo(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length < 2)
{
Expand Down Expand Up @@ -60,7 +59,8 @@ private void WeatherTwo(IConsoleShell shell, string argstr, string[] args)
var maxTime = TimeSpan.MaxValue;

// If it's already running then just fade out with how much time we're into the weather.
if (TryComp<WeatherComponent>(MapManager.GetMapEntityId(mapId), out var weatherComp) &&
if (_mapSystem.TryGetMap(mapId, out var mapUid) &&
TryComp<WeatherComponent>(mapUid, out var weatherComp) &&
weatherComp.Weather.TryGetValue(args[1], out var existing))
{
maxTime = curTime - existing.StartTime;
Expand Down
31 changes: 14 additions & 17 deletions Content.Shared/Weather/SharedWeatherSystem.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Content.Shared.Maps;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
using Robust.Shared.Physics.Components;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Timing;
Expand All @@ -18,15 +16,14 @@ public abstract class SharedWeatherSystem : EntitySystem
[Dependency] private readonly ITileDefinitionManager _tileDefManager = default!;
[Dependency] private readonly MetaDataSystem _metadata = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedMapSystem _mapSystem = default!;

private EntityQuery<BlockWeatherComponent> _blockQuery;
private EntityQuery<PhysicsComponent> _physicsQuery;

public override void Initialize()
{
base.Initialize();
_blockQuery = GetEntityQuery<BlockWeatherComponent>();
_physicsQuery = GetEntityQuery<PhysicsComponent>();
SubscribeLocalEvent<WeatherComponent, EntityUnpausedEvent>(OnWeatherUnpaused);
}

Expand All @@ -41,9 +38,7 @@ private void OnWeatherUnpaused(EntityUid uid, WeatherComponent component, ref En
}
}

public bool CanWeatherAffect(
MapGridComponent grid,
TileRef tileRef)
public bool CanWeatherAffect(EntityUid uid, MapGridComponent grid, TileRef tileRef)
{
if (tileRef.Tile.IsEmpty)
return true;
Expand All @@ -53,9 +48,9 @@ public bool CanWeatherAffect(
if (!tileDef.Weather)
return false;

var anchoredEnts = grid.GetAnchoredEntitiesEnumerator(tileRef.GridIndices);
var anchoredEntities = _mapSystem.GetAnchoredEntitiesEnumerator(uid, grid, tileRef.GridIndices);

while (anchoredEnts.MoveNext(out var ent))
while (anchoredEntities.MoveNext(out var ent))
{
if (_blockQuery.HasComponent(ent.Value))
return false;
Expand Down Expand Up @@ -154,20 +149,22 @@ public override void Update(float frameTime)
/// </summary>
public void SetWeather(MapId mapId, WeatherPrototype? proto, TimeSpan? endTime)
{
var mapUid = MapManager.GetMapEntityId(mapId);
var weatherComp = EnsureComp<WeatherComponent>(mapUid);
if (!_mapSystem.TryGetMap(mapId, out var mapUid))
return;

var weatherComp = EnsureComp<WeatherComponent>(mapUid.Value);

foreach (var (eProto, weather) in weatherComp.Weather)
{
// Reset cooldown if it's an existing one.
if (eProto == proto?.ID)
if (proto == null || eProto == proto.ID)
{
weather.EndTime = endTime;

if (weather.State == WeatherState.Ending)
weather.State = WeatherState.Running;

Dirty(mapUid, weatherComp);
Dirty(mapUid.Value, weatherComp);
continue;
}

Expand All @@ -177,12 +174,12 @@ public void SetWeather(MapId mapId, WeatherPrototype? proto, TimeSpan? endTime)
if (weather.EndTime == null || weather.EndTime > end)
{
weather.EndTime = end;
Dirty(mapUid, weatherComp);
Dirty(mapUid.Value, weatherComp);
}
}

if (proto != null)
StartWeather(mapUid, weatherComp, proto, endTime);
StartWeather(mapUid.Value, weatherComp, proto, endTime);
}

/// <summary>
Expand Down Expand Up @@ -229,9 +226,9 @@ protected virtual bool SetState(EntityUid uid, WeatherState state, WeatherCompon
[Serializable, NetSerializable]
protected sealed class WeatherComponentState : ComponentState
{
public Dictionary<string, WeatherData> Weather;
public Dictionary<ProtoId<WeatherPrototype>, WeatherData> Weather;

public WeatherComponentState(Dictionary<string, WeatherData> weather)
public WeatherComponentState(Dictionary<ProtoId<WeatherPrototype>, WeatherData> weather)
{
Weather = weather;
}
Expand Down
13 changes: 6 additions & 7 deletions Content.Shared/Weather/WeatherComponent.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Dictionary;

namespace Content.Shared.Weather;

Expand All @@ -12,8 +11,8 @@ public sealed partial class WeatherComponent : Component
/// <summary>
/// Currently running weathers
/// </summary>
[ViewVariables, DataField("weather", customTypeSerializer:typeof(PrototypeIdDictionarySerializer<WeatherData, WeatherPrototype>))]
public Dictionary<string, WeatherData> Weather = new();
[DataField]
public Dictionary<ProtoId<WeatherPrototype>, WeatherData> Weather = new();

public static readonly TimeSpan StartupTime = TimeSpan.FromSeconds(15);
public static readonly TimeSpan ShutdownTime = TimeSpan.FromSeconds(15);
Expand All @@ -29,19 +28,19 @@ public sealed partial class WeatherData
/// <summary>
/// When the weather started if relevant.
/// </summary>
[ViewVariables, DataField("startTime", customTypeSerializer: typeof(TimeOffsetSerializer))]
[DataField(customTypeSerializer: typeof(TimeOffsetSerializer))] //TODO: Remove Custom serializer
public TimeSpan StartTime = TimeSpan.Zero;

/// <summary>
/// When the applied weather will end.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("endTime", customTypeSerializer: typeof(TimeOffsetSerializer))]
[DataField(customTypeSerializer: typeof(TimeOffsetSerializer))] //TODO: Remove Custom serializer
public TimeSpan? EndTime;

[ViewVariables]
public TimeSpan Duration => EndTime == null ? TimeSpan.MaxValue : EndTime.Value - StartTime;

[DataField("state")]
[DataField]
public WeatherState State = WeatherState.Invalid;
}

Expand Down

0 comments on commit a1e66cf

Please sign in to comment.