GE Lua Documentation

Press F to search!

getAssignedPlayers

Definition


-- @/lua/ge/extensions/core/multiseat.lua:17
local function getAssignedPlayers(devices, logEnabled, seatPlayers)
  -- push/pop the multiseat actinomap
  local multiseat = settings.getValue("multiseat")
  local changed = multiseat ~= lastMultiseat
  if changed then
    local o = scenetree.findObject("MultiseatActionMap")
    if o then
      if multiseat then o:push()
      else o:pop() end
    else
      log("E", "", "No multiseat action map found")
    end
  end
  lastMultiseat = multiseat

  -- assign each input device to a different player (except keyboard and mouse, those go to the same player)
  local maxPlayers = getMaxPlayersAmount(multiseat)
  local nVehicles = tableSize(getAllVehicles())
  local nControllers = tableSize(devices) - 1 -- assume mouse goes together with keyboard
  local players = math.max(1,math.min(maxPlayers, nControllers))
  if logEnabled and players > 1 then log("D", "multiseat", "Settled for "..players.." players:  supported="..maxPlayers..", vehicles="..nVehicles..", devices="..nControllers.." (& mouse)") end
  local devnames = tableKeys(devices)
  table.sort(devnames)
  local lastPlayer = 0
  lastPlayer = (lastPlayer + 1) % players -- skip the first player, it will be used by keyboard and mouse anyway
  local result = {}
  for _,devname in ipairs(devnames) do
    if devname:startswith("keyboard") or devname:startswith("mouse") then
      result[devname] = 0
      assignPlayerToDevice(devname, 0)
    else
      result[devname] = lastPlayer
      assignPlayerToDevice(devname, lastPlayer)
      lastPlayer = (lastPlayer + 1) % players
    end
  end
  if logEnabled and players > 1 then log((players>1) and "I" or "D", "", "Assigned players: "..dumps(result):gsub("\n", ""):gsub("  ", " ")) end

  -- re-seat all players in vehicles when requested
  local potentialSeatChanges = changed or multiseat -- skip re-seating players when there's no chance they'll end in a different car
  if potentialSeatChanges and seatPlayers then
    -- locate all vehicles
    local usedVehicles = {}
    for id, vehicle in activeVehiclesIterator() do
      usedVehicles[id] = 0
    end
    -- count amount of seats used on each vehicle
    for player=0, players-1 do
      local veh = getPlayerVehicle(player)
      if veh then
        local id = veh:getId()
        usedVehicles[id] = usedVehicles[id] + 1
      end
    end
    -- assign players on foot to vehicles (favour the least occupied vehicles)
    for player=0, maxPlayers-1 do
      if player > players-1 then
        be:exitVehicle(player)
      else
        local veh = getPlayerVehicle(player)
        if not veh then -- player has no vehicle, is on foot
          -- locate least occupied vehicle
          local leastUsedId = nil
          local leastUsedN = math.huge
          for id,n in pairs(usedVehicles) do
            if n < leastUsedN then
              leastUsedId = id
              leastUsedN = n
            end
          end
          -- seat this player in the vehicle we found
          if leastUsedId then
            local vehicle = getObjectByID(leastUsedId)
            be:enterVehicle(player, vehicle)
            -- update vehicle occupation counters
            usedVehicles[leastUsedId] = usedVehicles[leastUsedId] + 1
          end
        end
      end
    end
  end
  return result
end

Callers

@/lua/ge/extensions/scenario/scenarios.lua
    -- reseat players in their new vehicles
    local assignedPlayers = extensions.core_input_bindings.getAssignedPlayers()
    for _,assignment in ipairs(scenario.multiseatInput) do

  local assignedPlayers = extensions.core_input_bindings.getAssignedPlayers()
  local defaultIndex = allowUnassigned and 0 or 1
@/lua/ge/extensions/core/multiseatCamera.lua
  local pos = vec3(0, 0, 0)
  local plvehicles = tableValuesAsLookupDict(extensions.core_input_bindings.getAssignedPlayers())
@/lua/ge/extensions/core/input/bindings.lua
  if M.autoAssignPlayersToDevices then
    M.assignedPlayers = core_multiseat.getAssignedPlayers(M.devices, true)
  end
  if M.autoAssignPlayersToDevices then
    M.assignedPlayers = core_multiseat.getAssignedPlayers(M.devices, true)
  end

local function getAssignedPlayers()
  return M.assignedPlayers
  if M.autoAssignPlayersToDevices then
    M.assignedPlayers = core_multiseat.getAssignedPlayers(M.devices, true, true)
  end
  if M.autoAssignPlayersToDevices then
    M.assignedPlayers = core_multiseat.getAssignedPlayers(M.devices, true, true)
  end
  if M.autoAssignPlayersToDevices then
    M.assignedPlayers = core_multiseat.getAssignedPlayers(M.devices, true)
  end