Fix rotors and pistons getting stuck and breaking the sequence.
This commit is contained in:
@ -8,7 +8,7 @@ namespace IngameScript
|
||||
{
|
||||
public class SequenceableDoor : ISequenceable
|
||||
{
|
||||
public bool Running { get; }
|
||||
public bool Running { get; private set; } = false;
|
||||
public int Step { get; set; }
|
||||
|
||||
public bool DeployOpen { get; set; }
|
||||
@ -18,12 +18,13 @@ namespace IngameScript
|
||||
private IMyDoor _door;
|
||||
|
||||
public SequenceableDoor(
|
||||
Program _program,
|
||||
IMyDoor door,
|
||||
MyIni ini,
|
||||
string sectionName)
|
||||
{
|
||||
_door = door;
|
||||
|
||||
MyIni ini = _program.Ini;
|
||||
ini.TryParse(door.CustomData);
|
||||
DeployOpen = ini.Get(sectionName, "deployOpen").ToBoolean(true);
|
||||
LockOpen = ini.Get(sectionName, "lockOpen").ToBoolean(true);
|
||||
@ -33,6 +34,9 @@ namespace IngameScript
|
||||
|
||||
public IEnumerator<bool> Run(bool deploy)
|
||||
{
|
||||
if (Running) yield break;
|
||||
Running = true;
|
||||
|
||||
if (deploy && DeployOpen || !deploy && !DeployOpen)
|
||||
{
|
||||
foreach (bool tick in _openDoor()) yield return true;
|
||||
@ -41,6 +45,7 @@ namespace IngameScript
|
||||
{
|
||||
foreach (bool tick in _closeDoor()) yield return true;
|
||||
}
|
||||
Running = false;
|
||||
}
|
||||
|
||||
public IEnumerable<bool> _openDoor()
|
||||
@ -48,7 +53,8 @@ namespace IngameScript
|
||||
_door.Enabled = true;
|
||||
_door.OpenDoor();
|
||||
while (_door.Status != DoorStatus.Open) yield return true;
|
||||
if (LockOpen) {
|
||||
if (LockOpen)
|
||||
{
|
||||
_door.Enabled = false;
|
||||
}
|
||||
}
|
||||
@ -58,7 +64,8 @@ namespace IngameScript
|
||||
_door.Enabled = true;
|
||||
_door.CloseDoor();
|
||||
while (_door.Status != DoorStatus.Closed) yield return true;
|
||||
if (LockClosed) {
|
||||
if (LockClosed)
|
||||
{
|
||||
_door.Enabled = false;
|
||||
}
|
||||
}
|
||||
|
@ -11,17 +11,17 @@ namespace IngameScript
|
||||
public class SequenceableFactory
|
||||
{
|
||||
public static ISequenceable MakeSequenceable(
|
||||
Program program,
|
||||
IMyTerminalBlock block,
|
||||
MyIni ini,
|
||||
string sectionName = "sequence")
|
||||
{
|
||||
if (block is IMyMotorStator)
|
||||
{
|
||||
return new SequenceableRotor(block as IMyMotorStator, ini, sectionName);
|
||||
return new SequenceableRotor(program, block as IMyMotorStator, sectionName);
|
||||
}
|
||||
if (block is IMyPistonBase)
|
||||
{
|
||||
// return new SequenceablePiston(block as IMyPistonBase, step);
|
||||
return new SequenceablePiston(program, block as IMyPistonBase, sectionName);
|
||||
}
|
||||
if (block is IMyShipMergeBlock)
|
||||
{
|
||||
@ -29,7 +29,7 @@ namespace IngameScript
|
||||
}
|
||||
if (block is IMyDoor)
|
||||
{
|
||||
return new SequenceableDoor(block as IMyDoor, ini, sectionName);
|
||||
return new SequenceableDoor(program, block as IMyDoor, sectionName);
|
||||
}
|
||||
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 int Step { get; set; }
|
||||
|
||||
private Program _program;
|
||||
private float _velocity;
|
||||
private float _deployAngle;
|
||||
private float _stowAngle;
|
||||
@ -20,13 +21,16 @@ namespace IngameScript
|
||||
private MyRotationDirection _stowDirection;
|
||||
|
||||
public SequenceableRotor(
|
||||
Program program,
|
||||
IMyMotorStator rotor,
|
||||
MyIni ini,
|
||||
string sectionName)
|
||||
{
|
||||
_program = program;
|
||||
_rotor = rotor;
|
||||
|
||||
MyIni ini = _program.Ini;
|
||||
ini.TryParse(rotor.CustomData);
|
||||
|
||||
_deployAngle = ini.Get(sectionName, "deployAngle").ToSingle(90F);
|
||||
_stowAngle = ini.Get(sectionName, "stowAngle").ToSingle(0F);
|
||||
_velocity = ini.Get(sectionName, "velocity").ToSingle(5F);
|
||||
@ -50,10 +54,15 @@ namespace IngameScript
|
||||
|
||||
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;
|
||||
while (Math.Abs(_rotor.Angle - _targetAngle) > 0.1 || _rotor.Angle != _lastAngle)
|
||||
_rotor.RotorLock = false;
|
||||
_rotor.RotateToAngle(dir, degAngle, _velocity);
|
||||
|
||||
float _lastAngle = -1;
|
||||
while (_rotor.Angle != _lastAngle)
|
||||
{
|
||||
_lastAngle = _rotor.Angle;
|
||||
yield return true;
|
||||
@ -63,18 +72,6 @@ namespace IngameScript
|
||||
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);
|
||||
|
Reference in New Issue
Block a user