Chunk and cluster fixes and improvements & bump version

Added Cluster.GetSimBodies() and SimBody.GetBlocks()
Fixed some issues with IDs and bad handling of them
This commit is contained in:
Norbi Peti 2020-10-02 16:40:06 +02:00
parent 64aace3bde
commit abbb83da26
5 changed files with 69 additions and 12 deletions

View file

@ -372,11 +372,13 @@ namespace GamecraftModdingAPI
/// Returns the rigid body of the chunk of blocks this one belongs to during simulation. /// Returns the rigid body of the chunk of blocks this one belongs to during simulation.
/// Can be used to apply forces or move the block around while the simulation is running. /// Can be used to apply forces or move the block around while the simulation is running.
/// </summary> /// </summary>
/// <returns>The SimBody of the chunk or null if the block doesn't exist.</returns> /// <returns>The SimBody of the chunk or null if the block doesn't exist or not in simulation mode.</returns>
public SimBody GetSimBody() public SimBody GetSimBody()
{ {
return BlockEngine.GetBlockInfo(this, return BlockEngine.GetBlockInfo(this,
(GridConnectionsEntityStruct st) => new SimBody(st.machineRigidBodyId, st.clusterId)); (GridConnectionsEntityStruct st) => st.machineRigidBodyId != uint.MaxValue
? new SimBody(st.machineRigidBodyId, st.clusterId)
: null);
} }
private void OnPlacedInit(object sender, BlockPlacedRemovedEventArgs e) private void OnPlacedInit(object sender, BlockPlacedRemovedEventArgs e)

View file

@ -218,7 +218,7 @@ namespace GamecraftModdingAPI.Blocks
} }
} }
return bodies.Select(id => new SimBody(id)).ToArray(); return bodies.Select(id => new SimBody(id, cid)).ToArray();
} }
public EGID? FindBlockEGID(uint id) public EGID? FindBlockEGID(uint id)
@ -239,8 +239,8 @@ namespace GamecraftModdingAPI.Blocks
foreach (var (coll, _) in groups) foreach (var (coll, _) in groups)
{ {
foreach (var conn in coll) foreach (var conn in coll)
{ { //Static blocks don't have a cluster ID but the cluster destruction manager should have one
if (conn.machineRigidBodyId == sbid) if (conn.machineRigidBodyId == sbid && conn.clusterId != uint.MaxValue)
return new Cluster(conn.clusterId); return new Cluster(conn.clusterId);
} }
} }
@ -248,6 +248,22 @@ namespace GamecraftModdingAPI.Blocks
return null; return null;
} }
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();
}
#if DEBUG #if DEBUG
public EntitiesDB GetEntitiesDB() public EntitiesDB GetEntitiesDB()
{ {

View file

@ -6,6 +6,7 @@ namespace GamecraftModdingAPI
{ {
/// <summary> /// <summary>
/// Represnts a cluster of blocks in time running mode, meaning blocks that are connected either directly or via joints. /// Represnts a cluster of blocks in time running mode, meaning blocks that are connected either directly or via joints.
/// Only exists if a cluster destruction manager is present. Static blocks like grass and dirt aren't part of a cluster.
/// </summary> /// </summary>
public class Cluster public class Cluster
{ {
@ -37,5 +38,37 @@ namespace GamecraftModdingAPI
get => Block.BlockEngine.GetBlockInfo<HealthEntityComponent>(Id).healthMultiplier; get => Block.BlockEngine.GetBlockInfo<HealthEntityComponent>(Id).healthMultiplier;
set => Block.BlockEngine.GetBlockInfo<HealthEntityComponent>(Id).healthMultiplier = value; set => Block.BlockEngine.GetBlockInfo<HealthEntityComponent>(Id).healthMultiplier = value;
} }
/// <summary>
/// Returns the simulation-time rigid bodies for the chunks in this cluster.
/// </summary>
/// <returns>An array of sim-bodies</returns>
public SimBody[] GetSimBodies()
{
return Block.BlockEngine.GetClusterBodies(Id.entityID);
}
public override string ToString()
{
return $"{nameof(Id)}: {Id}";
}
protected bool Equals(Cluster other)
{
return Id.Equals(other.Id);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Cluster) obj);
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
} }
} }

