Skip to content

Commit 5c42897

Browse files
authored
Rocket improvements (SubnauticaNitrox#1280)
* Fix for rocket's buildbots * Fix for rocket's stage persistance * Sync for preflights check * Bug fixes
1 parent fd48023 commit 5c42897

16 files changed

+214
-52
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using NitroxClient.Communication.Packets.Processors.Abstract;
2+
using NitroxClient.MonoBehaviours;
3+
using NitroxModel_Subnautica.Packets;
4+
using UnityEngine;
5+
6+
namespace NitroxClient.Communication.Packets.Processors
7+
{
8+
public class RocketPreflightCompleteProcessor : ClientPacketProcessor<RocketPreflightComplete>
9+
{
10+
public override void Process(RocketPreflightComplete packet)
11+
{
12+
GameObject gameObjectRocket = NitroxEntity.RequireObjectFrom(packet.Id);
13+
14+
switch (packet.FlightCheck)
15+
{
16+
//Preflights inside the cockpit, we do want to ignore TimeCapsule for now
17+
case PreflightCheck.LifeSupport:
18+
case PreflightCheck.PrimaryComputer:
19+
20+
CockpitSwitch[] cockpitSwitches = gameObjectRocket.GetComponentsInChildren<CockpitSwitch>(true);
21+
22+
foreach (CockpitSwitch cockpitSwitch in cockpitSwitches)
23+
{
24+
if (!cockpitSwitch.completed && cockpitSwitch.preflightCheck == packet.FlightCheck)
25+
{
26+
cockpitSwitch.animator.SetBool("Completed", true);
27+
cockpitSwitch.completed = true;
28+
29+
cockpitSwitch.preflightCheckSwitch?.CompletePreflightCheck();
30+
31+
if (cockpitSwitch.collision)
32+
{
33+
cockpitSwitch.collision.SetActive(false);
34+
}
35+
}
36+
}
37+
38+
break;
39+
40+
//CommunicationsArray, Hydraulics, AuxiliaryPowerUnit
41+
default:
42+
43+
ThrowSwitch[] throwSwitches = gameObjectRocket.GetComponentsInChildren<ThrowSwitch>(true);
44+
45+
foreach (ThrowSwitch throwSwitch in throwSwitches)
46+
{
47+
if (!throwSwitch.completed && throwSwitch.preflightCheck == packet.FlightCheck)
48+
{
49+
throwSwitch.animator?.SetTrigger("Throw");
50+
throwSwitch.completed = true;
51+
throwSwitch.preflightCheckSwitch?.CompletePreflightCheck();
52+
throwSwitch.cinematicTrigger.showIconOnHandHover = false;
53+
throwSwitch.triggerCollider.enabled = false;
54+
throwSwitch.lamp.GetComponent<SkinnedMeshRenderer>().material = throwSwitch.completeMat;
55+
}
56+
}
57+
58+
break;
59+
}
60+
61+
}
62+
}
63+
}
Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using NitroxClient.Communication.Packets.Processors.Abstract;
2-
using NitroxClient.GameLogic.Helper;
32
using NitroxClient.MonoBehaviours;
43
using NitroxClient.Unity.Helper;
54
using NitroxModel.Helper;
6-
using NitroxModel.Logger;
75
using NitroxModel_Subnautica.Packets;
86
using Story;
97
using UnityEngine;
@@ -14,24 +12,15 @@ public class RocketStageUpdateProcessor : ClientPacketProcessor<RocketStageUpdat
1412
{
1513
public override void Process(RocketStageUpdate packet)
1614
{
17-
GameObject gameObjectConstructor = NitroxEntity.RequireObjectFrom(packet.ConstructorId);
1815
GameObject gameObjectRocket = NitroxEntity.RequireObjectFrom(packet.Id);
1916

2017
Rocket rocket = gameObjectRocket.RequireComponent<Rocket>();
21-
rocket.StartRocketConstruction();
18+
GameObject build = rocket.StartRocketConstruction();
2219

2320
ItemGoalTracker.OnConstruct(packet.CurrentStageTech);
2421

25-
RocketConstructor rocketConstructor = gameObjectConstructor.GetComponentInChildren<RocketConstructor>(true);
26-
if (rocketConstructor)
27-
{
28-
GameObject gameObjectTobuild = SerializationHelper.GetGameObject(packet.SerializedGameObject);
29-
rocketConstructor.ReflectionCall("SendBuildBots", false, false, gameObjectTobuild);
30-
}
31-
else
32-
{
33-
Log.Error($"{nameof(RocketStageUpdateProcessor)}: Can't find attached rocketconstructor with id {packet.ConstructorId} for rocket with id {packet.Id}");
34-
}
22+
RocketConstructor rocketConstructor = gameObjectRocket.RequireComponentInChildren<RocketConstructor>(true);
23+
rocketConstructor.ReflectionCall("SendBuildBots", false, false, build);
3524
}
3625
}
3726
}

