# Wing Commander II (Kilrathi Saga, Win32): the reconstructed source.
#
# The original game's sources (src/*.c, src/*.cpp, src/*.asm and src/ix/)
# rebuild WC2.EXE with Microsoft Visual C++ 4.1 under wibo, byte-identical to
# the 1996 retail executable. With the SDL2 port (src/port/) they also build
# the native game. WC2's PE header reports linker 3.10.6038, the MSVC 4.1
# package's.
#
#   make                build WC2.EXE and check it against the retail hash
#   make verify-hash    the same check, by name
#   make run            run the rebuilt WC2.EXE in DREAMM
#   make run-original   run the retail WC2.EXE in DREAMM
#   make modern         build the SDL2 port, out-modern/wc2-modern
#   make modern-gui     build the SDL2 port with its launcher
#   make run-modern     run the SDL2 port in MODERN_RUN_DIR (data/wc2-full)
#   make site           build the source browser, out/site/html
#   make check-fuzz     build and run the flight fuzzer

# ---------------------------------------------------------------------------
# Original toolchain
# ---------------------------------------------------------------------------

WIBO = ./wibo
MSVC41_DIR = compilers/msvc41
MSVC41_STAMP = $(MSVC41_DIR)/.installed
CC = $(WIBO) $(MSVC41_DIR)/Bin/CL.EXE
LINK = $(WIBO) $(MSVC41_DIR)/Bin/LINK.EXE
LIBEXE = $(WIBO) $(MSVC41_DIR)/Bin/LIB.EXE

# MSVC expects Windows-style include/library search paths.  The recipes pass
# these through the host shell to wibo, so command-line /I paths use doubled
# backslashes while env vars keep normal Windows separators.
MSVC_INC = compilers\msvc41\Include;compilers\msvc41\SdkInclude
MSVC_LIB = compilers\msvc41\Lib

# These flags rebuild the retail code; changing an optimizer or codegen flag
# changes the output, and the hash check fails.
#
# Evidence for each choice:
#   /MTd   static DEBUG multithreaded CRT.  The binary links LIBCMTD: dbgheap.c,
#          dbgrpt.c, dbgdel.cpp, _CrtCheckMemory, "DAMAGE: after %hs block",
#          "Detected memory leaks!", plus tidtable.c/_mtinit for the MT variant.
#   /Oy    frame pointers omitted.  Almost every game function reads arguments
#          as [esp+N] with no `push ebp` prologue.
#   NO /Gf string pooling.  Two byte-identical "DIBsetWholePalette   SetEntries"
#          literals exist at 0x0046b6e0 and 0x0046b71c, so identical strings
#          were NOT merged.
#   /GX is off for the core by default.  The one proven exception is
#          console.cpp: its debug-console new expression at 0x40FCD0 has a C++
#          unwind map and jumps to __CxxFrameHandler.  A target-specific flag
#          below reproduces it.
# Extra compiler flags for temporary diagnostic builds, such as vport-debug's;
# empty for the retail build.
EXTRA_CFLAGS ?=

CFLAGS_COMMON = \
	/nologo \
	/c \
	/MTd \
	/I include \
	$(EXTRA_CFLAGS)

# WC1 used different optimizer settings for its game core and ix library. WC2
# does not: both areas have the unoptimized debug-build shape:
#
# Every ix function and the mapped WC2 game-core functions open with
#   `PUSH EBP / MOV EBP,ESP / PUSH EBX / PUSH ESI / PUSH EDI` -- saving all three
#   registers whether or not they are used -- spills intermediates to stack
#   temporaries such as [EBP-4], and jumps to one shared
#   `POP EDI / POP ESI / POP EBX / LEAVE / RET` epilogue.
CFLAGS_CORE = $(CFLAGS_COMMON) /Od /Oi
CFLAGS_IX   = $(CFLAGS_COMMON) /Od /Oi

# Default for anything not covered by a more specific rule.
CFLAGS = $(CFLAGS_CORE)

# The original was the IDE's Debug configuration: /DEBUG (which keeps every
# import thunk, /OPT:NOREF) without incremental linking, and no /ENTRY.
# Without /ENTRY LINK adds WinMainCRTStartup as an undefined symbol after
# reading every command-line object, so wincrt0.obj lands after the CRT
# members the game itself needs, at 0x00473E50.  The image's MISC and
# CodeView debug records name Debug/wc2.exe and C:\WCB\wc2\Debug\wc2.pdb.
LINKFLAGS = /nologo /SUBSYSTEM:WINDOWS /DEBUG /INCREMENTAL:NO \
	/PDB:C:/WCB/wc2/Debug/wc2.pdb /ALIGN:4096

# DDRAW.DLL and DSOUND.DLL are bound statically through the import table, so
# import libraries are required at link time.  The MSVC420 submodule already
# ships DDRAW.LIB/DSOUND.LIB and DDRAW.H/DSOUND.H, so no extra SDK is needed.
GAME_LIBPATH = $(MSVC_LIB)
#
# The import descriptors run DDRAW, WINMM, KERNEL32, USER32, GDI32, ADVAPI32,
# DSOUND: only the dsp library members, pulled after the CRT, need DirectSound,
# so its descriptor and thunks come from the second search pass.
GAME_LIBS = \
	ddraw.lib \
	dsound.lib \
	winmm.lib \
	kernel32.lib \
	user32.lib \
	gdi32.lib \
	advapi32.lib

# The ix code reached the link as five libraries.  The sound and streamer
# libraries precede the import libraries: their members sit before the first
# pass's import thunks at 0x0046DF08, and sample.cpp's ldexp is pulled after
# wincrt0.obj.  The dsp, LZO and file libraries are searched after LIBCMTD and
# OLDNAMES: their members follow the whole first-pass CRT at 0x00489990.
IX_LIBS_BEFORE_CRT = $(IX_SOUND_LIB) $(IX_STREAMER_LIB)
IX_LIBS_AFTER_CRT = $(IX_DSP_LIB) $(IX_LZO_LIB) $(IX_FILE_LIB)
CRT_LIBS = libcmtd.lib oldnames.lib

# ---------------------------------------------------------------------------
# Project inputs and generated outputs
# ---------------------------------------------------------------------------

TARGET = WC2.EXE
# LINK records this spelling in IMAGE_DEBUG_MISC, as in the retail image.
LINK_TARGET = Debug/wc2.exe
OUT_DIR = out
LINK_DRIVE_C = $(abspath $(OUT_DIR)/drive-c)
LINK_PDB_DIR = $(LINK_DRIVE_C)/WCB/wc2/Debug
PDB_SEED_TOOL = $(OUT_DIR)/createPdbSeed.exe
PDB_SEED = $(OUT_DIR)/wc2-pdb-seed.pdb
ORIGINAL_PDB_SIGNATURE = 0x32385F33
ORIGINAL_PDB_AGE = 22
PDB_SEED_AGE = 21
# PE header and three debug-directory entries: 1996-09-24 17:41:25 UTC.
ORIGINAL_TIMESTAMP = 0x32481D45
# SHA-256 of the complete 714,752-byte Kilrathi Saga retail executable.
ORIGINAL_SHA256 = f5e133e8eca8eb1a02e21a7d1afbb44d082e384a38d39db58a694371fa583bbe

UNAME_S := $(shell uname -s)
UNAME_M := $(shell uname -m)

MODERN_EXE_SUFFIX :=
MODERN_PLATFORM_LIBS :=
ifneq (,$(filter MINGW% MSYS% CYGWIN%,$(UNAME_S)))
MODERN_EXE_SUFFIX := .exe
MODERN_PLATFORM_LIBS := -limm32 -lshell32
endif

# The native build uses the host compiler and never contributes objects to the
# MSVC 4.1 build of WC2.EXE.
MODERN_OUT_DIR = out-modern
MODERN_TARGET = $(MODERN_OUT_DIR)/wc2-modern$(MODERN_EXE_SUFFIX)
MODERN_GUI_TARGET = $(MODERN_OUT_DIR)/wc2-modern-gui$(MODERN_EXE_SUFFIX)
MODERN_EMPTY :=
MODERN_SPACE := $(MODERN_EMPTY) $(MODERN_EMPTY)
# The port runs from the game's own directory: it chdir()s into gamedat/ and
# resolves every packet relative to that.  data/full holds only the reference
# executable, so pointing here at the installed tree is what makes run-modern
# find brief.pal and the rest of the resources.
MODERN_RUN_DIR ?= data/wc2-full
MODERN_ARGS ?=
SERIES ?= 1
MISSION ?= 0
MISSION_FLAGS ?=
CAMPAIGN ?= wc2
CUTSCENE_PHASE ?= preflight
ifneq ($(filter so1 so2,$(CAMPAIGN)),)
MODERN_CUTSCENE_SERIES = 1 2 3 4 5
MODERN_CUTSCENE_SELECTORS := $(foreach series,$(MODERN_CUTSCENE_SERIES),\
	$(addprefix v$(series)-t,0 1 2 3))
else
MODERN_CUTSCENE_SERIES = 1 2 3 4 5 6 7 8 9 10 11 12 13
MODERN_CUTSCENE_SELECTORS := $(filter-out v7-t3 v13-t3,\
	$(foreach series,$(MODERN_CUTSCENE_SERIES),\
		$(addprefix v$(series)-t,0 1 2 3)))
endif
MODERN_CUTSCENE_JOBS ?= 4
MODERN_CUTSCENE_FLAG = $(if $(filter postflight,$(CUTSCENE_PHASE)),--cutscene-only=postflight,--cutscene-only)

MODERN_CC ?= cc
MODERN_CXX ?= c++
MODERN_CMAKE ?= cmake
MODERN_SDL2_CONFIG ?= sdl2-config
MODERN_SDL_CFLAGS = $(shell \
	$(MODERN_SDL2_CONFIG) --cflags 2>/dev/null || \
	pkg-config --cflags sdl2 2>/dev/null)
