ConstructionSystem.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using Content.Server.Construction.Components;
  2. using Content.Server.Stack;
  3. using Content.Shared.Construction;
  4. using Content.Shared.DoAfter;
  5. using JetBrains.Annotations;
  6. using Robust.Server.Containers;
  7. using Robust.Shared.Prototypes;
  8. using Robust.Shared.Random;
  9. using SharedToolSystem = Content.Shared.Tools.Systems.SharedToolSystem;
  10. namespace Content.Server.Construction
  11. {
  12. /// <summary>
  13. /// The server-side implementation of the construction system, which is used for constructing entities in game.
  14. /// </summary>
  15. [UsedImplicitly]
  16. public sealed partial class ConstructionSystem : SharedConstructionSystem
  17. {
  18. [Dependency] private readonly IRobustRandom _robustRandom = default!;
  19. [Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
  20. [Dependency] private readonly ContainerSystem _container = default!;
  21. [Dependency] private readonly StackSystem _stackSystem = default!;
  22. [Dependency] private readonly SharedToolSystem _toolSystem = default!;
  23. public override void Initialize()
  24. {
  25. base.Initialize();
  26. InitializeComputer();
  27. InitializeGraphs();
  28. InitializeGuided();
  29. InitializeInteractions();
  30. InitializeInitial();
  31. InitializeMachines();
  32. SubscribeLocalEvent<ConstructionComponent, ComponentInit>(OnConstructionInit);
  33. SubscribeLocalEvent<ConstructionComponent, ComponentStartup>(OnConstructionStartup);
  34. }
  35. private void OnConstructionInit(Entity<ConstructionComponent> ent, ref ComponentInit args)
  36. {
  37. var construction = ent.Comp;
  38. if (GetCurrentGraph(ent, construction) is not {} graph)
  39. {
  40. Log.Warning($"Prototype {EntityManager.GetComponent<MetaDataComponent>(ent).EntityPrototype?.ID}'s construction component has an invalid graph specified.");
  41. return;
  42. }
  43. if (GetNodeFromGraph(graph, construction.Node) is not {} node)
  44. {
  45. Log.Warning($"Prototype {EntityManager.GetComponent<MetaDataComponent>(ent).EntityPrototype?.ID}'s construction component has an invalid node specified.");
  46. return;
  47. }
  48. ConstructionGraphEdge? edge = null;
  49. if (construction.EdgeIndex is {} edgeIndex)
  50. {
  51. if (GetEdgeFromNode(node, edgeIndex) is not {} currentEdge)
  52. {
  53. Log.Warning($"Prototype {EntityManager.GetComponent<MetaDataComponent>(ent).EntityPrototype?.ID}'s construction component has an invalid edge index specified.");
  54. return;
  55. }
  56. edge = currentEdge;
  57. }
  58. if (construction.TargetNode is {} targetNodeId)
  59. {
  60. if (GetNodeFromGraph(graph, targetNodeId) is not { } targetNode)
  61. {
  62. Log.Warning($"Prototype {EntityManager.GetComponent<MetaDataComponent>(ent).EntityPrototype?.ID}'s construction component has an invalid target node specified.");
  63. return;
  64. }
  65. UpdatePathfinding(ent, graph, node, targetNode, edge, construction);
  66. }
  67. }
  68. private void OnConstructionStartup(EntityUid uid, ConstructionComponent construction, ComponentStartup args)
  69. {
  70. if (GetCurrentNode(uid, construction) is not {} node)
  71. return;
  72. PerformActions(uid, null, node.Actions);
  73. }
  74. public override void Update(float frameTime)
  75. {
  76. base.Update(frameTime);
  77. UpdateInteractions();
  78. }
  79. }
  80. }