CC      := cc
CFLAGS  := -std=c99 -Wall -Wpedantic -Werror=implicit -Wmissing-prototypes -O2 -g -Isrc
LDFLAGS :=

GEN_SRC     := $(wildcard src/gen/*.c)
GEN_OBJ     := $(GEN_SRC:src/%.c=obj/%.o)

BLOBBER_SRC := $(wildcard src/blobber/*.c)
BLOBBER_OBJ := $(BLOBBER_SRC:src/%.c=obj/%.o)

MESH_SRC    := $(wildcard src/mesh/*.c)
MESH_OBJ    := $(MESH_SRC:src/%.c=obj/%.o)

.PHONY: all clean genesis genesis3d test stress godot godot-clean map tb-sync
all: genesis genesis3d

# --- TrenchBroom round-trip helpers -----------------------------------------
# Override on the command line: `make map SEED=99 DEPTH=30 LEVELS=4`.
GENERATOR ?= brogue
SEED      ?= 42
DEPTH     ?= 20
LEVELS    ?= 1
MAP_OUT   ?= $(abspath demo/maps/generated.map)
TB_GAME_DIR ?= $(HOME)/.TrenchBroom/games/brogue-genesis

map:
	@mkdir -p $(dir $(MAP_OUT))
	godot --headless --path demo --script scripts/export_map.gd -- \
		--generator $(GENERATOR) --seed $(SEED) --depth $(DEPTH) \
		--levels $(LEVELS) --out $(MAP_OUT)

tb-sync:
	godot --headless --path demo --script scripts/export_tb_config.gd -- $(TB_GAME_DIR)

test: bin/genesis
	@bash test/run_tests.sh

stress: bin/genesis
	@./bin/genesis --stress 5000

godot:
	@cd godot && scons -j$$(nproc) platform=linux target=template_debug

godot-clean:
	@rm -rf godot/build godot/godot-cpp/bin demo/addons/brogue_gen

genesis:   bin/genesis
genesis3d: bin/genesis3d

bin/genesis: $(GEN_OBJ) obj/genesis_main.o obj/json_emit.o
	@mkdir -p bin
	$(CC) $(LDFLAGS) -o $@ $^ -lm

bin/genesis3d: $(GEN_OBJ) $(BLOBBER_OBJ) $(MESH_OBJ) obj/genesis3d_main.o
	@mkdir -p bin
	$(CC) $(LDFLAGS) -o $@ $^ -lm

obj/%.o: src/%.c
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -c $< -o $@

obj/genesis_main.o: src/genesis_main.c
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -c $< -o $@

obj/genesis3d_main.o: src/genesis3d_main.c
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -c $< -o $@

obj/json_emit.o: src/json_emit.c src/json_emit.h
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -c $< -o $@

clean:
	rm -rf obj bin
