ConstructionGraphNode.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Diagnostics.CodeAnalysis;
  2. using Content.Shared.Construction.NodeEntities;
  3. using Content.Shared.Construction.Serialization;
  4. using Robust.Shared.Prototypes;
  5. using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
  6. namespace Content.Shared.Construction
  7. {
  8. [Serializable]
  9. [DataDefinition]
  10. public sealed partial class ConstructionGraphNode
  11. {
  12. [DataField("actions", serverOnly: true)]
  13. private IGraphAction[] _actions = Array.Empty<IGraphAction>();
  14. [DataField("edges")]
  15. private ConstructionGraphEdge[] _edges = Array.Empty<ConstructionGraphEdge>();
  16. [DataField("node", required: true)]
  17. public string Name { get; private set; } = default!;
  18. [ViewVariables]
  19. public IReadOnlyList<ConstructionGraphEdge> Edges => _edges;
  20. [ViewVariables]
  21. public IReadOnlyList<IGraphAction> Actions => _actions;
  22. [DataField("transform")]
  23. public IGraphTransform[] TransformLogic = Array.Empty<IGraphTransform>();
  24. [DataField("entity", customTypeSerializer: typeof(GraphNodeEntitySerializer), serverOnly:true)]
  25. public IGraphNodeEntity Entity { get; private set; } = new NullNodeEntity();
  26. /// <summary>
  27. /// Ignore requests to change the entity if the entity's current prototype inherits from specified replacement
  28. /// </summary>
  29. /// <remarks>
  30. /// When this bool is true and a construction node specifies that the current entity should be replaced with a new entity, if the
  31. /// current entity has an entity prototype which inherits from the replacement entity prototype, entity replacement will not occur.
  32. /// E.g., if an entity with the 'AirlockCommand' prototype was to be replaced with a new entity that had the 'Airlock' prototype,
  33. /// and 'DoNotReplaceInheritingEntities' was true, the entity would not be replaced because 'AirlockCommand' is derived from 'Airlock'
  34. /// This will largely be used for construction graphs which have removeable upgrades, such as hacking protections for airlocks,
  35. /// so that the upgrades can be removed and you can return to the last primary construction step without replacing the entity
  36. /// </remarks>
  37. [DataField("doNotReplaceInheritingEntities")]
  38. public bool DoNotReplaceInheritingEntities = false;
  39. public ConstructionGraphEdge? GetEdge(string target)
  40. {
  41. foreach (var edge in _edges)
  42. {
  43. if (edge.Target == target)
  44. return edge;
  45. }
  46. return null;
  47. }
  48. public int? GetEdgeIndex(string target)
  49. {
  50. for (var i = 0; i < _edges.Length; i++)
  51. {
  52. var edge = _edges[i];
  53. if (edge.Target == target)
  54. return i;
  55. }
  56. return null;
  57. }
  58. public bool TryGetEdge(string target, [NotNullWhen(true)] out ConstructionGraphEdge? edge)
  59. {
  60. return (edge = GetEdge(target)) != null;
  61. }
  62. }
  63. }