]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Add prediction for Tech Disks, cleanup (#29061)
authorTayrtahn <tayrtahn@gmail.com>
Wed, 19 Jun 2024 15:04:30 +0000 (11:04 -0400)
committerGitHub <noreply@github.com>
Wed, 19 Jun 2024 15:04:30 +0000 (01:04 +1000)
* Add prediction for Tech Disks, cleanup

* Remove IsServer check in OnMapInit

* Use HashSet for techs, remove LINQ

Content.Server/Research/Systems/ResearchSystem.Technology.cs
Content.Server/Research/TechnologyDisk/Components/TechnologyDiskComponent.cs [deleted file]
Content.Server/Research/TechnologyDisk/Systems/TechnologyDiskSystem.cs [deleted file]
Content.Shared/Research/Systems/SharedResearchSystem.cs
Content.Shared/Research/TechnologyDisk/Components/TechnologyDiskComponent.cs [new file with mode: 0644]
Content.Shared/Research/TechnologyDisk/Systems/TechnologyDiskSystem.cs [new file with mode: 0644]

index 9bd71cf7c6ec85b67a6bbeca70c81db0b7656d48..7578d316c5e8946dfd834155dcd7aa04d8c57e74 100644 (file)
@@ -131,25 +131,6 @@ public sealed partial class ResearchSystem
         RaiseLocalEvent(uid, ref ev);
     }
 
-    /// <summary>
-    /// Adds a lathe recipe to the specified technology database
-    /// without checking if it can be unlocked.
-    /// </summary>
-    public void AddLatheRecipe(EntityUid uid, string recipe, TechnologyDatabaseComponent? component = null)
-    {
-        if (!Resolve(uid, ref component))
-            return;
-
-        if (component.UnlockedRecipes.Contains(recipe))
-            return;
-
-        component.UnlockedRecipes.Add(recipe);
-        Dirty(uid, component);
-
-        var ev = new TechnologyDatabaseModifiedEvent();
-        RaiseLocalEvent(uid, ref ev);
-    }
-
     /// <summary>
     ///     Returns whether a technology can be unlocked on this database,
     ///     taking parent technologies into account.
