Minecraft world importer for Gamecraft.
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.

124 lines
4.3KB

  1. using System;
  2. using System.IO;
  3. using DataLoader;
  4. using GamecraftModdingAPI.Blocks;
  5. using Newtonsoft.Json;
  6. using RobocraftX.Blocks;
  7. using RobocraftX.Blocks.Ghost;
  8. using RobocraftX.Blocks.Scaling;
  9. using RobocraftX.Character;
  10. using RobocraftX.CommandLine.Custom;
  11. using RobocraftX.Common;
  12. using RobocraftX.Common.Input;
  13. using RobocraftX.Common.Utilities;
  14. using RobocraftX.CR.MachineEditing;
  15. using RobocraftX.StateSync;
  16. using Svelto.ECS;
  17. using Svelto.ECS.EntityStructs;
  18. using Unity.Jobs;
  19. using Unity.Mathematics;
  20. using uREPL;
  21. namespace GCMC
  22. {
  23. public class CubePlacerEngine : IQueryingEntitiesEngine, IDeterministicSim
  24. {
  25. public void Ready()
  26. {
  27. RuntimeCommands.Register<string>("importWorld", ImportWorld, "Imports a Minecraft world.");
  28. RuntimeCommands.Register<string>("placeCube", PlaceBlock, "Places a cube.");
  29. }
  30. public IEntitiesDB entitiesDB { get; set; }
  31. private void ImportWorld(string name)
  32. {
  33. try
  34. {
  35. Log.Output("Starting...");
  36. var blocksArray = JsonSerializer.Create()
  37. .Deserialize<Blocks[]>(new JsonTextReader(File.OpenText(name)));
  38. int C = 0;
  39. foreach (var blocks in blocksArray)
  40. {
  41. BlockIDs id;
  42. BlockColors color;
  43. byte darkness = 0;
  44. switch (blocks.Material)
  45. {
  46. case "DIRT":
  47. id = BlockIDs.DirtCube;
  48. color = BlockColors.Default;
  49. break;
  50. case "GRASS":
  51. id = BlockIDs.GrassCube;
  52. color = BlockColors.Default;
  53. break;
  54. case "STONE":
  55. id = BlockIDs.ConcreteCube;
  56. color = BlockColors.White;
  57. darkness = 5;
  58. break;
  59. case "LEAVES":
  60. id = BlockIDs.AluminiumCube;
  61. color = BlockColors.Green;
  62. darkness = 5;
  63. break;
  64. case "AIR":
  65. continue;
  66. case "LOG":
  67. id = BlockIDs.WoodCube;
  68. color = BlockColors.Default;
  69. break;
  70. default:
  71. Log.Output("Unknown block: " + blocks.Material);
  72. continue;
  73. }
  74. Placement.PlaceBlock(id, (blocks.Start + blocks.End) / 2, color: color, darkness: darkness,
  75. scale: (blocks.End - blocks.Start + 1) * 5, rotation: quaternion.identity);
  76. C++;
  77. }
  78. Log.Output(C + " blocks placed.");
  79. }
  80. catch (Exception e)
  81. {
  82. Console.WriteLine(e);
  83. Log.Error(e.Message);
  84. }
  85. }
  86. private void PlaceBlock(string args)
  87. {
  88. try
  89. {
  90. var s = args.Split(' ');
  91. ushort block = ushort.Parse(s[0]);
  92. byte color = byte.Parse(s[1]);
  93. byte darkness = byte.Parse(s[2]);
  94. float x = float.Parse(s[3]), y = float.Parse(s[4]), z = float.Parse(s[5]);
  95. int scale = int.Parse(s[6]);
  96. float scaleX = float.Parse(s[7]);
  97. float scaleY = float.Parse(s[8]);
  98. float scaleZ = float.Parse(s[9]);
  99. float rotX = float.Parse(s[10]);
  100. uint playerId = 0;
  101. Placement.PlaceBlock((BlockIDs) block, new float3(x, y, z), new quaternion(rotX, 0, 0, 1),
  102. (BlockColors) color, darkness, scale, new float3(scaleX, scaleY, scaleZ), playerId);
  103. }
  104. catch (Exception e)
  105. {
  106. Console.WriteLine(e);
  107. Log.Error(e.Message);
  108. }
  109. }
  110. public JobHandle SimulatePhysicsStep(in float deltaTime, in PhysicsUtility utility, in PlayerInput[] playerInputs)
  111. {
  112. return new JobHandle();
  113. }
  114. public string name { get; } = "Cube placer engine";
  115. }
  116. }