A stable modding interface between Techblox and mods https://mod.exmods.org/
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.

144 lines
5.9KB

  1. using System;
  2. using System.Reflection;
  3. using DataLoader;
  4. using HarmonyLib;
  5. using RobocraftX.Blocks;
  6. using RobocraftX.Blocks.Scaling;
  7. using RobocraftX.Character;
  8. using RobocraftX.Common;
  9. using RobocraftX.CR.MachineEditing;
  10. using Svelto.ECS;
  11. using Svelto.ECS.EntityStructs;
  12. using Unity.Mathematics;
  13. using UnityEngine;
  14. using GamecraftModdingAPI.Utility;
  15. using GamecraftModdingAPI.Engines;
  16. using GamecraftModdingAPI.Players;
  17. namespace GamecraftModdingAPI.Blocks
  18. {
  19. /// <summary>
  20. /// Engine which executes block placement actions
  21. /// </summary>
  22. public class PlacementEngine : IApiEngine
  23. {
  24. public bool IsInGame = false;
  25. public void Dispose()
  26. {
  27. IsInGame = false;
  28. }
  29. public void Ready()
  30. {
  31. IsInGame = true;
  32. }
  33. public EntitiesDB entitiesDB { get; set; }
  34. private static BlockEntityFactory _blockEntityFactory; //Injected from PlaceBlockEngine
  35. public EGID PlaceBlock(BlockIDs block, BlockColors color, byte darkness, float3 position, int uscale,
  36. float3 scale, Player player, float3 rotation)
  37. { //It appears that only the non-uniform scale has any visible effect, but if that's not given here it will be set to the uniform one
  38. if (darkness > 9)
  39. throw new Exception("That is too dark. Make sure to use 0-9 as darkness. (0 is default.)");
  40. return BuildBlock((ushort) block, (byte) (color + darkness * 10), position, uscale, scale, rotation,
  41. (player ?? new Player(PlayerType.Local)).Id);
  42. }
  43. private EGID BuildBlock(ushort block, byte color, float3 position, int uscale, float3 scale, float3 rot, uint playerId)
  44. {
  45. if (_blockEntityFactory == null)
  46. throw new Exception("The factory is null.");
  47. if (uscale < 1)
  48. throw new Exception("Scale needs to be at least 1");
  49. if (scale.x < 4e-5) scale.x = uscale;
  50. if (scale.y < 4e-5) scale.y = uscale;
  51. if (scale.z < 4e-5) scale.z = uscale;
  52. uint dbid = block;
  53. if (!PrefabsID.DBIDMAP.ContainsKey(dbid))
  54. throw new Exception("Block with ID " + dbid + " not found!");
  55. //RobocraftX.CR.MachineEditing.PlaceBlockEngine
  56. ScalingEntityStruct scaling = new ScalingEntityStruct {scale = scale};
  57. Quaternion rotQ = Quaternion.Euler(rot);
  58. RotationEntityStruct rotation = new RotationEntityStruct {rotation = rotQ};
  59. GridRotationStruct gridRotation = new GridRotationStruct
  60. {position = position, rotation = rotQ};
  61. CubeCategoryStruct category = new CubeCategoryStruct
  62. {category = CubeCategory.General, type = CubeType.Block};
  63. DBEntityStruct dbEntity = new DBEntityStruct {DBID = dbid};
  64. BlockPlacementScaleEntityStruct placementScale = new BlockPlacementScaleEntityStruct
  65. {
  66. blockPlacementHeight = uscale, blockPlacementWidth = uscale, desiredScaleFactor = uscale,
  67. snapGridScale = uscale,
  68. unitSnapOffset = 0, isUsingUnitSize = true
  69. };
  70. EquippedColourStruct colour = new EquippedColourStruct {indexInPalette = color};
  71. EGID newBlockID;
  72. switch (category.category)
  73. {
  74. case CubeCategory.SpawnPoint:
  75. case CubeCategory.BuildingSpawnPoint:
  76. newBlockID = MachineEditingGroups.NewUncheckedBlockEGID;
  77. break;
  78. default:
  79. newBlockID = MachineEditingGroups.NewBlockID;
  80. break;
  81. }
  82. EntityComponentInitializer
  83. structInitializer =
  84. _blockEntityFactory.Build(newBlockID, dbid); //The ghost block index is only used for triggers
  85. if (colour.indexInPalette != byte.MaxValue)
  86. structInitializer.Init(new ColourParameterEntityStruct
  87. {
  88. indexInPalette = colour.indexInPalette,
  89. needsUpdate = true
  90. });
  91. uint prefabId = PrefabsID.GetPrefabId(dbid, 0);
  92. structInitializer.Init(new GFXPrefabEntityStructGPUI(prefabId));
  93. structInitializer.Init(new PhysicsPrefabEntityStruct(prefabId));
  94. structInitializer.Init(dbEntity);
  95. structInitializer.Init(new PositionEntityStruct {position = position});
  96. structInitializer.Init(rotation);
  97. structInitializer.Init(scaling);
  98. structInitializer.Init(gridRotation);
  99. structInitializer.Init(new UniformBlockScaleEntityStruct
  100. {
  101. scaleFactor = placementScale.desiredScaleFactor
  102. });
  103. structInitializer.Init(new BlockPlacementInfoStruct()
  104. {
  105. loadedFromDisk = false,
  106. placedBy = playerId
  107. });
  108. PrimaryRotationUtility.InitialisePrimaryDirection(rotation.rotation, ref structInitializer);
  109. EGID playerEGID = new EGID(playerId, CharacterExclusiveGroups.OnFootGroup);
  110. ref PickedBlockExtraDataStruct pickedBlock = ref entitiesDB.QueryEntity<PickedBlockExtraDataStruct>(playerEGID);
  111. pickedBlock.placedBlockEntityID = playerEGID;
  112. pickedBlock.placedBlockWasAPickedBlock = false;
  113. return newBlockID;
  114. }
  115. public string Name { get; } = "GamecraftModdingAPIPlacementGameEngine";
  116. public bool isRemovable => false;
  117. [HarmonyPatch]
  118. public class FactoryObtainerPatch
  119. {
  120. static void Postfix(BlockEntityFactory blockEntityFactory)
  121. {
  122. _blockEntityFactory = blockEntityFactory;
  123. Logging.MetaDebugLog("Block entity factory injected.");
  124. }
  125. static MethodBase TargetMethod(Harmony instance)
  126. {
  127. return AccessTools.TypeByName("RobocraftX.CR.MachineEditing.PlaceBlockEngine").GetConstructors()[0];
  128. }
  129. }
  130. }
  131. }