Skip to content
This repository was archived by the owner on Apr 29, 2025. It is now read-only.

Commit 7936f15

Browse files
committed
feat: use lib grid to optimise nearby door calculations
1 parent 3457872 commit 7936f15

File tree

2 files changed

+57
-70
lines changed

2 files changed

+57
-70
lines changed

client/main.lua

Lines changed: 56 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@ if not LoadResourceFile(cache.resource, 'web/build/index.html') then
22
error('Unable to load UI. Build ox_doorlock or download the latest release.\n ^3https://github.com/overextended/ox_doorlock/releases/latest/download/ox_doorlock.zip^0')
33
end
44

5-
if not lib.checkDependency('ox_lib', '3.14.0', true) then return end
5+
if not lib.checkDependency('ox_lib', '3.30.4', true) then return end
6+
7+
local math = require 'glm'
8+
local doors = {}
9+
_ENV.doors = doors
10+
611

712
local function createDoor(door)
13+
local oldDoor = doors[door.id]
14+
15+
if oldDoor then
16+
lib.grid.removeEntry(oldDoor)
17+
end
18+
19+
doors[door.id] = door
820
local double = door.doors
921
door.zone = GetLabelText(GetNameOfZone(door.coords.x, door.coords.y, door.coords.z))
22+
door.radius = door.maxDistance
1023

1124
if double then
1225
for i = 1, 2 do
@@ -27,82 +40,55 @@ local function createDoor(door)
2740
DoorSystemSetAutomaticRate(door.hash, door.doorRate or 10.0, false, false)
2841
end
2942
end
43+
44+
lib.grid.addEntry(door)
3045
end
3146

32-
local nearbyDoors = {}
47+
local nearbyDoors = lib.array:new()
48+
local nearbyDoorsCount = 0
3349
local Entity = Entity
50+
local ratio = GetAspectRatio(true)
3451

