From 7f63944a6ef99a02cfc6943d91a42507e05da581 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 19 Feb 2022 02:25:58 +0100 Subject: [PATCH] Block fixes, add mass and complexity properties, make Player.LocalPlayer return null if not found --- TechbloxModdingAPI/Block.cs | 25 ++++++++++++++++++-- TechbloxModdingAPI/Blocks/BlockComplexity.cs | 17 +++++++++++++ TechbloxModdingAPI/Cluster.cs | 6 +++++ TechbloxModdingAPI/Player.cs | 7 ++++-- TechbloxModdingAPI/SimBody.cs | 4 ++-- 5 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 TechbloxModdingAPI/Blocks/BlockComplexity.cs diff --git a/TechbloxModdingAPI/Block.cs b/TechbloxModdingAPI/Block.cs index 1b1cd23..c76ee08 100644 --- a/TechbloxModdingAPI/Block.cs +++ b/TechbloxModdingAPI/Block.cs @@ -67,7 +67,7 @@ namespace TechbloxModdingAPI /// The block object or null if doesn't exist public static Block GetLastPlacedBlock() { - uint lastBlockID = (uint) AccessTools.Field(typeof(CommonExclusiveGroups), "_nextBlockEntityID").GetValue(null) - 1; + uint lastBlockID = CommonExclusiveGroups.blockIDGeneratorClient.Peek() - 1; EGID? egid = BlockEngine.FindBlockEGID(lastBlockID); return egid.HasValue ? New(egid.Value) : null; } @@ -366,8 +366,12 @@ namespace TechbloxModdingAPI get { if (blockGroup != null) return blockGroup; + if (!GameState.IsBuildMode()) return null; // Breaks in simulation var bgec = BlockEngine.GetBlockInfo(this); - return blockGroup = bgec.currentBlockGroup == -1 ? null : new BlockGroup(bgec.currentBlockGroup, this); + return blockGroup = bgec.currentBlockGroup == -1 + ? null + : GetInstance(new EGID((uint)bgec.currentBlockGroup, BlockGroupExclusiveGroups.BlockGroupEntityGroup), + egid => new BlockGroup((int)egid.entityID, this)); } set { @@ -395,6 +399,23 @@ namespace TechbloxModdingAPI set => BlockEngine.GetBlockInfo(this).isStatic = value; } + /// + /// The mass of the block. + /// + public float Mass + { + get => BlockEngine.GetBlockInfo(this).mass; + } + + /// + /// Block complexity used for build rules. Determines the 'cost' of the block. + /// + public BlockComplexity Complexity + { + get => new(BlockEngine.GetBlockInfo(this)); + set => BlockEngine.GetBlockInfo(this) = value; + } + /// /// Whether the block exists. The other properties will return a default value if the block doesn't exist. /// If the block was just placed, then this will also return false but the properties will work correctly. diff --git a/TechbloxModdingAPI/Blocks/BlockComplexity.cs b/TechbloxModdingAPI/Blocks/BlockComplexity.cs new file mode 100644 index 0000000..9f32866 --- /dev/null +++ b/TechbloxModdingAPI/Blocks/BlockComplexity.cs @@ -0,0 +1,17 @@ +using RobocraftX.Blocks; + +namespace TechbloxModdingAPI.Blocks +{ + public record BlockComplexity(int Cpu, int Power) + { + public BlockComplexity(BlockComplexityComponent component) : this(component.cpu, component.power) + { + } + + public int Cpu { get; } = Cpu; + public int Power { get; } = Power; + + public static implicit operator BlockComplexityComponent(BlockComplexity complexity) => + new() { cpu = complexity.Cpu, power = complexity.Power }; + } +} \ No newline at end of file diff --git a/TechbloxModdingAPI/Cluster.cs b/TechbloxModdingAPI/Cluster.cs index f88cec9..79eb7b9 100644 --- a/TechbloxModdingAPI/Cluster.cs +++ b/TechbloxModdingAPI/Cluster.cs @@ -1,6 +1,7 @@ using Gamecraft.Damage; using RobocraftX.Common; using Svelto.ECS; +using Techblox.Physics; namespace TechbloxModdingAPI { @@ -36,6 +37,11 @@ namespace TechbloxModdingAPI set => Block.BlockEngine.GetBlockInfo(this).healthMultiplier = value; } + /// + /// The mass of the cluster. + /// + public float Mass => Block.BlockEngine.GetBlockInfo(this).mass; + /// /// Returns the simulation-time rigid bodies for the chunks in this cluster. /// diff --git a/TechbloxModdingAPI/Player.cs b/TechbloxModdingAPI/Player.cs index 68f5e49..a535269 100644 --- a/TechbloxModdingAPI/Player.cs +++ b/TechbloxModdingAPI/Player.cs @@ -66,13 +66,16 @@ namespace TechbloxModdingAPI /// /// Returns the current player belonging to this client. It will be different after entering/leaving simulation. + /// May return null if the local player doesn't exist. /// public static Player LocalPlayer { get { - if (localPlayer == null || localPlayer.Id != playerEngine.GetLocalPlayer()) - localPlayer = GetInstance(playerEngine.GetLocalPlayer()); + var playerId = playerEngine.GetLocalPlayer(); + if (playerId == uint.MaxValue) return null; + if (localPlayer == null || localPlayer.Id != playerId) + localPlayer = GetInstance(playerId); return localPlayer; } } diff --git a/TechbloxModdingAPI/SimBody.cs b/TechbloxModdingAPI/SimBody.cs index bb05005..158927a 100644 --- a/TechbloxModdingAPI/SimBody.cs +++ b/TechbloxModdingAPI/SimBody.cs @@ -75,13 +75,13 @@ namespace TechbloxModdingAPI public float Mass { - get => math.rcp(GetStruct().physicsMass.InverseMass); + get => 0f; //TODO: Get mass from UECS entity //set => GetStruct().physicsMass.InverseMass = math.rcp(value); } public float3 CenterOfMass { - get => GetStruct().physicsMass.CenterOfMass; + get => Block.BlockEngine.GetBlockInfo(this).centreOfMass; //set => GetStruct().physicsMass.CenterOfMass = value; }