]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
use weighted random for ninja threats (#20469)
authordeltanedas <39013340+deltanedas@users.noreply.github.com>
Mon, 25 Sep 2023 03:16:09 +0000 (04:16 +0100)
committerGitHub <noreply@github.com>
Mon, 25 Sep 2023 03:16:09 +0000 (23:16 -0400)
* change threats to be weighted random and a little cleanup

* ninja rule stores weighted random id for threats

* move threats out of the rule and into weighted random

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>
Content.Server/Communications/CommsHackerSystem.cs
Content.Server/GameTicking/Rules/Components/NinjaRuleComponent.cs
Content.Shared/Communications/CommsHackerComponent.cs
Content.Shared/Communications/SharedCommsHackerSystem.cs
Resources/Prototypes/GameRules/midround.yml
Resources/Prototypes/threats.yml [new file with mode: 0644]

index 95cad6eb97411f97aa52686d017acb7a711fbb49..851be07454fb4d40f7d8f79f9ae9b693d42e755b 100644 (file)
@@ -4,6 +4,9 @@ using Content.Server.Ninja.Systems;
 using Content.Shared.Communications;
 using Content.Shared.DoAfter;
 using Content.Shared.Interaction;
+using Content.Shared.Random;
+using Content.Shared.Random.Helpers;
+using Robust.Shared.Prototypes;
 using Robust.Shared.Random;
 using Robust.Shared.Serialization;
 
@@ -13,6 +16,7 @@ public sealed class CommsHackerSystem : SharedCommsHackerSystem
 {
     [Dependency] private readonly ChatSystem _chat = default!;
     [Dependency] private readonly GameTicker _gameTicker = default!;
+    [Dependency] private readonly IPrototypeManager _proto = default!;
     [Dependency] private readonly IRobustRandom _random = default!;
     // TODO: remove when generic check event is used
     [Dependency] private readonly NinjaGlovesSystem _gloves = default!;
@@ -55,11 +59,12 @@ public sealed class CommsHackerSystem : SharedCommsHackerSystem
     /// </summary>
     private void OnDoAfter(EntityUid uid, CommsHackerComponent comp, TerrorDoAfterEvent args)
     {
-        if (args.Cancelled || args.Handled || comp.Threats.Count == 0 || args.Target == null)
+        if (args.Cancelled || args.Handled || args.Target == null)
             return;
 
-        var threat = _random.Pick(comp.Threats);
-        CallInThreat(threat);
+        var threats = _proto.Index<WeightedRandomPrototype>(comp.Threats);
+        var threat = threats.Pick(_random);
+        CallInThreat(_proto.Index<ThreatPrototype>(threat));
 
         // prevent calling in multiple threats
         RemComp<CommsHackerComponent>(uid);
@@ -71,7 +76,7 @@ public sealed class CommsHackerSystem : SharedCommsHackerSystem
     /// <summary>
     /// Makes announcement and adds game rule of the threat.
     /// </summary>
-    public void CallInThreat(Threat threat)
+    public void CallInThreat(ThreatPrototype threat)
     {
         _gameTicker.StartGameRule(threat.Rule, out _);
         _chat.DispatchGlobalAnnouncement(Loc.GetString(threat.Announcement), playSound: true, colorOverride: Color.Red);
index 6a1a4a6a9b008c8feecfb851e0859d79183464a6..e6966c1e377cccf2163dd08b036b0e58ec5e7d9c 100644 (file)
@@ -1,6 +1,8 @@
 using Content.Server.Ninja.Systems;
 using Content.Shared.Communications;
+using Content.Shared.Random;
 using Robust.Shared.Audio;
+using Robust.Shared.Prototypes;
 
 namespace Content.Server.GameTicking.Rules.Components;
 
@@ -15,7 +17,7 @@ public sealed partial class NinjaRuleComponent : Component
     /// List of threats that can be called in. Copied onto <see cref="CommsHackerComponent"/> when gloves are enabled.
     /// </summary>
     [DataField(required: true)]
-    public List<Threat> Threats = new();
+    public ProtoId<WeightedRandomPrototype> Threats = string.Empty;
 
     /// <summary>
     /// Sound played when making the player a ninja via antag control or ghost role
index 9116899ccae39dbfe2ef0e445998c4a02705a5c2..fdd1876c1d769713be9d7ed2250db01a4e8fbd52 100644 (file)
@@ -1,7 +1,6 @@
+using Content.Shared.Random;
 using Robust.Shared.GameStates;
 using Robust.Shared.Prototypes;
-using Robust.Shared.Serialization;
-using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
 
 namespace Content.Shared.Communications;
 
@@ -15,14 +14,14 @@ public sealed partial class CommsHackerComponent : Component
     /// <summary>
     /// Time taken to hack the console
     /// </summary>
-    [DataField("delay")]
+    [DataField, ViewVariables(VVAccess.ReadWrite)]
     public TimeSpan Delay = TimeSpan.FromSeconds(20);
 
     /// <summary>
-    /// Possible threats to choose from.
+    /// Weighted random for the possible threats to choose from.
     /// </summary>
-    [DataField("threats", required: true)]
-    public List<Threat> Threats = new();
+    [DataField(required: true)]
+    public ProtoId<WeightedRandomPrototype> Threats = string.Empty;
 }
 
 /// <summary>
@@ -30,18 +29,21 @@ public sealed partial class CommsHackerComponent : Component
 /// Generally some kind of mid-round minor antag, though you could make it call in scrubber backflow if you wanted to.
 /// You wouldn't do that, right?
 /// </summary>
-[DataDefinition]
-public sealed partial class Threat
+[Prototype("threat")]
+public sealed class ThreatPrototype : IPrototype
 {
+    [IdDataField]
+    public string ID { get; private set; } = default!;
+
     /// <summary>
     /// Locale id for the announcement to be made from CentCom.
     /// </summary>
-    [DataField("announcement")]
+    [DataField]
     public string Announcement = default!;
 
     /// <summary>
     /// The game rule for the threat to be added, it should be able to work when added mid-round otherwise this will do nothing.
     /// </summary>
-    [DataField("rule", customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
-    public string Rule = default!;
+    [DataField]
+    public EntProtoId Rule = default!;
 }
index 94c530878a4cb201c3df6745c6b275b594acf6ee..c4d9c1e19788844439d6909f72c22efc002b19bc 100644 (file)
@@ -10,9 +10,9 @@ namespace Content.Shared.Communications;
 public abstract class SharedCommsHackerSystem : EntitySystem
 {
     /// <summary>
-    /// Set the list of threats to choose from when hacking a comms console.
+    /// Set the threats prototype to choose from when hacking a comms console.
     /// </summary>
-    public void SetThreats(EntityUid uid, List<Threat> threats, CommsHackerComponent? comp = null)
+    public void SetThreats(EntityUid uid, string threats, CommsHackerComponent? comp = null)
     {
         if (!Resolve(uid, ref comp))
             return;
index 41fb9fa432f641eba08447d8722b017254fc16a6..1927cde53c37fc68977ba1c04220ebddedbc71b9 100644 (file)
@@ -14,8 +14,4 @@
     - TerrorObjective
     - NinjaSurviveObjective
   - type: NinjaRule
-    threats:
-    - announcement: terror-dragon
-      rule: Dragon
-    - announcement: terror-revenant
-      rule: RevenantSpawn
+    threats: NinjaThreats
diff --git a/Resources/Prototypes/threats.yml b/Resources/Prototypes/threats.yml
new file mode 100644 (file)
index 0000000..6e21cc1
--- /dev/null
@@ -0,0 +1,16 @@
+# threats called in by ninja hacking comms console
+- type: weightedRandom
+  id: NinjaThreats
+  weights:
+    Dragon: 1
+    Revenant: 1
+
+- type: threat
+  id: Dragon
+  announcement: terror-dragon
+  rule: Dragon
+
+- type: threat
+  id: Revenant
+  announcement: terror-revenant
+  rule: RevenantSpawn