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.

259 lines
7.8KB

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