private void removePilotToolStripMenuItem_Click(object sender, EventArgs e) { TreeNode node = SectorTree.SelectedNode; MyObjectBuilder_Sector sector = (MyObjectBuilder_Sector)node.Parent.Parent.Tag; Console.WriteLine("in method"); if (node.Nodes.ContainsKey("Pilot")) { Console.WriteLine("Contains Pilot"); TreeNode pilotNode = node.Nodes[node.Nodes.IndexOfKey("Pilot")]; MyObjectBuilder_Cockpit cockpit = (MyObjectBuilder_Cockpit)pilotNode.Tag; long Pilot_id = 0; if (cockpit.Pilot != null) { DialogResult dialogResult = MessageBox.Show("Overwrite \"" + SectorTree.SelectedNode.Parent.Parent.Text + "\"?", "Save", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { MyObjectBuilder_Character pilot = (MyObjectBuilder_Character)cockpit.Pilot; MyObjectBuilder_Character newPilot = (MyObjectBuilder_Character)pilot.Clone(); MyPositionAndOrientation newpos = (MyPositionAndOrientation)newPilot.PositionAndOrientation; newpos.Position.X = 0; newpos.Position.Y = 0; newpos.Position.Z = 0; newPilot.PositionAndOrientation = newpos; newPilot.AutoenableJetpackDelay = 0; newPilot.JetpackEnabled = true; newPilot.Battery.ProducerEnabled = true; cockpit.Pilot = null; sector.SectorObjects.Add(newPilot); Pilot_id = newPilot.EntityId; string file = Path.Combine(this.saves_path, SectorTree.SelectedNode.Parent.Parent.Text, "SANDBOX_0_0_0_.sbs"); string backupfile = Path.Combine(this.saves_path, SectorTree.SelectedNode.Parent.Parent.Text, "SANDBOX_0_0_0_.BAK2"); if (File.Exists(backupfile)) { File.Delete(backupfile); } File.Move(file, backupfile); label1.Text = "Saving..."; MyObjectBuilder_Sector mySector = (MyObjectBuilder_Sector)SectorTree.SelectedNode.Parent.Parent.Tag; using (FileStream sr = File.Open(file, FileMode.Create)) { MyObjectBuilder_Base.SerializeXML(sr, mySector); sr.Dispose(); } //now update the sbc file String sbcfile = Path.Combine(this.saves_path, node.Parent.Parent.Text, "Sandbox.sbc"); XDocument sbcFile = XDocument.Load(sbcfile); sbcFile.Root.SetElementValue("ControlledObject", Pilot_id); sbcFile.Save(sbcfile); this.create_tree(node.Parent.Parent, mySector); label1.Text = ""; } else { return; } } } }
private TreeNode newCubeGridNode(MyObjectBuilder_CubeGrid myGrid) { TreeNode node = null; string hasPilot = ""; TreeNode pilotNode = null; foreach (MyObjectBuilder_CubeBlock cb in myGrid.CubeBlocks) { if (cb.TypeId == MyObjectBuilderTypeEnum.Cockpit) { MyObjectBuilder_Cockpit cockpit = (MyObjectBuilder_Cockpit)cb; if (cockpit.Pilot != null) { hasPilot = "**"; pilotNode = new TreeNode("Pilot"); pilotNode.Tag = cockpit; pilotNode.Name = "Pilot"; } } } if (myGrid.GridSizeEnum == MyCubeSize.Small) { node = new TreeNode("Small Ship [" + myGrid.CubeBlocks.Count() + "]" + hasPilot); } if (myGrid.GridSizeEnum == MyCubeSize.Large) { if (myGrid.IsStatic) { node = new TreeNode("Station [" + myGrid.CubeBlocks.Count() + "]" + hasPilot); } else { node = new TreeNode("Large Ship [" + myGrid.CubeBlocks.Count() + "]" + hasPilot); } } node.Tag = myGrid; node.Nodes.Add(new TreeNode(myGrid.EntityId.ToString())); node.Nodes.Add(new TreeNode(myGrid.PositionAndOrientation.ToString())); node.Nodes.Add(myGrid.CubeBlocks.Count() + " Blocks"); if (hasPilot != "") { node.Nodes.Add(pilotNode); } return(node); }
//public static ObservableCollection<InventoryEditorModel> GetInventory(this MyObjectBuilder_EntityBase objectBuilderBase) //{ // var inventoryEditors = new ObservableCollection<InventoryEditorModel>(); // if (objectBuilderBase.ComponentContainer != null) // { // var inventoryBase = objectBuilderBase.ComponentContainer.Components.FirstOrDefault(e => e.TypeId == "MyInventoryBase"); // if (inventoryBase != null) // { // var singleInventory = inventoryBase.Component as MyObjectBuilder_Inventory; // if (singleInventory != null) // { // var iem = ParseInventory(singleInventory); // if (iem != null) // inventoryEditors.Add(iem); // } // var aggregate = inventoryBase.Component as MyObjectBuilder_InventoryAggregate; // if (aggregate != null) // foreach (var field in aggregate.Inventories) // { // var iem = ParseInventory(field as MyObjectBuilder_Inventory); // if (iem != null) // inventoryEditors.Add(iem); // } // } // } // return inventoryEditors; //} public static List <MyObjectBuilder_Character> GetHierarchyCharacters(this MyObjectBuilder_CubeBlock cube) { List <MyObjectBuilder_Character> list = new List <MyObjectBuilder_Character>(); MyObjectBuilder_Cockpit cockpit = cube as MyObjectBuilder_Cockpit; if (cockpit == null) { return(list); } var hierarchyBase = cockpit.ComponentContainer.Components.FirstOrDefault(e => e.TypeId == "MyHierarchyComponentBase")?.Component as MyObjectBuilder_HierarchyComponentBase; if (hierarchyBase != null) { list.AddRange(hierarchyBase.Children.Where(e => e is MyObjectBuilder_Character).Cast <MyObjectBuilder_Character>()); } return(list); }
/// <summary> /// Removes all sign of a pilot/characrter from a cockpit cube. /// </summary> /// <param name="cockpit">The specific cube.</param> /// <param name="character">Specific character to remove, if required, otherwise ANY chararcter will be removed.</param> /// <returns>Returns true if a character was removed.</returns> public static bool RemoveHierarchyCharacter(this MyObjectBuilder_Cockpit cockpit, MyObjectBuilder_Character character = null) { bool retValue = false; MyObjectBuilder_ComponentContainer.ComponentData hierarchyComponentBase = cockpit.ComponentContainer?.Components?.FirstOrDefault(e => e.TypeId == "MyHierarchyComponentBase"); var hierarchyBase = hierarchyComponentBase?.Component as MyObjectBuilder_HierarchyComponentBase; if (hierarchyBase != null && hierarchyBase.Children.Count > 0) { for (int i = 0; i < hierarchyBase.Children.Count; i++) { if (character != null && hierarchyBase.Children[i] == character) { retValue = true; hierarchyBase.Children.RemoveAt(i); i--; break; } if (character == null && hierarchyBase.Children[i] is MyObjectBuilder_Character) { retValue = true; hierarchyBase.Children.RemoveAt(i); i--; } } if (hierarchyBase.Children.Count == 0) { cockpit.ComponentContainer.Components.Remove(hierarchyComponentBase); } } if (retValue) { cockpit.ClearPilotAndAutopilot(); cockpit.PilotRelativeWorld = null; // This should also clear Pilot. cockpit.Pilot = null; } return(retValue); }
public override MyObjectBuilder_CubeBlock GetObjectBuilderCubeBlock(bool copy = false) { MyObjectBuilder_Cockpit objectBuilder = (MyObjectBuilder_Cockpit)base.GetObjectBuilderCubeBlock(copy); objectBuilder.Pilot = (m_pilot != null && m_pilot.Save) ? (MyObjectBuilder_Character)m_pilot.GetObjectBuilder(copy) : null; objectBuilder.Autopilot = (m_aiPilot != null) ? m_aiPilot.GetObjectBuilder() : null; objectBuilder.PilotGunDefinition = m_pilotGunDefinition; if (m_pilotRelativeWorld.HasValue) { objectBuilder.PilotRelativeWorld = new MyPositionAndOrientation(m_pilotRelativeWorld.Value); } else { objectBuilder.PilotRelativeWorld = null; } objectBuilder.IsInFirstPersonView = IsInFirstPersonView; objectBuilder.OxygenLevel = m_oxygenLevel; return(objectBuilder); }
public static void RemoveHierarchyCharacter(this MyObjectBuilder_CubeBlock cube, MyObjectBuilder_Character character) { if (character == null) { return; } MyObjectBuilder_Cockpit cockpit = cube as MyObjectBuilder_Cockpit; var hierarchyBase = cockpit?.ComponentContainer.Components.FirstOrDefault(e => e.TypeId == "MyHierarchyComponentBase")?.Component as MyObjectBuilder_HierarchyComponentBase; if (hierarchyBase != null) { for (int i = 0; i < hierarchyBase.Children.Count; i++) { if (hierarchyBase.Children[i] == character) { hierarchyBase.Children.RemoveAt(i); return; } } } }
public override bool HandleCommand(ulong userId, string[] words) { if (!PluginSettings.Instance.DockingEnabled) { return(false); } if (words.Length < 1) { Communication.SendPrivateInformation(userId, GetHelp()); return(true); } if (m_undocking) { Communication.SendPrivateInformation(userId, string.Format("Server is busy, try again")); return(true); } m_undocking = true; try { String pylonName = String.Join(" ", words); if (PlayerMap.Instance.GetPlayerIdsFromSteamId(userId).Count < 1) { Communication.SendPrivateInformation(userId, string.Format("Unable to find player Id: {0}", userId)); return(true); } long playerId = PlayerMap.Instance.GetPlayerIdsFromSteamId(userId).First( ); Dictionary <String, List <IMyCubeBlock> > testList; List <IMyCubeBlock> beaconList; DockingZone.FindByName(pylonName, out testList, out beaconList, playerId); if (beaconList.Count == 4) { foreach (IMyCubeBlock entity in beaconList) { if (!Entity.CheckOwnership(entity, playerId)) { Communication.SendPrivateInformation(userId, string.Format("You do not have permission to use '{0}'. You must either own all the beacons or they must be shared with faction.", pylonName)); return(true); } } IMyCubeBlock e = beaconList.First( ); IMyCubeGrid parent = (IMyCubeGrid)e.Parent; long[] beaconListIds = beaconList.Select(p => p.EntityId).ToArray( ); long ownerId = beaconList.First( ).OwnerId; List <DockingItem> dockingItems = Docking.Instance.Find(d => d.PlayerId == ownerId && d.TargetEntityId == parent.EntityId && d.DockingBeaconIds.Intersect(beaconListIds).Count( ) == 4); if (dockingItems.Count < 1) { Communication.SendPrivateInformation(userId, string.Format("You have no ships docked in docking zone '{0}'.", pylonName)); return(true); } DockingItem dockingItem = dockingItems.First( ); // Figure out center of docking area, and other distance information double maxDistance = 99; Vector3D vPos = new Vector3D(0, 0, 0); foreach (IMyCubeBlock b in beaconList) { Vector3D beaconPos = Entity.GetBlockEntityPosition(b); vPos += beaconPos; } vPos = vPos / 4; foreach (IMyCubeBlock b in beaconList) { Vector3D beaconPos = Entity.GetBlockEntityPosition(b); maxDistance = Math.Min(maxDistance, Vector3D.Distance(vPos, beaconPos)); } List <IMySlimBlock> blocks = new List <IMySlimBlock>( ); parent.GetBlocks(blocks); foreach (IMySlimBlock slim_cbe in blocks) { if (slim_cbe is IMyCubeBlock) { IMyCubeBlock cbe = slim_cbe.FatBlock; if (cbe.GetObjectBuilderCubeBlock( ) is MyObjectBuilder_Cockpit) { MyObjectBuilder_Cockpit c = (MyObjectBuilder_Cockpit)cbe.GetObjectBuilderCubeBlock( ); if (c.Pilot != null) { Communication.SendPrivateInformation(userId, string.Format( "Carrier ship has a pilot. The carrier should be unpiloted and fully stopped before undocking. (Sometimes this can lag a bit. Wait 10 seconds and try again)", pylonName)); return(true); } } } } String dockedShipFileName = Essentials.PluginPath + String.Format("\\Docking\\docked_{0}_{1}_{2}.sbc", ownerId, dockingItem.TargetEntityId, dockingItem.DockedEntityId); // Load Entity From File and add to game FileInfo fileInfo = new FileInfo(dockedShipFileName); //CubeGridEntity cubeGrid = new CubeGridEntity(fileInfo); MyObjectBuilder_CubeGrid cubeGrid = BaseObjectManager.ReadSpaceEngineersFile <MyObjectBuilder_CubeGrid, MyObjectBuilder_CubeGridSerializer>(dockedShipFileName); // Rotate our ship relative to our saved rotation and the new carrier rotation cubeGrid.PositionAndOrientation = new MyPositionAndOrientation(Matrix.CreateFromQuaternion(Quaternion.CreateFromRotationMatrix(parent.Physics.GetWorldMatrix( ).GetOrientation( )) * dockingItem.SaveQuat).GetOrientation( )); // Move our ship relative to the new carrier position and orientation Quaternion newQuat = Quaternion.CreateFromRotationMatrix(parent.Physics.GetWorldMatrix( ).GetOrientation( )); Vector3D rotatedPos = Vector3D.Transform(dockingItem.SavePos, newQuat); //cubeGrid.Position = rotatedPos + parent.GetPosition(); cubeGrid.PositionAndOrientation = new MyPositionAndOrientation(rotatedPos + parent.GetPosition( ), cubeGrid.PositionAndOrientation.Value.Forward, cubeGrid.PositionAndOrientation.Value.Up); // Add object to world cubeGrid.EntityId = BaseEntity.GenerateEntityId( ); cubeGrid.LinearVelocity = Vector3.Zero; cubeGrid.AngularVelocity = Vector3.Zero; bool undock = false; Wrapper.GameAction(() => { try { MyAPIGateway.Entities.CreateFromObjectBuilderAndAdd(cubeGrid); List <MyObjectBuilder_EntityBase> addList = new List <MyObjectBuilder_EntityBase>( ); addList.Add(cubeGrid); MyAPIGateway.Multiplayer.SendEntitiesCreated(addList); undock = true; } catch (Exception Ex) { Log.Info(string.Format("Error undocking ship: {0}", Ex.ToString( ))); Communication.SendPrivateInformation(userId, string.Format("Unable to undock ship due to error.")); } }); if (!undock) { return(true); } //SectorObjectManager.Instance.AddEntity(cubeGrid); // Remove the docking file File.Delete(dockedShipFileName); Docking.Instance.Remove(dockingItem); Communication.SendPrivateInformation(userId, string.Format("The ship '{0}' has been undocked from docking zone '{1}'", dockingItem.DockedName, pylonName)); /* * // Queue for cooldown * DockingCooldownItem cItem = new DockingCooldownItem(); * cItem.Name = pylonName; * cItem.startTime = DateTime.Now; * * lock (m_cooldownList) * m_cooldownList.Add(cItem); * * IMyEntity gridEntity = MyAPIGateway.Entities.GetEntityById(dockingItem.DockedEntityId); * IMyCubeGrid cubeGrid = (IMyCubeGrid)gridEntity; * * Quaternion q = Quaternion.CreateFromRotationMatrix(parent.WorldMatrix.GetOrientation()) * dockingItem.SaveQuat; * Quaternion newQuat = Quaternion.CreateFromRotationMatrix(parent.WorldMatrix.GetOrientation()); * Vector3 parentPosition = parent.GetPosition(); * Vector3 rotatedPos = Vector3.Transform(dockingItem.savePos, newQuat); * Vector3 position = rotatedPos + parentPosition; * Matrix positionMatrix = Matrix.CreateFromQuaternion(q); * * cubeGrid.ChangeGridOwnership(playerId, MyOwnershipShareModeEnum.None); * gridEntity.SetPosition(dockingItem.savePos); * * gridEntity.WorldMatrix = positionMatrix; * gridEntity.SetPosition(position); * * // We need to update again, as this doesn't seem to sync properly? I set world matrix, and setposition, and it doesn't go where it should, and I * // have to bump into it for it to show up, it's mega weird. * * if (PluginDocking.Settings.DockingItems == null) * throw new Exception("DockingItems is null"); * * // Remove from docked items * PluginDocking.Settings.DockingItems.Remove(dockingItem); * * // Notify user * Communication.SendPrivateInformation(userId, string.Format("The ship '{0}' has been undocked from docking zone '{1}'", gridEntity.DisplayName, pylonName)); */ // Queue for cooldown /* * DockingCooldownItem cItem = new DockingCooldownItem(); * cItem.name = pylonName; * cItem.startTime = DateTime.Now; * PluginDocking.CooldownList.Add(cItem); */ } else if (beaconList.Count > 4) // Too many beacons, must be 4 { Communication.SendPrivateInformation(userId, string.Format("Too many beacons with the name or another zone with the name '{0}'. Place only 4 beacons to create a zone or try a different zone name.", pylonName)); } else // Can't find docking zone { Communication.SendPrivateInformation(userId, string.Format("Can not locate docking zone '{0}'. There must be 4 beacons with the name '{0}' to create a docking zone. Beacons must be fully built!", pylonName)); } } catch (NullReferenceException ex) { Log.Error(ex); } finally { m_undocking = false; } return(true); }
public override void Init(MyObjectBuilder_CubeBlock objectBuilder, MyCubeGrid cubeGrid) { base.Init(objectBuilder, cubeGrid); PostBaseInit(); //MyDebug.AssertDebug(objectBuilder.TypeId == typeof(MyObjectBuilder_Cockpit)); var def = MyDefinitionManager.Static.GetCubeBlockDefinition(objectBuilder.GetId()); m_isLargeCockpit = (def.CubeSize == MyCubeSize.Large); m_cockpitInteriorModel = BlockDefinition.InteriorModel; m_cockpitGlassModel = BlockDefinition.GlassModel; NeedsUpdate = MyEntityUpdateEnum.EACH_100TH_FRAME; MyObjectBuilder_Cockpit cockpitOb = (MyObjectBuilder_Cockpit)objectBuilder; if (cockpitOb.Pilot != null) { MyEntity pilotEntity; MyCharacter pilot = null; if (MyEntities.TryGetEntityById(cockpitOb.Pilot.EntityId, out pilotEntity)) { //Pilot already exists, can be the case after cube grid split pilot = (MyCharacter)pilotEntity; if (pilot.IsUsing is MyShipController && pilot.IsUsing != this) { System.Diagnostics.Debug.Assert(false, "Pilot already sits on another place!"); pilot = null; } } else { pilot = (MyCharacter)MyEntities.CreateFromObjectBuilder(cockpitOb.Pilot); } if (pilot != null) { AttachPilot(pilot, storeOriginalPilotWorld: false, calledFromInit: true); if (cockpitOb.PilotRelativeWorld.HasValue) { m_pilotRelativeWorld = cockpitOb.PilotRelativeWorld.Value.GetMatrix(); } else { m_pilotRelativeWorld = null; } m_singleWeaponMode = cockpitOb.UseSingleWeaponMode; } IsInFirstPersonView = cockpitOb.IsInFirstPersonView; } if (cockpitOb.Autopilot != null) { MyAutopilotBase autopilot = MyAutopilotFactory.CreateAutopilot(cockpitOb.Autopilot); autopilot.Init(cockpitOb.Autopilot); AttachAutopilot(autopilot, updateSync: false); } m_pilotGunDefinition = cockpitOb.PilotGunDefinition; // backward compatibility check for automatic rifle without subtype if (m_pilotGunDefinition.HasValue) { if (m_pilotGunDefinition.Value.TypeId == typeof(MyObjectBuilder_AutomaticRifle) && string.IsNullOrEmpty(m_pilotGunDefinition.Value.SubtypeName)) { m_pilotGunDefinition = new MyDefinitionId(typeof(MyObjectBuilder_AutomaticRifle), "RifleGun"); } } if (!string.IsNullOrEmpty(m_cockpitInteriorModel)) { if (MyModels.GetModelOnlyDummies(m_cockpitInteriorModel).Dummies.ContainsKey("head")) { m_headLocalPosition = MyModels.GetModelOnlyDummies(m_cockpitInteriorModel).Dummies["head"].Matrix.Translation; } } else { if (MyModels.GetModelOnlyDummies(BlockDefinition.Model).Dummies.ContainsKey("head")) { m_headLocalPosition = MyModels.GetModelOnlyDummies(BlockDefinition.Model).Dummies["head"].Matrix.Translation; } } AddDebugRenderComponent(new Components.MyDebugRenderComponentCockpit(this)); InitializeConveyorEndpoint(); AddDebugRenderComponent(new Components.MyDebugRenderComponentDrawConveyorEndpoint(m_conveyorEndpoint)); m_oxygenLevel = cockpitOb.OxygenLevel; }
public CockpitEntity(CubeGridEntity parent, MyObjectBuilder_Cockpit definition, Object backingObject) : base(parent, definition, backingObject) { }
public CockpitEntity(CubeGridEntity parent, MyObjectBuilder_Cockpit definition) : base(parent, definition) { }
public static bool Move(string userName, Vector3D position) { //CharacterEntity charEntity = SectorObjectManager.Instance.GetTypedInternalData<CharacterEntity>().FirstOrDefault(x => x.DisplayName.ToLower() == userName.ToLower() && x.Health > 0); MyObjectBuilder_Character charEntity = FindCharacter(userName); if (charEntity == null) { Essentials.Log.Info(string.Format("Unable to find CharacterEntity of '{0}'", userName)); return(false); } CubeGridEntity gridEntity = new CubeGridEntity(new FileInfo(Essentials.PluginPath + "MovePlayer.sbc")); gridEntity.EntityId = BaseEntity.GenerateEntityId(); foreach (MyObjectBuilder_CubeBlock block in gridEntity.BaseCubeBlocks) { // set ownership MyObjectBuilder_Cockpit cockpit = block as MyObjectBuilder_Cockpit; if (cockpit != null) { cockpit.Pilot = charEntity; } } gridEntity.PositionAndOrientation = new MyPositionAndOrientation(position, Vector3.Forward, Vector3.Up); Wrapper.GameAction(() => { MyObjectBuilder_EntityBase baseEntity = gridEntity.Export(); MyAPIGateway.Entities.RemapObjectBuilder(baseEntity); IMyEntity entity = MyAPIGateway.Entities.CreateFromObjectBuilderAndAdd(baseEntity); Type someManager = SandboxGameAssemblyWrapper.Instance.GetAssemblyType(SectorObjectManager.EntityBaseNetManagerNamespace, SectorObjectManager.EntityBaseNetManagerClass); Wrapper.InvokeStaticMethod(someManager, SectorObjectManager.EntityBaseNetManagerSendEntity, new object[] { entity.GetObjectBuilder() }); gridEntity = new CubeGridEntity((MyObjectBuilder_CubeGrid)entity.GetObjectBuilder(), entity); MyMultiplayer.ReplicateImmediatelly(MyExternalReplicable.FindByObject(entity)); }); DateTime _loadTime = DateTime.Now; while (gridEntity.IsLoading) { if (DateTime.Now - _loadTime > TimeSpan.FromSeconds(20)) { break; } } if (gridEntity.IsLoading) { Essentials.Log.Info("Failed to load cockpit entity: {0}", gridEntity.EntityId); return(false); } foreach (CubeBlockEntity block in gridEntity.CubeBlocks) { if (block is CockpitEntity) { block.IntegrityPercent = 0.1f; } } gridEntity.Dispose(); MyMultiplayer.ReplicateImmediatelly(MyExternalReplicable.FindByObject(gridEntity)); return(true); }
public override bool HandleCommand(ulong userId, string[] words) { if (!PluginSettings.Instance.DockingEnabled) { return(false); } if (words.Length < 1) { Communication.SendPrivateInformation(userId, GetHelp()); return(true); } if (m_docking) { Communication.SendPrivateInformation(userId, "Server is busy"); return(true); } m_docking = true; try { String pylonName = String.Join(" ", words); /* * int timeLeft; * if (Entity.CheckCoolDown(pylonName, out timeLeft)) * { * Communication.Message(String.Format("The docking zone '{0}' is on cooldown. Please wait a {1} seconds before trying to dock/undock again.", pylonName, Math.Max(0, timeLeft))); * return; * } */ if (PlayerMap.Instance.GetPlayerIdsFromSteamId(userId).Count < 1) { Communication.SendPrivateInformation(userId, string.Format("Unable to find player Id: {0}", userId)); return(true); } long playerId = PlayerMap.Instance.GetPlayerIdsFromSteamId(userId).First(); Dictionary <String, List <IMyCubeBlock> > testList; List <IMyCubeBlock> beaconList; DockingZone.FindByName(pylonName, out testList, out beaconList, playerId); if (beaconList.Count == 4) { // Check ownership foreach (IMyCubeBlock entityBlock in beaconList) { IMyTerminalBlock terminal = (IMyTerminalBlock)entityBlock; if (!terminal.HasPlayerAccess(playerId)) { Communication.SendPrivateInformation(userId, string.Format("You do not have permission to use '{0}'. You must either own all the beacons or they must be shared with faction.", pylonName)); return(true); } } // Check for bounding box intsection of other docking zones int intersectElement = 0; if (Entity.CheckForIntersection(testList, beaconList, out intersectElement)) { Communication.SendPrivateInformation(userId, string.Format("The docking zone '{0}' intersects with docking zone '{1}'. Make sure you place your docking zones so they don't overlap.", pylonName, testList.ElementAt(intersectElement).Key)); return(true); } // Check if ship already docked in this zone IMyCubeBlock e = beaconList[0]; IMyCubeGrid parent = (IMyCubeGrid)e.Parent; long[] beaconListIds = beaconList.Select(b => b.EntityId).ToArray(); long ownerId = beaconList.First().OwnerId; List <DockingItem> checkItems = Docking.Instance.Find(d => d.PlayerId == ownerId && d.TargetEntityId == parent.EntityId && d.DockingBeaconIds.Intersect(beaconListIds).Count() == 4); if (checkItems.Count >= PluginSettings.Instance.DockingShipsPerZone) { Communication.SendPrivateInformation(userId, string.Format("Docking zone already '{0}' already contains the maximum capacity of ships.", pylonName)); return(true); } // Figure out center of docking area, and other distance information double maxDistance = 99; Vector3D vPos = new Vector3D(0, 0, 0); foreach (IMyCubeBlock b in beaconList) { Vector3D beaconPos = Entity.GetBlockEntityPosition(b); vPos += beaconPos; } vPos = vPos / 4; foreach (IMyCubeBlock b in beaconList) { Vector3D beaconPos = Entity.GetBlockEntityPosition(b); maxDistance = Math.Min(maxDistance, Vector3D.Distance(vPos, beaconPos)); } // Find ship in docking area IMyCubeGrid dockingEntity = null; HashSet <IMyEntity> cubeGrids = new HashSet <IMyEntity>(); MyAPIGateway.Entities.GetEntities(cubeGrids, f => f is IMyCubeGrid); foreach (IMyCubeGrid gridCheck in cubeGrids) { if (gridCheck.IsStatic || gridCheck == parent) { continue; } double distance = Vector3D.Distance(gridCheck.GetPosition(), vPos); if (distance < maxDistance) { dockingEntity = gridCheck; break; } } // Figure out if the ship fits in docking area, and then save ship if (dockingEntity != null) { // Get bounding box of both the docking zone and docking ship OrientedBoundingBoxD targetBounding = Entity.GetBoundingBox(beaconList); OrientedBoundingBoxD dockingBounding = Entity.GetBoundingBox(dockingEntity); // Make sure the docking zone contains the docking ship. If they intersect or are disjointed, then fail if (!Entity.GreaterThan(dockingBounding.HalfExtent * 2, targetBounding.HalfExtent * 2)) { Communication.SendPrivateInformation(userId, string.Format("The ship '{0}' is too large for it's carrier. The ship's bounding box must fit inside the docking zone bounding box!", dockingEntity.DisplayName)); return(true); } if (targetBounding.Contains(ref dockingBounding) != ContainmentType.Contains) { Communication.SendPrivateInformation(userId, string.Format("The ship '{0}' is not fully inside the docking zone '{1}'. Make sure the ship is fully contained inside the docking zone", dockingEntity.DisplayName, pylonName)); return(true); } // Calculate the mass and ensure the docking ship is less than half the mass of the dock float parentMass = Entity.CalculateMass(parent); float dockingMass = Entity.CalculateMass(dockingEntity); if (dockingMass > parentMass) { Communication.SendPrivateInformation(userId, string.Format("The ship you're trying to dock is too heavy for it's carrier. The ship mass must be less than half the large ship / stations mass! (DM={0}kg CM={1}kg)", dockingMass, parentMass)); return(true); } // Check to see if the ship is piloted, if it is, error out. // TODO: Check to see if we can get a real time copy of this entity? List <IMySlimBlock> blocks = new List <IMySlimBlock>(); dockingEntity.GetBlocks(blocks, x => x.FatBlock != null && x.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_Cockpit)); foreach (IMySlimBlock slim_cbe in blocks) { MyObjectBuilder_Cockpit c = (MyObjectBuilder_Cockpit)slim_cbe.FatBlock.GetObjectBuilderCubeBlock(); if (c.Pilot != null) { Communication.SendPrivateInformation(userId, string.Format("Ship in docking zone '{0}' has a pilot! Please exit the ship before trying to dock. (Sometimes this can lag a bit. Wait 10 seconds and try again)", pylonName)); return(true); } } // Save position and rotation information. Some fun stuff here. // Get our dock rotation as a quaternion Quaternion saveQuat = Quaternion.CreateFromRotationMatrix(parent.WorldMatrix.GetOrientation()); // Transform docked ship's local position by inverse of the the parent (unwinds parent) and save it for when we undock Vector3D savePos = Vector3D.Transform(dockingEntity.GetPosition() - parent.GetPosition(), Quaternion.Inverse(saveQuat)); // Get local rotation of dock ship, and save it for when we undock saveQuat = Quaternion.Inverse(saveQuat) * Quaternion.CreateFromRotationMatrix(dockingEntity.WorldMatrix.GetOrientation()); // Save ship to file and remove FileInfo info = new FileInfo(Path.Combine(Essentials.PluginPath, "Docking", string.Format("docked_{0}_{1}_{2}.sbc", ownerId, parent.EntityId, dockingEntity.EntityId))); if (!Directory.Exists(info.DirectoryName)) { Directory.CreateDirectory(info.DirectoryName); } //CubeGridEntity dockingGrid = new CubeGridEntity((MyObjectBuilder_CubeGrid)dockingEntity.GetObjectBuilder(), dockingEntity); MyObjectBuilder_CubeGrid gridBuilder = CubeGrids.SafeGetObjectBuilder(dockingEntity); if (gridBuilder == null) { Communication.SendPrivateInformation(userId, string.Format("Failed to load entity for export: {0}", dockingEntity.DisplayName)); return(true); } // Save item DockingItem dockItem = new DockingItem { DockedEntityId = dockingEntity.EntityId, TargetEntityId = parent.EntityId, PlayerId = ownerId, DockingBeaconIds = beaconList.Select(s => s.EntityId).ToArray( ), DockedName = dockingEntity.DisplayName, SavePos = savePos, SaveQuat = saveQuat }; Docking.Instance.Add(dockItem); // Serialize and save ship to file MyObjectBuilderSerializer.SerializeXML(info.FullName, false, gridBuilder); //BaseObjectManager.SaveContentFile<MyObjectBuilder_CubeGrid, MyObjectBuilder_CubeGridSerializer>(gridBuilder, info); BaseEntityNetworkManager.BroadcastRemoveEntity(dockingEntity); //dockingEntity.Close(); Communication.SendPrivateInformation(userId, string.Format("Docked ship '{0}' in docking zone '{1}'.", dockItem.DockedName, pylonName)); Log.Info("Docked ship \"{0}\" in docking zone \"{1}\". Saved to {2}", dockItem.DockedName, pylonName, info.FullName); /* * // Add a cool down * DockingCooldownItem cItem = new DockingCooldownItem(); * cItem.name = pylonName; * cItem.startTime = DateTime.Now; * PluginDocking.CooldownList.Add(cItem); */ } else { Communication.SendPrivateInformation(userId, string.Format("No ships in docking zone '{0}'.", pylonName)); } } else if (beaconList.Count > 4) { Communication.SendPrivateInformation(userId, string.Format("Too many beacons with the name or another zone with the name '{0}'. Place only 4 beacons to create a zone or try a different zone name.", pylonName)); } else { Communication.SendPrivateInformation(userId, string.Format("Can not locate docking zone '{0}'. There must be 4 beacons with the name '{0}' to create a docking zone. Beacons must be fully built!", pylonName)); } return(true); } catch (SecurityException ex) { Log.Error("Can't access docked ship file.", ex); return(false); } catch (UnauthorizedAccessException ex) { Log.Error("Can't access docked ship file.", ex); return(false); } catch (DirectoryNotFoundException ex) { Log.Error("Directory does not exist", ex); return(false); } catch (IOException ex) { return(false); } finally { m_docking = false; } }