Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Worldgen 09 - Rivers #220

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
16 changes: 5 additions & 11 deletions sote/game/scenes/world-gen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,25 @@ local function map_tiles_to_hex()
local lat, lon = tile:latlon()
local q, r, face = hex.latlon_to_hex_coords(lat, lon, wg.world.size)

wg.world:cache_tile_coord(tile.tile_id, q, r, face)
wg.world:cache_square_ti_by_hex_coords(q, r, face)
end
end

local function load_mapping_from_file(file)
for row in require("game.file-utils").csv_rows(file) do
local tile_id = tonumber(row[1])
local q = tonumber(row[2])
local r = tonumber(row[3])
local face = tonumber(row[4])

wg.world:cache_tile_coord(tile_id, q, r, face)
wg.world:cache_square_ti_by_hex_ti(tonumber(row[2]))
end
end

local function cache_tile_coord()
local function cache_cube_world_tiles()
print("Caching tile coordinates...")

if debug.map_tiles_from_file then
-- it's faster to load the pre-calculated coordinates from a file than to calculate them on the fly
load_mapping_from_file("d:\\temp\\hex_mapping.csv")
load_mapping_from_file("d:\\temp\\hex_mapping2.csv")
else
map_tiles_to_hex()
end
-- wg.world:map_hex_coords()

print("Done caching tile coordinates")
end
Expand Down Expand Up @@ -119,7 +113,7 @@ function wg.generate_coro()
coroutine.yield()

wg.message = "Caching tile coordinates"
prof.run_with_profiling(function() cache_tile_coord() end, "[scenes.world-gen]", "cache_tile_coord")
prof.run_with_profiling(function() cache_cube_world_tiles() end, "[scenes.world-gen]", "cache_cube_world_tiles")
coroutine.yield()
end

Expand Down
11 changes: 7 additions & 4 deletions sote/libsote/debug-control-panel.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
local dcp = {}

dcp.align_to_sote_coords = false -- this will align hex world storage to match the order from original sote, very useful when debugging/validating port
dcp.map_tiles_from_file = false -- this will load cube world tile IDs mapping to hex coordinates from a file, as it's faster than computing them from lat lon
dcp.map_tiles_from_file = false -- this will load cube world tile IDs mapping to hex from a file, as it's faster than computing them from lat lon
dcp.use_sote_climate_data = false -- climate model was ported from sote, but with some changes; this will enable import of original sote climate data from a csv file, to aid in debugging/validating port
dcp.use_sote_ice_data = false -- this will enable import of original sote ice data from a csv file, to aid in debugging/validating port
dcp.align_to_sote_coords = dcp.use_sote_climate_data or dcp.use_sote_ice_data -- this will align hex world storage to match the order from original sote, very useful when debugging/validating port

