-- variables
local voicePlayers = {}
-- draw (voice reosource)
local g_screenX,g_screenY = guiGetScreenSize()
local BONE_ID = 8
local WORLD_OFFSET = 0.4
local ICON_PATH = "images/voice.png"
local ICON_WIDTH = 0.15*g_screenX
--
local font = dxCreateFont("font.ttf", 12)
local iconHalfWidth = ICON_WIDTH/2
local ICON_DIMENSIONS = 12
local ICON_LINE = 20
local ICON_TEXT_SHADOW = tocolor ( 0, 0, 0, 255 )
local range = 15
--Draw the voice image
addEventHandler ( "onClientRender", root,
function()
local index = 0
local sX, sY, sZ = getElementPosition(localPlayer)
for player in pairs(voicePlayers) do
local rX, rY, rZ = getElementPosition(player)
local distance = getDistanceBetweenPoints3D(sX, sY, sZ, rX, rY, rZ)
setSoundVolume( player, 30-distance )
local color = tocolor(255, 255, 255, 255)
if distance <= range then
dxDrawVoiceLabel ( player, index, color )
end
index = index + 1
while true do
--is he streamed in?
if not isElementStreamedIn(player) then
break
end
--is he on screen?
if not isElementOnScreen(player) then
break
end
local headX,headY,headZ = getPedBonePosition(player,BONE_ID)
headZ = headZ + WORLD_OFFSET
--is the head position on screen?
local absX,absY = getScreenFromWorldPosition ( headX,headY,headZ )
if not absX or not absY then
break
end
local camX,camY,camZ = getCameraMatrix()
--is there anything obstructing the icon?
if not isLineOfSightClear ( camX, camY, camZ, headX, headY, headZ, true, false, false, true, false, true, false, player ) then
break
end
dxDrawVoice ( absX, absY, color, getDistanceBetweenPoints3D(camX, camY, camZ, headX, headY, headZ) )
break
end
end
end
)
function dxDrawVoice ( posX, posY, color, distance )
if distance <= range then
distance = 1/distance
dxDrawImage ( posX - iconHalfWidth*distance, posY - iconHalfWidth*distance-75, ICON_WIDTH*distance, ICON_WIDTH*distance, ICON_PATH, 0, 0, 0, color, false )
end
end
function dxDrawVoiceLabel ( player, index, color )
local sx, sy = guiGetScreenSize ()
local scale = (sy / 800)
local spacing = ( ICON_LINE * scale )
local icon = ICON_DIMENSIONS * scale
local px, py = sx-icon, (sy * 0.6 + spacing * index)
if getPedOccupiedVehicle( localPlayer ) then
py = (sy * 0.6 - spacing * index)
end
dxDrawImage ( px-icon, py, icon*1.2, icon*1.2, ICON_PATH, 0, 0, 0, color, false )
px = px - spacing
local name = getPlayerName ( player ):gsub("#%x%x%x%x%x%x","")
local textWidth = dxGetTextWidth(name, 1.2, "default-bold")
dxDrawText ( name, px + 1 - textWidth, py + 1, px - textWidth, py, ICON_TEXT_SHADOW, 1, font )
dxDrawText ( name, px - textWidth, py, px - textWidth, py, color, 1, font )
end
-- events
addEventHandler("onClientPlayerVoiceStart",getRootElement(),function()
if getElementData (localPlayer, "player:mute" ) then
cancelEvent()
return
end
if getElementData (source, "mute:player" ) then
cancelEvent()
return
end
if getElementData (source, "voice:off" ) then
cancelEvent()
return
end
local localDimension = getElementDimension( localPlayer )
local localInterior = getElementInterior( localPlayer )
if source == localPlayer then
setElementData(source, "voice:mowi", true)
-- draw
voicePlayers[source] = true
-- data to server
local posX, posY, posZ = getElementPosition(source)
local nearbyPlayers = getElementsWithinRange(posX, posY, posZ, 30, "player")
-- senc data
--triggerServerEvent( "voicechat:setDestination", resourceRoot, nearbyPlayers )
else
local sourceDimension = getElementDimension( source )
local sourceInterior = getElementInterior( source )
if tonumber(localDimension) ~= tonumber(sourceDimension) or tonumber(localInterior) ~= tonumber(sourceInterior) then
cancelEvent()
return
end
local sX, sY, sZ = getElementPosition(localPlayer)
local rX, rY, rZ = getElementPosition(source)
local distance = getDistanceBetweenPoints3D(sX, sY, sZ, rX, rY, rZ)
if distance <= range then
voicePlayers[source] = true
else
cancelEvent()
end
end
end)
addEventHandler("onClientPlayerVoiceStop",getRootElement(),function()
if source == localPlayer then
setElementData(source, "voice:mowi", false)
end
voicePlayers[source] = nil
end)
addEventHandler("onClientPlayerQuit",getRootElement(),function()
voicePlayers[source] = nil
end)