TechnologyDiskSystem.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using Content.Shared.Examine;
  2. using Content.Shared.Interaction;
  3. using Content.Shared.Lathe;
  4. using Content.Shared.Popups;
  5. using Content.Shared.Random.Helpers;
  6. using Content.Shared.Research.Components;
  7. using Content.Shared.Research.Prototypes;
  8. using Content.Shared.Research.Systems;
  9. using Content.Shared.Research.TechnologyDisk.Components;
  10. using Robust.Shared.Network;
  11. using Robust.Shared.Prototypes;
  12. using Robust.Shared.Random;
  13. namespace Content.Shared.Research.TechnologyDisk.Systems;
  14. public sealed class TechnologyDiskSystem : EntitySystem
  15. {
  16. [Dependency] private readonly INetManager _net = default!;
  17. [Dependency] private readonly IPrototypeManager _protoMan = default!;
  18. [Dependency] private readonly IRobustRandom _random = default!;
  19. [Dependency] private readonly SharedPopupSystem _popup = default!;
  20. [Dependency] private readonly SharedResearchSystem _research = default!;
  21. [Dependency] private readonly SharedLatheSystem _lathe = default!;
  22. public override void Initialize()
  23. {
  24. base.Initialize();
  25. SubscribeLocalEvent<TechnologyDiskComponent, MapInitEvent>(OnMapInit);
  26. SubscribeLocalEvent<TechnologyDiskComponent, AfterInteractEvent>(OnAfterInteract);
  27. SubscribeLocalEvent<TechnologyDiskComponent, ExaminedEvent>(OnExamine);
  28. }
  29. private void OnMapInit(Entity<TechnologyDiskComponent> ent, ref MapInitEvent args)
  30. {
  31. if (ent.Comp.Recipes != null)
  32. return;
  33. var weightedRandom = _protoMan.Index(ent.Comp.TierWeightPrototype);
  34. var tier = int.Parse(weightedRandom.Pick(_random));
  35. //get a list of every distinct recipe in all the technologies.
  36. var techs = new HashSet<ProtoId<LatheRecipePrototype>>();
  37. foreach (var tech in _protoMan.EnumeratePrototypes<TechnologyPrototype>())
  38. {
  39. if (tech.Tier != tier)
  40. continue;
  41. techs.UnionWith(tech.RecipeUnlocks);
  42. }
  43. if (techs.Count == 0)
  44. return;
  45. //pick one
  46. ent.Comp.Recipes = [];
  47. ent.Comp.Recipes.Add(_random.Pick(techs));
  48. Dirty(ent);
  49. }
  50. private void OnAfterInteract(Entity<TechnologyDiskComponent> ent, ref AfterInteractEvent args)
  51. {
  52. if (args.Handled || !args.CanReach || args.Target is not { } target)
  53. return;
  54. if (!HasComp<ResearchServerComponent>(target) || !TryComp<TechnologyDatabaseComponent>(target, out var database))
  55. return;
  56. if (ent.Comp.Recipes != null)
  57. {
  58. foreach (var recipe in ent.Comp.Recipes)
  59. {
  60. _research.AddLatheRecipe(target, recipe, database);
  61. }
  62. }
  63. _popup.PopupClient(Loc.GetString("tech-disk-inserted"), target, args.User);
  64. if (_net.IsServer)
  65. QueueDel(ent);
  66. args.Handled = true;
  67. }
  68. private void OnExamine(Entity<TechnologyDiskComponent> ent, ref ExaminedEvent args)
  69. {
  70. var message = Loc.GetString("tech-disk-examine-none");
  71. if (ent.Comp.Recipes != null && ent.Comp.Recipes.Count > 0)
  72. {
  73. var prototype = _protoMan.Index(ent.Comp.Recipes[0]);
  74. message = Loc.GetString("tech-disk-examine", ("result", _lathe.GetRecipeName(prototype)));
  75. if (ent.Comp.Recipes.Count > 1) //idk how to do this well. sue me.
  76. message += " " + Loc.GetString("tech-disk-examine-more");
  77. }
  78. args.PushMarkup(message);
  79. }
  80. }