findBestRoad
Definition
-- @/lua/ge/map.lua:2535
local function findBestRoad(pos, dir)
-- searches for best road with respect to position and direction, with a fallback to the generic findClosestRoad function
local mapNodes = map.nodes
local bestRoad1, bestRoad2, bestDist
local currRoads = {}
for item_id in edgeKdTree:queryNotNested(pointBBox(pos.x, pos.y, 20)) do -- assuming that no roads would have a radius greater than 20 m
local i = stringFind(item_id, '\0')
local n1id = stringSub(item_id, 1, i-1)
local n2id = stringSub(item_id, i+1, #item_id)
local curDist = pos:squaredDistanceToLineSegment(mapNodes[n1id].pos, mapNodes[n2id].pos)
if curDist <= square(math.max(mapNodes[n1id].radius, mapNodes[n2id].radius)) then
local xnorm = pos:xnormOnLine(mapNodes[n1id].pos, mapNodes[n2id].pos)
if xnorm >= 0 and xnorm <= 1 then -- insert result if it is within road boundaries
table.insert(currRoads, {n1id, n2id, curDist})
end
end
end
if not currRoads[1] then
--log('W', 'map', 'no results for findBestRoad, now using findClosestRoad')
return findClosestRoad(pos, wZ) -- fallback
elseif not currRoads[2] then -- only one entry in the table
return currRoads[1][1], currRoads[1][2], math.sqrt(currRoads[1][3])
-- need to return inNode to outNode
end
local bestDot = 0
for _, v in ipairs(currRoads) do
local dirDot = math.abs(dir:dot((mapNodes[v[1]].pos - mapNodes[v[2]].pos):normalized()))
if dirDot >= bestDot then -- best direction
bestDot = dirDot
bestRoad1, bestRoad2, bestDist = v[1], v[2], v[3]
end
end
return bestRoad1, bestRoad2, math.sqrt(bestDist)
end
Callers
@/lua/vehicle/controller/tech/roadsSensor.lua
local function getPointAhead(vpos, vfwd, pl)
local p_rearKey, p_frontKey, _ = mapmgr.findBestRoad(vpos, vfwd)
local p_front = coords[p_frontKey]
if aiSpeed > 2 then
p1Key, p2Key = mapmgr.findBestRoad(frontAxleMidpointProjGround, fwd)
else
@/lua/vehicle/mapmgr.lua
local function findBestRoad(pos, dir, wZ)
-- searches for best road with respect to position and direction, with a fallback to the generic findClosestRoad function
@/lua/vehicle/ai.lua
local wp1, wp2, dist1 = mapmgr.findBestRoad(ego.pos, ego.dirVec)
if wp1 == nil or wp2 == nil then
local plwp1, plwp2, dist2 = mapmgr.findBestRoad(player.pos, playerVel)
if plwp1 == nil or plwp2 == nil then
else
local wp1, wp2 = mapmgr.findBestRoad(ego.pos, ego.dirVec)