Revert "Gamerule Entities" (#15724)

This commit is contained in:
metalgearsloth
2023-04-24 16:21:05 +10:00
committed by GitHub
parent 39cc02b8f9
commit d3552dae00
124 changed files with 4328 additions and 3083 deletions

View File

@@ -1,7 +1,9 @@
using System.Linq;
using Content.Server.GameTicking;
using Content.Server.StationEvents.Components;
using Content.Server.GameTicking.Rules;
using Content.Server.GameTicking.Rules.Configurations;
using Content.Shared.CCVar;
using Content.Shared.GameTicking;
using Robust.Server.Player;
using Robust.Shared.Configuration;
using Robust.Shared.Prototypes;
@@ -29,14 +31,6 @@ public sealed class EventManagerSystem : EntitySystem
_sawmill = Logger.GetSawmill("events");
_configurationManager.OnValueChanged(CCVars.EventsEnabled, SetEnabled, true);
SubscribeLocalEvent<StationEventComponent, EntityUnpausedEvent>(OnUnpaused);
}
private void OnUnpaused(EntityUid uid, StationEventComponent component, ref EntityUnpausedEvent args)
{
component.StartTime += args.PausedTime;
component.EndTime += args.PausedTime;
}
public override void Shutdown()
@@ -52,15 +46,16 @@ public sealed class EventManagerSystem : EntitySystem
{
var randomEvent = PickRandomEvent();
if (randomEvent == null)
if (randomEvent == null
|| !_prototype.TryIndex<GameRulePrototype>(randomEvent.Id, out var proto))
{
var errStr = Loc.GetString("station-event-system-run-random-event-no-valid-events");
_sawmill.Error(errStr);
return errStr;
}
var ent = GameTicker.AddGameRule(randomEvent);
var str = Loc.GetString("station-event-system-run-event",("eventName", ToPrettyString(ent)));
GameTicker.AddGameRule(proto);
var str = Loc.GetString("station-event-system-run-event",("eventName", randomEvent.Id));
_sawmill.Info(str);
return str;
}
@@ -68,7 +63,7 @@ public sealed class EventManagerSystem : EntitySystem
/// <summary>
/// Randomly picks a valid event.
/// </summary>
public string? PickRandomEvent()
public StationEventRuleConfiguration? PickRandomEvent()
{
var availableEvents = AvailableEvents();
_sawmill.Info($"Picking from {availableEvents.Count} total available events");
@@ -79,7 +74,7 @@ public sealed class EventManagerSystem : EntitySystem
/// Pick a random event from the available events at this time, also considering their weightings.
/// </summary>
/// <returns></returns>
private string? FindEvent(Dictionary<EntityPrototype, StationEventComponent> availableEvents)
private StationEventRuleConfiguration? FindEvent(List<StationEventRuleConfiguration> availableEvents)
{
if (availableEvents.Count == 0)
{
@@ -89,20 +84,20 @@ public sealed class EventManagerSystem : EntitySystem
var sumOfWeights = 0;
foreach (var stationEvent in availableEvents.Values)
foreach (var stationEvent in availableEvents)
{
sumOfWeights += (int) stationEvent.Weight;
}
sumOfWeights = _random.Next(sumOfWeights);
foreach (var (proto, stationEvent) in availableEvents)
foreach (var stationEvent in availableEvents)
{
sumOfWeights -= (int) stationEvent.Weight;
if (sumOfWeights <= 0)
{
return proto.ID;
return stationEvent;
}
}
@@ -115,73 +110,67 @@ public sealed class EventManagerSystem : EntitySystem
/// </summary>
/// <param name="ignoreEarliestStart"></param>
/// <returns></returns>
private Dictionary<EntityPrototype, StationEventComponent> AvailableEvents(bool ignoreEarliestStart = false)
private List<StationEventRuleConfiguration> AvailableEvents(bool ignoreEarliestStart = false)
{
TimeSpan currentTime;
var playerCount = _playerManager.PlayerCount;
// playerCount does a lock so we'll just keep the variable here
var currentTime = !ignoreEarliestStart
? GameTicker.RoundDuration()
: TimeSpan.Zero;
var result = new Dictionary<EntityPrototype, StationEventComponent>();
foreach (var (proto, stationEvent) in AllEvents())
if (!ignoreEarliestStart)
{
if (CanRun(proto, stationEvent, playerCount, currentTime))
currentTime = GameTicker.RoundDuration();
}
else
{
currentTime = TimeSpan.Zero;
}
var result = new List<StationEventRuleConfiguration>();
foreach (var stationEvent in AllEvents())
{
if (CanRun(stationEvent, playerCount, currentTime))
{
_sawmill.Debug($"Adding event {proto.ID} to possibilities");
result.Add(proto, stationEvent);
_sawmill.Debug($"Adding event {stationEvent.Id} to possibilities");
result.Add(stationEvent);
}
}
return result;
}
public Dictionary<EntityPrototype, StationEventComponent> AllEvents()
private IEnumerable<StationEventRuleConfiguration> AllEvents()
{
var allEvents = new Dictionary<EntityPrototype, StationEventComponent>();
foreach (var prototype in _prototype.EnumeratePrototypes<EntityPrototype>())
{
if (prototype.Abstract)
continue;
if (!prototype.TryGetComponent<StationEventComponent>(out var stationEvent))
continue;
allEvents.Add(prototype, stationEvent);
}
return allEvents;
return _prototype.EnumeratePrototypes<GameRulePrototype>()
.Where(p => p.Configuration is StationEventRuleConfiguration)
.Select(p => (StationEventRuleConfiguration) p.Configuration);
}
private int GetOccurrences(EntityPrototype stationEvent)
private int GetOccurrences(StationEventRuleConfiguration stationEvent)
{
return GetOccurrences(stationEvent.ID);
return GameTicker.AllPreviousGameRules.Count(p => p.Item2.ID == stationEvent.Id);
}
private int GetOccurrences(string stationEvent)
{
return GameTicker.AllPreviousGameRules.Count(p => p.Item2 == stationEvent);
}
public TimeSpan TimeSinceLastEvent(EntityPrototype stationEvent)
public TimeSpan TimeSinceLastEvent(StationEventRuleConfiguration? stationEvent)
{
foreach (var (time, rule) in GameTicker.AllPreviousGameRules.Reverse())
{
if (rule == stationEvent.ID)
if (rule.Configuration is not StationEventRuleConfiguration)
continue;
if (stationEvent == null || rule.ID == stationEvent.Id)
return time;
}
return TimeSpan.Zero;
}
private bool CanRun(EntityPrototype prototype, StationEventComponent stationEvent, int playerCount, TimeSpan currentTime)
private bool CanRun(StationEventRuleConfiguration stationEvent, int playerCount, TimeSpan currentTime)
{
if (GameTicker.IsGameRuleActive(prototype.ID))
if (GameTicker.IsGameRuleStarted(stationEvent.Id))
return false;
if (stationEvent.MaxOccurrences.HasValue && GetOccurrences(prototype) >= stationEvent.MaxOccurrences.Value)
if (stationEvent.MaxOccurrences.HasValue && GetOccurrences(stationEvent) >= stationEvent.MaxOccurrences.Value)
{
return false;
}
@@ -196,7 +185,7 @@ public sealed class EventManagerSystem : EntitySystem
return false;
}
var lastRun = TimeSinceLastEvent(prototype);
var lastRun = TimeSinceLastEvent(stationEvent);
if (lastRun != TimeSpan.Zero && currentTime.TotalMinutes <
stationEvent.ReoccurrenceDelay + lastRun.TotalMinutes)
{