planetsim/CMakeLists.txt
Jonas Reith 69f3aa2d22 Fix unbounded sea-level sinking on long runs
Reported after a real long-running world (elapsedMy ~1340) showed seaLevel
drifted to -2900m, with "land" at -2000m next to "ocean" at -4000m.

Root cause: adjustSeaLevel()'s geographic land target (elevation above
seaLevel, fixed 30%) and advect()'s crust-type land conservation
(targetLand/landBand, held near whatever Phase-1 forming settled at for
that seed) are two independent notions of "land" that aren't guaranteed to
agree -- this seed's continental crust settled at only ~26%. With genuine
land structurally short of the target, the controller had no lower bound
and kept sinking seaLevel to misclassify progressively older, deeper
oceanic crust as land -- and since that crust keeps ageing and deepening
even at a fixed seaLevel, it was chasing a moving target with no way to
ever settle.

Added seaLevelMin/seaLevelMax config fields (default +-3000m) that clamp
adjustSeaLevel()'s candidate nudge, so a world that can't reach the target
land fraction settles at a plausible offset instead of an unbounded one.
New test_sealevel.cpp reproduces the pathology with a continuous elevation
distribution (not a synthetic cliff) and confirms the controller still
engages but never crosses either bound.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TMfyZv91tonDnPJqbaTVJE
2026-08-31 19:41:25 +02:00

118 lines
3.8 KiB
CMake

cmake_minimum_required(VERSION 3.16)
project(PlanetSim CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(FetchContent)
FetchContent_Declare(
raylib
GIT_REPOSITORY https://github.com/raysan5/raylib.git
GIT_TAG 5.5
)
# Build raylib as a static lib, no examples.
set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(BUILD_GAMES OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(raylib)
# raygui: raylib's official header-only companion widget library (buttons, checkboxes,
# dropdowns, sliders, scroll panels -- used by the Toolbar sidebar, src/render/Toolbar.cpp).
# Download-only: it's a single header (src/raygui.h), no build step of its own needed.
FetchContent_Declare(
raygui
GIT_REPOSITORY https://github.com/raysan5/raygui.git
GIT_TAG 5.0
GIT_SHALLOW TRUE
DOWNLOAD_ONLY TRUE
)
FetchContent_MakeAvailable(raygui)
set(SIM_SOURCES
src/sim/IcoSphere.cpp
src/sim/Planet.cpp
src/sim/PlanetTectonics.cpp
src/sim/PlanetDrift.cpp
src/sim/PlanetErosion.cpp
src/sim/PlanetHydrology.cpp
src/sim/PlanetBiomes.cpp
src/sim/PlanetClimate.cpp
src/sim/PlanetLive.cpp
src/sim/PlanetOcean.cpp
src/sim/PlanetWeather.cpp
src/sim/PlanetVolcano.cpp
src/sim/PlanetBiota.cpp
src/sim/PlanetFloraGen.cpp
src/sim/PlanetFaunaGen.cpp
src/sim/PlanetFungiGen.cpp
src/sim/NameGen.cpp
src/sim/PlanetGeography.cpp
src/sim/PlanetEcoregions.cpp
src/sim/PlanetCiv.cpp
src/sim/PlanetNation.cpp
src/sim/PlanetCulture.cpp
src/sim/PlanetConflict.cpp
src/sim/PlanetTrade.cpp
src/sim/PlanetEdit.cpp
src/sim/PlanetIO.cpp
)
set(RENDER_SOURCES
src/render/Colors.cpp
src/render/Map2D.cpp
src/render/Overlays.cpp
src/render/Picking.cpp
src/render/Panels.cpp
src/render/Toolbar.cpp
src/render/MainMenu.cpp
src/render/RayGuiImpl.cpp
src/render/Viewer.cpp
src/render/ViewerInput.cpp
src/render/ViewerRender.cpp
)
add_library(planetsim_sim STATIC ${SIM_SOURCES})
target_include_directories(planetsim_sim PUBLIC src/sim)
add_executable(planetsim
src/main.cpp
${RENDER_SOURCES}
)
# Flat includes ("Planet.hpp", "Viewer.hpp", ...) resolve across both folders.
target_include_directories(planetsim PRIVATE src/sim src/render ${raygui_SOURCE_DIR}/src)
target_link_libraries(planetsim PRIVATE planetsim_sim raylib)
# OpenMP parallelizes the per-cell passes in Planet::step(). Optional: without
# it the #pragma omp lines are ignored and the sim runs (correctly) serial.
find_package(OpenMP)
if(OpenMP_CXX_FOUND)
target_link_libraries(planetsim_sim PUBLIC OpenMP::OpenMP_CXX)
endif()
# Linux system libs raylib needs at link time.
if(UNIX AND NOT APPLE)
target_link_libraries(planetsim PRIVATE m pthread dl)
endif()
enable_testing()
foreach(test_name logic biota ocean live weather volcano geography ecoregions civ nation culture conflict diplomacy trade colony cultevo edit sealevel)
add_executable(test_${test_name} test_${test_name}.cpp)
target_link_libraries(test_${test_name} PRIVATE planetsim_sim)
add_test(NAME ${test_name} COMMAND test_${test_name})
endforeach()
add_executable(test_events test_events.cpp ${RENDER_SOURCES})
target_include_directories(test_events PRIVATE src/sim src/render ${raygui_SOURCE_DIR}/src)
target_link_libraries(test_events PRIVATE planetsim_sim raylib)
if(UNIX AND NOT APPLE)
target_link_libraries(test_events PRIVATE m pthread dl)
endif()
add_test(NAME events COMMAND test_events)
add_executable(test_viewerstate test_viewerstate.cpp ${RENDER_SOURCES})
target_include_directories(test_viewerstate PRIVATE src/sim src/render ${raygui_SOURCE_DIR}/src)
target_link_libraries(test_viewerstate PRIVATE planetsim_sim raylib)
if(UNIX AND NOT APPLE)
target_link_libraries(test_viewerstate PRIVATE m pthread dl)
endif()
add_test(NAME viewerstate COMMAND test_viewerstate)