# ---------------------------------------------------------------------------
# demo-taxi — taxi driver demo: procedurally generated town, drivable sedans,
# passenger/foot-traffic/vehicle-traffic NPCs, chained taxi fares
# ---------------------------------------------------------------------------
# Starts as an exact copy of demos/demo-interior-exterior-dynamics (same
# scene-switch-door setup, same streaming-terrain/sky/water exterior at
# world center ~(4000, 4000)); the taxi game (town generation, sedans,
# NPC traffic, fare chain) is built step by step on top of it - see
# PLAN.md in this directory.  Vehicle ideas come from demos/demo-sokoban
# (Jolt VehicleConstraint, VehicleControllerSystem enter/exit flow).
#
# The executable is self-contained in this build directory: a POST_BUILD
# step (stage_runtime.cmake) copies resources.cfg, the
# character prefab (prefabs/char_2.json, written by a previous editor/game
# run) and any runtime config JSONs here, and symlinks both demo scenes
# (demo_scene_interior.json / demo_scene_exterior.json, so scene edits in
# the source tree are visible without a rebuild) plus the big pre-staged
# runtime directories (resources/, characters/, lua-scripts/) from the
# editScene binary directory, plus the runtime-written per-terrain data
# directories (roads/, terrain_fixup/, heightmap.bin_blendmaps/) from
# the source tree so saves land in versioned files directly; the
# terrain heightmap is staged separately by configure_file (see below)
# so source changes re-copy it on the next build.  Run it from here:
#   cd <build>/src/features/editScene/demos/demo-taxi
#   ./demoTaxi
# Controls: mouse = look, W/A/S/D = move, Shift = run, E = use door,
# Escape = pause menu (frees the cursor).

get_filename_component(EDITSCENE_SOURCE_DIR
	"${CMAKE_CURRENT_SOURCE_DIR}/../.." ABSOLUTE)
get_filename_component(EDITSCENE_BINARY_DIR
	"${CMAKE_CURRENT_BINARY_DIR}/../.." ABSOLUTE)

# Reuse the editScene sources, swapping main.cpp for demo_main.cpp.
set(DEMO_SOURCES ${EDITSCENE_SOURCES})
list(REMOVE_ITEM DEMO_SOURCES main.cpp)
list(TRANSFORM DEMO_SOURCES PREPEND "${EDITSCENE_SOURCE_DIR}/")

# --- Embedded project configuration (F8 release binary) ---------------
# Read project.json at configure time and embed its parameters into the
# binary via a generated project.h, so the release binary is attached to
# its project directory without needing --project.  Editing project.json
# re-triggers the CMake configure step (CMAKE_CONFIGURE_DEPENDS).
set(PROJECT_JSON_PATH "${CMAKE_CURRENT_SOURCE_DIR}/project.json")
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
	"${PROJECT_JSON_PATH}")
file(READ "${PROJECT_JSON_PATH}" PROJECT_JSON_TEXT)
string(JSON PROJECT_APP_NAME GET "${PROJECT_JSON_TEXT}" appName)
string(JSON PROJECT_START_SCENE GET "${PROJECT_JSON_TEXT}" startScene)
string(JSON PROJECT_GAME_MODE GET "${PROJECT_JSON_TEXT}" gameMode)
if(PROJECT_GAME_MODE)
	set(PROJECT_GAME_MODE_VALUE 1)
else()
	set(PROJECT_GAME_MODE_VALUE 0)
endif()
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/project.h.in"
	"${CMAKE_CURRENT_BINARY_DIR}/generated/project.h" @ONLY)

# Terrain heightmap for the exterior scene's terrain entity (terrainId
# 4242424300000001, heightmapFile heightmap.bin — TerrainSystem resolves it
# as heightmaps/<terrainId>/<heightmapFile> relative to the CWD).  Generated
# in the source tree by gen_heightmap.py.  The terrain runs in streaming
# mode (base heights come from baseNoise, not the heightmap), but the file
# stays staged so flipping streamingEnabled off keeps working.  A
# configure_file COPYONLY copy is used on purpose: it registers a configure
# dependency on the source file, so regenerating heightmap.bin re-copies it
# into the staging directory on the next build without manual intervention.
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/heightmap.bin"
	"${CMAKE_CURRENT_BINARY_DIR}/heightmaps/4242424300000001/heightmap.bin"
	COPYONLY)