NitroxClient/GameLogic/Rockets.cs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
using NitroxClient.Communication.Abstract;
2-
using NitroxClient.GameLogic.Helper;
32
using NitroxModel.DataStructures;
43
using NitroxModel.DataStructures.Util;
54
using NitroxModel.Logger;
65
using NitroxModel_Subnautica.DataStructures.GameLogic;
76
using NitroxModel_Subnautica.Packets;
8-
using UnityEngine;
97

108
namespace NitroxClient.GameLogic
119
{
@@ -20,18 +18,48 @@ public Rockets(IPacketSender packetSender, Vehicles vehicles)
2018
this.vehicles = vehicles;
2119
}
2220

23-
public void BroadcastRocketStateUpdate(NitroxId id, NitroxId constructorId, TechType currentStageTech, GameObject builtGameObject)
21+
/** Rocket states :
22+
* 0 : Launch Platform
23+
* 1 : Gantry
24+
* 2 : Boosters
25+
* 3 : Fuel Reserve
26+
* 4 : Cockpit
27+
* 5 : Final rocket
28+
**/
29+
public void BroadcastRocketStateUpdate(NitroxId id, TechType currentStageTech)
2430
{
2531
Optional<NeptuneRocketModel> model = vehicles.TryGetVehicle<NeptuneRocketModel>(id);
2632

2733
if (model.HasValue)
2834
{
2935
model.Value.CurrentStage += 1;
30-
packetSender.Send(new RocketStageUpdate(id, constructorId, model.Value.CurrentStage, currentStageTech, SerializationHelper.GetBytes(builtGameObject)));
36+
37+
//State 5 cannot be reached for the server based on players events, so we do it by hand
38+
if (model.Value.CurrentStage > 3)
39+
{
40+
model.Value.CurrentStage = 5;
41+
}
42+
43+
packetSender.Send(new RocketStageUpdate(id, model.Value.CurrentStage, currentStageTech));
44+
}
45+
else
46+
{
47+
Log.Error($"{nameof(Rockets.BroadcastRocketStateUpdate)}: Can't find model for rocket with id {id} and currentStageTech {currentStageTech}");
48+
}
49+
}
50+
51+
public void CompletePreflightCheck(NitroxId id, PreflightCheck preflightCheck)
52+
{
53+
Optional<NeptuneRocketModel> model = vehicles.TryGetVehicle<NeptuneRocketModel>(id);
54+
55+
if (model.HasValue)
56+
{
57+
model.Value.PreflightChecks?.Add(preflightCheck);
58+
packetSender.Send(new RocketPreflightComplete(id, preflightCheck));
3159
}
3260
else
3361
{
34-
Log.Error($"{nameof(Rockets)}: Can't find model for rocket with id {id} with constructor {constructorId} and currentStageTech {currentStageTech}");
62+
Log.Error($"{nameof(Rockets.CompletePreflightCheck)}: Can't find model for rocket with id {id}");
3563
}
3664
}
3765

@@ -46,7 +74,7 @@ public void CallElevator(NitroxId id, RocketElevatorPanel panel, bool up)
4674
}
4775
else
4876
{
49-
Log.Error($"{nameof(Rockets)}: Can't find model for rocket with id {id}");
77+
Log.Error($"{nameof(Rockets.CallElevator)}: Can't find model for rocket with id {id}");
5078
}
5179
}
5280
}

NitroxClient/NitroxClient.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<Compile Include="Communication\Packets\Processors\RadioPlayPendingMessageProcessor.cs" />
5858
<Compile Include="Communication\Packets\Processors\ExosuitArmActionProcessor.cs" />
5959
<Compile Include="Communication\Packets\Processors\RocketElevatorCallProcessor.cs" />
60+
<Compile Include="Communication\Packets\Processors\RocketPreflightCompleteProcessor.cs" />
6061
<Compile Include="Communication\Packets\Processors\RocketStageUpdateProcessor.cs" />
6162
<Compile Include="Communication\Packets\Processors\VehicleSpawnedProcessor.cs" />
6263
<Compile Include="Debuggers\EntityDebugger.cs" />

