GE Lua Documentation

Press F to search!

filterParkingSpots

Definition


-- @/lua/ge/extensions/gameplay/parking.lua:196
local function filterParkingSpots(psList, filters) -- filter the sorted list of parking spots (as returned by findParkingSpots)
  if not psList or type(psList[1]) ~= "table" then return psList end
  filters = filters or defaultFilters

  local psCount = #psList
  local timeDay = 0

  if filters.useProbability then
    local timeObj = core_environment.getTimeOfDay()
    if timeObj and timeObj.time then
      timeDay = timeObj.time
    end
  end

  for i = psCount, 1, -1 do
    local ps = psList[i].ps
    local remove = false

    if filters.checkVehicles then -- strict but slow check for other vehicles occupying this spot
      if ps:hasAnyVehicles() then
        remove = true
      end
    end

    if filters.useProbability then
      local prob = ps.customFields:has("probability") and ps.customFields:get("probability") or vars.baseProbability
      if type(prob) ~= "number" then prob = 1 end
      prob = prob * vars.baseProbability

      local dayValue = 0.25 + math.abs(timeDay - 0.5) * 1.5 -- max 1 for midday, min 0.25 for midnight
      local timeDayCoef = dayValue

      if ps.customFields.tags.nightTime then
        local nightValue = 1 - math.abs(timeDay - 0.5) * 1.5 -- opposite of dayValue
        if ps.customFields.tags.dayTime then
          timeDayCoef = max(timeDayCoef, nightValue)
        else
          timeDayCoef = nightValue
        end
      end
      prob = prob * timeDayCoef

      if prob <= random() then
        remove = true
      end
    end

    if remove then
      table.remove(psList, i)
    end
  end

  if M.debugLevel > 0 then
    log("I", logTag, string.format("Filtered and accepted %d / %d parking spots", #psList, psCount))
  end

  return psList
end

Callers

@/lua/ge/extensions/gameplay/parking.lua

    psList = filterParkingSpots(psList, filters)
    psCount = #psList
    vehData.psList = findParkingSpots(vehData.aheadPos, 0, maxDist)
    vehData.psList = filterParkingSpots(vehData.psList, emptyFilters)
    vehData.focusPos:set(vehData.aheadPos)
    currParkingSpots = findParkingSpots(aheadPos, 0, areaRadius)
    currParkingSpots = filterParkingSpots(currParkingSpots)