Skip to content

Commit

Permalink
feat(getIsVehicleInitiallyLocked): integrate with lockNPCCarsChance
Browse files Browse the repository at this point in the history
  • Loading branch information
artur-michalak committed Aug 27, 2024
1 parent 91778f2 commit 86c00e4
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
4 changes: 0 additions & 4 deletions config/server.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
return {
runClearCronMinutes = 5,

-- NPC Vehicle Lock States
lockNPCDrivenCarsChance = .75, -- Chance on lock state for NPC cars being driven by NPCs
lockNPCParkedCarsChance = .75, -- Chance on lock state for NPC parked cars
}
3 changes: 2 additions & 1 deletion config/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ return {
---@type VehicleConfig
default = {
noLock = false,
spawnLocked = 1.0,
spawnLocked = .75,
drivenSpawnLocked = .75,
carjackingImmune = false,
lockpickImmune = false,
shared = false,
Expand Down
5 changes: 1 addition & 4 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ AddEventHandler('entityCreated', function (entity)

if not DoesEntityExist(vehicle) then return end -- ped can be not in vehicle, so we need to check if vehicle is a entity, otherwise it will return 0

local chance = math.random()
local isLocked = (getIsVehicleInitiallyLocked(vehicle)
or (type == EntityType.Ped and chance < config.lockNPCDrivenCarsChance)
or (type == EntityType.Vehicle and chance < config.lockNPCParkedCarsChance))
local isLocked = getIsVehicleInitiallyLocked(vehicle, type == EntityType.Ped)
and not getIsVehicleAlwaysUnlocked(vehicle)
SetVehicleDoorsLocked(vehicle, isLocked and 2 or 1)
end)
15 changes: 10 additions & 5 deletions shared/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,18 @@ end

---Checks the vehicle is always locked at spawn.
---@param vehicle number The entity number of the vehicle.
---@param isDriven boolean
---@return boolean `true` if the vehicle is locked, `false` otherwise.
function public.getIsVehicleInitiallyLocked(vehicle)
local isVehicleSpawnLocked = public.getVehicleConfig(vehicle).spawnLocked
if type(isVehicleSpawnLocked) == 'number' then
return math.random() < isVehicleSpawnLocked
function public.getIsVehicleInitiallyLocked(vehicle, isDriven)
local vehicleConfig = public.getVehicleConfig(vehicle)
local vehicleLockedChance = isDriven
and vehicleConfig.drivenSpawnLocked
or vehicleConfig.spawnLocked

if type(vehicleLockedChance) == 'number' then
return math.random() < vehicleLockedChance
else
return isVehicleSpawnLocked ~= nil
return vehicleLockedChance ~= nil
end
end

Expand Down
1 change: 1 addition & 0 deletions types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

---@class VehicleConfig
---@field spawnLocked? boolean | number ratio 0.0 - 1.0
---@field drivenSpawnLocked? boolean | number ratio 0.0 - 1.0
---@field noLock? boolean
---@field carjackingImmune? boolean
---@field lockpickImmune? boolean
Expand Down

0 comments on commit 86c00e4

Please sign in to comment.