Magically import images and more into Gamecraft as blocks
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.

100 lines
5.0KB

  1. using System;
  2. using System.Collections.Generic;
  3. using Svelto.DataStructures;
  4. using Unity.Mathematics;
  5. using UnityEngine;
  6. using GamecraftModdingAPI.Blocks;
  7. using GamecraftModdingAPI.Utility;
  8. using Pixi.Common;
  9. namespace Pixi.Robots
  10. {
  11. public class RobotBlueprintProvider : BlueprintProvider
  12. {
  13. public string Name { get; } = "RobotBlueprintProvider";
  14. private Dictionary<string, BlockJsonInfo[]> botprints = null;
  15. private RobotInternetImporter parent;
  16. public RobotBlueprintProvider(RobotInternetImporter rii)
  17. {
  18. parent = rii;
  19. }
  20. public BlockJsonInfo[] Blueprint(string name, BlockJsonInfo root)
  21. {
  22. if (botprints == null)
  23. {
  24. botprints = BlueprintUtility.ParseBlueprintResource("Pixi.blueprints.json");
  25. }
  26. if (!botprints.ContainsKey(root.name) || RobotInternetImporter.CubeSize != 3)
  27. {
  28. if (!parent.textBlockInfo.ContainsKey(name))
  29. {
  30. parent.textBlockInfo[name] = new FasterList<string>();
  31. }
  32. BlockJsonInfo copy = root;
  33. copy.name = "TextBlock";
  34. Logging.MetaLog($"Parsing uint from '{root.name}'");
  35. parent.textBlockInfo[name].Add(root.name + " (" + CubeUtility.CubeIdDescription(uint.Parse(root.name)) + ")");
  36. return new BlockJsonInfo[1] {copy};
  37. }
  38. BlockJsonInfo[] blueprint = botprints[root.name];
  39. BlockJsonInfo[] adjustedBlueprint = new BlockJsonInfo[blueprint.Length];
  40. Quaternion cubeQuaternion = Quaternion.Euler(ConversionUtility.FloatArrayToFloat3(root.rotation));
  41. if (blueprint.Length == 0)
  42. {
  43. Logging.LogWarning($"Found empty blueprint for {root.name} (during '{name}'), is the blueprint correct?");
  44. return new BlockJsonInfo[0];
  45. }
  46. // move blocks to correct position & rotation
  47. float3 defaultCorrectionVec = new float3((float)(0), (float)(CommandRoot.BLOCK_SIZE), (float)(0));
  48. float3 baseRot = new float3(blueprint[0].rotation[0], blueprint[0].rotation[1], blueprint[0].rotation[2]);
  49. float3 baseScale = new float3(blueprint[0].scale[0], blueprint[0].scale[1], blueprint[0].scale[2]);
  50. //Block[] placedBlocks = new Block[blueprint.Length];
  51. bool isBaseScaled = !(blueprint[0].scale[1] > 0f && blueprint[0].scale[1] < 2f);
  52. float3 correctionVec = isBaseScaled ? (float3)(Quaternion.Euler(baseRot) * baseScale / 2) * (float)-CommandRoot.BLOCK_SIZE : -defaultCorrectionVec;
  53. // FIXME scaled base blocks cause the blueprint to be placed in the wrong location (this also could be caused by a bug in DumpVON command)
  54. if (isBaseScaled)
  55. {
  56. Logging.LogWarning($"Found blueprint with scaled base block for {root.name} (during '{name}'), this is not currently supported");
  57. }
  58. float3 rootPos = ConversionUtility.FloatArrayToFloat3(root.position);
  59. for (int i = 0; i < blueprint.Length; i++)
  60. {
  61. BlockColor blueprintBlockColor = ColorSpaceUtility.QuantizeToBlockColor(blueprint[i].color);
  62. float[] physicalColor = blueprintBlockColor.Color == BlockColors.White && blueprintBlockColor.Darkness == 0 ? root.color : blueprint[i].color;
  63. float3 bluePos = ConversionUtility.FloatArrayToFloat3(blueprint[i].position);
  64. float3 blueScale = ConversionUtility.FloatArrayToFloat3(blueprint[i].scale);
  65. float3 blueRot = ConversionUtility.FloatArrayToFloat3(blueprint[i].rotation);
  66. float3 physicalLocation = (float3)(cubeQuaternion * bluePos) + rootPos;// + (blueprintSizeRotated / 2);
  67. //physicalLocation.x += blueprintSize.x / 2;
  68. physicalLocation += (float3)(cubeQuaternion * (correctionVec));
  69. //physicalLocation.y -= (float)(RobotCommands.blockSize * scale / 2);
  70. //float3 physicalScale = (float3)(cubeQuaternion * blueScale); // this actually over-rotates when combined with rotation
  71. float3 physicalScale = blueScale;
  72. float3 physicalRotation = (cubeQuaternion * Quaternion.Euler(blueRot)).eulerAngles;
  73. #if DEBUG
  74. Logging.MetaLog($"Placing blueprint block at {physicalLocation} rot{physicalRotation} scale{physicalScale}");
  75. Logging.MetaLog($"Location math check original:{bluePos} rotated: {(float3)(cubeQuaternion * bluePos)} actualPos: {rootPos} result: {physicalLocation}");
  76. Logging.MetaLog($"Scale math check original:{blueScale} rotation: {(float3)cubeQuaternion.eulerAngles} result: {physicalScale}");
  77. Logging.MetaLog($"Rotation math check original:{blueRot} rotated: {(cubeQuaternion * Quaternion.Euler(blueRot))} result: {physicalRotation}");
  78. #endif
  79. adjustedBlueprint[i] = new BlockJsonInfo
  80. {
  81. color = physicalColor,
  82. name = blueprint[i].name,
  83. position = ConversionUtility.Float3ToFloatArray(physicalLocation),
  84. rotation = ConversionUtility.Float3ToFloatArray(physicalRotation),
  85. scale = ConversionUtility.Float3ToFloatArray(physicalScale)
  86. };
  87. }
  88. return adjustedBlueprint;
  89. }
  90. }
  91. }