MODERN_SDL_LIBS = $(shell \
	$(MODERN_SDL2_CONFIG) --libs 2>/dev/null || \
	pkg-config --libs sdl2 2>/dev/null)
MODERN_LZO_CFLAGS = $(shell pkg-config --cflags lzo2 2>/dev/null)
MODERN_LZO_INCLUDEDIR = $(shell pkg-config --variable=includedir lzo2 2>/dev/null)
MODERN_LZO_LIBS = $(shell pkg-config --libs lzo2 2>/dev/null)
# SDL_MAIN_HANDLED keeps SDL's headers from renaming main() to SDL_main on
# Windows, which needs SDL2main.a to supply a WinMain wrapper.  The port has
# its own main and calls SDL_SetMainReady() itself, so the rename is only a
# way to fail the link.
MODERN_CPPFLAGS = -DSDL_PORT=1 -DSDL_MAIN_HANDLED -Iinclude -Isrc/port \
	$(MODERN_SDL_CFLAGS) \
	$(MODERN_LZO_CFLAGS) $(addprefix -I,$(MODERN_LZO_INCLUDEDIR))
# The reconstruction reproduces MSVC 4.1's tolerance for mismatched pointer
# and integer arguments; clang treats those as errors by default.  Demote them
# so the port compiles the same sources the reference build does.
# -fno-common so a global defined in two translation units is an error here
# too.  Apple's linker merges those tentative definitions and GNU ld does not,
# which is how a duplicate reached CI having built cleanly on macOS.
MODERN_CFLAGS ?= -O2 -std=c11 -fno-common -Wno-return-type \
	-Wno-return-mismatch -Wno-error=incompatible-pointer-types \
	-Wno-int-conversion
MODERN_CXXFLAGS ?= -O2 -std=c++11
MODERN_DEPFLAGS = -MMD -MP
MODERN_SECTION_FLAGS = -ffunction-sections -fdata-sections
# The game's packet structures are byte-packed, so a pointer member lands on an
# odd offset by design and every access to one trips UBSan's alignment check.
# aarch64 and x86-64 both perform those loads correctly; the rest of UBSan and
# all of ASan stay on.
MODERN_UBSAN_EXCLUDE = -fno-sanitize=alignment
# Developer builds retain runtime diagnostics.  Tagged release jobs
# explicitly select the optimized, uninstrumented host build.  The flight
# fuzzer adds coverage to every object and keeps going after UBSan reports.
ifeq ($(MODERN_FUZZING),1)
override MODERN_SANITIZER_FLAGS = -g -O1 -fsanitize=fuzzer-no-link,address,undefined \
	$(MODERN_UBSAN_EXCLUDE) -fsanitize-recover=undefined -fno-omit-frame-pointer
else ifeq ($(MODERN_RELEASE),1)
override MODERN_SANITIZER_FLAGS =
else
override MODERN_SANITIZER_FLAGS = -fsanitize=address,undefined \
	$(MODERN_UBSAN_EXCLUDE) -fno-omit-frame-pointer
endif
MODERN_DEAD_STRIP_DARWIN = -Wl,-dead_strip -Wl,-no_fixup_chains
MODERN_DEAD_STRIP_OTHER = -Wl,--gc-sections
MODERN_DEAD_STRIP_FLAGS = $(if $(filter Darwin,$(UNAME_S)),\
	$(MODERN_DEAD_STRIP_DARWIN),$(MODERN_DEAD_STRIP_OTHER))

# Where the retail executable lives. `make data/full/WC2.ORI.EXE` copies it out
# of the sibling analysis tree so this repo never has to vendor the binary.
ORIGINAL_EXE = data/full/WC2.ORI.EXE
ORIGINAL_SRC ?= ../releases/win32/WC2.EXE

# ---------------------------------------------------------------------------
# Host platform and DREAMM runtime
# ---------------------------------------------------------------------------
#
# DREAMM is the only supported way to run the rebuilt executable.  Wine is not
# used: the Kilrathi Saga port is a 1996 Win32 binary that drives DirectDraw and
# DirectSound directly and expects a real Windows 95 environment, which is what
# DREAMM emulates.  Wine's own reimplementation of those APIs changes exactly
# the behaviour this project is trying to observe.
#
# DREAMM is downloaded on demand into .dreamm/ so the repository does not need
# to vendor platform-specific runtime binaries.

DREAMM_DIR = .dreamm
DREAMM_VERSION = 4.0x47
DREAMM_BASE_URL = https://dreamm.aarongiles.com/releases/4.0x

# 8-bit, not 16: the DIB layer creates a DirectDraw palette and pushes entries
# into it (DIBcascade -> CreatePalette, DIBsetPalette/DIBramPalette ->
# SetEntries).  Those calls only succeed against a palettized primary surface,
# so the game expects an 8-bit display mode; in 16-bit it takes the
# DIBerror("DIBmakeDIB   CreatePalette") path.  Override with DREAMM_PROPS=.
DREAMM_PROPS ?= -prop winres=640x480x8

ifeq ($(UNAME_S),Darwin)
DREAMM_ARCHIVE = dreamm-$(DREAMM_VERSION)-macos.dmg
DREAMM_BIN = $(DREAMM_DIR)/DREAMM.app/Contents/MacOS/dreamm
else
ifeq ($(UNAME_M),aarch64)
DREAMM_ARCHIVE = dreamm-$(DREAMM_VERSION)-linux-arm64.tgz
else
DREAMM_ARCHIVE = dreamm-$(DREAMM_VERSION)-linux-x64.tgz
endif
DREAMM_BIN = $(DREAMM_DIR)/dreamm
endif
DREAMM = $(CURDIR)/$(DREAMM_BIN)
DREAMM_STAMP = $(DREAMM_DIR)/.$(DREAMM_ARCHIVE).stamp

