cmake_minimum_required(VERSION 3.21)
project(brandling_safari LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "" FORCE)
endif()

option(BS_BUILD_CLIENT "Build the game client (needs OpenGL + the HD-2D engine)" ON)
option(BS_BUILD_SERVER "Build the dedicated server" ON)
set(HD2D_ENGINE_DIR "${CMAKE_SOURCE_DIR}/../Vibe-HD-2D" CACHE PATH "Path to the Vibe-HD-2D engine checkout")
set(HD2D_ENGINE_GIT "ssh://git@git.gabjimmy.com:30009/Gabe/Vibe-HD-2D.git" CACHE STRING "Engine repo used when HD2D_ENGINE_DIR is missing")

include(FetchContent)
# Old third-party CMakeLists (ENet) still declare cmake_minimum_required < 3.5.
set(CMAKE_POLICY_VERSION_MINIMUM 3.5)

# --- Dependencies -------------------------------------------------------------
find_package(glm QUIET)
if(NOT TARGET glm::glm)
  FetchContent_Declare(glm GIT_REPOSITORY https://github.com/g-truc/glm.git GIT_TAG 1.0.1)
  FetchContent_MakeAvailable(glm)
endif()

find_package(nlohmann_json 3 QUIET)
if(NOT TARGET nlohmann_json::nlohmann_json)
  FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz)
  FetchContent_MakeAvailable(json)
endif()

FetchContent_Declare(enet GIT_REPOSITORY https://github.com/lsalzman/enet.git GIT_TAG v1.3.18)
FetchContent_MakeAvailable(enet)
target_include_directories(enet PUBLIC ${enet_SOURCE_DIR}/include)

# --- Shared game core (no graphics) ---------------------------------------------
add_library(bs_shared STATIC
  shared/Appearance.cpp shared/Net.cpp shared/Protocol.cpp shared/Roster.cpp shared/Zone.cpp)
target_include_directories(bs_shared PUBLIC shared)
target_link_libraries(bs_shared PUBLIC glm::glm nlohmann_json::nlohmann_json enet)
target_compile_definitions(bs_shared PUBLIC GLM_FORCE_RADIANS GLM_ENABLE_EXPERIMENTAL
  BS_ASSET_DIR="${CMAKE_SOURCE_DIR}/assets")
if(WIN32)
  target_link_libraries(bs_shared PUBLIC ws2_32 winmm)  # ENet sockets + timers
endif()
if(MSVC)
  target_compile_options(bs_shared PRIVATE /W4 /utf-8)
else()
  target_compile_options(bs_shared PRIVATE -Wall -Wextra -Wno-unused-parameter)
endif()
if(MINGW)
  # no libgcc / libstdc++ DLLs to ship next to the executables; C99 printf (%zu) on msvcrt
  add_link_options(-static-libgcc -static-libstdc++)
  target_compile_definitions(bs_shared PUBLIC __USE_MINGW_ANSI_STDIO=1)
endif()

# --- Server -------------------------------------------------------------------
if(BS_BUILD_SERVER)
  find_package(SQLite3 REQUIRED)
  find_package(Threads REQUIRED)
  file(GLOB BS_SERVER_SOURCES CONFIGURE_DEPENDS server/*.cpp)
  add_executable(safari_server ${BS_SERVER_SOURCES})
  if(TARGET SQLite3::SQLite3)  # CMake >= 4.3 name; older releases only define SQLite::SQLite3
    set(BS_SQLITE SQLite3::SQLite3)
  else()
    set(BS_SQLITE SQLite::SQLite3)
  endif()
  target_link_libraries(safari_server PRIVATE bs_shared ${BS_SQLITE} Threads::Threads)
  if(NOT MSVC)
    target_compile_options(safari_server PRIVATE -Wall -Wextra -Wno-unused-parameter)
  endif()
endif()

# --- Headless test bot (no graphics) ------------------------------------------
add_executable(safari_bot bot/main.cpp)
target_link_libraries(safari_bot PRIVATE bs_shared)
if(NOT MSVC)
  target_compile_options(safari_bot PRIVATE -Wall -Wextra -Wno-unused-parameter)
endif()

# --- Client -------------------------------------------------------------------
if(BS_BUILD_CLIENT)
  if(NOT EXISTS "${HD2D_ENGINE_DIR}/CMakeLists.txt")
    FetchContent_Declare(hd2d_engine GIT_REPOSITORY ${HD2D_ENGINE_GIT} GIT_TAG main)
    FetchContent_GetProperties(hd2d_engine)
    if(NOT hd2d_engine_POPULATED)
      FetchContent_Populate(hd2d_engine)
    endif()
    set(HD2D_ENGINE_DIR ${hd2d_engine_SOURCE_DIR})
  endif()
  set(HD2D_BUILD_DEMO OFF CACHE BOOL "" FORCE)
  add_subdirectory(${HD2D_ENGINE_DIR} hd2d)

  file(GLOB BS_CLIENT_SOURCES CONFIGURE_DEPENDS client/*.cpp)
  add_executable(safari_client ${BS_CLIENT_SOURCES})
  target_link_libraries(safari_client PRIVATE bs_shared hd2d)
  if(NOT MSVC)
    target_compile_options(safari_client PRIVATE -Wall -Wextra -Wno-unused-parameter)
  endif()
  if(WIN32)
    # A game, not a console tool: no console window opens next to it.
    set_target_properties(safari_client PROPERTIES WIN32_EXECUTABLE TRUE)
    if(MSVC)
      target_link_options(safari_client PRIVATE /ENTRY:mainCRTStartup)  # keep int main()
    endif()
  endif()
  # The client launches the dedicated server for "Host game" from its own folder.
  if(BS_BUILD_SERVER)
    add_dependencies(safari_client safari_server)
  endif()
endif()