diff --git a/Content.Server/Research/TechnologyDisk/Components/TechnologyDiskComponent.cs b/Content.Server/Research/TechnologyDisk/Components/TechnologyDiskComponent.cs
deleted file mode 100644 (file)
index eb5a062..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-using Content.Shared.Random;
-using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
-
-namespace Content.Server.Research.TechnologyDisk.Components;
-
-[RegisterComponent]
-public sealed partial class TechnologyDiskComponent : Component
-{
-    /// <summary>
-    /// The recipe that will be added. If null, one will be randomly generated
-    /// </summary>
-    [DataField("recipes")]
-    public List<string>? Recipes;
-
-    /// <summary>
-    /// A weighted random prototype for how rare each tier should be.
-    /// </summary>
-    [DataField("tierWeightPrototype", customTypeSerializer: typeof(PrototypeIdSerializer<WeightedRandomPrototype>))]
-    public string TierWeightPrototype = "TechDiskTierWeights";
-}
diff --git a/Content.Server/Research/TechnologyDisk/Systems/TechnologyDiskSystem.cs b/Content.Server/Research/TechnologyDisk/Systems/TechnologyDiskSystem.cs
deleted file mode 100644 (file)
index 176b2b6..0000000
+++ /dev/null
@@ -1,92 +0,0 @@
-using System.Linq;
-using Content.Server.Popups;
-using Content.Server.Research.Systems;
-using Content.Server.Research.TechnologyDisk.Components;
-using Content.Shared.Examine;
-using Content.Shared.Interaction;
-using Content.Shared.Random;
-using Content.Shared.Random.Helpers;
-using Content.Shared.Research.Components;
-using Content.Shared.Research.Prototypes;
-using Robust.Shared.Prototypes;
-using Robust.Shared.Random;
-
-namespace Content.Server.Research.TechnologyDisk.Systems;
-
-public sealed class TechnologyDiskSystem : EntitySystem
-{
-    [Dependency] private readonly PopupSystem _popup = default!;
-    [Dependency] private readonly ResearchSystem _research = default!;
-    [Dependency] private readonly IPrototypeManager _prototype = default!;
-    [Dependency] private readonly IRobustRandom _random = default!;
-
-    /// <inheritdoc/>
-    public override void Initialize()
-    {
-        SubscribeLocalEvent<TechnologyDiskComponent, AfterInteractEvent>(OnAfterInteract);
-        SubscribeLocalEvent<TechnologyDiskComponent, ExaminedEvent>(OnExamine);
-        SubscribeLocalEvent<TechnologyDiskComponent, MapInitEvent>(OnMapInit);
-    }
-
-    private void OnAfterInteract(EntityUid uid, TechnologyDiskComponent component, AfterInteractEvent args)
-    {
-        if (args.Handled || !args.CanReach || args.Target is not { } target)
-            return;
-
-        if (!HasComp<ResearchServerComponent>(target) || !TryComp<TechnologyDatabaseComponent>(target, out var database))
-            return;
-
-        if (component.Recipes != null)
-        {
-            foreach (var recipe in component.Recipes)
-            {
-                _research.AddLatheRecipe(target, recipe, database);
-            }
-        }
-        _popup.PopupEntity(Loc.GetString("tech-disk-inserted"), target, args.User);
-        QueueDel(uid);
-        args.Handled = true;
-    }
-
-    private void OnExamine(EntityUid uid, TechnologyDiskComponent component, ExaminedEvent args)
-    {
-        var message = Loc.GetString("tech-disk-examine-none");
-        if (component.Recipes != null && component.Recipes.Any())
-        {
-            var prototype = _prototype.Index<LatheRecipePrototype>(component.Recipes[0]);
-            var resultPrototype = _prototype.Index<EntityPrototype>(prototype.Result);
-            message = Loc.GetString("tech-disk-examine", ("result", resultPrototype.Name));
-
-            if (component.Recipes.Count > 1) //idk how to do this well. sue me.
-                message += " " + Loc.GetString("tech-disk-examine-more");
-        }
-        args.PushMarkup(message);
-    }
-
-    private void OnMapInit(EntityUid uid, TechnologyDiskComponent component, MapInitEvent args)
-    {
-        if (component.Recipes != null)
-            return;
-
-        var weightedRandom = _prototype.Index<WeightedRandomPrototype>(component.TierWeightPrototype);
-        var tier = int.Parse(weightedRandom.Pick(_random));
-
-        //get a list of every distinct recipe in all the technologies.
-        var techs = new List<ProtoId<LatheRecipePrototype>>();
-        foreach (var tech in _prototype.EnumeratePrototypes<TechnologyPrototype>())
-        {
-            if (tech.Tier != tier)
-                continue;
-
-            techs.AddRange(tech.RecipeUnlocks);
-        }
-        techs = techs.Distinct().ToList();
-
-        if (!techs.Any())
-            return;
-
-        //pick one
-        component.Recipes = new();
-        component.Recipes.Add(_random.Pick(techs));
-    }
-}
index 9819e949b8b68ae9225a2e5f863632d1f60c2b15..81c6950f283b02fabd94c9a1a9070e84b5ce547c 100644 (file)
@@ -280,4 +280,23 @@ public abstract class SharedResearchSystem : EntitySystem
         comp.UnlockedTechnologies.Clear();
         Dirty(uid, comp);
     }
+
+    /// <summary>
+    /// Adds a lathe recipe to the specified technology database
+    /// without checking if it can be unlocked.
+    /// </summary>
+    public void AddLatheRecipe(EntityUid uid, string recipe, TechnologyDatabaseComponent? component = null)
+    {
+        if (!Resolve(uid, ref component))
+            return;
+
+        if (component.UnlockedRecipes.Contains(recipe))
+            return;
+
+        component.UnlockedRecipes.Add(recipe);
+        Dirty(uid, component);
+
+        var ev = new TechnologyDatabaseModifiedEvent();
+        RaiseLocalEvent(uid, ref ev);
+    }
 }
