Ejemplo n.º 1
0
 /// <summary>
 /// Send a voice update to a client
 /// </summary>
 /// <param name="targetId">target client</param>
 /// <param name="data">new voice data</param>
 public void SendUpdate(long targetId, VoiceLocationInformation data)
 {
     if (_connectedPeers.TryGetValue(targetId, out var peer))
     {
         if (peer.ConnectionState == ConnectionState.Connected)
         {
             // Logger.Debug($"SendUpdate {targetId} {data.Name} {data.Position}");
             SendTo(peer, new VoicePaketUpdate()
             {
                 PlayerName         = data.Name,
                 Position           = data.Position,
                 PositionIsRelative = data.IsRelative,
                 VolumeModifier     = data.VolumeModifier,
             }, DeliveryMethod.ReliableOrdered);
         }
     }
 }
Ejemplo n.º 2
0
        public void UpdateTeamspeakForUser(Client player, List <Client> allPlayers)
        {
            var playerPos     = player.position;
            var playerRot     = player.rotation;
            var rotation      = Math.PI / 180 * (playerRot.Z * -1);
            var playerVehicle = player.vehicle;
            var cId           = player.handle.Value;

            if (!player.hasData("VOICE_ID"))
            {
                if (player.hasData("VOICE_TIMEOUT") && (player.getData("VOICE_TIMEOUT") < DateTime.Now))
                {
                    API.triggerResourceEvent("PUREVOICE_CLIENT_NOVOICE", player);
                    player.kick("Please install the TS3 PureVoice Plugin");
                }
                return;
            }
            var targetId = player.getData("VOICE_ID");

            var playersIHear = new Dictionary <string, VoiceLocationInformation>();

            // Players near me
            var inRangePlayers = allPlayers.Where(cl => (cl != player) && (cl.position.DistanceTo2D(playerPos) <= VoiceMaxRange) && (cl.dimension == player.dimension) && cl.hasData("VOICE_TS_ID")).ToList();

            if (inRangePlayers != null)
            {
                foreach (var streamedPlayer in inRangePlayers)
                {
                    var n = GetTeamspeakID(streamedPlayer);
                    if (streamedPlayer == player)
                    {
                        continue;
                    }

                    var streamedPlayerPos = streamedPlayer.position;
                    var distance          = playerPos.DistanceTo(streamedPlayerPos);
                    var range             = 5; // VoiceRange of streamed player in meters, maybe make this a player-Data?
                    var volumeModifier    = 0f;

                    if (distance <= range)
                    {
                        if (Math.Abs(streamedPlayerPos.Z - playerPos.Z) > range)
                        {
                            continue;
                        }

                        var subPos = streamedPlayerPos.Subtract(playerPos);

                        var x = subPos.X * Math.Cos(rotation) - subPos.Y * Math.Sin(rotation);
                        var y = subPos.X * Math.Sin(rotation) + subPos.Y * Math.Cos(rotation);

                        if (distance > 2)
                        {
                            volumeModifier = (float)(distance / range) * -10f;
                        }
                        if (volumeModifier > 0)
                        {
                            volumeModifier = 0;
                        }
                        volumeModifier = Math.Min(10.0f, Math.Max(-10.0f, volumeModifier));
                        if (!playersIHear.ContainsKey(n))
                        {
                            playersIHear[n] = new VoiceLocationInformation(n, GetTeamspeakClientID(streamedPlayer));
                        }
                        playersIHear[n].Update(new TSVector((float)(Math.Round(x * 1000) / 1000), (float)(Math.Round(y * 1000) / 1000), 0), (float)(Math.Round(volumeModifier * 1000) / 1000), false);
                    }
                }
            }


            // Players in same vehicle are always near
            if (player.isInVehicle)
            {
                var others = player.vehicle.occupants.ToList();
                if (others != null)
                {
                    others.Remove(player);
                    others.ForEach(p =>
                    {
                        var tsId = GetTeamspeakID(p);
                        if (!playersIHear.ContainsKey(tsId))
                        {
                            playersIHear[tsId] = new VoiceLocationInformation(tsId, GetTeamspeakClientID(p));
                        }
                        playersIHear[tsId].Update(new TSVector(0, 0, 0), 4, true);
                    });
                }
            }

            PlayerHears[player.handle.Value] = playersIHear;

            _voiceServer.SendUpdate(targetId, playersIHear.Count > 0 ? playersIHear.Values.ToList() : new List <VoiceLocationInformation>());
        }