3552
lib.callback('ox_doorlock:getDoors', false, function(data)
36-
doors = data
37-
38-
for _, door in pairs(data) do
39-
createDoor(door)
40-
end
53+
for _, door in pairs(data) do createDoor(door) end
4154

4255
while true do
43-
table.wipe(nearbyDoors)
4456
local coords = GetEntityCoords(cache.ped)
57+
nearbyDoors = lib.grid.getNearbyEntries(coords)
58+
nearbyDoorsCount = #nearbyDoors
59+
ratio = GetAspectRatio(true)
4560

46-
for _, door in pairs(doors) do
61+
for index = 1, nearbyDoorsCount do
62+
local door = nearbyDoors[index]
4763
local double = door.doors
4864
door.distance = #(coords - door.coords)
4965

5066
if double then
51-
if door.distance < 80 then
52-
for i = 1, 2 do
53-
if not double[i].entity and IsModelValid(double[i].model) then
54-
local entity = GetClosestObjectOfType(double[i].coords.x, double[i].coords.y, double[i].coords.z, 1.0, double[i].model, false, false, false)
55-
56-
if entity ~= 0 then
57-
double[i].entity = entity
58-
Entity(entity).state.doorId = door.id
59-
end
60-
end
61-
end
67+
for i = 1, 2 do
68+
if IsModelValid(double[i].model) then
69+
local entity = GetClosestObjectOfType(double[i].coords.x, double[i].coords.y, double[i].coords.z, 1.0, double[i].model, false, false, false)
6270

63-
if door.distance < 20 then
64-
nearbyDoors[#nearbyDoors + 1] = door
65-
end
66-
else
67-
for i = 1, 2 do
68-
double[i].entity = nil
69-
end
70-
end
71-
elseif door.distance < 80 then
72-
if not door.entity and IsModelValid(door.model) then
73-
local entity = GetClosestObjectOfType(door.coords.x, door.coords.y, door.coords.z, 1.0, door.model, false, false, false)
74-
75-
if entity ~= 0 then
76-
local min, max = GetModelDimensions(door.model)
77-
local points = {
78-
GetOffsetFromEntityInWorldCoords(entity, min.x, min.y, min.z).xy,
79-
GetOffsetFromEntityInWorldCoords(entity, min.x, min.y, max.z).xy,
80-
GetOffsetFromEntityInWorldCoords(entity, min.x, max.y, max.z).xy,
81-
GetOffsetFromEntityInWorldCoords(entity, min.x, max.y, min.z).xy,
82-
GetOffsetFromEntityInWorldCoords(entity, max.x, min.y, min.z).xy,
83-
GetOffsetFromEntityInWorldCoords(entity, max.x, min.y, max.z).xy,
84-
GetOffsetFromEntityInWorldCoords(entity, max.x, max.y, max.z).xy,
85-
GetOffsetFromEntityInWorldCoords(entity, max.x, max.y, min.z).xy
86-
}
87-
88-
local centroid = vec2(0, 0)
89-
90-
for i = 1, 8 do
91-
centroid += points[i]
71+
if entity ~= 0 then
72+
double[i].entity = entity
73+
Entity(entity).state.doorId = door.id
9274
end
93-
94-
centroid = centroid / 8
95-
door.coords = vec3(centroid.x, centroid.y, door.coords.z)
96-
door.entity = entity
97-
Entity(entity).state.doorId = door.id
98-
end
75+
else double[i].entity = nil end
9976
end
100-
101-
if door.distance < 20 then
102-
nearbyDoors[#nearbyDoors + 1] = door
103-
end
104-
elseif door.entity then
105-
door.entity = nil
77+
elseif IsModelValid(door.model) then
78+
local entity = GetClosestObjectOfType(door.coords.x, door.coords.y, door.coords.z, 1.0, door.model, false, false, false)
79+
80+
if entity ~= 0 then
81+
local min, max = GetModelDimensions(door.model)
82+
local center = vec3((min.x + max.x) / 2, (min.y + max.y) / 2, (min.z + max.z) / 2)
83+
local heading = GetEntityHeading(entity) * (math.pi / 180)
84+
local sin, cos = math.sincos(heading)
85+
local rotatedX = cos * center.x - sin * center.y
86+
local rotatedY = sin * center.x + cos * center.y
87+
door.coords = vec3(door.coords.x + rotatedX, door.coords.y + rotatedY, door.coords.z + center.z)
88+
door.entity = entity
89+
90+
Entity(entity).state.doorId = door.id
91+
else door.entity = nil end
10692
end
10793
end
10894

@@ -114,7 +100,6 @@ RegisterNetEvent('ox_doorlock:setState', function(id, state, source, data)
114100
if not doors then return end
115101

116102
if data then
117-
doors[id] = data
118103
createDoor(data)
119104

120105
if NuiHasLoaded then
@@ -243,6 +228,9 @@ RegisterNetEvent('ox_doorlock:editDoorlock', function(id, data)
243228
end
244229
end
245230

231+
lib.grid.removeEntry(doors[id])
232+
lib.grid.addEntry(data)
233+
246234
doors[id] = data
247235

248236
if NuiHasLoaded then
@@ -303,15 +291,14 @@ CreateThread(function()
303291
local DrawSprite = drawSprite and DrawSprite
304292

305293
while true do
306-
local num = #nearbyDoors
294+
ClosestDoor = nearbyDoors[1]
307295

308-
if num > 0 then
309-
local ratio = drawSprite and GetAspectRatio(true)
310-
for i = 1, num do
296+
if nearbyDoorsCount > 0 then
297+
for i = 1, nearbyDoorsCount do
311298
local door = nearbyDoors[i]
312299

313300
if door.distance < door.maxDistance then
314-
if door.distance < (ClosestDoor?.distance or 10) then
301+
if door.distance < ClosestDoor.distance then
315302
ClosestDoor = door
316303
end
317304

@@ -326,7 +313,7 @@ CreateThread(function()
326313
end
327314
end
328315
end
329-
else ClosestDoor = nil end
316+
end
330317

331318
if ClosestDoor and ClosestDoor.distance < ClosestDoor.maxDistance then
332319
if Config.DrawTextUI and not ClosestDoor.hideUi and ClosestDoor.state ~= showUI then
@@ -342,7 +329,7 @@ CreateThread(function()
342329
showUI = nil
343330
end
344331

345-
Wait(num > 0 and 0 or 500)
332+
Wait(nearbyDoorsCount > 0 and 0 or 500)
346333
end
347334
end)
348335

server/main.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ if not LoadResourceFile(cache.resource, 'web/build/index.html') then
44
end
55

66
if not lib.checkDependency('oxmysql', '2.4.0') then return end
7-
if not lib.checkDependency('ox_lib', '3.14.0') then return end
7+
if not lib.checkDependency('ox_lib', '3.30.4') then return end
88

99
lib.versionCheck('overextended/ox_doorlock')
1010
require 'server.convert'

0 commit comments

Comments
 (0)