# Traffic vehicle prefab (phase 5.4): demo-owned, generated by
# extract_sedan_prefab.py from the vehicletest scene's sedan1 subtree and
# versioned with the demo.  configure_file COPYONLY like the heightmap:
# regenerating the prefab re-copies it into the staging directory on the
# next build without manual intervention.
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/prefabs/sedan-traffic.json"
	"${CMAKE_CURRENT_BINARY_DIR}/prefabs/sedan-traffic.json"
	COPYONLY)

# Town layout library (phase 5.9): tagged/scored building layout
# templates loaded by TownGenSystem::loadLayoutLibrary() from the CWD.
# Staged as a symlink to the source file, not a copy: the Layout
# Library editor's Save writes through the link straight into the
# source tree (plain ofstream trunc, no rename), so there is no
# sync-back step and no stale staged copy.  The rm -f clears the
# regular file left behind by the previous configure_file staging.
execute_process(COMMAND ${CMAKE_COMMAND} -E rm -f
	"${CMAKE_CURRENT_BINARY_DIR}/town_layouts.txt")
execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink
	"${CMAKE_CURRENT_SOURCE_DIR}/town_layouts.txt"
	"${CMAKE_CURRENT_BINARY_DIR}/town_layouts.txt")

# Per-terrain runtime data directories (roads/, terrain_fixup/,
# heightmap.bin_blendmaps/ under heightmaps/4242424300000001/) are
# SYMLINKED to the source tree by stage_runtime.cmake at build time,
# like the demo scenes: editor/game saves (road regions, lot params,
# terrain fixups, blend maps) write straight into the versioned source
# files, so there is no sync-back step and no staged copy a reconfigure
# could overwrite.  Commit new/changed files after editing.

add_executable(demoTaxi
	demo_main.cpp
	${DEMO_SOURCES}
)

target_compile_definitions(demoTaxi
	PRIVATE EDITSCENE_HAS_EMBEDDED_PROJECT)

add_dependencies(demoTaxi morph)

# Define JPH_DEBUG_RENDERER for physics debug drawing (same as editor)
target_compile_definitions(demoTaxi PRIVATE JPH_DEBUG_RENDERER)

target_link_libraries(demoTaxi
	OgreMain
	OgreBites
	OgreOverlay
	OgreMeshLodGenerator
	OgrePaging
	OgreTerrain
	flecs::flecs_static
	nlohmann_json::nlohmann_json
	Jolt::Jolt
	OgreProcedural::OgreProcedural
	RecastNavigation::Recast
	RecastNavigation::Detour
	RecastNavigation::DetourTileCache
	RecastNavigation::DetourCrowd
	RecastNavigation::DebugUtils
	PackageArchive
	RoadGeometryLib
	lua
	SDL2::SDL2
)

target_include_directories(demoTaxi PRIVATE
	${CMAKE_CURRENT_BINARY_DIR}/generated
	${EDITSCENE_SOURCE_DIR}
	${EDITSCENE_SOURCE_DIR}/recastnavigation/Recast/Include
	${EDITSCENE_SOURCE_DIR}/recastnavigation/Detour/Include
	${EDITSCENE_SOURCE_DIR}/recastnavigation/DetourTileCache/Include
	${EDITSCENE_SOURCE_DIR}/recastnavigation/DetourCrowd/Include
	${EDITSCENE_SOURCE_DIR}/recastnavigation/DebugUtils/Include
	${CMAKE_SOURCE_DIR}/src/FastNoiseLite
	${CMAKE_SOURCE_DIR}/src/lua/lua-5.4.8/src
	${CMAKE_SOURCE_DIR}/src/lua/lpeg-1.1.0
)

# Stage the standalone runtime next to the executable (see header comment).
add_custom_command(TARGET demoTaxi POST_BUILD
	COMMAND ${CMAKE_COMMAND}
		-DDEMO_DIR=${CMAKE_CURRENT_BINARY_DIR}
		-DEDITSCENE_BIN=${EDITSCENE_BINARY_DIR}
		-DEDITSCENE_SRC=${EDITSCENE_SOURCE_DIR}
		-DSRC_DIR=${CMAKE_CURRENT_SOURCE_DIR}
		-P "${CMAKE_CURRENT_SOURCE_DIR}/stage_runtime.cmake"
	COMMENT "Staging demo-taxi standalone runtime"
)
