Advertisement
JamesK89

Linear Signal Controller

Apr 26th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | None | 0 0
  1. System.Text.RegularExpressions.Regex reLinearSignalName = new System.Text.RegularExpressions.Regex(
  2.     @"^((?<group>\w+)\s+Signal\s+#(?<number>[0-9]+))$",
  3.     System.Text.RegularExpressions.RegexOptions.IgnoreCase |
  4.     System.Text.RegularExpressions.RegexOptions.Singleline
  5. );
  6.  
  7. IMyProgrammableBlock thisCPU;
  8.  
  9. void FindProgrammableBlock()
  10. {
  11.     List<IMyTerminalBlock> blocks = GridTerminalSystem.Blocks.FindAll(b => b is IMyProgrammableBlock && (b as IMyProgrammableBlock).IsRunning);
  12.  
  13.     if(blocks.Count > 1)
  14.     {
  15.         throw new Exception("More than one running CPU running; unable to find this executing CPU.");
  16.     }
  17.  
  18.     thisCPU = blocks[0] as IMyProgrammableBlock;
  19. }
  20.  
  21. void Main()
  22. {
  23.     FindProgrammableBlock();
  24.  
  25.     List<IMyTerminalBlock> lightList = new List<IMyTerminalBlock>();
  26.     GridTerminalSystem.GetBlocksOfType<IMyInteriorLight>(lightList);
  27.  
  28.     Dictionary<string, List<IMyInteriorLight>> groupLightList = new Dictionary<string, List<IMyInteriorLight>>();
  29.  
  30.     for (int i = 0; i < lightList.Count; i++)
  31.     {
  32.         IMyInteriorLight light = lightList[i] as IMyInteriorLight;
  33.  
  34.         if (light.CubeGrid != thisCPU.CubeGrid)
  35.         {
  36.             continue;
  37.         }
  38.  
  39.         string name = light.CustomName;
  40.  
  41.         System.Text.RegularExpressions.Match match = reLinearSignalName.Match(name);
  42.         int signalId = 0;
  43.  
  44.         if (match != null && match.Success && match.Groups.Count > 1)
  45.         {
  46.             int.TryParse(match.Groups["number"].Value, out signalId);
  47.  
  48.             string groupName = (match.Groups["group"].Value ?? string.Empty).ToLower();
  49.             bool isOn = (light as IMyFunctionalBlock).Enabled;
  50.  
  51.             List<IMyInteriorLight> l = new List<IMyInteriorLight>();
  52.  
  53.             if(groupLightList.ContainsKey(groupName))
  54.             {
  55.                 l = groupLightList[groupName];
  56.             }
  57.             else
  58.             {
  59.                 groupLightList.Add(groupName, l);
  60.             }
  61.  
  62.             l.Add(light);
  63.         }
  64.     }
  65.  
  66.     ChangeLights(groupLightList);
  67. }
  68.  
  69. void ChangeLights(Dictionary<string, List<IMyInteriorLight>> groupLightList)
  70. {
  71.     List<string> keys = new List<string>(groupLightList.Keys);
  72.  
  73.     for (int i = 0; i < keys.Count; i++)
  74.     {
  75.         string key = keys[i];
  76.         List<IMyInteriorLight> l = groupLightList[key];
  77.  
  78.         l.Sort(SortLinearLightList);
  79.  
  80.         int turnOn = 0;
  81.  
  82.         for (int j = 0; j < l.Count; j++)
  83.         {
  84.             if ((l[j] as IMyFunctionalBlock).Enabled)
  85.             {
  86.                 turnOn = j + 1;
  87.                 if (turnOn >= l.Count) turnOn = 0;
  88.                 break;
  89.             }
  90.         }
  91.  
  92.         for (int j = 0; j < l.Count; j++)
  93.         {
  94.             if (j == turnOn)
  95.             {
  96.                 l[j].ApplyAction("OnOff_On");
  97.             }
  98.             else
  99.             {
  100.                 l[j].ApplyAction("OnOff_Off");
  101.             }
  102.         }
  103.     }
  104. }
  105.  
  106. int SortLinearLightList(IMyInteriorLight x, IMyInteriorLight y)
  107. {
  108.     System.Text.RegularExpressions.Match xMatch = reLinearSignalName.Match(x.CustomName);
  109.     int xNum = int.Parse(xMatch.Groups["number"].Value);
  110.  
  111.     System.Text.RegularExpressions.Match yMatch = reLinearSignalName.Match(y.CustomName);
  112.     int yNum = int.Parse(yMatch.Groups["number"].Value);
  113.  
  114.     int ret = 0;
  115.  
  116.     if(xNum < yNum)
  117.     {
  118.         ret = -1;
  119.     }
  120.     else if(yNum > xNum)
  121.     {
  122.         ret = 1;
  123.     }
  124.  
  125.     return ret;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement