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.6KB

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