2020-01-03 00:14:26 +00:00
|
|
|
using System;
|
|
|
|
using System.Reflection;
|
2020-01-04 00:54:35 +00:00
|
|
|
|
2020-01-03 00:14:26 +00:00
|
|
|
using DataLoader;
|
|
|
|
using Harmony;
|
|
|
|
using RobocraftX.Blocks;
|
|
|
|
using RobocraftX.Blocks.Ghost;
|
|
|
|
using RobocraftX.Blocks.Scaling;
|
|
|
|
using RobocraftX.Character;
|
|
|
|
using RobocraftX.CommandLine.Custom;
|
|
|
|
using RobocraftX.Common;
|
|
|
|
using RobocraftX.Common.Input;
|
|
|
|
using RobocraftX.Common.Utilities;
|
|
|
|
using RobocraftX.CR.MachineEditing;
|
|
|
|
using RobocraftX.StateSync;
|
|
|
|
using Svelto.ECS;
|
|
|
|
using Svelto.ECS.EntityStructs;
|
|
|
|
using Unity.Jobs;
|
|
|
|
using Unity.Mathematics;
|
|
|
|
using UnityEngine;
|
|
|
|
using uREPL;
|
|
|
|
|
2020-01-04 00:54:35 +00:00
|
|
|
using GamecraftModdingAPI.Utility;
|
|
|
|
|
|
|
|
namespace GamecraftModdingAPI.Blocks
|
2020-01-03 00:14:26 +00:00
|
|
|
{
|
2020-01-04 00:54:35 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Engine which executes block placement actions
|
|
|
|
/// </summary>
|
2020-01-03 00:14:26 +00:00
|
|
|
public class PlacementEngine : IApiEngine
|
|
|
|
{
|
|
|
|
public bool IsInGame = false;
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
IsInGame = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Ready()
|
|
|
|
{
|
|
|
|
IsInGame = true;
|
|
|
|
}
|
|
|
|
|
2020-03-12 22:36:23 +00:00
|
|
|
public EntitiesDB entitiesDB { get; set; }
|
2020-01-03 00:14:26 +00:00
|
|
|
internal static BlockEntityFactory _blockEntityFactory; //Injected from PlaceBlockEngine
|
|
|
|
|
2020-01-03 13:38:59 +00:00
|
|
|
public void PlaceBlock(BlockIDs block, BlockColors color, byte darkness, float3 position, int uscale,
|
2020-01-15 19:41:50 +00:00
|
|
|
float3 scale, uint playerId, float3 rotation)
|
2020-01-03 00:14:26 +00:00
|
|
|
{ //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
|
2020-01-04 00:54:35 +00:00
|
|
|
if (darkness > 9)
|
|
|
|
throw new Exception("That is too dark. Make sure to use 0-9 as darkness. (0 is default.)");
|
|
|
|
BuildBlock((ushort) block, (byte) (color + darkness * 10), position, uscale, scale, rotation).Init(
|
|
|
|
new BlockPlacementInfoStruct()
|
|
|
|
{
|
|
|
|
loadedFromDisk = false,
|
|
|
|
placedBy = playerId
|
|
|
|
});
|
2020-01-03 00:14:26 +00:00
|
|
|
}
|
|
|
|
|
2020-01-15 19:41:50 +00:00
|
|
|
private EntityStructInitializer BuildBlock(ushort block, byte color, float3 position, int uscale, float3 scale, float3 rot)
|
2020-01-03 00:14:26 +00:00
|
|
|
{
|
|
|
|
if (_blockEntityFactory == null)
|
|
|
|
throw new Exception("The factory is null.");
|
|
|
|
if (uscale < 1)
|
|
|
|
throw new Exception("Scale needs to be at least 1");
|
2020-01-03 13:38:59 +00:00
|
|
|
if (scale.x < 4e-5) scale.x = uscale;
|
|
|
|
if (scale.y < 4e-5) scale.y = uscale;
|
|
|
|
if (scale.z < 4e-5) scale.z = uscale;
|
2020-02-08 21:05:16 +00:00
|
|
|
uint dbid = block;
|
|
|
|
if (!PrefabsID.DBIDMAP.ContainsKey(dbid))
|
|
|
|
throw new Exception("Block with ID " + dbid + " not found!");
|
2020-01-03 00:14:26 +00:00
|
|
|
//RobocraftX.CR.MachineEditing.PlaceBlockEngine
|
|
|
|
ScalingEntityStruct scaling = new ScalingEntityStruct {scale = scale};
|
2020-01-15 19:41:50 +00:00
|
|
|
Quaternion rotQ = Quaternion.Euler(rot);
|
|
|
|
RotationEntityStruct rotation = new RotationEntityStruct {rotation = rotQ};
|
2020-01-03 00:14:26 +00:00
|
|
|
GridRotationStruct gridRotation = new GridRotationStruct
|
2020-01-15 19:41:50 +00:00
|
|
|
{position = position, rotation = rotQ};
|
2020-01-03 00:14:26 +00:00
|
|
|
CubeCategoryStruct category = new CubeCategoryStruct
|
|
|
|
{category = CubeCategory.General, type = CubeType.Block};
|
|
|
|
DBEntityStruct dbEntity = new DBEntityStruct {DBID = dbid};
|
|
|
|
uint num = PrefabsID.DBIDMAP[dbid];
|
|
|
|
GFXPrefabEntityStructGO gfx = new GFXPrefabEntityStructGO {prefabID = num};
|
|
|
|
BlockPlacementScaleEntityStruct placementScale = new BlockPlacementScaleEntityStruct
|
|
|
|
{
|
|
|
|
blockPlacementHeight = uscale, blockPlacementWidth = uscale, desiredScaleFactor = uscale,
|
|
|
|
snapGridScale = uscale,
|
|
|
|
unitSnapOffset = 0, isUsingUnitSize = true
|
|
|
|
};
|
|
|
|
EquippedColourStruct colour = new EquippedColourStruct {indexInPalette = color};
|
|
|
|
EGID egid2;
|
|
|
|
switch (category.category)
|
|
|
|
{
|
|
|
|
case CubeCategory.SpawnPoint:
|
|
|
|
case CubeCategory.BuildingSpawnPoint:
|
|
|
|
egid2 = MachineEditingGroups.NewSpawnPointBlockID;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
egid2 = MachineEditingGroups.NewBlockID;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cubeId = PrefabsID.GenerateDBID((ushort) category.category, block);
|
|
|
|
EntityStructInitializer
|
|
|
|
structInitializer =
|
|
|
|
_blockEntityFactory.Build(egid2, (uint) cubeId); //The ghost block index is only used for triggers
|
|
|
|
if (colour.indexInPalette != byte.MaxValue)
|
|
|
|
structInitializer.Init(new ColourParameterEntityStruct
|
|
|
|
{
|
|
|
|
indexInPalette = colour.indexInPalette
|
|
|
|
});
|
|
|
|
structInitializer.Init(new GFXPrefabEntityStructGPUI(gfx.prefabID));
|
|
|
|
structInitializer.Init(new PhysicsPrefabEntityStruct(gfx.prefabID));
|
|
|
|
structInitializer.Init(dbEntity);
|
|
|
|
structInitializer.Init(new PositionEntityStruct {position = position});
|
|
|
|
structInitializer.Init(rotation);
|
|
|
|
structInitializer.Init(scaling);
|
|
|
|
structInitializer.Init(gridRotation);
|
|
|
|
structInitializer.Init(new UniformBlockScaleEntityStruct
|
|
|
|
{
|
|
|
|
scaleFactor = placementScale.desiredScaleFactor
|
|
|
|
});
|
|
|
|
return structInitializer;
|
|
|
|
}
|
|
|
|
|
2020-01-04 00:54:35 +00:00
|
|
|
public string Name { get; } = "GamecraftModdingAPIPlacementGameEngine";
|
2020-01-03 00:14:26 +00:00
|
|
|
|
|
|
|
[HarmonyPatch]
|
|
|
|
public class FactoryObtainerPatch
|
|
|
|
{
|
|
|
|
static void Postfix(BlockEntityFactory blockEntityFactory)
|
|
|
|
{
|
|
|
|
_blockEntityFactory = blockEntityFactory;
|
2020-01-04 00:54:35 +00:00
|
|
|
Logging.MetaDebugLog("Block entity factory injected.");
|
2020-01-03 00:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static MethodBase TargetMethod(HarmonyInstance instance)
|
|
|
|
{
|
|
|
|
return typeof(PlaceBlockEngine).GetConstructors()[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|