/// <summary> /// Place the matching smart rotor on top of this rotor. This is called in an separate thread. /// </summary> /// <param name="workData">The work data used in this method.</param> private void PlacePad(WorkData workData) { using (Log.BeginMethod(nameof(PlacePad))) { var data = workData as PlacePadData; if (data?.Head == null) { data?.FlagAsFailed(); return; } var head = data.Head; var cubeGrid = head.CubeGrid; var gridSize = cubeGrid.GridSize; var matrix = head.WorldMatrix; var up = matrix.Up; var left = matrix.Left; var forward = matrix.Forward; var headPosition = head.GetPosition(); var baseSubtype = Stator.BlockDefinition.SubtypeId; var headSubType = head.BlockDefinition.SubtypeId; string padSubtype; if (!Mod.Static.Defs.BaseToPad.TryGetValue(baseSubtype, out padSubtype)) { Log.Error($"No matching pad found for '{baseSubtype}'"); data.FlagAsFailed(); return; } Vector3D origin; switch (headSubType) { case Defs.Part.NORMAL: origin = headPosition + left * gridSize * 2 + forward * gridSize; break; case Defs.Part.LARGE_NORMAL: origin = headPosition + left * (gridSize * 5) + forward * gridSize; break; case Defs.Part.SMALL: origin = headPosition + left + forward * gridSize; break; case Defs.Part.LARGE_SMALL: origin = headPosition + left * (gridSize * 5) + forward * gridSize; break; default: Log.Error(new Exception($"Unknown PocketGearPart SubtypeId: {headSubType}")); data.FlagAsFailed(); return; } var padPosition = cubeGrid.WorldToGridInteger(origin); if (cubeGrid.CubeExists(padPosition)) { var slimBlock = cubeGrid.GetCubeBlock(padPosition); var pad = slimBlock?.FatBlock as IMyLandingGear; if (pad != null) { if (pad.BlockDefinition.SubtypeId == padSubtype) { Pad = pad; Log.Debug("Pad is already there."); data.FlagAsSucceeded(); return; } } Log.Error(ERROR_BUILD_SPOT_OCCUPIED); MyAPIGateway.Utilities.ShowNotification(ERROR_BUILD_SPOT_OCCUPIED); data.FlagAsFailed(); return; } var canPlaceCube = cubeGrid.CanAddCube(padPosition); if (!canPlaceCube) { Log.Error(ERROR_UNABLE_TO_PLACE); MyAPIGateway.Utilities.ShowNotification(ERROR_UNABLE_TO_PLACE); data.FlagAsFailed(); return; } try { var colorMask = Stator.SlimBlock.ColorMaskHSV; var buildPercent = head.SlimBlock.IsFullIntegrity ? 1 : 0.00001525902f; var padBuilder = new MyObjectBuilder_MotorAdvancedStator { SubtypeName = padSubtype, Owner = Stator.OwnerId, BuiltBy = Stator.OwnerId, BuildPercent = buildPercent, IntegrityPercent = buildPercent, LimitsActive = true, MaxAngle = MathHelper.ToRadians(195), MinAngle = MathHelper.ToRadians(-15), Min = padPosition, BlockOrientation = new SerializableBlockOrientation(head.Orientation.Forward, head.Orientation.Up), ColorMaskHSV = colorMask }; MyAPIGateway.Utilities.InvokeOnGameThread(() => { cubeGrid.AddBlock(padBuilder, false); var slimBlock = cubeGrid.GetCubeBlock(padPosition); slimBlock.UpdateVisual(); Pad = slimBlock?.FatBlock as IMyLandingGear; Log.Debug("Pad should now be placed."); data.FlagAsSucceeded(); }); } catch (Exception exception) { Log.Error(exception); Log.Error(exception.StackTrace); data.FlagAsFailed(); } } }
/// <inheritdoc /> protected override void PlaceSmartHinge(WorkData workData) { using (Log.BeginMethod(nameof(PlaceSmartHinge))) { var data = workData as PlaceSmartHingeData; if (data?.Head == null) { return; } var head = data.Head; var cubeGrid = head.CubeGrid; var gridSize = cubeGrid.GridSize; var matrix = head.WorldMatrix; var up = matrix.Up; var left = matrix.Left; var forward = matrix.Forward; var headPosition = head.GetPosition(); var baseSubtype = Stator.BlockDefinition.SubtypeId; Vector3D origin; switch (baseSubtype) { case Defs.SolarDefs.LB_SMART_SOLAR_BASE: case Defs.SolarDefs.LB_SMART_SOLAR_BASE_TYPE_B: default: origin = headPosition + up * gridSize; break; case Defs.SolarDefs.SB_SMART_SOLAR_BASE_TYPE_B: origin = headPosition + up + left * gridSize + forward * gridSize; break; } var hingePosition = cubeGrid.WorldToGridInteger(origin); string hingeSubtype; if (!Mod.Static.Defs.Solar.BaseToHinge.TryGetValue(baseSubtype, out hingeSubtype)) { Log.Error($"No matching hinge found for '{baseSubtype}'"); data.FlagAsFailed(); return; } if (cubeGrid.CubeExists(hingePosition)) { var slimBlock = cubeGrid.GetCubeBlock(hingePosition); var hinge = slimBlock?.FatBlock as IMyMotorAdvancedStator; if (hinge != null) { if (hinge.BlockDefinition.SubtypeId == hingeSubtype) { data.FlagAsSucceeded(); return; } } Log.Error(ERROR_BUILD_SPOT_OCCUPIED); MyAPIGateway.Utilities.ShowNotification(ERROR_BUILD_SPOT_OCCUPIED); data.FlagAsFailed(); return; } var canPlaceCube = cubeGrid.CanAddCube(hingePosition); if (!canPlaceCube) { Log.Error(ERROR_UNABLE_TO_PLACE); MyAPIGateway.Utilities.ShowNotification(ERROR_UNABLE_TO_PLACE); data.FlagAsFailed(); } try { var colorMask = Stator.SlimBlock.ColorMaskHSV; var skin = Stator.SlimBlock.SkinSubtypeId; var buildPercent = head.SlimBlock.IsFullIntegrity ? 1 : 0.00001525902f; var hingeBuilder = new MyObjectBuilder_MotorAdvancedStator { SubtypeName = hingeSubtype, Owner = Stator.OwnerId, BuiltBy = Stator.OwnerId, BuildPercent = buildPercent, IntegrityPercent = buildPercent, LimitsActive = true, MaxAngle = MathHelper.ToRadians(195), MinAngle = MathHelper.ToRadians(-15), Min = hingePosition, BlockOrientation = new SerializableBlockOrientation(head.Orientation.Up, head.Orientation.Left), ColorMaskHSV = colorMask }; MyAPIGateway.Utilities.InvokeOnGameThread(() => { cubeGrid.AddBlock(hingeBuilder, false); var slimBlock = cubeGrid.GetCubeBlock(hingePosition); var hinge = slimBlock?.FatBlock as IMyMotorAdvancedStator; if (hinge != null) { if (hinge.BlockDefinition.SubtypeId == hingeSubtype) { List <IMyTerminalAction> defaultActions; MyAPIGateway.TerminalControls.GetActions <IMyMotorAdvancedStator>(out defaultActions); var attach = defaultActions.FirstOrDefault(x => x.Id == ADD_HEAD_ACTION_ID)?.Action; if (attach == null) { return; } attach(hinge); if (hinge.Top != null) { var hingePartCubeGrid = hinge.TopGrid as MyCubeGrid; if (hingePartCubeGrid != null) { hingePartCubeGrid.ChangeColorAndSkin(hingePartCubeGrid.GetCubeBlock(hinge.Top.Position), colorMask, skin); } if (head.SlimBlock.IsFullIntegrity) { var topSlimBlock = hinge.Top.SlimBlock; var welderMountAmount = topSlimBlock.MaxIntegrity - topSlimBlock.Integrity; topSlimBlock.IncreaseMountLevel(welderMountAmount, hinge.OwnerId); } data.FlagAsSucceeded(); } } } }); } catch (Exception exception) { Log.Error(exception); Log.Error(exception.StackTrace); data.FlagAsFailed(); } } }
private static void CheckGridConnection(IMyEntity block) { IMyCubeBlock cubeBlock = (IMyCubeBlock)block; if (cubeBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_PistonBase)) { MyObjectBuilder_PistonBase pistonBase = (MyObjectBuilder_PistonBase)cubeBlock.GetObjectBuilderCubeBlock(); IMyEntity connectedEntity = null; if (pistonBase.TopBlockId.HasValue && MyAPIGateway.Entities.TryGetEntityById(pistonBase.TopBlockId.Value, out connectedEntity)) { IMyEntity parent = connectedEntity.Parent; if (parent is IMyCubeGrid) { if (!m_conveyorConnected.ContainsKey(parent.EntityId)) { m_conveyorConnected.Add(parent.EntityId, new long[] { pistonBase.TopBlockId.Value, pistonBase.EntityId }); } } } } else if (cubeBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_ExtendedPistonBase)) { MyObjectBuilder_PistonBase pistonBase = (MyObjectBuilder_PistonBase)cubeBlock.GetObjectBuilderCubeBlock(); IMyEntity connectedEntity = null; if (pistonBase.TopBlockId.HasValue && MyAPIGateway.Entities.TryGetEntityById(pistonBase.TopBlockId.Value, out connectedEntity)) { IMyEntity parent = connectedEntity.Parent; if (parent is IMyCubeGrid) { if (!m_conveyorConnected.ContainsKey(parent.EntityId)) { m_conveyorConnected.Add(parent.EntityId, new long[] { pistonBase.TopBlockId.Value, pistonBase.EntityId }); } } } } else if (cubeBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_ShipConnector)) { MyObjectBuilder_ShipConnector connector = (MyObjectBuilder_ShipConnector)cubeBlock.GetObjectBuilderCubeBlock(); IMyEntity connectedEntity = null; if (connector.Connected && MyAPIGateway.Entities.TryGetEntityById(connector.ConnectedEntityId, out connectedEntity)) { if (!m_conveyorConnected.ContainsKey(connectedEntity.Parent.EntityId)) { //IMyCubeGrid grid = MyAPIGateway.Entities.GetEntityById(connectedEntity.Parent.EntityId); m_conveyorConnected.Add(connectedEntity.Parent.EntityId, new long[] { connectedEntity.EntityId, connector.EntityId }); } } } else if (cubeBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_MotorAdvancedStator)) { MyObjectBuilder_MotorAdvancedStator stator = (MyObjectBuilder_MotorAdvancedStator)cubeBlock.GetObjectBuilderCubeBlock(); IMyEntity connectedEntity = null; if (stator.RotorEntityId.HasValue && MyAPIGateway.Entities.TryGetEntityById(stator.RotorEntityId.Value, out connectedEntity)) { IMyEntity parent = connectedEntity.Parent; if (parent is IMyCubeGrid) { if (!m_conveyorConnected.ContainsKey(parent.EntityId)) { m_conveyorConnected.Add(parent.EntityId, new long[] { stator.RotorEntityId.Value, stator.EntityId }); } } } } }
/// <inheritdoc /> protected override void PlaceSmartHinge(WorkData workData) { using (Mod.PROFILE ? Profiler.Measure(nameof(SmartSolarRotorBase), nameof(PlaceSmartHinge)) : null) { using (Log.BeginMethod(nameof(PlaceSmartHinge))) { var data = workData as PlaceSmartHingeData; if (data?.Head == null) { return; } var head = data.Head; var cubeGrid = head.CubeGrid; var gridSize = cubeGrid.GridSize; var matrix = head.WorldMatrix; var up = matrix.Up; var left = matrix.Left; var headPosition = head.GetPosition(); var origin = headPosition + up * gridSize; var hingePosition = cubeGrid.WorldToGridInteger(origin); if (cubeGrid.CubeExists(hingePosition)) { Log.Debug($"There is already a block on this position: {hingePosition}."); return; } var canPlaceCube = cubeGrid.CanAddCube(hingePosition); if (!canPlaceCube) { Log.Debug($"Unable to place block on this position: {hingePosition}."); } try { var instantBuild = MyAPIGateway.Session.CreativeMode || MyAPIGateway.Session.HasCreativeRights && MyAPIGateway.Session.EnableCopyPaste; var buildPercent = instantBuild ? 1 : 0.00001525902f; var hingeBuilder = new MyObjectBuilder_MotorAdvancedStator { SubtypeName = "MA_SmartRotor_Solar_Hinge", Owner = Stator.OwnerId, BuiltBy = Stator.OwnerId, BuildPercent = buildPercent, IntegrityPercent = buildPercent, LimitsActive = true, MaxAngle = MathHelper.ToRadians(195), MinAngle = MathHelper.ToRadians(-15), CustomName = "Auto Placed" }; var cubeGridBuilder = new MyObjectBuilder_CubeGrid { CreatePhysics = true, GridSizeEnum = head.CubeGrid.GridSizeEnum, PositionAndOrientation = new MyPositionAndOrientation(origin, up, left) }; cubeGridBuilder.CubeBlocks.Add(hingeBuilder); var gridsToMerge = new List <MyObjectBuilder_CubeGrid> { cubeGridBuilder }; MyAPIGateway.Utilities.InvokeOnGameThread(() => (cubeGrid as MyCubeGrid)?.PasteBlocksToGrid(gridsToMerge, 0, false, false)); } catch (Exception exception) { Log.Error(exception); } } } }
private static List <IMyCubeGrid> GetConnectedGridList(HashSet <IMyEntity> checkedGrids, List <IMySlimBlock> blocks) { List <IMyCubeGrid> connectedGrids = new List <IMyCubeGrid>(); foreach (IMySlimBlock slimBlock in blocks) { try { if (slimBlock.FatBlock != null && slimBlock.FatBlock is IMyCubeBlock) { IMyCubeBlock cubeBlock = (IMyCubeBlock)slimBlock.FatBlock; // Check for Piston if (cubeBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_PistonBase)) { MyObjectBuilder_PistonBase pistonBase = (MyObjectBuilder_PistonBase)cubeBlock.GetObjectBuilderCubeBlock(); IMyEntity entity = null; if (MyAPIGateway.Entities.TryGetEntityById(pistonBase.TopBlockId, out entity)) { IMyCubeGrid parent = (IMyCubeGrid)entity.Parent; if (!checkedGrids.Contains(parent)) { connectedGrids.Add(parent); } } } else if (cubeBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_ExtendedPistonBase)) { MyObjectBuilder_PistonBase pistonBase = (MyObjectBuilder_PistonBase)cubeBlock.GetObjectBuilderCubeBlock(); IMyEntity entity = null; if (MyAPIGateway.Entities.TryGetEntityById(pistonBase.TopBlockId, out entity)) { IMyCubeGrid parent = (IMyCubeGrid)entity.Parent; if (!checkedGrids.Contains(parent)) { connectedGrids.Add(parent); } } } // Connector else if (cubeBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_ShipConnector)) { MyObjectBuilder_ShipConnector connector = (MyObjectBuilder_ShipConnector)cubeBlock.GetObjectBuilderCubeBlock(); IMyEntity entity = null; if (MyAPIGateway.Entities.TryGetEntityById(connector.ConnectedEntityId, out entity)) //IMyEntity entity = MyAPIGateway.Entities.GetEntityById(connector.ConnectedEntityId); //if(entity != null) { IMyCubeGrid parent = (IMyCubeGrid)entity.Parent; if (!checkedGrids.Contains(parent)) { connectedGrids.Add(parent); } } } else if (cubeBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_MotorAdvancedStator)) { MyObjectBuilder_MotorAdvancedStator stator = (MyObjectBuilder_MotorAdvancedStator)cubeBlock.GetObjectBuilderCubeBlock(); IMyEntity connectedEntity = null; if (stator.RotorEntityId.HasValue && MyAPIGateway.Entities.TryGetEntityById(stator.RotorEntityId.Value, out connectedEntity)) { IMyCubeGrid parent = (IMyCubeGrid)connectedEntity.Parent; if (!checkedGrids.Contains(parent)) { connectedGrids.Add(parent); } } } } } catch { continue; } } return(connectedGrids); }