GE Lua Documentation

Press F to search!

moveToParkingSpot

Definition


-- @/lua/ge/extensions/gameplay/parking.lua:81

local function moveToParkingSpot(vehId, parkingSpot, lowPrecision) -- assigns a parked vehicle to a parking spot
  local obj = getObjectByID(vehId)
  local width, length = obj.initialNodePosBB:getExtents().x - 0.1, obj.initialNodePosBB:getExtents().y
  local backwards, offsetPos, offsetRot

  if parkingSpot.customFields.tags.forwards then
    backwards = false
  elseif parkingSpot.customFields.tags.backwards then
    backwards = true
  else
    backwards = random() > 0.75 + vars.neatness * 0.25 -- backwards direction is less common by default
  end

  if not parkingSpot.customFields.tags.perfect then -- randomize position and rotation slightly
    local offsetVal = 1 - square(vars.neatness)
    local xGap, yGap = max(0, parkingSpot.scl.x - width), max(0, parkingSpot.scl.y - length)
    local xRandom, yRandom = randomGauss3() / 3 - 0.5, clamp(randomGauss3() / 3 - (backwards and 0.75 or 0.25), -0.5, 0.5)
    offsetPos = vec3(xRandom * offsetVal * xGap, yRandom * offsetVal * yGap, 0)
    offsetRot = quatFromEuler(0, 0, (randomGauss3() / 3 - 0.5) * offsetVal * 0.25)
  end

  local options = {
    skipVehicleIntersectionCheck = true
  }
  parkingSpot:moveResetVehicleTo(vehId, lowPrecision, backwards, offsetPos, offsetRot, true, false, nil, options)
  if M.debugLevel > 0 then
    log("I", logTag, string.format("Teleported vehId %d to parking spot %d", vehId, parkingSpot.id))
  end

  getObjectByID(vehId):queueLuaCommand("electrics.setIgnitionLevel(0)")

  if parkedVehData[vehId] then
    if parkedVehData[vehId].parkingSpotId then
      sites.parkingSpots.objects[parkedVehData[vehId].parkingSpotId].vehicle = nil
    end

    if parkedVehData[vehId].randomPaint then
      core_vehicle_manager.setVehiclePaintsNames(vehId, core_vehiclePaints.getRandomPaintsByVehicle(vehId))
    end

    -- the following code is somewhat hacky
    if parkingSpot.customFields.tags.street then -- enables tracking, so that AI can try to avoid this vehicle
      if not map.objects[vehId] then getObjectByID(vehId):queueLuaCommand("mapmgr.enableTracking()") end
    else -- disables tracking, to optimize performance
      getObjectByID(vehId):queueLuaCommand("mapmgr.disableTracking()")
    end

    parkingSpot.vehicle = vehId -- parking spot contains this vehicle
    parkedVehData[vehId].parkingSpotId = parkingSpot.id -- vehicle is assigned to this parking spot
    parkedVehData[vehId].activeRadius = 50
    parkedVehData[vehId]._teleport = nil
  end
end

Callers

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

      moveToParkingSpot(vehId, ps, not getObjectByID(vehId):isReady())
      break
      vehPool:setVeh(newId, true)
      moveToParkingSpot(newId, ps)
      break