diff --git a/Content.Shared/Research/TechnologyDisk/Components/TechnologyDiskComponent.cs b/Content.Shared/Research/TechnologyDisk/Components/TechnologyDiskComponent.cs
new file mode 100644 (file)
index 0000000..ce8a138
--- /dev/null
@@ -0,0 +1,24 @@
+using Content.Shared.Random;
+using Content.Shared.Research.Prototypes;
+using Robust.Shared.GameStates;
+using Robust.Shared.Prototypes;
+
+namespace Content.Shared.Research.TechnologyDisk.Components;
+
+[RegisterComponent, NetworkedComponent]
+[AutoGenerateComponentState]
+public sealed partial class TechnologyDiskComponent : Component
+{
+    /// <summary>
+    /// The recipe that will be added. If null, one will be randomly generated
+    /// </summary>
+    [DataField]
+    [AutoNetworkedField]
+    public List<ProtoId<LatheRecipePrototype>>? Recipes;
+
+    /// <summary>
+    /// A weighted random prototype for how rare each tier should be.
+    /// </summary>
+    [DataField]
+    public ProtoId<WeightedRandomPrototype> TierWeightPrototype = "TechDiskTierWeights";
+}
diff --git a/Content.Shared/Research/TechnologyDisk/Systems/TechnologyDiskSystem.cs b/Content.Shared/Research/TechnologyDisk/Systems/TechnologyDiskSystem.cs
new file mode 100644 (file)
index 0000000..b0d615f
--- /dev/null
@@ -0,0 +1,94 @@
+using Content.Shared.Examine;
+using Content.Shared.Interaction;
+using Content.Shared.Popups;
+using Content.Shared.Random.Helpers;
+using Content.Shared.Research.Components;
+using Content.Shared.Research.Prototypes;
+using Content.Shared.Research.Systems;
+using Content.Shared.Research.TechnologyDisk.Components;
+using Robust.Shared.Network;
+using Robust.Shared.Prototypes;
+using Robust.Shared.Random;
+
+namespace Content.Shared.Research.TechnologyDisk.Systems;
+
+public sealed class TechnologyDiskSystem : EntitySystem
+{
+    [Dependency] private readonly INetManager _net = default!;
+    [Dependency] private readonly IPrototypeManager _protoMan = default!;
+    [Dependency] private readonly IRobustRandom _random = default!;
+    [Dependency] private readonly SharedPopupSystem _popup = default!;
+    [Dependency] private readonly SharedResearchSystem _research = default!;
+
+    public override void Initialize()
+    {
+        base.Initialize();
+
+        SubscribeLocalEvent<TechnologyDiskComponent, MapInitEvent>(OnMapInit);
+        SubscribeLocalEvent<TechnologyDiskComponent, AfterInteractEvent>(OnAfterInteract);
+        SubscribeLocalEvent<TechnologyDiskComponent, ExaminedEvent>(OnExamine);
+    }
+
+    private void OnMapInit(Entity<TechnologyDiskComponent> ent, ref MapInitEvent args)
+    {
+        if (ent.Comp.Recipes != null)
+            return;
+
+        var weightedRandom = _protoMan.Index(ent.Comp.TierWeightPrototype);
+        var tier = int.Parse(weightedRandom.Pick(_random));
+
+        //get a list of every distinct recipe in all the technologies.
+        var techs = new HashSet<ProtoId<LatheRecipePrototype>>();
+        foreach (var tech in _protoMan.EnumeratePrototypes<TechnologyPrototype>())
+        {
+            if (tech.Tier != tier)
+                continue;
+
+            techs.UnionWith(tech.RecipeUnlocks);
+        }
+
+        if (techs.Count == 0)
+            return;
+
+        //pick one
+        ent.Comp.Recipes = [];
+        ent.Comp.Recipes.Add(_random.Pick(techs));
+        Dirty(ent);
+    }
+
+    private void OnAfterInteract(Entity<TechnologyDiskComponent> ent, ref AfterInteractEvent args)
+    {
+        if (args.Handled || !args.CanReach || args.Target is not { } target)
+            return;
+
+        if (!HasComp<ResearchServerComponent>(target) || !TryComp<TechnologyDatabaseComponent>(target, out var database))
+            return;
+
+        if (ent.Comp.Recipes != null)
+        {
+            foreach (var recipe in ent.Comp.Recipes)
+            {
+                _research.AddLatheRecipe(target, recipe, database);
+            }
+        }
+        _popup.PopupClient(Loc.GetString("tech-disk-inserted"), target, args.User);
+        if (_net.IsServer)
+            QueueDel(ent);
+        args.Handled = true;
+    }
+
+    private void OnExamine(Entity<TechnologyDiskComponent> ent, ref ExaminedEvent args)
+    {
+        var message = Loc.GetString("tech-disk-examine-none");
+        if (ent.Comp.Recipes != null && ent.Comp.Recipes.Count > 0)
+        {
+            var prototype = _protoMan.Index(ent.Comp.Recipes[0]);
+            var resultPrototype = _protoMan.Index<EntityPrototype>(prototype.Result);
+            message = Loc.GetString("tech-disk-examine", ("result", resultPrototype.Name));
+
+            if (ent.Comp.Recipes.Count > 1) //idk how to do this well. sue me.
+                message += " " + Loc.GetString("tech-disk-examine-more");
+        }
+        args.PushMarkup(message);
+    }
+}