Skip to content

Commit

Permalink
refactor(config/shared): combined rest of shared vehicle properties i…
Browse files Browse the repository at this point in the history
…nto one table (#103)
  • Loading branch information
Manason authored Aug 24, 2024
1 parent 76573e9 commit 53d98e6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 54 deletions.
2 changes: 0 additions & 2 deletions client/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ local getIsCloseToCoords = functions.getIsCloseToCoords
local getIsBlacklistedWeapon = functions.getIsBlacklistedWeapon
local getIsVehicleLockpickImmune = functions.getIsVehicleLockpickImmune
local getIsVehicleCarjackingImmune = functions.getIsVehicleCarjackingImmune
local getIsVehicleTypeShared = functions.getIsVehicleTypeShared
local getIsVehicleShared = functions.getIsVehicleShared

local public = {}
Expand All @@ -15,7 +14,6 @@ public.getIsCloseToCoords = getIsCloseToCoords

function public.getIsVehicleShared(vehicle)
return config.sharedVehicleClasses[GetVehicleClass(vehicle)]
or getIsVehicleTypeShared(vehicle)
or getIsVehicleShared(vehicle)
end

Expand Down
35 changes: 6 additions & 29 deletions config/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ return {
---1. model
---2. type
---3. default
---Each field inherits from its parent if not specified.
---Each field falls back to its parent value if not specified.
---Example: model's shared value is nil, so the type's shared value is used.
---@type VehiclesConfig
vehicles = {
default = {
noLock = false,
spawnLocked = 1.0,
carjackingImmune = false,
lockpickImmune = false,
shared = false,
},
types = {

Expand All @@ -19,34 +24,6 @@ return {
-- }
}
},
sharedVehicles = {
-- `stockade` -- example
},

sharedVehicleTypes = {
'bike'
},

-- Vehicles that will never lock
---@type VehicleSelection
noLockVehicles = {
models = {
-- `stockade` -- example
},

types = {

}
},

-- Vehicles that cannot be jacked
carjackingImmuneVehicles = {
`stockade`
},

lockpickImmuneVehicles = {
-- `stockade` -- example
},

-- Weapons that cannot be used for carjacking
noCarjackWeapons = {
Expand Down
52 changes: 34 additions & 18 deletions shared/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@ end

---Checks if the vehicle has no locks and is accessible to everyone.
---@param vehicle number The entity number of the vehicle.
---@return boolean? `true` if the vehicle is blacklisted, `nil` otherwise.
---@return boolean `true` if the vehicle is blacklisted, `false` otherwise.
function public.getIsVehicleShared(vehicle)
return getIsOnList(GetEntityModel(vehicle), config.sharedVehicles)
return public.getVehicleConfig(vehicle).shared
end

---Checks if the vehicle cannot be locked.
---@param vehicle number The entity number of the vehicle.
---@return boolean? `true` if the vehicle is blacklisted, `nil` otherwise.
function public.getIsVehicleAlwaysUnlocked(vehicle)
return getIsOnList(GetEntityModel(vehicle), config.noLockVehicles.models)
or getIsOnList(GetVehicleType(vehicle), config.noLockVehicles.types)
or Entity(vehicle).state.ignoreLocks
return public.getVehicleConfig(vehicle).noLock or Entity(vehicle).state.ignoreLocks
end

---Checks the vehicle is always locked at spawn.
Expand All @@ -48,16 +46,16 @@ end

---Checks the vehicle is carjacking immune.
---@param vehicle number The entity number of the vehicle.
---@return boolean? `true` if the vehicle is immune, `nil` otherwise.
---@return boolean `true` if the vehicle is immune, `false` otherwise.
function public.getIsVehicleCarjackingImmune(vehicle)
return getIsOnList(GetEntityModel(vehicle), config.carjackingImmuneVehicles)
return public.getVehicleConfig(vehicle).carjackingImmune
end

---Checks the vehicle is lockpicking immune.
---@param vehicle number The entity number of the vehicle.
---@return boolean? `true` if the vehicle is immune, `nil` otherwise.
---@return boolean `true` if the vehicle is immune, `false` otherwise.
function public.getIsVehicleLockpickImmune(vehicle)
return getIsOnList(GetEntityModel(vehicle), config.lockpickImmuneVehicles)
return public.getVehicleConfig(vehicle).lockpickImmune
end

---Checks if the weapon cannot be used to steal keys from drivers.
Expand All @@ -67,22 +65,40 @@ function public.getIsBlacklistedWeapon(weaponHash)
return getIsOnList(weaponHash, config.noCarjackWeapons)
end

---Checks if the vehicle type has no locks and is accessible to everyone.
---@param vehicle number The entity number of the vehicle.
---@return boolean? `true` if the vehicle type is accessible, `nil` otherwise.
function public.getIsVehicleTypeShared(vehicle)
return getIsOnList(GetVehicleType(vehicle), config.sharedVehicleTypes)
local function findConfigValue(filteredConfig, key, default)
if filteredConfig.modelConfig[key] ~= nil then
return filteredConfig.modelConfig[key]
elseif filteredConfig.typeConfig[key] ~= nil then
return filteredConfig.typeConfig[key]
elseif filteredConfig.defaultConfig[key] ~= nil then
return filteredConfig.defaultConfig[key]
else
return default
end
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
local filteredConfig = {
modelConfig = config.vehicles.models[GetEntityModel(vehicle)],
typeConfig = config.vehicles.types[GetVehicleType(vehicle)],
defaultConfig = config.vehicles.default
}

local noLock = findConfigValue(filteredConfig, 'noLock', false)
local spawnLocked = noLock and 0.0 or findConfigValue(filteredConfig, 'spawnLocked', 1.0)
local carjackingImmune = findConfigValue(filteredConfig, 'carjackingImmune', false)
local lockpickImmune = findConfigValue(filteredConfig, 'lockpickImmune', false)
local shared = findConfigValue(filteredConfig, 'shared', false)

return {
spawnLocked = modelConfig.spawnLocked or typeConfig.spawnLocked or defaultConfig.spawnLocked or 1.0
spawnLocked = spawnLocked,
noLock = noLock,
carjackingImmune = carjackingImmune,
lockpickImmune = lockpickImmune,
shared = shared,
}
end

Expand Down
10 changes: 5 additions & 5 deletions types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

---@alias VehicleType 'automobile' | 'bike' | 'boat' | 'heli' | 'plane' | 'submarine' | 'trailer' | 'train'

---@class VehicleSelection
---@field types VehicleType[]
---@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
---@field spawnLocked? boolean | number ratio 0.0 - 1.0
---@field noLock? boolean
---@field carjackingImmune? boolean
---@field lockpickImmune? boolean
---@field shared? boolean

0 comments on commit 53d98e6

Please sign in to comment.