Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- System.Text.RegularExpressions.Regex reLinearSignalName = new System.Text.RegularExpressions.Regex(
- @"^((?<group>\w+)\s+Signal\s+#(?<number>[0-9]+))$",
- System.Text.RegularExpressions.RegexOptions.IgnoreCase |
- System.Text.RegularExpressions.RegexOptions.Singleline
- );
- IMyProgrammableBlock thisCPU;
- void FindProgrammableBlock()
- {
- List<IMyTerminalBlock> blocks = GridTerminalSystem.Blocks.FindAll(b => b is IMyProgrammableBlock && (b as IMyProgrammableBlock).IsRunning);
- if(blocks.Count > 1)
- {
- throw new Exception("More than one running CPU running; unable to find this executing CPU.");
- }
- thisCPU = blocks[0] as IMyProgrammableBlock;
- }
- void Main()
- {
- FindProgrammableBlock();
- List<IMyTerminalBlock> lightList = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyInteriorLight>(lightList);
- Dictionary<string, List<IMyInteriorLight>> groupLightList = new Dictionary<string, List<IMyInteriorLight>>();
- for (int i = 0; i < lightList.Count; i++)
- {
- IMyInteriorLight light = lightList[i] as IMyInteriorLight;
- if (light.CubeGrid != thisCPU.CubeGrid)
- {
- continue;
- }
- string name = light.CustomName;
- System.Text.RegularExpressions.Match match = reLinearSignalName.Match(name);
- int signalId = 0;
- if (match != null && match.Success && match.Groups.Count > 1)
- {
- int.TryParse(match.Groups["number"].Value, out signalId);
- string groupName = (match.Groups["group"].Value ?? string.Empty).ToLower();
- bool isOn = (light as IMyFunctionalBlock).Enabled;
- List<IMyInteriorLight> l = new List<IMyInteriorLight>();
- if(groupLightList.ContainsKey(groupName))
- {
- l = groupLightList[groupName];
- }
- else
- {
- groupLightList.Add(groupName, l);
- }
- l.Add(light);
- }
- }
- ChangeLights(groupLightList);
- }
- void ChangeLights(Dictionary<string, List<IMyInteriorLight>> groupLightList)
- {
- List<string> keys = new List<string>(groupLightList.Keys);
- for (int i = 0; i < keys.Count; i++)
- {
- string key = keys[i];
- List<IMyInteriorLight> l = groupLightList[key];
- l.Sort(SortLinearLightList);
- int turnOn = 0;
- for (int j = 0; j < l.Count; j++)
- {
- if ((l[j] as IMyFunctionalBlock).Enabled)
- {
- turnOn = j + 1;
- if (turnOn >= l.Count) turnOn = 0;
- break;
- }
- }
- for (int j = 0; j < l.Count; j++)
- {
- if (j == turnOn)
- {
- l[j].ApplyAction("OnOff_On");
- }
- else
- {
- l[j].ApplyAction("OnOff_Off");
- }
- }
- }
- }
- int SortLinearLightList(IMyInteriorLight x, IMyInteriorLight y)
- {
- System.Text.RegularExpressions.Match xMatch = reLinearSignalName.Match(x.CustomName);
- int xNum = int.Parse(xMatch.Groups["number"].Value);
- System.Text.RegularExpressions.Match yMatch = reLinearSignalName.Match(y.CustomName);
- int yNum = int.Parse(yMatch.Groups["number"].Value);
- int ret = 0;
- if(xNum < yNum)
- {
- ret = -1;
- }
- else if(yNum > xNum)
- {
- ret = 1;
- }
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement