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.

Signals.cs 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using Svelto.ECS;
  2. using GamecraftModdingAPI.Utility;
  3. namespace GamecraftModdingAPI.Blocks
  4. {
  5. /// <summary>
  6. /// [EXPERIMENTAL] Common block signal operations
  7. /// The functionality in this class only works when in a game.
  8. /// </summary>
  9. public static class Signals
  10. {
  11. // Signal constants
  12. public static readonly float HIGH = 1.0f;
  13. public static readonly float POSITIVE_HIGH = HIGH;
  14. public static readonly float NEGATIVE_HIGH = -1.0f;
  15. public static readonly float LOW = 0.0f;
  16. private static SignalEngine signalEngine = new SignalEngine();
  17. /// <summary>
  18. /// Set the electric block's (first) signal value.
  19. /// </summary>
  20. /// <param name="blockID">The block's id.</param>
  21. /// <param name="signal">The signal value (-1 to 1; not enforced).</param>
  22. /// <param name="input">Whether to retrieve input IDs (true) or output IDs (false).</param>
  23. /// <param name="owned">Whether the block is in the owned group (true) or functional group (false)</param>
  24. public static void SetSignalByBlock(uint blockID, float signal, bool input = true, bool owned = true)
  25. {
  26. EGID egid = new EGID(blockID, owned ? BlockIdentifiers.OWNED_BLOCKS : BlockIdentifiers.FUNCTIONAL_BLOCK_PARTS);
  27. if (signalEngine.IsInGame && GameState.IsSimulationMode())
  28. {
  29. signalEngine.SetSignal(egid, signal, out uint _, input);
  30. }
  31. }
  32. public static void SetSignalByBlock(EGID blockID, float signal, bool input = true)
  33. {
  34. if (signalEngine.IsInGame && GameState.IsSimulationMode())
  35. {
  36. signalEngine.SetSignal(blockID, signal, out uint _, input);
  37. }
  38. }
  39. /// <summary>
  40. /// Set the signal's value.
  41. /// </summary>
  42. /// <param name="signalID">The channel cluster's id.</param>
  43. /// <param name="signal">The signal value (-1 to 1; not enforced).</param>
  44. /// <param name="input">Whether to retrieve input IDs (true) or output IDs (false).</param>
  45. public static void SetSignalByID(uint signalID, float signal, bool input = true)
  46. {
  47. if (signalEngine.IsInGame && GamecraftModdingAPI.Utility.GameState.IsSimulationMode())
  48. {
  49. signalEngine.SetSignal(signalID, signal, input);
  50. }
  51. }
  52. /// <summary>
  53. /// Add a value to an electric block's signal.
  54. /// </summary>
  55. /// <param name="blockID">The block's id.</param>
  56. /// <param name="signal">The signal value to add.</param>
  57. /// <param name="clamp">Whether to clamp the resulting signal value between -1 and 1.</param>
  58. /// <param name="input">Whether to retrieve input IDs (true) or output IDs (false).</param>
  59. /// <param name="owned">Whether the block is in the owned group (true) or functional group (false)</param>
  60. /// <returns>The signal's new value.</returns>
  61. public static float AddSignalByBlock(uint blockID, float signal, bool clamp = true, bool input = true, bool owned = true)
  62. {
  63. EGID egid = new EGID(blockID, owned ? BlockIdentifiers.OWNED_BLOCKS : BlockIdentifiers.FUNCTIONAL_BLOCK_PARTS);
  64. if (signalEngine.IsInGame && GamecraftModdingAPI.Utility.GameState.IsSimulationMode())
  65. {
  66. return signalEngine.AddSignal(egid, signal, out uint _, clamp, input);
  67. }
  68. return 0f;
  69. }
  70. public static float AddSignalByBlock(EGID blockID, float signal, bool clamp = true, bool input = true)
  71. {
  72. if (signalEngine.IsInGame && GamecraftModdingAPI.Utility.GameState.IsSimulationMode())
  73. {
  74. return signalEngine.AddSignal(blockID, signal, out uint _, clamp, input);
  75. }
  76. return 0f;
  77. }
  78. /// <summary>
  79. /// Add a value to a conductive cluster channel.
  80. /// </summary>
  81. /// <param name="signalID">The channel cluster's id.</param>
  82. /// <param name="signal">The signal value to add.</param>
  83. /// <param name="clamp">Whether to clamp the resulting signal value between -1 and 1.</param>
  84. /// <param name="input">Whether to retrieve input IDs (true) or output IDs (false).</param>
  85. /// <returns>The signal's new value.</returns>
  86. public static float AddSignalByID(uint signalID, float signal, bool clamp = true, bool input = true)
  87. {
  88. if (signalEngine.IsInGame && GamecraftModdingAPI.Utility.GameState.IsSimulationMode())
  89. {
  90. return signalEngine.AddSignal(signalID, signal, clamp, input);
  91. }
  92. return 0f;
  93. }
  94. /// <summary>
  95. /// Get a electric block's signal's (first) value.
  96. /// </summary>
  97. /// <param name="blockID">The block's id.</param>
  98. /// <param name="input">Whether to retrieve input IDs (true) or output IDs (false).</param>
  99. /// <param name="owned">Whether the block is in the owned group (true) or functional group (false)</param>
  100. /// <returns>The signal's value.</returns>
  101. public static float GetSignalByBlock(uint blockID, bool input = true, bool owned = true)
  102. {
  103. EGID egid = new EGID(blockID, owned? BlockIdentifiers.OWNED_BLOCKS : BlockIdentifiers.FUNCTIONAL_BLOCK_PARTS);
  104. if (signalEngine.IsInGame && GamecraftModdingAPI.Utility.GameState.IsSimulationMode())
  105. {
  106. return signalEngine.GetSignal(egid, out uint _, input);
  107. }
  108. return 0f;
  109. }
  110. public static float GetSignalByBlock(EGID blockID, bool input = true)
  111. {
  112. if (signalEngine.IsInGame && GamecraftModdingAPI.Utility.GameState.IsSimulationMode())
  113. {
  114. return signalEngine.GetSignal(blockID, out uint _, input);
  115. }
  116. return 0f;
  117. }
  118. /// <summary>
  119. /// Get a signal's value.
  120. /// </summary>
  121. /// <param name="signalID">The signal's id.</param>
  122. /// <param name="input">Whether to retrieve input IDs (true) or output IDs (false).</param>
  123. /// <returns>The signal's value.</returns>
  124. public static float GetSignalByID(uint signalID, bool input = true)
  125. {
  126. if (signalEngine.IsInGame && GamecraftModdingAPI.Utility.GameState.IsSimulationMode())
  127. {
  128. return signalEngine.GetSignal(signalID, input);
  129. }
  130. return 0f;
  131. }
  132. /// <summary>
  133. /// Get the ID of every electric block in the game world.
  134. /// </summary>
  135. /// <returns>The block IDs.</returns>
  136. public static EGID[] GetElectricBlocks()
  137. {
  138. return signalEngine.GetElectricBlocks();
  139. }
  140. /// <summary>
  141. /// Get the unique identifiers for the input wires connected to an electric block.
  142. /// </summary>
  143. /// <param name="blockID">The block's id.</param>
  144. /// <param name="input">Whether to retrieve input IDs (true) or output IDs (false).</param>
  145. /// <param name="owned">Whether the block is in the owned group (true) or functional group (false)</param>
  146. /// <returns>The unique IDs.</returns>
  147. public static uint[] GetSignalIDs(uint blockID, bool input = true, bool owned = true)
  148. {
  149. EGID egid = new EGID(blockID, owned ? BlockIdentifiers.OWNED_BLOCKS : BlockIdentifiers.FUNCTIONAL_BLOCK_PARTS);
  150. return signalEngine.GetSignalIDs(egid, input);
  151. }
  152. public static uint[] GetSignalIDs(EGID blockID, bool input = true, bool owned = true)
  153. {
  154. return signalEngine.GetSignalIDs(blockID, input);
  155. }
  156. public static void Init()
  157. {
  158. GameEngineManager.AddGameEngine(signalEngine);
  159. }
  160. }
  161. }