Exemplo n.º 1
0
        public MyObjectBuilder_Player GetObjectBuilder()
        {
            MyObjectBuilder_Player objectBuilder = new MyObjectBuilder_Player();

            objectBuilder.DisplayName = DisplayName;
            objectBuilder.IdentityId  = Identity.IdentityId;
            objectBuilder.Connected   = true;

            if (!IsColorsSetToDefaults(m_buildColorHSVSlots))
            {
                objectBuilder.BuildColorSlots = new List <Vector3>();

                foreach (var color in m_buildColorHSVSlots)
                {
                    objectBuilder.BuildColorSlots.Add(color);
                }
            }

            return(objectBuilder);
        }
Exemplo n.º 2
0
        public void Init(MyObjectBuilder_Player objectBuilder)
        {
            DisplayName = objectBuilder.DisplayName;
            Identity    = Sync.Players.TryGetIdentity(objectBuilder.IdentityId);

            if (m_buildColorHSVSlots.Count < m_buildColorSlotCount)
            {
                var defaultCount = m_buildColorHSVSlots.Count;
                for (int index = 0; index < m_buildColorSlotCount - defaultCount; ++index)
                {
                    m_buildColorHSVSlots.Add(MyRenderComponentBase.OldBlackToHSV);
                }
            }

            if ((objectBuilder.BuildColorSlots == null) || (objectBuilder.BuildColorSlots.Count == 0))
            {
                SetDefaultColors();
            }
            else if (objectBuilder.BuildColorSlots.Count == m_buildColorSlotCount)
            {
                m_buildColorHSVSlots = objectBuilder.BuildColorSlots;
            }
            else if (objectBuilder.BuildColorSlots.Count > m_buildColorSlotCount)
            {
                m_buildColorHSVSlots = new List <Vector3>(m_buildColorSlotCount);
                for (int i = 0; i < m_buildColorSlotCount; i++)
                {
                    m_buildColorHSVSlots.Add(objectBuilder.BuildColorSlots[i]);
                }
            }
            else
            {
                m_buildColorHSVSlots = objectBuilder.BuildColorSlots;
                for (int i = m_buildColorHSVSlots.Count - 1; i < m_buildColorSlotCount; i++)
                {
                    m_buildColorHSVSlots.Add(MyRenderComponentBase.OldBlackToHSV);
                }
            }

            if (!Sync.IsServer)
            {
                return;
            }

            // Don't care about bot build colours for now
            if (Id.SerialId != 0)
            {
                return;
            }

            if (MyCubeBuilder.AllPlayersColors == null)
            {
                MyCubeBuilder.AllPlayersColors = new Dictionary <PlayerId, List <Vector3> >();
            }

            if (!MyCubeBuilder.AllPlayersColors.ContainsKey(Id))
            {
                MyCubeBuilder.AllPlayersColors.Add(Id, m_buildColorHSVSlots);
            }
            else
            {
                MyCubeBuilder.AllPlayersColors.TryGetValue(Id, out m_buildColorHSVSlots);
            }
        }
        public MyPlayer CreateNewPlayer(MyIdentity identity, PlayerId id, string playerName)
        {
            Debug.Assert(Sync.IsServer);

            MyNetworkClient steamClient;
            Sync.Clients.TryGetClient(id.SteamId, out steamClient);
            Debug.Assert(steamClient != null, "Could not find a client for the new player!");
            if (steamClient == null) return null;

            var playerBuilder = new MyObjectBuilder_Player();
            playerBuilder.DisplayName = playerName;
            playerBuilder.IdentityId = identity.IdentityId;

            var player = CreateNewPlayerInternal(steamClient, id, playerBuilder);
            if (player != null)
            {
                List<Vector3> buildColors = null;
                if (!MyPlayer.IsColorsSetToDefaults(player.BuildColorSlots))
                    buildColors = player.BuildColorSlots;

                MyMultiplayer.RaiseStaticEvent(s => MyPlayerCollection.OnPlayerCreated, id.SteamId, id.SerialId, identity.IdentityId, playerName, buildColors);
            }
            return player;
        }
        public MyPlayer InitNewPlayer(PlayerId id, MyObjectBuilder_Player playerOb)
        {
            MyNetworkClient steamClient;
            Sync.Clients.TryGetClient(id.SteamId, out steamClient);
            Debug.Assert(steamClient != null, "Could not find a client for the new player!");
            if (steamClient == null) return null;

            MyPlayer playerInstance = CreateNewPlayerInternal(steamClient, id, playerOb);
            return playerInstance;
        }
        static void OnPlayerCreated(ulong clientSteamId, int playerSerialId, long identityId, string displayName,
            [Serialize(MyObjectFlags.Nullable)] List<Vector3> buildColors)
        {
            var identity = Sync.Players.TryGetIdentity(identityId);
            Debug.Assert(identity != null, "Identity for the new player not found!");
            if (identity == null) return;

            MyNetworkClient client = null;
            Sync.Clients.TryGetClient(clientSteamId, out client);
            Debug.Assert(client != null, "Could not find client of the new player!");
            if (client == null) return;

            var playerId = new MyPlayer.PlayerId(clientSteamId, playerSerialId);
            var playerBuilder = new MyObjectBuilder_Player();
            playerBuilder.DisplayName = displayName;
            playerBuilder.IdentityId = identityId;
            playerBuilder.BuildColorSlots = buildColors;

            Sync.Players.CreateNewPlayerInternal(client, playerId, playerBuilder);
        }
        public MyPlayer CreateNewPlayer(MyIdentity identity, MyNetworkClient steamClient, string playerName)
        {
            Debug.Assert(Sync.IsServer);

            // TODO: Limit number of players per client
            var playerId = FindFreePlayerId(steamClient.SteamUserId);

            var playerBuilder = new MyObjectBuilder_Player();
            playerBuilder.DisplayName = playerName;
            playerBuilder.IdentityId = identity.IdentityId;

            return CreateNewPlayerInternal(steamClient, playerId, playerBuilder);
        }
        public void LoadConnectedPlayers(MyObjectBuilder_Checkpoint checkpoint, MyPlayer.PlayerId? savingPlayerId = null)
        {
#warning TODO: Probably not needed? If not, remove the method
            //long identityId = FindLocalIdentityId(checkpoint);

            // Backward compatibility
            if (checkpoint.AllPlayers != null && checkpoint.AllPlayers.Count != 0)
            {
                foreach (var playerItem in checkpoint.AllPlayers)
                {
                    long identityId = playerItem.PlayerId;

                    var playerOb = new MyObjectBuilder_Player();
                    playerOb.Connected = true;
                    playerOb.DisplayName = playerItem.Name;
                    playerOb.IdentityId = identityId;

                    var playerId = new PlayerId(playerItem.SteamId, 0);
                    if (savingPlayerId != null && playerId == savingPlayerId)
                    {
                        playerId = new PlayerId(Sync.MyId);
                        ChangeDisplayNameOfPlayerAndIdentity(playerOb, MySteam.UserName);
                    }

                    LoadPlayerInternal(ref playerId, playerOb, obsolete: true);
                }
            }
            // Backward compatibility
            else if (checkpoint.ConnectedPlayers != null && checkpoint.ConnectedPlayers.Dictionary.Count != 0)
            {
                Debug.Assert(checkpoint.DisconnectedPlayers != null, "Inconsistency in save! ConnectedPlayers were present, but DisconnectedPlayers not!");
                foreach (var playerItem in checkpoint.ConnectedPlayers.Dictionary)
                {
                    var playerId = new PlayerId(playerItem.Key.ClientId, playerItem.Key.SerialId);
                    if (savingPlayerId != null && playerId == savingPlayerId)
                    {
                        playerId = new PlayerId(Sync.MyId);
                        ChangeDisplayNameOfPlayerAndIdentity(playerItem.Value, MySteam.UserName);
                    }

                    playerItem.Value.Connected = true;
                    LoadPlayerInternal(ref playerId, playerItem.Value, obsolete: false);
                }

                foreach (var playerItem in checkpoint.DisconnectedPlayers.Dictionary)
                {
                    var playerId = new PlayerId(playerItem.Key.ClientId, playerItem.Key.SerialId);

                    var playerOb = new MyObjectBuilder_Player();
                    playerOb.Connected = false;
                    playerOb.IdentityId = playerItem.Value;
                    playerOb.DisplayName = null;

                    if (savingPlayerId != null && playerId == savingPlayerId)
                    {
                        playerId = new PlayerId(Sync.MyId);
                        ChangeDisplayNameOfPlayerAndIdentity(playerOb, MySteam.UserName);
                    }

                    LoadPlayerInternal(ref playerId, playerOb, obsolete: false);
                }

                //LoadDisconnectedPlayers(checkpoint.DisconnectedPlayers.Dictionary);
            }
            else if (checkpoint.AllPlayersData != null)
            {
                foreach (var playerItem in checkpoint.AllPlayersData.Dictionary)
                {
                    var playerId = new PlayerId(playerItem.Key.ClientId, playerItem.Key.SerialId);
                    if (savingPlayerId != null && playerId.SteamId == savingPlayerId.Value.SteamId)
                    {
                        playerId = new PlayerId(Sync.MyId, playerId.SerialId);
                        if (playerId.SerialId == 0)
                            ChangeDisplayNameOfPlayerAndIdentity(playerItem.Value, MySteam.UserName);
                    }

                    LoadPlayerInternal(ref playerId, playerItem.Value, obsolete: false);

                    MyPlayer player = null;
                    if (m_players.TryGetValue(playerId, out player))
                    {
                        List<Vector3> buildColors = null;
                        if (checkpoint.AllPlayersColors != null && checkpoint.AllPlayersColors.Dictionary.TryGetValue(playerItem.Key, out buildColors))
                        {
                            player.SetBuildColorSlots(buildColors);
                        }
                        else if (checkpoint.CharacterToolbar != null && checkpoint.CharacterToolbar.ColorMaskHSVList != null && checkpoint.CharacterToolbar.ColorMaskHSVList.Count > 0) // Backwards compatibility
                        {
                            player.SetBuildColorSlots(checkpoint.CharacterToolbar.ColorMaskHSVList);
                        }
                    }
                }
            }

            if (MyCubeBuilder.AllPlayersColors != null && checkpoint.AllPlayersColors != null)
            {
                foreach (var colorPair in checkpoint.AllPlayersColors.Dictionary)
                {
                    var playerId = new PlayerId(colorPair.Key.ClientId, colorPair.Key.SerialId);
                    if (!MyCubeBuilder.AllPlayersColors.ContainsKey(playerId))
                        MyCubeBuilder.AllPlayersColors.Add(playerId, colorPair.Value);
                }
            }
        }
        public void SavePlayers(MyObjectBuilder_Checkpoint checkpoint)
        {
            checkpoint.ConnectedPlayers = new SerializableDictionary<MyObjectBuilder_Checkpoint.PlayerId, MyObjectBuilder_Player>();
            checkpoint.DisconnectedPlayers = new SerializableDictionary<MyObjectBuilder_Checkpoint.PlayerId, long>();
            checkpoint.AllPlayersData = new SerializableDictionary<MyObjectBuilder_Checkpoint.PlayerId, MyObjectBuilder_Player>();
            checkpoint.AllPlayersColors = new SerializableDictionary<MyObjectBuilder_Checkpoint.PlayerId, List<Vector3>>();

            foreach (var player in m_players.Values)
            {
                var id = new MyObjectBuilder_Checkpoint.PlayerId() { ClientId = player.Id.SteamId, SerialId = player.Id.SerialId };
                MyObjectBuilder_Player playerOb = player.GetObjectBuilder();

                checkpoint.AllPlayersData.Dictionary.Add(id, playerOb);
            }

            foreach (var identityPair in m_playerIdentityIds)
            {
                if (m_players.ContainsKey(identityPair.Key)) continue;

                var id = new MyObjectBuilder_Checkpoint.PlayerId() { ClientId = identityPair.Key.SteamId, SerialId = identityPair.Key.SerialId };
                var identity = TryGetIdentity(identityPair.Value);
                MyObjectBuilder_Player playerOb = new MyObjectBuilder_Player();
                playerOb.DisplayName = identity != null ? identity.DisplayName : null;
                playerOb.IdentityId = identityPair.Value;
                playerOb.Connected = false;
                if (MyCubeBuilder.AllPlayersColors != null)
                    MyCubeBuilder.AllPlayersColors.TryGetValue(identityPair.Key, out playerOb.BuildColorSlots);

                checkpoint.AllPlayersData.Dictionary.Add(id, playerOb);
            }

            if (MyCubeBuilder.AllPlayersColors != null)
            {
                foreach (var colorPair in MyCubeBuilder.AllPlayersColors)
                {
                    if (m_players.ContainsKey(colorPair.Key) || m_playerIdentityIds.ContainsKey(colorPair.Key)) continue;	// avoid data duplication in saves

                    var id = new MyObjectBuilder_Checkpoint.PlayerId() { ClientId = colorPair.Key.SteamId, SerialId = colorPair.Key.SerialId };
                    checkpoint.AllPlayersColors.Dictionary.Add(id, colorPair.Value);
                }
            }
        }
        private void ChangeDisplayNameOfPlayerAndIdentity(MyObjectBuilder_Player playerOb, string name)
        {
            playerOb.DisplayName = MySteam.UserName;

            var identity = TryGetIdentity(playerOb.IdentityId);
            Debug.Assert(identity != null, "Identity of a player was null when loading! Inconsistency!");
            if (identity != null)
            {
                identity.SetDisplayName(MySteam.UserName);
            }
        }
        private void LoadPlayerInternal(ref PlayerId playerId, MyObjectBuilder_Player playerOb, bool obsolete = false)
        {
            var identity = TryGetIdentity(playerOb.IdentityId);
            Debug.Assert(identity != null, "Identity of a player was null when loading! Inconsistency!");
            if (identity == null) return;
            if (obsolete && identity.IsDead) return;

            // This happens when you load an existing game - only the local player will be in Sync.Clients, but there were
            // more connected players at the time of the save. In this case, we have to consider them as disconnected players
            if (Sync.IsServer && Sync.MyId != playerId.SteamId)
                playerOb.Connected = Sync.Clients.HasClient(playerId.SteamId);
            if (!playerOb.Connected)
            {
                if (!m_playerIdentityIds.ContainsKey(playerId))
                    m_playerIdentityIds.Add(playerId, playerOb.IdentityId);
                identity.SetDead(true);
                return;
            }

            var player = InitNewPlayer(playerId, playerOb);

            if (player.IsLocalPlayer)
            {
                var handler = Sync.Players.LocalPlayerLoaded;
                if (handler != null)
                    handler(playerId.SerialId);
            }
        }
        private MyPlayer CreateNewPlayerInternal(MyNetworkClient steamClient, PlayerId playerId, MyObjectBuilder_Player playerBuilder)
        {
            if (!m_playerIdentityIds.ContainsKey(playerId))
            {
                m_playerIdentityIds.Add(playerId, playerBuilder.IdentityId);
            }

            MyPlayer newPlayer = GetPlayerById(playerId);
            if (newPlayer == null)
            {
                newPlayer = new MyPlayer(steamClient, playerId);
                newPlayer.Init(playerBuilder);

                newPlayer.IdentityChanged += player_IdentityChanged;
                newPlayer.Controller.ControlledEntityChanged += controller_ControlledEntityChanged;

                AddPlayer(playerId, newPlayer);

                if (MyFakes.ENABLE_MISSION_TRIGGERS && MySessionComponentMissionTriggers.Static != null)
                    MySessionComponentMissionTriggers.Static.TryCreateFromDefault(playerId);

            }
            return newPlayer;
        }
