cmake_minimum_required(VERSION 3.13.0)
project(world2)

set(CMAKE_CXX_STANDARD 17)
set(BLENDER "${CMAKE_SOURCE_DIR}/../../blender-bin/bin/blender" CACHE STRING "Blender path")
set(CREATE_DIRECTORIES
	${CMAKE_BINARY_DIR}/assets/blender/shapes/male
	${CMAKE_BINARY_DIR}/assets/blender/shapes/female
	${CMAKE_BINARY_DIR}/assets/blender/scripts
	${CMAKE_BINARY_DIR}/characters/shapes/male/chibi
	${CMAKE_BINARY_DIR}/characters/shapes/female/chibi
	${CMAKE_BINARY_DIR}/characters/male
	${CMAKE_BINARY_DIR}/characters/female)
# workaround horribly broken assimp cmake, fixed with assimp 5.1
#add_library(assimp INTERFACE IMPORTED)
#set_target_properties(assimp PROPERTIES
#       INTERFACE_LINK_LIBRARIES "assimp;pugixml"
#       # INTERFACE_LINK_DIRECTORIES "${ASSIMP_LIBRARY_DIRS}"
#       INTERFACE_LINK_DIRECTORIES "${CMAKE_PREFIX_PATH}/lib"
#)
file(GLOB WATER_SRC ${CMAKE_SOURCE_DIR}/water/*.cpp)

# The COMPONENTS part checks that OGRE was built the way we need it
# The CONFIG flag makes sure we get OGRE instead of OGRE-next
find_package(OGRE REQUIRED COMPONENTS Bites Paging Terrain CONFIG)
find_package(ZLIB)
find_package(SDL2)
find_package(assimp REQUIRED CONFIG)
find_package(OgreProcedural REQUIRED CONFIG)
find_package(pugixml REQUIRED CONFIG)
find_package(flecs REQUIRED CONFIG)
find_package(Tracy REQUIRED CONFIG)

add_library(fix::assimp INTERFACE IMPORTED)
set_target_properties(fix::assimp PROPERTIES
       INTERFACE_LINK_LIBRARIES "${ASSIMP_LIBRARIES};pugixml"
       INTERFACE_LINK_DIRECTORIES "${ASSIMP_LIBRARY_DIRS}"
#       INTERFACE_LINK_DIRECTORIES "${CMAKE_PREFIX_PATH}/lib"
)

add_library(fix::OgreProcedural INTERFACE IMPORTED)
set_target_properties(fix::OgreProcedural PROPERTIES
       INTERFACE_LINK_LIBRARIES "OgreProcedural"
       INTERFACE_LINK_DIRECTORIES "${CMAKE_PREFIX_PATH}/lib"
)

if(OGRE_STATIC)
add_library(OgreGLSupportStatic INTERFACE IMPORTED)
set_target_properties(OgreGLSupportStatic PROPERTIES
       INTERFACE_LINK_LIBRARIES "OgreGLSupportStatic"
       INTERFACE_LINK_DIRECTORIES "${CMAKE_PREFIX_PATH}/lib"
)
endif()

add_library(fix::pugixml INTERFACE IMPORTED)
set_target_properties(fix::pugixml PROPERTIES
       INTERFACE_LINK_LIBRARIES "pugixml"
       INTERFACE_LINK_DIRECTORIES "${CMAKE_PREFIX_PATH}/lib"
)



add_subdirectory(src/lua)
add_subdirectory(src/gamedata)
add_subdirectory(src/miniaudio)
add_subdirectory(src/sound)
add_subdirectory(src/sceneloader)
add_subdirectory(audio/gui)
add_subdirectory(lua-scripts)
add_subdirectory(morph)
add_subdirectory(src/world)
add_subdirectory(src/tests)
add_subdirectory(src/physics)
add_subdirectory(src/editor)
add_subdirectory(src/crowd)
add_subdirectory(assets/blender/vehicles)
add_subdirectory(assets/blender/buildings/parts)
add_subdirectory(assets/blender/characters)
add_subdirectory(resources)
add_subdirectory(src/text_editor)
add_subdirectory(src/features)

add_executable(Game Game.cpp ${WATER_SRC})
target_include_directories(Game PRIVATE src/gamedata)
target_link_libraries(Game OgreBites OgrePaging OgreTerrain OgreMeshLodGenerator
	OgreProcedural::OgreProcedural
        OgreCrowd
	GameData
	sound
        sceneloader physics lua
	flecs::flecs_static
        Tracy::TracyClient
	-Wl,--as-needed
	)
if(OGRE_STATIC)
target_link_options(Game PRIVATE -static-libstdc++ -static-libgcc)
endif()
add_dependencies(Game stage_files import_buildings import_water_stuff import_vehicles import_vrm audio_data_gui)

add_executable(Procedural Procedural.cpp)
target_link_libraries(Procedural OgreBites OgrePaging OgreTerrain
	OgreProcedural::OgreProcedural
	-Wl,--as-needed
	)
if(OGRE_STATIC)
target_link_options(Procedural PRIVATE -static-libstdc++ -static-libgcc)
endif()
add_dependencies(Procedural stage_files import_buildings)
file(GLOB FONTS_SRC ${CMAKE_SOURCE_DIR}/assets/fonts/*.ttf)
set(FONT_OUTPUT_FILES)
foreach(FONT_FILE ${FONTS_SRC})
	get_filename_component(FILE_NAME ${FONT_FILE} NAME_WE)
	set(FONT_OUTPUT_FILE ${CMAKE_BINARY_DIR}/resources/fonts/${FILE_NAME}.ttf)
	add_custom_command(
		OUTPUT ${FONT_OUTPUT_FILE}
		COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/resources/fonts
		COMMAND ${CMAKE_COMMAND} -E copy ${FONT_FILE} ${FONT_OUTPUT_FILE}
		DEPENDS ${FONT_FILE})
	list(APPEND FONT_OUTPUT_FILES ${FONT_OUTPUT_FILE})
endforeach()
add_custom_target(fonts ALL DEPENDS ${FONT_OUTPUT_FILES})
file(GLOB BUILDINGS_SRC ${CMAKE_SOURCE_DIR}/assets/blender/buildings/*.blend)
set(BUILDING_OUTPUT_FILES)
foreach(BUILDING_FILE ${BUILDINGS_SRC})
	get_filename_component(FILE_NAME ${BUILDING_FILE} NAME_WE)
	set(BUILDING_OUTPUT_FILE ${CMAKE_BINARY_DIR}/resources/buildings/${FILE_NAME}.gltf)
	add_custom_command(
		OUTPUT ${BUILDING_OUTPUT_FILE}
		COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/resources/buildings
		COMMAND ${BLENDER} ${BUILDING_FILE}
			-b -Y -P
			${CMAKE_SOURCE_DIR}/assets/blender/scripts/export_buildings.py
			-- ${BUILDING_OUTPUT_FILE}
		COMMAND touch ${BUILDING_OUTPUT_FILE}
		DEPENDS ${BUILDING_FILE})
	list(APPEND BUILDING_OUTPUT_FILES ${BUILDING_OUTPUT_FILE})
endforeach()

add_custom_target(import_buildings ALL DEPENDS ${BUILDING_OUTPUT_FILES})

set(CHARACTER_SHAPES_SRC edited-normal-male-base.blend edited-shape-test-male.blend)
set(CHARACTER_SHAPES_OUTPUT_FILES)
foreach(CHARACTER_SHAPE_FILE ${CHARACTER_SHAPES_SRC})
	get_filename_component(FILE_NAME ${CHARACTER_SHAPE_FILE} NAME_WE)
	set(CHARACTER_SHAPE_OUTPUT_FILE ${CMAKE_BINARY_DIR}/characters/shapes/male/${FILE_NAME}/${FILE_NAME}.scene)
	add_custom_command(
		OUTPUT ${CHARACTER_SHAPE_OUTPUT_FILE}
		COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/assets/blender
		COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/characters/shapes/male/${FILE_NAME}
		COMMAND ${BLENDER} ${CMAKE_SOURCE_DIR}/assets/blender/${CHARACTER_SHAPE_FILE}
			-b -Y -P
			${CMAKE_SOURCE_DIR}/assets/blender/scripts/export_characters_ogre.py
			-- ${CHARACTER_SHAPE_OUTPUT_FILE}
			COMMAND touch ${CHARACTER_SHAPE_OUTPUT_FILE}
			DEPENDS ${CMAKE_SOURCE_DIR}/assets/blender/${CHARACTER_SHAPE_FILE})
		list(APPEND CHARACTER_SHAPES_OUTPUT_FILES ${CHARACTER_SHAPE_OUTPUT_FILE})
endforeach()
add_custom_target(import_character_shapes ALL DEPENDS ${CHARACTER_SHAPES_OUTPUT_FILES})

set(WATER_STUFF)
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/water/sea.glb
	COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/water
	COMMAND ${BLENDER} ${CMAKE_SOURCE_DIR}/assets/blender/sea.blend
		-b -Y -P
		${CMAKE_SOURCE_DIR}/assets/blender/scripts/export_buildings.py
		-- ${CMAKE_BINARY_DIR}/water/sea.glb
	COMMAND touch ${CMAKE_BINARY_DIR}/water/sea.glb
	DEPENDS ${CMAKE_SOURCE_DIR}/assets/blender/sea.blend)
list(APPEND WATER_STUFF ${CMAKE_BINARY_DIR}/water/sea.glb)
add_custom_target(import_water_stuff ALL DEPENDS ${WATER_STUFF})

add_executable(TerrainTest terrain.cpp)
target_link_libraries(TerrainTest OgreBites OgrePaging OgreTerrain lua
	-Wl,--as-needed
	)
target_include_directories(TerrainTest PRIVATE . src/terrain src/lua src/lua/lua-5.4.8/src)
if(OGRE_STATIC)
	target_link_libraries(TerrainTest fix::assimp  pugixml)
	target_link_options(TerrainTest PRIVATE -static-libstdc++ -static-libgcc)
	target_link_libraries(Procedural fix::assimp pugixml)
endif()

add_dependencies(TerrainTest stage_lua_scripts stage_files)

add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/stories
	COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_BINARY_DIR}/lua-scripts/stories ${CMAKE_BINARY_DIR}/stories
	DEPENDS ${CMAKE_BINARY_DIR}/lua-scripts/stories
)
add_custom_target(stage_stories ALL DEPENDS stage_lua_scripts ${CMAKE_BINARY_DIR}/stories)

add_custom_command(
	OUTPUT ${CMAKE_BINARY_DIR}/resources.cfg
        COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/resources.cfg ${CMAKE_BINARY_DIR}/resources.cfg
DEPENDS ${CMAKE_SOURCE_DIR}/resources.cfg)

set(SKYBOX_SRC
	early_morning_bk.jpg
	early_morning_dn.jpg
	early_morning_fr.jpg
	early_morning_lf.jpg
	early_morning_rt.jpg
	early_morning_up.jpg
	skybox.material
)
set(DEBUG_MATERIAL_SRC
    debug.frag
	debug.material
	debug.program
	debug.vert
)
set(WATER_SRC
	water.material
	water.program
	water.frag
	water.vert
	depth.frag
	depth.vert
	water.compositor
	waves2.png
)
set(MATERIAL_FILES)
foreach(MATERIAL_FILE ${SKYBOX_SRC})
		list(APPEND MATERIAL_FILES skybox/${MATERIAL_FILE})
endforeach()
foreach(MATERIAL_FILE ${DEBUG_MATERIAL_SRC})
		list(APPEND MATERIAL_FILES resources/debug/${MATERIAL_FILE})
endforeach()
foreach(MATERIAL_FILE ${WATER_SRC})
		list(APPEND MATERIAL_FILES water/${MATERIAL_FILE})
endforeach()



set(MATERIALS_OUTPUT)
foreach(MATERIAL_FILE ${MATERIAL_FILES})
		set(OUTPUT_FILE ${CMAKE_BINARY_DIR}/${MATERIAL_FILE})
		set(INPUT_FILE ${CMAKE_SOURCE_DIR}/${MATERIAL_FILE})
		add_custom_command(OUTPUT ${OUTPUT_FILE}
			COMMAND ${CMAKE_COMMAND} -E copy  ${INPUT_FILE} ${OUTPUT_FILE}
			DEPENDS ${INPUT_FILE})
		list(APPEND MATERIALS_OUTPUT ${OUTPUT_FILE})
endforeach()

add_custom_target(stage_files ALL DEPENDS ${CMAKE_BINARY_DIR}/resources.cfg ${MATERIALS_OUTPUT}
	${CMAKE_BINARY_DIR}/resources/terrain/world_map.png
	${CMAKE_BINARY_DIR}/resources/terrain/brushes.png
        edited-blends stage_resources)

add_custom_target(remove_scenes COMMAND rm -f ${VRM_SOURCE} ${VRM_IMPORTED_BLENDS} ${CHARACTER_GLBS})

target_compile_definitions(Game PRIVATE FLECS_CPP_NO_AUTO_REGISTRATION JPH_PROFILE_ENABLED JPH_DEBUG_RENDERER JPH_PROFILE_ENABLED JPH_DOUBLE_PRECISION)

install(TARGETS Game DESTINATION bin)
install(TARGETS Editor DESTINATION bin)

