Co-authored-by: moonheart08 <moonheart08@users.noreply.github.com>
This commit is contained in:
metalgearsloth
2022-05-19 14:44:24 +10:00
committed by GitHub
parent a6a25e30e5
commit 8d6a3ecea7
12 changed files with 133 additions and 22 deletions

View File

@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Server.GameTicking.Presets;
using Content.Server.GameTicking.Rules;
using Content.Server.Ghost.Components;
@@ -14,7 +15,7 @@ namespace Content.Server.GameTicking
{
public const float PresetFailedCooldownIncrease = 30f;
private GamePresetPrototype? _preset;
public GamePresetPrototype? Preset { get; private set; }
private bool StartPreset(IPlayerSession[] origReadyPlayers, bool force)
{
@@ -24,7 +25,7 @@ namespace Content.Server.GameTicking
if (!startAttempt.Cancelled)
return true;
var presetTitle = _preset != null ? Loc.GetString(_preset.ModeTitle) : string.Empty;
var presetTitle = Preset != null ? Loc.GetString(Preset.ModeTitle) : string.Empty;
void FailedPresetRestart()
{
@@ -36,7 +37,7 @@ namespace Content.Server.GameTicking
if (_configurationManager.GetCVar(CCVars.GameLobbyFallbackEnabled))
{
var oldPreset = _preset;
var oldPreset = Preset;
ClearGameRules();
SetGamePreset(_configurationManager.GetCVar(CCVars.GameLobbyFallbackPreset));
AddGamePresetRules();
@@ -48,7 +49,7 @@ namespace Content.Server.GameTicking
_chatManager.DispatchServerAnnouncement(
Loc.GetString("game-ticker-start-round-cannot-start-game-mode-fallback",
("failedGameMode", presetTitle),
("fallbackMode", Loc.GetString(_preset!.ModeTitle))));
("fallbackMode", Loc.GetString(Preset!.ModeTitle))));
if (startAttempt.Cancelled)
{
@@ -78,7 +79,7 @@ namespace Content.Server.GameTicking
if (DummyTicker)
return;
_preset = preset;
Preset = preset;
UpdateInfoText();
if (force)
@@ -120,10 +121,10 @@ namespace Content.Server.GameTicking
private bool AddGamePresetRules()
{
if (DummyTicker || _preset == null)
if (DummyTicker || Preset == null)
return false;
foreach (var rule in _preset.Rules)
foreach (var rule in Preset.Rules)
{
if (!_prototypeManager.TryIndex(rule, out GameRulePrototype? ruleProto))
continue;
@@ -136,7 +137,8 @@ namespace Content.Server.GameTicking
private void StartGamePresetRules()
{
foreach (var rule in _addedGameRules)
// May be touched by the preset during init.
foreach (var rule in _addedGameRules.ToArray())
{
StartGameRule(rule);
}

View File

@@ -35,7 +35,7 @@ namespace Content.Server.GameTicking
private string GetInfoText()
{
if (_preset == null)
if (Preset == null)
{
return string.Empty;
}
@@ -43,8 +43,8 @@ namespace Content.Server.GameTicking
var playerCount = $"{_playerManager.PlayerCount}";
var map = _gameMapManager.GetSelectedMap();
var mapName = map?.MapName ?? Loc.GetString("game-ticker-no-map-selected");
var gmTitle = Loc.GetString(_preset.ModeTitle);
var desc = Loc.GetString(_preset.Description);
var gmTitle = Loc.GetString(Preset.ModeTitle);
var desc = Loc.GetString(Preset.Description);
return Loc.GetString("game-ticker-get-info-text",("roundId", RoundId), ("playerCount", playerCount),("mapName", mapName),("gmTitle", gmTitle),("desc", desc));
}

View File

@@ -262,7 +262,7 @@ namespace Content.Server.GameTicking
RunLevel = GameRunLevel.PostRound;
//Tell every client the round has ended.
var gamemodeTitle = _preset != null ? Loc.GetString(_preset.ModeTitle) : string.Empty;
var gamemodeTitle = Preset != null ? Loc.GetString(Preset.ModeTitle) : string.Empty;
// Let things add text here.
var textEv = new RoundEndTextAppendEvent();
@@ -469,11 +469,11 @@ namespace Content.Server.GameTicking
private void AnnounceRound()
{
if (_preset == null) return;
if (Preset == null) return;
foreach (var proto in _prototypeManager.EnumeratePrototypes<RoundAnnouncementPrototype>())
{
if (!proto.GamePresets.Contains(_preset.ID)) continue;
if (!proto.GamePresets.Contains(Preset.ID)) continue;
if (proto.Message != null)
_chatManager.DispatchStationAnnouncement(Loc.GetString(proto.Message), playDefaultSound: false);

View File

@@ -83,8 +83,10 @@ public sealed class NukeopsRuleSystem : GameRuleSystem
_aliveNukeops.Clear();
var numOps = (int)Math.Min(Math.Floor((double)ev.PlayerPool.Count / _cfg.GetCVar(CCVars.NukeopsPlayersPerOp)),
_cfg.GetCVar(CCVars.NukeopsMaxOps));
// Between 1 and <max op count>: needs at least n players per op.
var numOps = Math.Max(1,
(int)Math.Min(
Math.Floor((double)ev.PlayerPool.Count / _cfg.GetCVar(CCVars.NukeopsPlayersPerOp)), _cfg.GetCVar(CCVars.NukeopsMaxOps)));
var ops = new IPlayerSession[numOps];
for (var i = 0; i < numOps; i++)
{

View File

@@ -0,0 +1,42 @@
using System.Linq;
using Content.Server.GameTicking.Presets;
using Content.Shared.Random;
using Content.Shared.Random.Helpers;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Server.GameTicking.Rules;
public sealed class SecretRuleSystem : GameRuleSystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly GameTicker _ticker = default!;
public override string Prototype => "Secret";
public override void Started()
{
PickRule();
}
public override void Ended()
{
// noop
// Preset should already handle it.
return;
}
private void PickRule()
{
// TODO: This doesn't consider what can't start due to minimum player count, but currently there's no way to know anyway.
// as they use cvars.
var preset = _prototypeManager.Index<WeightedRandomPrototype>("Secret").Pick(_random);
Logger.InfoS("gamepreset", $"Selected {preset} for secret.");
foreach (var rule in _prototypeManager.Index<GamePresetPrototype>(preset).Rules)
{
_ticker.AddGameRule(_prototypeManager.Index<GameRulePrototype>(rule));
}
}
}

View File

@@ -28,6 +28,7 @@ public sealed class TraitorRuleSystem : GameRuleSystem
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IObjectivesManager _objectivesManager = default!;
[Dependency] private readonly IChatManager _chatManager = default!;
[Dependency] private readonly GameTicker _gameTicker = default!;
public override string Prototype => "Traitor";
@@ -47,11 +48,7 @@ public sealed class TraitorRuleSystem : GameRuleSystem
SubscribeLocalEvent<RoundEndTextAppendEvent>(OnRoundEndText);
}
public override void Started()
{
// This seems silly, but I'll leave it.
_chatManager.DispatchServerAnnouncement(Loc.GetString("rule-traitor-added-announcement"));
}
public override void Started() {}
public override void Ended()
{
@@ -63,6 +60,13 @@ public sealed class TraitorRuleSystem : GameRuleSystem
if (!Enabled)
return;
// If the current preset doesn't explicitly contain the traitor game rule, just carry on and remove self.
if (_gameTicker.Preset?.Rules.Contains(Prototype) ?? false)
{
_gameTicker.EndGameRule(_prototypeManager.Index<GameRulePrototype>(Prototype));
return;
}
var minPlayers = _cfg.GetCVar(CCVars.TraitorMinPlayers);
if (!ev.Forced && ev.Players.Length < minPlayers)
{

View File

@@ -1,4 +1,5 @@
using Content.Shared.Dataset;
using System.Linq;
using Content.Shared.Dataset;
using Robust.Shared.Random;
namespace Content.Shared.Random.Helpers
@@ -9,5 +10,28 @@ namespace Content.Shared.Random.Helpers
{
return random.Pick(prototype.Values);
}
public static string Pick(this WeightedRandomPrototype prototype, IRobustRandom? random = null)
{
IoCManager.Resolve(ref random);
var picks = prototype.Weights;
var sum = picks.Values.Sum();
var accumulated = 0f;
var rand = random.NextFloat() * sum;
foreach (var (key, weight) in picks)
{
accumulated += weight;
if (accumulated >= rand)
{
return key;
}
}
// Shouldn't happen
throw new InvalidOperationException($"Invalid weighted pick for {prototype.ID}!");
}
}
}

View File

@@ -0,0 +1,15 @@
using Robust.Shared.Prototypes;
namespace Content.Shared.Random;
/// <summary>
/// Generic random weighting dataset to use.
/// </summary>
[Prototype("weightedRandom")]
public sealed class WeightedRandomPrototype : IPrototype
{
[IdDataFieldAttribute] public string ID { get; } = default!;
[DataField("weights")]
public Dictionary<string, float> Weights = new();
}

View File

@@ -0,0 +1,2 @@
secret-title = Secret
secret-description = It's a secret to everyone. The threats you encounter are randomized.

View File

@@ -7,6 +7,17 @@
showInVote: true
description: extended-description
- type: gamePreset
id: Secret
alias:
- secret
- sekrit
name: secret-title
showInVote: true
description: secret-description
rules:
- Secret
- type: gamePreset
id: Sandbox
alias:

View File

@@ -21,3 +21,6 @@
- type: gameRule
id: Sandbox
- type: gameRule
id: Secret

View File

@@ -0,0 +1,6 @@
- type: weightedRandom
id: Secret
weights:
Extended: 0.25
Nukeops: 0.25
Traitor: 0.75