fish-station/Content.Server/_Sunrise/PlanetPrison/PlanetPrisonStationSystem.cs
2025-12-26 17:44:36 +03:00

150 lines
5.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Numerics;
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.Shuttles;
using Content.Shared._Sunrise.SunriseCCVars;
using Content.Shared.Light.Components;
using Content.Shared.Maps;
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;
// TODO: Рефактор с целью устранения варнингов и перехода системы на более современное API
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 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!;
public override void Initialize()
{
SubscribeLocalEvent<PlanetPrisonStationComponent, ComponentInit>(OnPlanetPrisonStationInit);
SubscribeLocalEvent<PlanetPrisonStationComponent, ComponentShutdown>(OnPrisonShutdown);
Log.Level = LogLevel.Info;
}
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 (component.MapId != MapId.Nullspace)
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 = ChooseType(component);
if (!_protoManager.TryIndex(_random.Pick(component.Biomes), out var biome))
{
Log.Warning("No Prison map found, skipping setup.");
return;
}
if (!_protoManager.TryIndex(station, out var gameMap))
{
Log.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 opts = DeserializationOptions.Default with {InitializeMaps = true};
var uids = _gameTicker.LoadGameMap(gameMap, out var mapId, opts, rot: Angle.Zero);
component.MapId = mapId;
if (uids.Count != 1)
{
Log.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);
var restricted = new RestrictedRangeComponent
{
Origin = new Vector2(0, 0),
Range = 200,
};
AddComp(mapUid, restricted);
EnsureComp<LightCycleComponent>(mapUid);
var destComp = _entManager.EnsureComponent<FTLDestinationComponent>(mapUid);
destComp.BeaconsOnly = true;
_shuttle.SetFTLWhitelist(mapUid, component.ShuttleWhitelist);
}
private ProtoId<GameMapPrototype> ChooseType(PlanetPrisonStationComponent component)
{
return _cfg.GetCVar(SunriseCCVars.PlanetPrisonModern)
? _random.Pick(component.StationsModern)
: _random.Pick(component.StationsOld);
}
}