//public MyPlayer InitNewPlayer(MyIdentity identity, PlayerId id, string playerName) //{ // 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; // return CreateNewPlayerInternal(identity, steamClient, playerName, ref id); //} public MyPlayer InitNewPlayer(MyIdentity identity, 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(identity, steamClient, playerOb.DisplayName, ref id); return playerInstance; }
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>(); foreach (var player in m_players.Values) { var id = new MyObjectBuilder_Checkpoint.PlayerId() { ClientId = player.Id.SteamId, SerialId = player.Id.SerialId }; MyObjectBuilder_Player playerOb = new MyObjectBuilder_Player(); playerOb.DisplayName = player.DisplayName; playerOb.IdentityId = player.Identity.IdentityId; playerOb.Connected = true; checkpoint.AllPlayersData.Dictionary.Add(id, playerOb); } foreach (var entry in m_playerIdentityIds) { if (m_players.ContainsKey(entry.Key) || entry.Key.SerialId != 0) continue; var id = new MyObjectBuilder_Checkpoint.PlayerId() { ClientId = entry.Key.SteamId, SerialId = entry.Key.SerialId }; MyObjectBuilder_Player playerOb = new MyObjectBuilder_Player(); playerOb.Connected = false; playerOb.IdentityId = entry.Value; var identity = TryGetIdentity(entry.Value); playerOb.DisplayName = identity != null ? identity.DisplayName : null; checkpoint.AllPlayersData.Dictionary.Add(id, playerOb); } //foreach (var entry in m_playerIdentityIds) //{ // // Skip connected players // if (m_players.ContainsKey(entry.Key)) continue; // var id = new MyObjectBuilder_Checkpoint.PlayerId() { ClientId = entry.Key.SteamId, SerialId = entry.Key.SerialId }; // checkpoint.DisconnectedPlayers.Dictionary.Add(id, entry.Value); //} }
private List<AllPlayerData> SavePlayers() { var allPlayersData = new List<AllPlayerData>(); foreach (var player in m_players.Values) { AllPlayerData data = new AllPlayerData(); data.SteamId = player.Id.SteamId; data.SerialId = player.Id.SerialId; MyObjectBuilder_Player playerOb = new MyObjectBuilder_Player(); playerOb.DisplayName = player.DisplayName; playerOb.IdentityId = player.Identity.IdentityId; playerOb.Connected = true; data.Player = playerOb; allPlayersData.Add(data); } return allPlayersData; }
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); } }
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(MySteam.UserId); 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(MySteam.UserId); 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(MySteam.UserId); 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 MyPlayer.PlayerId(playerItem.Key.ClientId, playerItem.Key.SerialId); if (savingPlayerId != null && playerId == savingPlayerId) { playerId = new PlayerId(MySteam.UserId); ChangeDisplayNameOfPlayerAndIdentity(playerItem.Value, MySteam.UserName); } LoadPlayerInternal(ref playerId, playerItem.Value, obsolete: false); } } /*long identityId = FindLocalIdentityId(checkpoint); //Player was saved in death state or there is no player if (identityId == 0) { checkpoint.ControlledObject = 0; //This will lead to RequestRespawn checkpoint.CameraController = MyCameraControllerEnum.Entity; IsCameraAwaitingEntity = true; } else { // TODO: Refactor this later MyEntity controlledObject = null; if (checkpoint.ControlledObject != -1) { MyEntities.TryGetEntityById(checkpoint.ControlledObject, out controlledObject); System.Diagnostics.Debug.Assert(controlledObject != null); if (controlledObject is IMyControllableEntity) { var cockpit = controlledObject as MyCockpit; if (cockpit != null) { var pilot = cockpit.Pilot; if (pilot == null) { Debug.Fail("Creating new pilot for cockpit, because saved cockpit was controlled and had no pilot sitting in it."); MySandboxGame.Log.WriteLine("Creating new pilot for cockpit, because saved cockpit was controlled and had no pilot sitting in it."); var characterOb = MyCharacter.Random(); characterOb.Battery = new MyObjectBuilder_Battery() { CurrentCapacity = MyEnergyConstants.BATTERY_MAX_CAPACITY }; pilot = (MyCharacter)MyEntityFactory.CreateEntity(characterOb); pilot.Init(characterOb); MyWorldGenerator.InitInventoryWithDefaults(pilot.GetInventory()); cockpit.RequestUse(UseActionEnum.Manipulate, pilot); } MySession.Player.Init(pilot, null, identityId); } else if (controlledObject.Parent is MyCockpit) { MySession.Player.Init((MyCharacter)controlledObject, null, identityId); controlledObject = controlledObject.Parent; } else { if (!MySandboxGame.IsDedicated) { if (controlledObject is MyCharacter) { MySession.Player.Init((MyCharacter)controlledObject, null, identityId); } } } if (!MySandboxGame.IsDedicated) MySession.Player.Controller.TakeControl((IMyControllableEntity)controlledObject); } } if (checkpoint.Players != null) { foreach (var playerIt in checkpoint.Players.Dictionary) { if (playerIt.Key == Player.SteamUserId) { Player.PlayerId = identityId; if (string.IsNullOrEmpty(Player.Model)) { if (!string.IsNullOrEmpty(playerIt.Value.PlayerModel)) Player.Model = playerIt.Value.PlayerModel; else Player.Model = MyCharacter.DefaultModel; } } MyPlayer player; if (Sync.Controllers.TryGetPlayer(playerIt.Key, out player)) { MyCharacter playerEntity; if (MyEntities.TryGetEntityById<MyCharacter>(playerIt.Value.PlayerEntity, out playerEntity)) { player.Init(playerEntity, playerIt.Value.PlayerModel, playerIt.Key == Player.SteamUserId ? identityId : playerIt.Value.PlayerId); } } } } }*/ }
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; }
//private void LoadPlayerInternal(long identityId, ref PlayerId playerId, string playerName, bool obsolete = false) //{ // var identity = TryGetIdentity(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.Clients.HasClient(playerId.SteamId)) // { // if (!m_playerIdentityIds.ContainsKey(playerId)) // m_playerIdentityIds.Add(playerId, identityId); // identity.SetDead(true); // return; // } // InitNewPlayer(identity, playerId, playerName); // return; //} 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 && MySteam.UserId != 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(identity, playerId, playerOb); if (player.IsLocalPlayer()) { var handler = Sync.Players.LocalPlayerLoaded; if (handler != null) handler(playerId.SerialId); } }
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); }
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); }
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); } } }
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(MySteam.UserId); 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(MySteam.UserId); 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(MySteam.UserId); 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 == savingPlayerId) { playerId = new PlayerId(MySteam.UserId); 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); } } }
private MyPlayer CreateNewPlayerInternal(MyNetworkClient steamClient, PlayerId playerId, MyObjectBuilder_Player playerBuilder) { if (!m_playerIdentityIds.ContainsKey(playerId)) { m_playerIdentityIds.Add(playerId, playerBuilder.IdentityId); } MyPlayer 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; }