static void OnAttachToCockpit(MySyncCharacter sync, ref AttachToCockpitMsg msg, MyNetworkClient sender)
        {
            MyCharacter character = sync.Entity;
            MyCockpit   cockpit   = MyEntities.GetEntityById(msg.CockpitEntityId) as MyCockpit;

            Debug.Assert(cockpit != null);
            if (cockpit == null)
            {
                return;
            }

            cockpit.AttachPilot(character, false);
        }
        public void SpawnAtShip(MyPlayer player, string respawnShipId)
        {
            Debug.Assert(Sync.IsServer, "Spawning can only be called on the server!");
            if (!Sync.IsServer)
            {
                return;
            }

            ResetRespawnCooldown(player.Id);
            if (Sync.MultiplayerActive)
            {
                SyncCooldownToPlayer(player.Id.SteamId);
            }

            MyCharacter       character    = null;
            MyCockpit         cockpit      = null;
            List <MyCubeGrid> respawnGrids = new List <MyCubeGrid>();

            var respawnShipDef = MyDefinitionManager.Static.GetRespawnShipDefinition(respawnShipId);


            Debug.Assert(respawnShipDef != null);
            if (respawnShipDef == null)
            {
                return;
            }

            var prefabDef = respawnShipDef.Prefab;

            Debug.Assert(prefabDef != null);
            if (prefabDef == null)
            {
                return;
            }

            if (prefabDef.CubeGrids == null)
            {
                MyDefinitionManager.Static.ReloadPrefabsFromFile(prefabDef.PrefabPath);
                prefabDef = MyDefinitionManager.Static.GetPrefabDefinition(prefabDef.Id.SubtypeName);
            }
            // Deploy ship
            Vector3 direction, position;

            GetSpawnPosition(prefabDef.BoundingSphere.Radius, out direction, out position);
            MyPrefabManager.Static.SpawnPrefab(
                respawnGrids,
                prefabDef.Id.SubtypeName,
                position,
                -direction,
                Vector3.CalculatePerpendicularVector(-direction),
                spawningOptions: Sandbox.ModAPI.SpawningOptions.RotateFirstCockpitTowardsDirection,
                updateSync: true);

            // Find cockpit
            foreach (var grid in respawnGrids)
            {
                foreach (var block in grid.GetBlocks())
                {
                    if (block.FatBlock is MyCockpit)
                    {
                        cockpit = (MyCockpit)block.FatBlock;
                        break;
                    }
                }
                if (cockpit != null)
                {
                    break;
                }
            }
            System.Diagnostics.Debug.Assert(cockpit != null, "character is spawning in ship without cockpit !");

            // Create character
            Matrix matrix = Matrix.Identity;

            if (cockpit != null)
            {
                matrix             = cockpit.WorldMatrix;
                matrix.Translation = cockpit.WorldMatrix.Translation - Vector3.Up - Vector3.Forward;
            }
            else if (respawnGrids.Count > 0)
            {
                matrix.Translation = respawnGrids[0].PositionComp.WorldAABB.Center + respawnGrids[0].PositionComp.WorldAABB.HalfExtents;
            }

            character = MyCharacter.CreateCharacter(matrix, Vector3.Zero, player.Identity.DisplayName, player.Identity.Model, null, cockpit: cockpit);

            if (cockpit != null)
            {
                cockpit.AttachPilot(character, false);
            }

            CloseRespawnShip(player);
            foreach (var respawnGrid in respawnGrids)
            {
                respawnGrid.ChangeGridOwnership(player.Identity.IdentityId, MyOwnershipShareModeEnum.None);
                player.RespawnShip.Add(respawnGrid.EntityId);
            }

            Sync.Players.SetPlayerCharacter(player, character, cockpit);
            Sync.Players.RevivePlayer(player);
        }
