Skip to content

Commit 53f0bb7

Browse files
authored
Fixed the swim in subroots bug when joining a server. (SubnauticaNitrox#1273)
* InitialSync: Disable swimming animation if player is in un-flooded subroot Done by also change the animation style for the remote player initial synch if in not flooded sub. ToDo: Same for EscapePods * Players in escape pods won't swim for players joining - Synched Ids for escape pods - Add value for escape pod id for remote players - Escape pod changed event will be broadcast
1 parent 84f080b commit 53f0bb7

13 files changed

Lines changed: 189 additions & 10 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using NitroxClient.Communication.Packets.Processors.Abstract;
2+
using NitroxClient.GameLogic;
3+
using NitroxClient.MonoBehaviours;
4+
using NitroxModel.DataStructures.Util;
5+
using NitroxModel.Packets;
6+
using UnityEngine;
7+
8+
namespace NitroxClient.Communication.Packets.Processors
9+
{
10+
public class EscapePodChangedProcessor : ClientPacketProcessor<EscapePodChanged>
11+
{
12+
private readonly PlayerManager remotePlayerManager;
13+
14+
public EscapePodChangedProcessor(PlayerManager remotePlayerManager)
15+
{
16+
this.remotePlayerManager = remotePlayerManager;
17+
}
18+
19+
public override void Process(EscapePodChanged packet)
20+
{
21+
Optional<RemotePlayer> remotePlayer = remotePlayerManager.Find(packet.PlayerId);
22+
23+
if (remotePlayer.HasValue)
24+
{
25+
EscapePod escapePod = null;
26+
27+
if (packet.EscapePodId.HasValue)
28+
{
29+
GameObject sub = NitroxEntity.RequireObjectFrom(packet.EscapePodId.Value);
30+
escapePod = sub.GetComponent<EscapePod>();
31+
}
32+
33+
remotePlayer.Value.SetEscapePod(escapePod);
34+
}
35+
}
36+
}
37+
}
38+
39+

NitroxClient/GameLogic/EscapePodManager.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ public EscapePodManager(IPacketSender packetSender, IMultiplayerSession multipla
3636
this.multiplayerSession = multiplayerSession;
3737
}
3838

39-
public void AssignPlayerToEscapePod(EscapePodModel escapePod)
39+
public void AssignPlayerToEscapePod(EscapePodModel escapePod, bool firstTimeSpawning)
4040
{
4141
Validate.NotNull(escapePod, "Escape pod can not be null");
42-
42+
NitroxEntity.SetNewId(EscapePod.main.gameObject, escapePod.Id);
4343
EscapePod.main.transform.position = escapePod.Location.ToUnity();
4444
EscapePod.main.playerSpawn.position = escapePod.Location.ToUnity() + playerSpawnRelativeToEscapePodPosition; // This Might not correctly handle rotated EscapePods
4545

@@ -53,12 +53,14 @@ public void AssignPlayerToEscapePod(EscapePodModel escapePod)
5353
{
5454
Log.Error("Escape pod did not have a rigid body!");
5555
}
56-
57-
Player.main.transform.position = EscapePod.main.playerSpawn.position;
58-
Player.main.transform.rotation = EscapePod.main.playerSpawn.rotation;
59-
56+
57+
Player.main.transform.position = EscapePod.main.playerSpawn.position;
58+
Player.main.transform.rotation = EscapePod.main.playerSpawn.rotation;
59+
if (firstTimeSpawning)
60+
{
61+
Player.main.currentEscapePod = EscapePod.main;
62+
}
6063
Player.main.escapePod.Update(true); // Tells the game to update various EscapePod features
61-
6264
MyEscapePodId = escapePod.Id;
6365
}
6466

@@ -94,6 +96,7 @@ public GameObject CreateNewEscapePod(EscapePodModel model)
9496
else
9597
{
9698
escapePod = Object.Instantiate(EscapePod.main.gameObject);
99+
NitroxEntity.SetNewId(escapePod, model.Id);
97100
}
98101

99102
escapePod.transform.position = model.Location.ToUnity();

NitroxClient/GameLogic/InitialSync/EscapePodInitialSyncProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public override IEnumerator Process(InitialPlayerSync packet, WaitScreen.ManualW
1818
{
1919
EscapePodModel escapePod = packet.EscapePodsData.Find(x => x.Id.Equals(packet.AssignedEscapePodId));
2020

21-
escapePodManager.AssignPlayerToEscapePod(escapePod);
21+
escapePodManager.AssignPlayerToEscapePod(escapePod, packet.FirstTimeConnecting);
2222
yield return null;
2323

2424
escapePodManager.SyncEscapePodIds(packet.EscapePodsData);

NitroxClient/GameLogic/InitialSync/RemotePlayerInitialSyncProcessor.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,31 @@ public override IEnumerator Process(InitialPlayerSync packet, WaitScreen.ManualW
4343

4444
if (sub.HasValue)
4545
{
46-
player.SetSubRoot(sub.Value.GetComponent<SubRoot>());
46+
Log.Debug($"sub value set to {sub.Value}. Try to find subroot");
47+
SubRoot subroot = null;
48+
sub.Value.TryGetComponent<SubRoot>(out subroot);
49+
if (subroot != null)
50+
{
51+
Log.Debug("Found subroot for player. Will add him and update animation.");
52+
player.SetSubRoot(subroot);
53+
// Set the animation for the remote player to standing instead of swimming if player is not in a flooded subroot
54+
if (!subroot.IsUnderwater(player.Body.transform.position))
55+
{
56+
player.UpdateAnimation(AnimChangeType.UNDERWATER, AnimChangeState.OFF);
57+
}
58+
}
59+
Log.Debug("Trying to find escape pod.");
60+
EscapePod escapePod = null;
61+
sub.Value.TryGetComponent<EscapePod>(out escapePod);
62+
if (escapePod != null)
63+
{
64+
Log.Debug("Found escape pod for player. Will add him and update animation.");
65+
player.UpdateAnimation(AnimChangeType.UNDERWATER, AnimChangeState.OFF);
66+
}
4767
}
4868
else
4969
{
50-
Log.Error("Could not spawn remote player into subroot with id: " + playerData.SubRootId.Value);
70+
Log.Error("Could not spawn remote player into subroot/escape pod with id: " + playerData.SubRootId.Value);
5171
}
5272
}
5373

NitroxClient/GameLogic/LocalPlayer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ public void BroadcastSubrootChange(Optional<NitroxId> subrootId)
7979
SubRootChanged packet = new SubRootChanged(multiplayerSession.Reservation.PlayerId, subrootId);
8080
packetSender.Send(packet);
8181
}
82+
public void BroadcastEscapePodChange(Optional<NitroxId> escapePodId)
83+
{
84+
EscapePodChanged packet = new EscapePodChanged(multiplayerSession.Reservation.PlayerId, escapePodId);
85+
packetSender.Send(packet);
86+
}
8287

8388
private GameObject CreateBodyPrototype()
8489
{

NitroxClient/GameLogic/RemotePlayer.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class RemotePlayer : INitroxPlayer
3030

3131
public Vehicle Vehicle { get; private set; }
3232
public SubRoot SubRoot { get; private set; }
33+
public EscapePod EscapePod { get; private set; }
3334
public PilotingChair PilotingChair { get; private set; }
3435

3536
public RemotePlayer(GameObject playerBody, PlayerContext playerContext, List<TechType> equippedTechTypes, PlayerModelManager playerModelManager)
@@ -156,6 +157,23 @@ public void SetSubRoot(SubRoot newSubRoot)
156157
}
157158
}
158159

160+
public void SetEscapePod(EscapePod newEscapePod)
161+
{
162+
if (EscapePod != newEscapePod)
163+
{
164+
if (newEscapePod != null)
165+
{
166+
Attach(newEscapePod.transform, true);
167+
}
168+
else
169+
{
170+
Detach();
171+
}
172+
173+
EscapePod = newEscapePod;
174+
}
175+
}
176+
159177
public void SetVehicle(Vehicle newVehicle)
160178
{
161179
if (Vehicle != newVehicle)

NitroxClient/NitroxClient.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<Compile Include="Communication\PacketReceiver.cs" />
5151
<Compile Include="Communication\Packets\Processors\BedEnterProcessor.cs" />
5252
<Compile Include="Communication\Packets\Processors\EntityMetadataUpdateProcessor.cs" />
53+
<Compile Include="Communication\Packets\Processors\EscapePodChangedProcessor.cs" />
5354
<Compile Include="Communication\Packets\Processors\GameModeChangedProcessor.cs" />
5455
<Compile Include="Communication\Packets\Processors\EnergyMixinValueChangedProcessor.cs" />
5556
<Compile Include="Communication\Packets\Processors\PingRenamedProcessor.cs" />

NitroxModel/NitroxModel.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
<Compile Include="Networking\NitroxDeliveryMethod.cs" />
112112
<Compile Include="Packets\EntityMetadataUpdate.cs" />
113113
<Compile Include="Packets\BedEnter.cs" />
114+
<Compile Include="Packets\EscapePodChanged.cs" />
114115
<Compile Include="Packets\EscapePodRadioRepair.cs" />
115116
<Compile Include="Packets\EscapePodRepair.cs" />
116117
<Compile Include="Packets\IShortString.cs" />
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using NitroxModel.DataStructures.Util;
2+
using System;
3+
using NitroxModel.DataStructures;
4+
5+
namespace NitroxModel.Packets
6+
{
7+
[Serializable]
8+
public class EscapePodChanged : Packet
9+
{
10+
public ushort PlayerId { get; }
11+
public Optional<NitroxId> EscapePodId { get; }
12+
13+
public EscapePodChanged(ushort playerId, Optional<NitroxId> escapePodId)
14+
{
15+
PlayerId = playerId;
16+
EscapePodId = escapePodId;
17+
}
18+
19+
public override string ToString()
20+
{
21+
return "[EscapePodChanged - PlayerId: " + PlayerId + " SubRootId: " + EscapePodId + "]";
22+
}
23+
}
24+
}
25+

NitroxPatcher/NitroxPatcher.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<ItemGroup>
4747
<Compile Include="Main.cs" />
4848
<Compile Include="Patches\Dynamic\OnGoalUnlockTracker_NotifyGoalComplete_Patch.cs" />
49+
<Compile Include="Patches\Dynamic\Player_SetCurrentEscapePod_Patch.cs" />
4950
<Compile Include="Patches\Dynamic\PrecursorKeyTerminal_OnHandClick_Patch.cs" />
5051
<Compile Include="Patches\Dynamic\PrecursorTeleporterActivationTerminal_OnProxyHandClick_Patch.cs" />
5152
<Compile Include="Patches\Dynamic\PrecursorTeleporter_OnActivateTeleporter_Patch.cs" />

0 commit comments

Comments
 (0)