dcp.save_maps = false -- this will export maps to PNG
dcp.maps_selection = {
Expand All @@ -11,7 +12,9 @@ dcp.maps_selection = {
climate = false,
waterflow = false,
waterbodies = false,
debug = false
watersheds = false,
debug1 = false,
debug2 = false
}

-- seed = 58738 -- climate integration was done on this one
Expand All @@ -20,7 +23,7 @@ dcp.maps_selection = {
-- seed = 6618 -- tiny islands?
-- seed = 49597 -- interesting looking one, huge northern ice cap (with lua climate model)
-- seed = 91170 -- huge lake
dcp.fixed_seed = nil
-- dcp.fixed_seed = nil
-- dcp.fixed_seed = 12177

return dcp
24 changes: 14 additions & 10 deletions sote/libsote/debug-loggers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@ function logger:log(message, do_flush)
end
end

local loggers = {}

local latlon_logger = nil
local neighbors_logger = nil
local waterflow_logger = nil
local parent_material_logger = nil
local glacial_logger = nil
local climate_logger = nil
local lakes_logger = nil

local function get_logger(logger_instance, logname, path, unique)
if logger_instance == nil then
logger_instance = logger:new(logname, path, unique)
Expand All @@ -62,32 +52,46 @@ local function get_logger(logger_instance, logname, path, unique)
return logger_instance
end

local loggers = {}

local latlon_logger = nil
function loggers.get_latlon_logger(path)
return get_logger(latlon_logger, "latlon", path)
end

local neighbors_logger = nil
function loggers.get_neighbors_logger(path)
return get_logger(neighbors_logger, "neighbours", path)
end

local waterflow_logger = nil
function loggers.get_waterflow_logger(path)
return get_logger(waterflow_logger, "waterflow", path)
end

local parent_material_logger = nil
function loggers.get_parent_material_logger(path)
return get_logger(parent_material_logger, "parent_material", path)
end

local glacial_logger = nil
function loggers.get_glacial_logger(path)
return get_logger(glacial_logger, "glacial", path)
end

local climate_logger = nil
function loggers.get_climate_logger(path)
return get_logger(climate_logger, "climate", path)
end

local lakes_logger = nil
function loggers.get_lakes_logger(path)
return get_logger(lakes_logger, "lakes", path)
end

local rivers_logger = nil
function loggers.get_rivers_logger(path)
return get_logger(rivers_logger, "rivers", path)
end

return loggers
1 change: 1 addition & 0 deletions sote/libsote/glaciation/glacial-formation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ function gf.run(world_obj)

if enable_debug then
world:adjust_debug_channels(2)
world:reset_debug_all()
end

world:fill_ffi_array(glacial_seed, false)
Expand Down
4 changes: 2 additions & 2 deletions sote/libsote/heap-sort.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ end
local ffi = require("ffi")

---@param get_primary fun(i:number):any
---@param get_secondary fun(i:number):any
---@param get_secondary nil|fun(i:number):any
---@param n number
---@param desc_primary boolean
---@param desc_secondary boolean
---@param desc_secondary boolean|nil
function hs.heap_sort_indices(get_primary, get_secondary, n, desc_primary, desc_secondary)
desc_primary = desc_primary or false
desc_secondary = desc_secondary or false
Expand Down
2 changes: 0 additions & 2 deletions sote/libsote/hydrology/calculate-waterflow.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ end

local function clear_current_elevation_on_lakes(world)
world:for_each_waterbody(function(wb)
if not wb:is_valid() then return end

if wb.type == wb.TYPES.freshwater_lake or wb.type == wb.TYPES.saltwater_lake then
wb.tmp_float_1 = 0
end
Expand Down
29 changes: 13 additions & 16 deletions sote/libsote/hydrology/gen-dynamic-lakes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ local dl = {}

local world

local waterbody = require "libsote.hydrology.waterbody"
local open_issues = require "libsote.hydrology.open-issues"

-- local logger = require("libsote.debug-loggers").get_lakes_logger("d:/temp")

local prof = require "libsote.profiling-helper"
local prof_prefix = "[gen-dynamic-lakes]"

local function run_with_profiling(func, log_txt)
prof.run_with_profiling(func, prof_prefix, log_txt)
end
Expand Down Expand Up @@ -112,7 +113,7 @@ local function water_flow_from_tile_to_tile()
if world:get_waterbody_by_tile(ti) then goto continue1 end

--* If elevation difference is 0, it can be inferred that we have no tiles lower than the target tile, therefore it should construct a lake.
local new_wb = world:create_new_waterbody_from_tile(ti)
local new_wb = world:create_waterbody_from_tile(ti, waterbody.TYPES.saltwater_lake)

--* Mark the tile as water
world.is_land[ti] = false
Expand All @@ -125,7 +126,6 @@ local function water_flow_from_tile_to_tile()
new_wb:set_lowest_shore_tile(world)
new_wb.tmp_float_1 = new_wb.tmp_float_1 + water_to_give
new_wb.water_level = true_elevation_for_waterflow
new_wb.type = new_wb.TYPES.saltwater_lake

-- logger:log("\tlake " .. new_wb.id .. " created at " .. ti)

Expand All @@ -139,7 +139,7 @@ local function add_lowest_shore_tile_to_waterbody(wb, lsti)
wb.tmp_float_1 = wb.tmp_float_1 + world.tmp_float_2[lsti] / lake_divisor --* If a tile gets eaten by a lake, we automatically move any water that was in the tile to the lake
world.tmp_float_2[lsti] = 0

world:add_tile_to_waterbody(wb, lsti)
world:add_tile_to_waterbody(lsti, wb)

local true_elevation_for_waterflow = world:true_elevation_for_waterflow(lsti)
world:for_each_neighbor(lsti, function(nti)
Expand Down Expand Up @@ -210,13 +210,12 @@ local function manage_expansion_and_drainage(wb, water_to_disburse)

--* Drain lake into shore tile with low neighbor that is not the same waterbody
if has_lower_neigh then
-- local log_str = "\t\t\tlake " .. wb.id .. " draining into tile " .. lowest_shore_ti
local lsti_wb = world:get_waterbody_by_tile(lowest_shore_ti)
if lsti_wb then
-- logger:log(log_str .. " which belongs to lake " .. lsti_wb.id)
-- if lsti_wb then
-- logger:log("\t\t\tlake " .. wb.id .. " draining into tile " .. lowest_shore_ti .. " which belongs to lake " .. lsti_wb.id)
-- else
-- logger:log(log_str)
end
-- logger:log("\t\t\tlake " .. wb.id .. " draining into tile " .. lowest_shore_ti)
-- end

wb.lake_open = true
wb.type = wb.TYPES.freshwater_lake
Expand All @@ -241,8 +240,6 @@ local function resize_lakes()
--* Loop through all waterbodies. Check for active, and check for tempWater.

world:for_each_waterbody(function(wb)
-- if not wb:is_valid() then return end -- do we really need this check?

if wb.tmp_float_1 <= 0 or wb.type == wb.TYPES.ocean then return end
--* Only resize lakes and seas

Expand Down Expand Up @@ -333,14 +330,16 @@ function dl.run(world_obj)
run_with_profiling(function() water_flow_phase() end, "water_flow_phase")

world:for_each_waterbody(function(wb)
if not wb:is_valid() then return end
-- logger:log("lake " .. wb.id .. " (" .. wb:size() .. ", " .. wb.water_level .. ")")
for _, ti in ipairs(wb.tiles) do

wb:for_each_tile(function(ti)
world.is_land[ti] = false
end
end)
end)
end

return dl

--* River Plan ///
--* Possibly generate wetlands first, so that when rivers flow through them, they can then have an "out tile" similar to a lake. The difference is that you don't
--* have "standing water" like a lake. Instead, its more like a broad, slow moving, shallow river.
Expand All @@ -359,5 +358,3 @@ end
--* Check all endoric waterbodies, including oceans, seas, and saltwater lakes
--* Check shoreline of those waterbodies and check for tiles with watermovement reaching a particular threshold.
--* Build a list and follow the waterflow backward from low elevation to high. Stop once the river forks.

return dl
5 changes: 3 additions & 2 deletions sote/libsote/hydrology/gen-initial-waterbodies.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local giw = {}

local waterbody = require "libsote.hydrology.waterbody"
local queue = require("engine.queue"):new()

local waterbodies_created = 0
Expand All @@ -11,7 +12,7 @@ local function process(tile_index, world)

-- "no ice" check is skipped for now

local new_wb = world:create_new_waterbody_from_tile(tile_index)
local new_wb = world:create_waterbody_from_tile(tile_index, waterbody.TYPES.ocean) -- everything seems to start out as oceans?
waterbodies_created = waterbodies_created + 1

queue:enqueue(tile_index)
Expand All @@ -22,7 +23,7 @@ local function process(tile_index, world)
world:for_each_neighbor(ti, function(nti)
if world.is_land[nti] or world:is_tile_waterbody_valid(nti) then return end

world:add_tile_to_waterbody(new_wb, nti)
world:add_tile_to_waterbody(nti, new_wb)

queue:enqueue(nti)
end)
Expand Down
Loading