View file

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net472</TargetFramework> <TargetFramework>net472</TargetFramework>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Version>1.5.0</Version> <Version>1.6.0</Version>
<Authors>Exmods</Authors> <Authors>Exmods</Authors>
<PackageLicenseExpression>GNU General Public Licence 3+</PackageLicenseExpression> <PackageLicenseExpression>GNU General Public Licence 3+</PackageLicenseExpression>
<PackageProjectUrl>https://git.exmods.org/modtainers/GamecraftModdingAPI</PackageProjectUrl> <PackageProjectUrl>https://git.exmods.org/modtainers/GamecraftModdingAPI</PackageProjectUrl>
@ -445,10 +445,6 @@
<HintPath>..\ref\Gamecraft_Data\Managed\RobocraftX_TextBlock.dll</HintPath> <HintPath>..\ref\Gamecraft_Data\Managed\RobocraftX_TextBlock.dll</HintPath>
<HintPath>..\..\ref\Gamecraft_Data\Managed\RobocraftX_TextBlock.dll</HintPath> <HintPath>..\..\ref\Gamecraft_Data\Managed\RobocraftX_TextBlock.dll</HintPath>
</Reference> </Reference>
<Reference Include="RobocratX.SimulationCompositionRoot">
<HintPath>..\ref\Gamecraft_Data\Managed\RobocratX.SimulationCompositionRoot.dll</HintPath>
<HintPath>..\..\ref\Gamecraft_Data\Managed\RobocratX.SimulationCompositionRoot.dll</HintPath>
</Reference>
<Reference Include="SpawningPointCompositionRoot"> <Reference Include="SpawningPointCompositionRoot">
<HintPath>..\ref\Gamecraft_Data\Managed\SpawningPointCompositionRoot.dll</HintPath> <HintPath>..\ref\Gamecraft_Data\Managed\SpawningPointCompositionRoot.dll</HintPath>
<HintPath>..\..\ref\Gamecraft_Data\Managed\SpawningPointCompositionRoot.dll</HintPath> <HintPath>..\..\ref\Gamecraft_Data\Managed\SpawningPointCompositionRoot.dll</HintPath>

View file

@ -17,12 +17,13 @@ namespace GamecraftModdingAPI
public EGID Id { get; } public EGID Id { get; }
/// <summary> /// <summary>
/// The cluster this chunk belongs to, or null if the chunk doesn't exist. Get the SimBody from a Block if possible for good performance here. /// The cluster this chunk belongs to, or null if no cluster destruction manager present or the chunk doesn't exist.
/// Get the SimBody from a Block if possible for good performance here.
/// </summary> /// </summary>
public Cluster Cluster => cluster ?? (cluster = clusterId == uint.MaxValue ? Block.BlockEngine.GetCluster(Id.entityID) : new Cluster(clusterId)); public Cluster Cluster => cluster ?? (cluster = clusterId == uint.MaxValue ? Block.BlockEngine.GetCluster(Id.entityID) : new Cluster(clusterId));
private Cluster cluster; private Cluster cluster;
private uint clusterId; private readonly uint clusterId = uint.MaxValue;
public SimBody(EGID id) public SimBody(EGID id)
{ {
@ -120,6 +121,15 @@ namespace GamecraftModdingAPI
return Block.BlockEngine.GetConnectedSimBodies(Id.entityID); return Block.BlockEngine.GetConnectedSimBodies(Id.entityID);
} }
/// <summary>
/// The blocks that form this rigid body.
/// </summary>
/// <returns></returns>
public Block[] GetBlocks()
{
return Block.BlockEngine.GetBodyBlocks(Id.entityID);
}
private ref RigidBodyEntityStruct GetStruct() private ref RigidBodyEntityStruct GetStruct()
{ {
return ref Block.BlockEngine.GetBlockInfo<RigidBodyEntityStruct>(Id); return ref Block.BlockEngine.GetBlockInfo<RigidBodyEntityStruct>(Id);