# Nightwatch: Five Shifts. A survival horror game for NumWorks (built from games/template).
NAME ?= nightwatch
TITLE ?= Nightwatch
VERSION := $(shell tr -d '[:space:]' < VERSION)

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.
# night.c (the rules) and save.c (progress) are the pure logic: the unit tests and the strategy bots link them on
# their own. bot.c (the bots) is in the simulator and the tests only, never in the calculator build.
LOGIC = src/night.c src/save.c
GAME = src/main.c src/game.c src/gfx.c src/assets_data.c $(LOGIC)
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)

WARN = -Wall -Wextra -Werror -Wno-missing-field-initializers
DEFS = -DGAME_TITLE='"$(TITLE)"' -DGAME_SLUG='"$(NAME)"' -DGAME_VERSION='"$(VERSION)"'
# -Wdouble-promotion: the calculator FPU is single precision; the game uses integers only.
# -ffp-contract=off: no fused multiply-add, so the calculator and the host tests compute the same bits.
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
HOST_FLAGS = -std=c11 -O1 -g -ffp-contract=off $(WARN) -Wdouble-promotion $(DEFS) -DPLATFORM_DEVICE=0

.PHONY: all build sim unit bots test check size assets emu clean name
all: build sim

build: build/$(NAME).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 VERSION
	@mkdir -p build/arm
	$(ARM_CC) $(ARM_FLAGS) -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 $@

# Converts the app the way the calculator installer does: fails on a broken .nwa.
check: build/$(NAME).nwa
	$(NWLINK) nwa-bin $< build/$(NAME).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.exe: $(GAME) src/bot.c src/platform_pc.c $(HEADERS) VERSION
	@mkdir -p build
	$(HOST_CC) $(HOST_FLAGS) $(GAME) src/bot.c src/platform_pc.c -lm -o $@

# The rules and the save code on their own: unit tests with a fake storage that can fail, and the strategy bots.
build/unit.exe: tests/unit/test_main.c $(LOGIC) src/bot.c $(HEADERS)
	@mkdir -p build
	$(HOST_CC) $(HOST_FLAGS) -Isrc tests/unit/test_main.c $(LOGIC) src/bot.c -lm -o $@

build/bots.exe: tests/unit/bots.c src/night.c src/bot.c $(HEADERS)
	@mkdir -p build
	$(HOST_CC) $(HOST_FLAGS) -O2 -Isrc tests/unit/bots.c src/night.c src/bot.c -lm -o $@

unit: build/unit.exe
	./build/unit.exe

bots: build/bots.exe
	./build/bots.exe

# Unit tests, the strategy bots, then every script in tests/ (screenshots land in build/shots).
test: build/unit.exe build/bots.exe build/sim.exe
	./build/unit.exe
	./build/bots.exe
	@mkdir -p build/shots
	@for t in tests/*.txt; do echo "== $$t"; ./build/sim.exe $$t build/shots || exit 1; done
	$(PYTHON) tools/ppm2png.py build/shots

# The real .nwa in the repository's ARM emulator (brand/tools/emu.py; needs `pip install unicorn pyelftools`):
# plays nights, quits, relaunches with the same storage and checks the progress records (tools/emu_check.py).
emu: build/$(NAME).nwa
	$(PYTHON) tools/emu_check.py

name:
	@echo $(NAME)

clean:
	rm -rf build
