|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- using RobocraftX.Blocks;
- using Gamecraft.Wires;
- using Svelto.ECS;
-
- using GamecraftModdingAPI.Engines;
-
-
- namespace GamecraftModdingAPI.Blocks
- {
- public class TweakableEngine : IApiEngine
- {
- public string Name { get; } = "GamecraftModdingAPITweakableGameEngine";
-
- public EntitiesDB entitiesDB { set; private get; }
-
- public bool isRemovable => false;
-
- public bool IsInGame = false;
-
- public void Dispose()
- {
- IsInGame = false;
- }
-
- public void Ready()
- {
- IsInGame = true;
- }
-
- // Implementations for Tweakable static class
-
- public T GetStatAny<T>(EGID blockID, TweakableStat stat)
- {
- switch (stat)
- {
- case TweakableStat.TopSpeed:
- if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
- {
- return (T)(object)entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID).maxVelocity;
- }
- break;
- case TweakableStat.Torque:
- if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
- {
- return (T)(object)entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID).maxForce;
- }
- break;
- case TweakableStat.MaxExtension:
- if (entitiesDB.Exists<PistonReadOnlyStruct>(blockID))
- {
- return (T)(object)entitiesDB.QueryEntity<PistonReadOnlyStruct>(blockID).maxDeviation;
- }
- break;
- case TweakableStat.MinAngle:
- if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
- {
- return (T)(object)entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID).minDeviation;
- }
- break;
- case TweakableStat.MaxAngle:
- if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
- {
- return (T)(object)entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID).maxDeviation;
- }
- break;
- case TweakableStat.Reverse:
- if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
- {
- return (T)(object)entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID).reverse;
- }
- else if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
- {
- return (T)(object)entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID).reverse;
- }
- break;
- case TweakableStat.StartValue:
- if (entitiesDB.Exists<SignalGeneratorEntityStruct>(blockID))
- {
- return (T)(object)entitiesDB.QueryEntity<SignalGeneratorEntityStruct>(blockID).startValue;
- }
- break;
- }
- return default(T);
- }
-
- public T GetStatAny<T>(uint blockID, TweakableStat stat)
- {
- return GetStatAny<T>(new EGID(blockID, BlockIdentifiers.OWNED_BLOCKS), stat);
- }
-
- public dynamic GetStatDynamic(EGID blockID, TweakableStat stat)
- {
- switch (stat)
- {
- case TweakableStat.TopSpeed:
- if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
- {
- return entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID).maxVelocity;
- }
- break;
- case TweakableStat.Torque:
- if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
- {
- return entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID).maxForce;
- }
- break;
- case TweakableStat.MaxExtension:
- if (entitiesDB.Exists<PistonReadOnlyStruct>(blockID))
- {
- return entitiesDB.QueryEntity<PistonReadOnlyStruct>(blockID).maxDeviation;
- }
- break;
- case TweakableStat.MinAngle:
- if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
- {
- return entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID).minDeviation;
- }
- break;
- case TweakableStat.MaxAngle:
- if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
- {
- return entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID).maxDeviation;
- }
- break;
- case TweakableStat.Reverse:
- if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
- {
- return entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID).reverse;
- }
- else if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
- {
- return entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID).reverse;
- }
- break;
- case TweakableStat.StartValue:
- if (entitiesDB.Exists<SignalGeneratorEntityStruct>(blockID))
- {
- return entitiesDB.QueryEntity<SignalGeneratorEntityStruct>(blockID).startValue;
- }
- break;
- }
- return null;
- }
-
- public dynamic GetStatDynamic(uint blockID, TweakableStat stat)
- {
- return GetStatDynamic(new EGID(blockID, BlockIdentifiers.OWNED_BLOCKS), stat);
- }
-
- public T SetStatAny<T>(EGID blockID, TweakableStat stat, T value)
- {
- switch (stat)
- {
- case TweakableStat.TopSpeed:
- if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
- {
- ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
- refStruct.maxVelocity = (float)(object)value;
- return (T)(object)refStruct.maxVelocity;
- }
- break;
- case TweakableStat.Torque:
- if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
- {
- ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
- refStruct.maxForce = (float)(object)value;
- return (T)(object)refStruct.maxForce;
- }
- break;
- case TweakableStat.MaxExtension:
- if (entitiesDB.Exists<PistonReadOnlyStruct>(blockID))
- {
- ref PistonReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<PistonReadOnlyStruct>(blockID);
- refStruct.maxDeviation = (float)(object)value;
- return (T)(object)refStruct.maxDeviation;
- }
- break;
- case TweakableStat.MinAngle:
- if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
- {
- ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
- refStruct.minDeviation = (float)(object)value;
- return (T)(object)refStruct.minDeviation;
- }
- break;
- case TweakableStat.MaxAngle:
- if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
- {
- ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
- refStruct.maxDeviation = (float)(object)value;
- return (T)(object)refStruct.maxDeviation;
- }
- break;
- case TweakableStat.Reverse:
- if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
- {
- ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
- refStruct.reverse = (bool)(object)value;
- return (T)(object)refStruct.reverse;
- }
- else if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
- {
- ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
- refStruct.reverse = (bool)(object)value;
- return (T)(object)refStruct.reverse;
- }
- break;
- case TweakableStat.StartValue:
- if (entitiesDB.Exists<SignalGeneratorEntityStruct>(blockID))
- {
- ref SignalGeneratorEntityStruct refStruct = ref entitiesDB.QueryEntity<SignalGeneratorEntityStruct>(blockID);
- refStruct.startValue = (float)(object)value;
- return (T)(object)refStruct.startValue;
- }
- break;
- }
- return default(T);
- }
-
- public T SetStatAny<T>(uint blockID, TweakableStat stat, T value)
- {
- return SetStatAny<T>(new EGID(blockID, BlockIdentifiers.OWNED_BLOCKS), stat, value);
- }
-
- public dynamic SetStatDynamic(EGID blockID, TweakableStat stat, dynamic value)
- {
- switch (stat)
- {
- case TweakableStat.TopSpeed:
- if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
- {
- ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
- refStruct.maxVelocity = value;
- return refStruct.maxVelocity;
- }
- break;
- case TweakableStat.Torque:
- if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
- {
- ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
- refStruct.maxForce = value;
- return refStruct.maxForce;
- }
- break;
- case TweakableStat.MaxExtension:
- if (entitiesDB.Exists<PistonReadOnlyStruct>(blockID))
- {
- ref PistonReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<PistonReadOnlyStruct>(blockID);
- refStruct.maxDeviation = value;
- return refStruct.maxDeviation;
- }
- break;
- case TweakableStat.MinAngle:
- if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
- {
- ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
- refStruct.minDeviation = value;
- return refStruct.minDeviation;
- }
- break;
- case TweakableStat.MaxAngle:
- if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
- {
- ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
- refStruct.maxDeviation = value;
- return refStruct.maxDeviation;
- }
- break;
- case TweakableStat.Reverse:
- if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
- {
- ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
- refStruct.reverse = value;
- return refStruct.reverse;
- }
- else if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
- {
- ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
- refStruct.reverse = value;
- return refStruct.reverse;
- }
- break;
- case TweakableStat.StartValue:
- if (entitiesDB.Exists<SignalGeneratorEntityStruct>(blockID))
- {
- ref SignalGeneratorEntityStruct refStruct = ref entitiesDB.QueryEntity<SignalGeneratorEntityStruct>(blockID);
- refStruct.startValue = value;
- return refStruct.startValue;
- }
- break;
- }
- return null;
- }
-
- public dynamic SetStatDynamic(uint blockID, TweakableStat stat, dynamic value)
- {
- return SetStatDynamic(new EGID(blockID, BlockIdentifiers.OWNED_BLOCKS), stat, value);
- }
-
- public T AddStatAny<T>(EGID blockID, TweakableStat stat, T value)
- {
- switch (stat)
- {
- case TweakableStat.TopSpeed:
- if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
- {
- ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
- refStruct.maxVelocity += (float)(object)value;
- return (T)(object)refStruct.maxVelocity;
- }
- break;
- case TweakableStat.Torque:
- if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
- {
- ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
- refStruct.maxForce += (float)(object)value;
- return (T)(object)refStruct.maxForce;
- }
- break;
- case TweakableStat.MaxExtension:
- if (entitiesDB.Exists<PistonReadOnlyStruct>(blockID))
- {
- ref PistonReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<PistonReadOnlyStruct>(blockID);
- refStruct.maxDeviation += (float)(object)value;
- return (T)(object)refStruct.maxDeviation;
- }
- break;
- case TweakableStat.MinAngle:
- if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
- {
- ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
- refStruct.minDeviation += (float)(object)value;
- return (T)(object)refStruct.minDeviation;
- }
- break;
- case TweakableStat.MaxAngle:
- if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
- {
- ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
- refStruct.maxDeviation += (float)(object)value;
- return (T)(object)refStruct.maxDeviation;
- }
- break;
- case TweakableStat.Reverse:
- // '+' is associated with logical OR in some fields, so it technically isn't invalid to "add" booleans
- if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
- {
- ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
- refStruct.reverse = refStruct.reverse || (bool)(object)value;
- return (T)(object)refStruct.reverse;
- }
- else if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
- {
- ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
- refStruct.reverse = refStruct.reverse || (bool)(object)value;
- return (T)(object)refStruct.reverse;
- }
- break;
- case TweakableStat.StartValue:
- if (entitiesDB.Exists<SignalGeneratorEntityStruct>(blockID))
- {
- ref SignalGeneratorEntityStruct refStruct = ref entitiesDB.QueryEntity<SignalGeneratorEntityStruct>(blockID);
- refStruct.startValue += (float)(object)value;
- return (T)(object)refStruct.startValue;
- }
- break;
- }
- return default(T);
- }
-
- public T AddStatAny<T>(uint blockID, TweakableStat stat, T value)
- {
- return AddStatAny<T>(new EGID(blockID, BlockIdentifiers.OWNED_BLOCKS), stat, value);
- }
-
- public dynamic AddStatDynamic(EGID blockID, TweakableStat stat, dynamic value)
- {
- switch (stat)
- {
- case TweakableStat.TopSpeed:
- if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
- {
- ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
- refStruct.maxVelocity += value;
- return refStruct.maxVelocity;
- }
- break;
- case TweakableStat.Torque:
- if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
- {
- ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
- refStruct.maxForce += value;
- return refStruct.maxForce;
- }
- break;
- case TweakableStat.MaxExtension:
- if (entitiesDB.Exists<PistonReadOnlyStruct>(blockID))
- {
- ref PistonReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<PistonReadOnlyStruct>(blockID);
- refStruct.maxDeviation += value;
- return refStruct.maxDeviation;
- }
- break;
- case TweakableStat.MinAngle:
- if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
- {
- ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
- refStruct.minDeviation += value;
- return refStruct.minDeviation;
- }
- break;
- case TweakableStat.MaxAngle:
- if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
- {
- ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
- refStruct.maxDeviation += value;
- return refStruct.maxDeviation;
- }
- break;
- case TweakableStat.Reverse:
- // '+' is associated with logical OR in some fields, so it technically isn't invalid to "add" booleans
- if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
- {
- ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
- refStruct.reverse = refStruct.reverse || value;
- return refStruct.reverse;
- }
- else if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
- {
- ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
- refStruct.reverse = refStruct.reverse || value;
- return refStruct.reverse;
- }
- break;
- case TweakableStat.StartValue:
- if (entitiesDB.Exists<SignalGeneratorEntityStruct>(blockID))
- {
- ref SignalGeneratorEntityStruct refStruct = ref entitiesDB.QueryEntity<SignalGeneratorEntityStruct>(blockID);
- refStruct.startValue += value;
- return refStruct.startValue;
- }
- break;
- }
- return null;
- }
-
- public dynamic AddStatDynamic(uint blockID, TweakableStat stat, dynamic value)
- {
- return AddStatDynamic(new EGID(blockID, BlockIdentifiers.OWNED_BLOCKS), stat, value);
- }
- }
- }
|