Skip to content

Commit d18c74d

Browse files
Merge pull request #3 from OhmV-IR/DeathMarkers
Death markers
2 parents a070121 + 99d9ce4 commit d18c74d

File tree

13 files changed

+141
-8
lines changed

13 files changed

+141
-8
lines changed

NitroxClient/Communication/MultiplayerSession/ConnectionState/CommunicatingState.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Threading.Tasks;
1+
using System.Threading.Tasks;
22
using NitroxClient.Communication.Abstract;
33

44
namespace NitroxClient.Communication.MultiplayerSession.ConnectionState
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using NitroxClient.Communication.Packets.Processors.Abstract;
2+
using NitroxClient.GameLogic;
3+
using NitroxModel.Packets;
4+
5+
namespace NitroxClient.Communication.Packets.Processors;
6+
7+
public class DeathMarkersChangedProcessor : ClientPacketProcessor<DeathMarkersChanged>
8+
{
9+
private readonly LocalPlayer localPlayer;
10+
11+
public DeathMarkersChangedProcessor(LocalPlayer localPlayer)
12+
{
13+
this.localPlayer = localPlayer;
14+
}
15+
16+
public override void Process(DeathMarkersChanged packet)
17+
{
18+
localPlayer.MarkDeathPointsWithBeacon = packet.MarkDeathPointsWithBeacon;
19+
}
20+
}

NitroxClient/Communication/Packets/Processors/PlayerDeathProcessor.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using NitroxClient.Communication.Packets.Processors.Abstract;
1+
using NitroxClient.Communication.Packets.Processors.Abstract;
22
using NitroxClient.GameLogic;
33
using NitroxModel.Helper;
44
using NitroxModel.Packets;
@@ -20,7 +20,6 @@ public override void Process(PlayerDeathEvent playerDeath)
2020
Log.Debug($"{player.PlayerName} died");
2121
Log.InGame(Language.main.Get("Nitrox_PlayerDied").Replace("{PLAYER}", player.PlayerName));
2222
player.PlayerDeathEvent.Trigger(player);
23-
2423
// TODO: Add any death related triggers (i.e. scoreboard updates, rewards, etc.)
2524
}
2625
}

NitroxClient/GameLogic/InitialSync/PlayerInitialSyncProcessor.cs

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public PlayerInitialSyncProcessor(Items item, ItemContainers itemContainers, Loc
3535
AddStep(sync => SetPlayerStats(sync.PlayerStatsData));
3636
AddStep(sync => SetPlayerGameMode(sync.GameMode));
3737
AddStep(sync => SetPlayerKeepInventoryOnDeath(sync.KeepInventoryOnDeath));
38+
AddStep(sync => SetPlayerMarkDeathPointsWithBeacon(sync.MarkDeathPointsWithBeacon));
3839
}
3940

4041
private void SetPlayerPermissions(Perms permissions)
@@ -141,4 +142,9 @@ private void SetPlayerKeepInventoryOnDeath(bool keepInventoryOnDeath)
141142
{
142143
localPlayer.KeepInventoryOnDeath = keepInventoryOnDeath;
143144
}
145+
146+
private void SetPlayerMarkDeathPointsWithBeacon(bool markDeathPointsWithBeacon)
147+
{
148+
localPlayer.MarkDeathPointsWithBeacon = markDeathPointsWithBeacon;
149+
}
144150
}

NitroxClient/GameLogic/LocalPlayer.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ public class LocalPlayer : ILocalNitroxPlayer
3737
/// </summary>
3838
public ushort? PlayerId => multiplayerSession?.Reservation?.PlayerId;
3939
public PlayerSettings PlayerSettings => multiplayerSession.PlayerSettings;
40-
4140
public Perms Permissions { get; set; }
4241
public IntroCinematicMode IntroCinematicMode { get; set; }
4342
public bool KeepInventoryOnDeath { get; set; }
43+
public bool MarkDeathPointsWithBeacon { get; set; }
4444

