GE Lua Documentation

Press F to search!

getRandomParkingSpots

Definition


-- @/lua/ge/extensions/gameplay/parking.lua:276

local function getRandomParkingSpots(originPos, minDist, maxDist, minCount, filters) -- returns a list of random parking spots, with a bias for origin position
  if not sites then return {} end
  minDist = minDist or 0
  maxDist = maxDist or 10000
  local radius = max(minDist, 100)
  local psList, psCount
  if not minCount or minCount <= 0 then
    minCount = math.huge
    radius = maxDist
  end

  repeat -- find enough parking spots in the area
    psList = findParkingSpots(originPos or core_camera.getPosition(), minDist, radius)
    if psList[1] then
      lastPos:set(psList[1].ps.pos)
    end

    psList = filterParkingSpots(psList, filters)
    psCount = #psList
    radius = radius * 2
  until psCount >= minCount or radius >= maxDist

  if minCount == math.huge then
    minCount = max(1, math.ceil(psCount / 4)) -- auto minimum count
  end
  if psCount < minCount then return {} end

  local finalPsList = {}
  local selected = {}
  local ratio = min(0.95, 1 - (minCount / psCount)) -- ratio of selectable parking spots
  local fallbackValue = 1

  repeat -- randomize which parking spots are selected, with a bias towards nearer parking spots
    for i, ps in ipairs(psList) do
      local minValue = min(fallbackValue, lerp(ratio, 1, square(i / psCount))) -- minValue is lower for nearer parking spots
      if not selected[ps.ps.name] and random() >= minValue then
        selected[ps.ps.name] = 1
        table.insert(finalPsList, ps)
      end
      if finalPsList[minCount] then break end
    end

    fallbackValue = fallbackValue / 2
  until finalPsList[minCount] -- repeat until the minimum number of parking spots is reached

  return finalPsList, psList
end

Callers

@/lua/ge/extensions/gameplay/parking.lua
  vehIds = vehIds or parkedVehIds
  local randomPsList, psList = getRandomParkingSpots(focus and focus.pos, minDist, maxDist, #vehIds)
  if not psList then return end
  local transforms
  local psList = getRandomParkingSpots(options.pos, nil, nil, amount, {checkVehicles = true})
  if psList[1] then
@/lua/ge/extensions/gameplay/city.lua

local function getRandomParkingSpots(minDist, startName) -- returns parking spot & zone names (start & destination) with a minimum distance
  -- startName is optional