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.

159 lines
4.7KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using RobocraftX;
  7. using RobocraftX.Blocks;
  8. using RobocraftX.Blocks.Ghost;
  9. using RobocraftX.Common;
  10. using RobocraftX.Multiplayer;
  11. using RobocraftX.SimulationModeState;
  12. using RobocraftX.UECS;
  13. using Unity.Entities;
  14. using Svelto.Context;
  15. using Svelto.DataStructures;
  16. using Svelto.ECS;
  17. using Svelto.ECS.EntityStructs;
  18. using Unity.Transforms;
  19. using Unity.Mathematics;
  20. using UnityEngine;
  21. using Gamecraft.Wires;
  22. using GamecraftModdingAPI.Utility;
  23. namespace GamecraftModdingAPI.Blocks
  24. {
  25. /// <summary>
  26. /// Engine which executes signal actions
  27. /// </summary>
  28. public class SignalEngine : IApiEngine
  29. {
  30. public string Name { get; } = "GamecraftModdingAPISignalGameEngine";
  31. public IEntitiesDB entitiesDB { set; private get; }
  32. public bool IsInGame = false;
  33. private System.Random random = new System.Random();
  34. public void Dispose()
  35. {
  36. IsInGame = false;
  37. }
  38. public void Ready()
  39. {
  40. IsInGame = true;
  41. }
  42. // implementations for Signal static class
  43. public bool SetSignal(EGID blockID, float signal, out uint signalID, bool input = true)
  44. {
  45. signalID = GetSignalIDs(blockID, input)[0];
  46. return SetSignal(signalID, signal);
  47. }
  48. public bool SetSignal(uint signalID, float signal, bool input = true)
  49. {
  50. ExclusiveGroup group = input ? NamedExclusiveGroup<InputPortsGroup>.Group : NamedExclusiveGroup<OutputPortsGroup>.Group;
  51. if (entitiesDB.Exists<PortEntityStruct>(signalID, group))
  52. {
  53. entitiesDB.QueryEntity<PortEntityStruct>(signalID, group).value = signal;
  54. return true;
  55. }
  56. return false;
  57. }
  58. public float AddSignal(EGID blockID, float signal, out uint signalID, bool clamp = true, bool input = true)
  59. {
  60. signalID = GetSignalIDs(blockID, input)[0];
  61. return AddSignal(signalID, signal, clamp, input);
  62. }
  63. public float AddSignal(uint signalID, float signal, bool clamp = true, bool input = true)
  64. {
  65. ExclusiveGroup group = input ? NamedExclusiveGroup<InputPortsGroup>.Group : NamedExclusiveGroup<OutputPortsGroup>.Group;
  66. if (entitiesDB.Exists<PortEntityStruct>(signalID, group))
  67. {
  68. ref PortEntityStruct pes = ref entitiesDB.QueryEntity<PortEntityStruct>(signalID, group);
  69. pes.value += signal;
  70. if (clamp)
  71. {
  72. if (pes.value > Signals.POSITIVE_HIGH)
  73. {
  74. pes.value = Signals.POSITIVE_HIGH;
  75. }
  76. else if (pes.value < Signals.NEGATIVE_HIGH)
  77. {
  78. pes.value = Signals.NEGATIVE_HIGH;
  79. }
  80. return pes.value;
  81. }
  82. }
  83. return signal;
  84. }
  85. public float GetSignal(EGID blockID, out uint signalID, bool input = true)
  86. {
  87. signalID = GetSignalIDs(blockID, input)[0];
  88. return GetSignal(signalID, input);
  89. }
  90. public float GetSignal(uint signalID, bool input = true)
  91. {
  92. ExclusiveGroup group = input ? NamedExclusiveGroup<InputPortsGroup>.Group : NamedExclusiveGroup<OutputPortsGroup>.Group;
  93. if (entitiesDB.Exists<PortEntityStruct>(signalID, group))
  94. {
  95. return entitiesDB.QueryEntity<PortEntityStruct>(signalID, group).value;
  96. }
  97. return 0f;
  98. }
  99. public uint[] GetSignalIDs(EGID blockID, bool input = true)
  100. {
  101. ref BlockPortsStruct bps = ref entitiesDB.QueryEntity<BlockPortsStruct>(blockID);
  102. uint[] signals;
  103. if (input) {
  104. signals = new uint[bps.inputCount];
  105. for (uint i = 0u; i < bps.inputCount; i++)
  106. {
  107. signals[i] = bps.firstInputID + i;
  108. }
  109. } else {
  110. signals = new uint[bps.outputCount];
  111. for (uint i = 0u; i < bps.outputCount; i++)
  112. {
  113. signals[i] = bps.firstOutputID + i;
  114. }
  115. }
  116. return signals;
  117. }
  118. public EGID[] GetElectricBlocks()
  119. {
  120. uint count = entitiesDB.Count<BlockPortsStruct>(BlockIdentifiers.OWNED_BLOCKS) + entitiesDB.Count<BlockPortsStruct>(BlockIdentifiers.FUNCTIONAL_BLOCK_PARTS);
  121. uint i = 0;
  122. EGID[] res = new EGID[count];
  123. foreach (ref BlockPortsStruct s in entitiesDB.QueryEntities<BlockPortsStruct>(BlockIdentifiers.OWNED_BLOCKS))
  124. {
  125. res[i] = s.ID;
  126. i++;
  127. }
  128. foreach (ref BlockPortsStruct s in entitiesDB.QueryEntities<BlockPortsStruct>(BlockIdentifiers.FUNCTIONAL_BLOCK_PARTS))
  129. {
  130. res[i] = s.ID;
  131. i++;
  132. }
  133. return res;
  134. }
  135. public bool IsSimulationMode()
  136. {
  137. return GamecraftModdingAPI.Utility.GameState.IsSimulationMode();
  138. }
  139. }
  140. }