Configure Mechanical Door to use CustomData for configuration. Also refactor Sequenceables to use the yield enumerator pattern.

This commit is contained in:
2025-02-10 19:43:10 -05:00
parent 8c55176aed
commit c8babebaef
5 changed files with 75 additions and 66 deletions

View File

@ -1,3 +1,5 @@
using System.Collections.Generic;
namespace IngameScript
{
partial class Program
@ -5,10 +7,8 @@ namespace IngameScript
public interface ISequenceable
{
bool Running { get; }
int Step { get; }
void Start(bool reverse = true);
bool Check();
void Finish();
int Step { get; set; }
IEnumerator<bool> Run(bool deploy);
}
}
}

View File

@ -13,11 +13,11 @@ namespace IngameScript
public static ISequenceable MakeSequenceable(
IMyTerminalBlock block,
MyIni ini,
int step = 0)
string sectionName = "sequence")
{
if (block is IMyMotorStator)
{
return new SequenceableRotor(block as IMyMotorStator, ini, step);
return new SequenceableRotor(block as IMyMotorStator, ini, sectionName);
}
if (block is IMyPistonBase)
{

View File

@ -1,5 +1,6 @@
using Sandbox.ModAPI.Ingame;
using System;
using System.Collections.Generic;
using VRage.Game.ModAPI.Ingame.Utilities;
namespace IngameScript
@ -9,55 +10,71 @@ namespace IngameScript
public class SequenceableRotor : ISequenceable
{
public bool Running { get; private set; } = false;
public int Step { get; private set; }
private float _targetAngle;
private float _lastAngle;
public int Step { get; set; }
private float _velocity;
private float _openAngle;
private float _closedAngle;
private float _deployAngle;
private float _stowAngle;
private IMyMotorStator _rotor;
private MyRotationDirection _deployDirection;
private MyRotationDirection _stowDirection;
public SequenceableRotor(
IMyMotorStator rotor,
MyIni ini,
int defaultStep = 0)
string sectionName)
{
_rotor = rotor;
ini.TryParse(rotor.CustomData);
_openAngle = ini.Get("sequencer", "openAngle").ToSingle(90F);
_closedAngle = ini.Get("sequencer", "closedAngle").ToSingle(0F);
_velocity = ini.Get("sequencer", "velocity").ToSingle(5F);
Step = ini.Get("sequencer", "step").ToInt32(defaultStep);
}
public void Start(bool reverse)
{
float degAngle = reverse ? _closedAngle : _openAngle;
_targetAngle = degToRad(degAngle);
_rotor.RotorLock = false;
_rotor.RotateToAngle(MyRotationDirection.AUTO, degAngle, _velocity);
Running = true;
}
public bool Check()
{
if (Math.Abs(_rotor.Angle - _targetAngle) < 0.1 && _rotor.Angle == _lastAngle)
_deployAngle = ini.Get(sectionName, "deployAngle").ToSingle(90F);
_stowAngle = ini.Get(sectionName, "stowAngle").ToSingle(0F);
_velocity = ini.Get(sectionName, "velocity").ToSingle(5F);
switch (ini.Get(sectionName, "deployDirection").ToString("auto"))
{
return false;
case "auto":
_deployDirection = MyRotationDirection.AUTO;
_stowDirection = MyRotationDirection.AUTO;
break;
case "cw":
_deployDirection = MyRotationDirection.CW;
_stowDirection = MyRotationDirection.CCW;
break;
case "ccw":
_deployDirection = MyRotationDirection.CCW;
_stowDirection = MyRotationDirection.CW;
break;
}
_lastAngle = _rotor.Angle;
return true;
Step = ini.Get(sectionName, "step").ToInt32(0);
}
public void Finish()
public IEnumerator<bool> Run(bool deploy = true)
{
float _targetAngle = setup(deploy);
float _lastAngle = _rotor.Angle;
while (Math.Abs(_rotor.Angle - _targetAngle) > 0.1 || _rotor.Angle != _lastAngle)
{
_lastAngle = _rotor.Angle;
yield return true;
}
_rotor.RotorLock = true;
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)
{
return degrees * ((float)Math.PI / 180F);

View File

@ -34,7 +34,7 @@ namespace IngameScript
// To activate the Sequencer, call this once, then call `MoveNext()` once per tick
// on the returned object until it returns false.
// (then be sure to call `Dispose() on that object`)
public IEnumerator<bool> RunSequence(bool reverse = false)
public IEnumerator<bool> RunSequence(bool deploy = true)
{
if (Running)
{
@ -44,36 +44,30 @@ namespace IngameScript
// This is a bit convoluted, but we're extracting the iterator for use in the foreach directly.
// This allows us to iterate in reverse when reversing/"closing" the sequence.
IEnumerable<KeyValuePair<int, List<ISequenceable>>> steps = reverse ? _sequence.Reverse() : _sequence;
IEnumerable<KeyValuePair<int, List<ISequenceable>>> steps = deploy ? _sequence : _sequence.Reverse();
foreach (KeyValuePair<int, List<ISequenceable>> kvp in steps)
{
List<ISequenceable> blocks = kvp.Value;
List<IEnumerator<bool>> subJobs = new List<IEnumerator<bool>>();
Running = true;
foreach (ISequenceable block in blocks)
{
// TODO: add some sort of handling for block.Running == true
block.Start(reverse);
// TODO: add some sort of handling for block.Running == true, maybe?
// It *should* be an impossible case, but...
subJobs.Add(block.Run(deploy));
}
yield return true;
while (Running)
{
Running = false;
foreach (ISequenceable block in blocks)
foreach (IEnumerator<bool> subJob in subJobs)
{
if (block.Check()) Running = true;
if (subJob.MoveNext()) Running = true;
}
yield return true;
}
foreach (ISequenceable block in blocks)
{
block.Finish();
}
}
yield break;
}
}
}