Advertisement
lowheartrate

default police_radar/radar.lua

May 5th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.79 KB | None | 0 0
  1. --[[
  2.     The DOJ inspired Radar system
  3.     By BlockBa5her
  4.     Released and protected under the GPL-3.0 License
  5. ]]
  6. -- Touch the below up until "the line"
  7. --[[
  8.     CONTROLS:
  9.     G to open
  10.     X to freeze
  11.     PGUP & PGDOWN to change fast speed
  12.     (simple as that really)
  13.     (also, must be in a vehicle for it to work)
  14. ]]
  15. local settings = {
  16.     minSpeed = 2.0, -- will not count vehicle if under this speed
  17.     drawDistance = 125.0, -- how far the radar will look for cars ahead of it
  18.     speedInKmh = false, -- If enabled, then it will display speed in KMH, otherwise, MPH
  19.     playSound = true, -- Plays a sound if the car in front go above the fast speed limit
  20.     speedInterval = 5.0 -- Amount that the fast speed changed when you change it in game
  21. }
  22. local whitelist = {
  23.     vehs = { -- make sure these are upper, and have quotes
  24.         "POLICE",
  25.         "POLICE2",
  26.         "POLICE3",
  27.         "POLICE4",
  28.         "SHERIFF",
  29.         "SHERIFF2",
  30.         "FBI",
  31.         "FBI2",
  32.         "RIOT",
  33.         "POLICEB",
  34.     },
  35.     enable = false -- if enabled, then will check if the vehicle that the player is driving is the same as one of above
  36. }
  37. --[[
  38.                    DON'T TOUCH BELOW THE LINE UNLESS YOU KNOW WHAT YOU ARE DOING!
  39.     -------------------------------------------------------------------------------------------------
  40. ]]
  41. -- fields n shit
  42. local radar =
  43. {
  44.     shown = false,
  45.     freeze = false,
  46.     patrolSpeed = 0.0,
  47.     fastSpeed = 70.0
  48. }
  49. local lastVeh =
  50. {
  51.     frwd = {
  52.         plate = "NULL",
  53.         model = "NULL",
  54.         speed = 0.0
  55.     },
  56.     bckwd = {
  57.         plate = "NULL",
  58.         model = "NULL",
  59.         speed = 0.0
  60.     }
  61. }
  62. local lastVehFast = {
  63.     plate = "NULL",
  64.     model = "NULL",
  65.     speed = 0.0
  66. }
  67.  
  68. -- common funcs that i need
  69. local function drawTxt(x,y ,width,height,scale, font, text, r,g,b,a)
  70.     SetTextFont(font)
  71.     SetTextProportional(0)
  72.     SetTextScale(scale, scale)
  73.     SetTextColour(r, g, b, a)
  74.     SetTextDropShadow()
  75.     SetTextOutline()
  76.     SetTextEntry("STRING")
  77.     AddTextComponentString(text)
  78.     DrawText(x - width/2, y - height/2 + 0.005)
  79. end
  80. local function drawTxt2(xy, wh, scale, font, text, rgba, align)
  81.     if align == 2 then
  82.         SetTextRightJustify(true)
  83.         SetTextWrap(0, xy[1])
  84.     end
  85.     if align == 1 then
  86.         SetTextCentre(true)
  87.     end
  88.     drawTxt(xy[1], xy[2], wh[1], wh[2], scale, font, text, rgba[1], rgba[2], rgba[3], rgba[4])
  89. end
  90. local function drawRect(xy, wh, rgba)
  91.     DrawRect(xy[1], xy[2], wh[1], wh[2], rgba[1], rgba[2], rgba[3], rgba[4])
  92. end
  93. local function DrawRaycast(entity)
  94.     local coords = GetOffsetFromEntityInWorldCoords(entity,0.0,1.0,0.3)
  95.     local coords2 = GetOffsetFromEntityInWorldCoords(entity, 0.0, settings.drawDistance,0.0)
  96.     local rayhandle = CastRayPointToPoint(coords, coords2, 10, entity, 0)
  97.     local _, _, _, _, entityHit = GetRaycastResult(rayhandle)
  98.     if entityHit>0 and IsEntityAVehicle(entityHit) then
  99.         return entityHit
  100.     else
  101.         return nil
  102.     end
  103. end
  104. local function DrawRaycastBackward(entity)
  105.     local coords = GetOffsetFromEntityInWorldCoords(entity,0.0,1.0,0.3)
  106.     local coords2 = GetOffsetFromEntityInWorldCoords(entity, 0.0, settings.drawDistance*-1,0.0)
  107.     local rayhandle = CastRayPointToPoint(coords, coords2, 10, entity, 0)
  108.     local _, _, _, _, entityHit = GetRaycastResult(rayhandle)
  109.     if entityHit>0 and IsEntityAVehicle(entityHit) then
  110.         return entityHit
  111.     else
  112.         return nil
  113.     end
  114. end
  115. local function has_value (tab, val)
  116.     for index, value in ipairs(tab) do
  117.         if value == val then
  118.             return true
  119.         end
  120.     end
  121.  
  122.     return false
  123. end
  124. local function getBoolText(bool)
  125.     if bool then
  126.         return "~g~True"
  127.     else
  128.         return "~r~False"
  129.     end
  130. end
  131. local function round(num, numDecimalPlaces)
  132.   local mult = 10^(numDecimalPlaces or 0)
  133.   return math.floor(num * mult + 0.5) / mult
  134. end
  135.  
  136. -- rendering the radar
  137. local function renderRadar()
  138.             local masterDraw = {0.75, 0.9175}
  139.  
  140.             local titleDraw = {masterDraw[1], masterDraw[2] - 0.0775}
  141.             local element1Draw = {masterDraw[1] - 0.175, masterDraw[2] - 0.0475}
  142.             local element2Draw = {masterDraw[1] - 0.0625, masterDraw[2] - 0.0475}
  143.             local element3Draw = {masterDraw[1] + 0.0625, masterDraw[2] - 0.0475}
  144.             local element4Draw = {masterDraw[1] + 0.175, masterDraw[2] - 0.0475}
  145.  
  146.             -- main box
  147.             drawRect({masterDraw[1],masterDraw[2]}, {0.5, 0.165}, {0,0,0,255})
  148.  
  149.             -- pause txt
  150.             if radar.freeze then
  151.                 drawTxt2({0.999,0.97}, {0.01, 0.01}, 0.45, 4, "PAUSED", {255,255,255,255}, 2)
  152.             end
  153.  
  154.             -- menu text
  155.             drawTxt2({titleDraw[1], titleDraw[2]}, {0.01, 0.01}, 0.45, 1, "NIC RADAR", {255,255,255,255}, 1)
  156.  
  157.             -- veh text
  158.             -- FRWD
  159.             drawTxt2({element1Draw[1], element1Draw[2]}, {0.01, 0.01}, 0.4, 0, "~b~FRWD", {255,255,255,255}, 1)
  160.             -- Speed TXT
  161.             drawTxt2({element1Draw[1], element1Draw[2] + 0.023}, {0.01, 0.01}, 0.7, 0, string.format("%s", tostring(round(lastVeh.frwd.speed, 2))), {255,255,255,255}, 1)
  162.             -- Plate Veh TXT
  163.             drawTxt2({element1Draw[1], element1Draw[2] + 0.076}, {0.01, 0.01}, 0.35, 1, string.format("~y~%s ~w~/ ~y~%s", lastVeh.frwd.plate, lastVeh.frwd.model), {255,255,255,255}, 1)
  164.  
  165.             -- veh text
  166.             -- BKWD
  167.             drawTxt2({element2Draw[1], element2Draw[2]}, {0.01, 0.01}, 0.4, 0, "~b~BCKWD", {255,255,255,255}, 1)
  168.             -- Speed TXT
  169.             drawTxt2({element2Draw[1], element2Draw[2] + 0.023}, {0.01, 0.01}, 0.7, 0, string.format("%s", tostring(round(lastVeh.bckwd.speed, 2))), {255,255,255,255}, 1)
  170.             -- Plate Veh TXT
  171.             drawTxt2({element2Draw[1], element2Draw[2] + 0.076}, {0.01, 0.01}, 0.35, 1, string.format("~y~%s ~w~/ ~y~%s", lastVeh.bckwd.plate, lastVeh.bckwd.model), {255,255,255,255}, 1)
  172.  
  173.             -- fast text
  174.             -- FAST
  175.             drawTxt2({element3Draw[1], element3Draw[2]}, {0.01, 0.01}, 0.4, 0, "~r~FAST", {255,255,255,255}, 1)
  176.             -- Speed TXT
  177.             drawTxt2({element3Draw[1], element3Draw[2] + 0.023}, {0.01, 0.01}, 0.7, 0, string.format("%s", tostring(round(lastVehFast.speed, 2))), {255,255,255,255}, 1)
  178.             -- Plate Veh TXT
  179.             drawTxt2({element3Draw[1], element3Draw[2] + 0.076}, {0.01, 0.01}, 0.35, 1, string.format("~y~%s ~w~/ ~y~%s", lastVehFast.plate, lastVehFast.model), {255,255,255,255}, 1)
  180.             -- Fast Speed Display
  181.             drawTxt2({element3Draw[1], element3Draw[2] + 0.1}, {0.01, 0.01}, 0.25, 0, string.format("~g~Fast Speed~w~: (~b~%s~w~)", radar.fastSpeed), {255,255,255,255}, 1)
  182.  
  183.             -- patrol text
  184.             -- PATROL
  185.             drawTxt2({element4Draw[1], element4Draw[2]}, {0.01, 0.01}, 0.4, 0, "~y~PATROL", {255,255,255,255}, 1)
  186.             -- Speed TXT
  187.             drawTxt2({element4Draw[1], element4Draw[2] + 0.023}, {0.01, 0.01}, 0.7, 0, string.format("%s", tostring(math.floor(radar.patrolSpeed))), {255,255,255,255}, 1)
  188. end
  189.  
  190. -- logic (the musician)
  191. Citizen.CreateThread(function()
  192.     while true do
  193.         Wait(0)
  194.  
  195.         local _whitelist = false
  196.         if whitelist.enable == true and IsPedInAnyVehicle(GetPlayerPed(-1), false) then
  197.             if has_value(whitelist.vehs, GetDisplayNameFromVehicleModel(GetEntityModel(GetVehiclePedIsIn(GetPlayerPed(-1), false)))) then
  198.                 _whitelist = true
  199.             end
  200.         end
  201.         if whitelist.enable == false and IsPedInAnyVehicle(GetPlayerPed(-1), false) then
  202.             _whitelist = true
  203.         end
  204.         if IsControlJustReleased(1, 47) and _whitelist then -- G
  205.             radar.shown = not radar.shown
  206.             radar.freeze = false
  207.             Wait(75)
  208.         end
  209.         if IsControlJustReleased(1, 73) and _whitelist then -- X
  210.             radar.freeze = not radar.freeze
  211.         end
  212.         if IsControlJustReleased(1, 10) and _whitelist then
  213.             radar.fastSpeed = radar.fastSpeed + settings.speedInterval
  214.         end
  215.         if IsControlJustReleased(1, 11) and _whitelist then
  216.             radar.fastSpeed = radar.fastSpeed - settings.speedInterval
  217.         end
  218.  
  219.         if radar.shown and _whitelist then
  220.  
  221.             if settings.speedInKmh then
  222.                 radar.patrolSpeed = GetEntitySpeed(GetVehiclePedIsIn(GetPlayerPed(-1), false)) * 3.6
  223.             else
  224.                 radar.patrolSpeed = GetEntitySpeed(GetVehiclePedIsIn(GetPlayerPed(-1), false)) * 2.236936
  225.             end
  226.  
  227.             if radar.freeze == false then
  228.                 local carM = DrawRaycast(GetPlayerPed(-1))
  229.                 local carM2 = DrawRaycastBackward(GetPlayerPed(-1))
  230.                
  231.                 --[[
  232.                     FORWARD RAYCAST STUFF
  233.                 ]]
  234.                 if carM ~= nil then
  235.                     local plate = GetVehicleNumberPlateText(carM)
  236.                     local vehSpeedKM = GetEntitySpeed(carM)*3.6
  237.                     local vehSpeedMph = GetEntitySpeed(carM)*2.236936
  238.                    
  239.                     if vehSpeedMph > settings.minSpeed then          
  240.                         lastVeh.frwd.plate = plate
  241.                         if settings.speedInKmh then
  242.                             lastVeh.frwd.speed = vehSpeedKM
  243.                         else
  244.                             lastVeh.frwd.speed = vehSpeedMph
  245.                         end
  246.                         lastVeh.frwd.model = string.upper(GetLabelText(GetDisplayNameFromVehicleModel(GetEntityModel(carM))))
  247.  
  248.                         if lastVeh.frwd.speed > radar.fastSpeed then
  249.                             if lastVehFast.plate ~= lastVeh.frwd.plate and settings.playSound then -- Prevents from playing sound over and over again
  250.                                 PlaySoundFrontend(-1, "TENNIS_POINT_WON", "HUD_AWARDS")
  251.                             end
  252.                             lastVehFast.plate = lastVeh.frwd.plate
  253.                             lastVehFast.model = lastVeh.frwd.model
  254.                             lastVehFast.speed = lastVeh.frwd.speed
  255.                         end
  256.                     end
  257.                 end
  258.  
  259.                 --[[
  260.                     BACKWARD RAYCAST STUFF
  261.                 ]]
  262.                 if carM2 ~= nil then
  263.                     local plate = GetVehicleNumberPlateText(carM2)
  264.                     local vehSpeedKM = GetEntitySpeed(carM2)*3.6
  265.                     local vehSpeedMph = GetEntitySpeed(carM2)*2.236936
  266.                    
  267.                     if vehSpeedMph > settings.minSpeed then          
  268.                         lastVeh.bckwd.plate = plate
  269.                         if settings.speedInKmh then
  270.                             lastVeh.bckwd.speed = vehSpeedKM
  271.                         else
  272.                             lastVeh.bckwd.speed = vehSpeedMph
  273.                         end
  274.                         lastVeh.bckwd.model = string.upper(GetLabelText(GetDisplayNameFromVehicleModel(GetEntityModel(carM2))))
  275.  
  276.                         if lastVeh.bckwd.speed > radar.fastSpeed then
  277.                             if lastVehFast.plate ~= lastVeh.bckwd.plate and settings.playSound then -- Prevents from playing sound over and over again
  278.                                 PlaySoundFrontend(-1, "TENNIS_POINT_WON", "HUD_AWARDS")
  279.                             end
  280.                             lastVehFast.plate = lastVeh.bckwd.plate
  281.                             lastVehFast.model = lastVeh.bckwd.model
  282.                             lastVehFast.speed = lastVeh.bckwd.speed
  283.                         end
  284.                     end
  285.                 end
  286.             end
  287.             renderRadar()
  288.         end
  289.     end  
  290. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement