Refactor Game Modes (#5857)

This commit is contained in:
Vera Aguilera Puerto
2021-12-21 21:23:29 +01:00
committed by GitHub
parent d1a1ee3cbe
commit f4d8ec1b35
62 changed files with 2087 additions and 1804 deletions

View File

@@ -18,6 +18,7 @@ using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Maths;
using Robust.Shared.Network;
using Robust.Shared.Player;
using Robust.Shared.Random;
using Robust.Shared.Utility;
@@ -64,6 +65,7 @@ namespace Content.Server.GameTicking
private void PreRoundSetup()
{
DefaultMap = _mapManager.CreateMap();
_pauseManager.AddUninitializedMap(DefaultMap);
var startTime = _gameTiming.RealTime;
var map = _gameMapManager.GetSelectedMapChecked(true);
var grid = _mapLoader.LoadBlueprint(DefaultMap, map.MapPath);
@@ -99,134 +101,181 @@ namespace Content.Server.GameTicking
public async void StartRound(bool force = false)
{
// If this game ticker is a dummy, do nothing!
if (DummyTicker)
return;
DebugTools.Assert(RunLevel == GameRunLevel.PreRoundLobby);
Logger.InfoS("ticker", "Starting round!");
SendServerMessage(Loc.GetString("game-ticker-start-round"));
List<IPlayerSession> readyPlayers;
if (LobbyEnabled)
#if EXCEPTION_TOLERANCE
try
{
readyPlayers = _playersInLobby.Where(p => p.Value == LobbyPlayerStatus.Ready).Select(p => p.Key).ToList();
}
else
{
readyPlayers = _playersInLobby.Keys.ToList();
}
#endif
// If this game ticker is a dummy, do nothing!
if (DummyTicker)
return;
_roundStartDateTime = DateTime.UtcNow;
RunLevel = GameRunLevel.InRound;
DebugTools.Assert(RunLevel == GameRunLevel.PreRoundLobby);
Logger.InfoS("ticker", "Starting round!");
RoundLengthMetric.Set(0);
SendServerMessage(Loc.GetString("game-ticker-start-round"));
var playerIds = _playersInLobby.Keys.Select(player => player.UserId.UserId).ToArray();
RoundId = await _db.AddNewRound(playerIds);
AddGamePresetRules();
var startingEvent = new RoundStartingEvent();
RaiseLocalEvent(startingEvent);
// Get the profiles for each player for easier lookup.
var profiles = _prefsManager.GetSelectedProfilesForPlayers(
readyPlayers
.Select(p => p.UserId).ToList())
.ToDictionary(p => p.Key, p => (HumanoidCharacterProfile) p.Value);
foreach (var readyPlayer in readyPlayers)
{
if (!profiles.ContainsKey(readyPlayer.UserId))
List<IPlayerSession> readyPlayers;
if (LobbyEnabled)
{
profiles.Add(readyPlayer.UserId, HumanoidCharacterProfile.Random());
}
}
var assignedJobs = AssignJobs(readyPlayers, profiles);
// For players without jobs, give them the overflow job if they have that set...
foreach (var player in readyPlayers)
{
if (assignedJobs.ContainsKey(player))
{
continue;
}
var profile = profiles[player.UserId];
if (profile.PreferenceUnavailable == PreferenceUnavailableMode.SpawnAsOverflow)
{
// 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, station)) in assignedJobs)
{
SpawnPlayer(player, profiles[player.UserId], station, job, false);
}
// Time to start the preset.
Preset = MakeGamePreset(profiles);
DisallowLateJoin |= Preset.DisallowLateJoin;
if (!Preset.Start(assignedJobs.Keys.ToList(), force))
{
if (_configurationManager.GetCVar(CCVars.GameLobbyFallbackEnabled))
{
SetStartPreset(_configurationManager.GetCVar(CCVars.GameLobbyFallbackPreset));
var newPreset = MakeGamePreset(profiles);
_chatManager.DispatchServerAnnouncement(
Loc.GetString("game-ticker-start-round-cannot-start-game-mode-fallback",
("failedGameMode", Preset.ModeTitle),
("fallbackMode", newPreset.ModeTitle)));
if (!newPreset.Start(readyPlayers, force))
{
throw new ApplicationException("Fallback preset failed to start!");
}
DisallowLateJoin = false;
DisallowLateJoin |= newPreset.DisallowLateJoin;
Preset = newPreset;
readyPlayers = _playersInLobby.Where(p => p.Value == LobbyPlayerStatus.Ready).Select(p => p.Key)
.ToList();
}
else
{
SendServerMessage(Loc.GetString("game-ticker-start-round-cannot-start-game-mode-restart", ("failedGameMode", Preset.ModeTitle)));
readyPlayers = _playersInLobby.Keys.ToList();
}
RoundLengthMetric.Set(0);
var playerIds = _playersInLobby.Keys.Select(player => player.UserId.UserId).ToArray();
RoundId = await _db.AddNewRound(playerIds);
var startingEvent = new RoundStartingEvent();
RaiseLocalEvent(startingEvent);
// Get the profiles for each player for easier lookup.
var profiles = _prefsManager.GetSelectedProfilesForPlayers(
readyPlayers
.Select(p => p.UserId).ToList())
.ToDictionary(p => p.Key, p => (HumanoidCharacterProfile) p.Value);
foreach (var readyPlayer in readyPlayers)
{
if (!profiles.ContainsKey(readyPlayer.UserId))
{
profiles.Add(readyPlayer.UserId, HumanoidCharacterProfile.Random());
}
}
var origReadyPlayers = readyPlayers.ToArray();
var startAttempt = new RoundStartAttemptEvent(origReadyPlayers, force);
RaiseLocalEvent(startAttempt);
var presetTitle = _preset != null ? Loc.GetString(_preset.ModeTitle) : string.Empty;
void FailedPresetRestart()
{
SendServerMessage(Loc.GetString("game-ticker-start-round-cannot-start-game-mode-restart",
("failedGameMode", presetTitle)));
RestartRound();
DelayStart(TimeSpan.FromSeconds(PresetFailedCooldownIncrease));
return;
}
}
Preset.OnGameStarted();
_roundStartTimeSpan = _gameTiming.RealTime;
SendStatusToAll();
ReqWindowAttentionAll();
UpdateLateJoinStatus();
UpdateJobsAvailable();
if (startAttempt.Cancelled)
{
if (_configurationManager.GetCVar(CCVars.GameLobbyFallbackEnabled))
{
var oldPreset = _preset;
ClearGameRules();
SetGamePreset(_configurationManager.GetCVar(CCVars.GameLobbyFallbackPreset));
AddGamePresetRules();
startAttempt.Uncancel();
RaiseLocalEvent(startAttempt);
_chatManager.DispatchServerAnnouncement(
Loc.GetString("game-ticker-start-round-cannot-start-game-mode-fallback",
("failedGameMode", presetTitle),
("fallbackMode", Loc.GetString(_preset!.ModeTitle))));
if (startAttempt.Cancelled)
{
FailedPresetRestart();
}
RefreshLateJoinAllowed();
}
else
{
FailedPresetRestart();
return;
}
}
// Allow game rules to spawn players by themselves if needed. (For example, nuke ops or wizard)
RaiseLocalEvent(new RulePlayerSpawningEvent(readyPlayers, profiles, force));
var assignedJobs = AssignJobs(readyPlayers, profiles);
// For players without jobs, give them the overflow job if they have that set...
foreach (var player in origReadyPlayers)
{
if (assignedJobs.ContainsKey(player))
{
continue;
}
var profile = profiles[player.UserId];
if (profile.PreferenceUnavailable == PreferenceUnavailableMode.SpawnAsOverflow)
{
// 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, station)) in assignedJobs)
{
SpawnPlayer(player, profiles[player.UserId], station, job, false);
}
RefreshLateJoinAllowed();
// Allow rules to add roles to players who have been spawned in. (For example, on-station traitors)
RaiseLocalEvent(new RulePlayerJobsAssignedEvent(assignedJobs.Keys.ToArray(), profiles, force));
_pauseManager.DoMapInitialize(DefaultMap);
_roundStartDateTime = DateTime.UtcNow;
RunLevel = GameRunLevel.InRound;
_roundStartTimeSpan = _gameTiming.RealTime;
SendStatusToAll();
ReqWindowAttentionAll();
UpdateLateJoinStatus();
UpdateJobsAvailable();
#if EXCEPTION_TOLERANCE
}
catch(Exception e)
{
Logger.WarningS("ticker", $"Exception caught while trying to start the round! Restarting...");
_runtimeLog.LogException(e, nameof(GameTicker));
RestartRound();
}
#endif
}
private void RefreshLateJoinAllowed()
{
var refresh = new RefreshLateJoinAllowedEvent();
RaiseLocalEvent(refresh);
DisallowLateJoin = refresh.DisallowLateJoin;
}
public void EndRound(string text = "")
@@ -241,8 +290,13 @@ namespace Content.Server.GameTicking
RunLevel = GameRunLevel.PostRound;
//Tell every client the round has ended.
var gamemodeTitle = Preset?.ModeTitle ?? string.Empty;
var roundEndText = text + $"\n{Preset?.GetRoundEndDescription() ?? string.Empty}";
var gamemodeTitle = _preset != null ? Loc.GetString(_preset.ModeTitle) : string.Empty;
// Let things add text here.
var textEv = new RoundEndTextAppendEvent();
RaiseLocalEvent(textEv);
var roundEndText = $"{text}\n{textEv.Text}";
//Get the timespan of the round.
var roundDuration = RoundDuration();
@@ -332,8 +386,6 @@ namespace Content.Server.GameTicking
}
else
{
Preset = null;
if (_playerManager.PlayerCount == 0)
_roundStartCountdownHasNotStartedYetDueToNoPlayers = true;
else
@@ -375,10 +427,7 @@ namespace Content.Server.GameTicking
_mapManager.Restart();
// Clear up any game rules.
foreach (var rule in _gameRules)
{
rule.Removed();
}
ClearGameRules();
_gameRules.Clear();
@@ -452,4 +501,102 @@ namespace Content.Server.GameTicking
New = @new;
}
}
/// <summary>
/// Event raised to refresh the late join status.
/// If you want to disallow late joins, listen to this and call Disallow.
/// </summary>
public class RefreshLateJoinAllowedEvent
{
public bool DisallowLateJoin { get; private set; } = false;
public void Disallow()
{
DisallowLateJoin = true;
}
}
/// <summary>
/// Attempt event raised on round start.
/// This can be listened to by GameRule systems to cancel round start if some condition is not met, like player count.
/// </summary>
public class RoundStartAttemptEvent : CancellableEntityEventArgs
{
public IPlayerSession[] Players { get; }
public bool Forced { get; }
public RoundStartAttemptEvent(IPlayerSession[] players, bool forced)
{
Players = players;
Forced = forced;
}
}
/// <summary>
/// Event raised before readied up players are spawned and given jobs by the GameTicker.
/// You can use this to spawn people off-station, like in the case of nuke ops or wizard.
/// Remove the players you spawned from the PlayerPool and call <see cref="GameTicker.PlayerJoinGame"/> on them.
/// </summary>
public class RulePlayerSpawningEvent
{
/// <summary>
/// Pool of players to be spawned.
/// If you want to handle a specific player being spawned, remove it from this list and do what you need.
/// </summary>
/// <remarks>If you spawn a player by yourself from this event, don't forget to call <see cref="GameTicker.PlayerJoinGame"/> on them.</remarks>
public List<IPlayerSession> PlayerPool { get; }
public IReadOnlyDictionary<NetUserId, HumanoidCharacterProfile> Profiles { get; }
public bool Forced { get; }
public RulePlayerSpawningEvent(List<IPlayerSession> playerPool, IReadOnlyDictionary<NetUserId, HumanoidCharacterProfile> profiles, bool forced)
{
PlayerPool = playerPool;
Profiles = profiles;
Forced = forced;
}
}
/// <summary>
/// Event raised after players were assigned jobs by the GameTicker.
/// You can give on-station people special roles by listening to this event.
/// </summary>
public class RulePlayerJobsAssignedEvent
{
public IPlayerSession[] Players { get; }
public IReadOnlyDictionary<NetUserId, HumanoidCharacterProfile> Profiles { get; }
public bool Forced { get; }
public RulePlayerJobsAssignedEvent(IPlayerSession[] players, IReadOnlyDictionary<NetUserId, HumanoidCharacterProfile> profiles, bool forced)
{
Players = players;
Profiles = profiles;
Forced = forced;
}
}
/// <summary>
/// Event raised to allow subscribers to add text to the round end summary screen.
/// </summary>
public class RoundEndTextAppendEvent
{
private bool _doNewLine;
/// <summary>
/// Text to display in the round end summary screen.
/// </summary>
public string Text { get; private set; } = string.Empty;
/// <summary>
/// Invoke this method to add text to the round end summary screen.
/// </summary>
/// <param name="text"></param>
public void AddLine(string text)
{
if (_doNewLine)
Text += "\n";
Text += text;
_doNewLine = true;
}
}
}