From 7f060eb129f100754647e5221e9ebab2b00e6925 Mon Sep 17 00:00:00 2001 From: deltanedas <39013340+deltanedas@users.noreply.github.com> Date: Sat, 2 Mar 2024 16:33:24 +0000 Subject: [PATCH] make thief a subgamemode (#25740) * add SubGamemodes comp/sys * remove RuleChance from thief rule * use SubGamemodes for adding thief rule instead of adding ThiefRule component to random gamemodes * clean up thief rule prototype * add better logging + end rule if it fails to start * march 1st incident * preset ops * the dreaded --------- Co-authored-by: deltanedas <@deltanedas:kde.org> --- .../Rules/Components/SubGamemodesComponent.cs | 20 +++++++++++++++++++ .../Rules/Components/ThiefRuleComponent.cs | 6 ------ .../GameTicking/Rules/SubGamemodesSystem.cs | 17 ++++++++++++++++ .../GameTicking/Rules/ThiefRuleSystem.cs | 10 +++++----- Resources/Prototypes/GameRules/midround.yml | 7 +++---- Resources/Prototypes/GameRules/roundstart.yml | 16 +++++++++------ Resources/Prototypes/game_presets.yml | 3 +++ 7 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 Content.Server/GameTicking/Rules/Components/SubGamemodesComponent.cs create mode 100644 Content.Server/GameTicking/Rules/SubGamemodesSystem.cs diff --git a/Content.Server/GameTicking/Rules/Components/SubGamemodesComponent.cs b/Content.Server/GameTicking/Rules/Components/SubGamemodesComponent.cs new file mode 100644 index 0000000000..76ee6103d9 --- /dev/null +++ b/Content.Server/GameTicking/Rules/Components/SubGamemodesComponent.cs @@ -0,0 +1,20 @@ +using Content.Server.GameTicking.Rules; +using Content.Shared.Storage; + +namespace Content.Server.GameTicking.Rules.Components; + +/// +/// When this gamerule is added it has a chance of adding other gamerules. +/// Since it's done when added and not when started you can still use normal start logic. +/// Used for starting subgamemodes in game presets. +/// +[RegisterComponent, Access(typeof(SubGamemodesSystem))] +public sealed partial class SubGamemodesComponent : Component +{ + /// + /// Spawn entries for each gamerule prototype. + /// Use orGroups if you want to limit rules. + /// + [DataField(required: true)] + public List Rules = new(); +} diff --git a/Content.Server/GameTicking/Rules/Components/ThiefRuleComponent.cs b/Content.Server/GameTicking/Rules/Components/ThiefRuleComponent.cs index 3b0158dcd1..9dfd6e6627 100644 --- a/Content.Server/GameTicking/Rules/Components/ThiefRuleComponent.cs +++ b/Content.Server/GameTicking/Rules/Components/ThiefRuleComponent.cs @@ -29,12 +29,6 @@ public sealed partial class ThiefRuleComponent : Component [DataField] public bool PacifistThieves = true; - /// - /// A chance for this mode to be added to the game. - /// - [DataField] - public float RuleChance = 1f; - [DataField] public ProtoId ThiefPrototypeId = "Thief"; diff --git a/Content.Server/GameTicking/Rules/SubGamemodesSystem.cs b/Content.Server/GameTicking/Rules/SubGamemodesSystem.cs new file mode 100644 index 0000000000..42e7e82335 --- /dev/null +++ b/Content.Server/GameTicking/Rules/SubGamemodesSystem.cs @@ -0,0 +1,17 @@ +using Content.Server.GameTicking.Rules.Components; +using Content.Shared.Storage; + +namespace Content.Server.GameTicking.Rules; + +public sealed class SubGamemodesSystem : GameRuleSystem +{ + protected override void Added(EntityUid uid, SubGamemodesComponent comp, GameRuleComponent rule, GameRuleAddedEvent args) + { + var picked = EntitySpawnCollection.GetSpawns(comp.Rules, RobustRandom); + foreach (var id in picked) + { + Log.Info($"Starting gamerule {id} as a subgamemode of {ToPrettyString(uid):rule}"); + GameTicker.AddGameRule(id); + } + } +} diff --git a/Content.Server/GameTicking/Rules/ThiefRuleSystem.cs b/Content.Server/GameTicking/Rules/ThiefRuleSystem.cs index 32f6de608c..32f9040f89 100644 --- a/Content.Server/GameTicking/Rules/ThiefRuleSystem.cs +++ b/Content.Server/GameTicking/Rules/ThiefRuleSystem.cs @@ -37,19 +37,19 @@ public sealed class ThiefRuleSystem : GameRuleSystem private void OnPlayersSpawned(RulePlayerJobsAssignedEvent ev) { var query = QueryActiveRules(); - while (query.MoveNext(out _, out var comp, out _)) + while (query.MoveNext(out var uid, out _, out var comp, out var gameRule)) { - //Chance to not launch the game rule - if (!_random.Prob(comp.RuleChance)) - continue; - //Get all players eligible for this role, allow selecting existing antags //TO DO: When voxes specifies are added, increase their chance of becoming a thief by 4 times >:) var eligiblePlayers = _antagSelection.GetEligiblePlayers(ev.Players, comp.ThiefPrototypeId, acceptableAntags: AntagAcceptability.All, allowNonHumanoids: true); //Abort if there are none if (eligiblePlayers.Count == 0) + { + Log.Warning($"No eligible thieves found, ending game rule {ToPrettyString(uid):rule}"); + GameTicker.EndGameRule(uid, gameRule); continue; + } //Calculate number of thieves to choose var thiefCount = _random.Next(1, comp.MaxAllowThief + 1); diff --git a/Resources/Prototypes/GameRules/midround.yml b/Resources/Prototypes/GameRules/midround.yml index 28767e2c18..37fc4b44cd 100644 --- a/Resources/Prototypes/GameRules/midround.yml +++ b/Resources/Prototypes/GameRules/midround.yml @@ -28,13 +28,12 @@ - CarpRiftsObjective - DragonSurviveObjective -# need for admin panel antag create (because the rule doesn't have a roundstart entity like TraitorRule) - type: entity - id: Thief - parent: BaseGameRule noSpawn: true + parent: BaseGameRule + id: Thief components: - - type: ThiefRule + - type: ThiefRule - type: entity noSpawn: true diff --git a/Resources/Prototypes/GameRules/roundstart.yml b/Resources/Prototypes/GameRules/roundstart.yml index a3ef4a7f0b..a836faf500 100644 --- a/Resources/Prototypes/GameRules/roundstart.yml +++ b/Resources/Prototypes/GameRules/roundstart.yml @@ -5,6 +5,16 @@ components: - type: GameRule +- type: entity + noSpawn: true + parent: BaseGameRule + id: SubGamemodesRule + components: + - type: SubGamemodes + rules: + - id: Thief + prob: 0.5 + - type: entity id: DeathMatch31 parent: BaseGameRule @@ -62,8 +72,6 @@ minPlayers: 20 - type: NukeopsRule faction: Syndicate - - type: ThiefRule #the thieves come as an extension of another gamemode - ruleChance: 0.5 - type: entity id: Pirates @@ -78,8 +86,6 @@ noSpawn: true components: - type: TraitorRule - - type: ThiefRule #the thieves come as an extension of another gamemode - ruleChance: 0.5 - type: entity id: Revolutionary @@ -87,8 +93,6 @@ noSpawn: true components: - type: RevolutionaryRule - - type: ThiefRule #the thieves come as an extension of another gamemode - ruleChance: 0.5 - type: entity id: Sandbox diff --git a/Resources/Prototypes/game_presets.yml b/Resources/Prototypes/game_presets.yml index f1f16ab9b8..a5b20a3db6 100644 --- a/Resources/Prototypes/game_presets.yml +++ b/Resources/Prototypes/game_presets.yml @@ -93,6 +93,7 @@ showInVote: false rules: - Traitor + - SubGamemodesRule - BasicStationEventScheduler - BasicRoundstartVariation @@ -118,6 +119,7 @@ showInVote: false rules: - Nukeops + - SubGamemodesRule - BasicStationEventScheduler - BasicRoundstartVariation @@ -132,6 +134,7 @@ showInVote: false rules: - Revolutionary + - SubGamemodesRule - BasicStationEventScheduler - BasicRoundstartVariation -- 2.51.2