Revert "Gamerule Entities" (#15724)
This commit is contained in:
@@ -1,197 +1,209 @@
|
||||
using System.Linq;
|
||||
using Content.Server.Administration.Logs;
|
||||
using Content.Server.Atmos.EntitySystems;
|
||||
using Content.Server.Chat.Systems;
|
||||
using Content.Server.GameTicking.Rules;
|
||||
using Content.Server.GameTicking.Rules.Components;
|
||||
using Content.Server.GameTicking.Rules.Configurations;
|
||||
using Content.Server.Station.Components;
|
||||
using Content.Server.Station.Systems;
|
||||
using Content.Server.StationEvents.Components;
|
||||
using Content.Shared.Database;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Map.Components;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Server.StationEvents.Events;
|
||||
|
||||
/// <summary>
|
||||
/// An abstract entity system inherited by all station events for their behavior.
|
||||
/// </summary>
|
||||
public abstract class StationEventSystem<T> : GameRuleSystem<T> where T : Component
|
||||
namespace Content.Server.StationEvents.Events
|
||||
{
|
||||
[Dependency] protected readonly IAdminLogManager AdminLogManager = default!;
|
||||
[Dependency] private readonly IGameTiming _timing = default!;
|
||||
[Dependency] protected readonly IMapManager MapManager = default!;
|
||||
[Dependency] protected readonly IPrototypeManager PrototypeManager = default!;
|
||||
[Dependency] protected readonly IRobustRandom RobustRandom = default!;
|
||||
[Dependency] private readonly AtmosphereSystem _atmosphere = default!;
|
||||
[Dependency] protected readonly ChatSystem ChatSystem = default!;
|
||||
[Dependency] protected readonly SharedAudioSystem Audio = default!;
|
||||
[Dependency] private readonly SharedTransformSystem _transform = default!;
|
||||
[Dependency] protected readonly StationSystem StationSystem = default!;
|
||||
|
||||
protected ISawmill Sawmill = default!;
|
||||
|
||||
public override void Initialize()
|
||||
/// <summary>
|
||||
/// An abstract entity system inherited by all station events for their behavior.
|
||||
/// </summary>
|
||||
public abstract class StationEventSystem : GameRuleSystem
|
||||
{
|
||||
base.Initialize();
|
||||
[Dependency] protected readonly IRobustRandom RobustRandom = default!;
|
||||
[Dependency] protected readonly IAdminLogManager AdminLogManager = default!;
|
||||
[Dependency] protected readonly IPrototypeManager PrototypeManager = default!;
|
||||
[Dependency] protected readonly IMapManager MapManager = default!;
|
||||
[Dependency] private readonly AtmosphereSystem _atmosphere = default!;
|
||||
[Dependency] protected readonly ChatSystem ChatSystem = default!;
|
||||
[Dependency] protected readonly StationSystem StationSystem = default!;
|
||||
|
||||
Sawmill = Logger.GetSawmill("stationevents");
|
||||
}
|
||||
protected ISawmill Sawmill = default!;
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void Added(EntityUid uid, T component, GameRuleComponent gameRule, GameRuleAddedEvent args)
|
||||
{
|
||||
base.Added(uid, component, gameRule, args);
|
||||
/// <summary>
|
||||
/// How long has the event existed. Do not change this.
|
||||
/// </summary>
|
||||
protected float Elapsed { get; set; }
|
||||
|
||||
if (!TryComp<StationEventComponent>(uid, out var stationEvent))
|
||||
return;
|
||||
|
||||
AdminLogManager.Add(LogType.EventAnnounced, $"Event added / announced: {ToPrettyString(uid)}");
|
||||
|
||||
if (stationEvent.StartAnnouncement != null)
|
||||
public override void Initialize()
|
||||
{
|
||||
ChatSystem.DispatchGlobalAnnouncement(Loc.GetString(stationEvent.StartAnnouncement), playSound: false, colorOverride: Color.Gold);
|
||||
base.Initialize();
|
||||
|
||||
Sawmill = Logger.GetSawmill("stationevents");
|
||||
}
|
||||
|
||||
Audio.PlayGlobal(stationEvent.StartAudio, Filter.Broadcast(), true);
|
||||
stationEvent.StartTime = _timing.CurTime + stationEvent.StartDelay;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void Started(EntityUid uid, T component, GameRuleComponent gameRule, GameRuleStartedEvent args)
|
||||
{
|
||||
base.Started(uid, component, gameRule, args);
|
||||
|
||||
if (!TryComp<StationEventComponent>(uid, out var stationEvent))
|
||||
return;
|
||||
|
||||
AdminLogManager.Add(LogType.EventStarted, LogImpact.High, $"Event started: {ToPrettyString(uid)}");
|
||||
var duration = stationEvent.MaxDuration == null
|
||||
? stationEvent.Duration
|
||||
: TimeSpan.FromSeconds(RobustRandom.NextDouble(stationEvent.Duration.TotalSeconds,
|
||||
stationEvent.MaxDuration.Value.TotalSeconds));
|
||||
stationEvent.EndTime = _timing.CurTime + duration;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void Ended(EntityUid uid, T component, GameRuleComponent gameRule, GameRuleEndedEvent args)
|
||||
{
|
||||
base.Ended(uid, component, gameRule, args);
|
||||
|
||||
if (!TryComp<StationEventComponent>(uid, out var stationEvent))
|
||||
return;
|
||||
|
||||
AdminLogManager.Add(LogType.EventStopped, $"Event ended: {ToPrettyString(uid)}");
|
||||
|
||||
if (stationEvent.EndAnnouncement != null)
|
||||
/// <summary>
|
||||
/// Called once to setup the event after StartAfter has elapsed, or if an event is forcibly started.
|
||||
/// </summary>
|
||||
public override void Started()
|
||||
{
|
||||
ChatSystem.DispatchGlobalAnnouncement(Loc.GetString(stationEvent.EndAnnouncement), playSound: false, colorOverride: Color.Gold);
|
||||
AdminLogManager.Add(LogType.EventStarted, LogImpact.High, $"Event started: {Configuration.Id}");
|
||||
}
|
||||
|
||||
Audio.PlayGlobal(stationEvent.EndAudio, Filter.Broadcast(), true);
|
||||
/// <summary>
|
||||
/// Called once as soon as an event is added, for announcements.
|
||||
/// Can also be used for some initial setup.
|
||||
/// </summary>
|
||||
public override void Added()
|
||||
{
|
||||
AdminLogManager.Add(LogType.EventAnnounced, $"Event added / announced: {Configuration.Id}");
|
||||
|
||||
if (Configuration is not StationEventRuleConfiguration ev)
|
||||
return;
|
||||
|
||||
if (ev.StartAnnouncement != null)
|
||||
{
|
||||
ChatSystem.DispatchGlobalAnnouncement(Loc.GetString(ev.StartAnnouncement), playSound: false, colorOverride: Color.Gold);
|
||||
}
|
||||
|
||||
if (ev.StartAudio != null)
|
||||
{
|
||||
SoundSystem.Play(ev.StartAudio.GetSound(), Filter.Broadcast(), ev.StartAudio.Params);
|
||||
}
|
||||
|
||||
Elapsed = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called once when the station event ends for any reason.
|
||||
/// </summary>
|
||||
public override void Ended()
|
||||
{
|
||||
AdminLogManager.Add(LogType.EventStopped, $"Event ended: {Configuration.Id}");
|
||||
|
||||
if (Configuration is not StationEventRuleConfiguration ev)
|
||||
return;
|
||||
|
||||
if (ev.EndAnnouncement != null)
|
||||
{
|
||||
ChatSystem.DispatchGlobalAnnouncement(Loc.GetString(ev.EndAnnouncement), playSound: false, colorOverride: Color.Gold);
|
||||
}
|
||||
|
||||
if (ev.EndAudio != null)
|
||||
{
|
||||
SoundSystem.Play(ev.EndAudio.GetSound(), Filter.Broadcast(), ev.EndAudio.Params);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called every tick when this event is running.
|
||||
/// Events are responsible for their own lifetime, so this handles starting and ending after time.
|
||||
/// </summary>
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
if (!RuleAdded || Configuration is not StationEventRuleConfiguration data)
|
||||
return;
|
||||
|
||||
Elapsed += frameTime;
|
||||
|
||||
if (!RuleStarted && Elapsed >= data.StartAfter)
|
||||
{
|
||||
GameTicker.StartGameRule(PrototypeManager.Index<GameRulePrototype>(Prototype));
|
||||
}
|
||||
|
||||
if (RuleStarted && Elapsed >= data.EndAfter)
|
||||
{
|
||||
GameTicker.EndGameRule(PrototypeManager.Index<GameRulePrototype>(Prototype));
|
||||
}
|
||||
}
|
||||
|
||||
#region Helper Functions
|
||||
|
||||
protected void ForceEndSelf()
|
||||
{
|
||||
GameTicker.EndGameRule(PrototypeManager.Index<GameRulePrototype>(Prototype));
|
||||
}
|
||||
|
||||
protected bool TryFindRandomTile(out Vector2i tile, out EntityUid targetStation, out EntityUid targetGrid, out EntityCoordinates targetCoords)
|
||||
{
|
||||
tile = default;
|
||||
|
||||
targetCoords = EntityCoordinates.Invalid;
|
||||
if (StationSystem.Stations.Count == 0)
|
||||
{
|
||||
targetStation = EntityUid.Invalid;
|
||||
targetGrid = EntityUid.Invalid;
|
||||
return false;
|
||||
}
|
||||
targetStation = RobustRandom.Pick(StationSystem.Stations);
|
||||
var possibleTargets = Comp<StationDataComponent>(targetStation).Grids;
|
||||
if (possibleTargets.Count == 0)
|
||||
{
|
||||
targetGrid = EntityUid.Invalid;
|
||||
return false;
|
||||
}
|
||||
|
||||
targetGrid = RobustRandom.Pick(possibleTargets);
|
||||
|
||||
if (!TryComp<MapGridComponent>(targetGrid, out var gridComp))
|
||||
return false;
|
||||
|
||||
var found = false;
|
||||
var (gridPos, _, gridMatrix) = Transform(targetGrid).GetWorldPositionRotationMatrix();
|
||||
var gridBounds = gridMatrix.TransformBox(gridComp.LocalAABB);
|
||||
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
var randomX = RobustRandom.Next((int) gridBounds.Left, (int) gridBounds.Right);
|
||||
var randomY = RobustRandom.Next((int) gridBounds.Bottom, (int) gridBounds.Top);
|
||||
|
||||
tile = new Vector2i(randomX - (int) gridPos.X, randomY - (int) gridPos.Y);
|
||||
if (_atmosphere.IsTileSpace(gridComp.Owner, Transform(targetGrid).MapUid, tile,
|
||||
mapGridComp: gridComp)
|
||||
|| _atmosphere.IsTileAirBlocked(gridComp.Owner, tile, mapGridComp: gridComp))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
found = true;
|
||||
targetCoords = gridComp.GridTileToLocal(tile);
|
||||
break;
|
||||
}
|
||||
|
||||
if (!found) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static GameRulePrototype GetRandomEventUnweighted(IPrototypeManager? prototypeManager = null, IRobustRandom? random = null)
|
||||
{
|
||||
IoCManager.Resolve(ref prototypeManager, ref random);
|
||||
|
||||
return random.Pick(prototypeManager.EnumeratePrototypes<GameRulePrototype>()
|
||||
.Where(p => p.Configuration is StationEventRuleConfiguration).ToArray());
|
||||
}
|
||||
|
||||
public float GetSeverityModifier()
|
||||
{
|
||||
var ev = new GetSeverityModifierEvent();
|
||||
RaiseLocalEvent(ev);
|
||||
return ev.Modifier;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called every tick when this event is running.
|
||||
/// Events are responsible for their own lifetime, so this handles starting and ending after time.
|
||||
/// Raised broadcast to determine what the severity modifier should be for an event, some positive number that can be multiplied with various things.
|
||||
/// Handled by usually other game rules (like the ramping scheduler).
|
||||
/// Most events should try and make use of this if possible.
|
||||
/// </summary>
|
||||
/// <inheritdoc/>
|
||||
public override void Update(float frameTime)
|
||||
public sealed class GetSeverityModifierEvent : EntityEventArgs
|
||||
{
|
||||
var query = EntityQueryEnumerator<StationEventComponent, GameRuleComponent>();
|
||||
while (query.MoveNext(out var uid, out var stationEvent, out var ruleData))
|
||||
{
|
||||
if (!GameTicker.IsGameRuleAdded(uid, ruleData))
|
||||
continue;
|
||||
|
||||
if (!GameTicker.IsGameRuleActive(uid, ruleData) && _timing.CurTime >= stationEvent.StartTime)
|
||||
{
|
||||
GameTicker.StartGameRule(uid, ruleData);
|
||||
}
|
||||
else if (GameTicker.IsGameRuleActive(uid, ruleData) && _timing.CurTime >= stationEvent.EndTime)
|
||||
{
|
||||
GameTicker.EndGameRule(uid, ruleData);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Should be multiplied/added to rather than set, for commutativity.
|
||||
/// </summary>
|
||||
public float Modifier = 1.0f;
|
||||
}
|
||||
|
||||
#region Helper Functions
|
||||
|
||||
protected void ForceEndSelf(EntityUid uid, GameRuleComponent? component = null)
|
||||
{
|
||||
GameTicker.EndGameRule(uid, component);
|
||||
}
|
||||
|
||||
protected bool TryFindRandomTile(out Vector2i tile, out EntityUid targetStation, out EntityUid targetGrid, out EntityCoordinates targetCoords)
|
||||
{
|
||||
tile = default;
|
||||
|
||||
targetCoords = EntityCoordinates.Invalid;
|
||||
if (StationSystem.Stations.Count == 0)
|
||||
{
|
||||
targetStation = EntityUid.Invalid;
|
||||
targetGrid = EntityUid.Invalid;
|
||||
return false;
|
||||
}
|
||||
targetStation = RobustRandom.Pick(StationSystem.Stations);
|
||||
var possibleTargets = Comp<StationDataComponent>(targetStation).Grids;
|
||||
if (possibleTargets.Count == 0)
|
||||
{
|
||||
targetGrid = EntityUid.Invalid;
|
||||
return false;
|
||||
}
|
||||
|
||||
targetGrid = RobustRandom.Pick(possibleTargets);
|
||||
|
||||
if (!TryComp<MapGridComponent>(targetGrid, out var gridComp))
|
||||
return false;
|
||||
|
||||
var found = false;
|
||||
var (gridPos, _, gridMatrix) = _transform.GetWorldPositionRotationMatrix(targetGrid);
|
||||
var gridBounds = gridMatrix.TransformBox(gridComp.LocalAABB);
|
||||
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
var randomX = RobustRandom.Next((int) gridBounds.Left, (int) gridBounds.Right);
|
||||
var randomY = RobustRandom.Next((int) gridBounds.Bottom, (int) gridBounds.Top);
|
||||
|
||||
tile = new Vector2i(randomX - (int) gridPos.X, randomY - (int) gridPos.Y);
|
||||
if (_atmosphere.IsTileSpace(targetGrid, Transform(targetGrid).MapUid, tile,
|
||||
mapGridComp: gridComp)
|
||||
|| _atmosphere.IsTileAirBlocked(targetGrid, tile, mapGridComp: gridComp))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
found = true;
|
||||
targetCoords = gridComp.GridTileToLocal(tile);
|
||||
break;
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
public float GetSeverityModifier()
|
||||
{
|
||||
var ev = new GetSeverityModifierEvent();
|
||||
RaiseLocalEvent(ev);
|
||||
return ev.Modifier;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raised broadcast to determine what the severity modifier should be for an event, some positive number that can be multiplied with various things.
|
||||
/// Handled by usually other game rules (like the ramping scheduler).
|
||||
/// Most events should try and make use of this if possible.
|
||||
/// </summary>
|
||||
public sealed class GetSeverityModifierEvent : EntityEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Should be multiplied/added to rather than set, for commutativity.
|
||||
/// </summary>
|
||||
public float Modifier = 1.0f;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user