# NumWorks game template. Rename the game here (see AGENT.md, "Start here").
NAME ?= jardinsecume
TITLE ?= Jardins d Ecume

SHELL := bash
ARM_CC ?= arm-none-eabi-gcc
HOST_CC ?= gcc
PYTHON ?= python
NWLINK = node node_modules/nwlink/bin/nwlink

# Every game source except the platform layer. Add new .c files here.
# runlog.c and hiscore.c are the high-score system (HIGHSCORES.md): delete them with the HS lines in game.c for a game
# without a score.
GAME = src/main.c src/game.c src/ranch.c src/view.c src/gfx.c src/assets_data.c
HEADERS = $(wildcard src/*.h) ../common/numgames_brand.h ../common/epsilon_app.h ../common/epsilon_files.h
DEVICE_OBJECTS = $(patsubst src/%.c,build/arm/%.o,$(GAME) src/platform_eadk.c)
LITE_OBJECTS = $(patsubst src/%.c,build/arm-lite/%.o,$(GAME) src/platform_eadk.c)

WARN = -Wall -Wextra -Werror -Wno-missing-field-initializers
DEFS = -DGAME_TITLE='"$(TITLE)"' -DGAME_SLUG='"$(NAME)"'
# -Wdouble-promotion: the calculator FPU is single precision, a stray double is slow.
# -ffp-contract=off: no fused multiply-add, so a float result is the same on the calculator and in a server replay.
ARM_FLAGS = -std=c11 -O2 -fno-math-errno -ffp-contract=off $(WARN) -Wdouble-promotion $(DEFS) -ffunction-sections -fdata-sections \
  -flto -fno-fat-lto-objects -fwhole-program -fvisibility=internal $(shell $(NWLINK) eadk-cflags-device 2>/dev/null)
ARM_LINK = -Wl,--relocatable -nostartfiles --specs=nano.specs -Wl,--gc-sections -Wl,-e,main \
  -Wl,-u,eadk_app_name -Wl,-u,eadk_app_icon -Wl,-u,eadk_api_level -flinker-output=nolto-rel

.PHONY: all build build-lite sim sim-lite test check check-lite size assets clean name
all: build sim

# One universal file for every model (the default). build-lite is the N0110/N0115 file when their storage is not tested:
# it never writes to the calculator, so its high-score table lasts the session (HIGHSCORES.md, "Lite build").
build: build/$(NAME).nwa
build-lite: build/$(NAME)-lite.nwa

node_modules/nwlink/bin/nwlink:
	npm ci --no-audit --no-fund

# Art is generated and committed; rerun after editing tools/gen_assets.py.
assets:
	$(PYTHON) tools/gen_assets.py

build/arm/%.o: src/%.c $(HEADERS) node_modules/nwlink/bin/nwlink
	@mkdir -p build/arm
	$(ARM_CC) $(ARM_FLAGS) -c $< -o $@

build/arm-lite/%.o: src/%.c $(HEADERS) node_modules/nwlink/bin/nwlink
	@mkdir -p build/arm-lite
	$(ARM_CC) $(ARM_FLAGS) -DSAVE_LITE -c $< -o $@

build/icon.o: assets/icon.png node_modules/nwlink/bin/nwlink
	@mkdir -p build
	$(NWLINK) png-icon-o $< $@

build/$(NAME).nwa: $(DEVICE_OBJECTS) build/icon.o
	$(ARM_CC) $(ARM_FLAGS) $(ARM_LINK) $(DEVICE_OBJECTS) build/icon.o -lm -o $@

build/$(NAME)-lite.nwa: $(LITE_OBJECTS) build/icon.o
	$(ARM_CC) $(ARM_FLAGS) -DSAVE_LITE $(ARM_LINK) $(LITE_OBJECTS) build/icon.o -lm -o $@

# Converts the app the way the calculator installer does: fails on a broken .nwa.
check: build/$(NAME).nwa
	$(NWLINK) nwa-bin $< build/$(NAME).bin

check-lite: build/$(NAME)-lite.nwa
	$(NWLINK) nwa-bin $< build/$(NAME)-lite.bin

size: build/$(NAME).nwa
	@ls -l $< | awk '{print "nwa file:", $$5, "bytes"}'
	@arm-none-eabi-size -A $< | awk '/^\.bss|^\.data/ {s+=$$2} END {print "static RAM (.bss+.data):", s, "bytes (keep under ~100000)"}'

sim: build/sim.exe build/sim-lite.exe

build/sim.exe: $(GAME) src/platform_pc.c $(HEADERS)
	@mkdir -p build
	$(HOST_CC) -std=c11 -O1 -g -ffp-contract=off $(WARN) $(DEFS) -DPLATFORM_DEVICE=0 $(GAME) src/platform_pc.c -lm -o $@

build/sim-lite.exe: $(GAME) src/platform_pc.c $(HEADERS)
	@mkdir -p build
	$(HOST_CC) -std=c11 -O1 -g -ffp-contract=off $(WARN) $(DEFS) -DPLATFORM_DEVICE=0 -DSAVE_LITE $(GAME) src/platform_pc.c -lm -o $@

# tests/*.txt run on the full build, tests/lite/*.txt on the lite one; screenshots land in build/shots.
test: sim
	@mkdir -p build/shots
	@for t in tests/*.txt; do echo "== $$t"; ./build/sim.exe $$t build/shots || exit 1; done
	@for t in tests/lite/*.txt; do echo "== $$t (lite)"; ./build/sim-lite.exe $$t build/shots || exit 1; done
	$(PYTHON) tools/ppm2png.py build/shots

name:
	@echo $(NAME)

clean:
	rm -rf build