NitroxModel-Subnautica/DataStructures/GameLogic/NeptuneRocketModel.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ namespace NitroxModel_Subnautica.DataStructures.GameLogic
1212
public class NeptuneRocketModel : VehicleModel
1313
{
1414
[ProtoMember(1)]
15-
public NitroxId ConstructorId { get; set; }
15+
public int CurrentStage { get; set; }
1616

1717
[ProtoMember(2)]
18-
public int CurrentStage { get; set; }
18+
public bool ElevatorUp { get; set; }
1919

2020
[ProtoMember(3)]
21-
public bool ElevatorUp { get; set; }
21+
public ThreadSafeCollection<PreflightCheck> PreflightChecks { get; set; }
2222

2323
protected NeptuneRocketModel()
2424
{
@@ -30,12 +30,12 @@ public NeptuneRocketModel(NitroxTechType techType, NitroxId id, NitroxVector3 po
3030
{
3131
CurrentStage = 0;
3232
ElevatorUp = false;
33-
ConstructorId = new NitroxId(); //The ID will be set forever and will be fetched once a rocket base platform starts (see Rocket_Start_Patch)
33+
PreflightChecks = new ThreadSafeCollection<PreflightCheck>();
3434
}
3535

3636
public override string ToString()
3737
{
38-
return $"[NeptuneRocketModel - {base.ToString()}, ConstructorId: {ConstructorId}, CurrentStage: {CurrentStage}, ElevatorUp: {ElevatorUp}]";
38+
return $"[NeptuneRocketModel - {base.ToString()}, CurrentStage: {CurrentStage}, ElevatorUp: {ElevatorUp}, Preflights: {PreflightChecks?.Count}]";
3939
}
4040
}
4141
}

NitroxModel-Subnautica/NitroxModel-Subnautica.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<Compile Include="Packets\CyclopsToggleInternalLighting.cs" />
9090
<Compile Include="Packets\ExosuitArmActionPacket.cs" />
9191
<Compile Include="Packets\RocketElevatorCall.cs" />
92+
<Compile Include="Packets\RocketPreflightComplete.cs" />
9293
<Compile Include="Packets\RocketStageUpdate.cs" />
9394
<Compile Include="Properties\AssemblyInfo.cs" />
9495
</ItemGroup>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using NitroxModel.Packets;
3+
using NitroxModel.DataStructures;
4+
5+
namespace NitroxModel_Subnautica.Packets
6+
{
7+
[Serializable]
8+
public class RocketPreflightComplete : Packet
9+
{
10+
public NitroxId Id { get; }
11+
public PreflightCheck FlightCheck { get; }
12+
13+
public RocketPreflightComplete(NitroxId id, PreflightCheck flightCheck)
14+
{
15+
Id = id;
16+
FlightCheck = flightCheck;
17+
}
18+
19+
public override string ToString()
20+
{
21+
return $"[RocketPreflightComplete - Id: {Id}, FlightCheck: {FlightCheck}]";
22+
}
23+
}
24+
}

NitroxModel-Subnautica/Packets/RocketStageUpdate.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,18 @@ public class RocketStageUpdate : Packet
99
{
1010
public NitroxId Id { get; }
1111
public int NewStage { get; }
12-
public NitroxId ConstructorId { get; }
1312
public TechType CurrentStageTech { get; }
14-
public byte[] SerializedGameObject { get; }
1513

16-
public RocketStageUpdate(NitroxId id, NitroxId constructorId, int newStage, TechType currentStageTech, byte[] serializedGameObject)
14+
public RocketStageUpdate(NitroxId id, int newStage, TechType currentStageTech)
1715
{
1816
Id = id;
19-
ConstructorId = constructorId;
2017
NewStage = newStage;
2118
CurrentStageTech = currentStageTech;
22-
SerializedGameObject = serializedGameObject;
2319
}
2420

2521
public override string ToString()
2622
{
27-
return $"[RocketStageUpdate - Id: {Id}, ConstructorId: {ConstructorId}, NewRocketStage: {NewStage}, CurrentStageTech: {CurrentStageTech}]";
23+
return $"[RocketStageUpdate - Id: {Id}, NewRocketStage: {NewStage}, CurrentStageTech: {CurrentStageTech}]";
2824
}
2925
}
3026
}