Esempio n. 3
0
        public void SpawnAtShip(MyPlayer player, string respawnShipId, MyBotDefinition botDefinition)
        {
            Debug.Assert(Sync.IsServer, "Spawning can only be called on the server!");
            if (!Sync.IsServer)
            {
                return;
            }

            ResetRespawnCooldown(player.Id);
            if (Sync.MultiplayerActive)
            {
                SyncCooldownToPlayer(player.Id.SteamId, player.Id.SteamId == Sync.MyId);
            }

            MyCharacter       character    = null;
            MyCockpit         cockpit      = null;
            List <MyCubeGrid> respawnGrids = new List <MyCubeGrid>();

            var respawnShipDef           = MyDefinitionManager.Static.GetRespawnShipDefinition(respawnShipId);
            MyPrefabDefinition prefabDef = respawnShipDef.Prefab;

            // Deploy ship
            Vector3D position = Vector3D.Zero;
            float    planetSpawnHeightRatio = 0.3f;
            float    spawnRangeMin          = 500f;
            float    spawnRangeMax          = 650f;

            if (prefabDef.CubeGrids != null && prefabDef.CubeGrids.Length > 0)
            {
                MyObjectBuilder_CubeGrid firstGrid = prefabDef.CubeGrids[0];
                if (firstGrid.UsePositionForSpawn)
                {
                    position = new Vector3D(
                        firstGrid.PositionAndOrientation.Value.Position.x,
                        firstGrid.PositionAndOrientation.Value.Position.y,
                        firstGrid.PositionAndOrientation.Value.Position.z);
                }

                planetSpawnHeightRatio = MyMath.Clamp(firstGrid.PlanetSpawnHeightRatio, 0.05f, 0.95f); // Clamped to prevent crazy data
                spawnRangeMin          = firstGrid.SpawnRangeMin;
                spawnRangeMax          = firstGrid.SpawnRangeMax;
            }
            Vector3D forward = Vector3.Forward;
            Vector3D up      = Vector3D.Up;

            GetSpawnPosition(prefabDef.BoundingSphere.Radius, ref position, out forward, out up, planetSpawnHeightRatio, spawnRangeMin, spawnRangeMax);

            MyPrefabManager.Static.SpawnPrefab(
                respawnGrids,
                prefabDef.Id.SubtypeName,
                position,
                forward,
                up,
                spawningOptions: VRage.Game.ModAPI.SpawningOptions.RotateFirstCockpitTowardsDirection,
                updateSync: true);

            // Find cockpits
            List <MyCockpit> shipCockpits = new List <MyCockpit>();

            foreach (MyCubeGrid grid in respawnGrids)
            {
                foreach (MyCockpit gridCockpit in grid.GetFatBlocks <MyCockpit>())
                {
                    // Ignore non-functional cockpits
                    if (!gridCockpit.IsFunctional)
                    {
                        continue;
                    }
                    shipCockpits.Add(gridCockpit);
                }
            }

            // First sort cockpits by order: Ship controlling cockpits set to main, then ship controlling cockpits not set to main, lastly whatever remains, e.g. CryoChambers and Passenger Seats
            if (shipCockpits.Count > 1)
            {
                shipCockpits.Sort(delegate(MyCockpit cockpitA, MyCockpit cockpitB)
                {
                    int controlCompare = cockpitB.EnableShipControl.CompareTo(cockpitA.EnableShipControl);
                    if (controlCompare != 0)
                    {
                        return(controlCompare);
                    }

                    int mainCompare = cockpitB.IsMainCockpit.CompareTo(cockpitA.IsMainCockpit);
                    if (mainCompare != 0)
                    {
                        return(mainCompare);
                    }

                    return(0);
                });
            }

            // Finally, select the most important cockpit
            if (shipCockpits.Count > 0)
            {
                cockpit = shipCockpits[0];
            }

            System.Diagnostics.Debug.Assert(cockpit != null, "character is spawning in ship without cockpit !");

            // Create character
            MatrixD matrix = MatrixD.Identity;

            if (cockpit != null)
            {
                matrix             = cockpit.WorldMatrix;
                matrix.Translation = cockpit.WorldMatrix.Translation - Vector3.Up - Vector3.Forward;
            }
            else if (respawnGrids.Count > 0)
            {
                matrix.Translation = respawnGrids[0].PositionComp.WorldAABB.Center + respawnGrids[0].PositionComp.WorldAABB.HalfExtents;
            }

            character = MyCharacter.CreateCharacter(matrix, Vector3.Zero, player.Identity.DisplayName, player.Identity.Model, null, botDefinition, cockpit: cockpit, playerSteamId: player.Id.SteamId);

            CloseRespawnShip(player);
            foreach (var respawnGrid in respawnGrids)
            {
                respawnGrid.ChangeGridOwnership(player.Identity.IdentityId, MyOwnershipShareModeEnum.None);
                respawnGrid.IsRespawnGrid = true;
                player.RespawnShip.Add(respawnGrid.EntityId);
            }

            if (cockpit != null)
            {
                cockpit.AttachPilot(character, false);
                MyMultiplayer.ReplicateImmediatelly(MyExternalReplicable.FindByObject(cockpit.CubeGrid), new EndpointId(player.Id.SteamId));
            }

            if (cockpit == null)
            {
                Sync.Players.SetPlayerCharacter(player, character, null);
            }
            else
            {
                character.SetPlayer(player);
                Sync.Players.SetPlayerToCockpit(player, cockpit);
            }
            Sync.Players.RevivePlayer(player);
        }
