From 7872f6453f6f95349dacf2bc07804f395b7020e9 Mon Sep 17 00:00:00 2001 From: Dan Date: Wed, 27 Nov 2019 20:41:46 +0000 Subject: [PATCH] JS switch/case, xmit light, speed display setting, reduced player vehicle check rate --- cl_radar.lua | 96 ++++++++++++++++++++++++++++++++++++-------------- cl_utils.lua | 10 +++--- config.lua | 2 +- nui/radar.css | 6 ++-- nui/radar.html | 12 +++---- nui/radar.js | 92 +++++++++++++++++++++++++++++++++-------------- 6 files changed, 150 insertions(+), 68 deletions(-) diff --git a/cl_radar.lua b/cl_radar.lua index a4b1eae..0d19d65 100644 --- a/cl_radar.lua +++ b/cl_radar.lua @@ -24,10 +24,19 @@ Citizen.SetTimeout( 1000, function() print( "WK_WARS2X: Sending resource name (" .. name .. ") to JavaScript side." ) -- Send a message through the NUI system to the JavaScript file to give the name of the resource - SendNUIMessage( { pathName = name } ) + SendNUIMessage( { _type = "updatePathName", pathName = name } ) end ) +--[[------------------------------------------------------------------------ + Player info variables +------------------------------------------------------------------------]]-- +PLY = {} +PLY.ped = PlayerPedId() +PLY.veh = nil +PLY.inDriverSeat = false + + --[[------------------------------------------------------------------------ Radar variables ------------------------------------------------------------------------]]-- @@ -202,7 +211,7 @@ end function RADAR:SetPatrolSpeed( speed ) if ( type( speed ) == "number" ) then - self.vars.patrolSpeed = speed + self.vars.patrolSpeed = self:GetVehSpeedFormatted( speed ) end end @@ -214,7 +223,7 @@ end function RADAR:SetActiveVehicles( t ) if ( type( t ) == "table" ) then - self.activeVehicles = t + self.activeVehicles = t end end @@ -530,9 +539,9 @@ end ------------------------------------------------------------------------]]-- function RADAR:GetVehSpeedFormatted( speed ) if ( self.vars.speedType == "mph" ) then - return math.ceil( speed * 2.236936 ) + return UTIL:Round( math.ceil( speed * 2.236936 ), 0 ) else - return math.ceil( speed * 3.6 ) + return UTIL:Round( math.ceil( speed * 3.6 ), 0 ) end end @@ -583,7 +592,7 @@ function RADAR:GetVehiclesForAntenna() -- Get the 'strongest' vehicle for the antenna for k, v in pairs( vehs[ant] ) do if ( self:CheckVehicleDataFitsMode( ant, v.rayType ) ) then - results[ant][1] = v + results[ant][1] = v break end end @@ -602,7 +611,7 @@ function RADAR:GetVehiclesForAntenna() end end - return { results["front"][1], results["front"][2], results["rear"][1], results["rear"][2] } + return { ["front"] = { results["front"][1], results["front"][2] }, ["rear"] = { results["rear"][1], results["rear"][2] } } end -- Num4 = 108 - INPUT_VEH_FLY_ROLL_LEFT_ONLY @@ -619,7 +628,7 @@ function RADAR:RunControlManager() end if ( IsDisabledControlJustPressed( 1, 166 ) ) then - SendNUIMessage( { activateRemote = true } ) + SendNUIMessage( { _type = "openRemote" } ) SetNuiFocus( true, true ) end @@ -655,25 +664,27 @@ RegisterNUICallback( "closeRemote", function( data ) end ) RegisterNUICallback( "setAntennaMode", function( data ) - RADAR:SetAntennaMode( data.value, tostring( data.mode ) ) + RADAR:SetAntennaMode( data.value, tonumber( data.mode ) ) print( "Set antenna: " .. data.value .. " to mode " .. tostring( data.mode ) ) end ) +RegisterNUICallback( "toggleAntenna", function( data ) + RADAR:ToggleAntenna( data.value ) + + SendNUIMessage( { _type = "antennaXmit", ant = data.value, on = RADAR:IsAntennaTransmitting( data.value ) } ) + + print( "Toggled " .. data.value .. " antenna" ) +end ) + --[[------------------------------------------------------------------------ Main function ------------------------------------------------------------------------]]-- function RADAR:Main() - -- Get the local player's ped and store it in a variable - local ped = PlayerPedId() - - -- Get the vehicle the player is sitting in - local plyVeh = GetVehiclePedIsIn( ped, false ) - -- Check to make sure the player is in the driver's seat, and also that the vehicle has a class of VC_EMERGENCY (18) - if ( DoesEntityExist( plyVeh ) and GetPedInVehicleSeat( plyVeh, -1 ) == ped and GetVehicleClass( plyVeh ) == 18 and self:IsPowerOn() ) then - local plyVehPos = GetEntityCoords( plyVeh ) + if ( DoesEntityExist( PLY.veh ) and PLY.inDriverSeat and GetVehicleClass( PLY.veh ) == 18 and self:IsPowerOn() ) then + local plyVehPos = GetEntityCoords( PLY.veh ) -- First stage of the radar - get all of the vehicles hit by the radar if ( self:GetRadarStage() == 0 --[[ and self:IsEitherAntennaOn() ]] ) then @@ -682,28 +693,59 @@ function RADAR:Main() self:ResetCapturedVehicles() self:ResetRayTraceState() - self:CreateRayThreads( plyVeh, vehs ) + self:CreateRayThreads( PLY.veh, vehs ) elseif ( self:GetRayTraceState() == self:GetNumOfRays() ) then self:IncreaseRadarStage() end elseif ( self:GetRadarStage() == 1 ) then - -- self:RemoveDuplicateCapturedVehicles() + local data = {} + + -- Get the player's vehicle speed + local speed = self:GetVehSpeedFormatted( GetEntitySpeed( PLY.veh ) ) + + data.patrolSpeed = UTIL:FormatSpeed( speed ) -- Only grab data to send if there have actually been vehicles captured by the radar if ( not UTIL:IsTableEmpty( self:GetCapturedVehicles() ) ) then local vehsForDisplay = self:GetVehiclesForAntenna() - -- self:SetActiveVehicles( vehsForDisplay ) -- not really any point in setting this + self:SetActiveVehicles( vehsForDisplay ) -- not really any point in setting this else - -- self:SetActiveVehicles( { nil, nil, nil, nil } ) + self:SetActiveVehicles( { ["front"] = { nil, nil }, ["rear"] = { nil, nil } } ) end + -- Work out what has to be sent + local av = self:GetActiveVehicles() + local test = { ["front"] = {}, ["rear"] = {} } + + if ( av["front"][1] ~= nil ) then + test["front"].speed = UTIL:FormatSpeed( self:GetVehSpeedFormatted( av["front"][1].speed ) ) + end + + if ( av["front"][2] ~= nil ) then + test["front"].fast = UTIL:FormatSpeed( self:GetVehSpeedFormatted( av["front"][2].speed ) ) + end + + -- Send the update to the NUI side + SendNUIMessage( { _type = "update", speed = data.patrolSpeed, antennas = test } ) + self:ResetRadarStage() self:ResetRayTraceState() end end end +-- Updates the local player information +Citizen.CreateThread( function() + while ( true ) do + PLY.ped = PlayerPedId() + PLY.veh = GetVehiclePedIsIn( PLY.ped, false ) + PLY.inDriverSeat = GetPedInVehicleSeat( PLY.veh, -1 ) == PLY.ped + + Citizen.Wait( 250 ) + end +end ) + -- Update the vehicle pool every 3 seconds Citizen.CreateThread( function() while ( true ) do @@ -722,7 +764,7 @@ Citizen.CreateThread( function() while ( true ) do RADAR:Main() - Citizen.Wait( 100 ) + Citizen.Wait( 50 ) end end ) @@ -739,9 +781,9 @@ end ) ------------------------------ DEBUG ------------------------------ -local types = { "FRONT", "FRONT FAST", "REAR", "REAR FAST" } +--[[ local types = { "FRONT", "FRONT FAST", "REAR", "REAR FAST" } ---[[ Citizen.CreateThread( function() +Citizen.CreateThread( function() while ( true ) do -- Caught veh debug printing local av = RADAR:GetActiveVehicles() @@ -756,9 +798,9 @@ local types = { "FRONT", "FRONT FAST", "REAR", "REAR FAST" } local speed = RADAR:GetVehSpeedFormatted( GetEntitySpeed( av[i].veh ) ) local veh = av[i].veh local rt = av[i].rayType - local dir = UTIL:IsEntityInMyHeading( GetEntityHeading( GetVehiclePedIsIn( PlayerPedId(), false ) ), GetEntityHeading( veh ), 100 ) - if ( av[i].relPos == -1 and dir ~= nil ) then dir = not dir end - if ( dir == true ) then dir = "Away" elseif ( dir == false ) then dir = "Closing" else dir = "nil" end + local dir = UTIL:GetEntityRelativeDirection( GetEntityHeading( GetVehiclePedIsIn( PlayerPedId(), false ) ), GetEntityHeading( veh ), 100 ) + + if ( dir == 1 ) then dir = "/\\" elseif ( dir == 2 ) then dir = "\\/" else dir = "none" end DrawMarker( 2, pos.x, pos.y, pos.z + 3, 0.0, 0.0, 0.0, 0.0, 180.0, 0.0, 1.0, 1.0, 1.0, 255, 255, 0, 70, false, true, 2, nil, nil, false ) diff --git a/cl_utils.lua b/cl_utils.lua index 2feeebf..b89fa02 100644 --- a/cl_utils.lua +++ b/cl_utils.lua @@ -66,7 +66,7 @@ function UTIL:OppositeAngle( ang ) return ( ang + 180 ) % 360 end -function UTIL:IsEntityInMyHeading( myAng, tarAng, angToCheck ) +function UTIL:GetEntityRelativeDirection( myAng, tarAng, angToCheck ) local rangeStartFront = myAng - ( angToCheck / 2 ) local rangeEndFront = myAng + ( angToCheck / 2 ) @@ -76,12 +76,12 @@ function UTIL:IsEntityInMyHeading( myAng, tarAng, angToCheck ) local rangeEndBack = opp + ( angToCheck / 2 ) if ( ( tarAng > rangeStartFront ) and ( tarAng < rangeEndFront ) ) then - return true + return 1 elseif ( ( tarAng > rangeStartBack ) and ( tarAng < rangeEndBack ) ) then - return false - else - return nil + return 2 end + + return 0 end function UTIL:Notify( text ) diff --git a/config.lua b/config.lua index 68ca66f..f05d07e 100644 --- a/config.lua +++ b/config.lua @@ -23,4 +23,4 @@ RADAR.config.reset_key = 244 RADAR.config.fast_lock_blips = true -- Debug mode -RADAR.config.debug_mode = true \ No newline at end of file +RADAR.config.debug_mode = false \ No newline at end of file diff --git a/nui/radar.css b/nui/radar.css index cf61330..c17a026 100644 --- a/nui/radar.css +++ b/nui/radar.css @@ -29,9 +29,9 @@ button:focus { outline: none; } position: absolute; margin: auto; /* top: 0; */ - right: 0; - bottom: 15px; - left: 0; + right: 10px; + bottom: 10px; + /* left: 0; */ background-image: url( "frame.png" ); } diff --git a/nui/radar.html b/nui/radar.html index 5080a16..6af84f8 100644 --- a/nui/radar.html +++ b/nui/radar.html @@ -119,31 +119,31 @@
- +
- +
- +
- +
- +
- +
diff --git a/nui/radar.js b/nui/radar.js index 53aa03d..3252a09 100644 --- a/nui/radar.js +++ b/nui/radar.js @@ -90,13 +90,14 @@ const remoteButtons = const antennaModes = { - same: 0, - opp: 1, - both: 2 + off: 0, + same: 1, + opp: 2, + both: 3 } // Hide the radar and remote, this way we can bypass setting a style of 'display: none;' in the HTML file -elements.radar.hide(); +// elements.radar.hide(); elements.remote.hide(); // Create the onclick event for the toggle display button @@ -109,7 +110,39 @@ function toggleRemote() elements.remote.toggle(); } -// function toggleLabel( ) +function setLight( ant, cat, item, state ) +{ + let obj = elements.antennas[ant][cat][item]; + + if ( state ) { + obj.addClass( "active" ); + } else { + obj.removeClass( "active" ); + } +} + +function setAntennaXmit( ant, state ) +{ + setLight( ant, "modes", "xmit", state ); + + if ( !state ) { + elements.antennas[ant].targetSpeed.html( "¦¦¦" ); + elements.antennas[ant].fast.speed.html( "HLd" ); + } +} + +function updateDisplays( ps, ants ) +{ + elements.patrolSpeed.html( ps ); + + if ( ants["front"].speed != null ) { + elements.antennas["front"].targetSpeed.html( ants["front"].speed ); + } + + if ( ants["front"].fast != null ) { + elements.antennas["front"].fast.speed.html( ants["front"].fast ); + } +} // This function is used to send data back through to the LUA side function sendData( name, data ) { @@ -121,20 +154,17 @@ function sendData( name, data ) { } // This runs when the JS file is loaded, loops through all of the remote buttons and assigns them an onclick function -function remoteInit() -{ - elements.remote.find( "button" ).each( function( i, obj ) { - if ( $( this ).attr( "data-action" ) && $( this ).attr( "data-value" ) ) { - $( this ).click( function() { - let action = $( this ).data( "action" ); - let value = $( this ).data( "value" ); - let mode = $( this ).data( "mode" ); +elements.remote.find( "button" ).each( function( i, obj ) { + if ( $( this ).attr( "data-nuitype" ) && $( this ).attr( "data-value" ) ) { + $( this ).click( function() { + let type = $( this ).data( "nuitype" ); + let value = $( this ).data( "value" ); + let mode = $( this ).attr( "data-mode" ) ? $( this ).data( "mode" ) : null; - sendData( action, { value, mode } ); - } ) - } - } ); -} + sendData( type, { value, mode } ); + } ) + } +} ); // Close the remote when the user presses the 'Escape' key document.onkeyup = function ( event ) { @@ -145,16 +175,26 @@ document.onkeyup = function ( event ) { } } -remoteInit(); - // The main event listener, this is what the NUI messages sent by the LUA side arrive at, they are // then handled properly via a switch/case that runs the relevant code window.addEventListener( "message", function( event ) { - var item = event.data; + var item = event.data; + var type = event.data._type; - if ( item.pathName ) { - resourceName = item.pathName; - } else if ( item.activateRemote ) { - toggleRemote(); - } + switch ( type ) { + case "updatePathName": + resourceName = item.pathName + break; + case "openRemote": + toggleRemote(); + break; + case "update": + updateDisplays( item.speed, item.antennas ); + break; + case "antennaXmit": + setAntennaXmit( item.ant, item.on ); + break; + default: + break; + } } ); \ No newline at end of file