GE Lua Documentation

Press F to search!

processScenarioData

Definition


-- @/lua/ge/extensions/scenario/scenariosLoader.lua:38

local function processScenarioData(scenarioKey, scenarioData, scenarioFilename)
    scenarioData.scenarioKey = scenarioKey

    if scenarioFilename then
      scenarioData.sourceFile = scenarioFilename
      scenarioData.official = isOfficialContentVPath(string.sub(scenarioFilename, 0))
      scenarioData.levelName = string.gsub(scenarioFilename, "(.*/)(.*)/scenarios/(.*)%.json", "%2")
      scenarioData.map = "ui.common.unknown"

      -- add mod information
      local mod = extensions.core_modmanager.getModFromPath(scenarioFilename, true)
      if mod then
        scenarioData.modID = mod.modID
        scenarioData.modName = mod.modname
        if mod.modData and mod.modData.title then
          scenarioData.modTitle = mod.modData.title
        end
      else
        scenarioData.modID = nil
        scenarioData.modName = 'BeamNG'
        scenarioData.modTitle = 'BeamNG.drive'
      end
      if scenarioFilename ~= 'flowgraphEditor' and scenarioData.levelName ~= 'flowgraphEditor' then
        -- improve the data a little bit
        scenarioData.mission = path.getPathLevelMain(scenarioData.levelName)

        if not FS:fileExists(scenarioData.mission) then
          -- Fallback to old MIS file
          scenarioData.mission = '/levels/'..scenarioData.levelName..'/'..scenarioData.levelName..'.mis'
        end

        if not FS:fileExists(scenarioData.mission) then
          -- Fallback to level directory
          scenarioData.mission = '/levels/'..scenarioData.levelName..'/'
          if not FS:directoryExists(scenarioData.mission) then log('E', logTag, scenarioData.levelName.." scenario file not found") end
        end
      end
      scenarioData.scenarioName = scenarioData.scenarioName or string.gsub(scenarioFilename, "(.*/)(.*)%.json", "%2")
      scenarioData.directory = string.gsub(scenarioFilename, "(.*)/(.*)%.json", "%1")
    end

    local tmp = '/levels/' .. scenarioData.levelName .. '/info.json'
    if FS:fileExists(tmp) then
      local infoJson = jsonReadFile(tmp)
      if infoJson and infoJson.title then
        scenarioData.map = infoJson.title
      end
    end

    -- below are the defaults for a scenario including automatic file guessing for some fields
    if not scenarioData.vehicles then
      scenarioData.vehicles = {scenario_player0 = {playerUsable = true, startFocus = true}, ['*'] = {playerUsable = false}}
    end

    scenarioData.aiControlledVehiclesById = {}

    if not scenarioData.difficulty then
      scenarioData.difficulty = 'easy'
    end
    scenarioData.extensions = scenarioData.extensions or {}
    table.insert(scenarioData.extensions, {name=scenarioData.scenarioName, optional=true}) -- try to load an extension with the scenarioname by default

    -- figure out if a html start file is existing
    local htmldiscovered = false
    if not scenarioData.startHTML then
      scenarioData.startHTML = scenarioData.scenarioName .. '.html'
      htmldiscovered = true
    end
    if scenarioData.directory and not FS:fileExists(scenarioData.directory.."/"..scenarioData.startHTML) then
      if not htmldiscovered then
        log('W', logTag, 'start html not found, disabled: ' .. scenarioData.startHTML)
      end
      scenarioData.startHTML = nil
    end

    if not scenarioData.introType then
        scenarioData.introType = 'htmlOnly'
    end

    -- figure out the prefabs: add default and check them
    if not scenarioData.prefabs then
      scenarioData.prefabs = {}
    end

    -- try to load some defaults
    local levelPath = scenarioData.mission
    buildPrefabCache(levelPath)

    if scenarioData.directory then
      local foundMatchingPrefabs = {}
      local prefabsToFind = {}
      prefabsToFind[string.lower(scenarioData.scenarioName)] = true
      for _, prefabPath in ipairs(scenarioData.prefabs) do
        if not string.find(prefabPath,".prefab") then -- checking if this is a short name entry in the table. Checking for ".prefab" also cover ".prefab.json"
          prefabsToFind[prefabPath] = true
        else
          table.insert(foundMatchingPrefabs, prefabPath)
        end
      end

      local prefabCache = M.prefabCache[levelPath] or {}

      -- check the local directory of the scenario first
      local directory_cache = prefabCache[scenarioData.directory.."/"] or {}
      for prefabName, prefabPath in pairs(directory_cache) do
        if prefabsToFind[prefabName] or string.find(prefabName, scenarioData.scenarioName) then
          table.insert(foundMatchingPrefabs, prefabPath)
          prefabsToFind[prefabName] = false
        end
      end

      -- then check the level's directory
      local levelPath_cache = prefabCache[levelPath] or {}
      for prefabName, prefabPath in pairs(levelPath_cache) do
        if prefabsToFind[prefabName] or string.find(prefabName, scenarioData.scenarioName) then
          table.insert(foundMatchingPrefabs, prefabPath)
          prefabsToFind[prefabName] = false
        end
      end
      scenarioData.prefabs = foundMatchingPrefabs
    end

    -- figure out the previews automatically and check for errors
    if not scenarioData.previews then
      local tmp = FS:findFiles(scenarioData.directory.."/", scenarioData.scenarioName..'*.jpg', 0, true, false)
      local matchedScenarios = FS:findFiles(scenarioData.directory.."/", scenarioData.scenarioName..'*.json', 0, true, false)
      local otherScenarios = {}
      for i,v in ipairs(matchedScenarios) do
        local otherScenarioName = string.gsub(v, "(.*/)(.*)%.json", "%2")
        if otherScenarioName ~= scenarioData.scenarioName then
          table.insert(otherScenarios, otherScenarioName)
        end
      end

      scenarioData.previews = {}
      for _, p in pairs(tmp) do
        if string.startswith(p, scenarioData.directory) then
          local imageFilename = string.sub(p, string.len(scenarioData.directory) + 2, string.len(p) - 4)
          local foundClash = false
          for i,otherScenarioName in ipairs(otherScenarios) do
            if imageFilename == otherScenarioName then
              foundClash = true
            end
          end
          if not foundClash then
            table.insert(scenarioData.previews, imageFilename..'.jpg')
          end
        end
      end
    end

    local np = {}
    if scenarioData.directory then
      for _,p in pairs(scenarioData.previews) do
          table.insert(np, imageExistsDefault(scenarioData.directory.."/"..p))
      end
      if tableIsEmpty(np) then
         table.insert(np, imageExistsDefault('/'))
      end
      scenarioData.previews = np
    end
    if #scenarioData.previews == 0 then
      log('W', logTag, 'scenario has no previews: ' .. tostring(scenarioData.scenarioName))
    end

    if not scenarioData.playersCountRange then scenarioData.playersCountRange = {} end
    if not scenarioData.playersCountRange.min then scenarioData.playersCountRange.min = 1 end
    scenarioData.playersCountRange.min = math.max( 1, scenarioData.playersCountRange.min )
    if not scenarioData.playersCountRange.max then scenarioData.playersCountRange.max = scenarioData.playersCountRange.min end
    scenarioData.playersCountRange.max = math.max( scenarioData.playersCountRange.min, scenarioData.playersCountRange.max )

    scenarioData.extraTime = 0

    -- set defaults if keys are missing

    scenarioData.lapCount = scenarioData.lapCount or 1
    scenarioData.whiteListActions = scenarioData.whiteListActions or {}
    table.insert(scenarioData.whiteListActions, "default_whitelist_scenario")

    scenarioData.blackListActions = scenarioData.blackListActions or {}
    table.insert(scenarioData.blackListActions, "default_blacklist_scenario")

    scenarioData.radiusMultiplierAI = scenarioData.radiusMultiplierAI or 1

    local restrictScenarios = settings.getValue("restrictScenarios")
    if restrictScenarios == nil then restrictScenarios = true end
    if (shipping_build and campaign_campaigns and campaign_campaigns.getCampaignActive()) then restrictScenarios = true end

    if not restrictScenarios then
      if not displayedRestrictMessage then
        displayedRestrictMessage = true
        log('W', logTag, '**** Restrictions on Scenario Turned off in game settings. Removing restrictions. ****')
      end
      scenarioData.whiteListActions = {}
      scenarioData.blackListActions = core_input_actionFilter.createActionTemplate({"vehicleTeleporting"})
    end

    -- process lapConfig
    scenarioData.BranchLapConfig = scenarioData.BranchLapConfig or scenarioData.lapConfig or {}
    scenarioData.lapConfig = {}
    for i, v in ipairs(scenarioData.BranchLapConfig) do
      if type(v) == 'string' then
        table.insert(scenarioData.lapConfig, v)
      end
    end
    scenarioData.initialLapConfig = deepcopy(scenarioData.lapConfig)

    if scenarioData.attemptsInfo then
      scenarioData.attemptsInfo.allowedAttempts = scenarioData.attemptsInfo.allowedAttempts or 0
      scenarioData.attemptsInfo.delayPerAttempt = scenarioData.attemptsInfo.delayPerAttempt or 1
      scenarioData.attemptsInfo.allowVehicleSelectPerAttempt = scenarioData.attemptsInfo.allowVehicleSelectPerAttempt or false
      scenarioData.attemptsInfo.failAttempts = scenarioData.attemptsInfo.failAttempts or {}
      scenarioData.attemptsInfo.completeAttempt = scenarioData.attemptsInfo.completeAttempt or {}
      scenarioData.attemptsInfo.attemptNumber = 0
      scenarioData.attemptsInfo.waitTimerStart = false
      scenarioData.attemptsInfo.waitTimer = 0
      scenarioData.attemptsInfo.waitTimerActive = false
      scenarioData.attemptsInfo.currentAttemptReported = false
    end
    return scenarioData
end

Callers

@/lua/ge/extensions/scenario/scenariosLoader.lua
      if type(scenarioData) == 'table' and #scenarioData == 1 then
        processedScenario = processScenarioData(key, scenarioData[1], scenarioPath)
        --TODO: this is converted to string to avoid data loss when json en-/decoding.

  return processScenarioData(nil, scenarioData, nil)
end
        }
        local newScenario = processScenarioData(nil, scenarioData, fgPath)
        if newScenario then
@/lua/ge/extensions/scenario/quickRaceLoader.lua

  local processedScenario = scenario_scenariosLoader.processScenarioData(scenarioKey, scenarioFile)