Improved method for getting vehicles for display

This commit is contained in:
Dan
2019-11-10 17:28:21 +00:00
parent 13b013160c
commit e4e00557e6
2 changed files with 39 additions and 26 deletions

View File

@@ -343,15 +343,19 @@ function RADAR:GetFastestFrontAndRear()
local t = self.capturedVehicles local t = self.capturedVehicles
table.sort( t, self.sorting[2].func ) table.sort( t, self.sorting[2].func )
local vehs = { front = nil, rear = nil } local vehs = { ["front"] = nil, ["rear"] = nil }
for i = 1, -1, -2 do for i = 1, -1, -2 do
for k, v in pairs( t ) do for k, v in pairs( t ) do
if ( v.relPos == i ) then if ( v.relPos == i ) then
local speed = self:GetVehSpeedFormatted( v.speed )
if ( speed >= self:GetFastLimit() ) then
if ( i == 1 and self:IsAntennaOn( "front" ) ) then if ( i == 1 and self:IsAntennaOn( "front" ) ) then
vehs.front = v vehs["front"] = v
elseif ( i == -1 and self:IsAntennaOn( "rear" ) ) then elseif ( i == -1 and self:IsAntennaOn( "rear" ) ) then
vehs.rear = v vehs["rear"] = v
end
end end
break break
@@ -368,40 +372,38 @@ function RADAR:GetVehiclesForAntenna()
local fastVehs = self:GetFastestFrontAndRear() local fastVehs = self:GetFastestFrontAndRear()
-- Loop through and split up the vehicles based on front and rear -- Loop through and split up the vehicles based on front and rear
local vehs = { front = {}, rear = {} } local vehs = { ["front"] = {}, ["rear"] = {} }
for k, v in pairs( self.capturedVehicles ) do for k, v in pairs( self.capturedVehicles ) do
if ( v.relPos == 1 and self:IsAntennaOn( "front" ) ) then if ( v.relPos == 1 and self:IsAntennaOn( "front" ) ) then
table.insert( vehs.front, v ) table.insert( vehs["front"], v )
elseif ( v.relPos == -1 and self:IsAntennaOn( "rear" ) ) then elseif ( v.relPos == -1 and self:IsAntennaOn( "rear" ) ) then
table.insert( vehs.rear, v ) table.insert( vehs["rear"], v )
end end
end end
-- Sort the tables based on the radar mode -- Sort the tables based on the radar mode
table.sort( vehs.front, self:GetSortModeFunc() ) table.sort( vehs["front"], self:GetSortModeFunc() )
table.sort( vehs.rear, self:GetSortModeFunc() ) table.sort( vehs["rear"], self:GetSortModeFunc() )
-- Grab the vehicles for display -- Grab the vehicles for display
local frontVeh, rearVeh = nil, nil local normVehs = { ["front"] = nil, ["rear"] = nil }
if ( not UTIL:IsTableEmpty( vehs.front ) ) then for ant in UTIL:Values( { "front", "rear" } ) do
if ( vehs.front[1].veh ~= fastVehs.front.veh ) then if ( not UTIL:IsTableEmpty( vehs[ant] ) ) then
frontVeh = vehs.front[1] if ( fastVehs[ant] ~= nil ) then
if ( vehs[ant][1].veh ~= fastVehs[ant].veh ) then
normVehs[ant] = vehs[ant][1]
else else
frontVeh = vehs.front[2] normVehs[ant] = vehs[ant][2]
end
elseif ( fastVehs.front == nil ) then
normVehs[ant] = vehs[ant][1]
end
end end
end end
if ( not UTIL:IsTableEmpty( vehs.rear ) ) then return { normVehs["front"], fastVehs["front"], normVehs["rear"], fastVehs["rear"] }
if ( vehs.rear[1].veh ~= fastVehs.rear.veh ) then
rearVeh = vehs.rear[1]
else
rearVeh = vehs.rear[2]
end
end
return { frontVeh, fastVehs.front, rearVeh, fastVehs.rear }
end end
function RADAR:GetDynamicDataValue( key ) function RADAR:GetDynamicDataValue( key )

View File

@@ -37,6 +37,17 @@ function UTIL:IsTableEmpty( t )
return c == 0 return c == 0
end end
-- Credit to Deltanic for this function
function UTIL:Values( xs )
local i = 0
return function()
i = i + 1
return xs[i]
end
end
function UTIL:GetVehicleInDirection( entFrom, coordFrom, coordTo ) function UTIL:GetVehicleInDirection( entFrom, coordFrom, coordTo )
local rayHandle = StartShapeTestCapsule( coordFrom.x, coordFrom.y, coordFrom.z, coordTo.x, coordTo.y, coordTo.z, 20.0, 10, entFrom, 7 ) local rayHandle = StartShapeTestCapsule( coordFrom.x, coordFrom.y, coordFrom.z, coordTo.x, coordTo.y, coordTo.z, 20.0, 10, entFrom, 7 )
local _, hitEntity, endCoords, surfaceNormal, vehicle = GetShapeTestResult( rayHandle ) local _, hitEntity, endCoords, surfaceNormal, vehicle = GetShapeTestResult( rayHandle )