2020-05-13 12:02:36 +00:00
|
|
|
using System.Collections.Generic;
|
2020-05-13 14:52:21 +00:00
|
|
|
|
2020-05-24 17:29:02 +00:00
|
|
|
using Gamecraft.Wires;
|
2020-05-13 12:02:36 +00:00
|
|
|
using RobocraftX.Blocks;
|
|
|
|
using RobocraftX.Common;
|
2020-05-19 22:08:02 +00:00
|
|
|
using RobocraftX.GUI.Hotbar.Colours;
|
2020-05-24 19:55:49 +00:00
|
|
|
using RobocraftX.Physics;
|
|
|
|
using RobocraftX.Scene.Simulation;
|
2020-05-13 12:02:36 +00:00
|
|
|
using Svelto.DataStructures;
|
|
|
|
using Svelto.ECS;
|
2020-05-13 14:52:21 +00:00
|
|
|
|
|
|
|
using GamecraftModdingAPI.Engines;
|
2020-05-27 15:20:53 +00:00
|
|
|
using GamecraftModdingAPI.Utility;
|
2020-05-13 12:02:36 +00:00
|
|
|
|
|
|
|
namespace GamecraftModdingAPI.Blocks
|
|
|
|
{
|
2020-05-22 01:00:33 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Engine for executing general block actions
|
|
|
|
/// </summary>
|
2020-05-18 03:19:16 +00:00
|
|
|
public class BlockEngine : IApiEngine
|
|
|
|
{
|
|
|
|
public string Name { get; } = "GamecraftModdingAPIBlockGameEngine";
|
2020-05-13 12:02:36 +00:00
|
|
|
|
2020-05-18 03:19:16 +00:00
|
|
|
public EntitiesDB entitiesDB { set; private get; }
|
2020-05-13 12:02:36 +00:00
|
|
|
|
2020-05-18 03:19:16 +00:00
|
|
|
public bool isRemovable => false;
|
2020-05-13 12:02:36 +00:00
|
|
|
|
2020-05-27 15:20:53 +00:00
|
|
|
internal bool Synced = true;
|
|
|
|
|
2020-05-18 03:19:16 +00:00
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
}
|
2020-05-13 12:02:36 +00:00
|
|
|
|
2020-05-18 03:19:16 +00:00
|
|
|
public void Ready()
|
|
|
|
{
|
|
|
|
}
|
2020-05-13 12:02:36 +00:00
|
|
|
|
2020-05-18 03:19:16 +00:00
|
|
|
public Block[] GetConnectedBlocks(EGID blockID)
|
|
|
|
{
|
|
|
|
if (!BlockExists(blockID)) return new Block[0];
|
|
|
|
Stack<uint> cubeStack = new Stack<uint>();
|
2020-06-02 23:49:54 +00:00
|
|
|
FasterList<uint> cubes = new FasterList<uint>(10);
|
|
|
|
var coll = entitiesDB.QueryEntities<GridConnectionsEntityStruct>(CommonExclusiveGroups
|
|
|
|
.OWNED_BLOCKS_GROUP);
|
|
|
|
for (int i = 0; i < coll.count; i++)
|
|
|
|
coll[i].isProcessed = false;
|
|
|
|
|
|
|
|
ConnectedCubesUtility.TreeTraversal.GetConnectedCubes(entitiesDB, blockID.entityID, cubeStack, cubes, (in GridConnectionsEntityStruct g) => { return false; });
|
|
|
|
|
|
|
|
var ret = new Block[cubes.count];
|
|
|
|
for (int i = 0; i < cubes.count; i++)
|
|
|
|
ret[i] = new Block(cubes[i]);
|
2020-05-18 03:19:16 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2020-05-13 14:52:21 +00:00
|
|
|
|
2020-05-19 22:08:02 +00:00
|
|
|
public void SetBlockColorFromPalette(ref ColourParameterEntityStruct color)
|
|
|
|
{
|
|
|
|
ref var paletteEntry = ref entitiesDB.QueryEntity<PaletteEntryEntityStruct>(color.indexInPalette,
|
|
|
|
CommonExclusiveGroups.COLOUR_PALETTE_GROUP);
|
|
|
|
color.paletteColour = paletteEntry.Colour;
|
|
|
|
}
|
|
|
|
|
2020-05-18 03:19:16 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Get a struct of a block. Can be used to set properties.
|
2020-05-19 22:08:02 +00:00
|
|
|
/// Returns a default value if not found.
|
2020-05-18 03:19:16 +00:00
|
|
|
/// </summary>
|
2020-05-19 22:08:02 +00:00
|
|
|
/// <param name="blockID">The block's ID</param>
|
|
|
|
/// <typeparam name="T">The struct to query</typeparam>
|
|
|
|
/// <returns>An editable reference to the struct</returns>
|
|
|
|
public ref T GetBlockInfo<T>(EGID blockID) where T : struct, IEntityComponent
|
2020-05-18 03:19:16 +00:00
|
|
|
{
|
2020-05-27 15:20:53 +00:00
|
|
|
if (!Synced)
|
|
|
|
{
|
|
|
|
Sync();
|
|
|
|
Synced = true;
|
|
|
|
}
|
2020-05-18 03:19:16 +00:00
|
|
|
if (entitiesDB.Exists<T>(blockID))
|
|
|
|
return ref entitiesDB.QueryEntity<T>(blockID);
|
2020-05-19 22:08:02 +00:00
|
|
|
T[] structHolder = new T[1]; //Create something that can be referenced
|
|
|
|
return ref structHolder[0]; //Gets a default value automatically
|
2020-05-18 03:19:16 +00:00
|
|
|
}
|
2020-05-17 18:13:45 +00:00
|
|
|
|
2020-05-18 03:19:16 +00:00
|
|
|
/// <summary>
|
2020-05-19 22:08:02 +00:00
|
|
|
/// Get a struct of a block. Can be used to set properties.
|
|
|
|
/// Returns a default value if not found.
|
2020-05-18 03:19:16 +00:00
|
|
|
/// </summary>
|
2020-05-19 22:08:02 +00:00
|
|
|
/// <param name="blockID">The block's ID</param>
|
|
|
|
/// <param name="exists">Whether the specified struct exists for the block</param>
|
|
|
|
/// <typeparam name="T">The struct to query</typeparam>
|
|
|
|
/// <returns>An editable reference to the struct</returns>
|
|
|
|
public ref T GetBlockInfo<T>(EGID blockID, out bool exists) where T : struct, IEntityComponent
|
2020-05-18 03:19:16 +00:00
|
|
|
{
|
2020-05-27 15:20:53 +00:00
|
|
|
if (!Synced)
|
|
|
|
{
|
|
|
|
Sync();
|
|
|
|
Synced = true;
|
|
|
|
}
|
2020-05-19 22:08:02 +00:00
|
|
|
exists = entitiesDB.Exists<T>(blockID);
|
|
|
|
if (exists)
|
|
|
|
return ref entitiesDB.QueryEntity<T>(blockID);
|
|
|
|
T[] structHolder = new T[1];
|
2020-05-21 19:04:55 +00:00
|
|
|
return ref structHolder[0];
|
2020-05-18 03:19:16 +00:00
|
|
|
}
|
2020-05-17 18:13:45 +00:00
|
|
|
|
2020-05-18 03:19:16 +00:00
|
|
|
public bool BlockExists(EGID id)
|
|
|
|
{
|
2020-06-03 00:38:35 +00:00
|
|
|
if (!Synced)
|
|
|
|
{
|
|
|
|
Sync();
|
|
|
|
Synced = true;
|
|
|
|
}
|
2020-05-18 03:19:16 +00:00
|
|
|
return entitiesDB.Exists<DBEntityStruct>(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool GetBlockInfoExists<T>(EGID blockID) where T : struct, IEntityComponent
|
|
|
|
{
|
2020-06-03 00:38:35 +00:00
|
|
|
if (!Synced)
|
|
|
|
{
|
|
|
|
Sync();
|
|
|
|
Synced = true;
|
|
|
|
}
|
2020-05-18 03:19:16 +00:00
|
|
|
return entitiesDB.Exists<T>(blockID);
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:29:02 +00:00
|
|
|
public SimBody[] GetSimBodiesFromID(byte id)
|
|
|
|
{
|
|
|
|
var ret = new FasterList<SimBody>(4);
|
|
|
|
if (!entitiesDB.HasAny<ObjectIdEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP))
|
|
|
|
return new SimBody[0];
|
|
|
|
var oids = entitiesDB.QueryEntities<ObjectIdEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
|
|
|
|
var connections = entitiesDB.QueryMappedEntities<GridConnectionsEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
|
|
|
|
foreach (ref ObjectIdEntityStruct oid in oids)
|
|
|
|
{
|
|
|
|
if (oid.objectId != id) continue;
|
|
|
|
var rid = connections.Entity(oid.ID.entityID).machineRigidBodyId;
|
|
|
|
foreach (var rb in ret)
|
|
|
|
{
|
|
|
|
if (rb.Id.entityID == rid)
|
|
|
|
goto DUPLICATE; //Multiple Object Identifiers on one rigid body
|
|
|
|
}
|
|
|
|
ret.Add(new SimBody(rid));
|
|
|
|
DUPLICATE: ;
|
|
|
|
}
|
|
|
|
return ret.ToArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
public ObjectIdentifier[] GetObjectIDsFromID(byte id)
|
|
|
|
{
|
|
|
|
var ret = new FasterList<ObjectIdentifier>(4);
|
|
|
|
if (!entitiesDB.HasAny<ObjectIdEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP))
|
|
|
|
return new ObjectIdentifier[0];
|
|
|
|
var oids = entitiesDB.QueryEntities<ObjectIdEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
|
|
|
|
foreach (ref ObjectIdEntityStruct oid in oids)
|
|
|
|
if (oid.objectId == id)
|
|
|
|
ret.Add(new ObjectIdentifier(oid.ID));
|
|
|
|
return ret.ToArray();
|
|
|
|
}
|
|
|
|
|
2020-05-24 19:55:49 +00:00
|
|
|
public SimBody[] GetConnectedSimBodies(uint id)
|
|
|
|
{
|
|
|
|
var joints = entitiesDB.QueryEntities<JointEntityStruct>(MachineSimulationGroups.JOINTS_GROUP);
|
|
|
|
var list = new FasterList<SimBody>(4);
|
|
|
|
foreach (var joint in joints)
|
|
|
|
{
|
|
|
|
if (joint.jointState == JointState.Broken) continue;
|
|
|
|
if (joint.connectedEntityA == id) list.Add(new SimBody(joint.connectedEntityB));
|
|
|
|
else if (joint.connectedEntityB == id) list.Add(new SimBody(joint.connectedEntityA));
|
|
|
|
}
|
|
|
|
|
|
|
|
return list.ToArray();
|
|
|
|
}
|
|
|
|
|
2020-05-27 15:20:53 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Synchronize newly created entity components with entities DB.
|
|
|
|
/// This forces a partial game tick, so it may be slow.
|
|
|
|
/// This also has the potential to make Gamecraft unstable.
|
|
|
|
/// Use this sparingly.
|
|
|
|
/// </summary>
|
|
|
|
private static void Sync()
|
|
|
|
{
|
|
|
|
DeterministicStepCompositionRootPatch.SubmitEntitiesNow();
|
|
|
|
}
|
|
|
|
|
2020-05-18 03:19:16 +00:00
|
|
|
#if DEBUG
|
|
|
|
public EntitiesDB GetEntitiesDB()
|
|
|
|
{
|
|
|
|
return entitiesDB;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2020-05-13 12:02:36 +00:00
|
|
|
}
|