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.

136 lines
4.3KB

  1. using System.Collections.Generic;
  2. using Gamecraft.Wires;
  3. using RobocraftX.Blocks;
  4. using RobocraftX.Common;
  5. using RobocraftX.GUI.Hotbar.Colours;
  6. using Svelto.DataStructures;
  7. using Svelto.ECS;
  8. using GamecraftModdingAPI.Engines;
  9. namespace GamecraftModdingAPI.Blocks
  10. {
  11. /// <summary>
  12. /// Engine for executing general block actions
  13. /// </summary>
  14. public class BlockEngine : IApiEngine
  15. {
  16. public string Name { get; } = "GamecraftModdingAPIBlockGameEngine";
  17. public EntitiesDB entitiesDB { set; private get; }
  18. public bool isRemovable => false;
  19. public void Dispose()
  20. {
  21. }
  22. public void Ready()
  23. {
  24. }
  25. public Block[] GetConnectedBlocks(EGID blockID)
  26. {
  27. if (!BlockExists(blockID)) return new Block[0];
  28. Stack<uint> cubeStack = new Stack<uint>();
  29. FasterList<uint> cubesToProcess = new FasterList<uint>();
  30. ConnectedCubesUtility.TreeTraversal.GetConnectedCubes(entitiesDB, blockID.entityID, cubeStack, cubesToProcess, (in GridConnectionsEntityStruct g) => { return false; });
  31. var ret = new Block[cubesToProcess.count];
  32. for (int i = 0; i < cubesToProcess.count; i++)
  33. ret[i] = new Block(cubesToProcess[i]);
  34. return ret;
  35. }
  36. public void SetBlockColorFromPalette(ref ColourParameterEntityStruct color)
  37. {
  38. ref var paletteEntry = ref entitiesDB.QueryEntity<PaletteEntryEntityStruct>(color.indexInPalette,
  39. CommonExclusiveGroups.COLOUR_PALETTE_GROUP);
  40. color.paletteColour = paletteEntry.Colour;
  41. }
  42. /// <summary>
  43. /// Get a struct of a block. Can be used to set properties.
  44. /// Returns a default value if not found.
  45. /// </summary>
  46. /// <param name="blockID">The block's ID</param>
  47. /// <typeparam name="T">The struct to query</typeparam>
  48. /// <returns>An editable reference to the struct</returns>
  49. public ref T GetBlockInfo<T>(EGID blockID) where T : struct, IEntityComponent
  50. {
  51. if (entitiesDB.Exists<T>(blockID))
  52. return ref entitiesDB.QueryEntity<T>(blockID);
  53. T[] structHolder = new T[1]; //Create something that can be referenced
  54. return ref structHolder[0]; //Gets a default value automatically
  55. }
  56. /// <summary>
  57. /// Get a struct of a block. Can be used to set properties.
  58. /// Returns a default value if not found.
  59. /// </summary>
  60. /// <param name="blockID">The block's ID</param>
  61. /// <param name="exists">Whether the specified struct exists for the block</param>
  62. /// <typeparam name="T">The struct to query</typeparam>
  63. /// <returns>An editable reference to the struct</returns>
  64. public ref T GetBlockInfo<T>(EGID blockID, out bool exists) where T : struct, IEntityComponent
  65. {
  66. exists = entitiesDB.Exists<T>(blockID);
  67. if (exists)
  68. return ref entitiesDB.QueryEntity<T>(blockID);
  69. T[] structHolder = new T[1];
  70. //ref T defRef = ref structHolder[0];
  71. return ref structHolder[0];
  72. }
  73. public bool BlockExists(EGID id)
  74. {
  75. return entitiesDB.Exists<DBEntityStruct>(id);
  76. }
  77. public bool GetBlockInfoExists<T>(EGID blockID) where T : struct, IEntityComponent
  78. {
  79. return entitiesDB.Exists<T>(blockID);
  80. }
  81. public SimBody[] GetSimBodiesFromID(byte id)
  82. {
  83. var ret = new FasterList<SimBody>(4);
  84. if (!entitiesDB.HasAny<ObjectIdEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP))
  85. return new SimBody[0];
  86. var oids = entitiesDB.QueryEntities<ObjectIdEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  87. var connections = entitiesDB.QueryMappedEntities<GridConnectionsEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  88. foreach (ref ObjectIdEntityStruct oid in oids)
  89. {
  90. if (oid.objectId != id) continue;
  91. var rid = connections.Entity(oid.ID.entityID).machineRigidBodyId;
  92. foreach (var rb in ret)
  93. {
  94. if (rb.Id.entityID == rid)
  95. goto DUPLICATE; //Multiple Object Identifiers on one rigid body
  96. }
  97. ret.Add(new SimBody(rid));
  98. DUPLICATE: ;
  99. }
  100. return ret.ToArray();
  101. }
  102. public ObjectIdentifier[] GetObjectIDsFromID(byte id)
  103. {
  104. var ret = new FasterList<ObjectIdentifier>(4);
  105. if (!entitiesDB.HasAny<ObjectIdEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP))
  106. return new ObjectIdentifier[0];
  107. var oids = entitiesDB.QueryEntities<ObjectIdEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  108. foreach (ref ObjectIdEntityStruct oid in oids)
  109. if (oid.objectId == id)
  110. ret.Add(new ObjectIdentifier(oid.ID));
  111. return ret.ToArray();
  112. }
  113. #if DEBUG
  114. public EntitiesDB GetEntitiesDB()
  115. {
  116. return entitiesDB;
  117. }
  118. #endif
  119. }
  120. }