Tools for building games mainly focused on changing block properties. And noclip.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
9.8KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Gamecraft.Wires;
  5. using GamecraftModdingAPI;
  6. using GamecraftModdingAPI.Blocks;
  7. using GamecraftModdingAPI.Commands;
  8. using GamecraftModdingAPI.Players;
  9. using GamecraftModdingAPI.Utility;
  10. using IllusionPlugin;
  11. using RobocraftX.CommandLine.Custom;
  12. using Unity.Mathematics;
  13. using uREPL;
  14. using Main = GamecraftModdingAPI.Main;
  15. namespace BlockMod
  16. {
  17. public class BlockMod : IEnhancedPlugin
  18. {
  19. private readonly CommandUtils _commandUtils;
  20. private readonly BlockSelections _blockSelections;
  21. public BlockMod()
  22. {
  23. _blockSelections = new BlockSelections();
  24. _commandUtils = new CommandUtils(_blockSelections);
  25. }
  26. public override void OnApplicationStart()
  27. {
  28. Main.Init();
  29. GameClient.SetDebugInfo("PlayerInfo", GetPlayerInfo);
  30. GameClient.SetDebugInfo("BlockModInfo", GetBlockInfo);
  31. _commandUtils.RegisterBlockCommand("scaleBlocks",
  32. "Scales the selected blocks, relative to current size (current scale * new scale)." +
  33. " The block you're looking at (when selected based on that) stays where it is, everything else is moved next to it.",
  34. (scaleX, scaleY, scaleZ, blocks, refBlock) =>
  35. {
  36. if (!GameState.IsBuildMode()) return; //Scaling & positioning is weird in simulation
  37. if (_blockSelections.CheckNoBlocks(blocks)) return;
  38. // ReSharper disable once PossibleNullReferenceException
  39. float3 reference = refBlock.Position;
  40. float3 scale = new float3(scaleX, scaleY, scaleZ);
  41. foreach (var block in blocks)
  42. {
  43. block.Scale *= scale;
  44. block.Position = reference + (block.Position - reference) * scale;
  45. }
  46. Logging.CommandLog("Blocks scaled.");
  47. });
  48. _commandUtils.RegisterBlockCommand("scaleIndividually", "Scales the blocks you're looking at, but doesn't move them." +
  49. " The scale is relative, 1 means no change.",
  50. (scaleX, scaleY, scaleZ, blocks, refBlock) =>
  51. {
  52. if (!GameState.IsBuildMode()) return; //Scaling & positioning is weird in simulation
  53. float3 scale = new float3(scaleX, scaleY, scaleZ);
  54. foreach (var block in blocks)
  55. block.Scale *= scale;
  56. });
  57. _commandUtils.RegisterBlockCommand("moveBlocks", "Moves the blocks around.", (x, y, z, blocks, refBlock) =>
  58. {
  59. if (GameState.IsBuildMode())
  60. foreach (var block in blocks)
  61. block.Position += new float3(x, y, z);
  62. else if (GameState.IsSimulationMode())
  63. foreach (var body in GetSimBodies(blocks))
  64. body.Position += new float3(x, y, z);
  65. });
  66. _commandUtils.RegisterBlockCommand("colorBlocks", "Colors the blocks you're looking at",
  67. (color, darkness, blocks, refBlock) =>
  68. {
  69. if (!Enum.TryParse(color, true, out BlockColors clr))
  70. {
  71. Logging.CommandLogWarning("Color " + color + " not found");
  72. }
  73. foreach (var block in blocks)
  74. block.Color = new BlockColor {Color = clr, Darkness = darkness};
  75. });
  76. CommandBuilder.Builder("selectBlocksLookedAt",
  77. "Selects blocks (1 or more) to change. Only works in build mode, however the blocks can be changed afterwards." +
  78. " Parameter: whether one (true) or all connected (false) blocks should be selected.")
  79. .Action<bool>(single =>
  80. {
  81. _blockSelections.refBlock = new Player(PlayerType.Local).GetBlockLookedAt();
  82. var refBlock = _blockSelections.refBlock;
  83. _blockSelections.blocks = single ? new[] {refBlock} : refBlock?.GetConnectedCubes() ?? new Block[0];
  84. var blocks = _blockSelections.blocks;
  85. Logging.CommandLog(blocks == null ? "Selection cleared." : blocks.Length + " blocks selected.");
  86. }).Build();
  87. CommandBuilder.Builder("selectBlocksWithID", "Selects blocks with a specific object ID.")
  88. .Action<char>(id =>
  89. _blockSelections.blocks = (_blockSelections.refBlock = ObjectIdentifier.GetByID(id).FirstOrDefault())
  90. ?.GetConnectedCubes() ?? new Block[0]).Build();
  91. CommandBuilder.Builder("selectSelectedBlocks", "Selects blocks that are box selected by the player.")
  92. .Action(() =>
  93. {
  94. _blockSelections.blocks = new Player(PlayerType.Local).GetSelectedBlocks();
  95. _blockSelections.refBlock = _blockSelections.blocks.Length > 0 ? _blockSelections.blocks[0] : null;
  96. }).Build();
  97. ConsoleCommands.RegisterWithChannel("selectSendSignal", ch => { }, ChannelType.Object,
  98. "Sends a signal for selecting a given object ID for a command block.");
  99. _commandUtils.RegisterBlockCommand("pushBlocks", "Adds velocity to the selected blocks. Only works in simulation.",
  100. (x, y, z, blocks, refBlock) =>
  101. {
  102. foreach (var block in GetSimBodies(blocks))
  103. block.Velocity += new float3(x, y, z);
  104. });
  105. _commandUtils.RegisterBlockCommand("pushRotateBlocks",
  106. "Adds angular velocity to the selected blocks. Only works in simulation.",
  107. (x, y, z, blocks, refBlock) =>
  108. {
  109. foreach (var block in GetSimBodies(blocks))
  110. block.AngularVelocity += new float3(x, y, z);
  111. });
  112. CommandBuilder.Builder("pushPlayer", "Adds velocity to the player.")
  113. .Action<float, float, float>((x, y, z) =>
  114. {
  115. new Player(PlayerType.Local).Velocity += new float3(x, y, z);
  116. }).Build();
  117. CommandBuilder.Builder("pushRotatePlayer", "Adds angular velocity to the player.")
  118. .Action<float, float, float>((x, y, z) =>
  119. {
  120. new Player(PlayerType.Local).AngularVelocity += new float3(x, y, z);
  121. }).Build();
  122. }
  123. private string GetBlockInfo()
  124. {
  125. if (GameState.IsBuildMode())
  126. return GetBlockInfoInBuildMode();
  127. if (GameState.IsSimulationMode())
  128. return GetBodyInfoInSimMode();
  129. return "Switching modes...";
  130. }
  131. private static string GetBlockInfoInBuildMode()
  132. {
  133. var block = Player.LocalPlayer.GetBlockLookedAt();
  134. if (block == null) return "";
  135. float3 pos = block.Position;
  136. float3 rot = block.Rotation;
  137. float3 scale = block.Scale;
  138. return $"Block: {block.Type} at {pos.x:F} {pos.y:F} {pos.z:F}\n" +
  139. $"- Rotation: {rot.x:F}° {rot.y:F}° {rot.z:F}°\n" +
  140. $"- Color: {block.Color.Color} darkness: {block.Color.Darkness}\n" +
  141. $"- Scale: {scale.x:F} {scale.y:F} {scale.z:F}\n" +
  142. $"- Label: {block.Label}\n" +
  143. $"- ID: {block.Id}";
  144. }
  145. private static string GetBodyInfoInSimMode()
  146. {
  147. var body = Player.LocalPlayer.GetSimBodyLookedAt();
  148. if (body == null) return GetBlockInfoInBuildMode();
  149. float3 pos = body.Position;
  150. float3 rot = body.Rotation;
  151. float3 vel = body.Velocity;
  152. float3 ave = body.AngularVelocity;
  153. float3 com = body.CenterOfMass;
  154. Cluster cluster = body.Cluster;
  155. return $"Body at {pos.x:F} {pos.y:F} {pos.z:F}\n" +
  156. $"- Rotation: {rot.x:F}° {rot.y:F}° {rot.z:F}°\n" +
  157. $"- Velocity: {vel.x:F} {vel.y:F} {vel.z:F}\n" +
  158. $"- Angular velocity: {ave.x:F} {ave.y:F} {ave.z:F}\n" +
  159. $"- {(body.Static ? "Static body" : $"Mass: {body.Mass:F} center: {com.x:F} {com.y:F} {com.z:F}")}\n" +
  160. $"- Volume: {body.Volume:F}\n" +
  161. $"- Chunk health: {body.CurrentHealth:F} / {body.InitialHealth:F} - Multiplier: {body.HealthMultiplier:F}\n" +
  162. $"- Cluster health: {cluster?.CurrentHealth:F} / {cluster?.InitialHealth:F} - Multiplier: {cluster?.HealthMultiplier:F}";
  163. }
  164. private string GetPlayerInfo()
  165. {
  166. var body = Player.LocalPlayer;
  167. if (body == null) return GetBlockInfoInBuildMode();
  168. float3 pos = body.Position;
  169. float3 rot = body.Rotation;
  170. float3 vel = body.Velocity;
  171. float3 ave = body.AngularVelocity;
  172. return $"You are at {pos.x:F} {pos.y:F} {pos.z:F}\n" +
  173. $"- Rotation: {rot.x:F}° {rot.y:F}° {rot.z:F}°\n" +
  174. $"- Velocity: {vel.x:F} {vel.y:F} {vel.z:F}\n" +
  175. $"- Angular velocity: {ave.x:F} {ave.y:F} {ave.z:F}\n" +
  176. $"- Mass: {body.Mass:F}\n" +
  177. $"- Health: {body.CurrentHealth:F} / {body.InitialHealth:F}";
  178. }
  179. private IEnumerable<SimBody> GetSimBodies(Block[] blocks)
  180. => blocks.Select(block => block.GetSimBody()).Distinct();
  181. public override void OnApplicationQuit() => Main.Shutdown();
  182. public override string Name { get; } = "BlockMod";
  183. public override string Version { get; } = "v1.0.0";
  184. }
  185. }