Phoenix84

ship controller

May 22nd, 2016 (edited)
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.60 KB | None | 0 0
  1.         public static IMyTerminalBlock GetShipController(this IMyFunctionalBlock ftl)
  2.         {
  3.             IMyTerminalBlock reference = null;
  4.             var cockpitObjectBuilder = new MyObjectBuilderType(typeof(MyObjectBuilder_Cockpit));
  5.             var rcObjectBuilder = new MyObjectBuilderType(typeof(MyObjectBuilder_RemoteControl));
  6.  
  7.             var cockpitlist = ftl.GetGroupedBlocks(cockpitObjectBuilder);
  8.             var rclist = ftl.GetGroupedBlocks(rcObjectBuilder);
  9.             cockpitlist.UnionWith(rclist);
  10.  
  11.             if (cockpitlist.Count != 0)
  12.             {
  13.                 // If it's grouped, just find the first non-passenger seat one
  14.                 foreach (var cockpit in cockpitlist)
  15.                 {
  16.                     var definition = MyDefinitionManager.Static.GetCubeBlockDefinition(cockpit.BlockDefinition);
  17.  
  18.                     if (definition != null && definition is MyShipControllerDefinition && (definition as MyShipControllerDefinition).EnableShipControl)
  19.                     {
  20.                         //if (Globals.Debug)
  21.                         //    MyAPIGateway.Utilities.ShowNotification("Found grouped cockpit: " + cockpit.CustomName, 5000);
  22.  
  23.                         reference = cockpit;
  24.  
  25.                         // If this is a main cockpit, stop searching for any others and use this one
  26.                         if (((cockpit as IMyCubeBlock).GetObjectBuilderCubeBlock() as MyObjectBuilder_ShipController).IsMainCockpit)
  27.                             break;
  28.                     }
  29.                 }
  30.                 return reference;       // If there were any grouped cockpits, never continue looking
  31.             }
  32.  
  33.             var blocks = new List<IMySlimBlock>();
  34.             var filterBlocks = new HashSet<IMySlimBlock>();
  35.  
  36.             // Get a list of cockpit/remote control blocks
  37.             // Might not work with modded cockpits
  38.             (ftl.CubeGrid as IMyCubeGrid).GetBlocks(blocks,
  39.                 (x) => x.FatBlock != null
  40.                 && (x.FatBlock.BlockDefinition.TypeId == cockpitObjectBuilder
  41.                     || x.FatBlock.BlockDefinition.TypeId == rcObjectBuilder
  42.                    )
  43.                 );
  44.  
  45.             // Loop through and filter by non-passenger
  46.             foreach (var block in blocks)
  47.             {
  48.                 var definition = MyDefinitionManager.Static.GetCubeBlockDefinition(block.FatBlock.BlockDefinition);
  49.  
  50.                 if (definition != null && definition is MyShipControllerDefinition && (definition as MyShipControllerDefinition).EnableShipControl)
  51.                     filterBlocks.Add(block);
  52.             }
  53.  
  54.             //if (Globals.Debug)
  55.             //    MyAPIGateway.Utilities.ShowNotification("Cockpit count: " + filterBlocks.Count);
  56.  
  57.             if (filterBlocks.Count == 1)
  58.             {
  59.                 //if (Globals.Debug)
  60.                 //    MyAPIGateway.Utilities.ShowNotification("Found one cockpit: " + filterBlocks[0].FatBlock.DisplayNameText);
  61.                 // If we only found one cockpit, use that
  62.                 reference = filterBlocks.FirstElement().FatBlock as IMyTerminalBlock;
  63.             }
  64.             else if (filterBlocks.Count > 1)
  65.             {
  66.                 foreach (var block in filterBlocks)
  67.                 {
  68.                     if (block.FatBlock.DisplayNameText.Contains("FTL"))
  69.                     {
  70.                         if (Globals.Debug)
  71.                             MyAPIGateway.Utilities.ShowNotification("Found named cockpit: " + block.FatBlock.DisplayNameText);
  72.                         reference = block.FatBlock as IMyTerminalBlock;
  73.                         break;
  74.                     }
  75.                     else if (((block.FatBlock as IMyCubeBlock).GetObjectBuilderCubeBlock() as MyObjectBuilder_ShipController).IsMainCockpit)
  76.                     {
  77.                         if (Globals.Debug)
  78.                             MyAPIGateway.Utilities.ShowNotification("Found main control cockpit: " + block.FatBlock.DisplayNameText);
  79.                         reference = block.FatBlock as IMyTerminalBlock;
  80.                         break;
  81.                     }
  82.                     else if (((block.FatBlock as IMyCubeBlock).GetObjectBuilderCubeBlock() as MyObjectBuilder_ShipController).ControlThrusters)
  83.                     {
  84.                         if (Globals.Debug)
  85.                             MyAPIGateway.Utilities.ShowNotification("Found thruster control cockpit: " + block.FatBlock.DisplayNameText);
  86.                         reference = block.FatBlock as IMyTerminalBlock;
  87.                     }
  88.                 }
  89.             }
  90.  
  91.             return reference;
  92.         }
Advertisement
Add Comment
Please, Sign In to add comment