Refactor how jobs are handed out (#5422)
* Completely refactor how job spawning works * Remove remains of old system. * Squash the final bug, cleanup. * Attempt to fix tests * Adjusts packed's round-start crew roster, re-enables a bunch of old roles. Also adds the Central Command Official as a proper role. * pretty up ui * refactor StationSystem into the correct folder & namespace. * remove a log, make sure the lobby gets updated if a new map is spontaneously added. * re-add accidentally removed log * We do a little logging * we do a little resolving * we do a little documenting * Renamed OverflowJob to FallbackOverflowJob Allows stations to configure their own roundstart overflow job list. * narrator: it did not compile * oops * support having no overflow jobs * filescope for consistency * small fixes * Bumps a few role counts for Packed, namely engis * log moment * E * Update Resources/Prototypes/Entities/Objects/Misc/identification_cards.yml Co-authored-by: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> * Update Content.Server/Maps/GameMapPrototype.cs Co-authored-by: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> * factored job logic, cleanup. * e * Address reviews * Remove the concept of a "default" grid. It has no future in our new multi-station world * why was clickable using that in the first place * fix bad evil bug that almost slipped through also adds chemist * rms obsolete things from chemist * Adds a sanity fallback * address reviews * adds ability to set name * fuck * cleanup joingame
This commit is contained in:
66
Content.Server/Administration/Commands/LoadGameMapCommand.cs
Normal file
66
Content.Server/Administration/Commands/LoadGameMapCommand.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using Content.Server.Maps;
|
||||
using Content.Server.Roles;
|
||||
using Content.Server.Station;
|
||||
using Content.Shared.Administration;
|
||||
using Robust.Server.Maps;
|
||||
using Robust.Shared.Console;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Server.Administration.Commands
|
||||
{
|
||||
[AdminCommand(AdminFlags.Fun)]
|
||||
public sealed class LoadGameMapCommand : IConsoleCommand
|
||||
{
|
||||
public string Command => "loadgamemap";
|
||||
|
||||
public string Description => "Loads the given game map at the given coordinates.";
|
||||
|
||||
public string Help => "loadgamemap <gamemap> <mapid> [<x> <y> [<name>]] ";
|
||||
|
||||
public void Execute(IConsoleShell shell, string argStr, string[] args)
|
||||
{
|
||||
var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
|
||||
var mapLoader = IoCManager.Resolve<IMapLoader>();
|
||||
var entityManager = IoCManager.Resolve<IEntityManager>();
|
||||
var stationSystem = entityManager.EntitySysManager.GetEntitySystem<StationSystem>();
|
||||
|
||||
if (args.Length is not (2 or 4 or 5))
|
||||
{
|
||||
shell.WriteError(Loc.GetString("shell-wrong-arguments-number"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (prototypeManager.TryIndex<GameMapPrototype>(args[0], out var gameMap))
|
||||
{
|
||||
if (int.TryParse(args[1], out var mapId))
|
||||
{
|
||||
var gameMapEnt = mapLoader.LoadBlueprint(new MapId(mapId), gameMap.MapPath);
|
||||
if (gameMapEnt is null)
|
||||
{
|
||||
shell.WriteError($"Failed to create the given game map, is the path {gameMap.MapPath} correct?");
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.Length >= 4 && int.TryParse(args[2], out var x) && int.TryParse(args[3], out var y))
|
||||
{
|
||||
var transform = entityManager.GetComponent<TransformComponent>(gameMapEnt.GridEntityId);
|
||||
transform.WorldPosition = new Vector2(x, y);
|
||||
}
|
||||
|
||||
var stationName = args.Length == 5 ? args[4] : null;
|
||||
|
||||
stationSystem.InitialSetupStationGrid(gameMapEnt.GridEntityId, gameMap, stationName);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
shell.WriteError($"The given map prototype {args[0]} is invalid.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.Administration;
|
||||
using Content.Server.Roles;
|
||||
using Content.Server.Station;
|
||||
using Content.Shared.Roles;
|
||||
using Content.Shared.Station;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Console;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Server.GameTicking.Commands
|
||||
@@ -24,35 +28,47 @@ namespace Content.Server.GameTicking.Commands
|
||||
}
|
||||
public void Execute(IConsoleShell shell, string argStr, string[] args)
|
||||
{
|
||||
if (args.Length != 2)
|
||||
{
|
||||
shell.WriteError(Loc.GetString("shell-wrong-arguments-number"));
|
||||
return;
|
||||
}
|
||||
|
||||
var player = shell.Player as IPlayerSession;
|
||||
var output = string.Join(".", args);
|
||||
|
||||
if (player == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var ticker = EntitySystem.Get<GameTicker>();
|
||||
var stationSystem = EntitySystem.Get<StationSystem>();
|
||||
if (ticker.RunLevel == GameRunLevel.PreRoundLobby)
|
||||
{
|
||||
shell.WriteLine("Round has not started.");
|
||||
return;
|
||||
}
|
||||
else if(ticker.RunLevel == GameRunLevel.InRound)
|
||||
else if (ticker.RunLevel == GameRunLevel.InRound)
|
||||
{
|
||||
string ID = args[0];
|
||||
var positions = ticker.GetAvailablePositions();
|
||||
string id = args[0];
|
||||
|
||||
if(positions.GetValueOrDefault(ID, 0) == 0) //n < 0 is treated as infinite
|
||||
if (!uint.TryParse(args[1], out var sid))
|
||||
{
|
||||
var jobPrototype = _prototypeManager.Index<JobPrototype>(ID);
|
||||
shell.WriteError(Loc.GetString("shell-argument-must-be-number"));
|
||||
}
|
||||
|
||||
var stationId = new StationId(sid);
|
||||
if(!stationSystem.IsJobAvailableOnStation(stationId, id))
|
||||
{
|
||||
var jobPrototype = _prototypeManager.Index<JobPrototype>(id);
|
||||
shell.WriteLine($"{jobPrototype.Name} has no available slots.");
|
||||
return;
|
||||
}
|
||||
ticker.MakeJoinGame(player, args[0]);
|
||||
ticker.MakeJoinGame(player, stationId, id);
|
||||
return;
|
||||
}
|
||||
|
||||
ticker.MakeJoinGame(player);
|
||||
ticker.MakeJoinGame(player, StationId.Invalid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
@@ -6,6 +5,7 @@ using System.Linq;
|
||||
using Content.Shared.GameTicking;
|
||||
using Content.Shared.Preferences;
|
||||
using Content.Shared.Roles;
|
||||
using Content.Shared.Station;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Network;
|
||||
@@ -25,131 +25,90 @@ namespace Content.Server.GameTicking
|
||||
[ViewVariables]
|
||||
private readonly Dictionary<string, int> _spawnedPositions = new();
|
||||
|
||||
private Dictionary<IPlayerSession, string> AssignJobs(List<IPlayerSession> available,
|
||||
private Dictionary<IPlayerSession, (string, StationId)> AssignJobs(List<IPlayerSession> available,
|
||||
Dictionary<NetUserId, HumanoidCharacterProfile> profiles)
|
||||
{
|
||||
// Calculate positions available round-start for each job.
|
||||
var availablePositions = GetBasePositions(true);
|
||||
var assigned = new Dictionary<IPlayerSession, (string, StationId)>();
|
||||
|
||||
// Output dictionary of assigned jobs.
|
||||
var assigned = new Dictionary<IPlayerSession, string>();
|
||||
|
||||
// Go over each priority level top to bottom.
|
||||
for (var i = JobPriority.High; i > JobPriority.Never; i--)
|
||||
List<(IPlayerSession, List<string>)> GetPlayersJobCandidates(bool heads, JobPriority i)
|
||||
{
|
||||
void ProcessJobs(bool heads)
|
||||
{
|
||||
// Get all candidates for this priority & heads combo.
|
||||
// That is all people with at LEAST one job at this priority & heads level,
|
||||
// and the jobs they have selected here.
|
||||
var candidates = available
|
||||
.Select(player =>
|
||||
{
|
||||
var profile = profiles[player.UserId];
|
||||
|
||||
var availableJobs = profile.JobPriorities
|
||||
.Where(j =>
|
||||
{
|
||||
var (jobId, priority) = j;
|
||||
if (!_prototypeManager.TryIndex(jobId, out JobPrototype? job))
|
||||
{
|
||||
// Job doesn't exist, probably old data?
|
||||
return false;
|
||||
}
|
||||
if (job.IsHead != heads)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return priority == i;
|
||||
})
|
||||
.Select(j => j.Key)
|
||||
.ToList();
|
||||
|
||||
return (player, availableJobs);
|
||||
})
|
||||
.Where(p => p.availableJobs.Count != 0)
|
||||
.ToList();
|
||||
|
||||
_robustRandom.Shuffle(candidates);
|
||||
|
||||
foreach (var (candidate, jobs) in candidates)
|
||||
return available.Select(player =>
|
||||
{
|
||||
while (jobs.Count != 0)
|
||||
{
|
||||
var picked = _robustRandom.Pick(jobs);
|
||||
var profile = profiles[player.UserId];
|
||||
|
||||
var openPositions = availablePositions.GetValueOrDefault(picked, 0);
|
||||
if (openPositions == 0)
|
||||
var availableJobs = profile.JobPriorities
|
||||
.Where(j =>
|
||||
{
|
||||
jobs.Remove(picked);
|
||||
continue;
|
||||
}
|
||||
var (jobId, priority) = j;
|
||||
if (!_prototypeManager.TryIndex(jobId, out JobPrototype? job))
|
||||
{
|
||||
// Job doesn't exist, probably old data?
|
||||
return false;
|
||||
}
|
||||
|
||||
availablePositions[picked] -= 1;
|
||||
assigned.Add(candidate, picked);
|
||||
break;
|
||||
if (job.IsHead != heads)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return priority == i;
|
||||
})
|
||||
.Select(j => j.Key)
|
||||
.ToList();
|
||||
|
||||
return (player, availableJobs);
|
||||
})
|
||||
.Where(p => p.availableJobs.Count != 0)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
void ProcessJobs(bool heads, Dictionary<string, int> availablePositions, StationId id, JobPriority i)
|
||||
{
|
||||
var candidates = GetPlayersJobCandidates(heads, i);
|
||||
|
||||
foreach (var (candidate, jobs) in candidates)
|
||||
{
|
||||
while (jobs.Count != 0)
|
||||
{
|
||||
var picked = _robustRandom.Pick(jobs);
|
||||
|
||||
var openPositions = availablePositions.GetValueOrDefault(picked, 0);
|
||||
if (openPositions == 0)
|
||||
{
|
||||
jobs.Remove(picked);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
available.RemoveAll(a => assigned.ContainsKey(a));
|
||||
availablePositions[picked] -= 1;
|
||||
assigned.Add(candidate, (picked, id));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Process heads FIRST.
|
||||
// This means that if you have head and non-head roles on the same priority level,
|
||||
// you will always get picked as head.
|
||||
// Unless of course somebody beats you to those head roles.
|
||||
ProcessJobs(true);
|
||||
ProcessJobs(false);
|
||||
available.RemoveAll(a => assigned.ContainsKey(a));
|
||||
}
|
||||
|
||||
// Current strategy is to fill each station one by one.
|
||||
foreach (var (id, station) in _stationSystem.StationInfo)
|
||||
{
|
||||
// Get the ROUND-START job list.
|
||||
var availablePositions = station.MapPrototype.AvailableJobs.ToDictionary(x => x.Key, x => x.Value[0]);
|
||||
|
||||
for (var i = JobPriority.High; i > JobPriority.Never; i--)
|
||||
{
|
||||
// Process jobs possible for heads...
|
||||
ProcessJobs(true, availablePositions, id, i);
|
||||
// and then jobs that are not heads.
|
||||
ProcessJobs(false, availablePositions, id, i);
|
||||
}
|
||||
}
|
||||
|
||||
return assigned;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the available positions for all jobs, *not* accounting for the current crew manifest.
|
||||
/// </summary>
|
||||
private Dictionary<string, int> GetBasePositions(bool roundStart)
|
||||
private string PickBestAvailableJob(HumanoidCharacterProfile profile, StationId station)
|
||||
{
|
||||
var availablePositions = _prototypeManager
|
||||
.EnumeratePrototypes<JobPrototype>()
|
||||
// -1 is treated as infinite slots.
|
||||
.ToDictionary(job => job.ID, job =>
|
||||
{
|
||||
if (job.SpawnPositions < 0)
|
||||
{
|
||||
return int.MaxValue;
|
||||
}
|
||||
|
||||
if (roundStart)
|
||||
{
|
||||
return job.SpawnPositions;
|
||||
}
|
||||
|
||||
return job.TotalPositions;
|
||||
});
|
||||
|
||||
return availablePositions;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the remaining available job positions in the current round.
|
||||
/// </summary>
|
||||
public Dictionary<string, int> GetAvailablePositions()
|
||||
{
|
||||
var basePositions = GetBasePositions(false);
|
||||
|
||||
foreach (var (jobId, count) in _spawnedPositions)
|
||||
{
|
||||
basePositions[jobId] = Math.Max(0, basePositions[jobId] - count);
|
||||
}
|
||||
|
||||
return basePositions;
|
||||
}
|
||||
|
||||
private string PickBestAvailableJob(HumanoidCharacterProfile profile)
|
||||
{
|
||||
var available = GetAvailablePositions();
|
||||
var available = _stationSystem.StationInfo[station].JobList;
|
||||
|
||||
bool TryPick(JobPriority priority, [NotNullWhen(true)] out string? jobId)
|
||||
{
|
||||
@@ -188,18 +147,17 @@ namespace Content.Server.GameTicking
|
||||
return picked;
|
||||
}
|
||||
|
||||
return OverflowJob;
|
||||
var overflows = _stationSystem.StationInfo[station].MapPrototype.OverflowJobs.Clone().ToList();
|
||||
return _robustRandom.Pick(overflows);
|
||||
}
|
||||
|
||||
[Conditional("DEBUG")]
|
||||
private void InitializeJobController()
|
||||
{
|
||||
// Verify that the overflow role exists and has the correct name.
|
||||
var role = _prototypeManager.Index<JobPrototype>(OverflowJob);
|
||||
DebugTools.Assert(role.Name == Loc.GetString(OverflowJobName),
|
||||
var role = _prototypeManager.Index<JobPrototype>(FallbackOverflowJob);
|
||||
DebugTools.Assert(role.Name == Loc.GetString(FallbackOverflowJobName),
|
||||
"Overflow role does not have the correct name!");
|
||||
|
||||
DebugTools.Assert(role.SpawnPositions < 0, "Overflow role must have infinite spawn positions!");
|
||||
}
|
||||
|
||||
private void AddSpawnedPosition(string jobId)
|
||||
@@ -211,17 +169,21 @@ namespace Content.Server.GameTicking
|
||||
{
|
||||
// If late join is disallowed, return no available jobs.
|
||||
if (DisallowLateJoin)
|
||||
return new TickerJobsAvailableEvent(Array.Empty<string>());
|
||||
return new TickerJobsAvailableEvent(new Dictionary<StationId, string>(), new Dictionary<StationId, Dictionary<string, int>>());
|
||||
|
||||
var jobs = GetAvailablePositions()
|
||||
.Where(e => e.Value > 0)
|
||||
.Select(e => e.Key)
|
||||
.ToArray();
|
||||
var jobs = new Dictionary<StationId, Dictionary<string, int>>();
|
||||
var stationNames = new Dictionary<StationId, string>();
|
||||
|
||||
return new TickerJobsAvailableEvent(jobs);
|
||||
foreach (var (id, station) in _stationSystem.StationInfo)
|
||||
{
|
||||
var list = station.JobList.ToDictionary(x => x.Key, x => x.Value);
|
||||
jobs.Add(id, list);
|
||||
stationNames.Add(id, station.Name);
|
||||
}
|
||||
return new TickerJobsAvailableEvent(stationNames, jobs);
|
||||
}
|
||||
|
||||
private void UpdateJobsAvailable()
|
||||
public void UpdateJobsAvailable()
|
||||
{
|
||||
RaiseNetworkEvent(GetJobsAvailable(), Filter.Empty().AddPlayers(_playersInLobby.Keys));
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
using System;
|
||||
using Content.Server.Players;
|
||||
using Content.Server.Roles;
|
||||
using Content.Server.Station;
|
||||
using Content.Shared.GameTicking;
|
||||
using Content.Shared.GameWindow;
|
||||
using Content.Shared.Preferences;
|
||||
using Content.Shared.Station;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Enums;
|
||||
@@ -107,7 +110,7 @@ namespace Content.Server.GameTicking
|
||||
async void SpawnWaitPrefs()
|
||||
{
|
||||
await _prefsManager.WaitPreferencesLoaded(session);
|
||||
SpawnPlayer(session);
|
||||
SpawnPlayer(session, StationId.Invalid);
|
||||
}
|
||||
|
||||
async void AddPlayerToDb(Guid id)
|
||||
|
||||
@@ -6,10 +6,13 @@ using Content.Server.GameTicking.Events;
|
||||
using Content.Server.Players;
|
||||
using Content.Server.Mind;
|
||||
using Content.Server.Ghost;
|
||||
using Content.Server.Roles;
|
||||
using Content.Server.Station;
|
||||
using Content.Shared.CCVar;
|
||||
using Content.Shared.Coordinates;
|
||||
using Content.Shared.GameTicking;
|
||||
using Content.Shared.Preferences;
|
||||
using Content.Shared.Station;
|
||||
using Prometheus;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.GameObjects;
|
||||
@@ -64,14 +67,17 @@ namespace Content.Server.GameTicking
|
||||
{
|
||||
DefaultMap = _mapManager.CreateMap();
|
||||
var startTime = _gameTiming.RealTime;
|
||||
var map = _gameMapManager.GetSelectedMapChecked(true).MapPath;
|
||||
var grid = _mapLoader.LoadBlueprint(DefaultMap, map);
|
||||
var map = _gameMapManager.GetSelectedMapChecked(true);
|
||||
var grid = _mapLoader.LoadBlueprint(DefaultMap, map.MapPath);
|
||||
|
||||
|
||||
if (grid == null)
|
||||
{
|
||||
throw new InvalidOperationException($"No grid found for map {map}");
|
||||
throw new InvalidOperationException($"No grid found for map {map.MapName}");
|
||||
}
|
||||
|
||||
_stationSystem.InitialSetupStationGrid(grid.GridEntityId, map);
|
||||
|
||||
var stationXform = EntityManager.GetComponent<TransformComponent>(grid.GridEntityId);
|
||||
|
||||
if (StationOffset)
|
||||
@@ -87,7 +93,6 @@ namespace Content.Server.GameTicking
|
||||
stationXform.LocalRotation = _robustRandom.NextFloat(MathF.Tau);
|
||||
}
|
||||
|
||||
DefaultGridId = grid.Index;
|
||||
_spawnPoint = grid.ToCoordinates();
|
||||
|
||||
var timeSpan = _gameTiming.RealTime - startTime;
|
||||
@@ -153,14 +158,36 @@ namespace Content.Server.GameTicking
|
||||
var profile = profiles[player.UserId];
|
||||
if (profile.PreferenceUnavailable == PreferenceUnavailableMode.SpawnAsOverflow)
|
||||
{
|
||||
assignedJobs.Add(player, OverflowJob);
|
||||
// Pick a random station
|
||||
var stations = _stationSystem.StationInfo.Keys.ToList();
|
||||
_robustRandom.Shuffle(stations);
|
||||
|
||||
if (stations.Count == 0)
|
||||
{
|
||||
assignedJobs.Add(player, (FallbackOverflowJob, StationId.Invalid));
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var station in stations)
|
||||
{
|
||||
// Pick a random overflow job from that station
|
||||
var overflows = _stationSystem.StationInfo[station].MapPrototype.OverflowJobs.Clone();
|
||||
_robustRandom.Shuffle(overflows);
|
||||
|
||||
// Stations with no overflow slots should simply get skipped over.
|
||||
if (overflows.Count == 0)
|
||||
continue;
|
||||
|
||||
// If the overflow exists, put them in as it.
|
||||
assignedJobs.Add(player, (overflows[0], stations[0]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Spawn everybody in!
|
||||
foreach (var (player, job) in assignedJobs)
|
||||
foreach (var (player, (job, station)) in assignedJobs)
|
||||
{
|
||||
SpawnPlayer(player, profiles[player.UserId], job, false);
|
||||
SpawnPlayer(player, profiles[player.UserId], station, job, false);
|
||||
}
|
||||
|
||||
// Time to start the preset.
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Content.Server.Access.Components;
|
||||
using Content.Server.Access.Systems;
|
||||
using Content.Server.CharacterAppearance.Components;
|
||||
using Content.Server.Ghost;
|
||||
using Content.Server.Ghost.Components;
|
||||
using Content.Server.Hands.Components;
|
||||
@@ -14,12 +14,15 @@ using Content.Server.Players;
|
||||
using Content.Server.Roles;
|
||||
using Content.Server.Spawners.Components;
|
||||
using Content.Server.Speech.Components;
|
||||
using Content.Server.Station;
|
||||
using Content.Shared.Administration.Logs;
|
||||
using Content.Shared.CharacterAppearance.Systems;
|
||||
using Content.Shared.GameTicking;
|
||||
using Content.Shared.Ghost;
|
||||
using Content.Shared.Inventory;
|
||||
using Content.Shared.Preferences;
|
||||
using Content.Shared.Roles;
|
||||
using Content.Shared.Station;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
@@ -28,6 +31,7 @@ using Robust.Shared.Map;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.ViewVariables;
|
||||
using static Content.Server.Station.StationSystem;
|
||||
|
||||
namespace Content.Server.GameTicking
|
||||
{
|
||||
@@ -38,22 +42,35 @@ namespace Content.Server.GameTicking
|
||||
|
||||
[Dependency] private readonly IdCardSystem _cardSystem = default!;
|
||||
|
||||
/// <summary>
|
||||
/// Can't yet be removed because every test ever seems to depend on it. I'll make removing this a different PR.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
private EntityCoordinates _spawnPoint;
|
||||
|
||||
// Mainly to avoid allocations.
|
||||
private readonly List<EntityCoordinates> _possiblePositions = new();
|
||||
|
||||
private void SpawnPlayer(IPlayerSession player, string? jobId = null, bool lateJoin = true)
|
||||
private void SpawnPlayer(IPlayerSession player, StationId station, string? jobId = null, bool lateJoin = true)
|
||||
{
|
||||
var character = GetPlayerProfile(player);
|
||||
|
||||
SpawnPlayer(player, character, jobId, lateJoin);
|
||||
SpawnPlayer(player, character, station, jobId, lateJoin);
|
||||
UpdateJobsAvailable();
|
||||
}
|
||||
|
||||
private void SpawnPlayer(IPlayerSession player, HumanoidCharacterProfile character, string? jobId = null, bool lateJoin = true)
|
||||
private void SpawnPlayer(IPlayerSession player, HumanoidCharacterProfile character, StationId station, string? jobId = null, bool lateJoin = true)
|
||||
{
|
||||
if (station == StationId.Invalid)
|
||||
{
|
||||
var stations = _stationSystem.StationInfo.Keys.ToList();
|
||||
_robustRandom.Shuffle(stations);
|
||||
if (stations.Count == 0)
|
||||
station = StationId.Invalid;
|
||||
else
|
||||
station = stations[0];
|
||||
}
|
||||
|
||||
// Can't spawn players with a dummy ticker!
|
||||
if (DummyTicker)
|
||||
return;
|
||||
@@ -78,7 +95,7 @@ namespace Content.Server.GameTicking
|
||||
newMind.ChangeOwningPlayer(data.UserId);
|
||||
|
||||
// Pick best job best on prefs.
|
||||
jobId ??= PickBestAvailableJob(character);
|
||||
jobId ??= PickBestAvailableJob(character, station);
|
||||
|
||||
var jobPrototype = _prototypeManager.Index<JobPrototype>(jobId);
|
||||
var job = new Job(newMind, jobPrototype);
|
||||
@@ -94,7 +111,7 @@ namespace Content.Server.GameTicking
|
||||
playDefaultSound: false);
|
||||
}
|
||||
|
||||
var mob = SpawnPlayerMob(job, character, lateJoin);
|
||||
var mob = SpawnPlayerMob(job, character, station, lateJoin);
|
||||
newMind.TransferTo(mob.Uid);
|
||||
|
||||
if (player.UserId == new Guid("{e887eb93-f503-4b65-95b6-2f282c014192}"))
|
||||
@@ -111,20 +128,28 @@ namespace Content.Server.GameTicking
|
||||
jobSpecial.AfterEquip(mob);
|
||||
}
|
||||
|
||||
_stationSystem.TryAssignJobToStation(station, jobId);
|
||||
|
||||
if (lateJoin)
|
||||
_adminLogSystem.Add(LogType.LateJoin, LogImpact.Medium, $"Player {player.Name} late joined as {character.Name:characterName} on station {_stationSystem.StationInfo[station].Name:stationName} with {mob} as a {job.Name:jobName}.");
|
||||
else
|
||||
_adminLogSystem.Add(LogType.RoundStartJoin, LogImpact.Medium, $"Player {player.Name} joined as {character.Name:characterName} on station {_stationSystem.StationInfo[station].Name:stationName} with {mob} as a {job.Name:jobName}.");
|
||||
|
||||
Preset?.OnSpawnPlayerCompleted(player, mob, lateJoin);
|
||||
}
|
||||
|
||||
public void Respawn(IPlayerSession player)
|
||||
{
|
||||
player.ContentData()?.WipeMind();
|
||||
_adminLogSystem.Add(LogType.Respawn, LogImpact.Medium, $"Player {player} was respawned.");
|
||||
|
||||
if (LobbyEnabled)
|
||||
PlayerJoinLobby(player);
|
||||
else
|
||||
SpawnPlayer(player);
|
||||
SpawnPlayer(player, StationId.Invalid);
|
||||
}
|
||||
|
||||
public void MakeJoinGame(IPlayerSession player, string? jobId = null)
|
||||
public void MakeJoinGame(IPlayerSession player, StationId station, string? jobId = null)
|
||||
{
|
||||
if (!_playersInLobby.ContainsKey(player)) return;
|
||||
|
||||
@@ -133,7 +158,7 @@ namespace Content.Server.GameTicking
|
||||
return;
|
||||
}
|
||||
|
||||
SpawnPlayer(player, jobId);
|
||||
SpawnPlayer(player, station, jobId);
|
||||
}
|
||||
|
||||
public void MakeObserve(IPlayerSession player)
|
||||
@@ -168,9 +193,9 @@ namespace Content.Server.GameTicking
|
||||
}
|
||||
|
||||
#region Mob Spawning Helpers
|
||||
private IEntity SpawnPlayerMob(Job job, HumanoidCharacterProfile? profile, bool lateJoin = true)
|
||||
private IEntity SpawnPlayerMob(Job job, HumanoidCharacterProfile? profile, StationId station, bool lateJoin = true)
|
||||
{
|
||||
var coordinates = lateJoin ? GetLateJoinSpawnPoint() : GetJobSpawnPoint(job.Prototype.ID);
|
||||
var coordinates = lateJoin ? GetLateJoinSpawnPoint(station) : GetJobSpawnPoint(job.Prototype.ID, station);
|
||||
var entity = EntityManager.SpawnEntity(PlayerPrototypeName, coordinates);
|
||||
|
||||
if (job.StartingGear != null)
|
||||
@@ -255,7 +280,7 @@ namespace Content.Server.GameTicking
|
||||
}
|
||||
|
||||
#region Spawn Points
|
||||
public EntityCoordinates GetJobSpawnPoint(string jobId)
|
||||
public EntityCoordinates GetJobSpawnPoint(string jobId, StationId station)
|
||||
{
|
||||
var location = _spawnPoint;
|
||||
|
||||
@@ -263,17 +288,24 @@ namespace Content.Server.GameTicking
|
||||
|
||||
foreach (var (point, transform) in EntityManager.EntityQuery<SpawnPointComponent, TransformComponent>())
|
||||
{
|
||||
if (point.SpawnType == SpawnPointType.Job && point.Job?.ID == jobId)
|
||||
var matchingStation =
|
||||
EntityManager.TryGetComponent<StationComponent>(transform.ParentUid, out var stationComponent) &&
|
||||
stationComponent.Station == station;
|
||||
DebugTools.Assert(EntityManager.TryGetComponent<IMapGridComponent>(transform.ParentUid, out _));
|
||||
|
||||
if (point.SpawnType == SpawnPointType.Job && point.Job?.ID == jobId && matchingStation)
|
||||
_possiblePositions.Add(transform.Coordinates);
|
||||
}
|
||||
|
||||
if (_possiblePositions.Count != 0)
|
||||
location = _robustRandom.Pick(_possiblePositions);
|
||||
else
|
||||
location = GetLateJoinSpawnPoint(station); // We need a sane fallback here, so latejoin it is.
|
||||
|
||||
return location;
|
||||
}
|
||||
|
||||
public EntityCoordinates GetLateJoinSpawnPoint()
|
||||
public EntityCoordinates GetLateJoinSpawnPoint(StationId station)
|
||||
{
|
||||
var location = _spawnPoint;
|
||||
|
||||
@@ -281,7 +313,13 @@ namespace Content.Server.GameTicking
|
||||
|
||||
foreach (var (point, transform) in EntityManager.EntityQuery<SpawnPointComponent, TransformComponent>())
|
||||
{
|
||||
if (point.SpawnType == SpawnPointType.LateJoin) _possiblePositions.Add(transform.Coordinates);
|
||||
var matchingStation =
|
||||
EntityManager.TryGetComponent<StationComponent>(transform.ParentUid, out var stationComponent) &&
|
||||
stationComponent.Station == station;
|
||||
DebugTools.Assert(EntityManager.TryGetComponent<IMapGridComponent>(transform.ParentUid, out _));
|
||||
|
||||
if (point.SpawnType == SpawnPointType.LateJoin && matchingStation)
|
||||
_possiblePositions.Add(transform.Coordinates);
|
||||
}
|
||||
|
||||
if (_possiblePositions.Count != 0)
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
using Content.Server.Administration.Logs;
|
||||
using Content.Server.Chat.Managers;
|
||||
using Content.Server.Maps;
|
||||
using Content.Server.Preferences.Managers;
|
||||
using Content.Server.Roles;
|
||||
using Content.Server.Station;
|
||||
using Content.Shared.Chat;
|
||||
using Content.Shared.GameTicking;
|
||||
using Content.Shared.GameWindow;
|
||||
@@ -27,7 +30,6 @@ namespace Content.Server.GameTicking
|
||||
[ViewVariables] private bool _postInitialized;
|
||||
|
||||
[ViewVariables] public MapId DefaultMap { get; private set; }
|
||||
[ViewVariables] public GridId DefaultGridId { get; private set; }
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
@@ -87,5 +89,7 @@ namespace Content.Server.GameTicking
|
||||
[Dependency] private readonly IWatchdogApi _watchdogApi = default!;
|
||||
[Dependency] private readonly IReflectionManager _reflectionManager = default!;
|
||||
[Dependency] private readonly IGameMapManager _gameMapManager = default!;
|
||||
[Dependency] private readonly StationSystem _stationSystem = default!;
|
||||
[Dependency] private readonly AdminLogSystem _adminLogSystem = default!;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,110 +11,109 @@ using Robust.Shared.Localization;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Maps
|
||||
namespace Content.Server.Maps;
|
||||
|
||||
public class GameMapManager : IGameMapManager
|
||||
{
|
||||
public 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!;
|
||||
|
||||
private GameMapPrototype _currentMap = default!;
|
||||
private bool _currentMapForced;
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
[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!;
|
||||
|
||||
private GameMapPrototype _currentMap = default!;
|
||||
private bool _currentMapForced;
|
||||
|
||||
public void Initialize()
|
||||
_configurationManager.OnValueChanged(CCVars.GameMap, value =>
|
||||
{
|
||||
_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);
|
||||
}
|
||||
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
public void SelectRandomMap()
|
||||
{
|
||||
var maps = CurrentlyEligibleMaps().ToList();
|
||||
_random.Shuffle(maps);
|
||||
_currentMap = maps[0];
|
||||
_currentMapForced = false;
|
||||
}
|
||||
|
||||
public GameMapPrototype GetSelectedMap()
|
||||
{
|
||||
return _currentMap;
|
||||
}
|
||||
|
||||
public GameMapPrototype GetSelectedMapChecked(bool loud = 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)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
return GetSelectedMap();
|
||||
}
|
||||
|
||||
public bool CheckMapExists(string gameMap)
|
||||
{
|
||||
return TryLookupMap(gameMap, out _);
|
||||
}
|
||||
|
||||
private bool IsMapEligible(GameMapPrototype map)
|
||||
{
|
||||
return map.MaxPlayers >= _playerManager.PlayerCount && map.MinPlayers <= _playerManager.PlayerCount;
|
||||
}
|
||||
|
||||
private bool TryLookupMap(string gameMap, [NotNullWhen(true)] out GameMapPrototype? map)
|
||||
{
|
||||
return _prototypeManager.TryIndex(gameMap, out map);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
public void SelectRandomMap()
|
||||
{
|
||||
var maps = CurrentlyEligibleMaps().ToList();
|
||||
_random.Shuffle(maps);
|
||||
_currentMap = maps[0];
|
||||
_currentMapForced = false;
|
||||
}
|
||||
|
||||
public GameMapPrototype GetSelectedMap()
|
||||
{
|
||||
return _currentMap;
|
||||
}
|
||||
|
||||
public GameMapPrototype GetSelectedMapChecked(bool loud = 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)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
return GetSelectedMap();
|
||||
}
|
||||
|
||||
public bool CheckMapExists(string gameMap)
|
||||
{
|
||||
return TryLookupMap(gameMap, out _);
|
||||
}
|
||||
|
||||
private bool IsMapEligible(GameMapPrototype map)
|
||||
{
|
||||
return map.MaxPlayers >= _playerManager.PlayerCount && map.MinPlayers <= _playerManager.PlayerCount;
|
||||
}
|
||||
|
||||
private bool TryLookupMap(string gameMap, [NotNullWhen(true)] out GameMapPrototype? map)
|
||||
{
|
||||
return _prototypeManager.TryIndex(gameMap, out map);
|
||||
}
|
||||
}
|
||||
@@ -1,53 +1,70 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Shared.Roles;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Dictionary;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Maps
|
||||
namespace Content.Server.Maps;
|
||||
|
||||
/// <summary>
|
||||
/// Prototype data for a game map.
|
||||
/// </summary>
|
||||
[Prototype("gameMap")]
|
||||
public class GameMapPrototype : IPrototype
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
[DataField("id", required: true)]
|
||||
public string ID { get; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// Prototype data for a game map.
|
||||
/// Minimum players for the given map.
|
||||
/// </summary>
|
||||
[Prototype("gameMap")]
|
||||
public class GameMapPrototype : IPrototype
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
[ViewVariables, DataField("id", required: true)]
|
||||
public string ID { get; } = default!;
|
||||
[DataField("minPlayers", required: true)]
|
||||
public uint MinPlayers { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Minimum players for the given map.
|
||||
/// </summary>
|
||||
[ViewVariables, DataField("minPlayers", required: true)]
|
||||
public uint MinPlayers { get; }
|
||||
/// <summary>
|
||||
/// Maximum players for the given map.
|
||||
/// </summary>
|
||||
[DataField("maxPlayers")]
|
||||
public uint MaxPlayers { get; } = uint.MaxValue;
|
||||
|
||||
/// <summary>
|
||||
/// Maximum players for the given map.
|
||||
/// </summary>
|
||||
[ViewVariables, DataField("maxPlayers")]
|
||||
public uint MaxPlayers { get; } = uint.MaxValue;
|
||||
/// <summary>
|
||||
/// Name of the given map.
|
||||
/// </summary>
|
||||
[DataField("mapName", required: true)]
|
||||
public string MapName { get; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// Name of the given map.
|
||||
/// </summary>
|
||||
[ViewVariables, DataField("mapName", required: true)]
|
||||
public string MapName { get; } = default!;
|
||||
/// <summary>
|
||||
/// Relative directory path to the given map, i.e. `Maps/saltern.yml`
|
||||
/// </summary>
|
||||
[DataField("mapPath", required: true)]
|
||||
public string MapPath { get; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// Relative directory path to the given map, i.e. `Maps/saltern.yml`
|
||||
/// </summary>
|
||||
[ViewVariables, DataField("mapPath", required: true)]
|
||||
public string MapPath { get; } = default!;
|
||||
/// <summary>
|
||||
/// Controls if the map can be used as a fallback if no maps are eligible.
|
||||
/// </summary>
|
||||
[DataField("fallback")]
|
||||
public bool Fallback { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Controls if the map can be used as a fallback if no maps are eligible.
|
||||
/// </summary>
|
||||
[ViewVariables, DataField("fallback")]
|
||||
public bool Fallback { get; }
|
||||
/// <summary>
|
||||
/// Controls if the map can be voted for.
|
||||
/// </summary>
|
||||
[DataField("votable")]
|
||||
public bool Votable { get; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Controls if the map can be voted for.
|
||||
/// </summary>
|
||||
[ViewVariables, DataField("votable")]
|
||||
public bool Votable { get; } = true;
|
||||
}
|
||||
/// <summary>
|
||||
/// Jobs used at round start should the station run out of job slots.
|
||||
/// Doesn't necessarily mean the station has infinite slots for the given jobs midround!
|
||||
/// </summary>
|
||||
[DataField("overflowJobs", required: true, customTypeSerializer:typeof(PrototypeIdListSerializer<JobPrototype>))]
|
||||
public List<string> OverflowJobs { get; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// Index of all jobs available on the station, of form
|
||||
/// jobname: [roundstart, midround]
|
||||
/// </summary>
|
||||
[DataField("availableJobs", required: true, customTypeSerializer:typeof(PrototypeIdDictionarySerializer<List<int>, JobPrototype>))]
|
||||
public Dictionary<string, List<int>> AvailableJobs { get; } = default!;
|
||||
}
|
||||
|
||||
@@ -1,68 +1,67 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Content.Server.Maps
|
||||
namespace Content.Server.Maps;
|
||||
|
||||
/// <summary>
|
||||
/// Manages which station map will be used for the next round.
|
||||
/// </summary>
|
||||
public interface IGameMapManager
|
||||
{
|
||||
void Initialize();
|
||||
|
||||
/// <summary>
|
||||
/// Manages which station map will be used for the next round.
|
||||
/// Returns all maps eligible to be played right now.
|
||||
/// </summary>
|
||||
public interface IGameMapManager
|
||||
{
|
||||
void Initialize();
|
||||
/// <returns>enumerator of map prototypes</returns>
|
||||
IEnumerable<GameMapPrototype> CurrentlyEligibleMaps();
|
||||
|
||||
/// <summary>
|
||||
/// Returns all maps eligible to be played right now.
|
||||
/// </summary>
|
||||
/// <returns>enumerator of map prototypes</returns>
|
||||
IEnumerable<GameMapPrototype> CurrentlyEligibleMaps();
|
||||
/// <summary>
|
||||
/// Returns all maps that can be voted for.
|
||||
/// </summary>
|
||||
/// <returns>enumerator of map prototypes</returns>
|
||||
IEnumerable<GameMapPrototype> AllVotableMaps();
|
||||
|
||||
/// <summary>
|
||||
/// Returns all maps that can be voted for.
|
||||
/// </summary>
|
||||
/// <returns>enumerator of map prototypes</returns>
|
||||
IEnumerable<GameMapPrototype> AllVotableMaps();
|
||||
/// <summary>
|
||||
/// Returns all maps.
|
||||
/// </summary>
|
||||
/// <returns>enumerator of map prototypes</returns>
|
||||
IEnumerable<GameMapPrototype> AllMaps();
|
||||
|
||||
/// <summary>
|
||||
/// Returns all maps.
|
||||
/// </summary>
|
||||
/// <returns>enumerator of map prototypes</returns>
|
||||
IEnumerable<GameMapPrototype> AllMaps();
|
||||
/// <summary>
|
||||
/// Attempts to select the given map.
|
||||
/// </summary>
|
||||
/// <param name="gameMap">map prototype</param>
|
||||
/// <returns>success or failure</returns>
|
||||
bool TrySelectMap(string gameMap);
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to select the given map.
|
||||
/// </summary>
|
||||
/// <param name="gameMap">map prototype</param>
|
||||
/// <returns>success or failure</returns>
|
||||
bool TrySelectMap(string gameMap);
|
||||
/// <summary>
|
||||
/// Forces the given map, making sure the game map manager won't reselect if conditions are no longer met at round restart.
|
||||
/// </summary>
|
||||
/// <param name="gameMap">map prototype</param>
|
||||
/// <returns>success or failure</returns>
|
||||
void ForceSelectMap(string gameMap);
|
||||
|
||||
/// <summary>
|
||||
/// Forces the given map, making sure the game map manager won't reselect if conditions are no longer met at round restart.
|
||||
/// </summary>
|
||||
/// <param name="gameMap">map prototype</param>
|
||||
/// <returns>success or failure</returns>
|
||||
void ForceSelectMap(string gameMap);
|
||||
/// <summary>
|
||||
/// Selects a random map.
|
||||
/// </summary>
|
||||
void SelectRandomMap();
|
||||
|
||||
/// <summary>
|
||||
/// Selects a random map.
|
||||
/// </summary>
|
||||
void SelectRandomMap();
|
||||
/// <summary>
|
||||
/// Gets the currently selected map, without double-checking if it can be used.
|
||||
/// </summary>
|
||||
/// <returns>selected map</returns>
|
||||
GameMapPrototype GetSelectedMap();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the currently selected map, without double-checking if it can be used.
|
||||
/// </summary>
|
||||
/// <returns>selected map</returns>
|
||||
GameMapPrototype GetSelectedMap();
|
||||
/// <summary>
|
||||
/// Gets the currently selected map, double-checking if it can be used.
|
||||
/// </summary>
|
||||
/// <returns>selected map</returns>
|
||||
GameMapPrototype GetSelectedMapChecked(bool loud = false);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the currently selected map, double-checking if it can be used.
|
||||
/// </summary>
|
||||
/// <returns>selected map</returns>
|
||||
GameMapPrototype GetSelectedMapChecked(bool loud = false);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the given map exists
|
||||
/// </summary>
|
||||
/// <param name="gameMap">name of the map</param>
|
||||
/// <returns>existence</returns>
|
||||
bool CheckMapExists(string gameMap);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Checks if the given map exists
|
||||
/// </summary>
|
||||
/// <param name="gameMap">name of the map</param>
|
||||
/// <returns>existence</returns>
|
||||
bool CheckMapExists(string gameMap);
|
||||
}
|
||||
13
Content.Server/Station/StationComponent.cs
Normal file
13
Content.Server/Station/StationComponent.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Content.Shared.Station;
|
||||
using Robust.Shared.Analyzers;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Server.Station;
|
||||
|
||||
[RegisterComponent, Friend(typeof(StationSystem))]
|
||||
public class StationComponent : Component
|
||||
{
|
||||
public override string Name => "Station";
|
||||
|
||||
public StationId Station = StationId.Invalid;
|
||||
}
|
||||
165
Content.Server/Station/StationSystem.cs
Normal file
165
Content.Server/Station/StationSystem.cs
Normal file
@@ -0,0 +1,165 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Server.GameTicking;
|
||||
using Content.Server.Maps;
|
||||
using Content.Shared.Station;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Log;
|
||||
|
||||
namespace Content.Server.Station;
|
||||
|
||||
/// <summary>
|
||||
/// System that manages the jobs available on a station, and maybe other things later.
|
||||
/// </summary>
|
||||
public class StationSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private GameTicker _gameTicker = default!;
|
||||
private uint _idCounter = 1;
|
||||
|
||||
private Dictionary<StationId, StationInfoData> _stationInfo = new();
|
||||
/// <summary>
|
||||
/// List of stations for the current round.
|
||||
/// </summary>
|
||||
public IReadOnlyDictionary<StationId, StationInfoData> StationInfo => _stationInfo;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<GameRunLevelChangedEvent>(OnRoundEnd);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cleans up station info.
|
||||
/// </summary>
|
||||
private void OnRoundEnd(GameRunLevelChangedEvent eventArgs)
|
||||
{
|
||||
if (eventArgs.New == GameRunLevel.PostRound)
|
||||
_stationInfo = new();
|
||||
}
|
||||
|
||||
public class StationInfoData
|
||||
{
|
||||
public readonly string Name;
|
||||
|
||||
/// <summary>
|
||||
/// Job list associated with the game map.
|
||||
/// </summary>
|
||||
public readonly GameMapPrototype MapPrototype;
|
||||
|
||||
/// <summary>
|
||||
/// The round job list.
|
||||
/// </summary>
|
||||
private readonly Dictionary<string, int> _jobList;
|
||||
|
||||
public IReadOnlyDictionary<string, int> JobList => _jobList;
|
||||
|
||||
public StationInfoData(string name, GameMapPrototype mapPrototype, Dictionary<string, int> jobList)
|
||||
{
|
||||
Name = name;
|
||||
MapPrototype = mapPrototype;
|
||||
_jobList = jobList;
|
||||
}
|
||||
|
||||
public bool TryAssignJob(string jobName)
|
||||
{
|
||||
if (_jobList.ContainsKey(jobName))
|
||||
{
|
||||
switch (_jobList[jobName])
|
||||
{
|
||||
case > 0:
|
||||
_jobList[jobName]--;
|
||||
return true;
|
||||
case -1:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new station and attaches it to the given grid.
|
||||
/// </summary>
|
||||
/// <param name="mapGrid">grid to attach to</param>
|
||||
/// <param name="mapPrototype">game map prototype of the station</param>
|
||||
/// <param name="stationName">name of the station to assign, if not the default</param>
|
||||
/// <param name="gridComponent">optional grid component of the grid.</param>
|
||||
/// <returns>The ID of the resulting station</returns>
|
||||
/// <exception cref="ArgumentException">Thrown when the given entity is not a grid.</exception>
|
||||
public StationId InitialSetupStationGrid(EntityUid mapGrid, GameMapPrototype mapPrototype, string? stationName = null, IMapGridComponent? gridComponent = null)
|
||||
{
|
||||
if (!Resolve(mapGrid, ref gridComponent))
|
||||
throw new ArgumentException("Tried to initialize a station on a non-grid entity!");
|
||||
|
||||
var jobListDict = mapPrototype.AvailableJobs.ToDictionary(x => x.Key, x => x.Value[1]);
|
||||
var id = AllocateStationInfo();
|
||||
|
||||
_stationInfo[id] = new StationInfoData(stationName ?? mapPrototype.MapName, mapPrototype, jobListDict);
|
||||
var station = EntityManager.AddComponent<StationComponent>(mapGrid);
|
||||
station.Station = id;
|
||||
|
||||
_gameTicker.UpdateJobsAvailable(); // new station means new jobs, tell any lobby-goers.
|
||||
|
||||
Logger.InfoS("stations",
|
||||
$"Setting up new {mapPrototype.ID} called {mapPrototype.MapName} on grid {mapGrid}:{gridComponent.GridIndex}");
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the given grid to the given station.
|
||||
/// </summary>
|
||||
/// <param name="mapGrid">grid to attach</param>
|
||||
/// <param name="station">station to attach the grid to</param>
|
||||
/// <param name="gridComponent">optional grid component of the grid.</param>
|
||||
/// <exception cref="ArgumentException">Thrown when the given entity is not a grid.</exception>
|
||||
public void AddGridToStation(EntityUid mapGrid, StationId station, IMapGridComponent? gridComponent = null)
|
||||
{
|
||||
if (!Resolve(mapGrid, ref gridComponent))
|
||||
throw new ArgumentException("Tried to initialize a station on a non-grid entity!");
|
||||
var stationComponent = EntityManager.AddComponent<StationComponent>(mapGrid);
|
||||
stationComponent.Station = station;
|
||||
|
||||
Logger.InfoS("stations", $"Adding grid {mapGrid}:{gridComponent.GridIndex} to station {station} named {_stationInfo[station].Name}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to assign a job on the given station.
|
||||
/// </summary>
|
||||
/// <param name="stationId">station to assign to</param>
|
||||
/// <param name="jobName">name of the job</param>
|
||||
/// <returns>assignment success</returns>
|
||||
public bool TryAssignJobToStation(StationId stationId, string jobName)
|
||||
{
|
||||
if (stationId != StationId.Invalid)
|
||||
return _stationInfo[stationId].TryAssignJob(jobName);
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the given job is available.
|
||||
/// </summary>
|
||||
/// <param name="stationId">station to check</param>
|
||||
/// <param name="jobName">name of the job</param>
|
||||
/// <returns>job availability</returns>
|
||||
public bool IsJobAvailableOnStation(StationId stationId, string jobName)
|
||||
{
|
||||
if (_stationInfo[stationId].JobList.TryGetValue(jobName, out var amount))
|
||||
return amount != 0;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private StationId AllocateStationInfo()
|
||||
{
|
||||
return new StationId(_idCounter++);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
using Content.Server.Atmos.Components;
|
||||
using System.Linq;
|
||||
using Content.Server.Atmos.EntitySystems;
|
||||
using Content.Server.GameTicking;
|
||||
using Content.Server.Station;
|
||||
using Content.Shared.Atmos;
|
||||
using Content.Shared.Station;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
@@ -15,6 +16,9 @@ namespace Content.Server.StationEvents.Events
|
||||
{
|
||||
internal sealed class GasLeak : StationEvent
|
||||
{
|
||||
[Dependency] private IRobustRandom _robustRandom = default!;
|
||||
[Dependency] private IEntityManager _entityManager = default!;
|
||||
|
||||
public override string Name => "GasLeak";
|
||||
|
||||
public override string? StartAnnouncement =>
|
||||
@@ -56,6 +60,8 @@ namespace Content.Server.StationEvents.Events
|
||||
|
||||
// Event variables
|
||||
|
||||
private StationId _targetStation;
|
||||
|
||||
private IEntity? _targetGrid;
|
||||
|
||||
private Vector2i _targetTile;
|
||||
@@ -84,17 +90,16 @@ namespace Content.Server.StationEvents.Events
|
||||
public override void Startup()
|
||||
{
|
||||
base.Startup();
|
||||
var robustRandom = IoCManager.Resolve<IRobustRandom>();
|
||||
|
||||
// Essentially we'll pick out a target amount of gas to leak, then a rate to leak it at, then work out the duration from there.
|
||||
if (TryFindRandomTile(out _targetTile, robustRandom))
|
||||
if (TryFindRandomTile(out _targetTile))
|
||||
{
|
||||
_foundTile = true;
|
||||
|
||||
_leakGas = robustRandom.Pick(LeakableGases);
|
||||
_leakGas = _robustRandom.Pick(LeakableGases);
|
||||
// Was 50-50 on using normal distribution.
|
||||
var totalGas = (float) robustRandom.Next(MinimumGas, MaximumGas);
|
||||
_molesPerSecond = robustRandom.Next(MinimumMolesPerSecond, MaximumMolesPerSecond);
|
||||
var totalGas = (float) _robustRandom.Next(MinimumGas, MaximumGas);
|
||||
_molesPerSecond = _robustRandom.Next(MinimumMolesPerSecond, MaximumMolesPerSecond);
|
||||
EndAfter = totalGas / _molesPerSecond + StartAfter;
|
||||
Logger.InfoS("stationevents", $"Leaking {totalGas} of {_leakGas} over {EndAfter - StartAfter} seconds at {_targetTile}");
|
||||
}
|
||||
@@ -147,8 +152,7 @@ namespace Content.Server.StationEvents.Events
|
||||
private void Spark()
|
||||
{
|
||||
var atmosphereSystem = EntitySystem.Get<AtmosphereSystem>();
|
||||
var robustRandom = IoCManager.Resolve<IRobustRandom>();
|
||||
if (robustRandom.NextFloat() <= SparkChance)
|
||||
if (_robustRandom.NextFloat() <= SparkChance)
|
||||
{
|
||||
if (!_foundTile ||
|
||||
_targetGrid == null ||
|
||||
@@ -165,27 +169,31 @@ namespace Content.Server.StationEvents.Events
|
||||
}
|
||||
}
|
||||
|
||||
private bool TryFindRandomTile(out Vector2i tile, IRobustRandom? robustRandom = null)
|
||||
private bool TryFindRandomTile(out Vector2i tile)
|
||||
{
|
||||
tile = default;
|
||||
var defaultGridId = EntitySystem.Get<GameTicker>().DefaultGridId;
|
||||
|
||||
if (!IoCManager.Resolve<IMapManager>().TryGetGrid(defaultGridId, out var grid) ||
|
||||
!IoCManager.Resolve<IEntityManager>().TryGetEntity(grid.GridEntityId, out _targetGrid)) return false;
|
||||
_targetStation = _robustRandom.Pick(_entityManager.EntityQuery<StationComponent>().ToArray()).Station;
|
||||
var possibleTargets = _entityManager.EntityQuery<StationComponent>()
|
||||
.Where(x => x.Station == _targetStation).ToArray();
|
||||
_targetGrid = _robustRandom.Pick(possibleTargets).Owner;
|
||||
|
||||
if (!_entityManager.TryGetComponent<IMapGridComponent>(_targetGrid!.Uid, out var gridComp))
|
||||
return false;
|
||||
var grid = gridComp.Grid;
|
||||
|
||||
var atmosphereSystem = EntitySystem.Get<AtmosphereSystem>();
|
||||
robustRandom ??= IoCManager.Resolve<IRobustRandom>();
|
||||
var found = false;
|
||||
var gridBounds = grid.WorldBounds;
|
||||
var gridPos = grid.WorldPosition;
|
||||
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
var randomX = robustRandom.Next((int) gridBounds.Left, (int) gridBounds.Right);
|
||||
var randomY = robustRandom.Next((int) gridBounds.Bottom, (int) gridBounds.Top);
|
||||
var randomX = _robustRandom.Next((int) gridBounds.Left, (int) gridBounds.Right);
|
||||
var randomY = _robustRandom.Next((int) gridBounds.Bottom, (int) gridBounds.Top);
|
||||
|
||||
tile = new Vector2i(randomX - (int) gridPos.X, randomY - (int) gridPos.Y);
|
||||
if (atmosphereSystem.IsTileSpace(defaultGridId, tile) || atmosphereSystem.IsTileAirBlocked(defaultGridId, tile)) continue;
|
||||
if (atmosphereSystem.IsTileSpace(grid, tile) || atmosphereSystem.IsTileAirBlocked(grid, tile)) continue;
|
||||
found = true;
|
||||
_targetCoords = grid.GridTileToLocal(tile);
|
||||
break;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using Content.Server.GameTicking;
|
||||
using System.Linq;
|
||||
using Content.Server.Radiation;
|
||||
using Content.Server.Station;
|
||||
using Content.Shared.Coordinates;
|
||||
using Content.Shared.Station;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
@@ -29,6 +31,7 @@ namespace Content.Server.StationEvents.Events
|
||||
private float _timeUntilPulse;
|
||||
private const float MinPulseDelay = 0.2f;
|
||||
private const float MaxPulseDelay = 0.8f;
|
||||
private StationId _target = StationId.Invalid;
|
||||
|
||||
private void ResetTimeUntilPulse()
|
||||
{
|
||||
@@ -44,6 +47,7 @@ namespace Content.Server.StationEvents.Events
|
||||
public override void Startup()
|
||||
{
|
||||
ResetTimeUntilPulse();
|
||||
_target = _robustRandom.Pick(_entityManager.EntityQuery<StationComponent>().ToArray()).Station;
|
||||
base.Startup();
|
||||
}
|
||||
|
||||
@@ -63,12 +67,18 @@ namespace Content.Server.StationEvents.Events
|
||||
if (_timeUntilPulse <= 0.0f)
|
||||
{
|
||||
var pauseManager = IoCManager.Resolve<IPauseManager>();
|
||||
var defaultGrid = IoCManager.Resolve<IMapManager>().GetGrid(EntitySystem.Get<GameTicker>().DefaultGridId);
|
||||
// Account for split stations by just randomly picking a piece of it.
|
||||
var possibleTargets = _entityManager.EntityQuery<StationComponent>()
|
||||
.Where(x => x.Station == _target).ToArray();
|
||||
var stationEnt = _robustRandom.Pick(possibleTargets).OwnerUid;
|
||||
|
||||
if (pauseManager.IsGridPaused(defaultGrid))
|
||||
if (!_entityManager.TryGetComponent<IMapGridComponent>(stationEnt, out var grid))
|
||||
return;
|
||||
|
||||
SpawnPulse(defaultGrid);
|
||||
if (pauseManager.IsGridPaused(grid.GridIndex))
|
||||
return;
|
||||
|
||||
SpawnPulse(grid.Grid);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user