Fix rotors and pistons getting stuck and breaking the sequence.
This commit is contained in:
parent
fb211a9426
commit
8611c6e597
|
@ -54,7 +54,7 @@ namespace IngameScript
|
||||||
|
|
||||||
if (!_sequences.ContainsKey(id)) _sequences[id] = new Sequencer(this, id);
|
if (!_sequences.ContainsKey(id)) _sequences[id] = new Sequencer(this, id);
|
||||||
|
|
||||||
ISequenceable wrapped = SequenceableFactory.MakeSequenceable(block, Ini, id);
|
ISequenceable wrapped = SequenceableFactory.MakeSequenceable(this, block, "sequence");
|
||||||
if (wrapped == null)
|
if (wrapped == null)
|
||||||
{
|
{
|
||||||
Console.Print($"Skipping incompatible block '{block.CustomName}'.");
|
Console.Print($"Skipping incompatible block '{block.CustomName}'.");
|
||||||
|
|
|
@ -41,8 +41,8 @@ namespace IngameScript
|
||||||
{
|
{
|
||||||
_doors.Add(block as IMyDoor);
|
_doors.Add(block as IMyDoor);
|
||||||
SequenceableDoor wrapped = SequenceableFactory.MakeSequenceable(
|
SequenceableDoor wrapped = SequenceableFactory.MakeSequenceable(
|
||||||
|
_program,
|
||||||
block as IMyDoor,
|
block as IMyDoor,
|
||||||
_program.Ini,
|
|
||||||
"airMonitor") as SequenceableDoor;
|
"airMonitor") as SequenceableDoor;
|
||||||
wrapped.Step = 0;
|
wrapped.Step = 0;
|
||||||
wrapped.DeployOpen = false;
|
wrapped.DeployOpen = false;
|
||||||
|
|
|
@ -31,7 +31,7 @@ namespace IngameScript
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the part; the Door object handles typing and sequencing.
|
// Add the part; the Door object handles typing and sequencing.
|
||||||
ISequenceable wrapped = SequenceableFactory.MakeSequenceable(block, Ini, "mechDoor");
|
ISequenceable wrapped = SequenceableFactory.MakeSequenceable(this, block, "mechDoor");
|
||||||
if (!(block is IMyShipMergeBlock)) wrapped.Step = 1; // TODO: actually support merge blocks here
|
if (!(block is IMyShipMergeBlock)) wrapped.Step = 1; // TODO: actually support merge blocks here
|
||||||
if (wrapped == null) { Console.Print($"Tried to add incompatible block '{block.CustomName}'"); continue; }
|
if (wrapped == null) { Console.Print($"Tried to add incompatible block '{block.CustomName}'"); continue; }
|
||||||
_doors[doorName].AddBlock(wrapped);
|
_doors[doorName].AddBlock(wrapped);
|
||||||
|
|
|
@ -8,7 +8,7 @@ namespace IngameScript
|
||||||
{
|
{
|
||||||
public class SequenceableDoor : ISequenceable
|
public class SequenceableDoor : ISequenceable
|
||||||
{
|
{
|
||||||
public bool Running { get; }
|
public bool Running { get; private set; } = false;
|
||||||
public int Step { get; set; }
|
public int Step { get; set; }
|
||||||
|
|
||||||
public bool DeployOpen { get; set; }
|
public bool DeployOpen { get; set; }
|
||||||
|
@ -18,12 +18,13 @@ namespace IngameScript
|
||||||
private IMyDoor _door;
|
private IMyDoor _door;
|
||||||
|
|
||||||
public SequenceableDoor(
|
public SequenceableDoor(
|
||||||
|
Program _program,
|
||||||
IMyDoor door,
|
IMyDoor door,
|
||||||
MyIni ini,
|
|
||||||
string sectionName)
|
string sectionName)
|
||||||
{
|
{
|
||||||
_door = door;
|
_door = door;
|
||||||
|
|
||||||
|
MyIni ini = _program.Ini;
|
||||||
ini.TryParse(door.CustomData);
|
ini.TryParse(door.CustomData);
|
||||||
DeployOpen = ini.Get(sectionName, "deployOpen").ToBoolean(true);
|
DeployOpen = ini.Get(sectionName, "deployOpen").ToBoolean(true);
|
||||||
LockOpen = ini.Get(sectionName, "lockOpen").ToBoolean(true);
|
LockOpen = ini.Get(sectionName, "lockOpen").ToBoolean(true);
|
||||||
|
@ -33,6 +34,9 @@ namespace IngameScript
|
||||||
|
|
||||||
public IEnumerator<bool> Run(bool deploy)
|
public IEnumerator<bool> Run(bool deploy)
|
||||||
{
|
{
|
||||||
|
if (Running) yield break;
|
||||||
|
Running = true;
|
||||||
|
|
||||||
if (deploy && DeployOpen || !deploy && !DeployOpen)
|
if (deploy && DeployOpen || !deploy && !DeployOpen)
|
||||||
{
|
{
|
||||||
foreach (bool tick in _openDoor()) yield return true;
|
foreach (bool tick in _openDoor()) yield return true;
|
||||||
|
@ -41,6 +45,7 @@ namespace IngameScript
|
||||||
{
|
{
|
||||||
foreach (bool tick in _closeDoor()) yield return true;
|
foreach (bool tick in _closeDoor()) yield return true;
|
||||||
}
|
}
|
||||||
|
Running = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<bool> _openDoor()
|
public IEnumerable<bool> _openDoor()
|
||||||
|
@ -48,7 +53,8 @@ namespace IngameScript
|
||||||
_door.Enabled = true;
|
_door.Enabled = true;
|
||||||
_door.OpenDoor();
|
_door.OpenDoor();
|
||||||
while (_door.Status != DoorStatus.Open) yield return true;
|
while (_door.Status != DoorStatus.Open) yield return true;
|
||||||
if (LockOpen) {
|
if (LockOpen)
|
||||||
|
{
|
||||||
_door.Enabled = false;
|
_door.Enabled = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,7 +64,8 @@ namespace IngameScript
|
||||||
_door.Enabled = true;
|
_door.Enabled = true;
|
||||||
_door.CloseDoor();
|
_door.CloseDoor();
|
||||||
while (_door.Status != DoorStatus.Closed) yield return true;
|
while (_door.Status != DoorStatus.Closed) yield return true;
|
||||||
if (LockClosed) {
|
if (LockClosed)
|
||||||
|
{
|
||||||
_door.Enabled = false;
|
_door.Enabled = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,17 +11,17 @@ namespace IngameScript
|
||||||
public class SequenceableFactory
|
public class SequenceableFactory
|
||||||
{
|
{
|
||||||
public static ISequenceable MakeSequenceable(
|
public static ISequenceable MakeSequenceable(
|
||||||
|
Program program,
|
||||||
IMyTerminalBlock block,
|
IMyTerminalBlock block,
|
||||||
MyIni ini,
|
|
||||||
string sectionName = "sequence")
|
string sectionName = "sequence")
|
||||||
{
|
{
|
||||||
if (block is IMyMotorStator)
|
if (block is IMyMotorStator)
|
||||||
{
|
{
|
||||||
return new SequenceableRotor(block as IMyMotorStator, ini, sectionName);
|
return new SequenceableRotor(program, block as IMyMotorStator, sectionName);
|
||||||
}
|
}
|
||||||
if (block is IMyPistonBase)
|
if (block is IMyPistonBase)
|
||||||
{
|
{
|
||||||
// return new SequenceablePiston(block as IMyPistonBase, step);
|
return new SequenceablePiston(program, block as IMyPistonBase, sectionName);
|
||||||
}
|
}
|
||||||
if (block is IMyShipMergeBlock)
|
if (block is IMyShipMergeBlock)
|
||||||
{
|
{
|
||||||
|
@ -29,7 +29,7 @@ namespace IngameScript
|
||||||
}
|
}
|
||||||
if (block is IMyDoor)
|
if (block is IMyDoor)
|
||||||
{
|
{
|
||||||
return new SequenceableDoor(block as IMyDoor, ini, sectionName);
|
return new SequenceableDoor(program, block as IMyDoor, sectionName);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
58
Mixins/Sequencer/SequenceablePiston.cs
Normal file
58
Mixins/Sequencer/SequenceablePiston.cs
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
using Sandbox.ModAPI.Ingame;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using VRage.Game.ModAPI.Ingame.Utilities;
|
||||||
|
|
||||||
|
namespace IngameScript
|
||||||
|
{
|
||||||
|
partial class Program
|
||||||
|
{
|
||||||
|
public class SequenceablePiston : ISequenceable
|
||||||
|
{
|
||||||
|
public bool Running { get; private set; } = false;
|
||||||
|
public int Step { get; set; }
|
||||||
|
|
||||||
|
private Program _program;
|
||||||
|
private IMyPistonBase _piston;
|
||||||
|
private float _deployPosition;
|
||||||
|
private float _stowPosition;
|
||||||
|
private float _velocity;
|
||||||
|
|
||||||
|
public SequenceablePiston(Program program, IMyPistonBase piston, string sectionName)
|
||||||
|
{
|
||||||
|
_program = program;
|
||||||
|
_piston = piston;
|
||||||
|
|
||||||
|
MyIni ini = _program.Ini;
|
||||||
|
ini.TryParse(piston.CustomData);
|
||||||
|
|
||||||
|
_deployPosition = ini.Get(sectionName, "deployPosition").ToSingle(10F);
|
||||||
|
_stowPosition = ini.Get(sectionName, "stowPosition").ToSingle(0F);
|
||||||
|
_velocity = ini.Get(sectionName, "velocity").ToSingle(5F);
|
||||||
|
Step = ini.Get(sectionName, "step").ToInt32(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerator<bool> Run(bool deploy)
|
||||||
|
{
|
||||||
|
if (Running) yield break;
|
||||||
|
Running = true;
|
||||||
|
_program.Console.Print("DEBUG: Piston Starting");
|
||||||
|
|
||||||
|
float targetValue = _stowPosition;
|
||||||
|
float lastValue = -1;
|
||||||
|
if (deploy) targetValue = _deployPosition;
|
||||||
|
_piston.MoveToPosition(targetValue, _velocity);
|
||||||
|
|
||||||
|
while (lastValue != _piston.CurrentPosition)
|
||||||
|
// Math.Abs(_piston.CurrentPosition - targetValue) > 0.01 ||
|
||||||
|
{
|
||||||
|
lastValue = _piston.CurrentPosition;
|
||||||
|
_program.Console.Print(_piston.Status.ToString());
|
||||||
|
yield return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Running = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,6 +12,7 @@ namespace IngameScript
|
||||||
public bool Running { get; private set; } = false;
|
public bool Running { get; private set; } = false;
|
||||||
public int Step { get; set; }
|
public int Step { get; set; }
|
||||||
|
|
||||||
|
private Program _program;
|
||||||
private float _velocity;
|
private float _velocity;
|
||||||
private float _deployAngle;
|
private float _deployAngle;
|
||||||
private float _stowAngle;
|
private float _stowAngle;
|
||||||
|
@ -20,13 +21,16 @@ namespace IngameScript
|
||||||
private MyRotationDirection _stowDirection;
|
private MyRotationDirection _stowDirection;
|
||||||
|
|
||||||
public SequenceableRotor(
|
public SequenceableRotor(
|
||||||
|
Program program,
|
||||||
IMyMotorStator rotor,
|
IMyMotorStator rotor,
|
||||||
MyIni ini,
|
|
||||||
string sectionName)
|
string sectionName)
|
||||||
{
|
{
|
||||||
|
_program = program;
|
||||||
_rotor = rotor;
|
_rotor = rotor;
|
||||||
|
|
||||||
|
MyIni ini = _program.Ini;
|
||||||
ini.TryParse(rotor.CustomData);
|
ini.TryParse(rotor.CustomData);
|
||||||
|
|
||||||
_deployAngle = ini.Get(sectionName, "deployAngle").ToSingle(90F);
|
_deployAngle = ini.Get(sectionName, "deployAngle").ToSingle(90F);
|
||||||
_stowAngle = ini.Get(sectionName, "stowAngle").ToSingle(0F);
|
_stowAngle = ini.Get(sectionName, "stowAngle").ToSingle(0F);
|
||||||
_velocity = ini.Get(sectionName, "velocity").ToSingle(5F);
|
_velocity = ini.Get(sectionName, "velocity").ToSingle(5F);
|
||||||
|
@ -50,10 +54,15 @@ namespace IngameScript
|
||||||
|
|
||||||
public IEnumerator<bool> Run(bool deploy = true)
|
public IEnumerator<bool> Run(bool deploy = true)
|
||||||
{
|
{
|
||||||
float _targetAngle = setup(deploy);
|
Running = true;
|
||||||
|
float degAngle = deploy ? _deployAngle : _stowAngle;
|
||||||
|
MyRotationDirection dir = deploy ? _deployDirection : _stowDirection;
|
||||||
|
|
||||||
float _lastAngle = _rotor.Angle;
|
_rotor.RotorLock = false;
|
||||||
while (Math.Abs(_rotor.Angle - _targetAngle) > 0.1 || _rotor.Angle != _lastAngle)
|
_rotor.RotateToAngle(dir, degAngle, _velocity);
|
||||||
|
|
||||||
|
float _lastAngle = -1;
|
||||||
|
while (_rotor.Angle != _lastAngle)
|
||||||
{
|
{
|
||||||
_lastAngle = _rotor.Angle;
|
_lastAngle = _rotor.Angle;
|
||||||
yield return true;
|
yield return true;
|
||||||
|
@ -63,18 +72,6 @@ namespace IngameScript
|
||||||
Running = false;
|
Running = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private float setup(bool deploy = true)
|
|
||||||
{
|
|
||||||
float degAngle = deploy ? _deployAngle : _stowAngle;
|
|
||||||
MyRotationDirection dir = deploy ? _deployDirection : _stowDirection;
|
|
||||||
|
|
||||||
_rotor.RotorLock = false;
|
|
||||||
_rotor.RotateToAngle(dir, degAngle, _velocity);
|
|
||||||
Running = true;
|
|
||||||
|
|
||||||
return degToRad(degAngle);
|
|
||||||
}
|
|
||||||
|
|
||||||
private float degToRad(float degrees)
|
private float degToRad(float degrees)
|
||||||
{
|
{
|
||||||
return degrees * ((float)Math.PI / 180F);
|
return degrees * ((float)Math.PI / 180F);
|
||||||
|
|
|
@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ActionSequencer", "ActionSe
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AirMonitor", "AirMonitor\AirMonitor.csproj", "{40B352CD-100E-4F87-A7A0-FF00F4B8846C}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AirMonitor", "AirMonitor\AirMonitor.csproj", "{40B352CD-100E-4F87-A7A0-FF00F4B8846C}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{8a3cdcc5-4b55-4d87-a415-698a0e1ff06f}") = "Sequencer", "Mixins\Sequencer\Sequencer.shproj", "{722a2146-2e38-477c-9587-55075b8fa0e6}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
@ -36,5 +38,9 @@ Global
|
||||||
{40B352CD-100E-4F87-A7A0-FF00F4B8846C}.Debug|Any CPU.Build.0 = Debug|x64
|
{40B352CD-100E-4F87-A7A0-FF00F4B8846C}.Debug|Any CPU.Build.0 = Debug|x64
|
||||||
{40B352CD-100E-4F87-A7A0-FF00F4B8846C}.Release|Any CPU.ActiveCfg = Release|x64
|
{40B352CD-100E-4F87-A7A0-FF00F4B8846C}.Release|Any CPU.ActiveCfg = Release|x64
|
||||||
{40B352CD-100E-4F87-A7A0-FF00F4B8846C}.Release|Any CPU.Build.0 = Release|x64
|
{40B352CD-100E-4F87-A7A0-FF00F4B8846C}.Release|Any CPU.Build.0 = Release|x64
|
||||||
|
# {722a2146-2e38-477c-9587-55075b8fa0e6}.Debug|Any CPU.ActiveCfg = Debug|x64
|
||||||
|
# {722a2146-2e38-477c-9587-55075b8fa0e6}.Debug|Any CPU.Build.0 = Debug|x64
|
||||||
|
# {722a2146-2e38-477c-9587-55075b8fa0e6}.Release|Any CPU.ActiveCfg = Release|x64
|
||||||
|
# {722a2146-2e38-477c-9587-55075b8fa0e6}.Release|Any CPU.Build.0 = Release|x64
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|
Loading…
Reference in New Issue
Block a user