A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.9KB

  1. using System;
  2. using Gamecraft.Wires;
  3. using Svelto.ECS;
  4. using Unity.Mathematics;
  5. using GamecraftModdingAPI;
  6. using GamecraftModdingAPI.Utility;
  7. namespace GamecraftModdingAPI.Blocks
  8. {
  9. /// <summary>
  10. /// Common implementation for blocks that support wiring.
  11. /// </summary>
  12. public class SignalingBlock : Block
  13. {
  14. /// <summary>
  15. /// Places a new signaling block.
  16. /// Any valid functional block type with IO ports will work.
  17. /// This re-implements Block.PlaceNew(...)
  18. /// </summary>
  19. public static new SignalingBlock PlaceNew(BlockIDs block, float3 position,
  20. float3 rotation = default, BlockColors color = BlockColors.Default, byte darkness = 0,
  21. int uscale = 1, float3 scale = default, Player player = null)
  22. {
  23. if (PlacementEngine.IsInGame && GameState.IsBuildMode())
  24. {
  25. try
  26. {
  27. EGID id = PlacementEngine.PlaceBlock(block, color, darkness,
  28. position, uscale, scale, player, rotation);
  29. Sync();
  30. return new SignalingBlock(id);
  31. }
  32. catch (Exception e)
  33. {
  34. Logging.MetaDebugLog(e);
  35. }
  36. }
  37. return null;
  38. }
  39. public SignalingBlock(EGID id) : base(id)
  40. {
  41. if (!BlockEngine.GetBlockInfoExists<BlockPortsStruct>(this.Id))
  42. {
  43. throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
  44. }
  45. }
  46. public SignalingBlock(uint id) : base(id)
  47. {
  48. if (!BlockEngine.GetBlockInfoExists<BlockPortsStruct>(this.Id))
  49. {
  50. throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
  51. }
  52. }
  53. protected ref BlockPortsStruct GetBlockPortsStruct()
  54. {
  55. return ref BlockEngine.GetBlockInfo<BlockPortsStruct>(Id);
  56. }
  57. /// <summary>
  58. /// Generates the input port identifiers.
  59. /// </summary>
  60. /// <returns>The input identifiers.</returns>
  61. protected EGID[] GetInputIds()
  62. {
  63. return SignalEngine.GetSignalInputs(Id);
  64. }
  65. /// <summary>
  66. /// Generates the output port identifiers.
  67. /// </summary>
  68. /// <returns>The output identifiers.</returns>
  69. protected EGID[] GetOutputIds()
  70. {
  71. return SignalEngine.GetSignalOutputs(Id);
  72. }
  73. /// <summary>
  74. /// Gets the port struct.
  75. /// </summary>
  76. /// <returns>The port struct.</returns>
  77. /// <param name="portId">Port identifier.</param>
  78. protected ref PortEntityStruct GetPortStruct(EGID portId)
  79. {
  80. return ref BlockEngine.GetBlockInfo<PortEntityStruct>(portId);
  81. }
  82. /// <summary>
  83. /// Gets the connected wire.
  84. /// </summary>
  85. /// <returns>The connected wire.</returns>
  86. /// <param name="portId">Port identifier.</param>
  87. /// <param name="connected">Whether the port has a wire connected to it.</param>
  88. protected ref WireEntityStruct GetConnectedWire(EGID portId, out bool connected)
  89. {
  90. return ref SignalEngine.MatchPortToWire(portId, Id, out connected);
  91. }
  92. /// <summary>
  93. /// [EXPERIMENTAL] Gets the channel data.
  94. /// </summary>
  95. /// <returns>The channel data.</returns>
  96. /// <param name="portId">Port identifier.</param>
  97. /// <param name="exists">Whether the channel actually exists.</param>
  98. protected ref ChannelDataStruct GetChannelData(EGID portId, out bool exists)
  99. {
  100. return ref SignalEngine.GetChannelDataStruct(portId, out exists);
  101. }
  102. /// <summary>
  103. /// The input port count.
  104. /// </summary>
  105. public uint InputCount
  106. {
  107. get => GetBlockPortsStruct().inputCount;
  108. }
  109. /// <summary>
  110. /// The output port count.
  111. /// </summary>
  112. public uint OutputCount
  113. {
  114. get => GetBlockPortsStruct().outputCount;
  115. }
  116. }
  117. }