Racing position in race scenario?

Discussion in 'Programming' started by danielr, Sep 4, 2019.

  1. danielr

    danielr
    Expand Collapse

    Joined:
    Jan 6, 2014
    Messages:
    81
    Hi, currently here's the wip "Racing Position" app that shows a blank area when used in a race scenario.

    racepos.jpg

    That feature is from way back pre-race update and on hold I suppose? In the meantime, is there a simple way with lua to create some kind of UI element that shows the current position in a race scenario that we could use until the official app is updated? Maybe someone has dabbled in this area and likes to share something that's helpful for anyone creating race scenarios.

    Btw, Wreckfest has a pretty cool approach with the position and deltas to the opponent in front, as well as behind the player. So far I only use a basic lua, copied from the official derby race scenario with Gamergulls ScriptAI lua code to load AI lines.

    Heres the lua so far:
    Code:
    local M = {}
    
    local helper = require('scenario/scenariohelper')
    local logTag = 'hirochiF8races58'
    
    local finalWaypointName = 'derby_wp14'
    local finalTriggerName = 'trigger_finish1'
    local playerInstance = 'scenario_player0'
    local aiInstance1 = 'scenario_opponent1'
    local aiInstance2 = 'scenario_opponent2'
    local aiInstance3 = 'scenario_opponent3'
    local aiInstance4 = 'scenario_opponent4'
    local aiInstance5 = 'scenario_opponent5'
    
    local running = false
    local playerWon = false
    local aiFinished = false
    local noOfLaps = 8
    local lapsCompleted = {}
    
    local function reset()
      lapsCompleted['scenario_player0'] = 0
      lapsCompleted['scenario_opponent1'] = 0
      lapsCompleted['scenario_opponent2'] = 0
      lapsCompleted['scenario_opponent3'] = 0
      lapsCompleted['scenario_opponent4'] = 0
      lapsCompleted['scenario_opponent5'] = 0
     
      running = false
      playerWon = false
      aiWon = false
    end
    
    local function fail(reason)
      scenario_scenarios.finish({failed = reason})
      reset()
    end
    
    local function success(reason)
      scenario_scenarios.finish({msg = reason})
      reset()
    end
    
    local racers = {"scenario_opponent1", "scenario_opponent2", "scenario_opponent3", "scenario_opponent4", "scenario_opponent5"}
    local filenames = {"f8mc-1sunburstofforad", "f8mc-2pickupmarauder", "f8mc-3etki300m", "f8mc-4legranluxev6", "f8mc-5fullsizedriftmissile"}
    
    local function onRaceStart()
    
      scenario_scenarios.trackVehicleMovementAfterDamage(playerInstance, {waitTimerLimit=10})
    
      running = true
    end
    
    local function onCountdownStarted()
      reset()
     
      local recordings = {}
        for i, v in ipairs(racers) do
            local id = scenetree.findObject(racers[i]):getId()
            local filename = "/replays/scriptai/tracks/"..filenames[i]..".track.json"
            local scriptData = jsonReadFile(filename)
            --dump(scriptData)
            recordings[id] = scriptData.recording
            scenetree.findObject(racers[i]):queueLuaCommand("ai.startFollowing("..serialize(recordings[id])..")")
        end
    end
    
    local function onRaceWaypointReached(data)
      --log('I', logTag,'onRaceWaypointReached called ')
      if data.waypointName == finalWaypointName then
        lapsCompleted[data.vehicleName] = lapsCompleted[data.vehicleName] + 1
        if lapsCompleted[data.vehicleName] == noOfLaps then
          if data.vehicleName == playerInstance and not aiFinished then
            playerWon = true
        end
        end
      end
    end
    
    local function onRaceResult()
      if playerWon then
        success('scenarios.west_coast_usa.derby_asphalt.win.msg')
      else
        fail('scenarios.west_coast_usa.derby_asphalt.fail.msg')
      end
    end
    
    local function onBeamNGTrigger(data)
    
      if data.triggerName == finalTriggerName and data.event == 'enter' then
        if data.subjectName == aiInstance1 or data.subjectName == aiInstance2 or data.subjectName == aiInstance3 or data.subjectName == aiInstance4 or data.subjectName == aiInstance5 then
    
          lapsCompleted[data.subjectName] = lapsCompleted[data.subjectName] + 1
          if lapsCompleted[data.subjectName] == noOfLaps then
            aiFinished = true
          end
        end
      end
    end
    
    M.onRaceStart = onRaceStart
    M.onCountdownStarted = onCountdownStarted
    M.onRaceWaypointReached = onRaceWaypointReached
    M.onRaceResult = onRaceResult
    return M
    
     
    #1 danielr, Sep 4, 2019
    Last edited: Sep 4, 2019
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice