2019-12-25 19:25:53 +00:00
|
|
|
|
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;
|
2020-02-07 00:25:31 +00:00
|
|
|
|
using Svelto.DataStructures;
|
2019-12-25 19:25:53 +00:00
|
|
|
|
using Svelto.ECS;
|
|
|
|
|
using Svelto.ECS.EntityStructs;
|
|
|
|
|
using Unity.Transforms;
|
|
|
|
|
using Unity.Mathematics;
|
|
|
|
|
using UnityEngine;
|
2020-02-20 01:32:58 +00:00
|
|
|
|
using Gamecraft.Wires;
|
2019-12-25 19:25:53 +00:00
|
|
|
|
|
|
|
|
|
using GamecraftModdingAPI.Utility;
|
|
|
|
|
|
|
|
|
|
namespace GamecraftModdingAPI.Blocks
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2020-01-04 00:54:35 +00:00
|
|
|
|
/// Engine which executes signal actions
|
2019-12-25 19:25:53 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public class SignalEngine : IApiEngine
|
|
|
|
|
{
|
|
|
|
|
public string Name { get; } = "GamecraftModdingAPISignalGameEngine";
|
|
|
|
|
|
|
|
|
|
public IEntitiesDB entitiesDB { set; private get; }
|
|
|
|
|
|
|
|
|
|
public bool IsInGame = false;
|
|
|
|
|
|
2020-02-20 01:32:58 +00:00
|
|
|
|
private System.Random random = new System.Random();
|
2019-12-25 19:25:53 +00:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
IsInGame = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Ready()
|
|
|
|
|
{
|
|
|
|
|
IsInGame = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-20 01:32:58 +00:00
|
|
|
|
public void DoTheThing()
|
|
|
|
|
{
|
|
|
|
|
GamecraftModdingAPI.Tasks.Repeatable thing = new GamecraftModdingAPI.Tasks.Repeatable(
|
|
|
|
|
() => { Thing(); },
|
|
|
|
|
() => { return IsSimulationMode(); } );
|
|
|
|
|
GamecraftModdingAPI.Tasks.Scheduler.Schedule(thing);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Thing()
|
|
|
|
|
{
|
|
|
|
|
uint count = 0;
|
|
|
|
|
EGID[] eBlocks = GetElectricBlocks();
|
|
|
|
|
for (uint i = 0u; i < eBlocks.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
uint[] ids = GetSignalIDs(eBlocks[i]);
|
|
|
|
|
for (uint j = 0u; j < ids.Length; j++)
|
|
|
|
|
{
|
|
|
|
|
SetSignal(ids[j], (float)random.NextDouble());
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Logging.Log($"Did the thing on {count} inputs");
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-25 19:25:53 +00:00
|
|
|
|
// implementations for Signal static class
|
|
|
|
|
|
2020-02-20 01:32:58 +00:00
|
|
|
|
public bool SetSignal(EGID blockID, float signal, out uint signalID, bool input = true)
|
2019-12-25 19:25:53 +00:00
|
|
|
|
{
|
2020-02-20 01:32:58 +00:00
|
|
|
|
signalID = GetSignalIDs(blockID, input)[0];
|
|
|
|
|
return SetSignal(signalID, signal);
|
2019-12-25 19:25:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-20 01:32:58 +00:00
|
|
|
|
public bool SetSignal(uint signalID, float signal, bool input = true)
|
2019-12-25 19:25:53 +00:00
|
|
|
|
{
|
2020-02-20 01:32:58 +00:00
|
|
|
|
ExclusiveGroup group = input ? NamedExclusiveGroup<InputPortsGroup>.Group : NamedExclusiveGroup<OutputPortsGroup>.Group;
|
|
|
|
|
if (entitiesDB.Exists<PortEntityStruct>(signalID, group))
|
2019-12-25 19:25:53 +00:00
|
|
|
|
{
|
2020-02-20 01:32:58 +00:00
|
|
|
|
entitiesDB.QueryEntity<PortEntityStruct>(signalID, group).value = signal;
|
|
|
|
|
return true;
|
2019-12-25 19:25:53 +00:00
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-20 01:32:58 +00:00
|
|
|
|
public float AddSignal(EGID blockID, float signal, out uint signalID, bool clamp = true, bool input = true)
|
2019-12-25 19:25:53 +00:00
|
|
|
|
{
|
2020-02-20 01:32:58 +00:00
|
|
|
|
signalID = GetSignalIDs(blockID, input)[0];
|
|
|
|
|
return AddSignal(signalID, signal, clamp, input);
|
2019-12-25 19:25:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-20 01:32:58 +00:00
|
|
|
|
public float AddSignal(uint signalID, float signal, bool clamp = true, bool input = true)
|
2019-12-25 19:25:53 +00:00
|
|
|
|
{
|
2020-02-20 01:32:58 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-25 19:25:53 +00:00
|
|
|
|
return signal;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-20 01:32:58 +00:00
|
|
|
|
public float GetSignal(EGID blockID, out uint signalID, bool input = true)
|
2019-12-25 19:25:53 +00:00
|
|
|
|
{
|
2020-02-20 01:32:58 +00:00
|
|
|
|
signalID = GetSignalIDs(blockID, input)[0];
|
|
|
|
|
return GetSignal(signalID, input);
|
2019-12-25 19:25:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-20 01:32:58 +00:00
|
|
|
|
public float GetSignal(uint signalID, bool input = true)
|
2019-12-25 19:25:53 +00:00
|
|
|
|
{
|
2020-02-20 01:32:58 +00:00
|
|
|
|
ExclusiveGroup group = input ? NamedExclusiveGroup<InputPortsGroup>.Group : NamedExclusiveGroup<OutputPortsGroup>.Group;
|
|
|
|
|
if (entitiesDB.Exists<PortEntityStruct>(signalID, group))
|
2019-12-25 19:25:53 +00:00
|
|
|
|
{
|
2020-02-20 01:32:58 +00:00
|
|
|
|
return entitiesDB.QueryEntity<PortEntityStruct>(signalID, group).value;
|
2019-12-25 19:25:53 +00:00
|
|
|
|
}
|
|
|
|
|
return 0f;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-20 01:32:58 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2019-12-25 19:25:53 +00:00
|
|
|
|
|
2020-02-20 01:32:58 +00:00
|
|
|
|
public EGID[] GetElectricBlocks()
|
|
|
|
|
{
|
|
|
|
|
uint count = entitiesDB.Count<BlockPortsStruct>(BlockIdentifiers.OWNED_BLOCKS) + entitiesDB.Count<BlockPortsStruct>(BlockIdentifiers.FUNCTIONAL_BLOCK_PARTS);
|
2019-12-25 21:16:17 +00:00
|
|
|
|
uint i = 0;
|
2020-02-20 01:32:58 +00:00
|
|
|
|
EGID[] res = new EGID[count];
|
|
|
|
|
foreach (ref BlockPortsStruct s in entitiesDB.QueryEntities<BlockPortsStruct>(BlockIdentifiers.OWNED_BLOCKS))
|
2019-12-25 21:16:17 +00:00
|
|
|
|
{
|
2020-02-20 01:32:58 +00:00
|
|
|
|
res[i] = s.ID;
|
2019-12-25 21:16:17 +00:00
|
|
|
|
i++;
|
|
|
|
|
}
|
2020-02-20 01:32:58 +00:00
|
|
|
|
foreach (ref BlockPortsStruct s in entitiesDB.QueryEntities<BlockPortsStruct>(BlockIdentifiers.FUNCTIONAL_BLOCK_PARTS))
|
2019-12-25 21:16:17 +00:00
|
|
|
|
{
|
2020-02-20 01:32:58 +00:00
|
|
|
|
res[i] = s.ID;
|
|
|
|
|
i++;
|
2019-12-25 19:25:53 +00:00
|
|
|
|
}
|
2020-02-20 01:32:58 +00:00
|
|
|
|
return res;
|
2019-12-25 19:25:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsSimulationMode()
|
|
|
|
|
{
|
2019-12-25 21:16:17 +00:00
|
|
|
|
return GamecraftModdingAPI.Utility.GameState.IsSimulationMode();
|
2019-12-25 19:25:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|