NitroxPatcher/NitroxPatcher.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<Compile Include="Patches\Dynamic\PrecursorKeyTerminal_OnHandClick_Patch.cs" />
5050
<Compile Include="Patches\Dynamic\PrecursorTeleporterActivationTerminal_OnProxyHandClick_Patch.cs" />
5151
<Compile Include="Patches\Dynamic\PrecursorTeleporter_OnActivateTeleporter_Patch.cs" />
52+
<Compile Include="Patches\Dynamic\RocketPreflightCheckManager_CompletePreflightCheck_Patch.cs" />
5253
<Compile Include="Patches\Dynamic\StarshipDoor_LockDoor_Patch.cs" />
5354
<Compile Include="Patches\Dynamic\StarshipDoor_OnDoorToggle_Patch.cs" />
5455
<Compile Include="Patches\Dynamic\StarshipDoor_UnlockDoor_Patch.cs" />

NitroxPatcher/Patches/Dynamic/RocketConstructor_StartRocketConstruction_Patch.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using NitroxClient.MonoBehaviours;
77
using NitroxModel.Core;
88
using NitroxModel.DataStructures;
9-
using UnityEngine;
109

1110
namespace NitroxPatcher.Patches.Dynamic
1211
{
@@ -24,29 +23,26 @@ public static IEnumerable<CodeInstruction> Transpiler(MethodBase original, IEnum
2423

2524
/* if (this.crafterLogic.Craft(currentStageTech, craftTime)) {
2625
* GameObject toBuild = this.rocket.StartRocketConstruction();
27-
* -> RocketConstructor_StartRocketConstruction_Patch.Callback(this.rocket, currentStageTech, this, toBuild);
26+
* -> RocketConstructor_StartRocketConstruction_Patch.Callback(this.rocket, currentStageTech);
2827
* ItemGoalTracker.OnConstruct(currentStageTech);
2928
* this.SendBuildBots(toBuild);
3029
* }
3130
*/
3231
if (instruction.opcode == INJECTION_CODE)
3332
{
34-
yield return new CodeInstruction(OpCodes.Ldarg_0);
35-
yield return new CodeInstruction(OpCodes.Ldfld, typeof(RocketConstructor).GetField("rocket", BindingFlags.Public | BindingFlags.Instance)); //this.rocket
3633
yield return new CodeInstruction(OpCodes.Ldarg_0); //this
34+
yield return new CodeInstruction(OpCodes.Ldfld, typeof(RocketConstructor).GetField("rocket", BindingFlags.Public | BindingFlags.Instance)); //this.rocket
3735
yield return new CodeInstruction(OpCodes.Ldloc_0); //techtype
38-
yield return new CodeInstruction(OpCodes.Ldloc_2); //toBuild GO
39-
yield return new CodeInstruction(OpCodes.Call, typeof(RocketConstructor_StartRocketConstruction_Patch).GetMethod("Callback", BindingFlags.Static | BindingFlags.Public));
36+
yield return new CodeInstruction(OpCodes.Call, typeof(RocketConstructor_StartRocketConstruction_Patch).GetMethod("Callback", BindingFlags.Static | BindingFlags.NonPublic));
4037
}
4138

4239
}
4340
}
4441

45-
public static void Callback(Rocket rocketInstanceAttachedToConstructor, RocketConstructor rocketConstructor, TechType currentStageTech, GameObject gameObjectToBuild)
42+
private static void Callback(Rocket rocketInstanceAttachedToConstructor, TechType currentStageTech)
4643
{
4744
NitroxId rocketId = NitroxEntity.GetId(rocketInstanceAttachedToConstructor.gameObject);
48-
NitroxId constructorId = NitroxEntity.GetId(rocketConstructor.gameObject);
49-
NitroxServiceLocator.LocateService<Rockets>().BroadcastRocketStateUpdate(rocketId, constructorId, currentStageTech, gameObjectToBuild);
45+
NitroxServiceLocator.LocateService<Rockets>().BroadcastRocketStateUpdate(rocketId, currentStageTech);
5046
}
5147

5248
public override void Patch(HarmonyInstance harmony)

0 commit comments

Comments
 (0)