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.

PlacementEngine.cs 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. using RobocraftX.Rendering.GPUI;
  18. namespace GamecraftModdingAPI.Blocks
  19. {
  20. /// <summary>
  21. /// Engine which executes block placement actions
  22. /// </summary>
  23. public class PlacementEngine : IApiEngine
  24. {
  25. public bool IsInGame;
  26. public void Dispose()
  27. {
  28. IsInGame = false;
  29. }
  30. public void Ready()
  31. {
  32. IsInGame = true;
  33. }
  34. public EntitiesDB entitiesDB { get; set; }
  35. private static BlockEntityFactory _blockEntityFactory; //Injected from PlaceBlockEngine
  36. public EGID PlaceBlock(BlockIDs block, BlockColors color, byte darkness, float3 position, int uscale,
  37. float3 scale, Player player, float3 rotation, out EntityComponentInitializer initializer)
  38. { //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
  39. if (darkness > 9)
  40. throw new Exception("That is too dark. Make sure to use 0-9 as darkness. (0 is default.)");
  41. initializer = BuildBlock((ushort) block, (byte) (color + darkness * 10), position, uscale, scale, rotation,
  42. (player ?? new Player(PlayerType.Local)).Id);
  43. return initializer.EGID;
  44. }
  45. private EntityComponentInitializer BuildBlock(ushort block, byte color, float3 position, int uscale, float3 scale, float3 rot, uint playerId)
  46. {
  47. if (_blockEntityFactory == null)
  48. throw new BlockException("The factory is null.");
  49. if (uscale < 1)
  50. throw new BlockException("Scale needs to be at least 1");
  51. if (scale.x < 4e-5) scale.x = uscale;
  52. if (scale.y < 4e-5) scale.y = uscale;
  53. if (scale.z < 4e-5) scale.z = uscale;
  54. uint dbid = block;
  55. if (!PrefabsID.HasPrefabRegistered(dbid, 0))
  56. throw new BlockException("Block with ID " + dbid + " not found!");
  57. //RobocraftX.CR.MachineEditing.PlaceBlockEngine
  58. ScalingEntityStruct scaling = new ScalingEntityStruct {scale = scale};
  59. Quaternion rotQ = Quaternion.Euler(rot);
  60. RotationEntityStruct rotation = new RotationEntityStruct {rotation = rotQ};
  61. GridRotationStruct gridRotation = new GridRotationStruct
  62. {position = position, rotation = rotQ};
  63. DBEntityStruct dbEntity = new DBEntityStruct {DBID = dbid};
  64. BlockPlacementScaleEntityStruct placementScale = new BlockPlacementScaleEntityStruct
  65. {
  66. blockPlacementHeight = uscale, blockPlacementWidth = uscale, desiredScaleFactor = uscale
  67. };
  68. EquippedColourStruct colour = new EquippedColourStruct {indexInPalette = color};
  69. EntityComponentInitializer
  70. structInitializer =
  71. _blockEntityFactory.Build(CommonExclusiveGroups.nextBlockEntityID, dbid); //The ghost block index is only used for triggers
  72. if (colour.indexInPalette != byte.MaxValue)
  73. structInitializer.Init(new ColourParameterEntityStruct
  74. {
  75. indexInPalette = colour.indexInPalette,
  76. hasNetworkChange = true
  77. });
  78. uint prefabId = PrefabsID.GetPrefabId(dbid, 0);
  79. structInitializer.Init(new GFXPrefabEntityStructGPUI(prefabId));
  80. structInitializer.Init(new PhysicsPrefabEntityStruct(prefabId));
  81. structInitializer.Init(dbEntity);
  82. structInitializer.Init(new PositionEntityStruct {position = position});
  83. structInitializer.Init(rotation);
  84. structInitializer.Init(scaling);
  85. structInitializer.Init(gridRotation);
  86. structInitializer.Init(new UniformBlockScaleEntityStruct
  87. {
  88. scaleFactor = placementScale.desiredScaleFactor
  89. });
  90. structInitializer.Init(new BlockPlacementInfoStruct()
  91. {
  92. loadedFromDisk = false,
  93. placedBy = playerId
  94. });
  95. /*structInitializer.Init(new CollisionFilterOverride
  96. {
  97. belongsTo = 32U,
  98. collidesWith = 239532U
  99. });*/
  100. PrimaryRotationUtility.InitialisePrimaryDirection(rotation.rotation, ref structInitializer);
  101. EGID playerEGID = new EGID(playerId, CharacterExclusiveGroups.OnFootGroup);
  102. ref PickedBlockExtraDataStruct pickedBlock = ref entitiesDB.QueryEntity<PickedBlockExtraDataStruct>(playerEGID);
  103. pickedBlock.placedBlockEntityID = structInitializer.EGID;
  104. pickedBlock.placedBlockWasAPickedBlock = false;
  105. return structInitializer;
  106. }
  107. public string Name { get; } = "GamecraftModdingAPIPlacementGameEngine";
  108. public bool isRemovable => false;
  109. [HarmonyPatch]
  110. public class FactoryObtainerPatch
  111. {
  112. static void Postfix(BlockEntityFactory blockEntityFactory)
  113. {
  114. _blockEntityFactory = blockEntityFactory;
  115. Logging.MetaDebugLog("Block entity factory injected.");
  116. }
  117. static MethodBase TargetMethod(Harmony instance)
  118. {
  119. return AccessTools.TypeByName("RobocraftX.CR.MachineEditing.PlaceBlockEngine").GetConstructors()[0];
  120. }
  121. }
  122. }
  123. }