Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync footstep audio #2177

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
d4e3ff2
Created packet and processors for footstep audio(not functional at al…
OhmV-IR Aug 28, 2024
7e0c624
prototype client-side footstep packet processing
OhmV-IR Aug 30, 2024
380d9ba
Switch to using player's AnimationController to find velocity
OhmV-IR Aug 30, 2024
e07a5c3
Footstep packets are forwarded from server to players within hearing …
OhmV-IR Sep 2, 2024
180bb35
Fixed FootstepPacket class to fit format(was crashing)
OhmV-IR Sep 2, 2024
26db96c
Removed duplicate var
OhmV-IR Sep 2, 2024
a772c16
cleanup
OhmV-IR Sep 2, 2024
eaf1122
Added transpiler to send packets when local client plays footstep sou…
OhmV-IR Sep 2, 2024
a879656
Lower volume of other players' footsteps(suggested by NinjaPedroX)
OhmV-IR Sep 2, 2024
8ef9842
Added same structure/submarine check for sending footstep packet to o…
OhmV-IR Sep 2, 2024
092af16
Removed logs
OhmV-IR Sep 7, 2024
c6814b3
Formatting changes
OhmV-IR Sep 7, 2024
737028e
Formatting changes
OhmV-IR Sep 7, 2024
103c14e
Newline changes
OhmV-IR Sep 18, 2024
5a5fc18
newline changes
OhmV-IR Sep 18, 2024
91936ec
Split long line into shorter lines
OhmV-IR Sep 18, 2024
6545583
Remove unneeded this keyword
OhmV-IR Sep 18, 2024
33d249c
Applying suggestions from Jannify's review
OhmV-IR Sep 18, 2024
8cc0956
Merge
OhmV-IR Sep 18, 2024
c1d0ef3
Added comment in packet processors to keep values updated, trying to …
OhmV-IR Sep 18, 2024
c4c851f
Pull sound range from .csv file(UNTESTED)
OhmV-IR Sep 18, 2024
756bece
Add whitespace
OhmV-IR Sep 24, 2024
30055b8
Add whitespace
OhmV-IR Sep 24, 2024
d26a186
Merged ifs and removed var type usage
OhmV-IR Sep 24, 2024
5c6f971
Merge branch 'sync-footstep-audio' of https://github.com/OhmV-IR/Nitr…
OhmV-IR Sep 24, 2024
38c6161
Merged else + if into else if
OhmV-IR Sep 24, 2024
9a5927b
Inverted if with early return condition
OhmV-IR Sep 24, 2024
38c1aa7
Compare subroot IDs using .Equals instead of 2 if statements
OhmV-IR Sep 26, 2024
6d4dcbf
Overriding .Equals in Optional<> for consistency and performance
OhmV-IR Sep 26, 2024
bef5717
Merged 2 if statements & fixed bad clause
OhmV-IR Sep 26, 2024
7e56f40
Fixed bug(was getting all players instead of only connected ones)
OhmV-IR Sep 28, 2024
76c9b07
Changed names of StepSound enum members
OhmV-IR Sep 30, 2024
2bd2d9d
removed unneeded this keyword
OhmV-IR Oct 1, 2024
7d81eaa
Reordered and cleaned usings
OhmV-IR Oct 1, 2024
5b17e1f
Did sort and clean usings with correct setting
OhmV-IR Oct 2, 2024
4d70262
added extra if check
OhmV-IR Oct 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Applying suggestions from Jannify's review
Co-authored-by: Jannify <23176718+Jannify@users.noreply.github.com>
  • Loading branch information
OhmV-IR committed Sep 18, 2024
commit 33d249c4ea84f2297dc7782bbb2a9e00d2441e3e
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class FootstepPacketProcessor : ClientPacketProcessor<FootstepPacket>
private readonly PlayerManager remotePlayerManager;
private readonly FootstepSounds localFootstepSounds;
private PARAMETER_ID fmodIndexSpeed = FMODUWE.invalidParameterId;
private readonly float footstepAudioRadius = 20f;
private readonly float footstepAudioMaxVolume = 0.5f;
private const float footstepAudioRadius = 20f;
private const float footstepAudioMaxVolume = 0.5f;

public FootstepPacketProcessor(PlayerManager remotePlayerManager)
{
Expand All @@ -24,42 +24,29 @@ public FootstepPacketProcessor(PlayerManager remotePlayerManager)

public override void Process(FootstepPacket packet)
{
var player = remotePlayerManager.Find(packet.playerID);
if (!player.HasValue)
var player = remotePlayerManager.Find(packet.PlayerID);
OhmV-IR marked this conversation as resolved.
Show resolved Hide resolved
if (player.HasValue)
{
return;
}
else
{
FMODAsset asset;
switch (packet.assetIndex)
FMODAsset asset = packet.AssetIndex switch
{
case FootstepPacket.StepSounds.PRECURSOR_STEP_SOUND:
asset = localFootstepSounds.precursorInteriorSound;
break;
case FootstepPacket.StepSounds.METAL_STEP_SOUND:
asset = localFootstepSounds.metalSound;
break;
case FootstepPacket.StepSounds.LAND_STEP_SOUND:
asset = localFootstepSounds.landSound;
break;
default:
asset = null;
break;
}
EventInstance @event = FMODUWE.GetEvent(asset);
if (@event.isValid())
FootstepPacket.StepSounds.PRECURSOR_STEP_SOUND => localFootstepSounds.precursorInteriorSound,
FootstepPacket.StepSounds.METAL_STEP_SOUND => localFootstepSounds.metalSound,
FootstepPacket.StepSounds.LAND_STEP_SOUND => localFootstepSounds.landSound,
_ => null
};
EventInstance evt = FMODUWE.GetEvent(asset);
if (evt.isValid())
{
if (FMODUWE.IsInvalidParameterId(fmodIndexSpeed))
{
fmodIndexSpeed = FMODUWE.GetEventInstanceParameterIndex(@event, "speed");
fmodIndexSpeed = FMODUWE.GetEventInstanceParameterIndex(evt, "speed");
}
ATTRIBUTES_3D attributes = player.Value.Body.To3DAttributes();
@event.set3DAttributes(attributes);
@event.setParameterValueByIndex(fmodIndexSpeed, player.Value.AnimationController.Velocity.magnitude);
@event.setVolume(FMODSystem.CalculateVolume(Player.mainObject.transform.position, player.Value.Body.transform.position, footstepAudioRadius, footstepAudioMaxVolume));
@event.start();
@event.release();
evt.set3DAttributes(attributes);
evt.setParameterValueByIndex(fmodIndexSpeed, player.Value.AnimationController.Velocity.magnitude);
evt.setVolume(FMODSystem.CalculateVolume(Player.mainObject.transform.position, player.Value.Body.transform.position, footstepAudioRadius, footstepAudioMaxVolume));
evt.start();
evt.release();
}
}
}
Expand Down
32 changes: 15 additions & 17 deletions NitroxModel/Packets/FootstepPacket.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
using System;

namespace NitroxModel.Packets
namespace NitroxModel.Packets;
[Serializable]
OhmV-IR marked this conversation as resolved.
Show resolved Hide resolved
public class FootstepPacket : Packet
{
[Serializable]
public class FootstepPacket : Packet
{
public ushort playerID { get; }
public StepSounds assetIndex { get; }
public ushort PlayerID { get; }
public StepSounds AssetIndex { get; }

public FootstepPacket(ushort playerID, StepSounds assetIndex)
{
this.playerID = playerID;
this.assetIndex = assetIndex;
}
public FootstepPacket(ushort playerID, StepSounds assetIndex)
{
this.PlayerID = playerID;
OhmV-IR marked this conversation as resolved.
Show resolved Hide resolved
this.AssetIndex = assetIndex;
}

public enum StepSounds : byte
{
PRECURSOR_STEP_SOUND,
METAL_STEP_SOUND,
LAND_STEP_SOUND
}
public enum StepSounds : byte
{
PRECURSOR_STEP_SOUND,
METAL_STEP_SOUND,
LAND_STEP_SOUND
}
OhmV-IR marked this conversation as resolved.
Show resolved Hide resolved
}
28 changes: 13 additions & 15 deletions NitroxPatcher/Patches/Dynamic/FootstepSounds_OnStep_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,24 @@ private static float CalculateVolume(float originalVolume, FootstepSounds instan
return soundData.Radius;
});

public static void SendFootstepPacket(FMODAsset asset)
private static void SendFootstepPacket(FMODAsset asset)
{
// Send footstep packet with the asset
var localPlayer = Resolve<LocalPlayer>();
if (localPlayer.PlayerId.HasValue)
if (Resolve<LocalPlayer>().PlayerId.HasValue)
{
FootstepPacket.StepSounds assetIndex;
if (asset.path == PRECURSOR_STEP_SOUND_PATH)
switch (asset.path)
{
assetIndex = FootstepPacket.StepSounds.PRECURSOR_STEP_SOUND;
case PRECURSOR_STEP_SOUND_PATH:
assetIndex = FootstepPacket.StepSounds.PRECURSOR_STEP_SOUND;
break;
case METAL_STEP_SOUND_PATH:
assetIndex = FootstepPacket.StepSounds.METAL_STEP_SOUND;
break;
default:
assetIndex = FootstepPacket.StepSounds.LAND_STEP_SOUND;
break;
}
else if (asset.path == METAL_STEP_SOUND_PATH)
{
assetIndex = FootstepPacket.StepSounds.METAL_STEP_SOUND;
}
else
{
assetIndex = FootstepPacket.StepSounds.LAND_STEP_SOUND;
}
var footstepPacket = new FootstepPacket(localPlayer.PlayerId.Value, assetIndex);
FootstepPacket footstepPacket = new(Resolve<LocalPlayer>().PlayerId.Value, assetIndex);
Resolve<IPacketSender>().Send(footstepPacket);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,32 @@
using NitroxServer.Communication.Packets.Processors.Abstract;
using NitroxServer.GameLogic;

namespace NitroxServer.Communication.Packets.Processors
namespace NitroxServer.Communication.Packets.Processors;
public class FootstepPacketProcessor : AuthenticatedPacketProcessor<FootstepPacket>
OhmV-IR marked this conversation as resolved.
Show resolved Hide resolved
{
public class FootstepPacketProcessor : AuthenticatedPacketProcessor<FootstepPacket>
{
private readonly float footstepAudioRange = 20f;
private readonly PlayerManager playerManager;
private readonly float footstepAudioRange = 20f;
private readonly PlayerManager playerManager;

public FootstepPacketProcessor(PlayerManager playerManager)
{
this.playerManager = playerManager;
}
public FootstepPacketProcessor(PlayerManager playerManager)
{
this.playerManager = playerManager;
}

public override void Process(FootstepPacket footstepPacket, Player sendingPlayer)
public override void Process(FootstepPacket footstepPacket, Player sendingPlayer)
{
foreach (Player player in playerManager.GetAllPlayers())
{
var players = playerManager.GetAllPlayers();
foreach (Player player in players)
if (NitroxVector3.Distance(player.Position, sendingPlayer.Position) <= footstepAudioRange && player != sendingPlayer)
{
if (sendingPlayer.SubRootId.HasValue)
// Forward footstep packet to players if they are within range to hear it and are in the same structure / submarine
if (sendingPlayer.SubRootId.HasValue && player.SubRootId.HasValue &&
sendingPlayer.SubRootId.Value == player.SubRootId.Value)
{
if (player.SubRootId.HasValue)
{
// If both players have id's, check if they are the same
if (NitroxVector3.Distance(player.Position, sendingPlayer.Position) <= footstepAudioRange && player != sendingPlayer && player.SubRootId.Value == sendingPlayer.SubRootId.Value)
{
// Forward footstep packet to players if they are within range to hear it and are in the same structure / submarine
player.SendPacket(footstepPacket);
}
}
// If one player has an id and the other doesn't, automatically false
player.SendPacket(footstepPacket);
}
else
else if (!sendingPlayer.SubRootId.HasValue && !player.SubRootId.HasValue)
{
// if both player's don't have SubRootIds
if (!player.SubRootId.HasValue)
{
if (NitroxVector3.Distance(player.Position, sendingPlayer.Position) <= footstepAudioRange && player != sendingPlayer)
{
// Forward footstep packet to players if they are within range to hear it and are in the same structure / submarine
player.SendPacket(footstepPacket);
}
}
// If one player doesn't have an id and other does, automatically false
player.SendPacket(footstepPacket);
}
}
}
Expand Down