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.

BlockEngine.cs 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Gamecraft.ColourPalette;
  5. using Gamecraft.TimeRunning;
  6. using Gamecraft.Wires;
  7. using RobocraftX.Blocks;
  8. using RobocraftX.Common;
  9. using RobocraftX.Physics;
  10. using RobocraftX.Scene.Simulation;
  11. using Svelto.DataStructures;
  12. using Svelto.ECS;
  13. using Svelto.ECS.Hybrid;
  14. using GamecraftModdingAPI.Engines;
  15. using Unity.Mathematics;
  16. namespace GamecraftModdingAPI.Blocks
  17. {
  18. /// <summary>
  19. /// Engine for executing general block actions
  20. /// </summary>
  21. public partial class BlockEngine : IApiEngine
  22. {
  23. public string Name { get; } = "GamecraftModdingAPIBlockGameEngine";
  24. public EntitiesDB entitiesDB { set; private get; }
  25. public bool isRemovable => false;
  26. public void Dispose()
  27. {
  28. }
  29. public void Ready()
  30. {
  31. }
  32. public Block[] GetConnectedBlocks(EGID blockID)
  33. {
  34. if (!BlockExists(blockID)) return new Block[0];
  35. Stack<EGID> cubeStack = new Stack<EGID>();
  36. FasterList<EGID> cubes = new FasterList<EGID>(10);
  37. var coll = entitiesDB.QueryEntities<GridConnectionsEntityStruct>();
  38. foreach (var (ecoll, _) in coll)
  39. {
  40. var ecollB = ecoll.ToBuffer();
  41. for(int i = 0; i < ecoll.count; i++)
  42. {
  43. ref var conn = ref ecollB.buffer[i];
  44. conn.isProcessed = false;
  45. }
  46. }
  47. ConnectedCubesUtility.TreeTraversal.GetConnectedCubes(entitiesDB, blockID, cubeStack, cubes,
  48. (in GridConnectionsEntityStruct g) => { return false; });
  49. var ret = new Block[cubes.count];
  50. for (int i = 0; i < cubes.count; i++)
  51. ret[i] = new Block(cubes[i]);
  52. return ret;
  53. }
  54. public float4 ConvertBlockColor(byte index) => index == byte.MaxValue
  55. ? new float4(-1f, -1f, -1f, -1f)
  56. : entitiesDB.QueryEntity<PaletteEntryEntityStruct>(index,
  57. CommonExclusiveGroups.COLOUR_PALETTE_GROUP).Colour;
  58. public ref T GetBlockInfo<T>(EGID blockID) where T : unmanaged, IEntityComponent
  59. {
  60. if (entitiesDB.Exists<T>(blockID))
  61. return ref entitiesDB.QueryEntity<T>(blockID);
  62. T[] structHolder = new T[1]; //Create something that can be referenced
  63. return ref structHolder[0]; //Gets a default value automatically
  64. }
  65. public ref T GetBlockInfoViewStruct<T>(EGID blockID) where T : struct, INeedEGID, IEntityViewComponent
  66. {
  67. if (entitiesDB.Exists<T>(blockID))
  68. {
  69. // TODO: optimize by using EntitiesDB internal calls instead of iterating over everything
  70. BT<MB<T>> entities = entitiesDB.QueryEntities<T>(blockID.groupID).ToBuffer();
  71. for (int i = 0; i < entities.count; i++)
  72. {
  73. if (entities.buffer[i].ID == blockID)
  74. {
  75. return ref entities.buffer[i];
  76. }
  77. }
  78. }
  79. T[] structHolder = new T[1]; //Create something that can be referenced
  80. return ref structHolder[0]; //Gets a default value automatically
  81. }
  82. public U GetBlockInfo<T, U>(Block block, Func<T, U> getter,
  83. U def = default) where T : unmanaged, IEntityComponent
  84. {
  85. if (entitiesDB.Exists<T>(block.Id))
  86. return getter(entitiesDB.QueryEntity<T>(block.Id));
  87. return GetBlockInitInfo(block, getter, def);
  88. }
  89. public U GetBlockInfoViewStruct<T, U>(Block block, Func<T, U> getter,
  90. U def = default) where T : struct, IEntityViewComponent
  91. {
  92. if (entitiesDB.Exists<T>(block.Id))
  93. return getter(entitiesDB.QueryEntity<T>(block.Id));
  94. return GetBlockInitInfo(block, getter, def);
  95. }
  96. private U GetBlockInitInfo<T, U>(Block block, Func<T, U> getter, U def) where T : struct, IEntityComponent
  97. {
  98. if (block.InitData.Group == null) return def;
  99. var initializer = new EntityComponentInitializer(block.Id, block.InitData.Group);
  100. if (initializer.Has<T>())
  101. return getter(initializer.Get<T>());
  102. return def;
  103. }
  104. public delegate void Setter<T, U>(ref T component, U value) where T : struct, IEntityComponent;
  105. public void SetBlockInfoViewStruct<T, U>(Block block, Setter<T, U> setter, U value) where T : struct, IEntityViewComponent
  106. {
  107. if (entitiesDB.Exists<T>(block.Id))
  108. setter(ref entitiesDB.QueryEntity<T>(block.Id), value);
  109. else
  110. SetBlockInitInfo(block, setter, value);
  111. }
  112. public void SetBlockInfo<T, U>(Block block, Setter<T, U> setter, U value) where T : unmanaged, IEntityComponent
  113. {
  114. if (entitiesDB.Exists<T>(block.Id))
  115. setter(ref entitiesDB.QueryEntity<T>(block.Id), value);
  116. else
  117. SetBlockInitInfo(block, setter, value);
  118. }
  119. private void SetBlockInitInfo<T, U>(Block block, Setter<T, U> setter, U value)
  120. where T : struct, IEntityComponent
  121. {
  122. if (block.InitData.Group != null)
  123. {
  124. var initializer = new EntityComponentInitializer(block.Id, block.InitData.Group);
  125. T component = initializer.Has<T>() ? initializer.Get<T>() : default;
  126. ref T structRef = ref component;
  127. setter(ref structRef, value);
  128. initializer.Init(structRef);
  129. }
  130. }
  131. public bool BlockExists(EGID blockID)
  132. {
  133. return entitiesDB.Exists<DBEntityStruct>(blockID);
  134. }
  135. public bool GetBlockInfoExists<T>(Block block) where T : struct, IEntityComponent
  136. {
  137. if (entitiesDB.Exists<T>(block.Id))
  138. return true;
  139. if (block.InitData.Group == null)
  140. return false;
  141. var init = new EntityComponentInitializer(block.Id, block.InitData.Group);
  142. return init.Has<T>();
  143. }
  144. public SimBody[] GetSimBodiesFromID(byte id)
  145. {
  146. var ret = new FasterList<SimBody>(4);
  147. if (!entitiesDB.HasAny<ObjectIdEntityStruct>(CommonExclusiveGroups.OBJID_BLOCK_GROUP))
  148. return new SimBody[0];
  149. var oids = entitiesDB.QueryEntities<ObjectIdEntityStruct>(CommonExclusiveGroups.OBJID_BLOCK_GROUP).ToBuffer();
  150. var connections = entitiesDB.QueryMappedEntities<GridConnectionsEntityStruct>(CommonExclusiveGroups.OBJID_BLOCK_GROUP);
  151. for (int i = 0; i < oids.count; i++)
  152. {
  153. ref ObjectIdEntityStruct oid = ref oids.buffer[i];
  154. if (oid.objectId != id) continue;
  155. var rid = connections.Entity(oid.ID.entityID).machineRigidBodyId;
  156. foreach (var rb in ret)
  157. {
  158. if (rb.Id.entityID == rid)
  159. goto DUPLICATE; //Multiple Object Identifiers on one rigid body
  160. }
  161. ret.Add(new SimBody(rid));
  162. DUPLICATE: ;
  163. }
  164. return ret.ToArray();
  165. }
  166. public ObjectIdentifier[] GetObjectIDsFromID(byte id, bool sim)
  167. {
  168. var ret = new FasterList<ObjectIdentifier>(4);
  169. if (!entitiesDB.HasAny<ObjectIdEntityStruct>(CommonExclusiveGroups.OBJID_BLOCK_GROUP))
  170. return new ObjectIdentifier[0];
  171. var oids = entitiesDB.QueryEntities<ObjectIdEntityStruct>(CommonExclusiveGroups.OBJID_BLOCK_GROUP).ToBuffer();
  172. for (int i = 0; i < oids.count; i++)
  173. {
  174. ref ObjectIdEntityStruct oid = ref oids.buffer[i];
  175. if (sim ? oid.simObjectId == id : oid.objectId == id)
  176. ret.Add(new ObjectIdentifier(oid.ID));
  177. }
  178. return ret.ToArray();
  179. }
  180. public SimBody[] GetConnectedSimBodies(uint id)
  181. {
  182. var joints = entitiesDB.QueryEntities<JointEntityStruct>(MachineSimulationGroups.JOINTS_GROUP).ToBuffer();
  183. var list = new FasterList<SimBody>(4);
  184. for (int i = 0; i < joints.count; i++)
  185. {
  186. ref var joint = ref joints.buffer[i];
  187. if (joint.jointState == JointState.Broken) continue;
  188. if (joint.connectedEntityA == id) list.Add(new SimBody(joint.connectedEntityB));
  189. else if (joint.connectedEntityB == id) list.Add(new SimBody(joint.connectedEntityA));
  190. }
  191. return list.ToArray();
  192. }
  193. public SimBody[] GetClusterBodies(uint cid)
  194. {
  195. var groups = entitiesDB.QueryEntities<GridConnectionsEntityStruct>();
  196. var bodies = new HashSet<uint>();
  197. foreach (var (coll, _) in groups)
  198. {
  199. foreach (var conn in coll.ToBuffer().buffer.ToManagedArray())
  200. {
  201. if (conn.clusterId == cid)
  202. bodies.Add(conn.machineRigidBodyId);
  203. }
  204. }
  205. return bodies.Select(id => new SimBody(id, cid)).ToArray();
  206. }
  207. public EGID? FindBlockEGID(uint id)
  208. {
  209. var groups = entitiesDB.FindGroups<DBEntityStruct>();
  210. foreach (ExclusiveGroupStruct group in groups)
  211. {
  212. if (entitiesDB.Exists<DBEntityStruct>(id, group))
  213. return new EGID(id, group);
  214. }
  215. return null;
  216. }
  217. public Cluster GetCluster(uint sbid)
  218. {
  219. var groups = entitiesDB.QueryEntities<GridConnectionsEntityStruct>();
  220. foreach (var (coll, _) in groups)
  221. {
  222. foreach (var conn in coll.ToBuffer().buffer.ToManagedArray())
  223. { //Static blocks don't have a cluster ID but the cluster destruction manager should have one
  224. if (conn.machineRigidBodyId == sbid && conn.clusterId != uint.MaxValue)
  225. return new Cluster(conn.clusterId);
  226. }
  227. }
  228. return null;
  229. }
  230. public Block[] GetBodyBlocks(uint sbid)
  231. {
  232. var groups = entitiesDB.QueryEntities<GridConnectionsEntityStruct>();
  233. var set = new HashSet<Block>();
  234. foreach (var (coll, _) in groups)
  235. {
  236. foreach (var conn in coll.ToBuffer().buffer.ToManagedArray())
  237. {
  238. if (conn.machineRigidBodyId == sbid)
  239. set.Add(new Block(conn.ID));
  240. }
  241. }
  242. return set.ToArray();
  243. }
  244. #if DEBUG
  245. public EntitiesDB GetEntitiesDB()
  246. {
  247. return entitiesDB;
  248. }
  249. #endif
  250. }
  251. }