[VC] [Lua] Traffic Data Loader

Started by Anonymous, May 13, 2018, 02:32:47 AM

Previous topic - Next topic

Anonymous

Be sure to have "nodes/paths.ipl" file existing, copied from game folder.


traffic = {}
traffic.sidewalk = {}
traffic.sidewalk.nodes = {}
traffic.sidewalk.nodeGroups = {}
traffic.road = {}
traffic.road.nodes = {}
traffic.road.nodeGroups = {}
traffic.water = {}
traffic.water.nodes = {}
traffic.water.nodeGroups = {}

addEventHandler('OnResourceStart', function(event, resource)
if resource ~= thisResource then return end

-- load ped nodes into server memory
print('Loading traffic data..')

local file = openFile('nodes/paths.ipl')
local data = file:readBytes(1171556)
file:close()
local lines = explode("rn", data)

local groupType, node
local groupNodes_sidewalk, groupNodes_road, groupNodes_water = {}, {}, {}
for i,v in ipairs(lines) do
if v:find(',', 0, true) then

if v:len() <= 8 then
-- group type
if #groupNodes_sidewalk > 0 then table.insert(traffic.sidewalk.nodes, groupNodes_sidewalk) end
if #groupNodes_road > 0 then table.insert(traffic.road.nodes, groupNodes_road) end
if #groupNodes_water > 0 then table.insert(traffic.water.nodes, groupNodes_water) end
groupNodes_sidewalk = {}
groupNodes_road = {}
groupNodes_water = {}

groupType = tonumber(v:sub(1, 1))
else
-- node type
node = explode(',', v)
for i2,v2 in ipairs(node) do
node[i2] = tonumber(v2)
end

node[4] = node[4] / 16.0
node[5] = node[5] / 16.0
node[6] = node[6] / 16.0

if groupType == 0 then -- sidewalk
table.insert(groupNodes_sidewalk, node)
traffic.sidewalk.nodeGroups[node] = groupNodes_sidewalk
elseif groupType == 1 then -- road
table.insert(groupNodes_road, node)
traffic.road.nodeGroups[node] = groupNodes_road
elseif groupType == 2 then -- water
table.insert(groupNodes_water, node)
traffic.water.nodeGroups[node] = groupNodes_water
end
end

end
end
if #groupNodes_sidewalk > 0 then table.insert(traffic.sidewalk.nodes, groupNodes_sidewalk) end
if #groupNodes_road > 0 then table.insert(traffic.road.nodes, groupNodes_road) end
if #groupNodes_water > 0 then table.insert(traffic.water.nodes, groupNodes_water) end

print('Done.')
end)

function explode(div,str) -- credit: http://richard.warburton.it
  -- http://lua-users.org/wiki/MakingLuaLikePhp
  if (div=='') then return false end
  local pos,arr = 0,{}
  -- for each divider found
  for st,sp in function() return string.find(str,div,pos,true) end do
    table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
    pos = sp + 1 -- Jump past current divider
  end
  table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
  return arr
end

MEGAMIND

#1
make a folder nodes/path.ipl along with server.lua and xml?
\"WEBNET OFFICIAL || TDCS.TK\"

Anonymous

#2
the nodes folder goes in same folder as server.lua

it just loads the path data

it doesnt put peds on paths

Anonymous

#3
I've just noticed that this code depends on a string explode() function, I've added that to the first post in this thread.