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.

151 lines
6.1KB

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