From d80934b156bf4d4bfa355ba66289b8547051989e Mon Sep 17 00:00:00 2001 From: Tayrtahn Date: Wed, 7 May 2025 20:38:56 -0400 Subject: [PATCH] Move random species selection earlier in player spawning logic (#37258) * Select random species earlier in spawning logic * ternary operator * Move it even earlier to fix more bugs --- .../GameTicking/GameTicker.Spawning.cs | 35 ++++++++++++ Content.Server/GameTicking/GameTicker.cs | 5 ++ .../Station/Systems/StationSpawningSystem.cs | 55 +------------------ 3 files changed, 41 insertions(+), 54 deletions(-) diff --git a/Content.Server/GameTicking/GameTicker.Spawning.cs b/Content.Server/GameTicking/GameTicker.Spawning.cs index 624bf35afe..4deacd954f 100644 --- a/Content.Server/GameTicking/GameTicker.Spawning.cs +++ b/Content.Server/GameTicking/GameTicker.Spawning.cs @@ -8,11 +8,16 @@ using Content.Server.Ghost; using Content.Server.Spawners.Components; using Content.Server.Speech.Components; using Content.Server.Station.Components; +using Content.Shared.CCVar; using Content.Shared.Database; using Content.Shared.GameTicking; +using Content.Shared.Humanoid; +using Content.Shared.Humanoid.Prototypes; using Content.Shared.Mind; using Content.Shared.Players; using Content.Shared.Preferences; +using Content.Shared.Random; +using Content.Shared.Random.Helpers; using Content.Shared.Roles; using Content.Shared.Roles.Jobs; using Robust.Shared.Map; @@ -180,6 +185,36 @@ namespace Content.Server.GameTicking return; } + string speciesId; + if (_randomizeCharacters) + { + var weightId = _cfg.GetCVar(CCVars.ICRandomSpeciesWeights); + + // If blank, choose a round start species. + if (string.IsNullOrEmpty(weightId)) + { + var roundStart = new List>(); + + var speciesPrototypes = _prototypeManager.EnumeratePrototypes(); + foreach (var proto in speciesPrototypes) + { + if (proto.RoundStart) + roundStart.Add(proto.ID); + } + + speciesId = roundStart.Count == 0 + ? SharedHumanoidAppearanceSystem.DefaultSpecies + : _robustRandom.Pick(roundStart); + } + else + { + var weights = _prototypeManager.Index(weightId); + speciesId = weights.Pick(_robustRandom); + } + + character = HumanoidCharacterProfile.RandomWithSpecies(speciesId); + } + // We raise this event to allow other systems to handle spawning this player themselves. (e.g. late-join wizard, etc) var bev = new PlayerBeforeSpawnEvent(player, character, jobId, lateJoin, station); RaiseLocalEvent(bev); diff --git a/Content.Server/GameTicking/GameTicker.cs b/Content.Server/GameTicking/GameTicker.cs index d286fd641e..fa8314cd27 100644 --- a/Content.Server/GameTicking/GameTicker.cs +++ b/Content.Server/GameTicking/GameTicker.cs @@ -9,6 +9,7 @@ using Content.Server.Players.PlayTimeTracking; using Content.Server.Preferences.Managers; using Content.Server.ServerUpdates; using Content.Server.Station.Systems; +using Content.Shared.CCVar; using Content.Shared.Chat; using Content.Shared.GameTicking; using Content.Shared.Mind; @@ -72,6 +73,8 @@ namespace Content.Server.GameTicking private ISawmill _sawmill = default!; + private bool _randomizeCharacters; + public override void Initialize() { base.Initialize(); @@ -82,6 +85,8 @@ namespace Content.Server.GameTicking _sawmill = _logManager.GetSawmill("ticker"); _sawmillReplays = _logManager.GetSawmill("ticker.replays"); + Subs.CVar(_cfg, CCVars.ICRandomCharacters, e => _randomizeCharacters = e, true); + // Initialize the other parts of the game ticker. InitializeStatusShell(); InitializeCVars(); diff --git a/Content.Server/Station/Systems/StationSpawningSystem.cs b/Content.Server/Station/Systems/StationSpawningSystem.cs index 16400039b9..924146bc1f 100644 --- a/Content.Server/Station/Systems/StationSpawningSystem.cs +++ b/Content.Server/Station/Systems/StationSpawningSystem.cs @@ -14,8 +14,6 @@ using Content.Shared.Humanoid.Prototypes; using Content.Shared.PDA; using Content.Shared.Preferences; using Content.Shared.Preferences.Loadouts; -using Content.Shared.Random; -using Content.Shared.Random.Helpers; using Content.Shared.Roles; using Content.Shared.Station; using JetBrains.Annotations; @@ -23,7 +21,6 @@ using Robust.Shared.Configuration; using Robust.Shared.Map; using Robust.Shared.Player; using Robust.Shared.Prototypes; -using Robust.Shared.Random; using Robust.Shared.Utility; namespace Content.Server.Station.Systems; @@ -44,16 +41,6 @@ public sealed class StationSpawningSystem : SharedStationSpawningSystem [Dependency] private readonly MetaDataSystem _metaSystem = default!; [Dependency] private readonly PdaSystem _pdaSystem = default!; [Dependency] private readonly IPrototypeManager _prototypeManager = default!; - [Dependency] private readonly IRobustRandom _random = default!; - - private bool _randomizeCharacters; - - /// - public override void Initialize() - { - base.Initialize(); - Subs.CVar(_configurationManager, CCVars.ICRandomCharacters, e => _randomizeCharacters = e, true); - } /// /// Attempts to spawn a player character onto the given station. @@ -136,53 +123,13 @@ public sealed class StationSpawningSystem : SharedStationSpawningSystem return jobEntity; } - string speciesId; - if (_randomizeCharacters) - { - var weightId = _configurationManager.GetCVar(CCVars.ICRandomSpeciesWeights); - - // If blank, choose a round start species. - if (string.IsNullOrEmpty(weightId)) - { - var roundStart = new List>(); - - var speciesPrototypes = _prototypeManager.EnumeratePrototypes(); - foreach (var proto in speciesPrototypes) - { - if (proto.RoundStart) - roundStart.Add(proto.ID); - } - - if (roundStart.Count == 0) - speciesId = SharedHumanoidAppearanceSystem.DefaultSpecies; - else - speciesId = _random.Pick(roundStart); - } - else - { - var weights = _prototypeManager.Index(weightId); - speciesId = weights.Pick(_random); - } - } - else if (profile != null) - { - speciesId = profile.Species; - } - else - { - speciesId = SharedHumanoidAppearanceSystem.DefaultSpecies; - } + string speciesId = profile != null ? profile.Species : SharedHumanoidAppearanceSystem.DefaultSpecies; if (!_prototypeManager.TryIndex(speciesId, out var species)) throw new ArgumentException($"Invalid species prototype was used: {speciesId}"); entity ??= Spawn(species.Prototype, coordinates); - if (_randomizeCharacters) - { - profile = HumanoidCharacterProfile.RandomWithSpecies(speciesId); - } - if (profile != null) { _humanoidSystem.LoadProfile(entity.Value, profile); -- 2.51.2