Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static IMyTerminalBlock GetShipController(this IMyFunctionalBlock ftl)
- {
- IMyTerminalBlock reference = null;
- var cockpitObjectBuilder = new MyObjectBuilderType(typeof(MyObjectBuilder_Cockpit));
- var rcObjectBuilder = new MyObjectBuilderType(typeof(MyObjectBuilder_RemoteControl));
- var cockpitlist = ftl.GetGroupedBlocks(cockpitObjectBuilder);
- var rclist = ftl.GetGroupedBlocks(rcObjectBuilder);
- cockpitlist.UnionWith(rclist);
- if (cockpitlist.Count != 0)
- {
- // If it's grouped, just find the first non-passenger seat one
- foreach (var cockpit in cockpitlist)
- {
- var definition = MyDefinitionManager.Static.GetCubeBlockDefinition(cockpit.BlockDefinition);
- if (definition != null && definition is MyShipControllerDefinition && (definition as MyShipControllerDefinition).EnableShipControl)
- {
- //if (Globals.Debug)
- // MyAPIGateway.Utilities.ShowNotification("Found grouped cockpit: " + cockpit.CustomName, 5000);
- reference = cockpit;
- // If this is a main cockpit, stop searching for any others and use this one
- if (((cockpit as IMyCubeBlock).GetObjectBuilderCubeBlock() as MyObjectBuilder_ShipController).IsMainCockpit)
- break;
- }
- }
- return reference; // If there were any grouped cockpits, never continue looking
- }
- var blocks = new List<IMySlimBlock>();
- var filterBlocks = new HashSet<IMySlimBlock>();
- // Get a list of cockpit/remote control blocks
- // Might not work with modded cockpits
- (ftl.CubeGrid as IMyCubeGrid).GetBlocks(blocks,
- (x) => x.FatBlock != null
- && (x.FatBlock.BlockDefinition.TypeId == cockpitObjectBuilder
- || x.FatBlock.BlockDefinition.TypeId == rcObjectBuilder
- )
- );
- // Loop through and filter by non-passenger
- foreach (var block in blocks)
- {
- var definition = MyDefinitionManager.Static.GetCubeBlockDefinition(block.FatBlock.BlockDefinition);
- if (definition != null && definition is MyShipControllerDefinition && (definition as MyShipControllerDefinition).EnableShipControl)
- filterBlocks.Add(block);
- }
- //if (Globals.Debug)
- // MyAPIGateway.Utilities.ShowNotification("Cockpit count: " + filterBlocks.Count);
- if (filterBlocks.Count == 1)
- {
- //if (Globals.Debug)
- // MyAPIGateway.Utilities.ShowNotification("Found one cockpit: " + filterBlocks[0].FatBlock.DisplayNameText);
- // If we only found one cockpit, use that
- reference = filterBlocks.FirstElement().FatBlock as IMyTerminalBlock;
- }
- else if (filterBlocks.Count > 1)
- {
- foreach (var block in filterBlocks)
- {
- if (block.FatBlock.DisplayNameText.Contains("FTL"))
- {
- if (Globals.Debug)
- MyAPIGateway.Utilities.ShowNotification("Found named cockpit: " + block.FatBlock.DisplayNameText);
- reference = block.FatBlock as IMyTerminalBlock;
- break;
- }
- else if (((block.FatBlock as IMyCubeBlock).GetObjectBuilderCubeBlock() as MyObjectBuilder_ShipController).IsMainCockpit)
- {
- if (Globals.Debug)
- MyAPIGateway.Utilities.ShowNotification("Found main control cockpit: " + block.FatBlock.DisplayNameText);
- reference = block.FatBlock as IMyTerminalBlock;
- break;
- }
- else if (((block.FatBlock as IMyCubeBlock).GetObjectBuilderCubeBlock() as MyObjectBuilder_ShipController).ControlThrusters)
- {
- if (Globals.Debug)
- MyAPIGateway.Utilities.ShowNotification("Found thruster control cockpit: " + block.FatBlock.DisplayNameText);
- reference = block.FatBlock as IMyTerminalBlock;
- }
- }
- }
- return reference;
- }
Advertisement
Add Comment
Please, Sign In to add comment