158 lines
4.7 KiB
C#
158 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
using RobocraftX;
|
|
using RobocraftX.Blocks;
|
|
using RobocraftX.Blocks.Ghost;
|
|
using RobocraftX.Common;
|
|
using RobocraftX.Multiplayer;
|
|
using RobocraftX.SimulationModeState;
|
|
using RobocraftX.UECS;
|
|
using Unity.Entities;
|
|
using Svelto.Context;
|
|
using Svelto.DataStructures;
|
|
using Svelto.ECS;
|
|
using Svelto.ECS.EntityStructs;
|
|
using Unity.Transforms;
|
|
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
using Gamecraft.Wires;
|
|
|
|
using GamecraftModdingAPI.Utility;
|
|
|
|
namespace GamecraftModdingAPI.Blocks
|
|
{
|
|
/// <summary>
|
|
/// Engine which executes signal actions
|
|
/// </summary>
|
|
public class SignalEngine : IApiEngine
|
|
{
|
|
public string Name { get; } = "GamecraftModdingAPISignalGameEngine";
|
|
|
|
public EntitiesDB entitiesDB { set; private get; }
|
|
|
|
public bool IsInGame = false;
|
|
|
|
private System.Random random = new System.Random();
|
|
|
|
public void Dispose()
|
|
{
|
|
IsInGame = false;
|
|
}
|
|
|
|
public void Ready()
|
|
{
|
|
IsInGame = true;
|
|
}
|
|
|
|
// implementations for Signal static class
|
|
|
|
public bool SetSignal(EGID blockID, float signal, out uint signalID, bool input = true)
|
|
{
|
|
signalID = GetSignalIDs(blockID, input)[0];
|
|
return SetSignal(signalID, signal);
|
|
}
|
|
|
|
public bool SetSignal(uint signalID, float signal, bool input = true)
|
|
{
|
|
ExclusiveGroup group = input ? NamedExclusiveGroup<InputPortsGroup>.Group : NamedExclusiveGroup<OutputPortsGroup>.Group;
|
|
if (entitiesDB.Exists<PortEntityStruct>(signalID, group))
|
|
{
|
|
entitiesDB.QueryEntity<PortEntityStruct>(signalID, group).value = signal;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public float AddSignal(EGID blockID, float signal, out uint signalID, bool clamp = true, bool input = true)
|
|
{
|
|
signalID = GetSignalIDs(blockID, input)[0];
|
|
return AddSignal(signalID, signal, clamp, input);
|
|
}
|
|
|
|
public float AddSignal(uint signalID, float signal, bool clamp = true, bool input = true)
|
|
{
|
|
ExclusiveGroup group = input ? NamedExclusiveGroup<InputPortsGroup>.Group : NamedExclusiveGroup<OutputPortsGroup>.Group;
|
|
if (entitiesDB.Exists<PortEntityStruct>(signalID, group))
|
|
{
|
|
ref PortEntityStruct pes = ref entitiesDB.QueryEntity<PortEntityStruct>(signalID, group);
|
|
pes.value += signal;
|
|
if (clamp)
|
|
{
|
|
if (pes.value > Signals.POSITIVE_HIGH)
|
|
{
|
|
pes.value = Signals.POSITIVE_HIGH;
|
|
}
|
|
else if (pes.value < Signals.NEGATIVE_HIGH)
|
|
{
|
|
pes.value = Signals.NEGATIVE_HIGH;
|
|
}
|
|
return pes.value;
|
|
}
|
|
}
|
|
return signal;
|
|
}
|
|
|
|
public float GetSignal(EGID blockID, out uint signalID, bool input = true)
|
|
{
|
|
signalID = GetSignalIDs(blockID, input)[0];
|
|
return GetSignal(signalID, input);
|
|
}
|
|
|
|
public float GetSignal(uint signalID, bool input = true)
|
|
{
|
|
ExclusiveGroup group = input ? NamedExclusiveGroup<InputPortsGroup>.Group : NamedExclusiveGroup<OutputPortsGroup>.Group;
|
|
if (entitiesDB.Exists<PortEntityStruct>(signalID, group))
|
|
{
|
|
return entitiesDB.QueryEntity<PortEntityStruct>(signalID, group).value;
|
|
}
|
|
return 0f;
|
|
}
|
|
|
|
public uint[] GetSignalIDs(EGID blockID, bool input = true)
|
|
{
|
|
ref BlockPortsStruct bps = ref entitiesDB.QueryEntity<BlockPortsStruct>(blockID);
|
|
uint[] signals;
|
|
if (input) {
|
|
signals = new uint[bps.inputCount];
|
|
for (uint i = 0u; i < bps.inputCount; i++)
|
|
{
|
|
signals[i] = bps.firstInputID + i;
|
|
}
|
|
} else {
|
|
signals = new uint[bps.outputCount];
|
|
for (uint i = 0u; i < bps.outputCount; i++)
|
|
{
|
|
signals[i] = bps.firstOutputID + i;
|
|
}
|
|
}
|
|
return signals;
|
|
}
|
|
|
|
public EGID[] GetElectricBlocks()
|
|
{
|
|
uint count = entitiesDB.Count<BlockPortsStruct>(BlockIdentifiers.OWNED_BLOCKS) + entitiesDB.Count<BlockPortsStruct>(BlockIdentifiers.FUNCTIONAL_BLOCK_PARTS);
|
|
uint i = 0;
|
|
EGID[] res = new EGID[count];
|
|
foreach (ref BlockPortsStruct s in entitiesDB.QueryEntities<BlockPortsStruct>(BlockIdentifiers.OWNED_BLOCKS))
|
|
{
|
|
res[i] = s.ID;
|
|
i++;
|
|
}
|
|
foreach (ref BlockPortsStruct s in entitiesDB.QueryEntities<BlockPortsStruct>(BlockIdentifiers.FUNCTIONAL_BLOCK_PARTS))
|
|
{
|
|
res[i] = s.ID;
|
|
i++;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
public bool IsSimulationMode()
|
|
{
|
|
return GamecraftModdingAPI.Utility.GameState.IsSimulationMode();
|
|
}
|
|
}
|
|
}
|