1
0

ConstructionSystem.Machine.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using Content.Server.Construction.Components;
  2. using Content.Shared.Construction.Components;
  3. using Robust.Shared.Containers;
  4. namespace Content.Server.Construction;
  5. public sealed partial class ConstructionSystem
  6. {
  7. private void InitializeMachines()
  8. {
  9. SubscribeLocalEvent<MachineComponent, ComponentInit>(OnMachineInit);
  10. SubscribeLocalEvent<MachineComponent, MapInitEvent>(OnMachineMapInit);
  11. }
  12. private void OnMachineInit(EntityUid uid, MachineComponent component, ComponentInit args)
  13. {
  14. component.BoardContainer = _container.EnsureContainer<Container>(uid, MachineFrameComponent.BoardContainerName);
  15. component.PartContainer = _container.EnsureContainer<Container>(uid, MachineFrameComponent.PartContainerName);
  16. }
  17. private void OnMachineMapInit(EntityUid uid, MachineComponent component, MapInitEvent args)
  18. {
  19. CreateBoardAndStockParts(uid, component);
  20. }
  21. private void CreateBoardAndStockParts(EntityUid uid, MachineComponent component)
  22. {
  23. // Entity might not be initialized yet.
  24. var boardContainer = _container.EnsureContainer<Container>(uid, MachineFrameComponent.BoardContainerName);
  25. var partContainer = _container.EnsureContainer<Container>(uid, MachineFrameComponent.PartContainerName);
  26. if (string.IsNullOrEmpty(component.Board))
  27. return;
  28. // We're done here, let's suppose all containers are correct just so we don't screw SaveLoadSave.
  29. if (boardContainer.ContainedEntities.Count > 0)
  30. return;
  31. var xform = Transform(uid);
  32. if (!TrySpawnInContainer(component.Board, uid, MachineFrameComponent.BoardContainerName, out var board))
  33. {
  34. throw new Exception($"Couldn't insert board with prototype {component.Board} to machine with prototype {Prototype(uid)?.ID ?? "N/A"}!");
  35. }
  36. if (!TryComp<MachineBoardComponent>(board, out var machineBoard))
  37. {
  38. throw new Exception($"Entity with prototype {component.Board} doesn't have a {nameof(MachineBoardComponent)}!");
  39. }
  40. foreach (var (stackType, amount) in machineBoard.StackRequirements)
  41. {
  42. var stack = _stackSystem.Spawn(amount, stackType, xform.Coordinates);
  43. if (!_container.Insert(stack, partContainer))
  44. throw new Exception($"Couldn't insert machine material of type {stackType} to machine with prototype {Prototype(uid)?.ID ?? "N/A"}");
  45. }
  46. foreach (var (compName, info) in machineBoard.ComponentRequirements)
  47. {
  48. for (var i = 0; i < info.Amount; i++)
  49. {
  50. if(!TrySpawnInContainer(info.DefaultPrototype, uid, MachineFrameComponent.PartContainerName, out _))
  51. throw new Exception($"Couldn't insert machine component part with default prototype '{compName}' to machine with prototype {Prototype(uid)?.ID ?? "N/A"}");
  52. }
  53. }
  54. foreach (var (tagName, info) in machineBoard.TagRequirements)
  55. {
  56. for (var i = 0; i < info.Amount; i++)
  57. {
  58. if(!TrySpawnInContainer(info.DefaultPrototype, uid, MachineFrameComponent.PartContainerName, out _))
  59. throw new Exception($"Couldn't insert machine component part with default prototype '{tagName}' to machine with prototype {Prototype(uid)?.ID ?? "N/A"}");
  60. }
  61. }
  62. }
  63. }