GE Lua Documentation

Press F to search!

compileVehicleCollection

Definition


-- @/lua/ge/extensions/core/vehicle/partmgmt.lua:275

-- Compile the vehicle collection tree into a list of vehicles, resolve offsets, and assign pre-runtime ids
local function compileVehicleCollection(collectionTree)
  local collectionTreeCopy = deepcopy(collectionTree)

  -- Calculate offset between parent and child
  local function calculateOffsetRec(entry, parentEntry)
    if parentEntry then
      local offsetData = entry.offsetData
      local veh, parentVeh = getObjectByID(entry.vehId), getObjectByID(parentEntry.vehId)
      if not veh then
        log('E', 'partmgmt', 'vehicle not found for entry: ' .. tostring(entry.vehId))
        return
      end
      if not parentVeh then
        log('E', 'partmgmt', 'parent vehicle not found for entry: ' .. tostring(entry.vehId))
        return
      end

      local vehicleMat, parentVehMat = veh:getRefNodeMatrix(), parentVeh:getRefNodeMatrix()
      local offsetMat = parentVehMat:inverse() * vehicleMat
      local offsetEuler = offsetMat:toEuler()

      if offsetData.type == 'default' then
        local offsetPos = offsetMat:getPosition()
        entry.offsetData.offset = {
          x = roundNear(offsetPos.x, 0.001),
          y = roundNear(offsetPos.y, 0.001),
          z = roundNear(offsetPos.z, 0.001),
          rx = roundNear(math.deg(offsetEuler.x), 0.1),
          ry = roundNear(math.deg(offsetEuler.y), 0.1),
          rz = roundNear(math.deg(offsetEuler.z), 0.1),
        }

      elseif offsetData.type == 'coupledNodes' then
        entry.offsetData.offset = {
          rx = roundNear(math.deg(offsetEuler.x), 0.1),
          ry = roundNear(math.deg(offsetEuler.y), 0.1),
          rz = roundNear(math.deg(offsetEuler.z), 0.1),
        }
      end
    end

    for _, child in ipairs(entry.children) do
      calculateOffsetRec(child, entry)
    end
  end
  calculateOffsetRec(collectionTreeCopy, nil)

  local vehicles = {}
  local vehIdMap = {}

  local function createVehIdMapRec(entry)
    vehIdMap[entry.vehId] = tableSize(vehIdMap) + 1
    for _, child in ipairs(entry.children) do
      createVehIdMapRec(child)
    end
  end

  createVehIdMapRec(collectionTreeCopy)

  local function convertVehIdToIdx(entry, parentEntry)
    local offsetData = entry.offsetData
    if offsetData then
      local parentVehId = parentEntry.vehId
      local parentId = vehIdMap[parentVehId]
      if parentId then
        offsetData.parentId = parentId
      end
    end

    local data = saveVehicle(entry)
    if data then
      table.insert(vehicles, data)
    end

    for _, child in ipairs(entry.children) do
      convertVehIdToIdx(child, entry)
    end
  end

  convertVehIdToIdx(collectionTreeCopy)

  return vehicles
end

Callers

@/lua/ge/extensions/core/vehicle/partmgmt.lua

  local vehicles = compileVehicleCollection(collectionTree)
  local collectionTree = core_vehicles.generateAttachedVehiclesTree(playerVehicleId)
  local vehicles = compileVehicleCollection(collectionTree)