From a7d23f4017557d35f3d55a76ae0769fb4ea1de6a Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 3 Jan 2020 01:14:26 +0100 Subject: [PATCH 1/2] Add block placement API --- GamecraftModdingAPI/Blocks/BlockColors.cs | 17 ++ GamecraftModdingAPI/Blocks/Placement.cs | 46 ++++++ GamecraftModdingAPI/Blocks/PlacementEngine.cs | 156 ++++++++++++++++++ GamecraftModdingAPI/Main.cs | 1 + 4 files changed, 220 insertions(+) create mode 100644 GamecraftModdingAPI/Blocks/BlockColors.cs create mode 100644 GamecraftModdingAPI/Blocks/Placement.cs create mode 100644 GamecraftModdingAPI/Blocks/PlacementEngine.cs diff --git a/GamecraftModdingAPI/Blocks/BlockColors.cs b/GamecraftModdingAPI/Blocks/BlockColors.cs new file mode 100644 index 0000000..7e405b4 --- /dev/null +++ b/GamecraftModdingAPI/Blocks/BlockColors.cs @@ -0,0 +1,17 @@ +namespace GamecraftModdingAPI.Blocks +{ + public enum BlockColors + { + Default = byte.MaxValue, + White = 0, + Pink, + Purple, + Blue, + Aqua, + Green, + Lime, + Yellow, + Orange, + Red + } +} \ No newline at end of file diff --git a/GamecraftModdingAPI/Blocks/Placement.cs b/GamecraftModdingAPI/Blocks/Placement.cs new file mode 100644 index 0000000..c7279e3 --- /dev/null +++ b/GamecraftModdingAPI/Blocks/Placement.cs @@ -0,0 +1,46 @@ +using System; +using GamecraftModdingAPI.Utility; +using GCMC; +using Unity.Mathematics; + +namespace GamecraftModdingAPI.Blocks +{ + /// + /// Common block movement operations + /// + public static class Placement + { + private static PlacementEngine placementEngine = new PlacementEngine(); + + /// + /// Place a block at the given position. If scaled, position means the center of the block. The default block size is 0.2 in terms of position. + /// Place blocks next to each other to connect them. + /// + /// The block's type + /// The block's color + /// The block color's darkness (0-9) - 0 is default color + /// The block's position in the grid - default block size is 0.2 + /// The block's rotation + /// The block's uniform scale - default scale is 1 (with 0.2 width) + /// The block's non-uniform scale - less than 1 means is used + /// The player who placed the block + /// + public static bool PlaceBlock(ushort block, float3 position, + quaternion rotation = new quaternion(), BlockColors color = BlockColors.Default, byte darkness = 0, + int uscale = 1, float3 scale = new float3(), uint playerId = 0) + { + if (placementEngine.IsInGame && GameState.IsBuildMode()) + { + placementEngine.PlaceBlock(block, color, darkness, position, uscale, scale, playerId, rotation); + return true; + } + + return false; + } + + public static void Init() + { + GameEngineManager.AddGameEngine(placementEngine); + } + } +} diff --git a/GamecraftModdingAPI/Blocks/PlacementEngine.cs b/GamecraftModdingAPI/Blocks/PlacementEngine.cs new file mode 100644 index 0000000..15303a8 --- /dev/null +++ b/GamecraftModdingAPI/Blocks/PlacementEngine.cs @@ -0,0 +1,156 @@ +using System; +using System.Reflection; +using DataLoader; +using GamecraftModdingAPI.Blocks; +using GamecraftModdingAPI.Utility; +using Harmony; +using JetBrains.Annotations; +using RobocraftX.Blocks; +using RobocraftX.Blocks.Ghost; +using RobocraftX.Blocks.Scaling; +using RobocraftX.Character; +using RobocraftX.CommandLine.Custom; +using RobocraftX.Common; +using RobocraftX.Common.Input; +using RobocraftX.Common.Utilities; +using RobocraftX.CR.MachineEditing; +using RobocraftX.StateSync; +using Svelto.ECS; +using Svelto.ECS.EntityStructs; +using Unity.Jobs; +using Unity.Mathematics; +using UnityEngine; +using uREPL; + +namespace GCMC +{ + public class PlacementEngine : IApiEngine + { + public bool IsInGame = false; + + public void Dispose() + { + IsInGame = false; + } + + public void Ready() + { + IsInGame = true; + } + + public IEntitiesDB entitiesDB { get; set; } + internal static BlockEntityFactory _blockEntityFactory; //Injected from PlaceBlockEngine + + /// + /// Places a block at the given position + /// + /// The block's type + /// The block's color + /// The block color's darkness - 0 is default color + /// The block's position - default block size is 0.2 + /// The block's uniform scale - default scale is 1 (with 0.2 width) + /// The block's non-uniform scale - less than 1 means is used + /// The player who placed the block + /// + public void PlaceBlock(ushort block, BlockColors color, byte darkness, float3 position, int uscale, + float3 scale, uint playerId, quaternion rotation) + { //It appears that only the non-uniform scale has any visible effect, but if that's not given here it will be set to the uniform one + try + { + if (darkness > 9) + throw new Exception("That is too dark. Make sure to use 0-9 as darkness. (0 is default.)"); + BuildBlock(block, (byte) (color + darkness * 10), position, uscale, scale, rotation).Init( + new BlockPlacementInfoStruct() + { + loadedFromDisk = false, + placedBy = playerId + }); + } + catch (Exception e) + { + Console.WriteLine(e); + Log.Error(e.Message); + } + } + + private EntityStructInitializer BuildBlock(ushort block, byte color, float3 position, int uscale, float3 scale, quaternion rot) + { + if (_blockEntityFactory == null) + throw new Exception("The factory is null."); + if (uscale < 1) + throw new Exception("Scale needs to be at least 1"); + if (scale.x < 1) scale.x = uscale; + if (scale.y < 1) scale.y = uscale; + if (scale.z < 1) scale.z = uscale; + //RobocraftX.CR.MachineEditing.PlaceBlockEngine + ScalingEntityStruct scaling = new ScalingEntityStruct {scale = scale}; + RotationEntityStruct rotation = new RotationEntityStruct {rotation = rot}; + GridRotationStruct gridRotation = new GridRotationStruct + {position = float3.zero, rotation = quaternion.identity}; + CubeCategoryStruct category = new CubeCategoryStruct + {category = CubeCategory.General, type = CubeType.Block}; + uint dbid = block; + DBEntityStruct dbEntity = new DBEntityStruct {DBID = dbid}; + uint num = PrefabsID.DBIDMAP[dbid]; + GFXPrefabEntityStructGO gfx = new GFXPrefabEntityStructGO {prefabID = num}; + BlockPlacementScaleEntityStruct placementScale = new BlockPlacementScaleEntityStruct + { + blockPlacementHeight = uscale, blockPlacementWidth = uscale, desiredScaleFactor = uscale, + snapGridScale = uscale, + unitSnapOffset = 0, isUsingUnitSize = true + }; + EquippedColourStruct colour = new EquippedColourStruct {indexInPalette = color}; + EGID egid2; + switch (category.category) + { + case CubeCategory.SpawnPoint: + case CubeCategory.BuildingSpawnPoint: + egid2 = MachineEditingGroups.NewSpawnPointBlockID; + break; + default: + egid2 = MachineEditingGroups.NewBlockID; + break; + } + + int cubeId = PrefabsID.GenerateDBID((ushort) category.category, block); + EntityStructInitializer + structInitializer = + _blockEntityFactory.Build(egid2, (uint) cubeId); //The ghost block index is only used for triggers + if (colour.indexInPalette != byte.MaxValue) + structInitializer.Init(new ColourParameterEntityStruct + { + indexInPalette = colour.indexInPalette + }); + structInitializer.Init(new GFXPrefabEntityStructGPUI(gfx.prefabID)); + structInitializer.Init(new PhysicsPrefabEntityStruct(gfx.prefabID)); + structInitializer.Init(dbEntity); + structInitializer.Init(new PositionEntityStruct {position = position}); + structInitializer.Init(rotation); + structInitializer.Init(scaling); + structInitializer.Init(gridRotation); + structInitializer.Init(new UniformBlockScaleEntityStruct + { + scaleFactor = placementScale.desiredScaleFactor + }); + return structInitializer; + } + + public string Name { get; } = "Cube placer engine"; + + [HarmonyPatch] + [UsedImplicitly] + public class FactoryObtainerPatch + { + static void Postfix(BlockEntityFactory blockEntityFactory) + { + _blockEntityFactory = blockEntityFactory; + Debug.Log("Block entity factory injected."); + } + + static MethodBase TargetMethod(HarmonyInstance instance) + { + return typeof(PlaceBlockEngine).GetConstructors()[0]; + } + } + } +} \ No newline at end of file diff --git a/GamecraftModdingAPI/Main.cs b/GamecraftModdingAPI/Main.cs index 4cff5cf..28b4dce 100644 --- a/GamecraftModdingAPI/Main.cs +++ b/GamecraftModdingAPI/Main.cs @@ -61,6 +61,7 @@ namespace GamecraftModdingAPI Blocks.Movement.Init(); Blocks.Rotation.Init(); Blocks.Signals.Init(); + Blocks.Placement.Init(); Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} initialized"); } From 94ad78d1ee705785e1e1f82434ce88ae481a1bf3 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 3 Jan 2020 14:38:59 +0100 Subject: [PATCH 2/2] Added block IDs, rotation doesn't work well --- GamecraftModdingAPI/Blocks/BlockIDs.cs | 174 ++++++++++++++++++ GamecraftModdingAPI/Blocks/Placement.cs | 4 +- GamecraftModdingAPI/Blocks/PlacementEngine.cs | 14 +- 3 files changed, 183 insertions(+), 9 deletions(-) create mode 100644 GamecraftModdingAPI/Blocks/BlockIDs.cs diff --git a/GamecraftModdingAPI/Blocks/BlockIDs.cs b/GamecraftModdingAPI/Blocks/BlockIDs.cs new file mode 100644 index 0000000..83dc735 --- /dev/null +++ b/GamecraftModdingAPI/Blocks/BlockIDs.cs @@ -0,0 +1,174 @@ +namespace GamecraftModdingAPI.Blocks +{ + public enum BlockIDs + { + AluminiumCube, + AxleS, + Battery, + HingeS, + MotorS, + HingeM, + MotorM, + TyreM, + AxleM, + IronCube, + RubberCube, + OiledCube, + AluminiumConeSegment, //12 + AluminiumCorner, + AluminiumRoundedCorner, + AluminiumSlicedCube, + AluminiumRoundedSlicedCube, + AluminiumCylinder, + AluminiumPyramidSegment, + AluminiumSlope, + AluminiumRoundedSlope, + AluminiumSphere, + RubberConeSegment, //22 + RubberCorner, + RubberRoundedCorner, + RubberSlicedCube, + RubberRoundedSlicedCube, + RubberCylinder, + RubberPyramidSegment, + RubberSlope, + RubberRoundedSlope, + RubberSphere, + OiledConeSegment, //32 + OiledCorner, + OiledRoundedCorner, + OiledSlicedCube, + OiledRoundedSlicedCube, + OiledCylinder, + OiledPyramidSegment, + OiledSlope, + OiledRoundedSlope, + OiledSphere, + IronConeSegment, //42 + IronCorner, + IronRoundedCorner, + IronSlicedCube, + IronRoundedSlicedCube, + IronCylinder, + IronPyramidSegment, + IronSlope, + IronRoundedSlope, + IronSphere, + GlassCube, //52 + GlassSlicedCube, + GlassSlope, + GlassCorner, + GlassPyramidSegment, + GlassRoundedSlicedCube, + GlassRoundedSlope, + GlassRoundedCorner, + GlassConeSegment, + GlassCylinder, + GlassSphere, + Lever, + Reactor, //64 - one ID is skipped + PlayerSpawn = 66, //Crashes without special handling + SmallSpawn, + MediumSpawn, + LargeSpawn, + BallJoint, + UniversalJoint, + ServoAxle, + ServoHinge, + StepperAxle, + StepperHinge, + TelescopicJoint, + DampedSpring, + ServoPiston, + StepperPiston, + PneumaticPiston, + PneumaticHinge, + PneumaticAxle, //82 + PilotSeat = 90, //Might crash + PassengerSeat, + PilotControls, + GrassCube, + DirtCube, + GrassConeSegment, + GrassCorner, + GrassRoundedCorner, + GrassSlicedCube, + GrassRoundedSlicedCube, + GrassPyramidSegment, + GrassSlope, + GrassRoundedSlope, + DirtConeSegment, + DirtCorner, + DirtRoundedCorner, + DirtSlicedCube, + DirtRoundedSlicedCube, + DirtPyramidSegment, + DirtSlope, + DirtRoundedSlope, + RubberHemisphere, + AluminiumHemisphere, + GrassInnerCornerBulged, + DirtInnerCornerBulged, + IronHemisphere, + OiledHemisphere, + GlassHemisphere, + TyreS, + ThreeWaySwitch, + Dial, //120 + CharacterOnEnterTrigger, //Probably crashes + CharacterOnLeaveTrigger, + CharacterOnStayTrigger, + ObjectOnEnterTrigger, + ObjectOnLeaveTrigger, + ObjectOnStayTrigger, + Button, + Switch, + TextBlock, //Brings up a screen + ConsoleBlock, //Brings up a screen + Door, + GlassDoor, + PoweredDoor, + PoweredGlassDoor, + AluminiumTubeCorner, + IronTubeCorner, + WoodCube, + WoodSlicedCube, + WoodSlope, + WoodCorner, + WoodPyramidSegment, + WoodConeSegment, + WoodRoundedSlicedCube, + WoodRoundedSlope, + WoodRoundedCorner, + WoodCylinder, + WoodHemisphere, + WoodSphere, + BrickCube, //149 + BrickSlicedCube = 151, + BrickSlope, + BrickCorner, + ConcreteCube, + ConcreteSlicedCube, + ConcreteSlope, + ConcreteCorner, + BeachTree1 = 200, + BeachTree2, + BeachTree3, + Rock1, + Rock2, + Rock3, + Rock4, + BirchTree1, + BirchTree2, + BirchTree3, + PineTree1, + PineTree2, + PineTree3, + Flower1, + Flower2, + Flower3, + Shrub1, + Shrub2, + Shrub3 + } +} \ No newline at end of file diff --git a/GamecraftModdingAPI/Blocks/Placement.cs b/GamecraftModdingAPI/Blocks/Placement.cs index c7279e3..110f92c 100644 --- a/GamecraftModdingAPI/Blocks/Placement.cs +++ b/GamecraftModdingAPI/Blocks/Placement.cs @@ -22,10 +22,10 @@ namespace GamecraftModdingAPI.Blocks /// The block's position in the grid - default block size is 0.2 /// The block's rotation /// The block's uniform scale - default scale is 1 (with 0.2 width) - /// The block's non-uniform scale - less than 1 means is used + /// The block's non-uniform scale - 0 means is used /// The player who placed the block /// - public static bool PlaceBlock(ushort block, float3 position, + public static bool PlaceBlock(BlockIDs block, float3 position, quaternion rotation = new quaternion(), BlockColors color = BlockColors.Default, byte darkness = 0, int uscale = 1, float3 scale = new float3(), uint playerId = 0) { diff --git a/GamecraftModdingAPI/Blocks/PlacementEngine.cs b/GamecraftModdingAPI/Blocks/PlacementEngine.cs index 15303a8..3171cd1 100644 --- a/GamecraftModdingAPI/Blocks/PlacementEngine.cs +++ b/GamecraftModdingAPI/Blocks/PlacementEngine.cs @@ -52,14 +52,14 @@ namespace GCMC /// The block's non-uniform scale - less than 1 means is used /// The player who placed the block /// - public void PlaceBlock(ushort block, BlockColors color, byte darkness, float3 position, int uscale, + public void PlaceBlock(BlockIDs block, BlockColors color, byte darkness, float3 position, int uscale, float3 scale, uint playerId, quaternion rotation) { //It appears that only the non-uniform scale has any visible effect, but if that's not given here it will be set to the uniform one try { if (darkness > 9) throw new Exception("That is too dark. Make sure to use 0-9 as darkness. (0 is default.)"); - BuildBlock(block, (byte) (color + darkness * 10), position, uscale, scale, rotation).Init( + BuildBlock((ushort) block, (byte) (color + darkness * 10), position, uscale, scale, rotation).Init( new BlockPlacementInfoStruct() { loadedFromDisk = false, @@ -79,12 +79,12 @@ namespace GCMC throw new Exception("The factory is null."); if (uscale < 1) throw new Exception("Scale needs to be at least 1"); - if (scale.x < 1) scale.x = uscale; - if (scale.y < 1) scale.y = uscale; - if (scale.z < 1) scale.z = uscale; + if (scale.x < 4e-5) scale.x = uscale; + if (scale.y < 4e-5) scale.y = uscale; + if (scale.z < 4e-5) scale.z = uscale; //RobocraftX.CR.MachineEditing.PlaceBlockEngine ScalingEntityStruct scaling = new ScalingEntityStruct {scale = scale}; - RotationEntityStruct rotation = new RotationEntityStruct {rotation = rot}; + RotationEntityStruct rotation = new RotationEntityStruct {rotation = quaternion.identity}; GridRotationStruct gridRotation = new GridRotationStruct {position = float3.zero, rotation = quaternion.identity}; CubeCategoryStruct category = new CubeCategoryStruct @@ -135,7 +135,7 @@ namespace GCMC return structInitializer; } - public string Name { get; } = "Cube placer engine"; + public string Name { get; } = nameof(PlacementEngine); [HarmonyPatch] [UsedImplicitly]