-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync radiation leaks, added a "real elapsed time"
- Loading branch information
1 parent
8abb67b
commit b8eb8c4
Showing
23 changed files
with
418 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
NitroxClient/GameLogic/Spawning/Metadata/Extractor/RadiationMetadataExtractor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using NitroxClient.GameLogic.Spawning.Metadata.Extractor.Abstract; | ||
using NitroxModel.Core; | ||
using NitroxModel.DataStructures.GameLogic.Entities.Metadata; | ||
|
||
namespace NitroxClient.GameLogic.Spawning.Metadata.Extractor; | ||
|
||
public class RadiationMetadataExtractor : EntityMetadataExtractor<RadiationLeak, RadiationMetadata> | ||
{ | ||
public override RadiationMetadata Extract(RadiationLeak leak) | ||
{ | ||
// Note: this extractor should only be used when this radiation leak is being repaired | ||
TimeManager timeManager = NitroxServiceLocator.LocateService<TimeManager>(); | ||
float realTimeFix = leak.liveMixin.IsFullHealth() ? (float)timeManager.RealTimeElapsed : -1; | ||
return new(leak.liveMixin.health, realTimeFix); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
NitroxClient/GameLogic/Spawning/Metadata/Processor/RadiationMetadataProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using NitroxClient.Communication; | ||
using NitroxClient.GameLogic.Spawning.Metadata.Processor.Abstract; | ||
using NitroxModel.Core; | ||
using NitroxModel.DataStructures.GameLogic.Entities.Metadata; | ||
using NitroxModel.Packets; | ||
using UnityEngine; | ||
|
||
namespace NitroxClient.GameLogic.Spawning.Metadata.Processor; | ||
|
||
public class RadiationMetadataProcessor : EntityMetadataProcessor<RadiationMetadata> | ||
{ | ||
public override void ProcessMetadata(GameObject gameObject, RadiationMetadata metadata) | ||
{ | ||
if (!gameObject.TryGetComponent(out LiveMixin liveMixin)) | ||
{ | ||
Log.Error($"[{nameof(RadiationMetadataProcessor)}] Couldn't find LiveMixin on {gameObject}"); | ||
return; | ||
} | ||
LiveMixinManager liveMixinManager = NitroxServiceLocator.LocateService<LiveMixinManager>(); | ||
using (PacketSuppressor<EntityMetadataUpdate>.Suppress()) | ||
{ | ||
liveMixinManager.SyncRemoteHealth(liveMixin, metadata.Health); | ||
} | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
NitroxClient/GameLogic/Spawning/WorldEntities/RadiationLeakEntitySpawner.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using NitroxClient.GameLogic.Spawning.Abstract; | ||
using NitroxClient.MonoBehaviours; | ||
using NitroxModel.DataStructures.GameLogic.Entities; | ||
using NitroxModel.DataStructures.GameLogic.Entities.Metadata; | ||
using NitroxModel.DataStructures.Util; | ||
using UnityEngine; | ||
|
||
namespace NitroxClient.GameLogic.Spawning.WorldEntities; | ||
|
||
public class RadiationLeakEntitySpawner : SyncEntitySpawner<RadiationLeakEntity> | ||
{ | ||
private const int TOTAL_LEAKS = 11; | ||
private readonly TimeManager timeManager; | ||
private readonly List<float> registeredLeaksFixTime = new(); | ||
|
||
public RadiationLeakEntitySpawner(TimeManager timeManager) | ||
{ | ||
this.timeManager = timeManager; | ||
} | ||
|
||
protected override IEnumerator SpawnAsync(RadiationLeakEntity entity, TaskResult<Optional<GameObject>> result) | ||
{ | ||
SpawnSync(entity, result); | ||
yield break; | ||
} | ||
|
||
protected override bool SpawnSync(RadiationLeakEntity entity, TaskResult<Optional<GameObject>> result) | ||
{ | ||
// This script is located under (Aurora Scene) //Aurora-Main/Aurora so it's a good starting point to search through the GameObjects | ||
CrashedShipExploder crashedShipExploder = CrashedShipExploder.main; | ||
LeakingRadiation leakingRadiation = LeakingRadiation.main; | ||
if (!crashedShipExploder || !leakingRadiation || entity.Metadata is not RadiationMetadata metadata) | ||
{ | ||
return true; | ||
} | ||
Transform radiationLeaksHolder = crashedShipExploder.transform.Find("radiationleaks").GetChild(0); | ||
RadiationLeak radiationLeak = radiationLeaksHolder.GetChild(entity.ObjectIndex).GetComponent<RadiationLeak>(); | ||
NitroxEntity.SetNewId(radiationLeak.gameObject, entity.Id); | ||
radiationLeak.liveMixin.health = metadata.Health; | ||
registeredLeaksFixTime.Add(metadata.FixRealTime); | ||
|
||
// We can only calculate the radiation increment and dissipation once we got all radiation leaks info | ||
if (crashedShipExploder.IsExploded() && registeredLeaksFixTime.Count == TOTAL_LEAKS) | ||
{ | ||
RecalculateRadiationRadius(leakingRadiation); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public void RecalculateRadiationRadius(LeakingRadiation leakingRadiation) | ||
{ | ||
float realElapsedTime = (float)timeManager.RealTimeElapsed; | ||
// We substract the explosion time from the real time because before that, the radius doesn't increment | ||
float realExplosionTime = timeManager.AuroraRealExplosionTime; | ||
float maxRegisteredLeakFixTime = registeredLeaksFixTime.Max(); | ||
|
||
// Note: Only increment radius if leaks were fixed AFTER explosion (before, game code doesn't increase radius) | ||
|
||
// If leaks aren't all fixed yet we calculate from current real elapsed time | ||
float deltaTimeAfterExplosion = realElapsedTime - realExplosionTime; | ||
if (maxRegisteredLeakFixTime == -1) | ||
{ | ||
if (deltaTimeAfterExplosion > 0) | ||
{ | ||
float radiusIncrement = deltaTimeAfterExplosion * leakingRadiation.kGrowRate; | ||
// Calculation lines from LeakingRadiation.Update | ||
leakingRadiation.currentRadius = Mathf.Clamp(leakingRadiation.kStartRadius + radiusIncrement, 0f, leakingRadiation.kMaxRadius); | ||
leakingRadiation.damagePlayerInRadius.damageRadius = leakingRadiation.currentRadius; | ||
leakingRadiation.radiatePlayerInRange.radiateRadius = leakingRadiation.currentRadius; | ||
} | ||
// If leaks aren't fixed, we won't need to calculate a radius decrement | ||
return; | ||
} | ||
leakingRadiation.radiationFixed = true; | ||
|
||
// If all leaks are fixed we calculate from the time they were fixed | ||
float deltaAliveTime = maxRegisteredLeakFixTime - realExplosionTime; | ||
if (deltaAliveTime > 0) | ||
{ | ||
float radiusIncrement = deltaAliveTime * leakingRadiation.kGrowRate; | ||
leakingRadiation.currentRadius = Mathf.Clamp(leakingRadiation.kStartRadius + radiusIncrement, 0f, leakingRadiation.kMaxRadius); | ||
} | ||
|
||
// Now calculate the natural dissipation decrement from the time leaks are fixed | ||
// If they were fixed before real explosion time, we calculate from real explosion time | ||
float deltaFixedTimeAfterExplosion = realElapsedTime - Mathf.Max(maxRegisteredLeakFixTime, realExplosionTime); | ||
if (deltaFixedTimeAfterExplosion > 0) | ||
{ | ||
float radiusDecrement = deltaFixedTimeAfterExplosion * leakingRadiation.kNaturalDissipation; | ||
leakingRadiation.currentRadius = Mathf.Clamp(leakingRadiation.currentRadius + radiusDecrement, 0f, leakingRadiation.kMaxRadius); | ||
} | ||
leakingRadiation.damagePlayerInRadius.damageRadius = leakingRadiation.currentRadius; | ||
leakingRadiation.radiatePlayerInRange.radiateRadius = leakingRadiation.currentRadius; | ||
} | ||
|
||
protected override bool SpawnsOwnChildren(RadiationLeakEntity entity) => false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
NitroxModel/DataStructures/GameLogic/Entities/Metadata/RadiationMetadata.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Runtime.Serialization; | ||
|
||
namespace NitroxModel.DataStructures.GameLogic.Entities.Metadata; | ||
|
||
[Serializable, DataContract] | ||
public class RadiationMetadata : EntityMetadata | ||
{ | ||
[DataMember(Order = 1)] | ||
public float Health { get; set; } | ||
|
||
[DataMember(Order = 2)] | ||
public float FixRealTime { get; set; } | ||
|
||
public RadiationMetadata(float health, float fixRealTime = -1) | ||
{ | ||
Health = health; | ||
FixRealTime = fixRealTime; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
NitroxModel/DataStructures/GameLogic/Entities/RadiationLeakEntity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.Serialization; | ||
using BinaryPack.Attributes; | ||
using NitroxModel.DataStructures.GameLogic.Entities.Metadata; | ||
using NitroxModel.DataStructures.Unity; | ||
|
||
namespace NitroxModel.DataStructures.GameLogic.Entities; | ||
|
||
[Serializable, DataContract] | ||
public class RadiationLeakEntity : GlobalRootEntity | ||
{ | ||
[DataMember(Order = 1)] | ||
public int ObjectIndex { get; set; } | ||
|
||
[IgnoreConstructor] | ||
protected RadiationLeakEntity() | ||
{ | ||
// Constructor for serialization. Has to be "protected" for json serialization. | ||
} | ||
|
||
public RadiationLeakEntity(NitroxId id, int objectIndex, RadiationMetadata metadata) | ||
{ | ||
Id = id; | ||
ObjectIndex = objectIndex; | ||
Metadata = metadata; | ||
} | ||
|
||
/// <remarks>Used for deserialization</remarks> | ||
public RadiationLeakEntity(int objectIndex, NitroxTransform transform, int level, string classId, bool spawnedByServer, NitroxId id, NitroxTechType techType, EntityMetadata metadata, NitroxId parentId, List<Entity> childEntities) : | ||
base(transform, level, classId, spawnedByServer, id, techType, metadata, parentId, childEntities) | ||
{ | ||
ObjectIndex = objectIndex; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
NitroxPatcher/Patches/Dynamic/LeakingRadiation_OnConsoleCommand_decontaminate_Patch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Reflection; | ||
using NitroxModel.Helper; | ||
|
||
namespace NitroxPatcher.Patches.Dynamic; | ||
|
||
/// <summary> | ||
/// Disables the "decontaminate" command | ||
/// </summary> | ||
public sealed partial class LeakingRadiation_OnConsoleCommand_decontaminate_Patch : NitroxPatch, IDynamicPatch | ||
{ | ||
private static readonly MethodInfo TARGET_METHOD = Reflect.Method((LeakingRadiation t) => t.OnConsoleCommand_decontaminate()); | ||
|
||
public static bool Prefix() | ||
{ | ||
// This command can't be synced because it would break how radiation leak is currently synced | ||
// Currently all radiation radius calculations depend on the fixed leaks | ||
// But this command would work even if leaks aren't fixed (modifying the radius but only on local client) | ||
Log.InGame(Language.main.Get("Nitrox_CommandNotAvailable")); | ||
return false; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
NitroxPatcher/Patches/Dynamic/LeakingRadiation_Update_Patch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using HarmonyLib; | ||
using NitroxClient.GameLogic; | ||
using NitroxModel.Helper; | ||
using UnityEngine; | ||
|
||
namespace NitroxPatcher.Patches.Dynamic; | ||
|
||
/// <summary> | ||
/// Replaces local use of <see cref="Time.deltaTime"/> by <see cref="TimeManager.DeltaTime"/> | ||
/// </summary> | ||
public sealed partial class LeakingRadiation_Update_Patch : NitroxPatch, IDynamicPatch | ||
{ | ||
public static readonly MethodInfo TARGET_METHOD = Reflect.Method((LeakingRadiation t) => t.Update()); | ||
private static readonly MethodInfo INSERTED_METHOD = Reflect.Method(() => GetDeltaTime()); | ||
private static readonly MethodInfo MATCHING_FIELD = Reflect.Property(() => Time.deltaTime).GetGetMethod(); | ||
|
||
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) | ||
{ | ||
return new CodeMatcher(instructions).MatchStartForward(new CodeMatch(OpCodes.Call, MATCHING_FIELD)) | ||
.SetOperandAndAdvance(INSERTED_METHOD) | ||
.InstructionEnumeration(); | ||
} | ||
|
||
/// <summary> | ||
/// Wrapper for dependency resolving and variable querying | ||
/// </summary> | ||
public static float GetDeltaTime() | ||
{ | ||
return Resolve<TimeManager>().DeltaTime; | ||
} | ||
} |
Oops, something went wrong.