cmake_minimum_required(VERSION 4.0)

project(stonehearth_like_sim VERSION 0.1.0 LANGUAGES C CXX)

if(NOT WIN32)
    message(FATAL_ERROR "Stonehearth-like Simulation Engine requires a Windows target.")
endif()

include(CTest)
if(BUILD_TESTING AND (NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_VERSION VERSION_LESS 16))
    message(FATAL_ERROR "The reflection test suites need GCC 16 or later. Set BUILD_TESTING=OFF to build the game alone.")
endif()

include(FetchContent)
FetchContent_Declare(void_library
    GIT_REPOSITORY https://git.faumaray.com/Void/Library
    GIT_TAG 7b0fea6095f603f9e1904f66dfdc7752f4be4792
    SYSTEM
    EXCLUDE_FROM_ALL
)

function(add_void_library)
    set(BUILD_SHARED_LIBS OFF)
    set(VOID_ENABLE_TESTING OFF)
    set(VOID_ENABLE_BENCHMARKS OFF)
    set(VOID_ENABLED_MODULES Algorithms Async Binary Core Crypto Data Math Text Time)
    set(CMAKE_DISABLE_FIND_PACKAGE_OpenSSL ON)
    if(BUILD_TESTING)
        list(APPEND VOID_ENABLED_MODULES Testing)
    endif()
    FetchContent_MakeAvailable(void_library)
endfunction()
add_void_library()

if(BUILD_TESTING AND NOT VOID_HAS_CXX26_REFLECTION)
    message(FATAL_ERROR "The reflection test suites need working C++26 reflection support.")
endif()

find_package(Threads REQUIRED)

add_library(settlement_core
    engine/src/checkpoint.cpp
    engine/src/checkpoint_files.cpp
    engine/src/command_dispatcher.cpp
    engine/src/first_colony.cpp
    engine/src/rules_manifest.cpp
    engine/src/save_service_win32.cpp
    engine/src/simulation_runtime.cpp
    engine/src/worker_pool.cpp
    engine/src/world.cpp
    engine/src/world_save.cpp
)

target_include_directories(settlement_core
    PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}/engine/include
)

target_compile_features(settlement_core PUBLIC cxx_std_23)
target_link_libraries(settlement_core
    PUBLIC Threads::Threads Void::Async Void::Core Void::Time
    PRIVATE Void::Algorithms Void::Binary Void::Crypto Void::Data Void::Math Void::Text
)

if(MSVC)
    target_compile_options(settlement_core PRIVATE /W4 /permissive-)
else()
    target_compile_options(settlement_core PRIVATE -Wall -Wextra -Wpedantic -Wconversion -Wshadow)
endif()

add_executable(settlement_sim
    app/main.cpp
)

target_link_libraries(settlement_sim PRIVATE settlement_core)

add_executable(settlement_colony_benchmark benchmarks/colony.cpp)
target_link_libraries(settlement_colony_benchmark PRIVATE settlement_core)

add_library(settlement_lan STATIC
    app/lan_session_win32.cpp
    app/lan_wire.cpp
)
target_include_directories(settlement_lan PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/app)
target_link_libraries(settlement_lan
    PUBLIC settlement_core
    PRIVATE Void::Binary ws2_32 bcrypt iphlpapi
)

enable_language(RC)
add_executable(settlement_viewer WIN32
    app/d3d_scene_win32.cpp
    app/viewer_win32.cpp
    app/house_icons.rc
)
target_link_libraries(settlement_viewer PRIVATE settlement_lan user32 gdi32 comctl32 comdlg32 d3d11 d3dcompiler)
if(MSVC)
    target_compile_options(settlement_lan PRIVATE /W4 /permissive-)
    target_compile_options(settlement_viewer PRIVATE $<$<COMPILE_LANGUAGE:CXX>:/W4;/permissive->)
else()
    target_compile_options(settlement_lan PRIVATE -Wall -Wextra -Wpedantic -Wconversion -Wshadow)
    target_link_options(settlement_viewer PRIVATE -municode)
    target_compile_options(settlement_viewer PRIVATE
        $<$<COMPILE_LANGUAGE:CXX>:-Wall;-Wextra;-Wpedantic;-Wconversion;-Wshadow>)
endif()

if(MSVC)
    target_compile_options(settlement_sim PRIVATE /W4 /permissive-)
else()
    target_compile_options(settlement_sim PRIVATE -Wall -Wextra -Wpedantic -Wconversion -Wshadow)
endif()

if(BUILD_TESTING)
    add_executable(settlement_core_tests
        tests/checkpoint_tests.cpp
        tests/checkpoint_file_tests.cpp
        tests/core_tests.cpp
        tests/executor_tests.cpp
        tests/identity_tests.cpp
        tests/replay_tests.cpp
        tests/rules_tests.cpp
        tests/runtime_tests.cpp
        tests/save_service_tests.cpp
        tests/session_tests.cpp
        tests/snapshot_tests.cpp
        tests/test_main.cpp
    )
    target_link_libraries(settlement_core_tests PRIVATE settlement_core Void::Testing)
    add_test(NAME settlement_core_tests COMMAND settlement_core_tests)

    add_executable(settlement_lan_tests tests/lan_tests.cpp tests/test_main.cpp)
    target_link_libraries(settlement_lan_tests PRIVATE settlement_lan Void::Testing)
    add_test(NAME settlement_lan_tests COMMAND settlement_lan_tests)
endif()