Esempio n. 4
0
        private void PutPlayerInRespawnGrid(MyPlayer player, List <MyCubeGrid> respawnGrids, MyBotDefinition botDefinition)
        {
            MyCharacter character = null;
            MyCockpit   cockpit   = null;

            // Find cockpits
            List <MyCockpit> shipCockpits = new List <MyCockpit>();

            foreach (MyCubeGrid grid in respawnGrids)
            {
                foreach (MyCockpit gridCockpit in grid.GetFatBlocks <MyCockpit>())
                {
                    // Ignore non-functional cockpits
                    if (!gridCockpit.IsFunctional)
                    {
                        continue;
                    }
                    shipCockpits.Add(gridCockpit);
                }
            }

            // First sort cockpits by order: Ship controlling cockpits set to main, then ship controlling cockpits not set to main, lastly whatever remains, e.g. CryoChambers and Passenger Seats
            if (shipCockpits.Count > 1)
            {
                shipCockpits.Sort(delegate(MyCockpit cockpitA, MyCockpit cockpitB)
                {
                    int controlCompare = cockpitB.EnableShipControl.CompareTo(cockpitA.EnableShipControl);
                    if (controlCompare != 0)
                    {
                        return(controlCompare);
                    }

                    int mainCompare = cockpitB.IsMainCockpit.CompareTo(cockpitA.IsMainCockpit);
                    if (mainCompare != 0)
                    {
                        return(mainCompare);
                    }

                    return(0);
                });
            }

            // Finally, select the most important cockpit
            if (shipCockpits.Count > 0)
            {
                cockpit = shipCockpits[0];
            }

            System.Diagnostics.Debug.Assert(cockpit != null, "character is spawning in ship without cockpit !");

            // Create character
            MatrixD matrix = MatrixD.Identity;

            if (cockpit != null)
            {
                matrix             = cockpit.WorldMatrix;
                matrix.Translation = cockpit.WorldMatrix.Translation - Vector3.Up - Vector3.Forward;
            }
            else if (respawnGrids.Count > 0)
            {
                matrix.Translation = respawnGrids[0].PositionComp.WorldAABB.Center + respawnGrids[0].PositionComp.WorldAABB.HalfExtents;
            }

            character = MyCharacter.CreateCharacter(matrix, Vector3.Zero, player.Identity.DisplayName, player.Identity.Model, null, botDefinition, cockpit: cockpit, playerSteamId: player.Id.SteamId);

            CloseRespawnShip(player);
            foreach (var respawnGrid in respawnGrids)
            {
                respawnGrid.ChangeGridOwnership(player.Identity.IdentityId, MyOwnershipShareModeEnum.None);
                respawnGrid.IsRespawnGrid = true;
                respawnGrid.m_playedTime  = 0;
                player.RespawnShip.Add(respawnGrid.EntityId);
            }
            //SaveRespawnShip(player);

            if (cockpit != null)
            {
                cockpit.AttachPilot(character, false);
                MyMultiplayer.ReplicateImmediatelly(MyExternalReplicable.FindByObject(cockpit.CubeGrid), new EndpointId(player.Id.SteamId));
            }

            if (cockpit == null)
            {
                Sync.Players.SetPlayerCharacter(player, character, null);
            }
            else
            {
                character.SetPlayer(player);
                Sync.Players.SetPlayerToCockpit(player, cockpit);
            }
            Sync.Players.RevivePlayer(player);
        }