diff --git a/Assets/GWConquest/Scripts/Battle.cs b/Assets/GWConquest/Scripts/Battle.cs index 6f430f1..b479949 100644 --- a/Assets/GWConquest/Scripts/Battle.cs +++ b/Assets/GWConquest/Scripts/Battle.cs @@ -231,7 +231,7 @@ namespace GWConquest public void FixedUpdate() { - if(IsServer) + if(IsSpawned && IsServer) { if(IsInPreparing) { @@ -674,7 +674,7 @@ namespace GWConquest } } - [ServerRpc] + [ServerRpc(RequireOwnership = false)] public void MoveUnitToFlankServerRpc(NetworkBehaviourReference unit, NetworkBehaviourReference flank, int flankIndex) { MoveUnitToFlank(unit.GetBehaviour(), flank.GetBehaviour(), flankIndex); @@ -687,7 +687,7 @@ namespace GWConquest unit.SetActionCooldown(GameManager.Instance.MoveToReserveCooldown / movement); } - [ServerRpc] + [ServerRpc(RequireOwnership = false)] public void MoveUnitToReserveServerRpc(NetworkBehaviourReference unit) { MoveUnitToReserve(unit.GetBehaviour()); diff --git a/Assets/GWConquest/Scripts/BattleFlank.cs b/Assets/GWConquest/Scripts/BattleFlank.cs index 86c5d59..71ea01c 100644 --- a/Assets/GWConquest/Scripts/BattleFlank.cs +++ b/Assets/GWConquest/Scripts/BattleFlank.cs @@ -124,7 +124,7 @@ namespace GWConquest { public void FixedUpdate() { - if(IsServer) + if(IsSpawned && IsServer) { for(int i = 0; i < MaxUnitCount; i++) { diff --git a/Assets/GWConquest/Scripts/District.cs b/Assets/GWConquest/Scripts/District.cs index 957ad1f..e2d8c04 100644 --- a/Assets/GWConquest/Scripts/District.cs +++ b/Assets/GWConquest/Scripts/District.cs @@ -298,7 +298,7 @@ namespace GWConquest public void FixedUpdate() { - if(IsServer) + if(IsSpawned && IsServer) { CheckControllingChange(); diff --git a/Assets/GWConquest/Scripts/DistrictFactory.cs b/Assets/GWConquest/Scripts/DistrictFactory.cs index b5bdec2..f52e9ea 100644 --- a/Assets/GWConquest/Scripts/DistrictFactory.cs +++ b/Assets/GWConquest/Scripts/DistrictFactory.cs @@ -167,7 +167,7 @@ namespace GWConquest } - [ServerRpc] + [ServerRpc(RequireOwnership = false)] public void AddProductionQueueEntryServerRpc(ushort buildableID, bool isUpgrade, NetworkBehaviourReference player) { IBuildable buildable = isUpgrade ? DistrictUpgrade.FromID(buildableID) : UnitClass.FromID(buildableID); @@ -181,7 +181,7 @@ namespace GWConquest public void FixedUpdate() { - if(IsProducing) + if(IsServer && IsSpawned && IsProducing) { productionQueue[0] = productionQueue[0].IncreaseTime(Time.fixedDeltaTime); IBuildable buildable = GetProductionQueueEntry(0); diff --git a/Assets/GWConquest/Scripts/Formation.cs b/Assets/GWConquest/Scripts/Formation.cs index 4ed4457..51ac8ae 100644 --- a/Assets/GWConquest/Scripts/Formation.cs +++ b/Assets/GWConquest/Scripts/Formation.cs @@ -300,7 +300,7 @@ namespace GWConquest public void StartMovingOnPath(List path, Formation targetFormation = null) { - if (path.Count > 1 && IsOwner && CanMove) + if (path.Count > 1 && Player == Player.CurrentPlayer && CanMove) { if (!CanMoveTo(path[0], path[1])) { @@ -309,12 +309,13 @@ namespace GWConquest } int[] zoneIDs = path.GetRange(1, path.Count - 1).Select(z => Zone.GetZoneId(z)).ToArray(); + Debug.Log($"[RPC] Sending RPC: Begin Movement Server with zone path {zoneIDs}, target {targetFormation}"); BeginMovementServerRpc(zoneIDs, targetFormation); } else { - Debug.LogFormat("Could not move formation {0} (controlled: {1}, movement state: {2})", - this, IsOwner, MovementState); + Debug.LogFormat("Could not move formation {0} (player: {1}, movement state: {2})", + this, Player, MovementState); } } @@ -344,6 +345,8 @@ namespace GWConquest pathQueue.SetEntries(zoneIDs); + Debug.Log($"First in path queue is now zone {PathQueue.First()}"); + if (targetFormation != null) { MovementTargetFormation = targetFormation; @@ -358,15 +361,16 @@ namespace GWConquest BeginMovementServer(new int[] { Zone.GetZoneId(targetZone) }, targetFormation: targetFormation); } - [ServerRpc] + [ServerRpc(RequireOwnership = false)] public void BeginMovementServerRpc(int[] zoneIDs, NullableNetworkBehaviourReference targetFormation) { + Debug.Log($"[RPC] Recieved RPC: Begin Movement Server with zone path {String.Join(',', zoneIDs)}, target {targetFormation}"); BeginMovementServer(zoneIDs, targetFormation: targetFormation.GetBehaviour()); } public void MoveToZone(Zone target) { - if (IsOwner && MovementState == FormationMovementState.PreparingMovement) + if (MovementState == FormationMovementState.PreparingMovement) { if (!CanMoveTo(currentZone, target)) { @@ -487,7 +491,7 @@ namespace GWConquest public void FixedUpdate() { - if (IsServer) + if (IsSpawned && IsServer) { if (IsEmbarked) { @@ -563,7 +567,7 @@ namespace GWConquest { if (MovementTargetFormation != null) { - if (MovementTargetFormation.HasNetworkObject && MovementTargetFormation.currentZone == currentZone) + if (MovementTargetFormation.IsSpawned && MovementTargetFormation.currentZone == currentZone) { MovementTargetFormation.AddSubFormation(this); } @@ -606,7 +610,7 @@ namespace GWConquest private void Update() { - if (HasNetworkObject) + if (IsSpawned) { if (GameManager.EntitiesLoaded) { @@ -785,11 +789,11 @@ namespace GWConquest public float GetFormationStrength() { - if (HasNetworkObject) + if (IsSpawned) { return Units.Sum(unit => { - if (unit != null && unit.HasNetworkObject) + if (unit != null && unit.IsSpawned) { return unit.Class.UnitStrength; } @@ -1057,7 +1061,7 @@ namespace GWConquest } } - [ClientRpc] + [ClientRpc(Delivery = RpcDelivery.Unreliable)] public void PlayFormationDemoralizedAnimationClientRpc() { if (HeroUnit != null && HeroUnit.CurrentIcon != null) @@ -1102,7 +1106,7 @@ namespace GWConquest } } - [ServerRpc] + [ServerRpc(RequireOwnership = false)] public void AssignLeaderServerRpc(NullableNetworkBehaviourReference unit) { heroUnit.Value = unit; diff --git a/Assets/GWConquest/Scripts/GWNetworkManager.cs b/Assets/GWConquest/Scripts/GWNetworkManager.cs index b50b031..ce2a2c3 100644 --- a/Assets/GWConquest/Scripts/GWNetworkManager.cs +++ b/Assets/GWConquest/Scripts/GWNetworkManager.cs @@ -5,6 +5,7 @@ using System.Linq; namespace GWConquest { public class GWNetworkManager : NetworkBehaviour { + public static bool SceneSynchronized = false; private static GWNetworkManager _instance; public static GWNetworkManager Instance { @@ -30,7 +31,7 @@ namespace GWConquest { private void OnSceneEvent(SceneEvent sceneEvent) { - Debug.LogFormat("GWNetworkManager: On Scene Event", sceneEvent); + Debug.LogFormat("GWNetworkManager: On Scene Event with type {0} and client id {1}", sceneEvent.SceneEventType, sceneEvent.ClientId); if(IsServer) { if(sceneEvent.SceneEventType == SceneEventType.LoadComplete) @@ -48,10 +49,14 @@ namespace GWConquest { } else { - if(sceneEvent.SceneEventType == SceneEventType.LoadComplete) + if(sceneEvent.SceneEventType == SceneEventType.SynchronizeComplete) { - ClientSceneLoadComplete(); - GameManager.Instance.SceneLoadLocalDone(true); + SceneSynchronized = true; + if(Player.CurrentPlayer != null) + { + Debug.Log("Player is already synchronized, completing scene load now"); + ClientSceneLoadComplete(); + } } } } @@ -100,16 +105,16 @@ namespace GWConquest { public void ServerSceneLoadCompleteRemote(ulong clientID) { - Player clientPlayer = GameManager.Instance.SpawnPlayer((ushort) ((GameManager.HostFactionIndex + 1) % 2)); - clientPlayer.NetworkObject.ChangeOwnership(clientID); + Player clientPlayer = GameManager.Instance.SpawnPlayer((ushort) ((GameManager.HostFactionIndex + 1) % 2), clientID); } public void ClientSceneLoadComplete() { GameOptions.ApplyOptions(); + GameManager.Instance.SceneLoadLocalDone(true); } - [ServerRpc] + [ServerRpc(RequireOwnership = false)] public void MoveItemServerRpc(NetworkBehaviourReference origin, NetworkBehaviourReference target, ushort itemID, int itemAmount) { NetworkBehaviour originBeh = origin.GetBehaviour(); @@ -148,7 +153,7 @@ namespace GWConquest { return unit; } - [ServerRpc] + [ServerRpc(RequireOwnership = false)] public void SpawnUnitServerRpc(int zoneID, ushort unitClassID, int playerID) { SpawnUnit(Zone.GetFromId(zoneID), UnitClass.FromID(unitClassID), Player.GetPlayerById(playerID)); diff --git a/Assets/GWConquest/Scripts/GameManager.cs b/Assets/GWConquest/Scripts/GameManager.cs index 7e4fef4..a29013d 100644 --- a/Assets/GWConquest/Scripts/GameManager.cs +++ b/Assets/GWConquest/Scripts/GameManager.cs @@ -139,10 +139,10 @@ namespace GWConquest IsLoaded = true; } - public Player SpawnPlayer(ushort faction) + public Player SpawnPlayer(ushort faction, ulong clientID=0) { GameObject go = Instantiate(PlayerPrefab); - go.GetComponent().Spawn(); + go.GetComponent().SpawnWithOwnership(clientID); Player player = go.GetComponent(); player.SetFaction(faction); player.AssignStartingPlanets(); @@ -151,6 +151,7 @@ namespace GWConquest public void SceneLoadLocalDone(bool isClient) { + Debug.LogFormat("Scene load local done in GameManager. Is client:{0}", isClient); EntitiesLoaded = true; if(isClient) diff --git a/Assets/GWConquest/Scripts/Planet.cs b/Assets/GWConquest/Scripts/Planet.cs index 13c4e71..b3ac720 100644 --- a/Assets/GWConquest/Scripts/Planet.cs +++ b/Assets/GWConquest/Scripts/Planet.cs @@ -266,7 +266,7 @@ namespace GWConquest public void FinishSetup() { - if(IsOwner) + if(IsServer) { UpdateConnectionsServer(); } @@ -353,7 +353,7 @@ namespace GWConquest private void FixedUpdate() { - if(GameManager.EntitiesLoaded && formationsChangedSinceLastFrame) + if(GameManager.EntitiesLoaded && IsSpawned && formationsChangedSinceLastFrame) { FormationsChanged?.Invoke(); diff --git a/Assets/GWConquest/Scripts/Player.cs b/Assets/GWConquest/Scripts/Player.cs index c1aaf1d..5b14241 100644 --- a/Assets/GWConquest/Scripts/Player.cs +++ b/Assets/GWConquest/Scripts/Player.cs @@ -64,13 +64,25 @@ namespace GWConquest { Debug.Log("Attaching player..."); PlayerList.Add(this); - if(IsOwner) + if(IsServer) { playerID.Value = (ushort) PlayerList.IndexOf(this); } knownPlanetsList.OnListChanged += OnPlanetsUpdated; visiblePlanetsList.OnListChanged += OnPlanetsUpdated; + + if(!IsServer) + { + CurrentPlayer = this; + Debug.LogFormat("Current player is {0} with connection ID {1}", this, OwnerClientId); + + if(GWNetworkManager.SceneSynchronized) + { + Debug.Log("The rest of the scene is synchronized. Completing scene load..."); + GWNetworkManager.Instance.ClientSceneLoadComplete(); + } + } } @@ -80,15 +92,6 @@ namespace GWConquest PlayerList.Remove(this); } - public override void OnGainedOwnership() - { - if(!IsServer) - { - CurrentPlayer = this; - Debug.LogFormat("Current player is {0} with connection ID {1}", this, OwnerClientId); - } - } - public void SetFaction(ushort ind) { factionIndex.Value = ind; @@ -114,15 +117,6 @@ namespace GWConquest } } - public void AssignControl(NetworkObject obj) - { - if(!IsAI) - { - obj.ChangeOwnership(OwnerClientId); - } - - } - public void AssignStartingPlanets() { if(Faction.StartingPlanets != null) diff --git a/Assets/GWConquest/Scripts/Unit.cs b/Assets/GWConquest/Scripts/Unit.cs index e7f8030..ebd4213 100644 --- a/Assets/GWConquest/Scripts/Unit.cs +++ b/Assets/GWConquest/Scripts/Unit.cs @@ -362,7 +362,7 @@ namespace GWConquest Death, Damage, Demoralized, ShieldDamage, ShieldBroken, GlancingHit } - [ClientRpc] + [ClientRpc(Delivery = RpcDelivery.Unreliable)] public void PlayUnitDamageAnimationClientRpc(WeaponType weaponType, DamageAnimationType animationType, NullableNetworkBehaviourReference attacker) { if(IconEnabled) @@ -433,7 +433,7 @@ namespace GWConquest public void FixedUpdate() { - if (IsServer) + if (IsSpawned && IsServer) { var battle = CurrentBattle; if (!IsDead && battle != null && !battle.IsInPreparing) @@ -542,7 +542,7 @@ namespace GWConquest SetActionCooldown(GameManager.Instance.ArtilleryCooldown / movement); } - [ServerRpc] + [ServerRpc(RequireOwnership = false)] public void StartShellingServerRpc(NetworkBehaviourReference target) { StartShelling(target.GetBehaviour());