Completely rework the Sequencery library and abandon MechDoor in favor of just using the action sequencer script.
This commit is contained in:
@ -27,7 +27,7 @@ namespace IngameScript
|
||||
|
||||
private MyCommandLine _cli = new MyCommandLine();
|
||||
private List<IEnumerator<bool>> _jobs = new List<IEnumerator<bool>>();
|
||||
private Dictionary<string, Sequencer> _sequences = new Dictionary<string, Sequencer>();
|
||||
private Dictionary<string, ActionGroup> _actionGroups = new Dictionary<string, ActionGroup>();
|
||||
|
||||
public Program()
|
||||
{
|
||||
@ -35,36 +35,48 @@ namespace IngameScript
|
||||
|
||||
// initialize all the sequencers
|
||||
List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
|
||||
GridTerminalSystem.GetBlocksOfType(blocks, block => MyIni.HasSection(block.CustomData, "sequence"));
|
||||
GridTerminalSystem.GetBlocksOfType(blocks, block => MyIni.HasSection(block.CustomData, "sequencer"));
|
||||
|
||||
foreach (IMyTerminalBlock block in blocks)
|
||||
{
|
||||
Ini.TryParse(block.CustomData);
|
||||
string id = Ini.Get("sequence", "id").ToString();
|
||||
|
||||
string id = Ini.Get("sequencer", "groupName").ToString().Trim();
|
||||
if (id == "")
|
||||
{
|
||||
Console.Print($"No id found for '{block.CustomName}'. Skipping.");
|
||||
Console.Print($"No groupName found for '{block.CustomName}'. Skipping.");
|
||||
continue;
|
||||
}
|
||||
if (id == "all")
|
||||
|
||||
string actionNames = Ini.Get("sequencer", "actions").ToString().Trim(); ;
|
||||
if (actionNames == "")
|
||||
{
|
||||
Console.Print($"'All' is a reserved keyword. Skipping '{block.CustomName}'.");
|
||||
Console.Print($"No actions defined for '{block.CustomName}'. Skipping.");
|
||||
continue;
|
||||
}
|
||||
if (!_actionGroups.ContainsKey(id)) _actionGroups[id] = new ActionGroup(Console, id);
|
||||
|
||||
if (!_sequences.ContainsKey(id)) _sequences[id] = new Sequencer(this, id);
|
||||
|
||||
ISequenceable wrapped = SequenceableFactory.MakeSequenceable(this, block, "sequence");
|
||||
if (wrapped == null)
|
||||
// Find the custom section for each action and parse the block into it.
|
||||
foreach (string actionName in actionNames.Split(','))
|
||||
{
|
||||
Console.Print($"Skipping incompatible block '{block.CustomName}'.");
|
||||
continue;
|
||||
string key = "action" + actionName.Trim();
|
||||
if (!MyIni.HasSection(block.CustomData, key))
|
||||
{
|
||||
Console.Print($"Missing config section '{key}'; skipping action.");
|
||||
continue;
|
||||
}
|
||||
int step = Int32.Parse(Ini.Get(key, "step").ToString("0"));
|
||||
IBlockAction blockAction = buildBlockAction(block, key);
|
||||
if (blockAction == null)
|
||||
{
|
||||
Console.Print($"Failed to add '{block.CustomName}' to action '{actionName}'.");
|
||||
continue;
|
||||
}
|
||||
_actionGroups[id].AddActionBlock(actionName.Trim(), step, blockAction);
|
||||
}
|
||||
|
||||
_sequences[id].AddBlock(wrapped);
|
||||
}
|
||||
|
||||
Console.Print($"Found {_sequences.Count} sequences.");
|
||||
Console.Print($"Found {_actionGroups.Count} sequences.");
|
||||
Console.UpdateTickCount();
|
||||
}
|
||||
|
||||
@ -76,30 +88,22 @@ namespace IngameScript
|
||||
{
|
||||
_cli.TryParse(argument);
|
||||
|
||||
List<Sequencer> sequencesToRun = new List<Sequencer>();
|
||||
bool deploy = !_cli.Switch("stow");
|
||||
|
||||
if (_cli.ArgumentCount > 0 && _cli.Argument(0) == "all")
|
||||
if (_cli.ArgumentCount != 2)
|
||||
{
|
||||
sequencesToRun = _sequences.Values.ToList();
|
||||
Console.Print("Must call script with exactly 2 arguments.");
|
||||
}
|
||||
for (int i = 0; i < _cli.ArgumentCount; i++)
|
||||
else
|
||||
{
|
||||
string id = _cli.Argument(i);
|
||||
if (!_sequences.ContainsKey(id))
|
||||
if (_actionGroups.ContainsKey(_cli.Argument(0)))
|
||||
{
|
||||
Console.Print($"Ignoring non-existent sequence '{id}'");
|
||||
continue;
|
||||
_jobs.Add(_actionGroups[_cli.Argument(0)].RunAction(_cli.Argument(1)));
|
||||
Runtime.UpdateFrequency |= UpdateFrequency.Update10;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.Print($"Failed to find action group '{_cli.Argument(0)}'");
|
||||
}
|
||||
sequencesToRun.Add(_sequences[id]);
|
||||
}
|
||||
|
||||
foreach (Sequencer sequence in sequencesToRun)
|
||||
{
|
||||
Console.Print($"Activating sequence '{sequence.Name}'");
|
||||
_jobs.Add(sequence.RunSequence(deploy));
|
||||
}
|
||||
if (_jobs.Count > 0) Runtime.UpdateFrequency = UpdateFrequency.Update10;
|
||||
}
|
||||
|
||||
// Process running jobs
|
||||
@ -113,10 +117,76 @@ namespace IngameScript
|
||||
Console.Print("Operation Complete.");
|
||||
}
|
||||
|
||||
if (_jobs.Count == 0)
|
||||
if (_jobs.Count == 0) Runtime.UpdateFrequency = UpdateFrequency.None;
|
||||
}
|
||||
|
||||
// Prerequisite: Ini.Parse has already been called for this block.
|
||||
private IBlockAction buildBlockAction(IMyTerminalBlock block, string key)
|
||||
{
|
||||
if (block is IMyDoor)
|
||||
{
|
||||
Runtime.UpdateFrequency = UpdateFrequency.None;
|
||||
BlockActionDoor.DoorAction action;
|
||||
switch (Ini.Get(key, "action").ToString("open"))
|
||||
{
|
||||
case "open":
|
||||
action = BlockActionDoor.DoorAction.Open;
|
||||
break;
|
||||
case "close":
|
||||
action = BlockActionDoor.DoorAction.Close;
|
||||
break;
|
||||
default:
|
||||
Console.Print($"Invalid door action for '{block.CustomName}'. Defaulting to open.");
|
||||
action = BlockActionDoor.DoorAction.Open;
|
||||
break;
|
||||
}
|
||||
|
||||
return new BlockActionDoor(
|
||||
block as IMyDoor,
|
||||
action,
|
||||
Ini.Get(key, "lock").ToBoolean(true)
|
||||
);
|
||||
}
|
||||
else if (block is IMyMotorStator)
|
||||
{
|
||||
MyRotationDirection direction;
|
||||
switch (Ini.Get(key, "direction").ToString("auto"))
|
||||
{
|
||||
case "auto":
|
||||
direction = MyRotationDirection.AUTO;
|
||||
break;
|
||||
case "cw":
|
||||
case "clockwise":
|
||||
direction = MyRotationDirection.CW;
|
||||
break;
|
||||
case "ccw":
|
||||
case "counterclockwise":
|
||||
case "anticlockwise":
|
||||
direction = MyRotationDirection.CCW;
|
||||
break;
|
||||
default:
|
||||
Console.Print($"Invalid direction for '{block.CustomName}'. Defaulting to auto.");
|
||||
direction = MyRotationDirection.AUTO;
|
||||
break;
|
||||
}
|
||||
|
||||
return new BlockActionRotor(
|
||||
block as IMyMotorStator,
|
||||
Ini.Get(key, "angle").ToSingle(0f),
|
||||
Ini.Get(key, "velocity").ToSingle(5f),
|
||||
direction
|
||||
);
|
||||
}
|
||||
else if (block is IMyPistonBase)
|
||||
{
|
||||
return new BlockActionPiston(
|
||||
block as IMyPistonBase,
|
||||
Ini.Get(key, "position").ToSingle(0f),
|
||||
Ini.Get(key, "velocity").ToSingle(2f)
|
||||
);
|
||||
}
|
||||
|
||||
Console.Print($"Can't add unsupported block '{block.CustomName}'");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user