Skip to content

Commit b9f58b4

Browse files
committed
Syncing for map room modules.
1 parent 510f701 commit b9f58b4

4 files changed

Lines changed: 69 additions & 11 deletions

File tree

NitroxClient/GameLogic/Bases/Spawning/BaseBioReactorSpawnProcessor.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,9 @@ public override void SpawnPostProcess(Base latestBase, Int3 latestCell, GameObje
2626
BaseBioReactorGeometry bioReactor = finishedPiece.RequireComponent<BaseBioReactorGeometry>();
2727
GameObject bioReactorModule = ((BaseBioReactor)bioReactor.ReflectionCall("GetModule")).gameObject;
2828

29-
NitroxId moduleId = generateDeterministicReactorModuleId(reactorId);
29+
NitroxId moduleId = reactorId.Increment();
3030
NitroxEntity.SetNewId(bioReactorModule, moduleId);
3131
}
3232

33-
private NitroxId generateDeterministicReactorModuleId(NitroxId parentId)
34-
{
35-
string guid = parentId.ToString();
36-
char newLastChar = (guid.Last() == '0') ? '1' : '0';
37-
38-
string moduleGuid = guid.Substring(0, guid.Length - 1) + newLastChar;
39-
40-
return new NitroxId(moduleGuid);
41-
}
42-
4333
}
4434
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using NitroxClient.MonoBehaviours;
3+
using NitroxModel.DataStructures;
4+
using UnityEngine;
5+
6+
namespace NitroxClient.GameLogic.Bases.Spawning
7+
{
8+
/*
9+
* When a map room is created, two objects are added to the base: a BaseMapRoom piece that is
10+
* entirely geometry and a MapRoomFunctionality game object in the base root. This class sets
11+
* deterministic ids on non-geometry pieces, such as the map module upgrade storage area, so
12+
* they can stay in sync during player interactions.
13+
*/
14+
public class MapRoomSpawnProcessor : BasePieceSpawnProcessor
15+
{
16+
public override TechType[] ApplicableTechTypes { get; } =
17+
{
18+
TechType.BaseMapRoom
19+
};
20+
21+
public override void SpawnPostProcess(Base latestBase, Int3 latestCell, GameObject finishedPiece)
22+
{
23+
NitroxId mapRoomGeometryPieceId = NitroxEntity.GetId(finishedPiece);
24+
GameObject mapRoomFunctionality = FindUntaggedMapRoomFunctionality(latestBase);
25+
26+
NitroxId mapRoomFunctionalityId = mapRoomGeometryPieceId.Increment();
27+
NitroxEntity.SetNewId(mapRoomFunctionality, mapRoomFunctionalityId);
28+
29+
GameObject mapRoomModules = mapRoomFunctionality.FindChild("MapRoomUpgrades");
30+
NitroxId mapRoomModulesId = mapRoomFunctionalityId.Increment();
31+
NitroxEntity.SetNewId(mapRoomModules, mapRoomModulesId);
32+
}
33+
34+
private GameObject FindUntaggedMapRoomFunctionality(Base latestBase)
35+
{
36+
foreach (Transform child in latestBase.transform)
37+
{
38+
NitroxEntity nitroxEntity = child.GetComponent<NitroxEntity>();
39+
MapRoomFunctionality mapRoomFunctionality = child.GetComponent<MapRoomFunctionality>();
40+
41+
if (mapRoomFunctionality && !nitroxEntity)
42+
{
43+
return child.gameObject;
44+
}
45+
}
46+
47+
throw new Exception("Unable to locate recently built map room");
48+
}
49+
}
50+
}

NitroxClient/NitroxClient.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<Compile Include="Communication\Packets\Processors\RocketStageUpdateProcessor.cs" />
6262
<Compile Include="Communication\Packets\Processors\VehicleSpawnedProcessor.cs" />
6363
<Compile Include="Debuggers\EntityDebugger.cs" />
64+
<Compile Include="GameLogic\Bases\Spawning\MapRoomSpawnProcessor.cs" />
6465
<Compile Include="GameLogic\Bases\Spawning\BaseBioReactorSpawnProcessor.cs" />
6566
<Compile Include="GameLogic\Bases\Spawning\BasePieceSpawnProcessor.cs" />
6667
<Compile Include="GameLogic\Bases\Spawning\BaseLadderSpawnProcessor.cs" />

NitroxModel/DataStructures/NitroxId.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Runtime.Serialization;
45
using System.Security.Permissions;
56
using ProtoBufNet;
@@ -30,6 +31,11 @@ public NitroxId(string str)
3031
guid = new Guid(str);
3132
}
3233

34+
public NitroxId(Guid guid)
35+
{
36+
this.guid = guid;
37+
}
38+
3339
public NitroxId(byte[] bytes)
3440
{
3541
guid = new Guid(bytes);
@@ -82,5 +88,16 @@ public override string ToString()
8288
{
8389
return guid.ToString();
8490
}
91+
92+
static int[] byteOrder = { 15, 14, 13, 12, 11, 10, 9, 8, 6, 7, 4, 5, 0, 1, 2, 3 };
93+
94+
public NitroxId Increment()
95+
{
96+
byte[] bytes = guid.ToByteArray();
97+
bool canIncrement = byteOrder.Any(i => ++bytes[i] != 0);
98+
Guid nextGuid = new Guid(canIncrement ? bytes : new byte[16]);
99+
100+
return new NitroxId(nextGuid);
101+
}
85102
}
86103
}

0 commit comments

Comments
 (0)