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.

294 lines
8.8KB

  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 Unity.Mathematics;
  15. using GamecraftModdingAPI.Engines;
  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. var array = coll.ToBuffer().buffer;
  200. for (var index = 0; index < array.capacity; index++)
  201. {
  202. var conn = array[index];
  203. if (conn.clusterId == cid)
  204. bodies.Add(conn.machineRigidBodyId);
  205. }
  206. }
  207. return bodies.Select(id => new SimBody(id, cid)).ToArray();
  208. }
  209. public EGID? FindBlockEGID(uint id)
  210. {
  211. var groups = entitiesDB.FindGroups<DBEntityStruct>();
  212. foreach (ExclusiveGroupStruct group in groups)
  213. {
  214. if (entitiesDB.Exists<DBEntityStruct>(id, group))
  215. return new EGID(id, group);
  216. }
  217. return null;
  218. }
  219. public Cluster GetCluster(uint sbid)
  220. {
  221. var groups = entitiesDB.QueryEntities<GridConnectionsEntityStruct>();
  222. foreach (var (coll, _) in groups)
  223. {
  224. var array = coll.ToBuffer().buffer;
  225. for (var index = 0; index < array.capacity; index++)
  226. {
  227. var conn = array[index];
  228. //Static blocks don't have a cluster ID but the cluster destruction manager should have one
  229. if (conn.machineRigidBodyId == sbid && conn.clusterId != uint.MaxValue)
  230. return new Cluster(conn.clusterId);
  231. }
  232. }
  233. return null;
  234. }
  235. public Block[] GetBodyBlocks(uint sbid)
  236. {
  237. var groups = entitiesDB.QueryEntities<GridConnectionsEntityStruct>();
  238. var set = new HashSet<Block>();
  239. foreach (var (coll, _) in groups)
  240. {
  241. var array = coll.ToBuffer().buffer;
  242. for (var index = 0; index < array.capacity; index++)
  243. {
  244. var conn = array[index];
  245. if (conn.machineRigidBodyId == sbid)
  246. set.Add(new Block(conn.ID));
  247. }
  248. }
  249. return set.ToArray();
  250. }
  251. #if DEBUG
  252. public EntitiesDB GetEntitiesDB()
  253. {
  254. return entitiesDB;
  255. }
  256. #endif
  257. }
  258. }