VE Lua Documentation

Press F to search!

findBestRoad

Definition


-- @/lua/vehicle/mapmgr.lua:257

local function findBestRoad(pos, dir, wZ)
  -- searches for best road with respect to position and direction, with a fallback to the generic findClosestRoad function
  local graph = mapData.graph
  pos = pos or obj:getPosition()
  dir = dir or obj:getDirectionVector()

  local nodePositions, nodeRadius = mapData.positions, mapData.radius
  local currRoads, currRoadsCount = {}, 0

  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 = sqDistToLineSegmentZBias(pos, nodePositions[n1id], nodePositions[n2id], wZ)

    if curDist <= square(math.max(nodeRadius[n1id], nodeRadius[n2id])) then
      local xnorm = pos:xnormOnLine(nodePositions[n1id], nodePositions[n2id])
      if xnorm >= 0 and xnorm <= 1 then -- insert result if it is within road boundaries
        currRoadsCount = currRoadsCount + 3
        currRoads[currRoadsCount-2] = n1id
        currRoads[currRoadsCount-1] = n2id
        currRoads[currRoadsCount] = curDist
      end
    end
  end

  if currRoadsCount == 0 then
    --log('W', 'mapmgr', 'no results for findBestRoad, now using findClosestRoad')
    return findClosestRoad(pos, wZ) -- fallback
  elseif currRoadsCount == 3 then -- only one entry in the table (in terms of full edge informations, hence 3 elements stored)
    return currRoads[1], currRoads[2], math.sqrt(currRoads[3])
  end

  local bestScore, bestIdx = -math.huge, nil
  for i = 1, currRoadsCount, 3 do
    local edge = graph[currRoads[i]][currRoads[i+1]]
    local score = push3(dir):dot((push3(nodePositions[edge.outNode]) - push3(nodePositions[edge.inNode])):normalized())
    if not edge.oneWay then score = math.abs(score) end -- manage edges with multiple directions
    score = score - edge.gated -- add gated road penalty to penalize choosing a gated road
    if score >= bestScore then
      bestScore = score
      bestIdx = i
    end
  end

  return currRoads[bestIdx], currRoads[bestIdx+1], math.sqrt(currRoads[bestIdx+2])
end

Callers

@/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)
@/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/ge/map.lua

local function findBestRoad(pos, dir)
  -- searches for best road with respect to position and direction, with a fallback to the generic findClosestRoad function