career_retrievedamaged/lua/ge/extensions/career/retrievedamaged.lua

125 lines
4.3 KiB
Lua

-- Copyright (C) 2025 snoutie
-- Authors: snoutie (copyright@achtarmig.org)
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as published
-- by the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Affero General Public License for more details.
-- You should have received a copy of the GNU Affero General Public License
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
local M = {}
M.dependencies = { 'career_career', 'career_modules_computer', 'career_modules_inventory', 'career_modules_payment',
'career_modules_playerAttributes' }
local function getVehicleSavesFolder(savePath)
return savePath .. "/career/_vehicle_saves"
end
local function getInventoryIdFolder(vehicleSavesPath, inventoryId)
return vehicleSavesPath .. "/" .. inventoryId
end
local function getInventoryIdFile(inventoryIdPath)
return inventoryIdPath .. "/save.json"
end
local function createFolderIfNeeded(path)
if not FS:directoryExists(path) then
FS:directoryCreate(path)
end
end
local function prepareFolders(inventoryId)
local _, savePath = career_saveSystem.getCurrentSaveSlot()
local vehicleSavesFolder = getVehicleSavesFolder(savePath)
createFolderIfNeeded(vehicleSavesFolder)
local inventoryIdFolder = getInventoryIdFolder(vehicleSavesFolder, inventoryId)
createFolderIfNeeded(inventoryIdFolder)
return getInventoryIdFile(inventoryIdFolder)
end
local function onSave(inventoryId)
local fileName = prepareFolders(inventoryId)
local vehicleId = career_modules_inventory.getVehicleIdFromInventoryId(inventoryId)
if vehicleId then
local object = be:getObjectByID(vehicleId)
object:queueLuaCommand('beamstate.save("' .. fileName .. '")')
end
end
local function onLoad(inventoryId)
local fileName = prepareFolders(inventoryId)
local vehicleId = career_modules_inventory.getVehicleIdFromInventoryId(inventoryId)
if vehicleId then
local object = be:getObjectByID(vehicleId)
object:queueLuaCommand('beamstate.load("' .. fileName .. '")')
end
end
local function spawnVehicle(inventoryId, callback)
if career_modules_inventory.getVehicleIdFromInventoryId(inventoryId) then
callback()
else
career_modules_inventory.spawnVehicle(inventoryId, nil, callback)
end
end
local function retrieve_favourite()
local fav_veh_id = career_modules_inventory.getFavoriteVehicle()
extensions.core_jobsystem.create(
function(job)
onSave(fav_veh_id)
job.sleep(1)
spawnVehicle(fav_veh_id, function()
local veh = be:getObjectByID(career_modules_inventory.getVehicleIdFromInventoryId(fav_veh_id))
local location = { pos = veh:getPosition(), rot = quat(0, 0, 1, 0) * quat(veh:getRefNodeRotation()) }
local garage = career_modules_inventory.getClosestGarage(location.pos)
onLoad(fav_veh_id)
job.sleep(1)
freeroam_facilities.teleportToGarage(garage.id, veh, false)
end)
end
)
end
local function onComputerAddFunctions(menuData, computerFunctions)
if menuData.computerFacility.functions["vehicleInventory"] then
local computerFunctionData = {
id = "retrieve_damaged",
label = "Retrieve Favourite Damaged",
callback = retrieve_favourite,
order = 1
}
if menuData.tutorialPartShoppingActive or menuData.tutorialTuningActive then
computerFunctionData.disabled = true
computerFunctionData.reason = career_modules_computer.reasons.tutorialActive
end
computerFunctions.general[computerFunctionData.id] = computerFunctionData
end
end
local function onSaveCurrentSaveSlot(currentSavePath, oldSaveDate, vehiclesThumbnailUpdate)
local vs = career_modules_inventory.getVehicles()
for id, _ in pairs(vs) do
onSave(id)
end
end
M.onSaveCurrentSaveSlot = onSaveCurrentSaveSlot
M.onComputerAddFunctions = onComputerAddFunctions
return M