11local floor = math.floor
22
3+ local file = fileOpen (" vehiclenodes.json" , true )
4+ local size = fileGetSize (file )
5+ local rawData = fromJSON (fileRead (file , size ))
6+ fileClose (file )
7+
8+ -- Since the JSON structure is wrapped inside an array [{...}], extract the first element!
9+ vehicleNodes = {}
10+ if rawData then
11+ if rawData [1 ] then
12+ vehicleNodes = rawData [1 ]
13+ else
14+ vehicleNodes = rawData
15+ end
16+ end
17+
18+ outputDebugString (" GPS: " .. (localPlayer and " Client" or " Server" ) .. " database initialized successfully!" )
19+
320local function getAreaID (x , y )
421 return floor ((y + 3000 )/ 750 )* 8 + floor ((x + 3000 )/ 750 )
522end
623
724local function getNodeByID (db , nodeID )
825 local areaID = floor (nodeID / 65536 )
9- return db [areaID ][nodeID ]
26+ local area = db [areaID ] or db [tostring (areaID )]
27+ if not area then return false end
28+ return area [nodeID ] or area [tostring (nodeID )]
1029end
1130
1231local function findNodeClosestToPoint (db , x , y , z )
1332 local areaID = getAreaID (x , y )
33+
34+ -- Fallback lookup for string-indexed areas from client-side JSON decoding
35+ local nodesToSearch = db [areaID ] or db [tostring (areaID )]
36+ if not nodesToSearch then return false end
37+
1438 local minDist , minNode
1539 local nodeX , nodeY , dist
16- for id ,node in pairs (db [ areaID ] ) do
40+ for id ,node in pairs (nodesToSearch ) do
1741 nodeX , nodeY = node .x , node .y
1842 dist = (x - nodeX )* (x - nodeX ) + (y - nodeY )* (y - nodeY )
1943 if not minDist or dist < minDist then
@@ -25,6 +49,7 @@ local function findNodeClosestToPoint(db, x, y, z)
2549end
2650
2751local function calculatePath (db , nodeFrom , nodeTo )
52+ if not nodeFrom or not nodeTo then return false end
2853 local next = next
2954
3055 local g = { [nodeFrom ] = 0 } -- { node = g }
@@ -61,16 +86,17 @@ local function calculatePath(db, nodeFrom, nodeTo)
6186 break
6287 end
6388
64- local successors = {}
6589 for id ,distance in pairs (current .neighbours ) do
6690 local successor = getNodeByID (db , id )
67- local successor_g = g [current ] + distance * distance
68- if not g [successor ] or g [successor ] > successor_g then
69- setmetatable (successor , nodeMT )
91+ if successor then
92+ local successor_g = g [current ] + distance * distance
93+ if not g [successor ] or g [successor ] > successor_g then
94+ setmetatable (successor , nodeMT )
7095
71- g [successor ] = successor_g
72- openheap :insertvalue (successor )
73- parent [successor ] = current
96+ g [successor ] = successor_g
97+ openheap :insertvalue (successor )
98+ parent [successor ] = current
99+ end
74100 end
75101 end
76102 end
0 commit comments