GE Lua Documentation

Press F to search!

findClosestRoad

Definition


-- @/lua/ge/map.lua:2507

-- this is also in vehicle/mapmgr.lua
-- find road (edge) closest to "position" and return the nodes ids (closestRoadNode1, closestRoadNode2) of that edge and distance to it.
local function findClosestRoad(pos, searchRadiusLim)
  -- searchRadiusLim: Optional

  if edgeKdTree == nil then return end
  searchRadiusLim = searchRadiusLim or 200
  local searchRadius = min(maxRadius, searchRadiusLim)

  local mapNodes = map.nodes
  local closestRoadNode1, closestRoadNode2, closestDist
  repeat
    closestDist = searchRadius * searchRadius
    for item_id in edgeKdTree:queryNotNested(pointBBox(pos.x, pos.y, searchRadius)) do
      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 < closestDist then
        closestDist = curDist
        closestRoadNode1 = n1id
        closestRoadNode2 = n2id
      end
    end
    searchRadius = searchRadius * 2
  until closestRoadNode1 or searchRadius > searchRadiusLim

  return closestRoadNode1, closestRoadNode2, sqrt(closestDist)
end

Callers

@/lua/vehicle/ai.lua
  if not currentRoute or internalState.changePlanTimer == 0 or currentRoute.plan.reRoute then
    local wp1, wp2 = mapmgr.findClosestRoad(ego.pos)
    if wp1 == nil or wp2 == nil then

  -- local wp1, wp2 = mapmgr.findClosestRoad(ego.pos)
  -- if (mapData.positions[wp2] - mapData.positions[wp1]):dot(ego.dirVec) > 0 then
    if not currentRoute or currentRoute.plan.reRoute or currentRoute.plan.planLen + getPathLen(currentRoute.path, currentRoute.plan[currentRoute.plan.planCount].pathidx) < getMinPlanLen() then
      local wp1, wp2 = mapmgr.findClosestRoad(ego.pos)
      if wp1 == nil or wp2 == nil then
      local positions = mapData.positions
      local wpAft, wpFore = mapmgr.findClosestRoad(ego.pos)
      if not (wpAft and wpFore) then
@/lua/ge/extensions/gameplay/race/path.lua
    local nodePos = vec3(self.pathnodes.objects[currentSegment.targetNode].pos)
    local name_a, name_b, distance = map.findClosestRoad(nodePos)
    if not name_a then -- no AI path due to no navgraph node
@/lua/ge/extensions/editor/trafficManager.lua
  if aiData.targetPos ~= nil then -- target position for AI mode
    local n1, n2 = map.findClosestRoad(aiData.targetPos)
    if n1 and n2 then
        local searchRadiusLim = 6.1  -- after road width assumption for 2 lanes split, each of 3.05m width; can be adjusted
        local n1, n2 = map.findClosestRoad(anchorPos, searchRadiusLim)
        if n1 and n2 then
        -- Find the closest road and use its direction for spawn orientation
        local n1, n2 = map.findClosestRoad(anchorPos)
        if n1 and n2 then
@/gameplay/missions/italy/arrive/012-Field/script.lua
    if self.riskTimer <= 0 then
      local n1, n2 = map.findClosestRoad(map.objects[self.vehId].pos)
@/lua/vehicle/mapmgr.lua
-- the same function is also located in ge/map.lua
local function findClosestRoad(pos, wZ)
  --log('A','mapmgr', 'findClosestRoad called with '..pos.x..','..pos.y..','..pos.z)
    --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)
@/lua/ge/extensions/gameplay/rally/geometry.lua
local function getRouteFromAtoB(startPos, targetPos)
  local startNode1, _ = map.findClosestRoad(startPos)
  local endNode1, _ = map.findClosestRoad(targetPos)
  local startNode1, _ = map.findClosestRoad(startPos)
  local endNode1, _ = map.findClosestRoad(targetPos)
  local path = map.getGraphpath():getPath(startNode1, endNode1)
@/lua/ge/extensions/gameplay/sites/location.lua
  if not map then return nil end
  local name_a,name_b,distance = map.findClosestRoad(vec3(self.pos))
  if not name_a or not name_b or not distance then return end
@/lua/ge/extensions/flowgraph/nodes/gameplay/rally/createRallyGroundMarker.lua
local function alignToNavgraph(pos)
  local n1, n2, dist = map.findClosestRoad(pos)
@/lua/ge/extensions/campaign/exploration.lua
  local pos = vec3(position)
  local first, second, distance = map.findClosestRoad(pos)
  local state = M.state
@/lua/ge/spawn.lua
      if not recoveryPoint then break end
      local n1Id, n2Id, dist = map.findClosestRoad(recoveryPoint.pos)
      if n1Id then
  if not pos then
    local n1Id, n2Id, dist = map.findClosestRoad(veh:getPosition())
    local n1 = mapData.nodes[n1Id]
