using Content.Server._CP14.Shuttles.Components;
using Content.Server.Shuttles.Components;
using Content.Server.Shuttles.Events;
using Content.Server.Shuttles.Systems;
using Content.Server.Spawners.Components;
using Content.Server.Station.Events;
using Content.Server.Station.Systems;
using Content.Shared.CCVar;
using Content.Shared.Movement.Components;
using Robust.Server.GameObjects;
using Robust.Shared.Configuration;
using Robust.Shared.Map;
using Robust.Shared.Random;
namespace Content.Server._CP14.Shuttles;
public sealed class CP14ExpeditionSystem : EntitySystem
{
[Dependency] private readonly IConfigurationManager _cfgManager = default!;
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly MapLoaderSystem _loader = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
[Dependency] private readonly ShuttleSystem _shuttles = default!;
[Dependency] private readonly StationSpawningSystem _stationSpawning = default!;
///
/// Flags if all players must arrive via the Arrivals system, or if they can spawn in other ways.
///
public float ArrivalTime { get; private set; }
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent(OnPostInitSetupExpeditionShip);
SubscribeLocalEvent(OnArrivalsDocked);
ArrivalTime = _cfgManager.GetCVar(CCVars.CP14ExpeditionArrivalTime);
_cfgManager.OnValueChanged(CCVars.CP14ExpeditionArrivalTime, time => ArrivalTime = time, true);
}
private void OnPostInitSetupExpeditionShip(Entity station, ref StationPostInitEvent args)
{
if (!Deleted(station.Comp.Shuttle))
return;
var dummyMap = _mapManager.CreateMap();
if (_loader.TryLoad(dummyMap, station.Comp.ShuttlePath.ToString(), out var shuttleUids))
{
var shuttle = shuttleUids[0];
station.Comp.Shuttle = shuttle;
var shuttleComp = Comp(station.Comp.Shuttle);
var expeditionShipComp = EnsureComp(station.Comp.Shuttle);
expeditionShipComp.Station = station;
var targetPoints = new List();
var targetEnumerator = EntityQueryEnumerator();
while (targetEnumerator.MoveNext(out var uid, out _, out _))
{
targetPoints.Add(uid);
}
var target = _random.Pick(targetPoints);
if (!HasComp(shuttle))
return;
var targetPos = _transform.GetWorldPosition(target);
var mapUid = _transform.GetMap(target);
if (mapUid == null)
return;
_shuttles.FTLToCoordinates(shuttle, shuttleComp, new EntityCoordinates(mapUid.Value, targetPos), Angle.Zero, hyperspaceTime: ArrivalTime, startupTime: 0.5f);
}
}
private void OnArrivalsDocked(Entity ent, ref FTLCompletedEvent args)
{
//Some announsement logic?
}
public bool TryGetExpeditionShip(out EntityUid? uid)
{
uid = null;
var arrivalsQuery = EntityQueryEnumerator();
while (arrivalsQuery.MoveNext(out var tempUid, out _))
{
uid = tempUid;
return true;
}
return false;
}
public void HandlePlayerSpawning(PlayerSpawningEvent ev)
{
if (ev.SpawnResult != null)
return;
if (!HasComp(ev.Station))
return;
TryGetExpeditionShip(out var ship);
if (!TryComp(ship, out TransformComponent? shipXform))
return;
var gridUid = shipXform.GridUid;
var points = EntityQueryEnumerator();
var possiblePositions = new List();
while (points.MoveNext(out var uid, out var spawnPoint, out var xform))
{
if (spawnPoint.SpawnType != SpawnPointType.LateJoin || xform.GridUid != gridUid)
continue;
possiblePositions.Add(xform.Coordinates);
}
if (possiblePositions.Count <= 0)
return;
var spawnLoc = _random.Pick(possiblePositions);
ev.SpawnResult = _stationSpawning.SpawnPlayerMob(
spawnLoc,
ev.Job,
ev.HumanoidCharacterProfile,
ev.Station);
EnsureComp(ev.SpawnResult.Value);
EnsureComp(ev.SpawnResult.Value);
}
}