From c0cb780eb458850c7637d3cfbc7fa7e4dbff9ad7 Mon Sep 17 00:00:00 2001 From: Sunrunner37 Date: Sun, 13 Jan 2019 01:08:16 +0100 Subject: [PATCH] * Fixed a bug with some base piece types not having their guid copied when base geometry is being recalculated. * When completing a base piece's construction try to create the base (using the parent id) if it does not exist. --- .../Processors/ConstructionCompletedProcessor.cs | 4 ++-- .../Processors/InitialPlayerSyncProcessor.cs | 4 ++-- .../GameLogic/Bases/BuildThrottlingQueue.cs | 10 +++++----- NitroxClient/GameLogic/Building.cs | 16 +++++++++++----- NitroxClient/MonoBehaviours/ThrottledBuilder.cs | 12 +++++++----- .../DataStructures/GameLogic/BasePiece.cs | 13 ++----------- NitroxModel/Packets/ConstructionCompleted.cs | 10 ++++------ .../BaseDeconstructable_Deconstruct_Patch.cs | 1 - .../Patches/Base_ClearGeometry_Patch.cs | 10 ++++------ NitroxPatcher/Patches/Base_SpawnPiece_Patch.cs | 4 +--- .../ConstructionCompletedPacketProcessor.cs | 2 +- NitroxServer/GameLogic/Bases/BaseData.cs | 12 ++++-------- .../Serialization/World/PersistedWorldData.cs | 2 +- 13 files changed, 44 insertions(+), 56 deletions(-) diff --git a/NitroxClient/Communication/Packets/Processors/ConstructionCompletedProcessor.cs b/NitroxClient/Communication/Packets/Processors/ConstructionCompletedProcessor.cs index a1c986bb11..bc5b20a5dc 100644 --- a/NitroxClient/Communication/Packets/Processors/ConstructionCompletedProcessor.cs +++ b/NitroxClient/Communication/Packets/Processors/ConstructionCompletedProcessor.cs @@ -16,8 +16,8 @@ public ConstructionCompletedProcessor(BuildThrottlingQueue buildEventQueue) public override void Process(ConstructionCompleted completedPacket) { - Log.Debug("Processing ConstructionCompleted " + completedPacket.Guid + " " + completedPacket.NewBaseCreatedGuid); - buildEventQueue.EnqueueConstructionCompleted(completedPacket.Guid, completedPacket.NewBaseCreatedGuid); + Log.Debug("Processing ConstructionCompleted " + completedPacket.Guid + " " + completedPacket.BaseGuid); + buildEventQueue.EnqueueConstructionCompleted(completedPacket.Guid, completedPacket.BaseGuid); } } } diff --git a/NitroxClient/Communication/Packets/Processors/InitialPlayerSyncProcessor.cs b/NitroxClient/Communication/Packets/Processors/InitialPlayerSyncProcessor.cs index d90760e4b4..d434b3ac5e 100644 --- a/NitroxClient/Communication/Packets/Processors/InitialPlayerSyncProcessor.cs +++ b/NitroxClient/Communication/Packets/Processors/InitialPlayerSyncProcessor.cs @@ -216,8 +216,8 @@ private void SpawnBasePieces(List basePieces) buildEventQueue.EnqueueBasePiecePlaced(basePiece); if (basePiece.ConstructionCompleted) - { - buildEventQueue.EnqueueConstructionCompleted(basePiece.Guid, basePiece.NewBaseGuid); + { + buildEventQueue.EnqueueConstructionCompleted(basePiece.Guid, basePiece.BaseGuid); } else { diff --git a/NitroxClient/GameLogic/Bases/BuildThrottlingQueue.cs b/NitroxClient/GameLogic/Bases/BuildThrottlingQueue.cs index 130ed18c50..ac80f7ca8a 100644 --- a/NitroxClient/GameLogic/Bases/BuildThrottlingQueue.cs +++ b/NitroxClient/GameLogic/Bases/BuildThrottlingQueue.cs @@ -32,10 +32,10 @@ public void EnqueueBasePiecePlaced(BasePiece basePiece) Enqueue(new BasePiecePlacedEvent(basePiece)); } - public void EnqueueConstructionCompleted(string guid, Optional newBaseCreatedGuid) + public void EnqueueConstructionCompleted(string guid, string baseGuid) { Log.Info("Enqueuing item to have construction completed " + guid); - Enqueue(new ConstructionCompletedEvent(guid, newBaseCreatedGuid)); + Enqueue(new ConstructionCompletedEvent(guid, baseGuid)); } public void EnqueueAmountChanged(string guid, float amount) @@ -97,12 +97,12 @@ public bool RequiresFreshFrame() public class ConstructionCompletedEvent : BuildEvent { public string Guid { get; } - public Optional NewBaseCreatedGuid { get; } + public string BaseGuid { get; } - public ConstructionCompletedEvent(string guid, Optional newBaseCreatedGuid) + public ConstructionCompletedEvent(string guid, string baseGuid) { Guid = guid; - NewBaseCreatedGuid = newBaseCreatedGuid; + BaseGuid = baseGuid; } public bool RequiresFreshFrame() diff --git a/NitroxClient/GameLogic/Building.cs b/NitroxClient/GameLogic/Building.cs index 7823999b32..04d25eadd3 100644 --- a/NitroxClient/GameLogic/Building.cs +++ b/NitroxClient/GameLogic/Building.cs @@ -1,5 +1,6 @@ using NitroxClient.Communication.Abstract; using NitroxClient.GameLogic.Helper; +using NitroxClient.GameLogic.Spawning; using NitroxClient.Unity.Helper; using NitroxModel.DataStructures.GameLogic; using NitroxModel.DataStructures.GameLogic.Buildings.Rotation; @@ -87,7 +88,7 @@ public void ChangeConstructionAmount(GameObject gameObject, float amount) public void ConstructionComplete(GameObject ghost) { - Optional newlyConstructedBaseGuid = Optional.Empty(); + string baseGuid = null; Optional opConstructedBase = TransientLocalObjectManager.Get(TransientObjectType.BASE_GHOST_NEWLY_CONSTRUCTED_BASE_GAMEOBJECT); string guid = GuidHelper.GetGuid(ghost); @@ -95,9 +96,9 @@ public void ConstructionComplete(GameObject ghost) if (opConstructedBase.IsPresent()) { GameObject constructedBase = (GameObject)opConstructedBase.Get(); - newlyConstructedBaseGuid = Optional.Of(GuidHelper.GetGuid(constructedBase)); + baseGuid = GuidHelper.GetGuid(constructedBase); } - + // For base pieces, we must switch the guid from the ghost to the newly constructed piece. // Furniture just uses the same game object as the ghost for the final product. if(ghost.GetComponent() != null) @@ -107,9 +108,14 @@ public void ConstructionComplete(GameObject ghost) UnityEngine.Object.Destroy(ghost); GuidHelper.SetNewGuid(finishedPiece, guid); + + if(baseGuid == null) + { + baseGuid = GuidHelper.GetGuid(finishedPiece.GetComponentInParent().gameObject); + } } - - ConstructionCompleted constructionCompleted = new ConstructionCompleted(guid, newlyConstructedBaseGuid); + + ConstructionCompleted constructionCompleted = new ConstructionCompleted(guid, baseGuid); packetSender.Send(constructionCompleted); } diff --git a/NitroxClient/MonoBehaviours/ThrottledBuilder.cs b/NitroxClient/MonoBehaviours/ThrottledBuilder.cs index ae2204478d..92814ee130 100644 --- a/NitroxClient/MonoBehaviours/ThrottledBuilder.cs +++ b/NitroxClient/MonoBehaviours/ThrottledBuilder.cs @@ -108,6 +108,7 @@ private void ActionBuildEvent(BuildEvent buildEvent) private void BuildBasePiece(BasePiecePlacedEvent basePiecePlacedBuildEvent) { + Log.Info("BuildBasePiece " + basePiecePlacedBuildEvent.BasePiece.Guid + " " + basePiecePlacedBuildEvent.BasePiece.TechType); BasePiece basePiece = basePiecePlacedBuildEvent.BasePiece; GameObject buildPrefab = CraftData.GetBuildPrefab(basePiece.TechType); MultiplayerBuilder.overridePosition = basePiece.ItemPosition; @@ -124,7 +125,7 @@ private void BuildBasePiece(BasePiecePlacedEvent basePiecePlacedBuildEvent) if(basePiece.ParentGuid.IsPresent()) { - parentBase = GuidHelper.RequireObjectFrom(basePiece.ParentGuid.Get()); + parentBase = GuidHelper.GetObjectFrom(basePiece.ParentGuid.Get()).OrElse(null); } Constructable constructable; @@ -155,6 +156,7 @@ private void BuildBasePiece(BasePiecePlacedEvent basePiecePlacedBuildEvent) private void ConstructionCompleted(ConstructionCompletedEvent constructionCompleted) { + Log.Info("Constructed completed " + constructionCompleted.Guid); GameObject constructing = GuidHelper.RequireObjectFrom(constructionCompleted.Guid); ConstructableBase constructableBase = constructing.GetComponent(); @@ -176,12 +178,12 @@ private void ConstructionCompleted(ConstructionCompletedEvent constructionComple Constructable constructable = constructing.GetComponent(); constructable.constructedAmount = 1f; constructable.SetState(true, true); - } + } - if (constructionCompleted.NewBaseCreatedGuid.IsPresent()) + if (constructionCompleted.BaseGuid != null && GuidHelper.GetObjectFrom(constructionCompleted.BaseGuid).IsEmpty()) { - string newBaseGuid = constructionCompleted.NewBaseCreatedGuid.Get(); - ConfigureNewlyConstructedBase(newBaseGuid); + Log.Info("Creating base: " + constructionCompleted.BaseGuid); + ConfigureNewlyConstructedBase(constructionCompleted.BaseGuid); } } diff --git a/NitroxModel/DataStructures/GameLogic/BasePiece.cs b/NitroxModel/DataStructures/GameLogic/BasePiece.cs index a79b966a7d..956375aa55 100644 --- a/NitroxModel/DataStructures/GameLogic/BasePiece.cs +++ b/NitroxModel/DataStructures/GameLogic/BasePiece.cs @@ -48,14 +48,7 @@ public string SerializableParentBaseGuid { public bool IsFurniture { get; set; } [ProtoMember(11)] - public string SerializableNewBaseGuid - { - get { return (NewBaseGuid.IsPresent()) ? NewBaseGuid.Get() : null; } - set { NewBaseGuid = Optional.OfNullable(value); } - } - - [ProtoIgnore] - public Optional NewBaseGuid { get; set; } + public string BaseGuid { get; set; } [ProtoMember(12, DynamicType = true)] public RotationMetadata SerializableRotationMetadata @@ -79,7 +72,6 @@ public BasePieceMetadata SerializableMetadata public BasePiece() { - NewBaseGuid = Optional.Empty(); ParentGuid = Optional.Empty(); RotationMetadata = Optional.Empty(); Metadata = Optional.Empty(); @@ -97,14 +89,13 @@ public BasePiece(string guid, Vector3 itemPosition, Quaternion rotation, Vector3 IsFurniture = isFurniture; ConstructionAmount = 0.0f; ConstructionCompleted = false; - NewBaseGuid = Optional.Empty(); RotationMetadata = rotationMetadata; Metadata = Optional.Empty(); } public override string ToString() { - return "[BasePiece - ItemPosition: " + ItemPosition + " Guid: " + Guid + " Rotation: " + Rotation + " CameraPosition: " + CameraPosition + "CameraRotation: " + CameraRotation + " TechType: " + TechType + " ParentGuid: " + ParentGuid + " ConstructionAmount: " + ConstructionAmount + " IsFurniture: " + IsFurniture + " NewBaseGuid: " + NewBaseGuid + " RotationMetadata: " + RotationMetadata + "]"; + return "[BasePiece - ItemPosition: " + ItemPosition + " Guid: " + Guid + " Rotation: " + Rotation + " CameraPosition: " + CameraPosition + "CameraRotation: " + CameraRotation + " TechType: " + TechType + " ParentGuid: " + ParentGuid + " ConstructionAmount: " + ConstructionAmount + " IsFurniture: " + IsFurniture + " BaseGuid: " + BaseGuid + " RotationMetadata: " + RotationMetadata + "]"; } } } diff --git a/NitroxModel/Packets/ConstructionCompleted.cs b/NitroxModel/Packets/ConstructionCompleted.cs index 1aa2bc0c84..3403a894a5 100644 --- a/NitroxModel/Packets/ConstructionCompleted.cs +++ b/NitroxModel/Packets/ConstructionCompleted.cs @@ -1,6 +1,4 @@ using System; -using NitroxModel.DataStructures.Util; -using UnityEngine; namespace NitroxModel.Packets { @@ -8,17 +6,17 @@ namespace NitroxModel.Packets public class ConstructionCompleted : Packet { public string Guid { get; } - public Optional NewBaseCreatedGuid { get; } + public string BaseGuid { get; } - public ConstructionCompleted(string guid, Optional newBaseCreatedGuid) + public ConstructionCompleted(string guid, string baseGuid) { Guid = guid; - NewBaseCreatedGuid = newBaseCreatedGuid; + BaseGuid = baseGuid; } public override string ToString() { - return "[ConstructionCompleted Guid: " + Guid + " NewBaseCreatedGuid: " + NewBaseCreatedGuid + "]"; + return "[ConstructionCompleted Guid: " + Guid + " BaseGuid: " + BaseGuid + "]"; } } } diff --git a/NitroxPatcher/Patches/BaseDeconstructable_Deconstruct_Patch.cs b/NitroxPatcher/Patches/BaseDeconstructable_Deconstruct_Patch.cs index dc48de9e02..8cd0bc5c03 100644 --- a/NitroxPatcher/Patches/BaseDeconstructable_Deconstruct_Patch.cs +++ b/NitroxPatcher/Patches/BaseDeconstructable_Deconstruct_Patch.cs @@ -5,7 +5,6 @@ using Harmony; using NitroxClient.GameLogic.Helper; using NitroxModel.Helper; -using NitroxModel.Logger; using UnityEngine; using static NitroxClient.GameLogic.Helper.TransientLocalObjectManager; diff --git a/NitroxPatcher/Patches/Base_ClearGeometry_Patch.cs b/NitroxPatcher/Patches/Base_ClearGeometry_Patch.cs index b540c46c6e..ee4d65b321 100644 --- a/NitroxPatcher/Patches/Base_ClearGeometry_Patch.cs +++ b/NitroxPatcher/Patches/Base_ClearGeometry_Patch.cs @@ -19,7 +19,7 @@ public class Base_ClearGeometry_Patch : NitroxPatch * them so that we can update the newly placed pieces with the proper id. The new * pieces are added by Base.SpawnPiece (see that patch) */ - public static Dictionary LastClearedCellsByGuid = new Dictionary(); + public static Dictionary LastClearedCellsByPosition = new Dictionary(); public static void Prefix(Base __instance) { @@ -43,14 +43,12 @@ public static void Prefix(Base __instance) { Transform child = cellObject.GetChild(i); - if(child != null && child.gameObject != null) + if (child != null && child.gameObject != null) { - Int3 cell = __instance.WorldToGrid(child.position); - - if(cell != Int3.zero && child.gameObject.GetComponent() != null) + if(child.gameObject.GetComponent() != null) { string guid = child.gameObject.GetComponent().Id; - LastClearedCellsByGuid[cell] = guid; + LastClearedCellsByPosition[child.position] = guid; } } } diff --git a/NitroxPatcher/Patches/Base_SpawnPiece_Patch.cs b/NitroxPatcher/Patches/Base_SpawnPiece_Patch.cs index 0429d539d8..85cec8ae32 100644 --- a/NitroxPatcher/Patches/Base_SpawnPiece_Patch.cs +++ b/NitroxPatcher/Patches/Base_SpawnPiece_Patch.cs @@ -24,11 +24,9 @@ public static void Postfix(Base __instance, Transform __result) return; } - Int3 cell = __instance.WorldToGrid(__result.position); - string guid; - if (Base_ClearGeometry_Patch.LastClearedCellsByGuid.TryGetValue(cell, out guid)) + if (Base_ClearGeometry_Patch.LastClearedCellsByPosition.TryGetValue(__result.position, out guid)) { GuidHelper.SetNewGuid(__result.gameObject, guid); } diff --git a/NitroxServer/Communication/Packets/Processors/ConstructionCompletedPacketProcessor.cs b/NitroxServer/Communication/Packets/Processors/ConstructionCompletedPacketProcessor.cs index b108da88dd..81eee32c5b 100644 --- a/NitroxServer/Communication/Packets/Processors/ConstructionCompletedPacketProcessor.cs +++ b/NitroxServer/Communication/Packets/Processors/ConstructionCompletedPacketProcessor.cs @@ -18,7 +18,7 @@ public ConstructionCompletedPacketProcessor(BaseData baseData, PlayerManager pla public override void Process(ConstructionCompleted packet, Player player) { - baseData.BasePieceConstructionCompleted(packet.Guid, packet.NewBaseCreatedGuid); + baseData.BasePieceConstructionCompleted(packet.Guid, packet.BaseGuid); playerManager.SendPacketToOtherPlayers(packet, player); } } diff --git a/NitroxServer/GameLogic/Bases/BaseData.cs b/NitroxServer/GameLogic/Bases/BaseData.cs index 8e09e7fca6..e99b52a2bc 100644 --- a/NitroxServer/GameLogic/Bases/BaseData.cs +++ b/NitroxServer/GameLogic/Bases/BaseData.cs @@ -67,7 +67,7 @@ public void BasePieceConstructionAmountChanged(string guid, float constructionAm } } - public void BasePieceConstructionCompleted(string guid, Optional newlyCreatedParentGuid) + public void BasePieceConstructionCompleted(string guid, string baseGuid) { BasePiece basePiece; @@ -77,14 +77,10 @@ public void BasePieceConstructionCompleted(string guid, Optional newlyCr { basePiece.ConstructionAmount = 1.0f; basePiece.ConstructionCompleted = true; - basePiece.NewBaseGuid = newlyCreatedParentGuid; + basePiece.BaseGuid = baseGuid; + basePiece.ParentGuid = Optional.OfNullable(baseGuid); - if (newlyCreatedParentGuid.IsPresent()) - { - basePiece.ParentGuid = Optional.Empty(); - } - - lock(completedBasePieceHistory) + lock (completedBasePieceHistory) { completedBasePieceHistory.Add(basePiece); } diff --git a/NitroxServer/Serialization/World/PersistedWorldData.cs b/NitroxServer/Serialization/World/PersistedWorldData.cs index 874ccf41aa..df83061005 100644 --- a/NitroxServer/Serialization/World/PersistedWorldData.cs +++ b/NitroxServer/Serialization/World/PersistedWorldData.cs @@ -14,7 +14,7 @@ namespace NitroxServer.Serialization.World [ProtoContract] public class PersistedWorldData { - private const long CURRENT_VERSION = 5; + private const long CURRENT_VERSION = 6; [ProtoMember(1)] public long version { get; set; } = CURRENT_VERSION;