mirror of
https://gitlab.com/gaelysam/mapgen_rivers.git
synced 2024-12-28 20:00:41 +01:00
Flow routing: Initialize basin_graph + comment where complexity might be non-linear
This commit is contained in:
parent
4bce5fab77
commit
f350f8785c
@ -111,6 +111,10 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional
|
|||||||
local d = dirs2[i] -- Get the directions water is coming from
|
local d = dirs2[i] -- Get the directions water is coming from
|
||||||
|
|
||||||
-- Iterate through the 4 directions
|
-- Iterate through the 4 directions
|
||||||
|
-- Loop is unrolled on purpose, for performance (critical part!)
|
||||||
|
----------
|
||||||
|
-- EAST --
|
||||||
|
----------
|
||||||
if d >= 8 then -- River coming from the East
|
if d >= 8 then -- River coming from the East
|
||||||
d = d - 8
|
d = d - 8
|
||||||
cur = cur + 1
|
cur = cur + 1
|
||||||
@ -123,7 +127,7 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional
|
|||||||
local l2 = basin_links[b2]
|
local l2 = basin_links[b2]
|
||||||
if not l2 then
|
if not l2 then
|
||||||
l2 = {b2, ib, elev=elev, i=i+1, is_y=false}
|
l2 = {b2, ib, elev=elev, i=i+1, is_y=false}
|
||||||
basin_links[b2] = l2
|
basin_links[b2] = l2 -- Potential non-linear complexity here
|
||||||
elseif l2.elev > elev then -- If this link is lower than the lowest registered link between these two basins, register it as the new lowest pass
|
elseif l2.elev > elev then -- If this link is lower than the lowest registered link between these two basins, register it as the new lowest pass
|
||||||
l2.elev = elev
|
l2.elev = elev
|
||||||
l2.i = i+1
|
l2.i = i+1
|
||||||
@ -146,6 +150,9 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-----------
|
||||||
|
-- SOUTH --
|
||||||
|
-----------
|
||||||
if d >= 4 then -- River coming from the South
|
if d >= 4 then -- River coming from the South
|
||||||
d = d - 4
|
d = d - 4
|
||||||
cur = cur + 1
|
cur = cur + 1
|
||||||
@ -180,6 +187,9 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
----------
|
||||||
|
-- WEST --
|
||||||
|
----------
|
||||||
if d >= 2 then -- River coming from the West
|
if d >= 2 then -- River coming from the West
|
||||||
d = d - 2
|
d = d - 2
|
||||||
cur = cur + 1
|
cur = cur + 1
|
||||||
@ -214,6 +224,9 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-----------
|
||||||
|
-- NORTH --
|
||||||
|
-----------
|
||||||
if d >= 1 then -- River coming from the North
|
if d >= 1 then -- River coming from the North
|
||||||
cur = cur + 1
|
cur = cur + 1
|
||||||
singular[cur] = i-X
|
singular[cur] = i-X
|
||||||
@ -287,7 +300,10 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional
|
|||||||
end
|
end
|
||||||
|
|
||||||
local basin_graph = {}
|
local basin_graph = {}
|
||||||
while cur > 1 do
|
for i=0, nbasins do
|
||||||
|
basin_graph[i] = {} -- Initialize (to ensure subtables don't go in the hash part)
|
||||||
|
end
|
||||||
|
for i=1, nbasins do
|
||||||
-- Iterate in lowlevel but its contents may change during the loop
|
-- Iterate in lowlevel but its contents may change during the loop
|
||||||
local b1 = lowlevel[cur]
|
local b1 = lowlevel[cur]
|
||||||
cur = cur - 1
|
cur = cur - 1
|
||||||
@ -307,13 +323,7 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional
|
|||||||
-- Add link to the graph, in both directions
|
-- Add link to the graph, in both directions
|
||||||
local bound = lnk1[b2]
|
local bound = lnk1[b2]
|
||||||
local bb1, bb2 = bound[1], bound[2]
|
local bb1, bb2 = bound[1], bound[2]
|
||||||
if not basin_graph[bb1] then
|
basin_graph[bb1][bb2] = bound -- Potential non-linear complexity here
|
||||||
basin_graph[bb1] = {}
|
|
||||||
end
|
|
||||||
if not basin_graph[bb2] then
|
|
||||||
basin_graph[bb2] = {}
|
|
||||||
end
|
|
||||||
basin_graph[bb1][bb2] = bound
|
|
||||||
basin_graph[bb2][bb1] = bound
|
basin_graph[bb2][bb1] = bound
|
||||||
|
|
||||||
-- Merge basin b1 into b2
|
-- Merge basin b1 into b2
|
||||||
|
Loading…
Reference in New Issue
Block a user