Files
wwdpublic/Content.Server/StationEvents/EventManagerSystem.cs
PHCodes 93c3b03b11 Psionics (#44)
* Psionics

It's a ton of stuff relating to the basic Psionics system and all the powers.

I'm saving this as a bit of a sanity check before moving forward.

Left to do:
1. Implementing the Psionic faction so that the chat works as intended.
2. Adding the start-state cooldown timers to the actions.

* Cleaned up everything with the word 'Psionic' on it.

Got the psionic chat working. Got some other stuff working

* Some final psionic cleanup.

The last batch of content.

* Update RobustToolbox

* rebased

* Revert "Update RobustToolbox"

This reverts commit c0cf35d03f828f6ccfeb05fcffd91cf074818fc9.

* Update RobustToolbox

* Revert "Update RobustToolbox"

This reverts commit c4dc828df7912e063ea856b2a83a790bc88d1e09.

* Update RobustToolbox

* Psionics

It's a ton of stuff relating to the basic Psionics system and all the powers.

I'm saving this as a bit of a sanity check before moving forward.

Left to do:
1. Implementing the Psionic faction so that the chat works as intended.
2. Adding the start-state cooldown timers to the actions.

* Cleaned up everything with the word 'Psionic' on it.

Got the psionic chat working. Got some other stuff working

* Some final psionic cleanup.

The last batch of content.

* rebased

* Cleaned up everything with the word 'Psionic' on it.

Got the psionic chat working. Got some other stuff working

* Broken Commit

With these changes in place, the unit does not work. Recording them so i don't lose my work.

* Brings it All Together.

Dawn of the final Commit. Rebase completed.

* Update RobustToolbox

* Changed 'Station Events' to 'StationEvents' and cleaned up the Delta-V Events.yml file of duplicate events.

* Delete ghost_roles.yml

Duplicate.

* Update familiars.yml

* Update familiars.yml

* Update GlimmerReactiveSystem.cs

* Makes tinfoil hats craftable.

* Decided I'm not dealing with adding fugitives or Glimmer Wisps right now.

* Psionic invisibility won't work now that Eye component exists. Or at least, the integrator test won't psas.

* Update special.yml

* Added #nyanotrasen code or //Nyanotrasen code to many, many files.

* Properly fixes comments.

---------

Signed-off-by: Colin-Tel <113523727+Colin-Tel@users.noreply.github.com>
Signed-off-by: PHCodes <47927305+PHCodes@users.noreply.github.com>
Co-authored-by: Debug <sidneymaatman@gmail.com>
Co-authored-by: Colin-Tel <113523727+Colin-Tel@users.noreply.github.com>
2023-10-08 20:07:53 +02:00

223 lines
7.0 KiB
C#

using System.Linq;
using Content.Server.GameTicking;
using Content.Server.StationEvents.Components;
using Content.Shared.CCVar;
using Robust.Server.Player;
using Robust.Shared.Configuration;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Content.Server.Psionics.Glimmer;
using Content.Shared.Psionics.Glimmer;
namespace Content.Server.StationEvents;
public sealed class EventManagerSystem : EntitySystem
{
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly IPrototypeManager _prototype = default!;
[Dependency] public readonly GameTicker GameTicker = default!;
[Dependency] private readonly GlimmerSystem _glimmerSystem = default!; //Nyano - Summary: pulls in the glimmer system.
private ISawmill _sawmill = default!;
public bool EventsEnabled { get; private set; }
private void SetEnabled(bool value) => EventsEnabled = value;
public override void Initialize()
{
base.Initialize();
_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;
if (component.EndTime != null)
component.EndTime = component.EndTime.Value + args.PausedTime;
}
public override void Shutdown()
{
base.Shutdown();
_configurationManager.UnsubValueChanged(CCVars.EventsEnabled, SetEnabled);
}
/// <summary>
/// Randomly runs a valid event.
/// </summary>
public string RunRandomEvent()
{
var randomEvent = PickRandomEvent();
if (randomEvent == null)
{
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)));
_sawmill.Info(str);
return str;
}
/// <summary>
/// Randomly picks a valid event.
/// </summary>
public string? PickRandomEvent()
{
var availableEvents = AvailableEvents();
_sawmill.Info($"Picking from {availableEvents.Count} total available events");
return FindEvent(availableEvents);
}
/// <summary>
/// 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)
{
if (availableEvents.Count == 0)
{
_sawmill.Warning("No events were available to run!");
return null;
}
var sumOfWeights = 0;
foreach (var stationEvent in availableEvents.Values)
{
sumOfWeights += (int) stationEvent.Weight;
}
sumOfWeights = _random.Next(sumOfWeights);
foreach (var (proto, stationEvent) in availableEvents)
{
sumOfWeights -= (int) stationEvent.Weight;
if (sumOfWeights <= 0)
{
return proto.ID;
}
}
_sawmill.Error("Event was not found after weighted pick process!");
return null;
}
/// <summary>
/// Gets the events that have met their player count, time-until start, etc.
/// </summary>
/// <param name="ignoreEarliestStart"></param>
/// <returns></returns>
private Dictionary<EntityPrototype, StationEventComponent> AvailableEvents(bool ignoreEarliestStart = false)
{
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 (CanRun(proto, stationEvent, playerCount, currentTime))
{
_sawmill.Debug($"Adding event {proto.ID} to possibilities");
result.Add(proto, stationEvent);
}
}
return result;
}
public Dictionary<EntityPrototype, StationEventComponent> 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;
}
private int GetOccurrences(EntityPrototype stationEvent)
{
return GetOccurrences(stationEvent.ID);
}
private int GetOccurrences(string stationEvent)
{
return GameTicker.AllPreviousGameRules.Count(p => p.Item2 == stationEvent);
}
public TimeSpan TimeSinceLastEvent(EntityPrototype stationEvent)
{
foreach (var (time, rule) in GameTicker.AllPreviousGameRules.Reverse())
{
if (rule == stationEvent.ID)
return time;
}
return TimeSpan.Zero;
}
private bool CanRun(EntityPrototype prototype, StationEventComponent stationEvent, int playerCount, TimeSpan currentTime)
{
if (GameTicker.IsGameRuleActive(prototype.ID))
return false;
if (stationEvent.MaxOccurrences.HasValue && GetOccurrences(prototype) >= stationEvent.MaxOccurrences.Value)
{
return false;
}
if (playerCount < stationEvent.MinimumPlayers)
{
return false;
}
if (currentTime != TimeSpan.Zero && currentTime.TotalMinutes < stationEvent.EarliestStart)
{
return false;
}
var lastRun = TimeSinceLastEvent(prototype);
if (lastRun != TimeSpan.Zero && currentTime.TotalMinutes <
stationEvent.ReoccurrenceDelay + lastRun.TotalMinutes)
{
return false;
}
// Nyano - Summary: - Begin modified code block: check for glimmer events.
// This could not be cleanly done anywhere else.
if (_configurationManager.GetCVar(CCVars.GlimmerEnabled) &&
prototype.TryGetComponent<GlimmerEventComponent>(out var glimmerEvent) &&
(_glimmerSystem.Glimmer < glimmerEvent.MinimumGlimmer ||
_glimmerSystem.Glimmer > glimmerEvent.MaximumGlimmer))
{
return false;
}
// Nyano - End modified code block.
return true;
}
}