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.

152 lines
4.6KB

  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 EntitiesDB entitiesDB { set; private get; }
  32. public bool IsInGame = false;
  33. public void Dispose()
  34. {
  35. IsInGame = false;
  36. }
  37. public void Ready()
  38. {
  39. IsInGame = true;
  40. }
  41. // implementations for Signal static class
  42. public bool SetSignal(EGID blockID, float signal, out uint signalID, bool input = true)
  43. {
  44. signalID = GetSignalIDs(blockID, input)[0];
  45. return SetSignal(signalID, signal);
  46. }
  47. public bool SetSignal(uint signalID, float signal, bool input = true)
  48. {
  49. ExclusiveGroup group = input ? NamedExclusiveGroup<InputPortsGroup>.Group : NamedExclusiveGroup<OutputPortsGroup>.Group;
  50. if (entitiesDB.Exists<PortEntityStruct>(signalID, group))
  51. {
  52. entitiesDB.QueryEntity<PortEntityStruct>(signalID, group).anyChannel.valueAsFloat = signal;
  53. return true;
  54. }
  55. return false;
  56. }
  57. public float AddSignal(EGID blockID, float signal, out uint signalID, bool clamp = true, bool input = true)
  58. {
  59. signalID = GetSignalIDs(blockID, input)[0];
  60. return AddSignal(signalID, signal, clamp, input);
  61. }
  62. public float AddSignal(uint signalID, float signal, bool clamp = true, bool input = true)
  63. {
  64. ExclusiveGroup group = input ? NamedExclusiveGroup<InputPortsGroup>.Group : NamedExclusiveGroup<OutputPortsGroup>.Group;
  65. if (entitiesDB.Exists<PortEntityStruct>(signalID, group))
  66. {
  67. ref PortEntityStruct pes = ref entitiesDB.QueryEntity<PortEntityStruct>(signalID, group);
  68. pes.anyChannel.valueAsFloat += signal;
  69. if (clamp)
  70. {
  71. if (pes.anyChannel.valueAsFloat > Signals.POSITIVE_HIGH)
  72. {
  73. pes.anyChannel.valueAsFloat = Signals.POSITIVE_HIGH;
  74. }
  75. else if (pes.anyChannel.valueAsFloat < Signals.NEGATIVE_HIGH)
  76. {
  77. pes.anyChannel.valueAsFloat = Signals.NEGATIVE_HIGH;
  78. }
  79. return pes.anyChannel.valueAsFloat;
  80. }
  81. }
  82. return signal;
  83. }
  84. public float GetSignal(EGID blockID, out uint signalID, bool input = true)
  85. {
  86. signalID = GetSignalIDs(blockID, input)[0];
  87. return GetSignal(signalID, input);
  88. }
  89. public float GetSignal(uint signalID, bool input = true)
  90. {
  91. ExclusiveGroup group = input ? NamedExclusiveGroup<InputPortsGroup>.Group : NamedExclusiveGroup<OutputPortsGroup>.Group;
  92. if (entitiesDB.Exists<PortEntityStruct>(signalID, group))
  93. {
  94. return entitiesDB.QueryEntity<PortEntityStruct>(signalID, group).anyChannel.valueAsFloat;
  95. }
  96. return 0f;
  97. }
  98. public uint[] GetSignalIDs(EGID blockID, bool input = true)
  99. {
  100. ref BlockPortsStruct bps = ref entitiesDB.QueryEntity<BlockPortsStruct>(blockID);
  101. uint[] signals;
  102. if (input) {
  103. signals = new uint[bps.inputCount];
  104. for (uint i = 0u; i < bps.inputCount; i++)
  105. {
  106. signals[i] = bps.firstInputID + i;
  107. }
  108. } else {
  109. signals = new uint[bps.outputCount];
  110. for (uint i = 0u; i < bps.outputCount; i++)
  111. {
  112. signals[i] = bps.firstOutputID + i;
  113. }
  114. }
  115. return signals;
  116. }
  117. public EGID[] GetElectricBlocks()
  118. {
  119. uint count = entitiesDB.Count<BlockPortsStruct>(BlockIdentifiers.OWNED_BLOCKS) + entitiesDB.Count<BlockPortsStruct>(BlockIdentifiers.FUNCTIONAL_BLOCK_PARTS);
  120. uint i = 0;
  121. EGID[] res = new EGID[count];
  122. foreach (ref BlockPortsStruct s in entitiesDB.QueryEntities<BlockPortsStruct>(BlockIdentifiers.OWNED_BLOCKS))
  123. {
  124. res[i] = s.ID;
  125. i++;
  126. }
  127. foreach (ref BlockPortsStruct s in entitiesDB.QueryEntities<BlockPortsStruct>(BlockIdentifiers.FUNCTIONAL_BLOCK_PARTS))
  128. {
  129. res[i] = s.ID;
  130. i++;
  131. }
  132. return res;
  133. }
  134. }
  135. }