2020-07-15 19:58:24 +00:00
|
|
|
using System;
|
2020-05-13 12:02:36 +00:00
|
|
|
using System.Collections.Generic;
|
2020-08-13 14:59:13 +00:00
|
|
|
using System.Linq;
|
2020-05-13 14:52:21 +00:00
|
|
|
|
2020-10-28 23:37:47 +00:00
|
|
|
using Gamecraft.ColourPalette;
|
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-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-08-13 14:59:13 +00:00
|
|
|
using Svelto.ECS.Hybrid;
|
2020-05-13 14:52:21 +00:00
|
|
|
|
|
|
|
using GamecraftModdingAPI.Engines;
|
2020-09-28 01:10:59 +00:00
|
|
|
using Unity.Mathematics;
|
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-07-15 19:58:24 +00:00
|
|
|
public partial class BlockEngine : IApiEngine
|
2020-05-18 03:19:16 +00:00
|
|
|
{
|
|
|
|
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-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];
|
2020-07-10 22:30:58 +00:00
|
|
|
Stack<EGID> cubeStack = new Stack<EGID>();
|
|
|
|
FasterList<EGID> cubes = new FasterList<EGID>(10);
|
|
|
|
var coll = entitiesDB.QueryEntities<GridConnectionsEntityStruct>();
|
|
|
|
foreach (var (ecoll, _) in coll)
|
|
|
|
foreach (ref var conn in ecoll)
|
|
|
|
conn.isProcessed = false;
|
2020-06-02 23:49:54 +00:00
|
|
|
|
2020-07-10 22:30:58 +00:00
|
|
|
ConnectedCubesUtility.TreeTraversal.GetConnectedCubes(entitiesDB, blockID, cubeStack, cubes,
|
|
|
|
(in GridConnectionsEntityStruct g) => { return false; });
|
2020-06-02 23:49:54 +00:00
|
|
|
|
|
|
|
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-09-28 01:10:59 +00:00
|
|
|
public float4 ConvertBlockColor(byte index) => index == byte.MaxValue
|
|
|
|
? new float4(-1f, -1f, -1f, -1f)
|
|
|
|
: entitiesDB.QueryEntity<PaletteEntryEntityStruct>(index,
|
|
|
|
CommonExclusiveGroups.COLOUR_PALETTE_GROUP).Colour;
|
2020-05-19 22:08:02 +00:00
|
|
|
|
2020-08-07 17:55:00 +00:00
|
|
|
public ref T GetBlockInfo<T>(EGID blockID) where T : unmanaged, IEntityComponent
|
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-08-07 17:55:00 +00:00
|
|
|
|
|
|
|
public ref T GetBlockInfoViewStruct<T>(EGID blockID) where T : struct, INeedEGID, IEntityComponent
|
|
|
|
{
|
|
|
|
if (entitiesDB.Exists<T>(blockID))
|
|
|
|
{
|
|
|
|
// TODO: optimize by using EntitiesDB internal calls instead of iterating over everything
|
|
|
|
EntityCollection<T> entities = entitiesDB.QueryEntities<T>(blockID.groupID);
|
|
|
|
for (int i = 0; i < entities.count; i++)
|
|
|
|
{
|
|
|
|
if (entities[i].ID == blockID)
|
|
|
|
{
|
|
|
|
return ref entities[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
T[] structHolder = new T[1]; //Create something that can be referenced
|
|
|
|
return ref structHolder[0]; //Gets a default value automatically
|
|
|
|
}
|
2020-05-17 18:13:45 +00:00
|
|
|
|
2020-07-15 19:58:24 +00:00
|
|
|
public U GetBlockInfo<T, U>(Block block, Func<T, U> getter,
|
2020-08-07 17:55:00 +00:00
|
|
|
U def = default) where T : unmanaged, IEntityComponent
|
2020-07-15 19:58:24 +00:00
|
|
|
{
|
|
|
|
if (entitiesDB.Exists<T>(block.Id))
|
|
|
|
return getter(entitiesDB.QueryEntity<T>(block.Id));
|
2020-08-13 14:59:13 +00:00
|
|
|
return GetBlockInitInfo(block, getter, def);
|
2020-07-15 19:58:24 +00:00
|
|
|
}
|
2020-08-07 17:55:00 +00:00
|
|
|
|
|
|
|
public U GetBlockInfoViewStruct<T, U>(Block block, Func<T, U> getter,
|
2020-08-13 14:59:13 +00:00
|
|
|
U def = default) where T : struct, IEntityViewComponent
|
2020-08-07 17:55:00 +00:00
|
|
|
{
|
|
|
|
if (entitiesDB.Exists<T>(block.Id))
|
2020-08-13 14:59:13 +00:00
|
|
|
return getter(entitiesDB.QueryEntity<T>(block.Id));
|
|
|
|
return GetBlockInitInfo(block, getter, def);
|
|
|
|
}
|
|
|
|
|
|
|
|
private U GetBlockInitInfo<T, U>(Block block, Func<T, U> getter, U def) where T : struct, IEntityComponent
|
|
|
|
{
|
2020-08-07 17:55:00 +00:00
|
|
|
if (block.InitData.Group == null) return def;
|
|
|
|
var initializer = new EntityComponentInitializer(block.Id, block.InitData.Group);
|
|
|
|
if (initializer.Has<T>())
|
|
|
|
return getter(initializer.Get<T>());
|
|
|
|
return def;
|
|
|
|
}
|
2020-07-15 19:58:24 +00:00
|
|
|
|
|
|
|
public delegate void Setter<T, U>(ref T component, U value) where T : struct, IEntityComponent;
|
|
|
|
|
2020-08-13 14:59:13 +00:00
|
|
|
public void SetBlockInfoViewStruct<T, U>(Block block, Setter<T, U> setter, U value) where T : struct, IEntityViewComponent
|
2020-08-07 17:55:00 +00:00
|
|
|
{
|
|
|
|
if (entitiesDB.Exists<T>(block.Id))
|
2020-08-13 14:59:13 +00:00
|
|
|
setter(ref entitiesDB.QueryEntity<T>(block.Id), value);
|
|
|
|
else
|
|
|
|
SetBlockInitInfo(block, setter, value);
|
2020-08-07 17:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetBlockInfo<T, U>(Block block, Setter<T, U> setter, U value) where T : unmanaged, IEntityComponent
|
2020-05-18 03:19:16 +00:00
|
|
|
{
|
2020-07-15 19:58:24 +00:00
|
|
|
if (entitiesDB.Exists<T>(block.Id))
|
|
|
|
setter(ref entitiesDB.QueryEntity<T>(block.Id), value);
|
2020-08-13 14:59:13 +00:00
|
|
|
else
|
|
|
|
SetBlockInitInfo(block, setter, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SetBlockInitInfo<T, U>(Block block, Setter<T, U> setter, U value)
|
|
|
|
where T : struct, IEntityComponent
|
|
|
|
{
|
|
|
|
if (block.InitData.Group != null)
|
2020-05-27 15:20:53 +00:00
|
|
|
{
|
2020-07-15 19:58:24 +00:00
|
|
|
var initializer = new EntityComponentInitializer(block.Id, block.InitData.Group);
|
2020-07-20 22:19:30 +00:00
|
|
|
T component = initializer.Has<T>() ? initializer.Get<T>() : default;
|
|
|
|
ref T structRef = ref component;
|
2020-07-15 19:58:24 +00:00
|
|
|
setter(ref structRef, value);
|
|
|
|
initializer.Init(structRef);
|
2020-05-27 15:20:53 +00:00
|
|
|
}
|
2020-05-18 03:19:16 +00:00
|
|
|
}
|
2020-05-17 18:13:45 +00:00
|
|
|
|
2020-07-15 19:58:24 +00:00
|
|
|
public bool BlockExists(EGID blockID)
|
2020-05-18 03:19:16 +00:00
|
|
|
{
|
2020-07-15 19:58:24 +00:00
|
|
|
return entitiesDB.Exists<DBEntityStruct>(blockID);
|
2020-05-18 03:19:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 19:58:24 +00:00
|
|
|
public bool GetBlockInfoExists<T>(Block block) where T : struct, IEntityComponent
|
2020-05-18 03:19:16 +00:00
|
|
|
{
|
2020-07-15 19:58:24 +00:00
|
|
|
if (entitiesDB.Exists<T>(block.Id))
|
|
|
|
return true;
|
|
|
|
if (block.InitData.Group == null)
|
|
|
|
return false;
|
|
|
|
var init = new EntityComponentInitializer(block.Id, block.InitData.Group);
|
|
|
|
return init.Has<T>();
|
2020-05-18 03:19:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:29:02 +00:00
|
|
|
public SimBody[] GetSimBodiesFromID(byte id)
|
|
|
|
{
|
|
|
|
var ret = new FasterList<SimBody>(4);
|
2020-07-10 22:30:58 +00:00
|
|
|
if (!entitiesDB.HasAny<ObjectIdEntityStruct>(CommonExclusiveGroups.BUILD_OBJID_BLOCK_GROUP))
|
2020-05-24 17:29:02 +00:00
|
|
|
return new SimBody[0];
|
2020-07-10 22:30:58 +00:00
|
|
|
var oids = entitiesDB.QueryEntities<ObjectIdEntityStruct>(CommonExclusiveGroups.BUILD_OBJID_BLOCK_GROUP);
|
|
|
|
var connections = entitiesDB.QueryMappedEntities<GridConnectionsEntityStruct>(CommonExclusiveGroups.BUILD_OBJID_BLOCK_GROUP);
|
2020-05-24 17:29:02 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-06-04 22:57:22 +00:00
|
|
|
public ObjectIdentifier[] GetObjectIDsFromID(byte id, bool sim)
|
2020-05-24 17:29:02 +00:00
|
|
|
{
|
|
|
|
var ret = new FasterList<ObjectIdentifier>(4);
|
2020-07-10 22:30:58 +00:00
|
|
|
if (!entitiesDB.HasAny<ObjectIdEntityStruct>(CommonExclusiveGroups.BUILD_OBJID_BLOCK_GROUP))
|
2020-05-24 17:29:02 +00:00
|
|
|
return new ObjectIdentifier[0];
|
2020-07-10 22:30:58 +00:00
|
|
|
var oids = entitiesDB.QueryEntities<ObjectIdEntityStruct>(CommonExclusiveGroups.BUILD_OBJID_BLOCK_GROUP);
|
2020-05-24 17:29:02 +00:00
|
|
|
foreach (ref ObjectIdEntityStruct oid in oids)
|
2020-06-04 22:57:22 +00:00
|
|
|
if (sim ? oid.simObjectId == id : oid.objectId == id)
|
2020-05-24 17:29:02 +00:00
|
|
|
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-08-13 14:59:13 +00:00
|
|
|
public SimBody[] GetClusterBodies(uint cid)
|
|
|
|
{
|
|
|
|
var groups = entitiesDB.QueryEntities<GridConnectionsEntityStruct>();
|
|
|
|
var bodies = new HashSet<uint>();
|
|
|
|
foreach (var (coll, _) in groups)
|
|
|
|
{
|
|
|
|
foreach (var conn in coll)
|
|
|
|
{
|
|
|
|
if (conn.clusterId == cid)
|
|
|
|
bodies.Add(conn.machineRigidBodyId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 14:40:06 +00:00
|
|
|
return bodies.Select(id => new SimBody(id, cid)).ToArray();
|
2020-08-13 14:59:13 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 00:26:36 +00:00
|
|
|
public EGID? FindBlockEGID(uint id)
|
|
|
|
{
|
|
|
|
var groups = entitiesDB.FindGroups<DBEntityStruct>();
|
|
|
|
foreach (ExclusiveGroupStruct group in groups)
|
|
|
|
{
|
|
|
|
if (entitiesDB.Exists<DBEntityStruct>(id, group))
|
|
|
|
return new EGID(id, group);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-08-13 14:59:13 +00:00
|
|
|
public Cluster GetCluster(uint sbid)
|
|
|
|
{
|
|
|
|
var groups = entitiesDB.QueryEntities<GridConnectionsEntityStruct>();
|
|
|
|
foreach (var (coll, _) in groups)
|
|
|
|
{
|
|
|
|
foreach (var conn in coll)
|
2020-10-02 14:40:06 +00:00
|
|
|
{ //Static blocks don't have a cluster ID but the cluster destruction manager should have one
|
|
|
|
if (conn.machineRigidBodyId == sbid && conn.clusterId != uint.MaxValue)
|
2020-08-13 14:59:13 +00:00
|
|
|
return new Cluster(conn.clusterId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-10-02 14:40:06 +00:00
|
|
|
public Block[] GetBodyBlocks(uint sbid)
|
|
|
|
{
|
|
|
|
var groups = entitiesDB.QueryEntities<GridConnectionsEntityStruct>();
|
|
|
|
var set = new HashSet<Block>();
|
|
|
|
foreach (var (coll, _) in groups)
|
|
|
|
{
|
|
|
|
foreach (var conn in coll)
|
|
|
|
{
|
|
|
|
if (conn.machineRigidBodyId == sbid)
|
|
|
|
set.Add(new Block(conn.ID));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return set.ToArray();
|
|
|
|
}
|
|
|
|
|
2020-05-18 03:19:16 +00:00
|
|
|
#if DEBUG
|
|
|
|
public EntitiesDB GetEntitiesDB()
|
|
|
|
{
|
|
|
|
return entitiesDB;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2020-05-13 12:02:36 +00:00
|
|
|
}
|