From aca85c27089c92a20167d5f5e1cae9e522deaad0 Mon Sep 17 00:00:00 2001 From: Regalis Date: Sat, 25 Mar 2017 18:24:09 +0200 Subject: [PATCH] All characters start disabled in the multiplayer mode, and are enabled when the client starts receiving position updates for the character, OR when the server starts receiving inputs from the client controlling the character, OR if the character is controlled locally. (-> no more floating, frozen characters at the start of a round) + the server disables AICharacters that are far away from all players --- .../Source/Characters/AI/EnemyAIController.cs | 2 - Subsurface/Source/Characters/Character.cs | 38 +++++++++++++++---- .../Source/Characters/CharacterNetworking.cs | 8 +++- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/Subsurface/Source/Characters/AI/EnemyAIController.cs b/Subsurface/Source/Characters/AI/EnemyAIController.cs index 2a98249ce..d38ed38ca 100644 --- a/Subsurface/Source/Characters/AI/EnemyAIController.cs +++ b/Subsurface/Source/Characters/AI/EnemyAIController.cs @@ -68,8 +68,6 @@ namespace Barotrauma public EnemyAIController(Character c, string file) : base(c) { - if (GameMain.Client != null && GameMain.Server == null) c.Enabled = false; - targetMemories = new Dictionary(); XDocument doc = ToolBox.TryLoadXml(file); diff --git a/Subsurface/Source/Characters/Character.cs b/Subsurface/Source/Characters/Character.cs index 0aa8aa28c..c08a3b995 100644 --- a/Subsurface/Source/Characters/Character.cs +++ b/Subsurface/Source/Characters/Character.cs @@ -618,7 +618,12 @@ namespace Barotrauma CharacterList.Add(this); - Enabled = true; + //characters start disabled in the multiplayer mode, and are enabled if/when + // - controlled by the player + // - client receives a position update from the server + // - server receives an input message from the client controlling the character + // - if an AICharacter, the server enables it when close enough to any of the players + Enabled = GameMain.NetworkMember == null; } private static string humanConfigFile; @@ -1247,7 +1252,7 @@ namespace Barotrauma c.AnimController.UpdateAnim(deltaTime); } } - + public static void AddAllToGUIUpdateList() { for (int i = 0; i < CharacterList.Count; i++) @@ -1258,12 +1263,31 @@ namespace Barotrauma public static void UpdateAll(Camera cam, float deltaTime) { - //if (NewCharacterQueue.Count>0) - //{ - // new Character(NewCharacterQueue.Dequeue(), Vector2.Zero); - //} + if (GameMain.Client == null) + { + foreach (Character c in CharacterList) + { + if (!(c is AICharacter)) continue; + + if (GameMain.Server != null) + { + //disable AI characters that are far away from all clients and the host's character + c.Enabled = + CharacterList.Any(c2 => + (c2.IsRemotePlayer || c2 == GameMain.Server.Character) && + Vector2.Distance(c2.WorldPosition, c.WorldPosition) < NetConfig.CharacterIgnoreDistance); + } + else + { + //disable AI characters that are far away from the sub and the controlled character + c.Enabled = Vector2.Distance(Submarine.MainSub.WorldPosition, c.WorldPosition) < NetConfig.CharacterIgnoreDistance || + (controlled != null && Vector2.Distance(controlled.WorldPosition, c.WorldPosition) < NetConfig.CharacterIgnoreDistance); + } + } + } - for (int i = 0; i