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.

SignalEngine.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. using System;
  2. using Svelto.ECS;
  3. using Svelto.DataStructures;
  4. using Gamecraft.Wires;
  5. using GamecraftModdingAPI.Engines;
  6. namespace GamecraftModdingAPI.Blocks
  7. {
  8. /// <summary>
  9. /// Engine which executes signal actions
  10. /// </summary>
  11. public class SignalEngine : IApiEngine, IFactoryEngine
  12. {
  13. public const float POSITIVE_HIGH = 1.0f;
  14. public const float NEGATIVE_HIGH = -1.0f;
  15. public const float HIGH = 1.0f;
  16. public const float ZERO = 0.0f;
  17. public string Name { get; } = "GamecraftModdingAPISignalGameEngine";
  18. public EntitiesDB entitiesDB { set; private get; }
  19. public IEntityFactory Factory { get; set; }
  20. public bool isRemovable => false;
  21. public bool IsInGame = false;
  22. public void Dispose()
  23. {
  24. IsInGame = false;
  25. }
  26. public void Ready()
  27. {
  28. IsInGame = true;
  29. }
  30. // implementations for block wiring
  31. public WireEntityStruct CreateNewWire(EGID startBlock, byte startPort, EGID endBlock, byte endPort)
  32. {
  33. EGID wireEGID = new EGID(WiresExclusiveGroups.NewWireEntityId, NamedExclusiveGroup<WiresGroup>.Group);
  34. EntityComponentInitializer wireInitializer = Factory.BuildEntity<WireEntityDescriptor>(wireEGID);
  35. wireInitializer.Init(new WireEntityStruct
  36. {
  37. sourceBlockEGID = startBlock,
  38. sourcePortUsage = startPort,
  39. destinationBlockEGID = endBlock,
  40. destinationPortUsage = endPort,
  41. ID = wireEGID
  42. });
  43. return wireInitializer.Get<WireEntityStruct>();
  44. }
  45. public ref WireEntityStruct GetWire(EGID wire)
  46. {
  47. if (!entitiesDB.Exists<WireEntityStruct>(wire))
  48. {
  49. throw new WiringException($"Wire {wire} does not exist");
  50. }
  51. return ref entitiesDB.QueryEntity<WireEntityStruct>(wire);
  52. }
  53. public ref PortEntityStruct GetPort(EGID port)
  54. {
  55. if (!entitiesDB.Exists<PortEntityStruct>(port))
  56. {
  57. throw new WiringException($"Port {port} does not exist (yet?)");
  58. }
  59. return ref entitiesDB.QueryEntity<PortEntityStruct>(port);
  60. }
  61. public ref PortEntityStruct GetPortByOffset(BlockPortsStruct bps, byte portNumber, bool input)
  62. {
  63. ExclusiveGroup group = input
  64. ? NamedExclusiveGroup<InputPortsGroup>.Group
  65. : NamedExclusiveGroup<OutputPortsGroup>.Group;
  66. uint id = (input ? bps.firstInputID : bps.firstOutputID) + portNumber;
  67. EGID egid = new EGID(id, group);
  68. if (!entitiesDB.Exists<PortEntityStruct>(egid))
  69. {
  70. throw new WiringException("Port does not exist");
  71. }
  72. return ref entitiesDB.QueryEntity<PortEntityStruct>(egid);
  73. }
  74. public ref PortEntityStruct GetPortByOffset(Block block, byte portNumber, bool input)
  75. {
  76. BlockPortsStruct bps = GetFromDbOrInitData<BlockPortsStruct>(block, block.Id, out bool exists);
  77. if (!exists)
  78. {
  79. throw new BlockException("Block does not exist");
  80. }
  81. return ref GetPortByOffset(bps, portNumber, input);
  82. }
  83. public ref T GetComponent<T>(EGID egid) where T : struct, IEntityComponent
  84. {
  85. return ref entitiesDB.QueryEntity<T>(egid);
  86. }
  87. public bool Exists<T>(EGID egid) where T : struct, IEntityComponent
  88. {
  89. return entitiesDB.Exists<T>(egid);
  90. }
  91. public bool SetSignal(EGID blockID, float signal, out uint signalID, bool input = true)
  92. {
  93. signalID = GetSignalIDs(blockID, input)[0];
  94. return SetSignal(signalID, signal);
  95. }
  96. public bool SetSignal(uint signalID, float signal, bool input = true)
  97. {
  98. var array = GetSignalStruct(signalID, out uint index, input);
  99. if (array.count > 0) array[index].valueAsFloat = signal;
  100. return false;
  101. }
  102. public float AddSignal(EGID blockID, float signal, out uint signalID, bool clamp = true, bool input = true)
  103. {
  104. signalID = GetSignalIDs(blockID, input)[0];
  105. return AddSignal(signalID, signal, clamp, input);
  106. }
  107. public float AddSignal(uint signalID, float signal, bool clamp = true, bool input = true)
  108. {
  109. var array = GetSignalStruct(signalID, out uint index, input);
  110. if (array.count > 0)
  111. {
  112. ref var channelData = ref array[index];
  113. channelData.valueAsFloat += signal;
  114. if (clamp)
  115. {
  116. if (channelData.valueAsFloat > POSITIVE_HIGH)
  117. {
  118. channelData.valueAsFloat = POSITIVE_HIGH;
  119. }
  120. else if (channelData.valueAsFloat < NEGATIVE_HIGH)
  121. {
  122. channelData.valueAsFloat = NEGATIVE_HIGH;
  123. }
  124. return channelData.valueAsFloat;
  125. }
  126. }
  127. return signal;
  128. }
  129. public float GetSignal(EGID blockID, out uint signalID, bool input = true)
  130. {
  131. signalID = GetSignalIDs(blockID, input)[0];
  132. return GetSignal(signalID, input);
  133. }
  134. public float GetSignal(uint signalID, bool input = true)
  135. {
  136. var array = GetSignalStruct(signalID, out uint index, input);
  137. return array.count > 0 ? array[index].valueAsFloat : 0f;
  138. }
  139. public uint[] GetSignalIDs(EGID blockID, bool input = true)
  140. {
  141. ref BlockPortsStruct bps = ref entitiesDB.QueryEntity<BlockPortsStruct>(blockID);
  142. uint[] signals;
  143. if (input) {
  144. signals = new uint[bps.inputCount];
  145. for (uint i = 0u; i < bps.inputCount; i++)
  146. {
  147. signals[i] = bps.firstInputID + i;
  148. }
  149. } else {
  150. signals = new uint[bps.outputCount];
  151. for (uint i = 0u; i < bps.outputCount; i++)
  152. {
  153. signals[i] = bps.firstOutputID + i;
  154. }
  155. }
  156. return signals;
  157. }
  158. public EGID[] GetSignalInputs(EGID blockID)
  159. {
  160. BlockPortsStruct ports = entitiesDB.QueryEntity<BlockPortsStruct>(blockID);
  161. EGID[] inputs = new EGID[ports.inputCount];
  162. for (uint i = 0; i < ports.inputCount; i++)
  163. {
  164. inputs[i] = new EGID(i + ports.firstInputID, NamedExclusiveGroup<InputPortsGroup>.Group);
  165. }
  166. return inputs;
  167. }
  168. public EGID[] GetSignalOutputs(EGID blockID)
  169. {
  170. BlockPortsStruct ports = entitiesDB.QueryEntity<BlockPortsStruct>(blockID);
  171. EGID[] outputs = new EGID[ports.outputCount];
  172. for (uint i = 0; i < ports.outputCount; i++)
  173. {
  174. outputs[i] = new EGID(i + ports.firstOutputID, NamedExclusiveGroup<OutputPortsGroup>.Group);
  175. }
  176. return outputs;
  177. }
  178. public EGID MatchBlockInputToPort(Block block, byte portUsage, out bool exists)
  179. {
  180. BlockPortsStruct ports = GetFromDbOrInitData<BlockPortsStruct>(block, block.Id, out exists);
  181. return new EGID(ports.firstInputID + portUsage, NamedExclusiveGroup<InputPortsGroup>.Group);
  182. }
  183. public EGID MatchBlockInputToPort(EGID block, byte portUsage, out bool exists)
  184. {
  185. if (!entitiesDB.Exists<BlockPortsStruct>(block))
  186. {
  187. exists = false;
  188. return default;
  189. }
  190. exists = true;
  191. BlockPortsStruct ports = entitiesDB.QueryEntity<BlockPortsStruct>(block);
  192. return new EGID(ports.firstInputID + portUsage, NamedExclusiveGroup<InputPortsGroup>.Group);
  193. }
  194. public EGID MatchBlockOutputToPort(Block block, byte portUsage, out bool exists)
  195. {
  196. BlockPortsStruct ports = GetFromDbOrInitData<BlockPortsStruct>(block, block.Id, out exists);
  197. return new EGID(ports.firstOutputID + portUsage, NamedExclusiveGroup<OutputPortsGroup>.Group);
  198. }
  199. public EGID MatchBlockOutputToPort(EGID block, byte portUsage, out bool exists)
  200. {
  201. if (!entitiesDB.Exists<BlockPortsStruct>(block))
  202. {
  203. exists = false;
  204. return default;
  205. }
  206. exists = true;
  207. BlockPortsStruct ports = entitiesDB.QueryEntity<BlockPortsStruct>(block);
  208. return new EGID(ports.firstOutputID + portUsage, NamedExclusiveGroup<OutputPortsGroup>.Group);
  209. }
  210. public ref WireEntityStruct MatchPortToWire(EGID portID, EGID blockID, out bool exists)
  211. {
  212. ref PortEntityStruct port = ref entitiesDB.QueryEntity<PortEntityStruct>(portID);
  213. var wires = entitiesDB.QueryEntities<WireEntityStruct>(NamedExclusiveGroup<WiresGroup>.Group);
  214. for (uint i = 0; i < wires.count; i++)
  215. {
  216. if ((wires[i].destinationPortUsage == port.usage && wires[i].destinationBlockEGID == blockID)
  217. || (wires[i].sourcePortUsage == port.usage && wires[i].sourceBlockEGID == blockID))
  218. {
  219. exists = true;
  220. return ref wires[i];
  221. }
  222. }
  223. exists = false;
  224. WireEntityStruct[] defRef = new WireEntityStruct[1];
  225. return ref defRef[0];
  226. }
  227. public ref WireEntityStruct MatchBlocksToWire(EGID startBlock, EGID endBlock, out bool exists, byte startPort = byte.MaxValue,
  228. byte endPort = byte.MaxValue)
  229. {
  230. EGID[] startPorts;
  231. if (startPort == byte.MaxValue)
  232. {
  233. // search all output ports on source block
  234. startPorts = GetSignalOutputs(startBlock);
  235. }
  236. else
  237. {
  238. BlockPortsStruct ports = entitiesDB.QueryEntity<BlockPortsStruct>(startBlock);
  239. startPorts = new EGID[] {new EGID(ports.firstOutputID + startPort, NamedExclusiveGroup<OutputPortsGroup>.Group) };
  240. }
  241. EGID[] endPorts;
  242. if (startPort == byte.MaxValue)
  243. {
  244. // search all input ports on destination block
  245. endPorts = GetSignalInputs(endBlock);
  246. }
  247. else
  248. {
  249. BlockPortsStruct ports = entitiesDB.QueryEntity<BlockPortsStruct>(endBlock);
  250. endPorts = new EGID[] {new EGID(ports.firstInputID + endPort, NamedExclusiveGroup<InputPortsGroup>.Group) };
  251. }
  252. EntityCollection<WireEntityStruct> wires = entitiesDB.QueryEntities<WireEntityStruct>(NamedExclusiveGroup<WiresGroup>.Group);
  253. for (int endIndex = 0; endIndex < endPorts.Length; endIndex++)
  254. {
  255. PortEntityStruct endPES = entitiesDB.QueryEntity<PortEntityStruct>(endPorts[endIndex]);
  256. for (int startIndex = 0; startIndex < startPorts.Length; startIndex++)
  257. {
  258. PortEntityStruct startPES = entitiesDB.QueryEntity<PortEntityStruct>(startPorts[startIndex]);
  259. for (int w = 0; w < wires.count; w++)
  260. {
  261. if ((wires[w].destinationPortUsage == endPES.usage && wires[w].destinationBlockEGID == endBlock)
  262. && (wires[w].sourcePortUsage == startPES.usage && wires[w].sourceBlockEGID == startBlock))
  263. {
  264. exists = true;
  265. return ref wires[w];
  266. }
  267. }
  268. }
  269. }
  270. exists = false;
  271. WireEntityStruct[] defRef = new WireEntityStruct[1];
  272. return ref defRef[0];
  273. }
  274. public ref ChannelDataStruct GetChannelDataStruct(EGID portID, out bool exists)
  275. {
  276. ref PortEntityStruct port = ref entitiesDB.QueryEntity<PortEntityStruct>(portID);
  277. var channels = entitiesDB.QueryEntities<ChannelDataStruct>(NamedExclusiveGroup<ChannelDataGroup>.Group);
  278. if (port.firstChannelIndexCachedInSim < channels.count)
  279. {
  280. exists = true;
  281. return ref channels[port.firstChannelIndexCachedInSim];
  282. }
  283. exists = false;
  284. ChannelDataStruct[] defRef = new ChannelDataStruct[1];
  285. return ref defRef[0];
  286. }
  287. public EGID[] GetElectricBlocks()
  288. {
  289. var res = new FasterList<EGID>();
  290. foreach (var (coll, _) in entitiesDB.QueryEntities<BlockPortsStruct>())
  291. foreach (ref BlockPortsStruct s in coll)
  292. res.Add(s.ID);
  293. return res.ToArray();
  294. }
  295. public EGID[] WiredToInput(EGID block, byte port)
  296. {
  297. WireEntityStruct[] wireEntityStructs = Search(NamedExclusiveGroup<WiresGroup>.Group,
  298. (WireEntityStruct wes) => wes.destinationPortUsage == port && wes.destinationBlockEGID == block);
  299. EGID[] result = new EGID[wireEntityStructs.Length];
  300. for (uint i = 0; i < wireEntityStructs.Length; i++)
  301. {
  302. result[i] = wireEntityStructs[i].ID;
  303. }
  304. return result;
  305. }
  306. public EGID[] WiredToOutput(EGID block, byte port)
  307. {
  308. WireEntityStruct[] wireEntityStructs = Search(NamedExclusiveGroup<WiresGroup>.Group,
  309. (WireEntityStruct wes) => wes.sourcePortUsage == port && wes.sourceBlockEGID == block);
  310. EGID[] result = new EGID[wireEntityStructs.Length];
  311. for (uint i = 0; i < wireEntityStructs.Length; i++)
  312. {
  313. result[i] = wireEntityStructs[i].ID;
  314. }
  315. return result;
  316. }
  317. private T[] Search<T>(ExclusiveGroup group, Func<T, bool> isMatch) where T : struct, IEntityComponent
  318. {
  319. FasterList<T> results = new FasterList<T>();
  320. EntityCollection<T> components = entitiesDB.QueryEntities<T>(group);
  321. for (uint i = 0; i < components.count; i++)
  322. {
  323. if (isMatch(components[i]))
  324. {
  325. results.Add(components[i]);
  326. }
  327. }
  328. return results.ToArray();
  329. }
  330. private ref T GetFromDbOrInitData<T>(Block block, EGID id, out bool exists) where T : struct, IEntityComponent
  331. {
  332. T[] defRef = new T[1];
  333. if (entitiesDB.Exists<T>(id))
  334. {
  335. exists = true;
  336. return ref entitiesDB.QueryEntity<T>(id);
  337. }
  338. if (block == null || block.InitData.Group == null)
  339. {
  340. exists = false;
  341. return ref defRef[0];
  342. }
  343. EntityComponentInitializer initializer = new EntityComponentInitializer(block.Id, block.InitData.Group);
  344. if (initializer.Has<T>())
  345. {
  346. exists = true;
  347. return ref initializer.Get<T>();
  348. }
  349. exists = false;
  350. return ref defRef[0];
  351. }
  352. private EntityCollection<ChannelDataStruct> GetSignalStruct(uint signalID, out uint index, bool input = true)
  353. {
  354. ExclusiveGroup group = input
  355. ? NamedExclusiveGroup<InputPortsGroup>.Group
  356. : NamedExclusiveGroup<OutputPortsGroup>.Group;
  357. if (entitiesDB.Exists<PortEntityStruct>(signalID, group))
  358. {
  359. index = entitiesDB.QueryEntity<PortEntityStruct>(signalID, group).anyChannelIndex;
  360. var channelData =
  361. entitiesDB.QueryEntities<ChannelDataStruct>(NamedExclusiveGroup<ChannelDataGroup>.Group);
  362. return channelData;
  363. }
  364. index = 0;
  365. return default; //count: 0
  366. }
  367. }
  368. }