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.

282 lines
12KB

  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 Block[] blocks = new Block[0];
  20. private Block refBlock;
  21. public override void OnApplicationStart()
  22. {
  23. Main.Init();
  24. GameClient.SetDebugInfo("BlockModInfo", GetBlockInfo);
  25. RegisterBlockCommand("scaleBlocks",
  26. "Scales the blocks you're looking at, relative to current size (current scale * new scale)." +
  27. " The block you're looking at stays where it is, everything else is moved next to it.",
  28. (scaleX, scaleY, scaleZ, blocks, refBlock) =>
  29. {
  30. if (!GameState.IsBuildMode()) return; //Scaling & positioning is weird in simulation
  31. if (CheckNoBlocks(blocks)) return;
  32. // ReSharper disable once PossibleNullReferenceException
  33. float3 reference = refBlock.Position;
  34. float3 scale = new float3(scaleX, scaleY, scaleZ);
  35. foreach (var block in blocks)
  36. {
  37. block.Scale *= scale;
  38. block.Position = reference + (block.Position - reference) * scale;
  39. }
  40. Logging.CommandLog("Blocks scaled.");
  41. });
  42. RegisterBlockCommand("scaleIndividually", "Scales the blocks you're looking at, but doesn't move them." +
  43. "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.",
  44. (scaleX, scaleY, scaleZ, blocks, refBlock) =>
  45. {
  46. if (!GameState.IsBuildMode()) return; //Scaling & positioning is weird in simulation
  47. float3 scale = new float3(scaleX, scaleY, scaleZ);
  48. foreach (var block in blocks)
  49. block.Scale *= scale;
  50. });
  51. RegisterBlockCommand("moveBlocks", "Moves the blocks around.", (x, y, z, blocks, refBlock) =>
  52. {
  53. if (GameState.IsBuildMode())
  54. foreach (var block in blocks)
  55. block.Position += new float3(x, y, z);
  56. else if (GameState.IsSimulationMode())
  57. foreach (var body in GetSimBodies(blocks))
  58. body.Position += new float3(x, y, z);
  59. });
  60. RegisterBlockCommand("colorBlocks", "Colors the blocks you're looking at",
  61. (color, darkness, blocks, refBlock) =>
  62. {
  63. if (!Enum.TryParse(color, true, out BlockColors clr))
  64. {
  65. Logging.CommandLogWarning("Color " + color + " not found");
  66. }
  67. foreach (var block in blocks)
  68. block.Color = new BlockColor {Color = clr, Darkness = darkness};
  69. });
  70. CommandBuilder.Builder("selectBlocksLookedAt",
  71. "Selects blocks (1 or more) to change. Only works in build mode, however the blocks can be changed afterwards." +
  72. " Paramter: whether one (true) or all connected (false) blocks should be selected.")
  73. .Action<bool>(single =>
  74. {
  75. refBlock = new Player(PlayerType.Local).GetBlockLookedAt();
  76. blocks = single ? new[] {refBlock} : refBlock?.GetConnectedCubes() ?? new Block[0];
  77. Logging.CommandLog(blocks == null ? "Selection cleared." : blocks.Length + " blocks selected.");
  78. }).Build();
  79. CommandBuilder.Builder("selectBlocksWithID", "Selects blocks with a specific object ID.")
  80. .Action<char>(id =>
  81. blocks = (refBlock = ObjectIdentifier.GetByID(id).FirstOrDefault())?.GetConnectedCubes() ??
  82. new Block[0]).Build();
  83. CommandBuilder.Builder("selectSelectedBlocks", "Selects blocks that are box selected by the player.")
  84. .Action(() =>
  85. {
  86. blocks = new Player(PlayerType.Local).GetSelectedBlocks();
  87. refBlock = blocks.Length > 0 ? blocks[0] : null;
  88. }).Build();
  89. ConsoleCommands.RegisterWithChannel("selectSendSignal", ch => { }, ChannelType.Object,
  90. "Sends a signal for selecting a given object ID for a command block.");
  91. RegisterBlockCommand("pushBlocks", "Adds velocity to the selected blocks. Only works in simulation.",
  92. (x, y, z, blocks, refBlock) =>
  93. {
  94. foreach (var block in GetSimBodies(blocks))
  95. block.Velocity += new float3(x, y, z);
  96. });
  97. RegisterBlockCommand("pushRotateBlocks",
  98. "Adds angular velocity to the selected blocks. Only works in simulation.",
  99. (x, y, z, blocks, refBlock) =>
  100. {
  101. foreach (var block in GetSimBodies(blocks))
  102. block.AngularVelocity += new float3(x, y, z);
  103. });
  104. CommandBuilder.Builder("pushPlayer", "Adds velocity to the player.")
  105. .Action<float, float, float>((x, y, z) =>
  106. {
  107. new Player(PlayerType.Local).Velocity += new float3(x, y, z);
  108. }).Build();
  109. CommandBuilder.Builder("pushRotatePlayer", "Adds angular velocity to the player.")
  110. .Action<float, float, float>((x, y, z) =>
  111. {
  112. new Player(PlayerType.Local).AngularVelocity += new float3(x, y, z);
  113. }).Build();
  114. }
  115. private string GetBlockInfo()
  116. {
  117. if (GameState.IsBuildMode())
  118. return GetBlockInfoInBuildMode();
  119. if (GameState.IsSimulationMode())
  120. return GetBodyInfoInSimMode();
  121. return "Switching modes...";
  122. }
  123. private static string GetBlockInfoInBuildMode()
  124. {
  125. var block = new Player(PlayerType.Local).GetBlockLookedAt();
  126. if (block == null) return "";
  127. float3 pos = block.Position;
  128. float3 rot = block.Rotation;
  129. float3 scale = block.Scale;
  130. return $"Block: {block.Type} at {pos.x:F} {pos.y:F} {pos.z:F}\n" +
  131. $"- Rotation: {rot.x:F}° {rot.y:F}° {rot.z:F}°\n" +
  132. $"- Color: {block.Color.Color} darkness: {block.Color.Darkness}\n" +
  133. $"- Scale: {scale.x:F} {scale.y:F} {scale.z:F}\n" +
  134. $"- Label: {block.Label}\n" +
  135. $"- ID: {block.Id}";
  136. }
  137. private static string GetBodyInfoInSimMode()
  138. {
  139. var body = new Player(PlayerType.Local).GetSimBodyLookedAt();
  140. if (body == null) return GetBlockInfoInBuildMode();
  141. float3 pos = body.Position;
  142. float3 rot = body.Rotation;
  143. float3 vel = body.Velocity;
  144. float3 ave = body.AngularVelocity;
  145. float3 com = body.CenterOfMass;
  146. Cluster cluster = body.Cluster;
  147. return $"Body at {pos.x:F} {pos.y:F} {pos.z:F}\n" +
  148. $"- Rotation: {rot.x:F}° {rot.y:F}° {rot.z:F}°\n" +
  149. $"- Velocity: {vel.x:F} {vel.y:F} {vel.z:F}\n" +
  150. $"- Angular velocity: {ave.x:F} {ave.y:F} {ave.z:F}\n" +
  151. $"- {(body.Static ? "Static body" : $"Mass: {body.Mass:F} center: {com.x:F} {com.y:F} {com.z:F}")}\n" +
  152. $"- Volume: {body.Volume:F}\n" +
  153. $"- Chunk health: {body.CurrentHealth:F} / {body.InitialHealth:F} - Multiplier: {body.HealthMultiplier:F}\n" +
  154. $"- Cluster health: {cluster?.CurrentHealth:F} / {cluster?.InitialHealth:F} - Multiplier: {cluster?.HealthMultiplier:F}";
  155. }
  156. private bool CheckNoBlocks(Block[] blocks)
  157. {
  158. if (blocks.Length == 0)
  159. {
  160. Logging.CommandLogWarning("No blocks selected. Use selectBlocks first.");
  161. return true;
  162. }
  163. return false;
  164. }
  165. private IEnumerable<SimBody> GetSimBodies(Block[] blocks)
  166. => blocks.Select(block => block.GetSimBody()).Distinct();
  167. private Block[] SelectBlocks(byte id)
  168. {
  169. var blocks = ObjectIdentifier.GetBySimID(id).SelectMany(block => block.GetConnectedCubes()).ToArray();
  170. return blocks;
  171. }
  172. private Block[] SelectBlocks(char id)
  173. {
  174. var blocks = ObjectIdentifier.GetByID(id).SelectMany(oid => oid.GetConnectedCubes())
  175. .ToArray();
  176. return blocks;
  177. }
  178. private void RegisterBlockCommandInternal(string name, string desc, Action<string, Block[], Block> action)
  179. {
  180. RuntimeCommands.Register<string>(name, a1 =>
  181. {
  182. action(a1, blocks, refBlock);
  183. }, desc);
  184. ConsoleCommands.RegisterWithChannel<string>(name, (a1, ch) =>
  185. {
  186. Console.WriteLine($"Command {name} with args {a1} and channel {ch} executing");
  187. var blks = SelectBlocks(ch);
  188. action(a1, blks, blks[0]);
  189. }, ChannelType.Object, desc);
  190. }
  191. private void RegisterBlockCommand(string name, string desc, Action<float, float, float, Block[], Block> action)
  192. {
  193. RegisterBlockCommandInternal(name, desc, (args, bs, b) =>
  194. {
  195. var argsa = args.Split(' ');
  196. if (argsa.Length < 3)
  197. {
  198. Log.Error("Too few arguments. Needed arguments are: <x> <y> <z> and [id] is optional.");
  199. return;
  200. }
  201. if (!float.TryParse(argsa[0], out float x) || !float.TryParse(argsa[1], out float y) ||
  202. !float.TryParse(argsa[2], out float z))
  203. {
  204. Log.Error("Could not parse arguments as floats.");
  205. return;
  206. }
  207. if (argsa.Length > 3)
  208. {
  209. if (argsa[3].Length == 0)
  210. {
  211. Log.Error("Missing channel.");
  212. return;
  213. }
  214. var blocks = SelectBlocks(argsa[3][0]);
  215. if (CheckNoBlocks(blocks)) return;
  216. action(x, y, z, blocks, blocks[0]);
  217. }
  218. else if (!CheckNoBlocks(bs))
  219. action(x, y, z, bs, b);
  220. });
  221. }
  222. private void RegisterBlockCommand(string name, string desc, Action<string, byte, Block[], Block> action)
  223. {
  224. RegisterBlockCommandInternal(name, desc, (args, bs, b) =>
  225. {
  226. var argsa = args.Split(' ');
  227. if (argsa.Length < 2)
  228. {
  229. Log.Error("Too few arguments. Needed arguments are: <color> <darkness> and [id] is optional.");
  230. return;
  231. }
  232. if (!byte.TryParse(argsa[1], out byte darkness))
  233. {
  234. Log.Error("Could not parse color darkness.");
  235. return;
  236. }
  237. if (argsa.Length > 2)
  238. {
  239. if (argsa[2].Length == 0)
  240. {
  241. Log.Error("Missing channel.");
  242. return;
  243. }
  244. var blocks = SelectBlocks(argsa[2][0]);
  245. if (CheckNoBlocks(blocks)) return;
  246. action(argsa[0], darkness, blocks, blocks[0]);
  247. }
  248. else if(!CheckNoBlocks(bs))
  249. action(argsa[0], darkness, bs, b);
  250. });
  251. }
  252. public override string Name { get; } = "BlockMod";
  253. public override string Version { get; } = "v1.0.0";
  254. }
  255. }