Add prediction for Tech Disks, cleanup (#29061)

* Add prediction for Tech Disks, cleanup

* Remove IsServer check in OnMapInit

* Use HashSet for techs, remove LINQ
This commit is contained in:
Tayrtahn
2024-06-19 11:04:30 -04:00
committed by GitHub
parent 20544b0c76
commit 024521b66b
6 changed files with 137 additions and 131 deletions

View 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.

View File

@@ -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";
}

View File

@@ -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));
}
}

View 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);
}
}

View File

@@ -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";
}

View File

@@ -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);
}
}