Skip to content

Commit

Permalink
refactor: vehicles config (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
Manason authored Aug 24, 2024
1 parent 2699175 commit 76573e9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
32 changes: 20 additions & 12 deletions config/shared.lua
Original file line number Diff line number Diff line change
@@ -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
},
Expand All @@ -19,18 +39,6 @@ return {
}
},

--- vehicles which spawn locked
---@type VehicleSelection
lockedVehicles = {
models = {
-- `stockade` -- example
},

types = {

}
},

-- Vehicles that cannot be jacked
carjackingImmuneVehicles = {
`stockade`
Expand Down
22 changes: 19 additions & 3 deletions shared/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
10 changes: 9 additions & 1 deletion types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@

---@class VehicleSelection
---@field types VehicleType[]
---@field models number[]
---@field models number[]

---@class VehiclesConfig
---@field default VehicleConfig
---@field types table<VehicleType, VehicleConfig>
---@field models table<number, VehicleConfig>

---@class VehicleConfig
---@field spawnLocked? boolean | number ratio 0.0 - 1.0

0 comments on commit 76573e9

Please sign in to comment.