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:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user