* Use new Subs.CVar helper Removes manual config OnValueChanged calls, removes need to remember to manually unsubscribe. This both reduces boilerplate and fixes many issues where subscriptions weren't removed on entity system shutdown. * Fix a bunch of warnings * More warning fixes * Use new DateTime serializer to get rid of ISerializationHooks in changelog code. * Get rid of some more ISerializationHooks for enums * And a little more * Apply suggestions from code review Co-authored-by: 0x6273 <0x40@keemail.me> --------- Co-authored-by: 0x6273 <0x40@keemail.me>
191 lines
6.2 KiB
C#
191 lines
6.2 KiB
C#
using System.Linq;
|
|
using System.Threading;
|
|
using Content.Server.Salvage.Expeditions;
|
|
using Content.Server.Salvage.Expeditions.Structure;
|
|
using Content.Shared.CCVar;
|
|
using Content.Shared.Examine;
|
|
using Content.Shared.Salvage.Expeditions;
|
|
using Robust.Shared.CPUJob.JobQueues;
|
|
using Robust.Shared.CPUJob.JobQueues.Queues;
|
|
using Robust.Shared.GameStates;
|
|
|
|
namespace Content.Server.Salvage;
|
|
|
|
public sealed partial class SalvageSystem
|
|
{
|
|
/*
|
|
* Handles setup / teardown of salvage expeditions.
|
|
*/
|
|
|
|
private const int MissionLimit = 3;
|
|
|
|
private readonly JobQueue _salvageQueue = new();
|
|
private readonly List<(SpawnSalvageMissionJob Job, CancellationTokenSource CancelToken)> _salvageJobs = new();
|
|
private const double SalvageJobTime = 0.002;
|
|
|
|
private float _cooldown;
|
|
|
|
private void InitializeExpeditions()
|
|
{
|
|
SubscribeLocalEvent<SalvageExpeditionConsoleComponent, ComponentInit>(OnSalvageConsoleInit);
|
|
SubscribeLocalEvent<SalvageExpeditionConsoleComponent, EntParentChangedMessage>(OnSalvageConsoleParent);
|
|
SubscribeLocalEvent<SalvageExpeditionConsoleComponent, ClaimSalvageMessage>(OnSalvageClaimMessage);
|
|
|
|
SubscribeLocalEvent<SalvageExpeditionDataComponent, EntityUnpausedEvent>(OnDataUnpaused);
|
|
|
|
SubscribeLocalEvent<SalvageExpeditionComponent, ComponentShutdown>(OnExpeditionShutdown);
|
|
SubscribeLocalEvent<SalvageExpeditionComponent, EntityUnpausedEvent>(OnExpeditionUnpaused);
|
|
SubscribeLocalEvent<SalvageExpeditionComponent, ComponentGetState>(OnExpeditionGetState);
|
|
|
|
SubscribeLocalEvent<SalvageStructureComponent, ExaminedEvent>(OnStructureExamine);
|
|
|
|
_cooldown = _configurationManager.GetCVar(CCVars.SalvageExpeditionCooldown);
|
|
Subs.CVar(_configurationManager, CCVars.SalvageExpeditionCooldown, SetCooldownChange);
|
|
}
|
|
|
|
private void OnExpeditionGetState(EntityUid uid, SalvageExpeditionComponent component, ref ComponentGetState args)
|
|
{
|
|
args.State = new SalvageExpeditionComponentState()
|
|
{
|
|
Stage = component.Stage
|
|
};
|
|
}
|
|
|
|
private void SetCooldownChange(float obj)
|
|
{
|
|
// Update the active cooldowns if we change it.
|
|
var diff = obj - _cooldown;
|
|
|
|
var query = AllEntityQuery<SalvageExpeditionDataComponent>();
|
|
|
|
while (query.MoveNext(out var comp))
|
|
{
|
|
comp.NextOffer += TimeSpan.FromSeconds(diff);
|
|
}
|
|
|
|
_cooldown = obj;
|
|
}
|
|
|
|
private void OnExpeditionShutdown(EntityUid uid, SalvageExpeditionComponent component, ComponentShutdown args)
|
|
{
|
|
component.Stream = _audio.Stop(component.Stream);
|
|
|
|
foreach (var (job, cancelToken) in _salvageJobs.ToArray())
|
|
{
|
|
if (job.Station == component.Station)
|
|
{
|
|
cancelToken.Cancel();
|
|
_salvageJobs.Remove((job, cancelToken));
|
|
}
|
|
}
|
|
|
|
if (Deleted(component.Station))
|
|
return;
|
|
|
|
// Finish mission
|
|
if (TryComp<SalvageExpeditionDataComponent>(component.Station, out var data))
|
|
{
|
|
FinishExpedition((component.Station, data), uid);
|
|
}
|
|
}
|
|
|
|
private void OnDataUnpaused(EntityUid uid, SalvageExpeditionDataComponent component, ref EntityUnpausedEvent args)
|
|
{
|
|
component.NextOffer += args.PausedTime;
|
|
}
|
|
|
|
private void OnExpeditionUnpaused(EntityUid uid, SalvageExpeditionComponent component, ref EntityUnpausedEvent args)
|
|
{
|
|
component.EndTime += args.PausedTime;
|
|
}
|
|
|
|
private void UpdateExpeditions()
|
|
{
|
|
var currentTime = _timing.CurTime;
|
|
_salvageQueue.Process();
|
|
|
|
foreach (var (job, cancelToken) in _salvageJobs.ToArray())
|
|
{
|
|
switch (job.Status)
|
|
{
|
|
case JobStatus.Finished:
|
|
_salvageJobs.Remove((job, cancelToken));
|
|
break;
|
|
}
|
|
}
|
|
|
|
var query = EntityQueryEnumerator<SalvageExpeditionDataComponent>();
|
|
while (query.MoveNext(out var uid, out var comp))
|
|
{
|
|
// Update offers
|
|
if (comp.NextOffer > currentTime || comp.Claimed)
|
|
continue;
|
|
|
|
comp.Cooldown = false;
|
|
comp.NextOffer += TimeSpan.FromSeconds(_cooldown);
|
|
GenerateMissions(comp);
|
|
UpdateConsoles((uid, comp));
|
|
}
|
|
}
|
|
|
|
private void FinishExpedition(Entity<SalvageExpeditionDataComponent> expedition, EntityUid uid)
|
|
{
|
|
var component = expedition.Comp;
|
|
component.NextOffer = _timing.CurTime + TimeSpan.FromSeconds(_cooldown);
|
|
Announce(uid, Loc.GetString("salvage-expedition-mission-completed"));
|
|
component.ActiveMission = 0;
|
|
component.Cooldown = true;
|
|
UpdateConsoles(expedition);
|
|
}
|
|
|
|
private void GenerateMissions(SalvageExpeditionDataComponent component)
|
|
{
|
|
component.Missions.Clear();
|
|
|
|
for (var i = 0; i < MissionLimit; i++)
|
|
{
|
|
var mission = new SalvageMissionParams
|
|
{
|
|
Index = component.NextIndex,
|
|
Seed = _random.Next(),
|
|
Difficulty = "Moderate",
|
|
};
|
|
|
|
component.Missions[component.NextIndex++] = mission;
|
|
}
|
|
}
|
|
|
|
private SalvageExpeditionConsoleState GetState(SalvageExpeditionDataComponent component)
|
|
{
|
|
var missions = component.Missions.Values.ToList();
|
|
return new SalvageExpeditionConsoleState(component.NextOffer, component.Claimed, component.Cooldown, component.ActiveMission, missions);
|
|
}
|
|
|
|
private void SpawnMission(SalvageMissionParams missionParams, EntityUid station)
|
|
{
|
|
var cancelToken = new CancellationTokenSource();
|
|
var job = new SpawnSalvageMissionJob(
|
|
SalvageJobTime,
|
|
EntityManager,
|
|
_timing,
|
|
_logManager,
|
|
_mapManager,
|
|
_prototypeManager,
|
|
_anchorable,
|
|
_biome,
|
|
_dungeon,
|
|
_metaData,
|
|
station,
|
|
missionParams,
|
|
cancelToken.Token);
|
|
|
|
_salvageJobs.Add((job, cancelToken));
|
|
_salvageQueue.EnqueueJob(job);
|
|
}
|
|
|
|
private void OnStructureExamine(EntityUid uid, SalvageStructureComponent component, ExaminedEvent args)
|
|
{
|
|
args.PushMarkup(Loc.GetString("salvage-expedition-structure-examine"));
|
|
}
|
|
}
|