4545
public LocalPlayer(IMultiplayerSession multiplayerSession, IPacketSender packetSender, ThrottledPacketSender throttledPacketSender)
4646
{
@@ -53,6 +53,7 @@ public LocalPlayer(IMultiplayerSession multiplayerSession, IPacketSender packetS
5353
Permissions = Perms.PLAYER;
5454
IntroCinematicMode = IntroCinematicMode.NONE;
5555
KeepInventoryOnDeath = false;
56+
MarkDeathPointsWithBeacon = false;
5657
}
5758

5859
public void BroadcastLocation(Vector3 location, Vector3 velocity, Quaternion bodyRotation, Quaternion aimingRotation)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using NitroxModel.DataStructures.Unity;
2+
using NitroxModel_Subnautica.DataStructures;
3+
using UnityEngine;
4+
5+
namespace NitroxClient.MonoBehaviours.Gui.InGame;
6+
public class DeathBeacon : MonoBehaviour
7+
{
8+
private const float DESPAWN_DISTANCE = 20f;
9+
private const float DESPAWN_DISTANCE_SQUARED = DESPAWN_DISTANCE * DESPAWN_DISTANCE;
10+
private const float CHECK_RATE = 1.5f; // in seconds
11+
12+
public static void SpawnDeathBeacon(NitroxVector3 location, string playerName)
13+
{
14+
GameObject beacon = new($"{playerName}DeathBeacon");
15+
beacon.transform.position = location.ToUnity();
16+
PingInstance signal = beacon.AddComponent<PingInstance>();
17+
signal.pingType = PingType.Signal;
18+
signal.origin = beacon.transform;
19+
signal.minDist = DESPAWN_DISTANCE + 15f;
20+
signal._label = Language.main.Get("Nitrox_PlayerDeathBeaconLabel").Replace("{PLAYER}", playerName);
21+
beacon.AddComponent<DeathBeacon>();
22+
signal.displayPingInManager = true;
23+
signal.Initialize();
24+
}
25+
26+
private void Start()
27+
{
28+
InvokeRepeating(nameof(CheckPlayerDistance), 0, CHECK_RATE);
29+
}
30+
31+
private void CheckPlayerDistance()
32+
{
33+
if ((Player.main.transform.position - transform.position).sqrMagnitude <= DESPAWN_DISTANCE_SQUARED)
34+
{
35+
Destroy(gameObject);
36+
}
37+
}
38+
}

NitroxClient/MonoBehaviours/PlayerDeathBroadcaster.cs

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using NitroxClient.GameLogic;
2+
using NitroxClient.MonoBehaviours.Gui.InGame;
3+
using NitroxModel_Subnautica.DataStructures;
24
using UnityEngine;
35

46
namespace NitroxClient.MonoBehaviours;
@@ -16,6 +18,10 @@ public void Awake()
1618

1719
private void PlayerDeath(Player player)
1820
{
21+
if (localPlayer.MarkDeathPointsWithBeacon)
22+
{
23+
DeathBeacon.SpawnDeathBeacon(player.transform.position.ToDto(), localPlayer.PlayerName);
24+
}
1925
localPlayer.BroadcastDeath(player.transform.position);
2026
}
2127

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace NitroxModel.Packets;
2+
3+
public class DeathMarkersChanged : Packet
4+
{
5+
public bool MarkDeathPointsWithBeacon { get; }
6+
7+
public DeathMarkersChanged(bool markDeathPointsWithBeacon)
8+
{
9+
MarkDeathPointsWithBeacon = markDeathPointsWithBeacon;
10+
}
11+
}

NitroxModel/Packets/InitialPlayerSync.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class InitialPlayerSync : Packet
3636
public bool IsFirstPlayer { get; }
3737
public Dictionary<NitroxId, int> BuildOperationIds { get; }
3838
public bool KeepInventoryOnDeath { get; }
39+
public bool MarkDeathPointsWithBeacon { get; }
3940

4041
public InitialPlayerSync(NitroxId playerGameObjectId,
4142
bool firstTimeConnecting,
@@ -59,7 +60,8 @@ public InitialPlayerSync(NitroxId playerGameObjectId,
5960
TimeData timeData,
6061
bool isFirstPlayer,
6162
Dictionary<NitroxId, int> buildOperationIds,
62-
bool keepInventoryOnDeath)
63+
bool keepInventoryOnDeath,
64+
bool markDeathPointsWithBeacon)
6365
{
6466
AssignedEscapePodId = assignedEscapePodId;
6567
PlayerGameObjectId = playerGameObjectId;
@@ -84,6 +86,7 @@ public InitialPlayerSync(NitroxId playerGameObjectId,
8486
IsFirstPlayer = isFirstPlayer;
8587
BuildOperationIds = buildOperationIds;
8688
KeepInventoryOnDeath = keepInventoryOnDeath;
89+
MarkDeathPointsWithBeacon = markDeathPointsWithBeacon;
8790
}
8891

8992
/// <remarks>Used for deserialization</remarks>
@@ -110,7 +113,8 @@ public InitialPlayerSync(
110113
TimeData timeData,
111114
bool isFirstPlayer,
112115
Dictionary<NitroxId, int> buildOperationIds,
113-
bool keepInventoryOnDeath)
116+
bool keepInventoryOnDeath,
117+
bool markDeathPointsWithBeacon)
114118
{
115119
AssignedEscapePodId = assignedEscapePodId;
116120
PlayerGameObjectId = playerGameObjectId;
@@ -135,6 +139,7 @@ public InitialPlayerSync(
135139
IsFirstPlayer = isFirstPlayer;
136140
BuildOperationIds = buildOperationIds;
137141
KeepInventoryOnDeath = keepInventoryOnDeath;
142+
MarkDeathPointsWithBeacon = markDeathPointsWithBeacon;
138143
}
139144
}
140145
}

