using System.Reflection; using DataLoader; using Gamecraft.Blocks.BlockGroups; using Gamecraft.Wires; using HarmonyLib; using RobocraftX.Blocks; using RobocraftX.Character; using RobocraftX.Common; using RobocraftX.CR.MachineEditing.BoxSelect; using RobocraftX.Rendering; using RobocraftX.Rendering.GPUI; using Svelto.ECS; using Svelto.ECS.EntityStructs; using Unity.Mathematics; using TechbloxModdingAPI.Engines; using TechbloxModdingAPI.Utility; using TechbloxModdingAPI.Utility.ECS; namespace TechbloxModdingAPI.Blocks.Engines { /// /// Engine which executes block placement actions /// public class PlacementEngine : IApiEngine { public bool IsInGame; public void Dispose() { IsInGame = false; } public void Ready() { IsInGame = true; } public EntitiesDB entitiesDB { get; set; } private static BlockEntityFactory _blockEntityFactory; //Injected from PlaceSingleBlockEngine private static IEntityFactory _entityFactory; public EntityInitializer PlaceBlock(BlockIDs block, float3 position, Player player, bool autoWire) { //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 return BuildBlock((ushort) block, position, autoWire, (player ?? Player.LocalPlayer).Id); } private EntityInitializer BuildBlock(ushort block, float3 position, bool autoWire, uint playerId) { if (_blockEntityFactory == null) throw new BlockException("The factory is null."); if(!FullGameFields._dataDb.ContainsKey(block)) throw new BlockException("Block with ID " + block + " not found!"); //RobocraftX.CR.MachineEditing.PlaceSingleBlockEngine DBEntityStruct dbEntity = new DBEntityStruct {DBID = block}; EntityInitializer structInitializer = _blockEntityFactory.Build(CommonExclusiveGroups.blockIDGeneratorClient.Next(), block); //The ghost block index is only used for triggers uint prefabAssetID = structInitializer.Has() ? structInitializer.Get().prefabAssetID : throw new BlockException("Prefab asset ID not found!"); //Set by the game uint prefabId = PrefabsID.GetOrAddPrefabID((ushort) prefabAssetID, (byte) BlockMaterial.SteelBodywork, 1, false); structInitializer.Init(new GFXPrefabEntityStructGPUI(prefabId)); structInitializer.Init(dbEntity); structInitializer.Init(new PositionEntityStruct {position = position}); structInitializer.Init(new RotationEntityStruct {rotation = quaternion.identity}); structInitializer.Init(new ScalingEntityStruct {scale = new float3(1, 1, 1)}); structInitializer.Init(new GridRotationStruct { position = position, rotation = quaternion.identity }); structInitializer.Init(new UniformBlockScaleEntityStruct {scaleFactor = 1}); structInitializer.Get().materialId = (byte) BlockMaterial.SteelBodywork; var bssesopt = entitiesDB.QueryEntityOptional(new EGID(playerId, BoxSelectExclusiveGroups.BoxSelectVolumeExclusiveGroup)); if (!bssesopt) throw new BlockException("Invalid player ID specified for block placement"); structInitializer.Init(new BlockPlacementInfoStruct { loadedFromDisk = false, placedByBuildingDrone = bssesopt.Get().buildingDroneReference, triggerAutoWiring = autoWire && structInitializer.Has() }); int nextFilterId = BlockGroupUtility.NextFilterId; structInitializer.Init(new BlockGroupEntityComponent { currentBlockGroup = nextFilterId }); _entityFactory.BuildEntity((uint) nextFilterId, BlockGroupExclusiveGroups.BlockGroupEntityGroup) .Init(new BlockGroupTransformEntityComponent { blockGroupGridRotation = quaternion.identity, blockGroupGridPosition = position }); foreach (var group in CharacterExclusiveGroups.AllCharacters) { EGID playerEGID = new EGID(playerId, group); if (!entitiesDB.TryQueryEntitiesAndIndex(playerEGID, out uint index, out var array)) continue; ref PickedBlockExtraDataStruct pickedBlock = ref array[index]; pickedBlock.placedBlockEntityID = structInitializer.EGID; pickedBlock.placedBlockWasAPickedBlock = false; } return structInitializer; } public string Name => "TechbloxModdingAPIPlacementGameEngine"; public bool isRemovable => false; [HarmonyPatch] class FactoryObtainerPatch { static void Postfix(BlockEntityFactory blockEntityFactory, IEntityFactory entityFactory) { _blockEntityFactory = blockEntityFactory; _entityFactory = entityFactory; Logging.MetaDebugLog("Block entity factory injected."); } static MethodBase TargetMethod(Harmony instance) { return AccessTools.TypeByName("RobocraftX.CR.MachineEditing.PlaceSingleBlockEngine").GetConstructors()[0]; } } } }