# The game runs out of the installed data directory.  C: is mounted writable
# from data/wc2-full/hd because DREAMM otherwise discards every write to C:, and the
# game rewrites INSTALL.DAT and its save slots.
#
# The disc is mounted at D: when present: the binary really does look for it
# (LocateStreamsDirOnDisc, FindCdRomDriveByVolumeLabel, PromptInsertCorrectCd),
# and the streaming music lives there. Point ISO at an image or a directory.
RUN_DIR = data/wc2-full
# Any disc image dropped in data/ or data/wc2-full/ is picked up automatically.
# The recipes cd into RUN_DIR, so the mount path is made relative to it.
ISO ?= $(firstword $(wildcard data/*.iso data/*.ISO data/wc2-full/*.iso data/wc2-full/*.ISO))
DREAMM_MOUNTS = -mount rw:C=hd \
                $(if $(ISO),-mount d=$(patsubst $(RUN_DIR)/%,%,$(patsubst data/%,../%,$(ISO))))

# ---------------------------------------------------------------------------
# Source order
# ---------------------------------------------------------------------------
#
# The object link order fixes addresses and therefore every binary comparison.
# MSVC emits functions in source order and the linker concatenates objects in
# command-line order, so address order == link order.
#
# Game-core link order.  Every core object is on the command line, so object
# order is address order.  The order and each object's extent are recovered
# from the retail image: the compiler starts each object's
# code on a 16-byte boundary and the linker fills the gap with INT3, the
# string literals of each object form one band in .data in the same order,
# and the DOS release's modules map one-to-one onto these objects with the
# same function order.
SRCS_ORDERED_CORE = \
	src/config.c \
	src/damage.c \
	src/joypres.c \
	src/palalloc.c \
	src/taperec.c \
	src/raster.asm \
	src/intro.c \
	src/hotspot.c \
	src/campaign.c \
	src/timer.c \
	src/geom.c \
	src/iffload.c \
	src/disk.c \
	src/screen.c \
	src/console.cpp \
	src/ship.c \
	src/palette.c \
	src/fields.c \
	src/titlemnu.c \
	src/mousepos.c \
	src/hugemove.c \
	src/spc.c \
	src/smart.c \
	src/textfmt.c \
	src/nearheap.c \
	src/input.c \
	src/orders.c \
	src/fade.c \
	src/auto.c \
	src/joycal.c \
	src/viewrect.c \
	src/sound.c \
	src/jump.c \
	src/gr.c \
	src/stream.cpp \
	src/campglob.c \
	src/logic.c \
	src/suffix.c \
	src/gameflow.c \
	src/pan.c \
	src/text.c \
	src/personnel.c \
	src/clock.c \
	src/mono.c \
	src/message.c \
	src/eject.c \
	src/cockpt.c \
	src/target.c \
	src/loadmisn.c \
	src/version.c \
	src/fl.c \
	src/brains.c \
	src/grdriver.c \
	src/pload.c \
	src/destroy.c \
	src/fstrcat.c \
	src/replay.c \
	src/shpext.c \
	src/vpfree.c \
	src/comm.c \
	src/wipe.c \
	src/fleetvw.c \
	src/mission.c \
	src/midscene.c \
	src/packet.c \
	src/joystick.c \
	src/speech.c \
	src/nav.c \
	src/kbmouse.c \
	src/hexnum.c \
	src/music.c \
	src/palfade.c \
	src/pktsize.c \
	src/strwidth.c \
	src/parsenum.c \
	src/winmain.c \
	src/carrier.c \
	src/fltsetup.c \
	src/usedcols.c \
	src/iffindex.c \
	src/memapi.c \
	src/optmenu.c \
	src/datafile.c \
	src/starfld.c \
	src/debug.cpp \
	src/getmode.c \
	src/startup.c \
	src/vpalloc.c \
	src/dib.cpp \
	src/turret.c \
	src/select.c \
	src/eventmgr.c \
	src/waitkey.c \
	src/main.c \
	src/death.c \
	src/memfree.c \
	src/resread.c \
	src/globals.c

# Each ix library's members, in the order LINK pulls them.
SRCS_IX_SOUND = \
	src/ix/system.cpp \
	src/ix/sound.cpp \
	src/ix/sample.cpp
SRCS_IX_STREAMER = \
	src/ix/streamer.cpp \
	src/ix/thread.cpp \
	src/ix/streamfile.cpp
SRCS_IX_DSP = \
	src/ix/dsp.cpp \
	src/ix/dspv.cpp \
	src/ix/dsps.cpp \
	src/ix/mixer.cpp \
	src/ix/dspmix.asm
SRCS_IX_LZO = \
	src/ix/lzo1x.cpp
SRCS_IX_FILE = \
	src/ix/ixfile.cpp

SRCS_ORDERED_IX = $(SRCS_IX_SOUND) $(SRCS_IX_STREAMER) $(SRCS_IX_DSP) \
	$(SRCS_IX_LZO) $(SRCS_IX_FILE)

SRCS_ORDERED = $(SRCS_ORDERED_CORE) $(SRCS_ORDERED_IX)

ALL_SRCS = $(wildcard src/*.c) $(wildcard src/*.cpp) $(wildcard src/*.asm) \
           $(wildcard src/ix/*.cpp) $(wildcard src/ix/*.asm)
SRCS = $(filter $(ALL_SRCS), $(SRCS_ORDERED)) \
       $(filter-out $(SRCS_ORDERED), $(ALL_SRCS))

# Every unit sees the shared declarations, and MSVC 4.1's codegen is
# sensitive to them: adding a prototype can move an operand or a stack
# slot in an unrelated file.  Rebuild on any header change so no stale
# object reaches the link.
GAME_HEADERS = $(wildcard include/*.h) $(wildcard src/ix/*.h)

objects_of = $(patsubst src/%,$(OUT_DIR)/%, \
         $(patsubst %.c,%.obj, $(patsubst %.cpp,%.obj, \
         $(patsubst %.asm,%.obj, $(1)))))

# Everything except the ix library members is an object on the command line.
LINK_OBJS = $(call objects_of,$(filter-out $(SRCS_ORDERED_IX),$(SRCS)))

IX_SOUND_LIB = $(OUT_DIR)/ix/sound.lib
IX_STREAMER_LIB = $(OUT_DIR)/ix/streamer.lib
IX_DSP_LIB = $(OUT_DIR)/ix/dsp.lib
IX_LZO_LIB = $(OUT_DIR)/ix/lzo.lib
IX_FILE_LIB = $(OUT_DIR)/ix/file.lib
IX_LIBS = $(IX_LIBS_BEFORE_CRT) $(IX_LIBS_AFTER_CRT)

# Platform-neutral recovered units: every core C and C++ unit.  sound.c keeps
# its C file name but is compiled as C++ (see its Win32 rule).  The assembled
# raster library has portable counterparts in src/port/graphics/raster.c.
MODERN_GAMEPLAY_SRCS = \
	$(filter-out src/sound.c,$(filter %.c,$(SRCS_ORDERED_CORE)))

MODERN_GAMEPLAY_CXX_SRCS = \
	$(filter %.cpp,$(SRCS_ORDERED_CORE))

MODERN_GAMEPLAY_CXX_C_SRCS = \
	src/sound.c

MODERN_IX_SRCS = \
	src/ix/streamer.cpp \
	src/ix/thread.cpp \
	src/ix/streamfile.cpp \
	src/ix/dsp.cpp \
	src/ix/dsps.cpp \
	src/ix/mixer.cpp \
	src/ix/dspv.cpp \
	src/ix/system.cpp \
	src/ix/sound.cpp \
	src/ix/sample.cpp \
	src/ix/lzo1x.cpp \
	src/ix/ixfile.cpp

MODERN_BASE_HOST_SRCS = \
	src/port/platform/compat.c \
	src/port/input/input.c \
	src/port/game/resources.c \
	src/port/game/data_patches.c \
	src/port/game/translation.c \
	src/port/platform/registry.c \
	src/port/platform/thread.c \
	src/port/platform/timer.c \
	src/port/graphics/video_state.c
MODERN_GAME_HOST_SRCS = \
	src/port/audio/audio.c \
	src/port/game/campaign.c \
	src/port/game/fixes.c \
	src/port/game/pilot_import.c \
	src/port/game/saves.c \
	src/port/game/cutscene.c \
	src/port/game/difficulty.c \
	src/port/input/events.c \
	src/port/input/flight_input.c \
	src/port/graphics/gl_functions.c \
	src/port/graphics/gl_renderer.c \
	src/port/graphics/hires_hud.c \
	src/port/graphics/interpolation.c \
	src/port/graphics/png.c \
	src/port/graphics/screenshot.c \
	src/port/platform/folders.c \
	src/port/input/joystick.c \
	src/port/input/pad_controls.c \
	src/port/input/rumble.c \
	src/port/input/keymap.c \
	src/port/platform/options.c \
	src/port/audio/mt32.c \
	src/port/audio/music.c \
	src/port/graphics/raster.c \
	src/port/graphics/space_positions.c \
	src/port/graphics/video.c
MODERN_GAME_HOST_CXX_SRCS = \
	src/port/audio/originfx.cpp \
	src/port/audio/wave.cpp
MODERN_YMFM_SRCS = \
	third_party/ymfm/ymfm_adpcm.cpp \
	third_party/ymfm/ymfm_opl.cpp \
	third_party/ymfm/ymfm_pcm.cpp
# libmt32emu emulates the MT-32 for DOS music (see third_party/munt/README.wc2).
MODERN_MUNT_SRCS = \
	third_party/munt/mt32emu/Analog.cpp \
	third_party/munt/mt32emu/BReverbModel.cpp \
	third_party/munt/mt32emu/Display.cpp \
	third_party/munt/mt32emu/File.cpp \
	third_party/munt/mt32emu/FileStream.cpp \
	third_party/munt/mt32emu/LA32FloatWaveGenerator.cpp \
	third_party/munt/mt32emu/LA32Ramp.cpp \
	third_party/munt/mt32emu/LA32WaveGenerator.cpp \
	third_party/munt/mt32emu/MidiStreamParser.cpp \
	third_party/munt/mt32emu/Part.cpp \
	third_party/munt/mt32emu/Partial.cpp \
	third_party/munt/mt32emu/PartialManager.cpp \
	third_party/munt/mt32emu/Poly.cpp \
	third_party/munt/mt32emu/ROMInfo.cpp \
	third_party/munt/mt32emu/SampleRateConverter.cpp \
	third_party/munt/mt32emu/Synth.cpp \
	third_party/munt/mt32emu/Tables.cpp \
	third_party/munt/mt32emu/TVA.cpp \
	third_party/munt/mt32emu/TVF.cpp \
	third_party/munt/mt32emu/TVP.cpp \
	third_party/munt/mt32emu/c_interface/c_interface.cpp \
	third_party/munt/mt32emu/sha1/sha1.cpp \
	third_party/munt/mt32emu/srchelper/InternalResampler.cpp \
	third_party/munt/mt32emu/srchelper/srctools/src/FIRResampler.cpp \
	third_party/munt/mt32emu/srchelper/srctools/src/IIR2xResampler.cpp \
	third_party/munt/mt32emu/srchelper/srctools/src/LinearResampler.cpp \
	third_party/munt/mt32emu/srchelper/srctools/src/ResamplerModel.cpp \
	third_party/munt/mt32emu/srchelper/srctools/src/SincResampler.cpp
MODERN_LAUNCHER_SRC = src/port/platform/launcher.c
MODERN_GUI_SOURCE_DIR = src/port/launcher
MODERN_GUI_BUILD_DIR = $(MODERN_OUT_DIR)/slint-gui
MODERN_GUI_BUILD_TYPE ?= Release
MODERN_TRANSLATION_LANGUAGES = es pl
MODERN_TRANSLATIONS = \
	$(MODERN_TRANSLATION_LANGUAGES:%=$(MODERN_OUT_DIR)/translations/%.dat)
MODERN_GUI_SRCS = \
	src/port/input/pad_bindings.h \
	src/port/input/joystick_calibration.h \
	$(MODERN_GUI_SOURCE_DIR)/launcher_axes.cpp \
	$(MODERN_GUI_SOURCE_DIR)/launcher_axes.h \
	$(wildcard $(MODERN_GUI_SOURCE_DIR)/generic-*.svg) \
	$(wildcard translations/*/LC_MESSAGES/*.po) \
	$(wildcard third_party/sc-controller/images/*.svg third_party/sc-controller/images/*/*.svg) \
	$(wildcard third_party/steam-deck/*.svg) \
	$(MODERN_GUI_SOURCE_DIR)/CMakeLists.txt \
	$(MODERN_GUI_SOURCE_DIR)/calibration.slint \
	$(MODERN_GUI_SOURCE_DIR)/controls.slint \
	$(MODERN_GUI_SOURCE_DIR)/control_connector.slint \
	$(MODERN_GUI_SOURCE_DIR)/steamdeck.slint \
	$(MODERN_GUI_SOURCE_DIR)/steamdeck-top.svg \
	$(MODERN_GUI_SOURCE_DIR)/launcher.cpp \
	$(MODERN_GUI_SOURCE_DIR)/launcher.slint \
	$(MODERN_GUI_SOURCE_DIR)/launcher_api.h \
	$(MODERN_GUI_SOURCE_DIR)/launcher_config.cpp \
	$(MODERN_GUI_SOURCE_DIR)/launcher_config.h \
	$(MODERN_GUI_SOURCE_DIR)/launcher_joystick.cpp \
	$(MODERN_GUI_SOURCE_DIR)/launcher_joystick.h \
	$(MODERN_GUI_SOURCE_DIR)/launcher_paths.cpp \
	$(MODERN_GUI_SOURCE_DIR)/launcher_paths.h \
	$(MODERN_GUI_SOURCE_DIR)/slint_static.h \
	$(MODERN_GUI_SOURCE_DIR)/wc2_logo.h

MODERN_GAMEPLAY_OBJS = \
	$(patsubst src/%.c,$(MODERN_OUT_DIR)/obj/%.o,$(MODERN_GAMEPLAY_SRCS)) \
	$(patsubst src/%.cpp,$(MODERN_OUT_DIR)/obj/%.o,$(MODERN_GAMEPLAY_CXX_SRCS)) \
	$(patsubst src/%.c,$(MODERN_OUT_DIR)/obj/%.o,$(MODERN_GAMEPLAY_CXX_C_SRCS))
MODERN_IX_OBJS = \
	$(patsubst src/%.cpp,$(MODERN_OUT_DIR)/obj/%.o,$(MODERN_IX_SRCS))
MODERN_BASE_HOST_OBJS = $(patsubst src/%.c,$(MODERN_OUT_DIR)/obj/%.o,$(MODERN_BASE_HOST_SRCS))
MODERN_YMFM_OBJS = $(patsubst %.cpp,$(MODERN_OUT_DIR)/obj/%.o,$(MODERN_YMFM_SRCS))
MODERN_MUNT_OBJS = $(patsubst %.cpp,$(MODERN_OUT_DIR)/obj/%.o,$(MODERN_MUNT_SRCS))
MODERN_GAME_HOST_OBJS = \
	$(patsubst src/%.c,$(MODERN_OUT_DIR)/obj/%.o,$(MODERN_GAME_HOST_SRCS)) \
	$(patsubst src/%.cpp,$(MODERN_OUT_DIR)/obj/%.o,$(MODERN_GAME_HOST_CXX_SRCS)) \
	$(MODERN_OUT_DIR)/obj/hires_cockpit_font.o \
	$(MODERN_YMFM_OBJS) $(MODERN_MUNT_OBJS)
MODERN_STATIC_GUI_LAUNCHER_OBJ = \
	$(MODERN_OUT_DIR)/obj/port/platform/launcher_gui.o
MODERN_GUI_GAME_OBJS = \
	$(MODERN_STATIC_GUI_LAUNCHER_OBJ) \
	$(MODERN_BASE_HOST_OBJS) \
	$(MODERN_GAME_HOST_OBJS) \
	$(MODERN_GAMEPLAY_OBJS) \
	$(MODERN_IX_OBJS)
# MSYS2 does not convert each path in a semicolon-separated CMake argument.
# Native Windows CMake and Ninja need drive-qualified object paths.
MODERN_GUI_CMAKE_OBJECTS = $(abspath $(MODERN_GUI_GAME_OBJS))
ifneq (,$(filter MINGW% MSYS% CYGWIN%,$(UNAME_S)))
MODERN_GUI_CMAKE_OBJECTS = $(shell cygpath -m $(abspath $(MODERN_GUI_GAME_OBJS)))
endif
MODERN_LAUNCHER_OBJ = $(patsubst src/%.c,$(MODERN_OUT_DIR)/obj/%.o,$(MODERN_LAUNCHER_SRC))
MODERN_DEPFILES = \
	$(MODERN_GAMEPLAY_OBJS:.o=.d) \
	$(MODERN_IX_OBJS:.o=.d) \
	$(MODERN_BASE_HOST_OBJS:.o=.d) \
	$(MODERN_GAME_HOST_OBJS:.o=.d) \
	$(MODERN_LAUNCHER_OBJ:.o=.d) \
	$(MODERN_STATIC_GUI_LAUNCHER_OBJ:.o=.d)

# ---------------------------------------------------------------------------
# Build targets and tool bootstrap
# ---------------------------------------------------------------------------

all: $(TARGET)

verify-hash: $(TARGET)

# The native port is deliberately built in a separate output tree.  It must
# never supply objects to the assembly-comparison target above.
modern: $(MODERN_TARGET)

# Slint is kept out of the normal native build dependency set. This target
# links it statically into a separate executable that opens the launcher by
# default without arguments, or explicitly with --gui alongside other options.
modern-gui: $(MODERN_GUI_TARGET)

.PHONY: modern-translations
modern-translations: $(MODERN_TRANSLATIONS)

.PHONY: update-translations
update-translations:
	python3 translations/bin/compileTranslations.py --update-ui

.PHONY: update-launcher-translations
SLINT_TR_EXTRACTOR ?= slint-tr-extractor
update-launcher-translations:
	@mkdir -p $(MODERN_OUT_DIR)
	$(SLINT_TR_EXTRACTOR) --package-name wc2-re -o $(MODERN_OUT_DIR)/launcher.pot \
		$(MODERN_GUI_SOURCE_DIR)/launcher.slint $(MODERN_GUI_SOURCE_DIR)/controls.slint \
		$(MODERN_GUI_SOURCE_DIR)/calibration.slint $(MODERN_GUI_SOURCE_DIR)/steamdeck.slint
	for language in $(MODERN_TRANSLATION_LANGUAGES); do \
		msgmerge --update --backup=none --no-fuzzy-matching --no-wrap \
			translations/$$language/LC_MESSAGES/launcher.po \
			$(MODERN_OUT_DIR)/launcher.pot || exit 1; \
	done

$(MODERN_OUT_DIR)/translations/%.dat: translations/bin/compileTranslations.py \
		$(wildcard translations/*/*.po) $(wildcard translations/*/*.json)
	python3 translations/bin/compileTranslations.py --source translations/$* --output $@

# Slint enables full Rust LTO upstream; the small launcher does not need its
# substantial clean-build cost.
$(MODERN_GUI_TARGET): $(MODERN_GUI_GAME_OBJS) $(MODERN_GUI_SRCS) $(MODERN_TRANSLATIONS) Makefile
	@command -v $(MODERN_CMAKE) >/dev/null 2>&1 || { \
		echo "CMake 3.21 or newer is required for modern-gui." >&2; \
		exit 1; \
	}
	@command -v cargo >/dev/null 2>&1 || { \
		echo "Rust 1.92 or newer is required to build Slint." >&2; \
		exit 1; \
	}
	@if test -f $(MODERN_GUI_BUILD_DIR)/CMakeCache.txt && \
	   ! grep -qx 'CMAKE_HOME_DIRECTORY:INTERNAL=$(abspath $(MODERN_GUI_SOURCE_DIR))' \
		$(MODERN_GUI_BUILD_DIR)/CMakeCache.txt; then \
		echo "The launcher's sources moved; configuring its build again."; \
		sed -n 's/^Rust_TOOLCHAIN:STRING=\(..*\)$$/-DRust_TOOLCHAIN=\1/p' \
			$(MODERN_GUI_BUILD_DIR)/CMakeCache.txt \
			> $(MODERN_GUI_BUILD_DIR)/moved-cmake-args; \
		rm -f $(MODERN_GUI_BUILD_DIR)/CMakeCache.txt; \
	fi
	$(MODERN_CMAKE) -S $(MODERN_GUI_SOURCE_DIR) \
		-B $(MODERN_GUI_BUILD_DIR) \
		-DCMAKE_CXX_COMPILER="$(MODERN_CXX)" \
		-DCMAKE_BUILD_TYPE=$(MODERN_GUI_BUILD_TYPE) \
		-DWC2_GAME_OBJECTS="$(subst $(MODERN_SPACE),;,$(MODERN_GUI_CMAKE_OBJECTS))" \
		-DWC2_GAME_SANITIZERS=$(if $(MODERN_SANITIZER_FLAGS),ON,OFF) \
		-DWC2_GUI_OUTPUT_DIRECTORY=$(abspath $(MODERN_OUT_DIR)) \
		$$(cat $(MODERN_GUI_BUILD_DIR)/moved-cmake-args 2>/dev/null)
	@rm -f $(MODERN_GUI_BUILD_DIR)/moved-cmake-args
	CARGO_PROFILE_RELEASE_LTO=false \
		$(MODERN_CMAKE) --build $(MODERN_GUI_BUILD_DIR) \
		--config $(MODERN_GUI_BUILD_TYPE) --target wc2-modern-gui
	@test -s $@

modern-check-deps:
	@if test -z "$(strip $(MODERN_SDL_CFLAGS))" || \
	   test -z "$(strip $(MODERN_SDL_LIBS))" || \
	   test -z "$(strip $(MODERN_LZO_LIBS))"; then \
		echo "SDL2 or LZO2 development files were not found." >&2; \
		echo "Install SDL2 and LZO2 development files." >&2; \
		exit 1; \
	fi

$(MODERN_STATIC_GUI_LAUNCHER_OBJ): MODERN_LAUNCHER_CPPFLAGS = -DWC2_STATIC_GUI=1
$(MODERN_STATIC_GUI_LAUNCHER_OBJ): \
		$(MODERN_LAUNCHER_SRC) Makefile | modern-check-deps
	@mkdir -p $(dir $@)
	$(MODERN_CC) $(MODERN_CPPFLAGS) $(MODERN_LAUNCHER_CPPFLAGS) $(MODERN_CFLAGS) \
		$(MODERN_SECTION_FLAGS) $(MODERN_SANITIZER_FLAGS) \
		$(MODERN_DEPFLAGS) -c $< -o $@

$(MODERN_OUT_DIR)/obj/%.o: src/%.c | modern-check-deps
	@mkdir -p $(dir $@)
	$(MODERN_CC) $(MODERN_CPPFLAGS) $(MODERN_CFLAGS) \
		$(MODERN_SECTION_FLAGS) $(MODERN_SANITIZER_FLAGS) \
		$(MODERN_DEPFLAGS) -c $< -o $@

$(MODERN_OUT_DIR)/obj/%.o: src/%.cpp | modern-check-deps
	@mkdir -p $(dir $@)
	$(MODERN_CXX) $(MODERN_CPPFLAGS) -Isrc/ix $(MODERN_CXXFLAGS) \
		$(MODERN_SECTION_FLAGS) $(MODERN_SANITIZER_FLAGS) \
		$(MODERN_DEPFLAGS) -c $< -o $@

# The hi-res HUD's double-size cockpit font (CC BY 4.0, see its README.md).
$(MODERN_OUT_DIR)/hires_cockpit_font.c: \
		third_party/double-resolution-cockpit-font/cockpit-font-2x.bin \
		bin/compileHiResFont.py
	@mkdir -p $(dir $@)
	python3 bin/compileHiResFont.py $@ --height 12 \
		--font $< g_abHiResCockpitFont g_nHiResCockpitFontSize

$(MODERN_OUT_DIR)/obj/hires_cockpit_font.o: \
		$(MODERN_OUT_DIR)/hires_cockpit_font.c | modern-check-deps
	@mkdir -p $(dir $@)
	$(MODERN_CC) $(MODERN_CFLAGS) $(MODERN_SECTION_FLAGS) \
		$(MODERN_SANITIZER_FLAGS) -c $< -o $@

$(MODERN_OUT_DIR)/obj/port/audio/originfx.o: src/port/audio/originfx.cpp | modern-check-deps
	@mkdir -p $(dir $@)
	$(MODERN_CXX) $(MODERN_CPPFLAGS) -Ithird_party/ymfm \
		$(MODERN_CXXFLAGS) -std=c++14 \
		$(MODERN_SECTION_FLAGS) $(MODERN_SANITIZER_FLAGS) \
		$(MODERN_DEPFLAGS) -c $< -o $@

# Upstream's CMake build defines these for every platform the port builds on.
# Its DAC emulation shifts negative samples left on purpose.
$(MODERN_MUNT_OBJS): $(MODERN_OUT_DIR)/obj/third_party/munt/%.o: third_party/munt/%.cpp | modern-check-deps
	@mkdir -p $(dir $@)
	$(MODERN_CXX) -DMT32EMU_WITH_INTERNAL_RESAMPLER -DMT32EMU_WITH_SNPRINTF \
		-DMT32EMU_WITH_EXP2F $(MODERN_CXXFLAGS) \
		$(MODERN_SECTION_FLAGS) $(MODERN_SANITIZER_FLAGS) -fno-sanitize=shift-base \
		$(MODERN_DEPFLAGS) -c $< -o $@

$(MODERN_OUT_DIR)/obj/port/audio/mt32.o: MODERN_CPPFLAGS += -Ithird_party/munt

$(MODERN_OUT_DIR)/obj/third_party/ymfm/%.o: third_party/ymfm/%.cpp | modern-check-deps
	@mkdir -p $(dir $@)
	$(MODERN_CXX) $(MODERN_CPPFLAGS) -Ithird_party/ymfm \
		$(MODERN_CXXFLAGS) -std=c++14 \
		$(MODERN_SECTION_FLAGS) $(MODERN_SANITIZER_FLAGS) \
		$(MODERN_DEPFLAGS) -c $< -o $@

$(MODERN_OUT_DIR)/obj/sound.o: src/sound.c | modern-check-deps
	@mkdir -p $(dir $@)
	$(MODERN_CXX) $(MODERN_CPPFLAGS) -Isrc/ix $(MODERN_CXXFLAGS) \
		$(MODERN_SECTION_FLAGS) $(MODERN_SANITIZER_FLAGS) \
		$(MODERN_DEPFLAGS) -x c++ -c $< -o $@

# Keep the recovered functions intact while the native objects enter the SDL
# positional-audio bridge.
$(MODERN_OUT_DIR)/obj/music.o: MODERN_CPPFLAGS += \
	-DSdlUsingOriginFxSoundEffects=SdlHandlesGameSoundEffects
$(MODERN_OUT_DIR)/obj/music.o: Makefile
$(MODERN_OUT_DIR)/obj/sound.o: MODERN_CPPFLAGS += \
	-Dix_system_new_sound=SdlNewWaveSound
$(MODERN_OUT_DIR)/obj/sound.o: Makefile

# Keep the retail-address body intact; only native callers use saves.c.
$(MODERN_OUT_DIR)/obj/personnel.o: MODERN_CPPFLAGS += \
	-DHasSavedPilotCampaign=SdlRetailHasSavedPilotCampaign
$(MODERN_OUT_DIR)/obj/personnel.o: Makefile

$(MODERN_TARGET): \
		$(MODERN_TRANSLATIONS) \
		$(MODERN_LAUNCHER_OBJ) \
		$(MODERN_BASE_HOST_OBJS) \
		$(MODERN_GAME_HOST_OBJS) \
		$(MODERN_GAMEPLAY_OBJS) \
		$(MODERN_IX_OBJS)
	@mkdir -p $(dir $@)
	$(MODERN_CXX) $(MODERN_CXXFLAGS) $(MODERN_SANITIZER_FLAGS) \
		$(filter %.o,$^) $(MODERN_SDL_LIBS) $(MODERN_LZO_LIBS) \
		$(MODERN_PLATFORM_LIBS) \
		$(MODERN_DEAD_STRIP_FLAGS) -o $@

run-modern: modern
	@case "$(MODERN_RUN_DIR)" in \
		/*) modern_run_dir="$(MODERN_RUN_DIR)" ;; \
		*) modern_run_dir="$(CURDIR)/$(MODERN_RUN_DIR)" ;; \
	esac; \
	test -d "$$modern_run_dir" || { \
		echo "Modern run directory does not exist: $$modern_run_dir" >&2; \
		exit 1; \
	}; \
	cd "$$modern_run_dir" && "$(CURDIR)/$(MODERN_TARGET)" $(MODERN_ARGS)

run-modern-gui: modern-gui
	@case "$(MODERN_RUN_DIR)" in \
		/*) modern_run_dir="$(MODERN_RUN_DIR)" ;; \
		*) modern_run_dir="$(CURDIR)/$(MODERN_RUN_DIR)" ;; \
	esac; \
	test -d "$$modern_run_dir" || { \
		echo "Modern run directory does not exist: $$modern_run_dir" >&2; \
		exit 1; \
	}; \
	cd "$$modern_run_dir" && \
		"$(CURDIR)/$(MODERN_GUI_TARGET)" --gui $(MODERN_ARGS)

# The SDL2 host recognizes the compressed resources in an installed DOS copy
# and plays its OriginFX music and synthesized effects through an embedded
# YM3812 emulator.
run-modern-dos: MODERN_RUN_DIR = data/dos
run-modern-dos: run-modern

# The original startup has a hidden direct-flight path selected by the ordered
# tokens "Origin vN tN e".  The modern build forwards these through the recovered
# WC2 option loader just as a native argument vector would be handled.
# The option loader exposes one fewer token than it reads, so retain a sentinel.
run-modern-mission: MODERN_ARGS = --campaign=$(CAMPAIGN) Origin v$(SERIES) t$(MISSION) e \
	$(MISSION_FLAGS) ignored
run-modern-mission: run-modern

# Run the campaign cutscene selected by the original developer arguments.  The
# SDL host invokes the campaign VM directly and exits when it returns.
run-modern-cutscene: MODERN_ARGS = --campaign=$(CAMPAIGN) $(MODERN_CUTSCENE_FLAG) Origin v$(SERIES) \
	t$(MISSION) ignored
run-modern-cutscene: run-modern

# MODULE.000 has four mission slots per series.  The base campaign uses every
# slot in series 1-13 except the empty fourth slots in series 7 and 13.
# SO1's MODULE.001 and SO2's MODULE.002 use all four slots in series 1-5.
modern-test-cutscenes: modern
	+@status=0; \
	$(MAKE) --no-print-directory --keep-going \
		-j$(MODERN_CUTSCENE_JOBS) \
		$(addprefix modern-test-cutscene-,$(MODERN_CUTSCENE_SELECTORS)) || \
		status=$$?; \
	if test $$status -ne 0; then \
		echo "Cutscene sanitizer sweep failed; inspect the logs above." >&2; \
		exit $$status; \
	fi; \
	echo "Cutscene sanitizer sweep passed: $(words $(MODERN_CUTSCENE_SELECTORS)) $(CAMPAIGN) $(CUTSCENE_PHASE) sequences."

modern-test-cutscene-%: modern
	@selector="$*"; \
	series=$${selector#v}; \
	mission=$${series##*-t}; \
	series=$${series%%-t*}; \
	case "$(MODERN_RUN_DIR)" in \
		/*) modern_run_dir="$(MODERN_RUN_DIR)" ;; \
		*) modern_run_dir="$(CURDIR)/$(MODERN_RUN_DIR)" ;; \
	esac; \
	test -d "$$modern_run_dir" || { \
		echo "Modern run directory does not exist: $$modern_run_dir" >&2; \
		exit 1; \
	}; \
	log_dir="$(CURDIR)/$(MODERN_OUT_DIR)/cutscene-asan/$(CAMPAIGN)/$(CUTSCENE_PHASE)"; \
	mkdir -p "$$log_dir"; \
	log="$$log_dir/$$selector.log"; \
	echo "Running cutscene $$selector"; \
	cd "$$modern_run_dir" && \
		SDL_VIDEODRIVER=dummy SDL_AUDIODRIVER=dummy \
		ASAN_OPTIONS=halt_on_error=1:abort_on_error=1 \
		UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1 \
		"$(CURDIR)/$(MODERN_TARGET)" --campaign=$(CAMPAIGN) $(MODERN_CUTSCENE_FLAG) \
		Origin "v$$series" "t$$mission" ignored \
		>"$$log" 2>&1; \
	status=$$?; \
	if test $$status -ne 0 || \
		grep -Eq 'ERROR: AddressSanitizer|runtime error:' "$$log"; then \
		echo "Cutscene $$selector failed; see $$log" >&2; \
		grep -E 'ERROR: AddressSanitizer|runtime error:|SUMMARY: (AddressSanitizer|UndefinedBehaviorSanitizer)' \
			"$$log" >&2 || tail -n 20 "$$log" >&2; \
		exit 1; \
	fi; \
	echo "Passed cutscene $$selector"

FUZZ_CC ?= clang
FUZZ_CXX ?= clang++
FUZZ_OUT_DIR ?= out-modern/fuzz
FUZZ_DATA = $(if $(GAME_DATA),$(GAME_DATA),$(MODERN_RUN_DIR))

# Compile every game object with coverage, separately from normal SDL builds.
# One campaign per process: each campaign loads its own ship and mission data.
.PHONY: fuzz-flight check-fuzz check-fuzz-so1 check-fuzz-so2
fuzz-flight:
	$(MAKE) MODERN_OUT_DIR="$(FUZZ_OUT_DIR)" MODERN_CC="$(FUZZ_CC)" \
		MODERN_CXX="$(FUZZ_CXX)" MODERN_FUZZING=1 \
		"$(FUZZ_OUT_DIR)/flight-fuzzer$(MODERN_EXE_SUFFIX)"

check-fuzz: fuzz-flight
	python3 fuzz/runFuzzer.py "$(FUZZ_OUT_DIR)/flight-fuzzer$(MODERN_EXE_SUFFIX)" \
		"$(FUZZ_DATA)" --campaign wc2 $(FUZZ_ARGS)

check-fuzz-so1: fuzz-flight
	python3 fuzz/runFuzzer.py "$(FUZZ_OUT_DIR)/flight-fuzzer$(MODERN_EXE_SUFFIX)" \
		"$(if $(SO1_DATA),$(SO1_DATA),$(FUZZ_DATA))" --campaign so1 $(FUZZ_ARGS)

check-fuzz-so2: fuzz-flight
	python3 fuzz/runFuzzer.py "$(FUZZ_OUT_DIR)/flight-fuzzer$(MODERN_EXE_SUFFIX)" \
		"$(if $(SO2_DATA),$(SO2_DATA),$(FUZZ_DATA))" --campaign so2 $(FUZZ_ARGS)

ifeq ($(MODERN_FUZZING),1)
$(MODERN_OUT_DIR)/obj/fuzz/flight.o: fuzz/flight.c | modern-check-deps
	@mkdir -p $(dir $@)
	$(MODERN_CC) $(MODERN_CPPFLAGS) $(MODERN_CFLAGS) $(MODERN_SANITIZER_FLAGS) \
		$(MODERN_DEPFLAGS) -c $< -o $@

# The fuzzer runs the game on a simulated clock: its copy of input.c reads the
# fuzzer's ticks and sleeps, and its copy of events.c leaves SdlPumpEvents to
# the fuzzer, which advances that clock and the text of mid-flight scenes.
$(MODERN_OUT_DIR)/obj/fuzz/port/input/events.o: src/port/input/events.c | modern-check-deps
	@mkdir -p $(dir $@)
	$(MODERN_CC) $(MODERN_CPPFLAGS) $(MODERN_CFLAGS) $(MODERN_SANITIZER_FLAGS) \
		-DSdlPumpEvents=SdlPumpFlightProbeHostEvents $(MODERN_DEPFLAGS) -c $< -o $@

$(MODERN_OUT_DIR)/obj/fuzz/port/input/input.o: src/port/input/input.c | modern-check-deps
	@mkdir -p $(dir $@)
	$(MODERN_CC) $(MODERN_CPPFLAGS) $(MODERN_CFLAGS) $(MODERN_SANITIZER_FLAGS) \
		-DSDL_ShowSimpleMessageBox=SdlFlightProbeMessageBox \
		-DSDL_GetTicks=SdlFlightProbeGetTicks -DSDL_Delay=SdlFlightProbeDelay \
		$(MODERN_DEPFLAGS) -c $< -o $@

$(MODERN_OUT_DIR)/flight-fuzzer$(MODERN_EXE_SUFFIX): $(MODERN_OUT_DIR)/obj/fuzz/flight.o \
		$(MODERN_OUT_DIR)/obj/fuzz/port/input/events.o $(MODERN_OUT_DIR)/obj/fuzz/port/input/input.o \
		$(filter-out $(MODERN_OUT_DIR)/obj/port/input/input.o,$(MODERN_BASE_HOST_OBJS)) \
		$(filter-out $(MODERN_OUT_DIR)/obj/port/input/events.o,$(MODERN_GAME_HOST_OBJS)) \
		$(MODERN_GAMEPLAY_OBJS) $(MODERN_IX_OBJS)
	$(MODERN_CXX) $(MODERN_CXXFLAGS) $(MODERN_SANITIZER_FLAGS) $^ \
		$(MODERN_SDL_LIBS) $(MODERN_LZO_LIBS) $(MODERN_PLATFORM_LIBS) \
		$(MODERN_DEAD_STRIP_FLAGS) -fsanitize=fuzzer -o $@

# Sanitizer flag changes must not leave stale objects in the fuzz tree.
$(MODERN_OUT_DIR)/obj/fuzz/flight.o $(MODERN_OUT_DIR)/obj/fuzz/port/input/events.o \
		$(MODERN_OUT_DIR)/obj/fuzz/port/input/input.o $(MODERN_BASE_HOST_OBJS) \
		$(MODERN_GAME_HOST_OBJS) $(MODERN_GAMEPLAY_OBJS) $(MODERN_IX_OBJS): Makefile
endif

-include $(MODERN_DEPFILES) $(MODERN_OUT_DIR)/obj/fuzz/flight.d \
	$(MODERN_OUT_DIR)/obj/fuzz/port/input/events.d $(MODERN_OUT_DIR)/obj/fuzz/port/input/input.d

ifeq ($(UNAME_S),Linux)
WIBO_PRESET = release64-clang
WIBO_BIN = wibo-src/build/release64-clang/wibo
else
WIBO_PRESET = release-macos
WIBO_BIN = wibo-src/build/release/wibo
endif

$(WIBO): wibo-src/src/files.cpp wibo-src/CMakeLists.txt
	cd wibo-src && cmake --preset $(WIBO_PRESET) && cmake --build --preset $(WIBO_PRESET)
	ln -sf $(WIBO_BIN) $@

# The decomp.me MSVC 4.1 package contains Bin/ and Include/ but no linker
# libraries or DirectX headers. Bootstrap the compiler from the requested
# release and add only build-support files from the old pinned SDK snapshot;
# the compiler and linker themselves always come from MSVC 4.1.
#
# The support snapshot's CRT and Win32 import libraries are the MSVC 4.20
# ones.  The original was linked against the MSVC 4.10 set instead:
# WinMainCRTStartup calls _heap_init without testing its result before
# _mtinit/_amsg_exit(_RT_THREAD), and every KERNEL32/USER32/GDI32/ADVAPI32/
# WINMM import hint matches the 4.10 import libraries, not the 4.20 ones.
# The 4.10 libraries are therefore installed over the support set; the
# DirectX import libraries (DDRAW.LIB, DSOUND.LIB), which 4.10 lacks, still
# come from the support snapshot.
MSVC41_URL = https://github.com/decompme/compilers/releases/download/compilers/msvc4.1.tar.gz
MSVC41_SHA256 = ba2a100dad6b5f1f097860c8cb5e0d3b5f78881e69f96f16168b174e12af80b0
MSVC41_ARCHIVE = 3rdparty/msvc4.1.tar.gz
MSVC41_SUPPORT_COMMIT = df2c13aad74c094988c6c7e784234c2e778a0e91
MSVC41_SUPPORT_URL = https://github.com/itsmattkc/MSVC420/archive/$(MSVC41_SUPPORT_COMMIT).tar.gz
MSVC41_SUPPORT_SHA256 = caddbd356e57106050565297c1931871339ee488a58019ba766dc6cb8b214939
MSVC41_SUPPORT_ARCHIVE = 3rdparty/msvc420-support.tar.gz
MSVC410_LIBS_COMMIT = 373f5e621ac8fb7b219873b37334ef1d0a2149e6
MSVC410_LIBS_URL = https://github.com/archaic-msvc/msvc410/archive/$(MSVC410_LIBS_COMMIT).tar.gz
MSVC410_LIBS_SHA256 = 72904772611a8cb23ea51fc2e670e58cf35d4e5d26c80d7a9c740ac73ef3636a
MSVC410_LIBS_ARCHIVE = 3rdparty/msvc410.tar.gz

# wibo needs this compatible runtime beside CL.EXE and LINK.EXE.
MSVCRT40_URL = https://raw.githubusercontent.com/neuromancer/my-teacher-is-an-alien-re/3d1bfe60522ae05b86bbd2252fd01c8d0a11c3df/3rdparty/msvcrt40.dll
MSVCRT40_SHA256 = ab55a2de2b6faf3daacd3e69473d385ceaead8033f7c79beb6bbf802f230f030
MSVCRT_SOURCE = 3rdparty/msvcrt40.dll
MSVCRT_DLL = $(MSVC41_DIR)/Bin/msvcrt40.dll

$(MSVC41_ARCHIVE):
	@mkdir -p $(dir $@)
	@echo "Downloading Microsoft Visual C++ 4.1..."
	@curl -fL --retry 3 -o "$@.tmp" "$(MSVC41_URL)"
	@printf '%s  %s\n' "$(MSVC41_SHA256)" "$@.tmp" | \
		shasum -a 256 -c - >/dev/null || \
		(rm -f "$@.tmp"; echo "Error: MSVC 4.1 archive checksum mismatch." >&2; exit 1)
	@mv "$@.tmp" "$@"

$(MSVC41_SUPPORT_ARCHIVE):
	@mkdir -p $(dir $@)
	@echo "Downloading pinned linker-library support files..."
	@curl -fL --retry 3 -o "$@.tmp" "$(MSVC41_SUPPORT_URL)"
	@printf '%s  %s\n' "$(MSVC41_SUPPORT_SHA256)" "$@.tmp" | \
		shasum -a 256 -c - >/dev/null || \
		(rm -f "$@.tmp"; echo "Error: support archive checksum mismatch." >&2; exit 1)
	@mv "$@.tmp" "$@"

$(MSVC410_LIBS_ARCHIVE):
	@mkdir -p $(dir $@)
	@echo "Downloading pinned Microsoft Visual C++ 4.10 libraries..."
	@curl -fL --retry 3 -o "$@.tmp" "$(MSVC410_LIBS_URL)"
	@printf '%s  %s\n' "$(MSVC410_LIBS_SHA256)" "$@.tmp" | \
		shasum -a 256 -c - >/dev/null || \
		(rm -f "$@.tmp"; echo "Error: MSVC 4.10 library archive checksum mismatch." >&2; exit 1)
	@mv "$@.tmp" "$@"

$(MSVC41_STAMP): $(MSVC41_ARCHIVE) $(MSVC41_SUPPORT_ARCHIVE) $(MSVC410_LIBS_ARCHIVE)
	@mkdir -p compilers
	@msvc41_install=$$(mktemp -d compilers/.msvc41-install.XXXXXX); \
		msvc41_support=$$(mktemp -d compilers/.msvc41-support.XXXXXX); \
		msvc410_libs=$$(mktemp -d compilers/.msvc41-libs.XXXXXX); \
		trap 'rm -rf "$$msvc41_install" "$$msvc41_support" "$$msvc410_libs"' 0; \
		tar -xzf "$(MSVC41_ARCHIVE)" -C "$$msvc41_install"; \
		tar -xzf "$(MSVC41_SUPPORT_ARCHIVE)" -C "$$msvc41_support"; \
		tar -xzf "$(MSVC410_LIBS_ARCHIVE)" -C "$$msvc410_libs" \
			'msvc410-$(MSVC410_LIBS_COMMIT)/lib'; \
		support_root=$$(find "$$msvc41_support" -mindepth 1 -maxdepth 1 \
			-type d -name 'MSVC420-*' -print -quit); \
		test -n "$$support_root"; \
		libs_root="$$msvc410_libs/msvc410-$(MSVC410_LIBS_COMMIT)"; \
		test -f "$$libs_root/lib/LIBCMTD.LIB"; \
		mkdir -p "$$msvc41_install/Lib" "$$msvc41_install/SdkInclude"; \
		cp -R "$$support_root/lib/." "$$msvc41_install/Lib/"; \
		cp -R "$$libs_root/lib/." "$$msvc41_install/Lib/"; \
		cp -R "$$support_root/include/." "$$msvc41_install/SdkInclude/"; \
		touch "$$msvc41_install/.installed"; \
		rm -rf "$(MSVC41_DIR)"; \
		mv "$$msvc41_install" "$(MSVC41_DIR)"; \
		rm -rf "$$msvc41_support" "$$msvc410_libs"; \
		trap - 0

# The PDB seed names this DLL as a direct prerequisite. Declare how the
# compiler install supplies it so a clean parallel build can schedule it.
$(MSVC41_DIR)/Bin/MSPDB41.DLL: | $(MSVC41_STAMP)
	@test -f "$@"

msvc41-toolchain: $(MSVCRT_DLL)

$(MSVCRT_DLL): $(MSVCRT_SOURCE) | $(MSVC41_STAMP)
	cp -f $< $@

$(MSVCRT_SOURCE):
	@mkdir -p $(dir $@)
	@echo "Downloading wibo-compatible msvcrt40.dll..."
	@curl -fL --retry 3 -o "$@.tmp" "$(MSVCRT40_URL)"
	@printf '%s  %s\n' "$(MSVCRT40_SHA256)" "$@.tmp" | \
		shasum -a 256 -c - >/dev/null || \
		(rm -f "$@.tmp"; echo "Error: msvcrt40.dll checksum mismatch." >&2; exit 1)
	@mv "$@.tmp" "$@"

# The raster library was hand-written assembly.  UASM, a
# MASM-compatible assembler built from a pinned source snapshot, reproduces it.
UASM_COMMIT = 29a2237acab283c572aff734c6fbbf26295068c1
UASM_URL = https://github.com/neuviemeporte/UASM/archive/$(UASM_COMMIT).tar.gz
UASM_SHA256 = 5f5fe810e641d118f16ba87135e4c26e4a13733c5f96dcd9db0c1edc618f34f9
UASM_ARCHIVE = 3rdparty/uasm.tar.gz
UASM_DIR = compilers/uasm
UASM = $(OUT_DIR)/uasm/uasm

# UASM's generic Makefile only builds for Unix (-D __UNIX__ pulls in
# sys/mman.h, which MinGW doesn't have); its GccWin.mak targets MinGW/Cygwin
# natively with -D__NT__ and a self-contained CC recipe, so CC is not passed
# there.  MinGW also ships no cc, so HOST_CC defaults to gcc on those hosts.
ifneq (,$(filter MINGW% MSYS% CYGWIN%,$(UNAME_S)))
HOST_CC ?= gcc
UASM_MAKEFILE := GccWin.mak
# Newer GCC errors (not just warns) on calls to undeclared functions in
# UASM's own sources; GccWin.mak predates that default, unlike the Unix
# Makefile which already disables it.
UASM_MAKE_CC_ARG := extra_c_flags="-DNDEBUG -O2 -fomit-frame-pointer -Wno-implicit-function-declaration -Wno-incompatible-pointer-types"
else
HOST_CC ?= cc
UASM_MAKEFILE := Makefile
UASM_MAKE_CC_ARG := CC=$(HOST_CC)
endif

$(UASM_ARCHIVE):
	@mkdir -p $(dir $@)
	@echo "Downloading pinned UASM sources..."
	@curl -fL --retry 3 -o "$@.tmp" "$(UASM_URL)"
	@printf '%s  %s\n' "$(UASM_SHA256)" "$@.tmp" | \
		shasum -a 256 -c - >/dev/null || \
		(rm -f "$@.tmp"; echo "Error: UASM archive checksum mismatch." >&2; exit 1)
	@mv "$@.tmp" "$@"

$(UASM_DIR)/gccmod.inc: $(UASM_ARCHIVE)
	@mkdir -p compilers
	@uasm_install=$$(mktemp -d compilers/.uasm-install.XXXXXX); \
		trap 'rm -rf "$$uasm_install"' 0; \
		tar -xzf "$(UASM_ARCHIVE)" -C "$$uasm_install"; \
		rm -rf "$(UASM_DIR)"; \
		mv "$$uasm_install/UASM-$(UASM_COMMIT)" "$(UASM_DIR)"; \
		touch "$@"; \
		trap - 0

# UASM's own makefile links with GNU ld's -Map option, which Apple's linker
# rejects, so build its objects there and link them here.
$(UASM): $(UASM_DIR)/gccmod.inc
	@mkdir -p $(dir $@)
	@echo "Building UASM..."
	@uasm_objs="main.o $$(sed -n 's|^\$$(OUTD)/\([A-Za-z0-9_]*\.o\).*|\1|p' \
			$(UASM_DIR)/gccmod.inc)"; \
		$(MAKE) -C $(UASM_DIR) -f $(UASM_MAKEFILE) $(UASM_MAKE_CC_ARG) \
			OUTD=$(abspath $(dir $@)) \
			$$(for o in $$uasm_objs; do printf '%s ' "$(abspath $(dir $@))/$$o"; done) \
			> $(dir $@)build.log 2>&1 || (cat $(dir $@)build.log >&2; exit 1); \
		$(HOST_CC) $$(for o in $$uasm_objs; do printf '%s ' "$(dir $@)$$o"; done) -o $@

# The raster library is assembled; everything else is compiled.
$(OUT_DIR)/%.obj: src/%.asm | $(UASM)
	@mkdir -p $(dir $(OUT_DIR)/$*)
	@$(UASM) -q -coff -Fo$@ -Fw$(OUT_DIR)/$*.err $< > $(OUT_DIR)/$*.stdout || \
		(cat $(OUT_DIR)/$*.stdout >&2; exit 1)

$(IX_SOUND_LIB): $(call objects_of,$(SRCS_IX_SOUND))
$(IX_STREAMER_LIB): $(call objects_of,$(SRCS_IX_STREAMER))
$(IX_DSP_LIB): $(call objects_of,$(SRCS_IX_DSP))
$(IX_LZO_LIB): $(call objects_of,$(SRCS_IX_LZO))
$(IX_FILE_LIB): $(call objects_of,$(SRCS_IX_FILE))
$(IX_LIBS): | $(MSVCRT_DLL)
	@rm -f $@
	$(LIBEXE) /nologo /OUT:$@ $(filter %.obj,$^)

$(PDB_SEED_TOOL): bin/createPdbSeed.c | $(WIBO) $(MSVCRT_DLL)
	@mkdir -p $(OUT_DIR)
	env INCLUDE='$(MSVC_INC)' LIB='$(GAME_LIBPATH)' $(CC) /nologo /MT $< \
		/Fo$(OUT_DIR)/createPdbSeed.obj /Fe$@ /link kernel32.lib

$(PDB_SEED): $(PDB_SEED_TOOL) Makefile $(MSVC41_DIR)/Bin/MSPDB41.DLL
	@rm -f $@.tmp
	env WIBO_PATH='$(MSVC41_DIR)/Bin' $(WIBO) $(PDB_SEED_TOOL) create \
		$(MSVC41_DIR)/Bin/MSPDB41.DLL $@.tmp $(ORIGINAL_PDB_SIGNATURE) $(PDB_SEED_AGE)
	@mv $@.tmp $@

$(TARGET):: $(LINK_OBJS) $(IX_LIBS) $(PDB_SEED) Makefile bin/stampPeTimestamp.py | $(WIBO) $(MSVCRT_DLL)
	@mkdir -p $(dir $(LINK_TARGET)) "$(LINK_PDB_DIR)"
	@cp -f $(PDB_SEED) "$(LINK_PDB_DIR)/wc2.pdb"
	env LIB='$(GAME_LIBPATH)' WIBO_DRIVE_C='$(LINK_DRIVE_C)' $(LINK) $(LINKFLAGS) \
		$(LINK_OBJS) $(IX_LIBS_BEFORE_CRT) $(GAME_LIBS) $(CRT_LIBS) \
		$(IX_LIBS_AFTER_CRT) /OUT:$(LINK_TARGET)
	@env WIBO_PATH='$(MSVC41_DIR)/Bin' $(WIBO) $(PDB_SEED_TOOL) verify \
		$(MSVC41_DIR)/Bin/MSPDB41.DLL "$(LINK_PDB_DIR)/wc2.pdb" \
		$(ORIGINAL_PDB_SIGNATURE) $(ORIGINAL_PDB_AGE)
	@python3 bin/stampPeTimestamp.py $(LINK_TARGET) $(ORIGINAL_TIMESTAMP) \
		--clear-misc-reserved
	@cp -f $(LINK_TARGET) $@

# A prerequisite-free double-colon rule runs after the link rule on every
# invocation, including an up-to-date build, without forcing a relink.
# Keep the hash check after link metadata normalization and the final copy.
$(TARGET)::
	@printf '%s  %s\n' "$(ORIGINAL_SHA256)" "$@" | shasum -a 256 -c -

# CL prints the name of each file it compiles; its output is shown only when
# it fails.
#
# The game-side wave module calls directly into IxSample and IxSound member
# functions.  Those ECX-based calls at 0x42B4A0 and 0x42B680 prove this one
# source unit was compiled as C++ while retaining its original .c filename.
$(OUT_DIR)/sound.obj: src/sound.c $(GAME_HEADERS) | $(WIBO) $(MSVCRT_DLL)
	@mkdir -p $(dir $@)
	@env INCLUDE='$(MSVC_INC)' $(CC) $(CFLAGS_CORE) /TP $< /Fo$@ \
		> $(@:.obj=.stdout) || (cat $(@:.obj=.stdout) >&2; exit 1)

$(OUT_DIR)/%.obj: src/%.c $(GAME_HEADERS) | $(WIBO) $(MSVCRT_DLL)
	@mkdir -p $(dir $@)
	@env INCLUDE='$(MSVC_INC)' $(CC) $(CFLAGS_CORE) $< /Fo$@ \
		> $(@:.obj=.stdout) || (cat $(@:.obj=.stdout) >&2; exit 1)

# ix/ is built unoptimised; see the CFLAGS_IX note above.
$(OUT_DIR)/ix/%.obj: src/ix/%.cpp $(GAME_HEADERS) | $(WIBO) $(MSVCRT_DLL)
	@mkdir -p $(dir $@)
	@env INCLUDE='$(MSVC_INC)' $(CC) $(CFLAGS_IX) /I src\\ix $< /Fo$@ \
		> $(@:.obj=.stdout) || (cat $(@:.obj=.stdout) >&2; exit 1)

# The console-owner wrapper at 0x40FCD0 contains compiler-generated C++
# construction cleanup.  /GX on this unit reproduces it exactly; debug.cpp has
# no such unwind records and stays on the core defaults.
$(OUT_DIR)/console.obj: CFLAGS_CORE_CPP_EXTRA = /GX

$(OUT_DIR)/%.obj: src/%.cpp $(GAME_HEADERS) | $(WIBO) $(MSVCRT_DLL)
	@mkdir -p $(dir $@)
	@env INCLUDE='$(MSVC_INC)' $(CC) $(CFLAGS_CORE) $(CFLAGS_CORE_CPP_EXTRA) $< /Fo$@ \
		> $(@:.obj=.stdout) || (cat $(@:.obj=.stdout) >&2; exit 1)

# ---------------------------------------------------------------------------
# Original binary
# ---------------------------------------------------------------------------

# The retail executable is not vendored.  Copy it in from the analysis tree.
$(ORIGINAL_EXE):
	@test -f "$(ORIGINAL_SRC)" || \
		(echo "Error: original not found at $(ORIGINAL_SRC). Set ORIGINAL_SRC=/path/to/WC2.EXE" >&2 && exit 1)
	@mkdir -p $(dir $@)
	@cp -f "$(ORIGINAL_SRC)" $@
	@echo "Staged original -> $@"

# Temporary diagnostic build: replace the "bad vport" text with the failing
# viewport's address, its fields and the allocation registry, so the caller can
# be identified.  Rebuilds only gr.c; `make` restores the retail build.
vport-debug:
	@rm -f $(OUT_DIR)/gr.obj
	@$(MAKE) EXTRA_CFLAGS=/DVPORT_DEBUG

# ---------------------------------------------------------------------------
# DREAMM launch targets
# ---------------------------------------------------------------------------

$(DREAMM_STAMP):
	@mkdir -p $(DREAMM_DIR)
	@echo "Downloading DREAMM $(DREAMM_VERSION)..."
	@curl -L -o $(DREAMM_DIR)/$(DREAMM_ARCHIVE) $(DREAMM_BASE_URL)/$(DREAMM_ARCHIVE)
ifeq ($(UNAME_S),Darwin)
	@rm -rf $(DREAMM_DIR)/mnt
	@hdiutil attach $(DREAMM_DIR)/$(DREAMM_ARCHIVE) -mountpoint $(DREAMM_DIR)/mnt -nobrowse -quiet
	@rm -rf $(DREAMM_DIR)/DREAMM.app
	@cp -R $(DREAMM_DIR)/mnt/DREAMM.app $(DREAMM_DIR)/
	@hdiutil detach $(DREAMM_DIR)/mnt -quiet
	@xattr -dr com.apple.quarantine $(DREAMM_DIR)/DREAMM.app
else
	@rm -rf $(DREAMM_DIR)/dreamm
	@tar xzf $(DREAMM_DIR)/$(DREAMM_ARCHIVE) -C $(DREAMM_DIR) --strip-components=1
endif
	@rm $(DREAMM_DIR)/$(DREAMM_ARCHIVE)
	@rm -f $(DREAMM_DIR)/.dreamm-*.stamp
	@touch "$(DREAMM_STAMP)"

$(DREAMM_BIN): $(DREAMM_STAMP)
	@test -x "$(DREAMM_BIN)" || \
		(echo "Error: DREAMM did not unpack to $(DREAMM_BIN)." >&2 && exit 1)

dreamm: $(DREAMM_BIN)

# The Kilrathi Saga disc carries a ready-to-run WC2 tree at /WC2 (WC2.EXE,
# GAMEDAT with the MODULE/CAMP/BRIEFING files, STREAMS, WINGCMDR.CFG).  Extract
# just that -- 142 MB of the disc's 634 -- rather than asking for a separate
# install.  bsdtar reads ISO9660 directly and ships with macOS and most Linuxes.
$(RUN_DIR)/GAMEDAT:
	@test -n "$(ISO)" || \
		(echo "Error: no disc image found. Put the Kilrathi Saga ISO in data/ or set ISO=." >&2 && exit 1)
	@command -v bsdtar >/dev/null 2>&1 || \
		(echo "Error: bsdtar not found; needed to read the ISO." >&2 && exit 1)
	@echo "Extracting WC2 from $(ISO)..."
	@mkdir -p "$(RUN_DIR)"
	@bsdtar -xf "$(ISO)" -C "$(RUN_DIR)" --strip-components=1 WC2

run-check: $(RUN_DIR)/GAMEDAT
	@mkdir -p "$(RUN_DIR)/hd"

stage-run: $(TARGET) run-check
	cp -f $(TARGET) "$(RUN_DIR)/WC2.EXE"

run: stage-run | $(DREAMM_BIN)
	cd "$(RUN_DIR)" && $(DREAMM) $(DREAMM_MOUNTS) $(DREAMM_PROPS) -launch WC2.EXE

run-original: run-check $(ORIGINAL_EXE) | $(DREAMM_BIN)
	cp -f $(ORIGINAL_EXE) "$(RUN_DIR)/WC2.ORI.EXE"
	cd "$(RUN_DIR)" && $(DREAMM) $(DREAMM_MOUNTS) $(DREAMM_PROPS) -launch WC2.ORI.EXE

# DREAMM's own debugger, the same target the sibling project uses.
debug: $(TARGET) run-check | $(DREAMM_BIN)
	cp -f $(TARGET) "$(RUN_DIR)/WC2.EXE"
	cd "$(RUN_DIR)" && $(DREAMM) $(DREAMM_MOUNTS) $(DREAMM_PROPS) -debug \
		-launch WC2.EXE > debug.log

# ---------------------------------------------------------------------------
# Source browser
# ---------------------------------------------------------------------------

# Same static Doxygen/Pygments browser as wc1-re. Only the Win32 branches
# are staged; the matching sources are never rewritten. No game data or
# original toolchain is needed. Needs clang, doxygen, unifdef and pygmentize.
# Output: out/site/html/index.html.
site:
	@sh site/build.sh $(SRCS)

# ---------------------------------------------------------------------------
# Cleanup and phony declarations
# ---------------------------------------------------------------------------

clean:
	rm -rf $(OUT_DIR)/*.obj $(OUT_DIR)/*.stdout $(OUT_DIR)/*.err \
	       $(OUT_DIR)/ix $(TARGET) $(LINK_TARGET)

clean-run:
	rm -f "$(RUN_DIR)/WC2.EXE" "$(RUN_DIR)/WC2.ORI.EXE" \
	       "$(RUN_DIR)/debug.log"

clean-dreamm:
	rm -rf $(DREAMM_DIR)

clean-modern:
	rm -rf $(MODERN_OUT_DIR)

.PHONY: all verify-hash vport-debug msvc41-toolchain \
	dreamm run run-check stage-run run-original debug \
	modern modern-gui modern-check-deps modern-test-cutscenes \
	run-modern run-modern-gui run-modern-cutscene run-modern-dos run-modern-mission \
	site clean clean-run clean-dreamm clean-modern
