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.

155 lines
5.6KB

  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. RuntimeCommands.Register("placedBy", GetPlacedBy, "Gets who placed a block.");
  30. }
  31. private void GetPlacedBy()
  32. {
  33. try
  34. {
  35. var placementInfo =
  36. entitiesDB.QueryEntity<BlockPlacementInfoStruct>(new EGID(BlockIdentifiers.LatestBlockID,
  37. BlockIdentifiers.OWNED_BLOCKS));
  38. Log.Output("Placed by: " + placementInfo.placedBy);
  39. Log.Output("Loaded from disk: " + placementInfo.loadedFromDisk);
  40. }
  41. catch (Exception e)
  42. {
  43. Log.Error("Failed to get who placed the block.");
  44. Console.WriteLine("Error getting who placed the block:\n" + e);
  45. }
  46. }
  47. public EntitiesDB entitiesDB { get; set; }
  48. private void ImportWorld(string name)
  49. {
  50. try
  51. {
  52. Log.Output("Starting...");
  53. var blocksArray = JsonSerializer.Create()
  54. .Deserialize<Blocks[]>(new JsonTextReader(File.OpenText(name)));
  55. int C = 0;
  56. foreach (var blocks in blocksArray)
  57. {
  58. BlockIDs id;
  59. BlockColors color;
  60. byte darkness = 0;
  61. switch (blocks.Material)
  62. {
  63. case "DIRT":
  64. id = BlockIDs.DirtCube;
  65. color = BlockColors.Default;
  66. break;
  67. case "GRASS":
  68. id = BlockIDs.GrassCube;
  69. color = BlockColors.Default;
  70. break;
  71. case "STONE":
  72. case "COAL_ORE":
  73. case "IRON_ORE":
  74. id = BlockIDs.ConcreteCube;
  75. color = BlockColors.White;
  76. darkness = 5;
  77. break;
  78. case "LEAVES":
  79. id = BlockIDs.AluminiumCube;
  80. color = BlockColors.Green;
  81. darkness = 5;
  82. break;
  83. case "AIR":
  84. continue;
  85. case "LOG":
  86. id = BlockIDs.WoodCube;
  87. color = BlockColors.Default;
  88. break;
  89. case "WATER":
  90. id = BlockIDs.AluminiumCube;
  91. color = BlockColors.Blue;
  92. break;
  93. case "SAND":
  94. id = BlockIDs.AluminiumCube;
  95. color = BlockColors.Yellow;
  96. break;
  97. default:
  98. Console.WriteLine("Unknown block: " + blocks.Material);
  99. continue;
  100. }
  101. Placement.PlaceBlock(id, (blocks.Start + blocks.End) / 10 * 3, color: color, darkness: darkness,
  102. scale: (blocks.End - blocks.Start + 1) * 3, rotation: float3.zero);
  103. C++;
  104. }
  105. Log.Output(C + " blocks placed.");
  106. }
  107. catch (Exception e)
  108. {
  109. Console.WriteLine(e);
  110. Log.Error(e.Message);
  111. }
  112. }
  113. private void PlaceBlock(string args)
  114. {
  115. try
  116. {
  117. var s = args.Split(' ');
  118. ushort block = ushort.Parse(s[0]);
  119. byte color = byte.Parse(s[1]);
  120. byte darkness = byte.Parse(s[2]);
  121. float x = float.Parse(s[3]), y = float.Parse(s[4]), z = float.Parse(s[5]);
  122. int scale = int.Parse(s[6]);
  123. float scaleX = float.Parse(s[7]);
  124. float scaleY = float.Parse(s[8]);
  125. float scaleZ = float.Parse(s[9]);
  126. float rotX = float.Parse(s[10]);
  127. float rotY = float.Parse(s[11]);
  128. float rotZ = float.Parse(s[12]);
  129. uint playerId = 0;
  130. Placement.PlaceBlock((BlockIDs) block, new float3(x, y, z), new float3(rotX, rotY, rotZ),
  131. (BlockColors) color, darkness, scale, new float3(scaleX, scaleY, scaleZ), playerId);
  132. }
  133. catch (Exception e)
  134. {
  135. Console.WriteLine(e);
  136. Log.Error(e.Message);
  137. }
  138. }
  139. public JobHandle SimulatePhysicsStep(in float deltaTime, in PhysicsUtility utility,
  140. in PlayerInput[] playerInputs)
  141. {
  142. return new JobHandle();
  143. }
  144. public string name { get; } = "Cube placer engine";
  145. }
  146. }