fish-station/Content.Server/_Sunrise/PlanetPrison/PlanetPrisonStationSystem.cs
2025-02-19 14:13:11 +03:00

148 lines
5.3 KiB
C#

using System.Linq;
using System.Numerics;
using Content.Server._Sunrise.NightDayMapLight;
using Content.Server.Chat.Managers;
using Content.Server.GameTicking;
using Content.Server.Maps;
using Content.Server.Parallax;
using Content.Server.Shuttles.Systems;
using Content.Shared._Sunrise.AlwaysPoweredMap;
using Content.Shared._Sunrise.Shuttles;
using Content.Shared._Sunrise.SunriseCCVars;
using Content.Shared.Parallax.Biomes;
using Content.Shared.Salvage;
using Content.Shared.Shuttles.Components;
using Robust.Server.GameObjects;
using Robust.Shared.Configuration;
using Robust.Shared.EntitySerialization;
using Robust.Shared.Map;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Server._Sunrise.PlanetPrison;
public sealed class PlanetPrisonStationSystem : EntitySystem
{
[Dependency] private readonly ISharedPlayerManager _player = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IChatManager _chat = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly MapSystem _map = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly GameTicker _gameTicker = default!;
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly BiomeSystem _biomeSystem = default!;
[Dependency] private readonly IPrototypeManager _protoManager = default!;
[Dependency] private readonly IEntityManager _entManager = default!;
[Dependency] private readonly ShuttleSystem _shuttle = default!;
private ISawmill _sawmill = default!;
public override void Initialize()
{
_sawmill = Logger.GetSawmill("station.prison");
SubscribeLocalEvent<PlanetPrisonStationComponent, ComponentInit>(OnPlanetPrisonStationInit);
SubscribeLocalEvent<PlanetPrisonStationComponent, ComponentShutdown>(OnPrisonShutdown);
}
private void OnPrisonShutdown(EntityUid uid, PlanetPrisonStationComponent component, ComponentShutdown args)
{
QueueDel(component.Entity);
component.Entity = EntityUid.Invalid;
if (_mapManager.MapExists(component.MapId))
_mapManager.DeleteMap(component.MapId);
component.MapId = MapId.Nullspace;
}
private void OnPlanetPrisonStationInit(EntityUid uid, PlanetPrisonStationComponent component, ComponentInit args)
{
var enable = _cfg.GetCVar(SunriseCCVars.MinPlayersEnable);
if (!enable)
return;
var minPlayers = _cfg.GetCVar(SunriseCCVars.MinPlayersPlanetPrison);
if (_player.PlayerCount <= minPlayers)
{
_chat.DispatchServerAnnouncement(Loc.GetString("planet-prison-not-enough-players", ("minimumPlayers", minPlayers)), Color.OrangeRed);
return;
}
if (TryComp<TransformComponent>(component.Entity, out var xform))
{
component.MapId = xform.MapID;
return;
}
AddPlanetPrison(component);
}
private void AddPlanetPrison(PlanetPrisonStationComponent component)
{
var query = AllEntityQuery<PlanetPrisonStationComponent>();
while (query.MoveNext(out var otherComp))
{
if (otherComp == component)
continue;
component.MapId = otherComp.MapId;
return;
}
var station = _random.Pick(component.Stations);
if (!_protoManager.TryIndex<BiomeTemplatePrototype>(_random.Pick(component.Biomes), out var biome))
{
_sawmill.Warning("No Prison map found, skipping setup.");
return;
}
if (!_prototypeManager.TryIndex<GameMapPrototype>(station, out var gameMap))
{
_sawmill.Warning("No Prison map found, skipping setup.");
return;
}
_chat.DispatchServerAnnouncement(Loc.GetString("planet-prison-select-map", ("stationName", gameMap.MapName)), Color.LightBlue);
_chat.DispatchServerAnnouncement(Loc.GetString("planet-prison-select-biome", ("biomeName", biome.ID)), Color.LightBlue);
var uids = _gameTicker.LoadGameMap(gameMap, out var mapId, rot: Angle.Zero);
if (uids.Count != 1)
{
_sawmill.Warning("Prison station have more 1 grid.");
QueueDel(component.Entity);
component.Entity = EntityUid.Invalid;
if (_mapManager.MapExists(component.MapId))
_mapManager.DeleteMap(component.MapId);
component.MapId = MapId.Nullspace;
return;
}
EnsureComp<IgnoreFtlCheckComponent>(uids[0]);
component.PrisonGrid = uids[0];
var mapUid = _mapManager.GetMapEntityId(mapId);
_biomeSystem.EnsurePlanet(mapUid, biome);
// Sunrise-Start
var restricted = new RestrictedRangeComponent
{
Origin = new Vector2(0, 0),
Range = 160,
};
AddComp(mapUid, restricted);
// Sunrise-End
EnsureComp<AlwaysPoweredMapComponent>(mapUid);
EnsureComp<NightDayMapLightComponent>(mapUid);
var destComp = _entManager.EnsureComponent<FTLDestinationComponent>(mapUid);
destComp.BeaconsOnly = true;
_shuttle.SetFTLWhitelist(mapUid, component.ShuttleWhitelist);
}
}