NitroxModel/Serialization/SubnauticaServerConfig.cs

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public class SubnauticaServerConfig : NitroxConfig<SubnauticaServerConfig>
3030
[PropertyDescription("Prevents players from losing items on death")]
3131
public bool KeepInventoryOnDeath { get; set; } = false;
3232

33+
[PropertyDescription("Places a beacon where players die")]
34+
public bool MarkDeathPointsWithBeacon { get; set; } = true;
35+
3336
[PropertyDescription("Measured in milliseconds")]
3437
public int SaveInterval
3538
{

NitroxPatcher/Patches/Dynamic/Player_OnKill_Patch.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.Linq;
33
using System.Reflection;
44
using System.Reflection.Emit;
@@ -12,6 +12,7 @@ public sealed partial class Player_OnKill_Patch : NitroxPatch, IDynamicPatch
1212
private static readonly MethodInfo TARGET_METHOD = Reflect.Method((Player t) => t.OnKill(default(DamageType)));
1313

1414
private static readonly MethodInfo SKIP_METHOD = Reflect.Method(() => GameModeUtils.IsPermadeath());
15+
1516
public static IEnumerable<CodeInstruction> Transpiler(MethodBase original, IEnumerable<CodeInstruction> instructions)
1617
{
1718
List<CodeInstruction> instructionList = instructions.ToList();

NitroxServer/Communication/Packets/Processors/PlayerJoiningMultiplayerSessionProcessor.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ public override void Process(PlayerJoiningMultiplayerSession packet, INitroxConn
8787
storyManager.GetTimeData(),
8888
isFirstPlayer,
8989
BuildingManager.GetEntitiesOperations(globalRootEntities),
90-
serverConfig.KeepInventoryOnDeath
90+
serverConfig.KeepInventoryOnDeath,
91+
serverConfig.MarkDeathPointsWithBeacon
9192
);
9293

9394
player.SendPacket(initialPlayerSync);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.IO;
2+
using NitroxModel.DataStructures.GameLogic;
3+
using NitroxModel.Packets;
4+
using NitroxModel.Serialization;
5+
using NitroxServer.ConsoleCommands.Abstract;
6+
using NitroxServer.ConsoleCommands.Abstract.Type;
7+
using NitroxServer.GameLogic;
8+
9+
namespace NitroxServer.ConsoleCommands;
10+
11+
internal class SetDeathMarkersCommand : Command
12+
{
13+
private readonly PlayerManager playerManager;
14+
private readonly SubnauticaServerConfig serverConfig;
15+
private readonly Server server;
16+
17+
public SetDeathMarkersCommand(PlayerManager playerManager, SubnauticaServerConfig serverConfig, Server server) : base("setdeathmarkers", Perms.ADMIN, "Sets \"Death Markers\" setting to on/off. If \"on\", a beacon will appear at the location where a player dies.")
18+
{
19+
this.playerManager = playerManager;
20+
this.serverConfig = serverConfig;
21+
this.server = server;
22+
AddParameter(new TypeBoolean("state", true, "the on/off state of if a death marker is spawned on death"));
23+
}
24+
25+
protected override void Execute(CallArgs args)
26+
{
27+
bool newDeathMarkersState = args.Get<bool>(0);
28+
using (serverConfig.Update(Path.Combine(KeyValueStore.Instance.GetSavesFolderDir(), server.Name)))
29+
{
30+
if (serverConfig.MarkDeathPointsWithBeacon != newDeathMarkersState)
31+
{
32+
serverConfig.MarkDeathPointsWithBeacon = newDeathMarkersState;
33+
playerManager.SendPacketToAllPlayers(new DeathMarkersChanged(newDeathMarkersState));
34+
SendMessageToAllPlayers($"MarkDeathPointsWithBeacon changed to \"{newDeathMarkersState}\" by {args.SenderName}");
35+
}
36+
else
37+
{
38+
SendMessage(args.Sender, $"MarkDeathPointsWithBeacon already set to {newDeathMarkersState}");
39+
}
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)