@/lua/ge/extensions/flowgraph/nodes/util/closestRoad.lua
    pos:setFromTable(self.oldPos)
    local name_a, name_b, distance = map.findClosestRoad(pos)
@/lua/ge/extensions/flowgraph/nodes/util/routePointToPoint.lua
    pos:setFromTable(self.pinIn[v].value)
    local n1, n2 = map.findClosestRoad(pos)
    if n1 then
@/lua/ge/extensions/gameplay/traffic/vehicle.lua

  local n1, n2 = map.findClosestRoad(self.pos) -- may not be accurate at junctions
  local legalSide = mapRules.rightHandDrive and -1 or 1 -- negative if left side, positive if right side
@/lua/ge/extensions/core/multiSpawn.lua
  local lane = 1
  local n1, n2 = map.findClosestRoad(pos)
  local legalSide = map.getRoadRules().rightHandDrive and -1 or 1
@/lua/ge/extensions/gameplay/delivery/delivery.lua
  for _, pair in ipairs(positions) do
    local name_a,_,distance_a = map.findClosestRoad(pair[1])
    local name_b,_,distance_b = map.findClosestRoad(pair[2])
    local name_a,_,distance_a = map.findClosestRoad(pair[1])
    local name_b,_,distance_b = map.findClosestRoad(pair[2])
    if name_a and name_b then
@/lua/ge/extensions/gameplay/traffic.lua
    -- adjust global respawn distance based on road network density
    local n1, n2 = map.findClosestRoad(focus.pos)
    if n1 and n2 then
@/lua/ge/extensions/ui/uiNavi.lua

local function findClosestRoad (x, y, z)
  return map.findClosestRoad(vec3(x, y, z))
local function findClosestRoad (x, y, z)
  return map.findClosestRoad(vec3(x, y, z))
end

  local route = map.getPath(findClosestRoad(newPos), destination)
  if oldPos:distance(newPos) > 0.1 then

  local startPoint = findClosestRoad(x)
  local endPoint = findClosestRoad(y)
  local startPoint = findClosestRoad(x)
  local endPoint = findClosestRoad(y)
@/lua/ge/extensions/gameplay/traffic/trafficUtils.lua
    local dist
    mapNode1, mapNode2, dist = map.findClosestRoad(pos)
    if not mapNode1 or dist > 30 then

    local n1, n2 = map.findClosestRoad(spawnData.pos)
    if checkRoad(n1, n2, options) then

    local n1, n2 = map.findClosestRoad(spawnData.pos)
    if checkRoad(n1, n2, options) then

  local n1, n2 = map.findClosestRoad(startPos)
  if n1 then
@/lua/ge/extensions/scenario/busdriver.lua
    debugDrawer:drawTextAdvanced(vec3Destination, String('['..i..'] '..stop[2] .. ' / ' .. stop[1]), ColorF(0,0,0,1), true, false, ColorI(255, 255, 255, 255))
    local firstDest, secondDest, _ = map.findClosestRoad(vec3Destination)
    if not isRightNode(vec3Destination, firstDest, secondDest) then
  vec3Destination.z = vec3Destination.z-heightCorrection+proj.z
  local firstDest, secondDest, _ = map.findClosestRoad(vec3Destination)
@/lua/ge/extensions/gameplay/taxi.lua

  local closestNode1, closestNode2 = map.findClosestRoad(pos)
  if not (closestNode1 and closestNode2) then return nil end
@/lua/ge/extensions/core/funstuff.lua

  local name_a, name_b, distance = map.findClosestRoad(veh:getPosition())
  if not name_a or not name_b then return end
@/lua/ge/extensions/core/trafficSignals.lua
  pos = pos or self.pos
  local n1, n2, dist = map.findClosestRoad(pos)
  if not n1 then
@/lua/ge/extensions/editor/aiTests.lua
          local nodes = map.getMap().nodes
          local n1, n2 = map.findClosestRoad(veh.pos)
          local p1, p2 = nodes[n1].pos, nodes[n2].pos
@/lua/ge/extensions/core/hotlapping.lua
  local radius = 5
  local n1, n2, dist = map.findClosestRoad(pos)
  if n1 and n2 and dist <= 30 then
@/lua/ge/extensions/career/modules/delivery/generator.lua
local function distanceBetween(posA, posB)
  local name_a,_,distance_a = map.findClosestRoad(posA)
  local name_b,_,distance_b = map.findClosestRoad(posB)
  local name_a,_,distance_a = map.findClosestRoad(posA)
  local name_b,_,distance_b = map.findClosestRoad(posB)
  if not name_a or not name_b then return 1 end
@/lua/ge/map.lua
  -- build the graph and the tree
  maxRadius = 4 -- case there are no nodes in the map i.e. next(map.nodes) == nil avoids infinite loop in findClosestRoad()
  gp = graphpath.newGraphpath()
    --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