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.

247 lines
7.0KB

  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.Rendering;
  11. using RobocraftX.Rendering.GPUI;
  12. using Svelto.ECS.EntityStructs;
  13. using Svelto.DataStructures;
  14. using Svelto.ECS;
  15. using Svelto.ECS.Hybrid;
  16. using Unity.Mathematics;
  17. using TechbloxModdingAPI.Engines;
  18. using TechbloxModdingAPI.Utility;
  19. namespace TechbloxModdingAPI.Blocks
  20. {
  21. /// <summary>
  22. /// Engine for executing general block actions
  23. /// </summary>
  24. public partial class BlockEngine : IApiEngine
  25. {
  26. public string Name { get; } = "TechbloxModdingAPIBlockGameEngine";
  27. public EntitiesDB entitiesDB { set; private get; }
  28. public bool isRemovable => false;
  29. public void Dispose()
  30. {
  31. }
  32. public void Ready()
  33. {
  34. }
  35. public Block[] GetConnectedBlocks(EGID blockID)
  36. {
  37. if (!BlockExists(blockID)) return new Block[0];
  38. Stack<EGID> cubeStack = new Stack<EGID>();
  39. FasterList<EGID> cubes = new FasterList<EGID>(10);
  40. var coll = entitiesDB.QueryEntities<GridConnectionsEntityStruct>();
  41. foreach (var (ecoll, _) in coll)
  42. {
  43. var ecollB = ecoll.ToBuffer();
  44. for(int i = 0; i < ecoll.count; i++)
  45. {
  46. ref var conn = ref ecollB.buffer[i];
  47. conn.isProcessed = false;
  48. }
  49. }
  50. ConnectedCubesUtility.TreeTraversal.GetConnectedCubes(entitiesDB, blockID, cubeStack, cubes,
  51. (in GridConnectionsEntityStruct g) => { return false; });
  52. var ret = new Block[cubes.count];
  53. for (int i = 0; i < cubes.count; i++)
  54. ret[i] = new Block(cubes[i]);
  55. return ret;
  56. }
  57. public float4 ConvertBlockColor(byte index) => index == byte.MaxValue
  58. ? new float4(-1f, -1f, -1f, -1f)
  59. : entitiesDB.QueryEntity<PaletteEntryEntityStruct>(index,
  60. CommonExclusiveGroups.COLOUR_PALETTE_GROUP).Colour;
  61. public OptionalRef<T> GetBlockInfoOptional<T>(Block block) where T : unmanaged, IEntityComponent
  62. {
  63. return entitiesDB.QueryEntityOptional<T>(block);
  64. }
  65. public ref T GetBlockInfo<T>(Block block) where T : unmanaged, IEntityComponent
  66. {
  67. return ref entitiesDB.QueryEntityOrDefault<T>(block);
  68. }
  69. internal ref T GetBlockInfo<T>(EcsObjectBase obj) where T : unmanaged, IEntityComponent
  70. {
  71. return ref entitiesDB.QueryEntityOrDefault<T>(obj);
  72. }
  73. public ref T GetBlockInfoViewComponent<T>(Block block) where T : struct, IEntityViewComponent
  74. {
  75. return ref entitiesDB.QueryEntityOrDefault<T>(block);
  76. }
  77. public void UpdateDisplayedBlock(EGID id)
  78. {
  79. if (!BlockExists(id)) return;
  80. var pos = entitiesDB.QueryEntity<PositionEntityStruct>(id);
  81. var rot = entitiesDB.QueryEntity<RotationEntityStruct>(id);
  82. var scale = entitiesDB.QueryEntity<ScalingEntityStruct>(id);
  83. entitiesDB.QueryEntity<RenderingDataStruct>(id).matrix = float4x4.TRS(pos.position, rot.rotation, scale.scale);
  84. }
  85. internal void UpdatePrefab(Block block, ushort type, byte material, bool flipped)
  86. {
  87. uint pid = PrefabsID.GetOrCreatePrefabID(type, material, 0, flipped);
  88. entitiesDB.QueryEntityOrDefault<GFXPrefabEntityStructGPUI>(block).prefabID = pid;
  89. entitiesDB.QueryEntityOrDefault<PhysicsPrefabEntityStruct>(block) = new PhysicsPrefabEntityStruct(pid);
  90. }
  91. public bool BlockExists(EGID blockID)
  92. {
  93. return entitiesDB.Exists<DBEntityStruct>(blockID);
  94. }
  95. public SimBody[] GetSimBodiesFromID(byte id)
  96. {
  97. var ret = new FasterList<SimBody>(4);
  98. var oide = entitiesDB.QueryEntities<ObjectIdEntityStruct>();
  99. EGIDMapper<GridConnectionsEntityStruct>? connections = null;
  100. foreach (var ((oids, count), _) in oide)
  101. {
  102. for (int i = 0; i < count; i++)
  103. {
  104. ref ObjectIdEntityStruct oid = ref oids[i];
  105. if (oid.objectId != id) continue;
  106. if (!connections.HasValue) //Would need reflection to get the group from the build group otherwise
  107. connections = entitiesDB.QueryMappedEntities<GridConnectionsEntityStruct>(oid.ID.groupID);
  108. var rid = connections.Value.Entity(oid.ID.entityID).machineRigidBodyId;
  109. foreach (var rb in ret)
  110. {
  111. if (rb.Id.entityID == rid)
  112. goto DUPLICATE; //Multiple Object Identifiers on one rigid body
  113. }
  114. ret.Add(new SimBody(rid));
  115. DUPLICATE: ;
  116. }
  117. }
  118. return ret.ToArray();
  119. }
  120. public ObjectIdentifier[] GetObjectIDsFromID(byte id, bool sim)
  121. {
  122. var ret = new FasterList<ObjectIdentifier>(4);
  123. var oide = entitiesDB.QueryEntities<ObjectIdEntityStruct>();
  124. foreach (var ((oids, count), _) in oide)
  125. {
  126. for (int i = 0; i < count; i++)
  127. {
  128. ref ObjectIdEntityStruct oid = ref oids[i];
  129. if (sim ? oid.simObjectId == id : oid.objectId == id)
  130. ret.Add(new ObjectIdentifier(oid.ID));
  131. }
  132. }
  133. return ret.ToArray();
  134. }
  135. public SimBody[] GetConnectedSimBodies(uint id)
  136. {
  137. var joints = entitiesDB.QueryEntities<JointEntityStruct>(MachineSimulationGroups.JOINTS_GROUP).ToBuffer();
  138. var list = new FasterList<SimBody>(4);
  139. for (int i = 0; i < joints.count; i++)
  140. {
  141. ref var joint = ref joints.buffer[i];
  142. if (joint.isBroken) continue;
  143. if (joint.connectedEntityA == id) list.Add(new SimBody(joint.connectedEntityB));
  144. else if (joint.connectedEntityB == id) list.Add(new SimBody(joint.connectedEntityA));
  145. }
  146. return list.ToArray();
  147. }
  148. public SimBody[] GetClusterBodies(uint cid)
  149. {
  150. var groups = entitiesDB.QueryEntities<GridConnectionsEntityStruct>();
  151. var bodies = new HashSet<uint>();
  152. foreach (var (coll, _) in groups)
  153. {
  154. var array = coll.ToBuffer().buffer;
  155. for (var index = 0; index < array.capacity; index++)
  156. {
  157. var conn = array[index];
  158. if (conn.clusterId == cid)
  159. bodies.Add(conn.machineRigidBodyId);
  160. }
  161. }
  162. return bodies.Select(id => new SimBody(id, cid)).ToArray();
  163. }
  164. public EGID? FindBlockEGID(uint id)
  165. {
  166. var groups = entitiesDB.FindGroups<DBEntityStruct>();
  167. foreach (ExclusiveGroupStruct group in groups)
  168. {
  169. if (entitiesDB.Exists<DBEntityStruct>(id, group))
  170. return new EGID(id, group);
  171. }
  172. return null;
  173. }
  174. public Cluster GetCluster(uint sbid)
  175. {
  176. var groups = entitiesDB.QueryEntities<GridConnectionsEntityStruct>();
  177. foreach (var (coll, _) in groups)
  178. {
  179. var array = coll.ToBuffer().buffer;
  180. for (var index = 0; index < array.capacity; index++)
  181. {
  182. var conn = array[index];
  183. //Static blocks don't have a cluster ID but the cluster destruction manager should have one
  184. if (conn.machineRigidBodyId == sbid && conn.clusterId != uint.MaxValue)
  185. return new Cluster(conn.clusterId);
  186. }
  187. }
  188. return null;
  189. }
  190. public Block[] GetBodyBlocks(uint sbid)
  191. {
  192. var groups = entitiesDB.QueryEntities<GridConnectionsEntityStruct>();
  193. var set = new HashSet<Block>();
  194. foreach (var (coll, _) in groups)
  195. {
  196. var array = coll.ToBuffer().buffer;
  197. for (var index = 0; index < array.capacity; index++)
  198. {
  199. var conn = array[index];
  200. if (conn.machineRigidBodyId == sbid)
  201. set.Add(new Block(conn.ID));
  202. }
  203. }
  204. return set.ToArray();
  205. }
  206. #if DEBUG
  207. public EntitiesDB GetEntitiesDB()
  208. {
  209. return entitiesDB;
  210. }
  211. #endif
  212. }
  213. }