Client LuaCsForBarotrauma
BarotraumaClient/ClientSource/Networking/Client.cs
1 using Barotrauma.Sounds;
2 using Microsoft.Xna.Framework;
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 
7 namespace Barotrauma.Networking
8 {
9  partial class Client : IDisposable
10  {
12  {
13  get;
14  set;
15  }
16 
17  // Players can boost per-user volume by 200%
18  public const float MaxVoiceChatBoost = 2.0f;
19 
20  private float voiceVolume = 1f;
21 
22  public float VoiceVolume
23  {
24  get => voiceVolume;
25  set => voiceVolume = Math.Clamp(value, 0f, MaxVoiceChatBoost);
26  }
27 
28  private SoundChannel radioNoiseChannel;
29  private float radioNoise;
30 
31  public float RadioNoise
32  {
33  get { return radioNoise; }
34  set { radioNoise = MathHelper.Clamp(value, 0.0f, 1.0f); }
35  }
36 
37  private bool mutedLocally;
38  public bool MutedLocally
39  {
40  get { return mutedLocally; }
41  set
42  {
43  if (mutedLocally == value) { return; }
44  mutedLocally = value;
45 #if CLIENT
46  GameMain.NetLobbyScreen.SetPlayerVoiceIconState(this, muted, mutedLocally);
47  GameMain.GameSession?.CrewManager?.SetPlayerVoiceIconState(this, muted, mutedLocally);
48 #endif
49  }
50  }
51 
52  public bool IsOwner;
53 
54 
55  public bool IsDownloading;
56 
57  public float Karma;
58 
59  public bool AllowKicking =>
60  !IsOwner &&
64 
65  public void UpdateVoipSound()
66  {
67  if (VoipSound == null || !VoipSound.IsPlaying)
68  {
69  radioNoiseChannel?.Dispose();
70  radioNoiseChannel = null;
71  if (VoipSound != null)
72  {
73  DebugConsole.Log("Destroying voipsound");
75  }
76  VoipSound = null;
77  return;
78  }
79 
81  {
82  VoipSound.Gain = 0.0f;
83  }
84 
85  float gain = 1.0f;
86  float noiseGain = 0.0f;
87  Vector3? position = null;
88  if (character != null && !character.IsDead)
89  {
90  if (GameSettings.CurrentConfig.Audio.UseDirectionalVoiceChat)
91  {
92  position = new Vector3(character.WorldPosition.X, character.WorldPosition.Y, 0.0f);
93  }
94  else
95  {
96  float dist = Vector3.Distance(new Vector3(character.WorldPosition, 0.0f), GameMain.SoundManager.ListenerPosition);
97  gain = 1.0f - MathUtils.InverseLerp(VoipSound.Near, VoipSound.Far, dist);
98  }
99  if (!VoipSound.UsingRadio)
100  {
101  //emulate the "garbling" of the text chat
102  //this in a sense means the volume diminishes exponentially when close to the maximum range of the sound
103  //(diminished by both the garbling and the distance attenuation)
104 
105  //which is good, because we want the voice chat to become unintelligible close to the max range,
106  //and we need to heavily reduce the volume to do that (otherwise it's just quiet, but still intelligible)
107  float garbleAmount = ChatMessage.GetGarbleAmount(Character.Controlled, character, ChatMessage.SpeakRangeVOIP);
108  gain *= 1.0f - garbleAmount;
109  }
110  if (RadioNoise > 0.0f)
111  {
112  noiseGain = gain * RadioNoise;
113  gain *= 1.0f - RadioNoise;
114  }
115  }
116  VoipSound.SetPosition(position);
117  VoipSound.Gain = gain;
118  if (noiseGain > 0.0f)
119  {
120  if (radioNoiseChannel == null || !radioNoiseChannel.IsPlaying)
121  {
122  radioNoiseChannel = SoundPlayer.PlaySound("radiostatic");
123  radioNoiseChannel.Category = "voip";
124  radioNoiseChannel.Looping = true;
125  }
126  radioNoiseChannel.Near = VoipSound.Near;
127  radioNoiseChannel.Far = VoipSound.Far;
128  radioNoiseChannel.Position = position;
129  radioNoiseChannel.Gain = noiseGain;
130  }
131  else if (radioNoiseChannel != null)
132  {
133  radioNoiseChannel.Gain = 0.0f;
134  }
135  }
136 
137  partial void InitProjSpecific()
138  {
139  VoipQueue = null; VoipSound = null;
140  if (SessionId == GameMain.Client.SessionId) { return; }
141  VoipQueue = new VoipQueue(SessionId, canSend: false, canReceive: true);
143  VoipSound = null;
144  }
145 
146  public void SetPermissions(ClientPermissions permissions, IEnumerable<Identifier> permittedConsoleCommands)
147  {
148  List<DebugConsole.Command> permittedCommands = new List<DebugConsole.Command>();
149  foreach (Identifier commandName in permittedConsoleCommands)
150  {
151  var consoleCommand = DebugConsole.Commands.Find(c => c.Names.Contains(commandName));
152  if (consoleCommand != null)
153  {
154  permittedCommands.Add(consoleCommand);
155  }
156  }
157  SetPermissions(permissions, permittedCommands);
158  }
159 
160  public void SetPermissions(ClientPermissions permissions, IEnumerable<DebugConsole.Command> permittedConsoleCommands)
161  {
162  if (GameMain.Client == null)
163  {
164  return;
165  }
166  Permissions = permissions;
167  PermittedConsoleCommands.Clear();
168  foreach (var command in permittedConsoleCommands)
169  {
170  PermittedConsoleCommands.Add(command);
171  }
172  }
173 
174  public void GivePermission(ClientPermissions permission)
175  {
176  if (GameMain.Client == null || !GameMain.Client.HasPermission(ClientPermissions.ManagePermissions))
177  {
178  return;
179  }
180  if (!Permissions.HasFlag(permission)) { Permissions |= permission; }
181  }
182 
183  public void RemovePermission(ClientPermissions permission)
184  {
185  if (GameMain.Client == null || !GameMain.Client.HasPermission(ClientPermissions.ManagePermissions))
186  {
187  return;
188  }
189  if (Permissions.HasFlag(permission)) { Permissions &= ~permission; }
190  }
191 
192  public bool HasPermission(ClientPermissions permission)
193  {
194  if (GameMain.Client == null)
195  {
196  return false;
197  }
198 
199  return Permissions.HasFlag(permission);
200  }
201 
202  public void ResetVotes()
203  {
204  for (int i = 0; i < votes.Length; i++)
205  {
206  votes[i] = null;
207  }
208  }
209 
210  partial void DisposeProjSpecific()
211  {
212  if (VoipQueue != null)
213  {
215  }
216  if (VoipSound != null)
217  {
218  VoipSound.Dispose();
219  VoipSound = null;
220  }
221  if (radioNoiseChannel != null)
222  {
223  radioNoiseChannel.Dispose();
224  radioNoiseChannel = null;
225  }
226  }
227  }
228 }
void SetPlayerVoiceIconState(Client client, bool muted, bool mutedLocally)
virtual Vector2 WorldPosition
Definition: Entity.cs:49
static GameSession?? GameSession
Definition: GameMain.cs:88
static NetLobbyScreen NetLobbyScreen
Definition: GameMain.cs:55
static GameClient Client
Definition: GameMain.cs:188
static Sounds.SoundManager SoundManager
Definition: GameMain.cs:80
void SetPlayerVoiceIconState(Client client, bool muted, bool mutedLocally)
const float SpeakRangeVOIP
This is shorter than the text chat speak range, because the voice chat is still intelligible (just qu...
static float GetGarbleAmount(Entity listener, Entity sender, float range, float obstructionMultiplier=2.0f)
How much messages sent by sender should get garbled. Takes the distance between the entities and opt...
readonly HashSet< DebugConsole.Command > PermittedConsoleCommands
void SetPermissions(ClientPermissions permissions, IEnumerable< DebugConsole.Command > permittedConsoleCommands)
void SetPermissions(ClientPermissions permissions, IEnumerable< Identifier > permittedConsoleCommands)
readonly byte SessionId
An ID for this client for the current session. THIS IS NOT A PERSISTENT VALUE. DO NOT STORE THIS LONG...
bool HasPermission(ClientPermissions permission)
Definition: GameClient.cs:2620
void RegisterQueue(VoipQueue queue)
Definition: VoipClient.cs:36
void UnregisterQueue(VoipQueue queue)
Definition: VoipClient.cs:42
override void Dispose()
Definition: VoipSound.cs:177
void SetPosition(Vector3? pos)
Definition: VoipSound.cs:94