Skip to content

Commit

Permalink
fix: allow more instructions per block
Browse files Browse the repository at this point in the history
  • Loading branch information
hfrances committed Mar 28, 2024
1 parent 73141ce commit 0ec571e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 42 deletions.
8 changes: 5 additions & 3 deletions SequentialScript/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public static Color ParseColor(string value)
public static IDictionary<string, IEnumerable<IMyTerminalBlock>> CreateBlockDictionary(IEnumerable<string> blockNames, IEnumerable<IMyTerminalBlock> blockList, IEnumerable<IMyBlockGroup> blockGroup)
{
var result = new Dictionary<string, IEnumerable<IMyTerminalBlock>>(StringComparer.OrdinalIgnoreCase);
var blockDictionary = blockList.GroupBy(x => x.DisplayNameText, StringComparer.OrdinalIgnoreCase).ToDictionary(x => x.Key, x => x.AsEnumerable(), StringComparer.OrdinalIgnoreCase);
IEnumerable<IMyTerminalBlock> blocks;

foreach (var blockName in blockNames)
Expand All @@ -89,7 +90,8 @@ public static IDictionary<string, IEnumerable<IMyTerminalBlock>> CreateBlockDict
{
if (blockName.StartsWith("*") && blockName.EndsWith("*"))
{
var groups = blockGroup.Where(x => x.Name.Equals(blockName.Substring(1, blockName.Length - 2), StringComparison.OrdinalIgnoreCase));
var realName = blockName.Substring(1, blockName.Length - 2);
var groups = blockGroup.Where(x => x.Name.Equals(realName, StringComparison.OrdinalIgnoreCase));

// Groups are between "*", but if it has not been found, it will check in block list.
if (groups.Any())
Expand All @@ -99,9 +101,9 @@ public static IDictionary<string, IEnumerable<IMyTerminalBlock>> CreateBlockDict
}
if (blocks == null)
{
blocks = blockList.Where(x => x.DisplayNameText.Equals(blockName, StringComparison.OrdinalIgnoreCase));
blockDictionary.TryGetValue(blockName, out blocks);
}
if (blocks.Any())
if (blocks?.Any() == true)
{
result.Add(blockName, blocks.ToArray());
}
Expand Down
2 changes: 1 addition & 1 deletion SequentialScript/Instructions.readme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
R e a d m e
-----------

V1.1-alpha.5
V1.1-alpha.6
See more information in the following link:
https://github.com/space-engineers-hf/SequentialScript
113 changes: 75 additions & 38 deletions SequentialScript/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ partial class Program : MyGridProgram
#endregion

DateTime _momento;
IList<IMyTerminalBlock> _terminalBlocks;
IList<IMyBlockGroup> _terminalGroups;
IDictionary<string, IEnumerable<IMyTerminalBlock>> _blocksDictionary;
IDictionary<string, ICommandInstruction> _commands;
InstructionCommand _command;
Expand All @@ -55,49 +57,79 @@ public void Main(string argument, UpdateType updateSource)
var debug = new StringBuilder();
var now = DateTime.UtcNow;

if (UPDATE_TICKS == 0 || ticks % UPDATE_TICKS == 0)
try
{
if (_command != null)
{
IEnumerable<Task> thenTasks;

AdvancedEcho($"Running {_command.CommandName}", append: true);
thenTasks = Tasks.CreateTasks(_command.Body, _blocksDictionary);
StartTasks(thenTasks, $"{debug}\nStarted.", appendMessage: true);
}
else if (_tasks != null)
if (UPDATE_TICKS == 0 || ticks % UPDATE_TICKS == 0)
{
IEnumerable<Task> tasksRunning;
if (_terminalBlocks == null || _terminalGroups == null)
{
AdvancedEcho($"Getting terminal blocks", append: true);
_terminalBlocks = GridTerminalSystem.GetBlocks();
AdvancedEcho($" - OK", append: true);
AdvancedEcho($"Getting terminal groups", append: true);
_terminalGroups = GridTerminalSystem.GetBlockGroups();
AdvancedEcho($" - OK", append: true);
}
else if (_blocksDictionary == null)
{
var blockNames = _commands.Values
.OfType<InstructionCommand>()
.SelectMany(cmd => cmd.Body)
.SelectMany(body => body.Instructions)
.Select(instruction => instruction.BlockName);

AdvancedEcho($"Building dictionary", append: true);
_blocksDictionary = Helper.CreateBlockDictionary(blockNames, _terminalBlocks, _terminalGroups);
AdvancedEcho($" - OK", append: true);
}
else if (_command != null)
{
IEnumerable<Task> thenTasks;

tasksRunning = _tasks.Run(debug);
if (tasksRunning.Any())
AdvancedEcho($"Running {_command.CommandName}", append: true);
thenTasks = Tasks.CreateTasks(_command.Body, _blocksDictionary);
StartTasks(thenTasks, $"{debug}\nStarted.", appendMessage: true);
}
else if (_tasks != null)
{
IEnumerable<Task> tasksRunning;

tasksRunning = _tasks.Run(debug);
if (tasksRunning.Any())
{
AdvancedEcho($"{debug}", append: true);
AdvancedEcho($"Running tasks: {(DateTime.UtcNow - now).TotalMilliseconds:N0}", append: true);
}
else
{
// Finish cycle.
EndCycle();
AdvancedEcho("Done.");
}
}
else if (_conditionCommand != null)
{
AdvancedEcho($"{debug}", append: true);
AdvancedEcho($"Running tasks: {(DateTime.UtcNow - now).TotalMilliseconds:N0}", append: true);
CheckNextCondition();
AdvancedEcho($"Condition command: {(DateTime.UtcNow - now).TotalMilliseconds:N0}", append: true);
}
else
{
// Finish cycle.
_tasks = null;
Runtime.UpdateFrequency = UpdateFrequency.None;
AdvancedEcho("Done.");
throw new Exception("Invalid state.");
}
ticks = 0;
}
else if (_conditionCommand != null)
{
CheckNextCondition();
AdvancedEcho($"Condition command: {(DateTime.UtcNow - now).TotalMilliseconds:N0}", append: true);
}
else
{
throw new Exception("Invalid state.");
}
ticks = 0;
ticks++;
}
ticks++;
catch (Exception ex)
{
EndCycle();
AdvancedEcho($"ERROR: {ex.Message}", append: true);
}

}
else
{
var debug = new StringBuilder();
ICommandInstruction command;

try
Expand All @@ -110,13 +142,9 @@ public void Main(string argument, UpdateType updateSource)
}
else if (_commands.TryGetValue(argument, out command))
{
var blockNames = _commands.Values
.OfType<InstructionCommand>()
.SelectMany(cmd => cmd.Body)
.SelectMany(body => body.Instructions)
.Select(instruction => instruction.BlockName);

_blocksDictionary = Helper.CreateBlockDictionary(blockNames, GridTerminalSystem.GetBlocks(), GridTerminalSystem.GetBlockGroups());
_terminalBlocks = null;
_terminalGroups = null;
_blocksDictionary = null;
if (command is InstructionCommand)
{
StartCommand((InstructionCommand)command, $"Command '{command.CommandName}' started.");
Expand All @@ -133,7 +161,11 @@ public void Main(string argument, UpdateType updateSource)
}
catch (Exception ex)
{
AdvancedEcho($"ERROR: {ex.Message}");
debug.AppendLine($"ERROR: {ex.Message}");
}
finally
{
AdvancedEcho($"{debug}");
}
}
}
Expand Down Expand Up @@ -243,6 +275,11 @@ void CheckNextCondition()
}
}

void EndCycle()
{
_tasks = null;
Runtime.UpdateFrequency = UpdateFrequency.None;
}

void AdvancedEchoReset()
{
Expand Down

0 comments on commit 0ec571e

Please sign in to comment.