diff --git a/BlockMod/BlockMod.cs b/BlockMod/BlockMod.cs index 5ecd627..11c5d3b 100644 --- a/BlockMod/BlockMod.cs +++ b/BlockMod/BlockMod.cs @@ -1,6 +1,9 @@ using System; +using System.Collections.Generic; using System.Linq; +using Gamecraft.Wires.ChannelsCommon; using GamecraftModdingAPI; +using GamecraftModdingAPI.Blocks; using GamecraftModdingAPI.Commands; using GamecraftModdingAPI.Engines; using GamecraftModdingAPI.Players; @@ -8,10 +11,12 @@ using GamecraftModdingAPI.Utility; using HarmonyLib; using IllusionPlugin; using RobocraftX.Character.Weapons; +using RobocraftX.CommandLine.Custom; using RobocraftX.Common.Players; using Svelto.ECS; using Svelto.ECS.Internal; using Unity.Mathematics; +using Main = GamecraftModdingAPI.Main; namespace BlockMod { @@ -22,18 +27,18 @@ namespace BlockMod Main.Init(); GameClient.SetDebugInfo("BlockModInfo", GetBlockInfo); Block[] blocks = new Block[0]; - CommandBuilder.Builder("scaleBlocksRelative", + Block refBlock = null; + CommandBuilder.Builder("scaleBlocks", "Scales the blocks you're looking at, relative to current size (current scale * new scale)." + " The block you're looking at stays where it is, everything else is moved next to it." + " The command remembers the cluster of blocks, use forgetBlocks to forget it.") .Action((scaleX, scaleY, scaleZ) => { - var bl = new Player(PlayerType.Local).GetBlockLookedAt(); - var cubes = GetCubes(ref blocks, bl); - if (cubes == null) return; - float3 reference = bl.Position; + if(CheckNoBlocks(blocks)) return; + // ReSharper disable once PossibleNullReferenceException + float3 reference = refBlock.Position; float3 scale = new float3(scaleX, scaleY, scaleZ); - foreach (var block in cubes) + foreach (var block in blocks) { block.Scale *= scale; block.Position = reference + (block.Position - reference) * scale; @@ -45,22 +50,74 @@ namespace BlockMod "The scale is relative, 1 means no change. Look at a block previously scaled to scale all of the blocks that were connected to it.") .Action((scaleX, scaleY, scaleZ) => { - var cubes = GetCubesLookedAt(ref blocks); - if (cubes == null) return; + if(CheckNoBlocks(blocks)) return; float3 scale = new float3(scaleX, scaleY, scaleZ); - foreach (var block in cubes) + foreach (var block in blocks) block.Scale *= scale; }).Build(); CommandBuilder.Builder("moveBlocks", "Moves the blocks around.") .Action((x, y, z) => { - var cubes = GetCubesLookedAt(ref blocks); - if (cubes == null) return; - foreach (var block in cubes) - block.Position += new float3(x, y, z); + if (CheckNoBlocks(blocks)) return; + if (GameState.IsBuildMode()) + foreach (var block in blocks) + block.Position += new float3(x, y, z); + else if (GameState.IsSimulationMode()) + foreach (var body in GetSimBodies(blocks)) + body.Position += new float3(x, y, z); + }).Build(); + CommandBuilder.Builder("colorBlocks", "Colors the blocks you're looking at") + .Action((color, darkness) => + { + if(CheckNoBlocks(blocks)) return; + if (!Enum.TryParse(color, true, out BlockColors clr)) + { + Logging.CommandLogWarning("Color " + color + " not found"); + } + foreach (var block in blocks) + block.Color = new BlockColor {Color = clr, Darkness = darkness}; + }).Build(); + + CommandBuilder.Builder("selectBlocksLookedAt", "Selects blocks (1 or more) to change. Only works in build mode, however the blocks can be changed afterwards." + + " Paramter: whether one (true) or all connected (false) blocks should be selected.") + .Action(single => + { + refBlock = new Player(PlayerType.Local).GetBlockLookedAt(); + blocks = single ? new[] {refBlock} : refBlock?.GetConnectedCubes() ?? new Block[0]; + Logging.CommandLog(blocks == null ? "Selection cleared." : blocks.Length + " blocks selected."); + }).Build(); + CommandBuilder.Builder("selectBlocksWithID", "Selects blocks with a specific object ID.") + .Action(id => + blocks = (refBlock = ObjectIdentifier.GetByID(id).FirstOrDefault())?.GetConnectedCubes() ?? + new Block[0]).Build(); + ConsoleCommands.RegisterWithChannel("selectBlocksFromChannel", id => + { + blocks = ObjectIdentifier.GetBySimID(id).SelectMany(block => block.GetConnectedCubes()).ToArray(); + }, + BinaryChannelUtilities.ChannelType.Object); + + CommandBuilder.Builder("pushBlocks", "Adds velocity to the selected blocks. Only works in simulation.") + .Action((x, y, z) => + { + foreach (var block in GetSimBodies(blocks)) + block.Velocity += new float3(x, y, z); + }).Build(); + CommandBuilder.Builder("pushRotateBlocks", "Adds angular velocity to the selected blocks. Only works in simulation.") + .Action((x, y, z) => + { + foreach (var block in GetSimBodies(blocks)) + block.AngularVelocity += new float3(x, y, z); + }).Build(); + CommandBuilder.Builder("pushPlayer", "Adds velocity to the player.") + .Action((x, y, z) => + { + new Player(PlayerType.Local).Velocity += new float3(x, y, z); + }).Build(); + CommandBuilder.Builder("pushRotatePlayer", "Adds angular velocity to the player.") + .Action((x, y, z) => + { + new Player(PlayerType.Local).AngularVelocity += new float3(x, y, z); }).Build(); - CommandBuilder.Builder("forgetBlocks", "Forgets the cluster of blocks that we're changing.") - .Action(() => blocks = new Block[0]).Build(); GameEngineManager.AddGameEngine(new Test()); } @@ -153,22 +210,19 @@ namespace BlockMod return "Switching modes..."; } - private bool SameCluster(Block[] bs, Block block) + private bool CheckNoBlocks(Block[] blocks) { - var id = block.Id; - return bs.Any(b => b.Id == id); + if (blocks.Length == 0) + { + Logging.CommandLogWarning("No blocks selected. Use selectBlocks first."); + return true; + } + + return false; } - private Block[] GetCubes(ref Block[] bs, Block block) => - SameCluster(bs, block) ? bs : bs = block.GetConnectedCubes(); - - private Block[] GetCubesLookedAt(ref Block[] bs) - { - var bl = new Player(PlayerType.Local).GetBlockLookedAt(); - if (bl == null) return null; - var cubes = GetCubes(ref bs, bl); - return cubes; - } + public IEnumerable GetSimBodies(Block[] blocks) + => blocks.Select(block => block.GetSimBody()).Distinct(); public void OnApplicationQuit() { diff --git a/BlockMod/BlockMod.csproj b/BlockMod/BlockMod.csproj index 6df0053..ae7d000 100644 --- a/BlockMod/BlockMod.csproj +++ b/BlockMod/BlockMod.csproj @@ -138,7 +138,7 @@ ..\..\ref\Gamecraft_Data\Managed\Gamecraft.Wires.Mockup.dll - + ..\..\GamecraftModdingAPI\GamecraftModdingAPI\bin\Debug\net472\GamecraftModdingAPI.dll @@ -345,52 +345,6 @@ ..\..\ref\Gamecraft_Data\Managed\Svelto.Tasks.dll - - ..\..\ref\Gamecraft_Data\Managed\System.dll - - - ..\..\ref\Gamecraft_Data\Managed\System.ComponentModel.Composition.dll - - - ..\..\ref\Gamecraft_Data\Managed\System.Configuration.dll - - - ..\..\ref\Gamecraft_Data\Managed\System.Core.dll - - - ..\..\ref\Gamecraft_Data\Managed\System.Data.dll - - - ..\..\ref\Gamecraft_Data\Managed\System.EnterpriseServices.dll - - - ..\..\ref\Gamecraft_Data\Managed\System.Numerics.dll - - - ..\..\ref\Gamecraft_Data\Managed\System.Runtime.dll - - - ..\..\ref\Gamecraft_Data\Managed\System.Runtime.CompilerServices.Unsafe.dll - - - - ..\..\ref\Gamecraft_Data\Managed\System.Runtime.Serialization.dll - - - ..\..\ref\Gamecraft_Data\Managed\System.Security.dll - - - ..\..\ref\Gamecraft_Data\Managed\System.ServiceModel.Internals.dll - - - ..\..\ref\Gamecraft_Data\Managed\System.Transactions.dll - - - ..\..\ref\Gamecraft_Data\Managed\System.Xml.dll - - - ..\..\ref\Gamecraft_Data\Managed\System.Xml.Linq.dll - ..\..\ref\Gamecraft_Data\Managed\Unity.Addressables.dll