Skip to content

Commit

Permalink
* Fixed a bug with some base piece types not having their guid copied…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
Sunrunner37 committed Jan 13, 2019
1 parent 696cea4 commit c0cb780
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ private void SpawnBasePieces(List<BasePiece> basePieces)
buildEventQueue.EnqueueBasePiecePlaced(basePiece);

if (basePiece.ConstructionCompleted)
{
buildEventQueue.EnqueueConstructionCompleted(basePiece.Guid, basePiece.NewBaseGuid);
{
buildEventQueue.EnqueueConstructionCompleted(basePiece.Guid, basePiece.BaseGuid);
}
else
{
Expand Down
10 changes: 5 additions & 5 deletions NitroxClient/GameLogic/Bases/BuildThrottlingQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public void EnqueueBasePiecePlaced(BasePiece basePiece)
Enqueue(new BasePiecePlacedEvent(basePiece));
}

public void EnqueueConstructionCompleted(string guid, Optional<string> 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)
Expand Down Expand Up @@ -97,12 +97,12 @@ public bool RequiresFreshFrame()
public class ConstructionCompletedEvent : BuildEvent
{
public string Guid { get; }
public Optional<string> NewBaseCreatedGuid { get; }
public string BaseGuid { get; }

public ConstructionCompletedEvent(string guid, Optional<string> newBaseCreatedGuid)
public ConstructionCompletedEvent(string guid, string baseGuid)
{
Guid = guid;
NewBaseCreatedGuid = newBaseCreatedGuid;
BaseGuid = baseGuid;
}

public bool RequiresFreshFrame()
Expand Down
16 changes: 11 additions & 5 deletions NitroxClient/GameLogic/Building.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -87,17 +88,17 @@ public void ChangeConstructionAmount(GameObject gameObject, float amount)

public void ConstructionComplete(GameObject ghost)
{
Optional<string> newlyConstructedBaseGuid = Optional<string>.Empty();
string baseGuid = null;
Optional<object> opConstructedBase = TransientLocalObjectManager.Get(TransientObjectType.BASE_GHOST_NEWLY_CONSTRUCTED_BASE_GAMEOBJECT);

string guid = GuidHelper.GetGuid(ghost);

if (opConstructedBase.IsPresent())
{
GameObject constructedBase = (GameObject)opConstructedBase.Get();
newlyConstructedBaseGuid = Optional<string>.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<ConstructableBase>() != null)
Expand All @@ -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<Base>().gameObject);
}
}
ConstructionCompleted constructionCompleted = new ConstructionCompleted(guid, newlyConstructedBaseGuid);

ConstructionCompleted constructionCompleted = new ConstructionCompleted(guid, baseGuid);
packetSender.Send(constructionCompleted);
}

Expand Down
12 changes: 7 additions & 5 deletions NitroxClient/MonoBehaviours/ThrottledBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<ConstructableBase>();
Expand All @@ -176,12 +178,12 @@ private void ConstructionCompleted(ConstructionCompletedEvent constructionComple
Constructable constructable = constructing.GetComponent<Constructable>();
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);
}
}

Expand Down
13 changes: 2 additions & 11 deletions NitroxModel/DataStructures/GameLogic/BasePiece.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>.OfNullable(value); }
}

[ProtoIgnore]
public Optional<string> NewBaseGuid { get; set; }
public string BaseGuid { get; set; }

[ProtoMember(12, DynamicType = true)]
public RotationMetadata SerializableRotationMetadata
Expand All @@ -79,7 +72,6 @@ public BasePieceMetadata SerializableMetadata

public BasePiece()
{
NewBaseGuid = Optional<string>.Empty();
ParentGuid = Optional<string>.Empty();
RotationMetadata = Optional<RotationMetadata>.Empty();
Metadata = Optional<BasePieceMetadata>.Empty();
Expand All @@ -97,14 +89,13 @@ public BasePiece(string guid, Vector3 itemPosition, Quaternion rotation, Vector3
IsFurniture = isFurniture;
ConstructionAmount = 0.0f;
ConstructionCompleted = false;
NewBaseGuid = Optional<string>.Empty();
RotationMetadata = rotationMetadata;
Metadata = Optional<BasePieceMetadata>.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 + "]";
}
}
}
10 changes: 4 additions & 6 deletions NitroxModel/Packets/ConstructionCompleted.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
using System;
using NitroxModel.DataStructures.Util;
using UnityEngine;

namespace NitroxModel.Packets
{
[Serializable]
public class ConstructionCompleted : Packet
{
public string Guid { get; }
public Optional<string> NewBaseCreatedGuid { get; }
public string BaseGuid { get; }

public ConstructionCompleted(string guid, Optional<string> 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 + "]";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Harmony;
using NitroxClient.GameLogic.Helper;
using NitroxModel.Helper;
using NitroxModel.Logger;
using UnityEngine;
using static NitroxClient.GameLogic.Helper.TransientLocalObjectManager;

Expand Down
10 changes: 4 additions & 6 deletions NitroxPatcher/Patches/Base_ClearGeometry_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int3, string> LastClearedCellsByGuid = new Dictionary<Int3, string>();
public static Dictionary<Vector3, string> LastClearedCellsByPosition = new Dictionary<Vector3, string>();

public static void Prefix(Base __instance)
{
Expand All @@ -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<UniqueIdentifier>() != null)
if(child.gameObject.GetComponent<UniqueIdentifier>() != null)
{
string guid = child.gameObject.GetComponent<UniqueIdentifier>().Id;
LastClearedCellsByGuid[cell] = guid;
LastClearedCellsByPosition[child.position] = guid;
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions NitroxPatcher/Patches/Base_SpawnPiece_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
12 changes: 4 additions & 8 deletions NitroxServer/GameLogic/Bases/BaseData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void BasePieceConstructionAmountChanged(string guid, float constructionAm
}
}

public void BasePieceConstructionCompleted(string guid, Optional<string> newlyCreatedParentGuid)
public void BasePieceConstructionCompleted(string guid, string baseGuid)
{
BasePiece basePiece;

Expand All @@ -77,14 +77,10 @@ public void BasePieceConstructionCompleted(string guid, Optional<string> newlyCr
{
basePiece.ConstructionAmount = 1.0f;
basePiece.ConstructionCompleted = true;
basePiece.NewBaseGuid = newlyCreatedParentGuid;
basePiece.BaseGuid = baseGuid;
basePiece.ParentGuid = Optional<string>.OfNullable(baseGuid);

if (newlyCreatedParentGuid.IsPresent())
{
basePiece.ParentGuid = Optional<string>.Empty();
}

lock(completedBasePieceHistory)
lock (completedBasePieceHistory)
{
completedBasePieceHistory.Add(basePiece);
}
Expand Down
2 changes: 1 addition & 1 deletion NitroxServer/Serialization/World/PersistedWorldData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c0cb780

Please sign in to comment.