Exemplo n.º 12
0
        static void OnPlayerCreated(ref PlayerCreatedMsg msg, MyNetworkClient sender)
        {
            var identity = Sync.Players.TryGetIdentity(msg.IdentityId);
            Debug.Assert(identity != null, "Identity for the new player not found!");
            if (identity == null) return;

            MyNetworkClient client = null;
            Sync.Clients.TryGetClient(msg.ClientSteamId, out client);
            Debug.Assert(client != null, "Could not find client of the new player!");
            if (client == null) return;

            var playerId = new MyPlayer.PlayerId(msg.ClientSteamId, msg.PlayerSerialId);
            var playerBuilder = new MyObjectBuilder_Player();
            playerBuilder.DisplayName = msg.DisplayName;
            playerBuilder.IdentityId = msg.IdentityId;
            playerBuilder.BuildColorSlots = msg.BuildColors;

            Sync.Players.CreateNewPlayerInternal(client, playerId, playerBuilder);
        }
Exemplo n.º 13
0
        public MyPlayer CreateNewPlayer(MyIdentity identity, PlayerId id, string playerName)
        {
            Debug.Assert(Sync.IsServer);

            MyNetworkClient steamClient;
            Sync.Clients.TryGetClient(id.SteamId, out steamClient);
            Debug.Assert(steamClient != null, "Could not find a client for the new player!");
            if (steamClient == null) return null;

            var playerBuilder = new MyObjectBuilder_Player();
            playerBuilder.DisplayName = playerName;
            playerBuilder.IdentityId = identity.IdentityId;

            var player = CreateNewPlayerInternal(steamClient, id, playerBuilder);
            if (player != null)
            {
                var msg = new PlayerCreatedMsg();
                msg.ClientSteamId = id.SteamId;
                msg.PlayerSerialId = id.SerialId;
                msg.IdentityId = identity.IdentityId;
                msg.DisplayName = playerName;
                msg.BuildColors = null;
                if (!MyPlayer.IsColorsSetToDefaults(player.BuildColorSlots))
                    msg.BuildColors = player.BuildColorSlots;

                Sync.Layer.SendMessageToAll(ref msg, MyTransportMessageEnum.Success);
            }
            return player;
        }