clean up weather systems (#28792)

* 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
This commit is contained in:
Ed
2024-06-18 13:27:34 +03:00
committed by GitHub
parent 82c4db67bc
commit a1e66cfbb4
5 changed files with 28 additions and 37 deletions

View File

@@ -38,7 +38,7 @@ public sealed partial class StencilOverlay
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;
}

View File

@@ -2,16 +2,11 @@ using System.Numerics;
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;
@@ -62,7 +57,7 @@ public sealed class WeatherSystem : SharedWeatherSystem
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);
@@ -75,7 +70,7 @@ public sealed class WeatherSystem : SharedWeatherSystem
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
@@ -107,7 +102,7 @@ public sealed class WeatherSystem : SharedWeatherSystem
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);

View File

@@ -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()
{
@@ -30,7 +29,7 @@ public sealed class WeatherSystem : SharedWeatherSystem
}
[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)
{
@@ -60,7 +59,8 @@ public sealed class WeatherSystem : SharedWeatherSystem
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;

View File

@@ -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;
@@ -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);
}
@@ -41,9 +38,7 @@ public abstract class SharedWeatherSystem : EntitySystem
}
}
public bool CanWeatherAffect(
MapGridComponent grid,
TileRef tileRef)
public bool CanWeatherAffect(EntityUid uid, MapGridComponent grid, TileRef tileRef)
{
if (tileRef.Tile.IsEmpty)
return true;
@@ -53,9 +48,9 @@ public abstract class SharedWeatherSystem : EntitySystem
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;
@@ -154,20 +149,22 @@ public abstract class SharedWeatherSystem : EntitySystem
/// </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;
}
@@ -177,12 +174,12 @@ public abstract class SharedWeatherSystem : EntitySystem
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>
@@ -229,9 +226,9 @@ public abstract class SharedWeatherSystem : EntitySystem
[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;
}

View File

@@ -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;
@@ -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);
@@ -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;
}