282 lines
10 KiB
C#
282 lines
10 KiB
C#
using System.Numerics;
|
|
using Content.Server.Salvage.Expeditions;
|
|
using Content.Server.Salvage.Expeditions.Structure;
|
|
using Content.Server.Shuttles.Components;
|
|
using Content.Server.Shuttles.Events;
|
|
using Content.Server.Shuttles.Systems;
|
|
using Content.Server.Station.Components;
|
|
using Content.Shared.Chat;
|
|
using Content.Shared.Humanoid;
|
|
using Content.Shared.Mobs.Components;
|
|
using Content.Shared.Mobs.Systems;
|
|
using Content.Shared.Salvage.Expeditions;
|
|
using Robust.Shared.Map.Components;
|
|
using Robust.Shared.Player;
|
|
using Robust.Shared.Utility;
|
|
|
|
namespace Content.Server.Salvage;
|
|
|
|
public sealed partial class SalvageSystem
|
|
{
|
|
/*
|
|
* Handles actively running a salvage expedition.
|
|
*/
|
|
|
|
[Dependency] private readonly MobStateSystem _mobState = default!;
|
|
|
|
private void InitializeRunner()
|
|
{
|
|
SubscribeLocalEvent<FTLRequestEvent>(OnFTLRequest);
|
|
SubscribeLocalEvent<FTLStartedEvent>(OnFTLStarted);
|
|
SubscribeLocalEvent<FTLCompletedEvent>(OnFTLCompleted);
|
|
SubscribeLocalEvent<ConsoleFTLAttemptEvent>(OnConsoleFTLAttempt);
|
|
}
|
|
|
|
private void OnConsoleFTLAttempt(ref ConsoleFTLAttemptEvent ev)
|
|
{
|
|
if (!TryComp<TransformComponent>(ev.Uid, out var xform) ||
|
|
!TryComp<SalvageExpeditionComponent>(xform.MapUid, out var salvage))
|
|
{
|
|
return;
|
|
}
|
|
|
|
// TODO: This is terrible but need bluespace harnesses or something.
|
|
var query = EntityQueryEnumerator<HumanoidAppearanceComponent, MobStateComponent, TransformComponent>();
|
|
|
|
while (query.MoveNext(out var uid, out var _, out var mobState, out var mobXform))
|
|
{
|
|
if (mobXform.MapUid != xform.MapUid)
|
|
continue;
|
|
|
|
// Don't count unidentified humans (loot) or anyone you murdered so you can still maroon them once dead.
|
|
if (_mobState.IsDead(uid, mobState))
|
|
continue;
|
|
|
|
// Okay they're on salvage, so are they on the shuttle.
|
|
if (mobXform.GridUid != ev.Uid)
|
|
{
|
|
ev.Cancelled = true;
|
|
ev.Reason = Loc.GetString("salvage-expedition-not-all-present");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Announces status updates to salvage crewmembers on the state of the expedition.
|
|
/// </summary>
|
|
private void Announce(EntityUid mapUid, string text)
|
|
{
|
|
var mapId = Comp<MapComponent>(mapUid).MapId;
|
|
|
|
// I love TComms and chat!!!
|
|
_chat.ChatMessageToManyFiltered(
|
|
Filter.BroadcastMap(mapId),
|
|
ChatChannel.Radio,
|
|
text,
|
|
text,
|
|
_mapManager.GetMapEntityId(mapId),
|
|
false,
|
|
true,
|
|
null);
|
|
}
|
|
|
|
private void OnFTLRequest(ref FTLRequestEvent ev)
|
|
{
|
|
if (!HasComp<SalvageExpeditionComponent>(ev.MapUid) ||
|
|
!TryComp<FTLDestinationComponent>(ev.MapUid, out var dest))
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Only one shuttle can occupy an expedition.
|
|
dest.Enabled = false;
|
|
_shuttleConsoles.RefreshShuttleConsoles();
|
|
}
|
|
|
|
private void OnFTLCompleted(ref FTLCompletedEvent args)
|
|
{
|
|
if (!TryComp<SalvageExpeditionComponent>(args.MapUid, out var component))
|
|
return;
|
|
|
|
// Someone FTLd there so start announcement
|
|
if (component.Stage != ExpeditionStage.Added)
|
|
return;
|
|
|
|
Announce(args.MapUid, Loc.GetString("salvage-expedition-announcement-countdown-minutes", ("duration", (component.EndTime - _timing.CurTime).Minutes)));
|
|
|
|
if (component.DungeonLocation != Vector2.Zero)
|
|
Announce(args.MapUid, Loc.GetString("salvage-expedition-announcement-dungeon", ("direction", component.DungeonLocation.GetDir())));
|
|
|
|
component.Stage = ExpeditionStage.Running;
|
|
Dirty(component);
|
|
}
|
|
|
|
private void OnFTLStarted(ref FTLStartedEvent ev)
|
|
{
|
|
// Started a mining mission so work out exempt entities
|
|
if (TryComp<SalvageMiningExpeditionComponent>(
|
|
_mapManager.GetMapEntityId(ev.TargetCoordinates.ToMap(EntityManager, _transform).MapId),
|
|
out var mining))
|
|
{
|
|
var ents = new List<EntityUid>();
|
|
var xformQuery = GetEntityQuery<TransformComponent>();
|
|
MiningTax(ents, ev.Entity, mining, xformQuery);
|
|
mining.ExemptEntities = ents;
|
|
}
|
|
|
|
if (!TryComp<SalvageExpeditionComponent>(ev.FromMapUid, out var expedition) ||
|
|
!TryComp<SalvageExpeditionDataComponent>(expedition.Station, out var station))
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Check if any shuttles remain.
|
|
var query = EntityQueryEnumerator<ShuttleComponent, TransformComponent>();
|
|
|
|
while (query.MoveNext(out _, out var xform))
|
|
{
|
|
if (xform.MapUid == ev.FromMapUid)
|
|
return;
|
|
}
|
|
|
|
// Last shuttle has left so finish the mission.
|
|
QueueDel(ev.FromMapUid.Value);
|
|
}
|
|
|
|
// Runs the expedition
|
|
private void UpdateRunner()
|
|
{
|
|
// Generic missions
|
|
var query = EntityQueryEnumerator<SalvageExpeditionComponent>();
|
|
|
|
// Run the basic mission timers (e.g. announcements, auto-FTL, completion, etc)
|
|
while (query.MoveNext(out var uid, out var comp))
|
|
{
|
|
var remaining = comp.EndTime - _timing.CurTime;
|
|
|
|
if (comp.Stage < ExpeditionStage.FinalCountdown && remaining < TimeSpan.FromSeconds(45))
|
|
{
|
|
comp.Stage = ExpeditionStage.FinalCountdown;
|
|
Dirty(uid, comp);
|
|
Announce(uid, Loc.GetString("salvage-expedition-announcement-countdown-seconds", ("duration", TimeSpan.FromSeconds(45).Seconds)));
|
|
}
|
|
else if (comp.Stage < ExpeditionStage.MusicCountdown && remaining < TimeSpan.FromMinutes(2))
|
|
{
|
|
// TODO: Some way to play audio attached to a map for players.
|
|
comp.Stream = _audio.PlayGlobal(comp.Sound, Filter.BroadcastMap(Comp<MapComponent>(uid).MapId), true);
|
|
comp.Stage = ExpeditionStage.MusicCountdown;
|
|
Dirty(uid, comp);
|
|
Announce(uid, Loc.GetString("salvage-expedition-announcement-countdown-minutes", ("duration", TimeSpan.FromMinutes(2).Minutes)));
|
|
}
|
|
else if (comp.Stage < ExpeditionStage.Countdown && remaining < TimeSpan.FromMinutes(5))
|
|
{
|
|
comp.Stage = ExpeditionStage.Countdown;
|
|
Dirty(uid, comp);
|
|
Announce(uid, Loc.GetString("salvage-expedition-announcement-countdown-minutes", ("duration", TimeSpan.FromMinutes(5).Minutes)));
|
|
}
|
|
// Auto-FTL out any shuttles
|
|
else if (remaining < TimeSpan.FromSeconds(ShuttleSystem.DefaultStartupTime) + TimeSpan.FromSeconds(0.5))
|
|
{
|
|
var ftlTime = (float) remaining.TotalSeconds;
|
|
|
|
if (remaining < TimeSpan.FromSeconds(ShuttleSystem.DefaultStartupTime))
|
|
{
|
|
ftlTime = MathF.Max(0, (float) remaining.TotalSeconds - 0.5f);
|
|
}
|
|
|
|
ftlTime = MathF.Min(ftlTime, ShuttleSystem.DefaultStartupTime);
|
|
var shuttleQuery = AllEntityQuery<ShuttleComponent, TransformComponent>();
|
|
|
|
if (TryComp<StationDataComponent>(comp.Station, out var data))
|
|
{
|
|
foreach (var member in data.Grids)
|
|
{
|
|
while (shuttleQuery.MoveNext(out var shuttleUid, out var shuttle, out var shuttleXform))
|
|
{
|
|
if (shuttleXform.MapUid != uid || HasComp<FTLComponent>(shuttleUid))
|
|
continue;
|
|
|
|
_shuttle.FTLTravel(shuttleUid, shuttle, member, ftlTime);
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (remaining < TimeSpan.Zero)
|
|
{
|
|
QueueDel(uid);
|
|
}
|
|
}
|
|
|
|
// Mining missions: NOOP since it's handled after ftling
|
|
|
|
// Structure missions
|
|
var structureQuery = EntityQueryEnumerator<SalvageStructureExpeditionComponent, SalvageExpeditionComponent>();
|
|
|
|
while (structureQuery.MoveNext(out var uid, out var structure, out var comp))
|
|
{
|
|
if (comp.Completed)
|
|
continue;
|
|
|
|
var structureAnnounce = false;
|
|
|
|
for (var i = 0; i < structure.Structures.Count; i++)
|
|
{
|
|
var objective = structure.Structures[i];
|
|
|
|
if (Deleted(objective))
|
|
{
|
|
structure.Structures.RemoveSwap(i);
|
|
structureAnnounce = true;
|
|
}
|
|
}
|
|
|
|
if (structureAnnounce)
|
|
{
|
|
Announce(uid, Loc.GetString("salvage-expedition-structure-remaining", ("count", structure.Structures.Count)));
|
|
}
|
|
|
|
if (structure.Structures.Count == 0)
|
|
{
|
|
comp.Completed = true;
|
|
Announce(uid, Loc.GetString("salvage-expedition-completed"));
|
|
}
|
|
}
|
|
|
|
// Elimination missions
|
|
var eliminationQuery = EntityQueryEnumerator<SalvageEliminationExpeditionComponent, SalvageExpeditionComponent>();
|
|
while (eliminationQuery.MoveNext(out var uid, out var elimination, out var comp))
|
|
{
|
|
if (comp.Completed)
|
|
continue;
|
|
|
|
var announce = false;
|
|
|
|
for (var i = 0; i < elimination.Megafauna.Count; i++)
|
|
{
|
|
var mob = elimination.Megafauna[i];
|
|
|
|
if (Deleted(mob) || _mobState.IsDead(mob))
|
|
{
|
|
elimination.Megafauna.RemoveSwap(i);
|
|
announce = true;
|
|
}
|
|
}
|
|
|
|
if (announce)
|
|
{
|
|
Announce(uid, Loc.GetString("salvage-expedition-megafauna-remaining", ("count", elimination.Megafauna.Count)));
|
|
}
|
|
|
|
if (elimination.Megafauna.Count == 0)
|
|
{
|
|
comp.Completed = true;
|
|
Announce(uid, Loc.GetString("salvage-expedition-completed"));
|
|
}
|
|
}
|
|
}
|
|
}
|