Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2020
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.04 KB | None | 0 0
  1. ------------------------------------------------------------
  2. ------------------------------------------------------------
  3. ---- Author: Dylan 'Itokoyamato' Thuillier ----
  4. ---- ----
  5. ---- Email: itokoyamato@hotmail.fr ----
  6. ---- ----
  7. ---- Resource: tokovoip_script ----
  8. ---- ----
  9. ---- File: c_main.lua ----
  10. ------------------------------------------------------------
  11. ------------------------------------------------------------
  12.  
  13. --------------------------------------------------------------------------------
  14. -- Client: Voip data processed before sending it to TS3Plugin
  15. --------------------------------------------------------------------------------
  16.  
  17. local targetPed;
  18. local useLocalPed = true;
  19. local isRunning = false;
  20. local scriptVersion = "1.3.5";
  21. local animStates = {}
  22. local displayingPluginScreen = false;
  23. local HeadBone = 0x796e;
  24.  
  25. --------------------------------------------------------------------------------
  26. -- Plugin functions
  27. --------------------------------------------------------------------------------
  28.  
  29. -- Handles the talking state of other players to apply talking animation to them
  30. local function setPlayerTalkingState(player, playerServerId)
  31. local talking = tonumber(getPlayerData(playerServerId, "voip:talking"));
  32. if (animStates[playerServerId] == 0 and talking == 1) then
  33. PlayFacialAnim(GetPlayerPed(player), "mic_chatter", "mp_facial");
  34. elseif (animStates[playerServerId] == 1 and talking == 0) then
  35. PlayFacialAnim(GetPlayerPed(player), "mood_normal_1", "facials@gen_male@base");
  36. end
  37. animStates[playerServerId] = talking;
  38. end
  39.  
  40. RegisterNUICallback("updatePluginData", function(data)
  41. local payload = data.payload;
  42. if (voip[payload.key] == payload.data) then return end
  43. voip[payload.key] = payload.data;
  44. setPlayerData(voip.serverId, "voip:" .. payload.key, voip[payload.key], true);
  45. voip:updateConfig();
  46. voip:updateTokoVoipInfo(true);
  47. end);
  48.  
  49. -- Receives data from the TS plugin on microphone toggle
  50. RegisterNUICallback("setPlayerTalking", function(data)
  51. voip.talking = tonumber(data.state);
  52.  
  53. if (voip.talking == 1) then
  54. setPlayerData(voip.serverId, "voip:talking", 1, true);
  55. PlayFacialAnim(GetPlayerPed(PlayerId()), "mic_chatter", "mp_facial");
  56. else
  57. setPlayerData(voip.serverId, "voip:talking", 0, true);
  58. PlayFacialAnim(PlayerPedId(), "mood_normal_1", "facials@gen_male@base");
  59. end
  60. end)
  61.  
  62. local function clientProcessing()
  63. local playerList = voip.playerList;
  64. local usersdata = {};
  65. local localHeading;
  66. local ped = PlayerPedId()
  67.  
  68. if (voip.headingType == 1) then
  69. localHeading = math.rad(GetEntityHeading(ped));
  70. else
  71. localHeading = math.rad(GetGameplayCamRot().z % 360);
  72. end
  73. local localPos;
  74.  
  75. if useLocalPed then
  76. localPos = GetPedBoneCoords(ped, HeadBone);
  77. else
  78. localPos = GetPedBoneCoords(targetPed, HeadBone);
  79. end
  80.  
  81. for i=1, #playerList do
  82. local player = playerList[i];
  83. local playerServerId = GetPlayerServerId(player);
  84. if (GetPlayerPed(player) and voip.serverId ~= playerServerId) then
  85. local playerPos = GetPedBoneCoords(GetPlayerPed(player), HeadBone);
  86. local dist = #(localPos - playerPos);
  87.  
  88. if (not getPlayerData(playerServerId, "voip:mode")) then
  89. setPlayerData(playerServerId, "voip:mode", 1);
  90. end
  91.  
  92. -- Process the volume for proximity voip
  93. local mode = tonumber(getPlayerData(playerServerId, "voip:mode"));
  94. if (not mode or (mode ~= 1 and mode ~= 2 and mode ~= 3)) then mode = 1 end;
  95. local volume = -30 + (30 - dist / voip.distance[mode] * 30);
  96. if (volume >= 0) then
  97. volume = 0;
  98. end
  99. --
  100. local angleToTarget = localHeading - math.atan(playerPos.y - localPos.y, playerPos.x - localPos.x);
  101.  
  102. -- Set player's default data
  103. local tbl = {
  104. uuid = getPlayerData(playerServerId, "voip:pluginUUID"),
  105. volume = -30,
  106. muted = 1,
  107. radioEffect = false,
  108. posX = voip.plugin_data.enableStereoAudio and math.cos(angleToTarget) * dist or 0,
  109. posY = voip.plugin_data.enableStereoAudio and math.sin(angleToTarget) * dist or 0,
  110. posZ = voip.plugin_data.enableStereoAudio and playerPos.z or 0
  111. };
  112. --
  113.  
  114. -- Process proximity
  115. tbl.forceUnmuted = 0
  116. if (dist >= voip.distance[mode]) then
  117. tbl.muted = 1;
  118. else
  119. tbl.volume = volume;
  120. tbl.muted = 0;
  121. tbl.forceUnmuted = 1
  122. end
  123.  
  124. usersdata[#usersdata + 1] = tbl
  125. setPlayerTalkingState(player, playerServerId);
  126. end
  127. end
  128.  
  129. -- Process channels
  130. for _, channel in pairs(voip.myChannels) do
  131. for _, subscriber in pairs(channel.subscribers) do
  132. if (subscriber ~= voip.serverId) then
  133. local remotePlayerUsingRadio = getPlayerData(subscriber, "radio:talking");
  134. local remotePlayerChannel = getPlayerData(subscriber, "radio:channel");
  135. local remotePlayerUuid = getPlayerData(subscriber, "voip:pluginUUID");
  136.  
  137. local founduserData = nil
  138. for k, v in pairs(usersdata) do
  139. if(v.uuid == remotePlayerUuid) then
  140. founduserData = v
  141. end
  142. end
  143.  
  144. if not founduserData then
  145. founduserData = {
  146. uuid = getPlayerData(subscriber, "voip:pluginUUID"),
  147. radioEffect = false,
  148. resave = true,
  149. volume = 0,
  150. muted = 1
  151. }
  152. end
  153.  
  154.  
  155. if (remotePlayerChannel <= voip.config.radioClickMaxChannel) then
  156. founduserData.radioEffect = true;
  157. end
  158.  
  159. if(not remotePlayerUsingRadio or remotePlayerChannel ~= channel.id) then
  160. founduserData.radioEffect = false;
  161. if not founduserData.forceUnmuted then
  162. founduserData.muted = true;
  163. end
  164. else
  165. founduserData.muted = false
  166. founduserData.volume = 0;
  167. founduserData.posX = 0;
  168. founduserData.posY = 0;
  169. founduserData.posZ = voip.plugin_data.enableStereoAudio and localPos.z or 0;
  170. end
  171. if(founduserData.resave) then
  172. usersdata[#usersdata + 1] = founduserData
  173. end
  174. end
  175. end
  176. end
  177.  
  178. voip.plugin_data.Users = usersdata; -- Update TokoVoip's data
  179. voip.plugin_data.posX = 0;
  180. voip.plugin_data.posY = 0;
  181. voip.plugin_data.posZ = voip.plugin_data.enableStereoAudio and localPos.z or 0;
  182. end
  183.  
  184. RegisterNetEvent("initializeVoip");
  185. AddEventHandler("initializeVoip", function()
  186. if (isRunning) then return Citizen.Trace("TokoVOIP is already running\n"); end
  187. isRunning = true;
  188.  
  189. voip = TokoVoip:init(TokoVoipConfig); -- Initialize TokoVoip and set default settings
  190.  
  191. -- Variables used script-side
  192. voip.plugin_data.Users = {};
  193. voip.plugin_data.radioTalking = false;
  194. voip.plugin_data.radioChannel = -1;
  195. voip.plugin_data.localRadioClicks = false;
  196. voip.mode = 1;
  197. voip.talking = false;
  198. voip.pluginStatus = -1;
  199. voip.pluginVersion = "0";
  200. voip.serverId = GetPlayerServerId(PlayerId());
  201.  
  202. -- Radio channels
  203. voip.myChannels = {};
  204.  
  205. -- Player data shared on the network
  206. setPlayerData(voip.serverId, "voip:mode", voip.mode, true);
  207. setPlayerData(voip.serverId, "voip:talking", voip.talking, true);
  208. setPlayerData(voip.serverId, "radio:channel", voip.plugin_data.radioChannel, true);
  209. setPlayerData(voip.serverId, "radio:talking", voip.plugin_data.radioTalking, true);
  210. setPlayerData(voip.serverId, "voip:pluginStatus", voip.pluginStatus, true);
  211. setPlayerData(voip.serverId, "voip:pluginVersion", voip.pluginVersion, true);
  212. refreshAllPlayerData();
  213.  
  214. -- Set targetped (used for spectator mod for admins)
  215. targetPed = GetPlayerPed(-1);
  216.  
  217. voip.processFunction = clientProcessing; -- Link the processing function that will be looped
  218. voip:initialize(); -- Initialize the websocket and controls
  219. voip:loop(); -- Start TokoVoip's loop
  220.  
  221. Citizen.Trace("TokoVoip: Initialized script (" .. scriptVersion .. ")\n");
  222.  
  223. -- Request this stuff here only one time
  224. RequestAnimDict("mp_facial");
  225. RequestAnimDict("facials@gen_male@base");
  226.  
  227. -- Debug data stuff
  228. local debugData = false;
  229. Citizen.CreateThread(function()
  230. while true do
  231. Wait(5)
  232.  
  233. if (IsControlPressed(0, Keys["LEFTSHIFT"])) then
  234. if (IsControlJustPressed(1, Keys["9"]) or IsDisabledControlJustPressed(1, Keys["9"])) then
  235. debugData = not debugData;
  236. end
  237. end
  238.  
  239. if (debugData) then
  240. local pos_y;
  241. local pos_x;
  242. local players = GetActivePlayers();
  243.  
  244. for i = 1, #players do
  245. local player = players[i];
  246. local playerServerId = GetPlayerServerId(players[i]);
  247.  
  248. pos_y = 1.1 + (math.ceil(i/12) * 0.1);
  249. pos_x = 0.60 + ((i - (12 * math.floor(i/12)))/15);
  250.  
  251. drawTxt(pos_x, pos_y, 1.0, 1.0, 0.2, "[" .. playerServerId .. "] " .. GetPlayerName(player) .. "\nMode: " .. tostring(getPlayerData(playerServerId, "voip:mode")) .. "\nChannel: " .. tostring(getPlayerData(playerServerId, "radio:channel")) .. "\nRadioTalking: " .. tostring(getPlayerData(playerServerId, "radio:talking")) .. "\npluginStatus: " .. tostring(getPlayerData(playerServerId, "voip:pluginStatus")) .. "\npluginVersion: " .. tostring(getPlayerData(playerServerId, "voip:pluginVersion")) .. "\nTalking: " .. tostring(getPlayerData(playerServerId, "voip:talking")), 255, 255, 255, 255);
  252. end
  253. local i = 0;
  254. for channelIndex, channel in pairs(voip.myChannels) do
  255. i = i + 1;
  256. drawTxt(0.8 + i/12, 0.5, 1.0, 1.0, 0.2, channel.name .. "(" .. channelIndex .. ")", 255, 255, 255, 255);
  257. local j = 0;
  258. for _, player in pairs(channel.subscribers) do
  259. j = j + 1;
  260. drawTxt(0.8 + i/12, 0.5 + j/60, 1.0, 1.0, 0.2, player, 255, 255, 255, 255);
  261. end
  262. end
  263. end
  264. end
  265. end);
  266. end)
  267. --------------------------------------------------------------------------------
  268. -- Radio functions
  269. --------------------------------------------------------------------------------
  270.  
  271. function addPlayerToRadio(channel)
  272. TriggerServerEvent("TokoVoip:addPlayerToRadio", channel, voip.serverId);
  273. end
  274. RegisterNetEvent("TokoVoip:addPlayerToRadio");
  275. AddEventHandler("TokoVoip:addPlayerToRadio", addPlayerToRadio);
  276.  
  277. function removePlayerFromRadio(channel)
  278. TriggerServerEvent("TokoVoip:removePlayerFromRadio", channel, voip.serverId);
  279. end
  280. RegisterNetEvent("TokoVoip:removePlayerFromRadio");
  281. AddEventHandler("TokoVoip:removePlayerFromRadio", removePlayerFromRadio);
  282.  
  283. RegisterNetEvent("TokoVoip:onPlayerLeaveChannel");
  284. AddEventHandler("TokoVoip:onPlayerLeaveChannel", function(channelId, playerServerId)
  285. -- Local player left channel
  286. if (playerServerId == voip.serverId and voip.myChannels[channelId]) then
  287. local previousChannel = voip.plugin_data.radioChannel;
  288. voip.myChannels[channelId] = nil;
  289. if (voip.plugin_data.radioChannel == channelId) then -- If current radio channel is still removed channel, reset to first available channel or none
  290. if (tablelength(voip.myChannels) > 0) then
  291. for channelId, _ in pairs(voip.myChannels) do
  292. voip.plugin_data.radioChannel = channelId;
  293. break;
  294. end
  295. else
  296. voip.plugin_data.radioChannel = -1; -- No radio channel available
  297. end
  298. end
  299.  
  300. if (previousChannel ~= voip.plugin_data.radioChannel) then -- Update network data only if we actually changed radio channel
  301. setPlayerData(voip.serverId, "radio:channel", voip.plugin_data.radioChannel, true);
  302. end
  303.  
  304. -- Remote player left channel we are subscribed to
  305. elseif (voip.myChannels[channelId]) then
  306. voip.myChannels[channelId].subscribers[playerServerId] = nil;
  307. end
  308. end)
  309.  
  310. RegisterNetEvent("TokoVoip:onPlayerJoinChannel");
  311. AddEventHandler("TokoVoip:onPlayerJoinChannel", function(channelId, playerServerId, channelData)
  312. -- Local player joined channel
  313. if (playerServerId == voip.serverId and channelData) then
  314. local previousChannel = voip.plugin_data.radioChannel;
  315.  
  316. voip.plugin_data.radioChannel = channelData.id;
  317. voip.myChannels[channelData.id] = channelData;
  318.  
  319. if (previousChannel ~= voip.plugin_data.radioChannel) then -- Update network data only if we actually changed radio channel
  320. setPlayerData(voip.serverId, "radio:channel", voip.plugin_data.radioChannel, true);
  321. end
  322.  
  323. -- Remote player joined a channel we are subscribed to
  324. elseif (voip.myChannels[channelId]) then
  325. voip.myChannels[channelId].subscribers[playerServerId] = playerServerId;
  326. end
  327. end)
  328.  
  329. function isPlayerInChannel(channel)
  330. if (voip.myChannels[channel]) then
  331. return true;
  332. else
  333. return false;
  334. end
  335. end
  336.  
  337. --------------------------------------------------------------------------------
  338. -- Specific utils
  339. --------------------------------------------------------------------------------
  340.  
  341. -- Toggle the blocking screen with usage explanation
  342. -- Not used
  343. function displayPluginScreen(toggle)
  344. if (displayingPluginScreen ~= toggle) then
  345. SendNUIMessage(
  346. {
  347. type = "displayPluginScreen",
  348. data = toggle
  349. }
  350. );
  351. displayingPluginScreen = toggle;
  352. end
  353. end
  354.  
  355. -- Used for admin spectator feature
  356. AddEventHandler("updateVoipTargetPed", function(newTargetPed, useLocal)
  357. targetPed = newTargetPed
  358. useLocalPed = useLocal
  359. end)
  360.  
  361. -- Make exports available on first tick
  362. exports("addPlayerToRadio", addPlayerToRadio);
  363. exports("removePlayerFromRadio", removePlayerFromRadio);
  364. exports("isPlayerInChannel", isPlayerInChannel);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement