GE Lua Documentation

Press F to search!

calculatePathStats

Definition


-- @/lua/ge/extensions/gameplay/crawl/utils.lua:1217

local function calculatePathStats(pathId, pathReversed)
  if not pathId then
    return nil
  end

  if pathStatsCache[pathId] then
    return pathStatsCache[pathId]
  end

  local path = gameplay_crawl_saveSystem.getPathById(pathId)
  if not path or not path.nodes or #path.nodes < 2 then
    return nil
  end

  local totalDistance = 0
  local totalElevationChange = 0
  local stepDistance = 5.0

  local pathnodes = path.nodes
  local pathPositions = {}

  for i, pn in ipairs(pathnodes) do
    if pathReversed then
      i = #pathnodes - i + 1
    end
    if pn.pos then
      table.insert(pathPositions, pn.pos)
    end
  end

  if #pathPositions < 2 then
    return nil
  end

  for i = 1, #pathPositions - 1 do
    local currentPos = pathPositions[i]
    local nextPos = pathPositions[i + 1]

    if currentPos and nextPos then
      local segmentLength = currentPos:distance(nextPos)
      local elevationChange = nextPos.z - currentPos.z

      local numSteps = math.max(1, math.floor(segmentLength / stepDistance))
      local stepSize = segmentLength / numSteps

      for step = 1, numSteps do
        totalDistance = totalDistance + stepSize
        totalElevationChange = totalElevationChange + (elevationChange / numSteps)
      end
    end
  end

  local stats = {
    totalDistance = totalDistance,
    totalElevationChange = totalElevationChange,
    elevationGain = math.max(0, totalElevationChange),
    elevationLoss = math.abs(math.min(0, totalElevationChange))
  }

  pathStatsCache[pathId] = stats


  return stats
end

Callers

@/lua/ge/extensions/gameplay/crawl/general.lua
      if trail.pathId then
        pathStats = gameplay_crawl_utils.calculatePathStats(trail.pathId, trail.pathReversed)
      end