Skip to content

Commit

Permalink
refactor(getIsVehicleInitiallyLocked): integrate with lockNPCCarsChan…
Browse files Browse the repository at this point in the history
…ce (#111)

* feat(getIsVehicleInitiallyLocked): integrate with lockNPCCarsChance

* chore(include): config.server

* refactor(entityCreated): isPed variable

* refactor(entityCreated): simplifying the validation

* feat(getVehicleConfig): drivenSpawnLocked

* feat(getIsVehicleInitiallyLocked): an explicit true check

Co-authored-by: Manason <[email protected]>

* refactor(config): spawnLocked to spawnLockedIfParked

* feat(entityCreated): isVehicle variable

* refactor(entityCreated): split return condition

---------

Co-authored-by: Manason <[email protected]>
  • Loading branch information
artur-michalak and Manason authored Sep 1, 2024
1 parent ab8806a commit a84afb4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 28 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
}
9 changes: 5 additions & 4 deletions 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,
spawnLockedIfParked = .75,
spawnLockedIfDriven = .75,
carjackingImmune = false,
lockpickImmune = false,
shared = false,
Expand All @@ -22,7 +23,7 @@ return {
categories = { -- known categories: super, service, utility, helicopters, motorcycles, suvs, planes, sports, emergency, military, sportsclassics, compacts, sedans
-- super = {
-- noLock = false,
-- spawnLocked = 1.0,
-- spawnLockedIfParked = 1.0,
-- carjackingImmune = false,
-- lockpickImmune = false,
-- shared = false,
Expand All @@ -38,7 +39,7 @@ return {
},
-- automobile = {
-- noLock = false,
-- spawnLocked = 1.0,
-- spawnLockedIfParked = 1.0,
-- carjackingImmune = false,
-- lockpickImmune = false,
-- shared = false,
Expand All @@ -50,7 +51,7 @@ return {
---@type table<Hash, VehicleConfig>
models = {
-- [`stockade`] = {
-- spawnLocked = 0.5
-- spawnLockedIfParked = 0.5
-- }
}
},
Expand Down
18 changes: 6 additions & 12 deletions server/main.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local config = require 'config.server'
local functions = require 'server.functions'
local sharedFunctions = require 'shared.functions'

Expand Down Expand Up @@ -64,19 +63,14 @@ AddEventHandler('entityCreated', function (entity)
then return end

local type = GetEntityType(entity)

if type ~= EntityType.Ped and type ~= EntityType.Vehicle then
return
end

local vehicle = type == EntityType.Ped and GetVehiclePedIsIn(entity, false) or entity
local isPed = type == EntityType.Ped
local isVehicle = type == EntityType.Vehicle
if not isPed and not isVehicle then return end
local vehicle = isPed and GetVehiclePedIsIn(entity, false) or 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))
and not getIsVehicleAlwaysUnlocked(vehicle)
local isLocked = not getIsVehicleAlwaysUnlocked(vehicle)
and getIsVehicleInitiallyLocked(vehicle, isPed)
SetVehicleDoorsLocked(vehicle, isLocked and 2 or 1)
end)
21 changes: 14 additions & 7 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.spawnLockedIfDriven
or vehicleConfig.spawnLockedIfParked

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

Expand Down Expand Up @@ -93,7 +98,8 @@ function public.getVehicleConfig(vehicle)
}

local noLock = findConfigValue(filteredConfig, 'noLock', false)
local spawnLocked = noLock and 0.0 or findConfigValue(filteredConfig, 'spawnLocked', 1.0)
local spawnLockedIfParked = noLock and 0.0 or findConfigValue(filteredConfig, 'spawnLockedIfParked', 1.0)
local spawnLockedIfDriven = noLock and 0.0 or findConfigValue(filteredConfig, 'spawnLockedIfDriven', 1.0)
local carjackingImmune = findConfigValue(filteredConfig, 'carjackingImmune', false)
local lockpickImmune = findConfigValue(filteredConfig, 'lockpickImmune', false)
local shared = findConfigValue(filteredConfig, 'shared', false)
Expand All @@ -102,7 +108,8 @@ function public.getVehicleConfig(vehicle)
local findKeysChance = findConfigValue(filteredConfig, 'findKeysChance', 1.0)

return {
spawnLocked = spawnLocked,
spawnLockedIfParked = spawnLockedIfParked,
spawnLockedIfDriven = spawnLockedIfDriven,
noLock = noLock,
carjackingImmune = carjackingImmune,
lockpickImmune = lockpickImmune,
Expand Down
3 changes: 2 additions & 1 deletion types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
---@alias Hash number|string actually a number but `model` is treated as a string by language server

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

0 comments on commit a84afb4

Please sign in to comment.