From 76573e9f4a401389de9726bdd5b5dcd5a0f48d05 Mon Sep 17 00:00:00 2001 From: Manason Date: Sat, 24 Aug 2024 07:16:58 -0700 Subject: [PATCH] refactor: vehicles config (#102) --- config/shared.lua | 32 ++++++++++++++++++++------------ shared/functions.lua | 22 +++++++++++++++++++--- types.lua | 10 +++++++++- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/config/shared.lua b/config/shared.lua index 0a646b1..1101c20 100644 --- a/config/shared.lua +++ b/config/shared.lua @@ -1,4 +1,24 @@ return { + ---For a given vehicle, the config used is based on precendence of: + ---1. model + ---2. type + ---3. default + ---Each field inherits from its parent if not specified. + ---@type VehiclesConfig + vehicles = { + default = { + spawnLocked = 1.0, + }, + types = { + + }, + models = { + -- Example: + -- [`stockade`] = { + -- spawnLocked = 0.5 + -- } + } + }, sharedVehicles = { -- `stockade` -- example }, @@ -19,18 +39,6 @@ return { } }, - --- vehicles which spawn locked - ---@type VehicleSelection - lockedVehicles = { - models = { - -- `stockade` -- example - }, - - types = { - - } - }, - -- Vehicles that cannot be jacked carjackingImmuneVehicles = { `stockade` diff --git a/shared/functions.lua b/shared/functions.lua index 6f39c90..d948df3 100644 --- a/shared/functions.lua +++ b/shared/functions.lua @@ -36,10 +36,14 @@ end ---Checks the vehicle is always locked at spawn. ---@param vehicle number The entity number of the vehicle. ----@return boolean? `true` if the vehicle is locked, `nil` otherwise. +---@return boolean `true` if the vehicle is locked, `false` otherwise. function public.getIsVehicleInitiallyLocked(vehicle) - return getIsOnList(GetEntityModel(vehicle), config.lockedVehicles.models) - or getIsOnList(GetVehicleType(vehicle), config.lockedVehicles.types) + local isVehicleSpawnLocked = public.getVehicleConfig(vehicle).spawnLocked + if type(isVehicleSpawnLocked) == 'number' then + return math.random() < isVehicleSpawnLocked + else + return isVehicleSpawnLocked ~= nil + end end ---Checks the vehicle is carjacking immune. @@ -70,4 +74,16 @@ function public.getIsVehicleTypeShared(vehicle) return getIsOnList(GetVehicleType(vehicle), config.sharedVehicleTypes) end +---Gets the vehicle's config +---@param vehicle number +---@return VehicleConfig +function public.getVehicleConfig(vehicle) + local modelConfig = config.vehicles.models[GetEntityModel(vehicle)] + local typeConfig = config.vehicles.types[GetVehicleType(vehicle)] + local defaultConfig = config.vehicles.default + return { + spawnLocked = modelConfig.spawnLocked or typeConfig.spawnLocked or defaultConfig.spawnLocked or 1.0 + } +end + return public diff --git a/types.lua b/types.lua index 27cfcd0..af5e78f 100644 --- a/types.lua +++ b/types.lua @@ -4,4 +4,12 @@ ---@class VehicleSelection ---@field types VehicleType[] ----@field models number[] \ No newline at end of file +---@field models number[] + +---@class VehiclesConfig +---@field default VehicleConfig +---@field types table +---@field models table + +---@class VehicleConfig +---@field spawnLocked? boolean | number ratio 0.0 - 1.0 \ No newline at end of file