Skip to content

Commit

Permalink
Int3 refactor (SubnauticaNitrox#1265)
Browse files Browse the repository at this point in the history
* Int3 refactor & cleanup

* DataExtensions cleanup

* review changes
  • Loading branch information
dartasen authored Oct 31, 2020
1 parent 0444495 commit 76f8ca5
Show file tree
Hide file tree
Showing 26 changed files with 243 additions and 251 deletions.
2 changes: 1 addition & 1 deletion NitroxClient/GameLogic/Spawning/CellRootSpawner.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using NitroxClient.MonoBehaviours;
using NitroxModel.DataStructures;
using NitroxModel.DataStructures.GameLogic;
using NitroxModel.DataStructures.Util;
using UnityEngine;
using NitroxInt3 = NitroxModel.DataStructures.Int3;

namespace NitroxClient.GameLogic.Spawning
{
Expand Down
10 changes: 4 additions & 6 deletions NitroxClient/GameLogic/Terrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using NitroxModel.Packets;
using NitroxClient.Communication.Abstract;
using UnityEngine;
using NitroxModel_Subnautica.Helper.Int3;
using NitroxModel_Subnautica.DataStructures;

namespace NitroxClient.GameLogic
{
Expand All @@ -15,20 +15,18 @@ public class Terrain
private readonly IMultiplayerSession multiplayerSession;
private readonly IPacketSender packetSender;
private readonly VisibleCells visibleCells;
private readonly PacketReceiver packetReceiver;

private bool cellsPendingSync;
private float timeWhenCellsBecameOutOfSync;

private List<AbsoluteEntityCell> added = new List<AbsoluteEntityCell>();
private List<AbsoluteEntityCell> removed = new List<AbsoluteEntityCell>();

public Terrain(IMultiplayerSession multiplayerSession, IPacketSender packetSender, VisibleCells visibleCells, PacketReceiver packetReceiver)
public Terrain(IMultiplayerSession multiplayerSession, IPacketSender packetSender, VisibleCells visibleCells)
{
this.multiplayerSession = multiplayerSession;
this.packetSender = packetSender;
this.visibleCells = visibleCells;
this.packetReceiver = packetReceiver;
}

public void CellLoaded(Int3 batchId, Int3 cellId, int level)
Expand All @@ -41,7 +39,7 @@ private IEnumerator WaitAndAddCell(Int3 batchId, Int3 cellId, int level)
{
yield return new WaitForSeconds(0.5f);

AbsoluteEntityCell cell = new AbsoluteEntityCell(batchId.Model(), cellId.Model(), level);
AbsoluteEntityCell cell = new AbsoluteEntityCell(batchId.ToDto(), cellId.ToDto(), level);

if (!visibleCells.Contains(cell))
{
Expand All @@ -52,7 +50,7 @@ private IEnumerator WaitAndAddCell(Int3 batchId, Int3 cellId, int level)

public void CellUnloaded(Int3 batchId, Int3 cellId, int level)
{
AbsoluteEntityCell cell = new AbsoluteEntityCell(batchId.Model(), cellId.Model(), level);
AbsoluteEntityCell cell = new AbsoluteEntityCell(batchId.ToDto(), cellId.ToDto(), level);

if (visibleCells.Contains(cell))
{
Expand Down
6 changes: 3 additions & 3 deletions NitroxClient/MonoBehaviours/Overrides/MultiplayerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
using UWE;
using NitroxModel.DataStructures.GameLogic.Buildings.Rotation;
using NitroxModel_Subnautica.DataStructures.GameLogic.Buildings.Rotation;
using NitroxModel_Subnautica.Helper.Int3;
using NitroxModel_Subnautica.DataStructures.GameLogic.Buildings.Rotation.Metadata;
using NitroxModel_Subnautica.DataStructures;

namespace NitroxClient.MonoBehaviours.Overrides
{
Expand Down Expand Up @@ -255,7 +255,7 @@ private static void ApplyRotationMetadata(GameObject ghostModel, RotationMetadat
BaseModuleRotationMetadata baseModuleRotationMetadata = (rotationMetadata as BaseModuleRotationMetadata);
BaseAddModuleGhost module = (component as BaseAddModuleGhost);

module.anchoredFace = new Base.Face(baseModuleRotationMetadata.Cell.Global(), (Base.Direction)baseModuleRotationMetadata.Direction);
module.anchoredFace = new Base.Face(baseModuleRotationMetadata.Cell.ToUnity(), (Base.Direction)baseModuleRotationMetadata.Direction);
module.ReflectionCall("RebuildGhostGeometry");
}
else if (component is BaseAddFaceGhost)
Expand All @@ -265,7 +265,7 @@ private static void ApplyRotationMetadata(GameObject ghostModel, RotationMetadat
Log.Info("Applying BaseAddFaceGhost " + baseModuleRotationMetadata);


Base.Face face = new Base.Face(baseModuleRotationMetadata.Cell.Global(), (Base.Direction)baseModuleRotationMetadata.Direction);
Base.Face face = new Base.Face(baseModuleRotationMetadata.Cell.ToUnity(), (Base.Direction)baseModuleRotationMetadata.Direction);
faceGhost.anchoredFace = face;

Base ghostBase = (Base)faceGhost.ReflectionGet("ghostBase");
Expand Down
12 changes: 4 additions & 8 deletions NitroxModel-Subnautica/DataStructures/DataExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
using System;
using NitroxModel.DataStructures;
using NitroxModel.DataStructures.GameLogic;
using UnityEngine;

namespace NitroxModel_Subnautica.DataStructures
{
public static class DataExtensions
{
public static Int3 ToUnity(this NitroxModel.DataStructures.Int3 v)
public static Int3 ToUnity(this NitroxInt3 v)
{
return new Int3(v.X, v.Y, v.Z);
}

public static NitroxModel.DataStructures.Int3 ToDto(this Int3 v)
public static NitroxInt3 ToDto(this Int3 v)
{
return new NitroxModel.DataStructures.Int3(v.x, v.y, v.z);
return new NitroxInt3(v.x, v.y, v.z);
}

public static NitroxVector3 ToDto(this Vector3 v)
Expand All @@ -26,11 +27,6 @@ public static Vector3 ToUnity(this NitroxVector3 v)
return new Vector3(v.X, v.Y, v.Z);
}

public static Vector3 AsVector3(this NitroxModel.DataStructures.Int3 v)
{
return new Vector3(v.X, v.Y, v.Z);
}

public static NitroxTechType ToDto(this TechType v)
{
return new NitroxTechType(v.ToString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using NitroxModel.DataStructures;
using NitroxModel.DataStructures.GameLogic.Buildings.Rotation;
using NitroxModel_Subnautica.Helper.Int3;
using ProtoBufNet;

namespace NitroxModel_Subnautica.DataStructures.GameLogic.Buildings.Rotation.Metadata
Expand All @@ -10,7 +10,7 @@ namespace NitroxModel_Subnautica.DataStructures.GameLogic.Buildings.Rotation.Met
public class AnchoredFaceRotationMetadata : RotationMetadata
{
[ProtoMember(1)]
public NitroxModel.DataStructures.Int3 Cell { get; set; }
public NitroxInt3 Cell { get; set; }

[ProtoMember(2)]
public int Direction { get; set; }
Expand All @@ -25,14 +25,14 @@ protected AnchoredFaceRotationMetadata() : base(typeof(BaseAddFaceGhost))

public AnchoredFaceRotationMetadata(Int3 cell, int facedirection, int facetype) : base(typeof(BaseAddFaceGhost))
{
Cell = cell.Model();
Cell = cell.ToDto();
Direction = facedirection;
FaceType = facetype;
}

public override string ToString()
{
return "[AnchoredFaceRotationMetadata Cell: " + Cell + " Direction: " + Direction + " FaceType: " + FaceType + "]";
return $"[AnchoredFaceRotationMetadata - Cell: {Cell}, Direction: {Direction}, FaceType: {FaceType}]";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using ProtoBufNet;
using System;
using NitroxModel.DataStructures.GameLogic.Buildings.Rotation;
using NitroxModel_Subnautica.Helper.Int3;
using NitroxModel.DataStructures;

namespace NitroxModel_Subnautica.DataStructures.GameLogic.Buildings.Rotation
{
Expand All @@ -11,7 +11,7 @@ public class BaseModuleRotationMetadata : RotationMetadata
{
// Base modules anchor based on a face. This can be constructed via these two attributes.
[ProtoMember(1)]
public NitroxModel.DataStructures.Int3 Cell { get; set; }
public NitroxInt3 Cell { get; set; }

[ProtoMember(2)]
public int Direction { get; set; }
Expand All @@ -23,13 +23,13 @@ protected BaseModuleRotationMetadata() : base(typeof(BaseAddModuleGhost))

public BaseModuleRotationMetadata(Int3 cell, int direction) : base(typeof(BaseAddModuleGhost))
{
Cell = cell.Model();
Cell = cell.ToDto();
Direction = direction;
}

public override string ToString()
{
return "[BaseModuleRotationMetadata cell: " + Cell + " direction: " + Direction + " ]";
return $"[BaseModuleRotationMetadata - Cell: {Cell}, Direction: {Direction}]";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public CorridorRotationMetadata(int rotation) : base(typeof(BaseAddCorridorGhost

public override string ToString()
{
return "[CorridorRotationMetadata Rotation: " + Rotation + " ]";
return $"[CorridorRotationMetadata - Rotation: {Rotation} ]";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public MapRoomRotationMetadata(byte cellType, int connectionMask) : base(typeof(

public override string ToString()
{
return "[MapRoomRotationMetadata CellType: " + CellType + " ConnectionMask: " + ConnectionMask + " ]";
return $"[MapRoomRotationMetadata - CellType: {CellType}, ConnectionMask: {ConnectionMask}]";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using NitroxModel.DataStructures.GameLogic.Entities;
using NitroxModel.DataStructures.Util;
using NitroxModel_Subnautica.Helper;
using UWE;

namespace NitroxModel_Subnautica.DataStructures.GameLogic.Entities
Expand All @@ -17,9 +16,8 @@ public SubnauticaUweWorldEntityFactory(Dictionary<string, WorldEntityInfo> world

public override Optional<UweWorldEntity> From(string classId)
{
WorldEntityInfo worldEntityInfo;

if (worldEntitiesByClassId.TryGetValue(classId, out worldEntityInfo))
if (worldEntitiesByClassId.TryGetValue(classId, out WorldEntityInfo worldEntityInfo))
{
UweWorldEntity uweWorldEntity = new UweWorldEntity(worldEntityInfo.techType.ToDto(),
worldEntityInfo.localScale.ToDto(),
Expand Down
15 changes: 0 additions & 15 deletions NitroxModel-Subnautica/Helper/Int3Converter.cs

This file was deleted.

2 changes: 1 addition & 1 deletion NitroxModel-Subnautica/Helper/SubnauticaMap.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using NitroxModel.DataStructures;
using NitroxModel.DataStructures.GameLogic;
using NitroxModel.Helper;
using NitroxInt3 = NitroxModel.DataStructures.Int3;

namespace NitroxModel_Subnautica.Helper
{
Expand Down
8 changes: 4 additions & 4 deletions NitroxModel-Subnautica/Helper/VehicleModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public static VehicleModel BuildFrom(NitroxTechType techType, NitroxId construct
switch (techType.ToUnity())
{
case TechType.Seamoth:
return new SeamothModel(techType, constructedItemId, position, rotation, interactiveChildIdentifiers, Optional.Empty, name, hsb, health);
return new SeamothModel(techType, constructedItemId, position, rotation, interactiveChildIdentifiers, dockingBayId, name, hsb, health);
case TechType.Exosuit:
return new ExosuitModel(techType, constructedItemId, position, rotation, interactiveChildIdentifiers, Optional.Empty, name, hsb, health);
return new ExosuitModel(techType, constructedItemId, position, rotation, interactiveChildIdentifiers, dockingBayId, name, hsb, health);
case TechType.Cyclops:
return new CyclopsModel(techType, constructedItemId, position, rotation, interactiveChildIdentifiers, Optional.Empty, name, hsb, health);
return new CyclopsModel(techType, constructedItemId, position, rotation, interactiveChildIdentifiers, dockingBayId, name, hsb, health);
case TechType.RocketBase:
return new NeptuneRocketModel(techType, constructedItemId, position, rotation, interactiveChildIdentifiers, Optional.Empty, name, hsb, health);
return new NeptuneRocketModel(techType, constructedItemId, position, rotation, interactiveChildIdentifiers, dockingBayId, name, hsb, health);
default:
throw new Exception($"Could not build from: {techType}");
}
Expand Down
1 change: 0 additions & 1 deletion NitroxModel-Subnautica/NitroxModel-Subnautica.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
<Compile Include="DataStructures\Surrogates\Vector3Surrogate.cs" />
<Compile Include="Helper\TextureScaler.cs" />
<Compile Include="Helper\SubnauticaMap.cs" />
<Compile Include="Helper\Int3Converter.cs" />
<Compile Include="Helper\VehicleHelper.cs" />
<Compile Include="Helper\VehicleModelComparer.cs" />
<Compile Include="Helper\VehicleModelFactory.cs" />
Expand Down
24 changes: 12 additions & 12 deletions NitroxModel/DataStructures/GameLogic/AbsoluteEntityCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ namespace NitroxModel.DataStructures.GameLogic
public class AbsoluteEntityCell
{
[ProtoMember(1)]
public Int3 BatchId { get; }
public NitroxInt3 BatchId { get; }

[ProtoMember(2)]
public Int3 CellId { get; }
public NitroxInt3 CellId { get; }

[ProtoMember(3)]
public int Level { get; }

private Int3 BatchPosition => BatchId * Map.Main.BatchSize - Map.Main.BatchDimensionCenter;
public Int3 Position => BatchPosition + CellId * GetCellSize();
private NitroxInt3 BatchPosition => BatchId * Map.Main.BatchSize - Map.Main.BatchDimensionCenter;
public NitroxInt3 Position => BatchPosition + CellId * GetCellSize();

public Int3 Center
public NitroxInt3 Center
{
get
{
Int3 cellSize = GetCellSize();
NitroxInt3 cellSize = GetCellSize();
return BatchPosition + CellId * cellSize + (cellSize >> 1);
}
}
Expand All @@ -34,7 +34,7 @@ protected AbsoluteEntityCell()
// Constructor for serialization. Has to be "protected" for json serialization.
}

public AbsoluteEntityCell(Int3 batchId, Int3 cellId, int level)
public AbsoluteEntityCell(NitroxInt3 batchId, NitroxInt3 cellId, int level)
{
BatchId = batchId;
CellId = cellId;
Expand All @@ -46,10 +46,10 @@ public AbsoluteEntityCell(NitroxVector3 worldSpace, int level)
Level = level;

NitroxVector3 localPosition = (worldSpace + Map.Main.BatchDimensionCenter) / Map.Main.BatchSize;
BatchId = Int3.Floor(localPosition);
BatchId = NitroxInt3.Floor(localPosition);

NitroxVector3 cell = (localPosition - BatchId) * GetCellsPerBlock();
CellId = Int3.Floor(new NitroxVector3(cell.X + 0.0001f, cell.Y + 0.0001f, cell.Z + 0.0001f));
CellId = NitroxInt3.Floor(new NitroxVector3(cell.X + 0.0001f, cell.Y + 0.0001f, cell.Z + 0.0001f));
}

public static bool operator ==(AbsoluteEntityCell left, AbsoluteEntityCell right)
Expand All @@ -62,7 +62,7 @@ public AbsoluteEntityCell(NitroxVector3 worldSpace, int level)
return !Equals(left, right);
}

public static Int3 GetCellSize(int level, Int3 blocksPerBatch)
public static NitroxInt3 GetCellSize(int level, NitroxInt3 blocksPerBatch)
{
// Our own implementation for BatchCells.GetCellSize, that works on the server and client.
return blocksPerBatch / GetCellsPerBlock(level);
Expand Down Expand Up @@ -116,12 +116,12 @@ public override int GetHashCode()
}
}

public Int3 GetCellSize()
public NitroxInt3 GetCellSize()
{
return GetCellSize(Map.Main.BatchDimensions);
}

public Int3 GetCellSize(Int3 blocksPerBatch)
public NitroxInt3 GetCellSize(NitroxInt3 blocksPerBatch)
{
return GetCellSize(Level, blocksPerBatch);
}
Expand Down
Loading

0 comments on commit 76f8ca5

Please sign in to comment.