Files
OldThink/Content.Server/Maps/GameMapManager.cs
Moony 36181334b5 StationSystem/jobs/partial spawning refactor (#7580)
* Partial work on StationSystem refactor.

* WIP station jobs API.

* forgor to fire off grid events.

* Partial implementation of StationSpawningSystem

* whoops infinite loop.

* Spawners should work now.

* it compiles.

* tfw

* Vestigial code cleanup.

* fix station deletion.

* attempt to make tests go brr

* add latejoin spawnpoints to test maps.

* make sure the station still exists while destructing spawners.

* forgot an exists check.

* destruction order check.

* hopefully fix final test.

* fail-safe radstorm.

* Deep-clean job code further. This is bugged!!!!!

* Fix job bug. (init order moment)

* whooo cleanup

* New job selection algorithm that tries to distribute fairly across stations.

* small nitpicks

* Give the heads their weights to replace the head field.

* make overflow assign take a station list.

* moment

* Fixes and test #1 of many.

* please fix nullspace

* AssignJobs should no longer even consider showing up on a trace.

* add comment.

* Introduce station configs, praying i didn't miss something.

* in one small change stations are now fully serializable.

* Further doc comments.

* whoops.

* Solve bug where assignjobs didn't account for roundstart.

* Fix spawning, improve the API.
Caught an oversight in stationsystem that should've broke everything but didn't, whoops.

* Goodbye JobController.

* minor fix..

* fix test fail, remove debug logs.

* quick serialization fixes.

* fixes..

* sus

* partialing

* Update Content.Server/Station/Systems/StationJobsSystem.Roundstart.cs

Co-authored-by: Kara <lunarautomaton6@gmail.com>

* Use dirtying to avoid rebuilding the list 2,100 times.

* add a bajillion more lines of docs (mostly in AssignJobs so i don't ever forget how it works)

* Update Content.IntegrationTests/Tests/Station/StationJobsTest.cs

Co-authored-by: Kara <lunarautomaton6@gmail.com>

* Add the Mysteriously Missing Captain Check.

* Put maprender back the way it belongs.

* I love addressing reviews.

* Update Content.Server/Station/Systems/StationJobsSystem.cs

Co-authored-by: Kara <lunarautomaton6@gmail.com>

* doc cleanup.

* Fix bureaucratic error, add job slot tests.

* zero cost abstractions when

* cri

* saner error.

* Fix spawning failing certain tests due to gameticker not handling falliability correctly.
Can't fix this until I refactor the rest of spawning code.

* submodule gaming

* Packedenger.

* Documentation consistency.

Co-authored-by: Kara <lunarautomaton6@gmail.com>
2022-05-10 13:43:30 -05:00

199 lines
6.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Server.Chat.Managers;
using Content.Server.Station;
using Content.Shared.CCVar;
using Robust.Server.Maps;
using Robust.Server.Player;
using Robust.Shared.Configuration;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Server.Maps;
public sealed class GameMapManager : IGameMapManager
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly IChatManager _chatManager = default!;
[ViewVariables]
private readonly Queue<string> _previousMaps = new();
[ViewVariables]
private GameMapPrototype _currentMap = default!;
[ViewVariables]
private bool _currentMapForced;
[ViewVariables]
private bool _mapRotationEnabled;
[ViewVariables]
private int _mapQueueDepth = 1;
public void Initialize()
{
_configurationManager.OnValueChanged(CCVars.GameMap, value =>
{
if (TryLookupMap(value, out var map))
_currentMap = map;
else
throw new ArgumentException($"Unknown map prototype {value} was selected!");
}, true);
_configurationManager.OnValueChanged(CCVars.GameMapForced, value => _currentMapForced = value, true);
_configurationManager.OnValueChanged(CCVars.GameMapRotation, value => _mapRotationEnabled = value, true);
_configurationManager.OnValueChanged(CCVars.GameMapMemoryDepth, value =>
{
_mapQueueDepth = value;
// Drain excess.
while (_previousMaps.Count > _mapQueueDepth)
{
_previousMaps.Dequeue();
}
}, true);
var maps = AllVotableMaps().ToArray();
_random.Shuffle(maps);
foreach (var map in maps)
{
if (_previousMaps.Count >= _mapQueueDepth)
break;
_previousMaps.Enqueue(map.ID);
}
}
public IEnumerable<GameMapPrototype> CurrentlyEligibleMaps()
{
var maps = AllVotableMaps().Where(IsMapEligible).ToArray();
return maps.Length == 0 ? AllMaps().Where(x => x.Fallback) : maps;
}
public IEnumerable<GameMapPrototype> AllVotableMaps()
{
return _prototypeManager.EnumeratePrototypes<GameMapPrototype>().Where(x => x.Votable);
}
public IEnumerable<GameMapPrototype> AllMaps()
{
return _prototypeManager.EnumeratePrototypes<GameMapPrototype>();
}
public bool TrySelectMap(string gameMap)
{
if (!TryLookupMap(gameMap, out var map) || !IsMapEligible(map)) return false;
_currentMap = map;
_currentMapForced = false;
var ticker = EntitySystem.Get<GameTicking.GameTicker>();
ticker.UpdateInfoText();
return true;
}
public void ForceSelectMap(string gameMap)
{
if (!TryLookupMap(gameMap, out var map))
throw new ArgumentException($"The map \"{gameMap}\" is invalid!");
_currentMap = map;
_currentMapForced = true;
var ticker = EntitySystem.Get<GameTicking.GameTicker>();
ticker.UpdateInfoText();
}
public void SelectRandomMap()
{
var maps = CurrentlyEligibleMaps().ToList();
_currentMap = _random.Pick(maps);
_currentMapForced = false;
var ticker = EntitySystem.Get<GameTicking.GameTicker>();
ticker.UpdateInfoText();
}
public GameMapPrototype GetSelectedMap()
{
if (!_mapRotationEnabled || _currentMapForced)
return _currentMap;
return SelectMapInQueue() ?? CurrentlyEligibleMaps().First();
}
public GameMapPrototype GetSelectedMapChecked(bool loud = false, bool markAsPlayed = false)
{
if (!_currentMapForced && !IsMapEligible(GetSelectedMap()))
{
var oldMap = GetSelectedMap().MapName;
SelectRandomMap();
if (loud)
{
_chatManager.DispatchServerAnnouncement(
Loc.GetString("gamemap-could-not-use-map-error",
("oldMap", oldMap), ("newMap", GetSelectedMap().MapName)
));
}
}
var map = GetSelectedMap();
if (markAsPlayed)
EnqueueMap(map.ID);
return map;
}
public bool CheckMapExists(string gameMap)
{
return TryLookupMap(gameMap, out _);
}
private bool IsMapEligible(GameMapPrototype map)
{
return map.MaxPlayers >= _playerManager.PlayerCount &&
map.MinPlayers <= _playerManager.PlayerCount &&
map.Conditions.All(x => x.Check(map));
}
private bool TryLookupMap(string gameMap, [NotNullWhen(true)] out GameMapPrototype? map)
{
return _prototypeManager.TryIndex(gameMap, out map);
}
public int GetMapQueuePriority(string gameMapProtoName)
{
var i = 0;
foreach (var map in _previousMaps.Reverse())
{
if (map == gameMapProtoName)
return i;
i++;
}
return _mapQueueDepth;
}
public GameMapPrototype? SelectMapInQueue()
{
Logger.InfoS("mapsel", string.Join(", ", _previousMaps));
var eligible = CurrentlyEligibleMaps()
.Where(x => x.Votable)
.Select(x => (proto: x, weight: GetMapQueuePriority(x.ID)))
.OrderByDescending(x => x.weight).ToArray();
Logger.InfoS("mapsel", string.Join(", ", eligible.Select(x => (x.proto.ID, x.weight))));
if (eligible.Length is 0)
return null;
var weight = eligible[0].weight;
return eligible.Where(x => x.Item2 == weight).OrderBy(x => x.proto.ID).First().proto;
}
private void EnqueueMap(string mapProtoName)
{
_previousMaps.Enqueue(mapProtoName);
while (_previousMaps.Count > _mapQueueDepth)
{
_previousMaps.Dequeue();
}
}
}