GE Lua Documentation

Press F to search!

updateArrows

Definition


-- @/lua/ge/extensions/core/groundMarkerArrows.lua:121

--local lastWpLog = {}

local function updateArrows(path, pathLength)
  --log('I', 'arrow', 'Updating arrows')
  local usedWpIds = {}
  local maxArrowDistance = 180 -- Only show arrows up to 180m in front

  --table.clear(lastWpLog)

  for i = 1, #path - 1 do
    --local wpLog = {pos = path[i].pos}
    --wpLog.distToTarget = path[i].distToTarget
    local dirNextPoint = (path[i+1].pos - path[i].pos)
    dirNextPoint:normalize()
    if i > 1 then
      local dirPrevPoint = (path[i].pos - path[i-1].pos)
      dirPrevPoint:normalize()
      --wpLog.dirPrevPoint = dirPrevPoint

      -- Calculate distance from vehicle to this point
      local distToVehicle = pathLength - path[i].distToTarget
      --wpLog.distToVehicle = distToVehicle
      if distToVehicle > maxArrowDistance then
        break -- Stop processing points beyond max distance
      end

      --wpLog.nodeToNodeAngle = math.acos(dirPrevPoint:cosAngle(dirNextPoint)) * 180/math.pi
      --wpLog.wp = path[i].wp
      --wpLog.linkCount = path[i].linkCount
      if (path[i].linkCount and path[i].linkCount > 2) and path[i].wp then
        local nodeToNodeAngle = math.acos(dirPrevPoint:cosAngle(dirNextPoint)) * 180/math.pi

        -- Check if the route has the smallest angle of any possible path of the intersection
        local routeHasSmallestAngle = true
        if nodeToNodeAngle <= 25 then
          for wpId, edgeInfo in pairs(map.getGraphpath().graph[path[i].wp]) do
            if path[i-1].wp ~= wpId then
              local connectedNodePos = map.getMap().nodes[wpId].pos
              local dirConnectedNode = (connectedNodePos - path[i].pos); dirConnectedNode:normalize()
              local connectedNodeAngle = math.acos(dirPrevPoint:cosAngle(dirConnectedNode)) * 180/math.pi
              if nodeToNodeAngle > connectedNodeAngle then
                routeHasSmallestAngle = false
                break
              end
            end
          end
        end
        --wpLog.routeHasSmallestAngle = routeHasSmallestAngle
        if nodeToNodeAngle > 25 or not routeHasSmallestAngle then
          usedWpIds[path[i].wp] = true
          local existingArrowId = wpToArrowId[path[i].wp]

          if existingArrowId then
            setSmootherTargets(existingArrowId, distToVehicle)
          else
            -- Create new arrow
            local arrow = getUnusedArrow()
            if arrow then
              local id = arrow:getId()
              --log('I', 'arrow', 'Creating new arrow ' .. tostring(id) .. ' at wp ' .. tostring(path[i].wp))

              wpToArrowId[path[i].wp] = id
              arrowToWp[id] = path[i].wp

              local arrowColor = core_groundMarkers.floatingArrowColor
              arrow:setField('instanceColor', 0, ""..arrowColor[1].." "..arrowColor[2].." "..arrowColor[3].." 1")
              arrow:setField('instanceColor1', 0, ""..arrowColor[1].." "..arrowColor[2].." "..arrowColor[3].." 1")

              local pos = path[i].pos + arrowHeight
              local rot = quatFromDir(dirNextPoint, upVec)
              -- Update proxy
              arrowProxies[id].pos = pos
              arrowProxies[id].rot = rot
              arrowProxies[id].state = "visible"
              arrowProxies[id].wp = path[i].wp
              setSmootherTargets(id, distToVehicle)
              arrowProxies[id].nudgeForwardSmoother:set(arrowProxies[id].nudgeForwardSmootherTarget)
              arrowProxies[id].nearFarScaleSmoother:set(arrowProxies[id].nearFarScaleSmootherTarget)
              arrowProxies[id].alphaSmoother:set(0)
            else
              log('W', 'arrow', 'Failed to get unused arrow for wp ' .. tostring(path[i].wp))
            end
          end
        end
      end
    end
    --table.insert(lastWpLog, wpLog)
  end

  if path[2] then
    if getPlayerVehicle(0) then
      local wrongDirection = getPlayerVehicle(0):getVelocity():dot(path[2].pos - getPlayerVehicle(0):getPosition()) < 0
      if wrongDirection then
        wrongDirectionCounter = wrongDirectionCounter + 1
      else
        wrongDirectionCounter = wrongDirectionCounter - 1
      end

      wrongDirectionCounter = clamp(wrongDirectionCounter, -20, 20)
    else
      wrongDirectionCounter = -20
    end
  end
  if wrongDirectionCounter > 0 then
    ui_message("Please make a U-turn when possible", 5, 'wrongDirection', 'turnHp')
  else
    ui_message("", 5, 'wrongDirection', 'turnHp')
  end

  for id, proxy in pairs(arrowProxies) do
    if not usedWpIds[proxy.wp] then
      if proxy.state == "visible" then
        proxy.state = "fadeout"
        proxy.alphaSmootherTarget = -0.2
        arrowToWp[id] = nil
        wpToArrowId[proxy.wp] = nil
      end
    end
  end
end

Callers

@/lua/ge/extensions/core/groundMarkers.lua
    -- Update arrows using the new module
    core_groundMarkerArrows.updateArrows(path, totalDist)
  end