Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
Doom: chosen maps from other WADs in the engine and tools, frame rate scan build
Mason3632ffe49247f8a
diff --git a/games/doom/Makefile b/games/doom/Makefileindex 71ac3493..2e1d6ca2 100644--- a/games/doom/Makefile+++ b/games/doom/Makefile@@ -5,6 +5,8 @@# make test the game played on a computer (tests/check.py, with the sanitizers)# make media the pictures of docs/ and the animations of the repository's docs/media (tools/media.py)# make data src/doom1.whx from the shareware DOOM1.WAD (tools/fetch.py downloads it; WAD=its path)+# make WAD=path/to/doom2.wad [PWAD="maps.wad ..."] [MAPS="MAP01-MAP04"]+# output/doom.nwa from another WAD (the README's "Doom from your own WAD"); host, test and check tooQ ?= @# the ARM toolchain on the PATH, or in TOOLCHAIN=<its bin folder>TOOLCHAIN ?=@@ -17,9 +19,17 @@ SIZE = $(BIN)arm-none-eabi-sizeBUILD_DIR = outputWAD ?=+# the packed WAD: the shareware one, or one made from WAD= (below)+WHX = src/doom1.whx+ifneq ($(WAD),)+ifeq ($(filter data,$(MAKECMDGOALS)),)+WHX = build/wad/doom.whx+endif+endif+# the engine (Chocolate Doom by way of RP2040 Doom), and the calculator's side of it (src/nw)ENGINE = $(wildcard src/*.c) $(wildcard src/doom/*.c)-NW = src/nw/i_system.c src/nw/i_video.c src/nw/i_input.c src/nw/i_sound.c src/nw/saves.c src/nw/main.c+NW = src/nw/i_system.c src/nw/i_video.c src/nw/i_input.c src/nw/i_sound.c src/nw/saves.c src/nw/main.c src/nw/scan.cHEADERS = $(wildcard src/*.h src/doom/*.h src/nw/*.h src/nw/*.inc src/compat/*.h src/compat/*/*.h) ../common/epsilon_app.h \../common/epsilon_files.hINC = -include src/nw/doomconf.h -Isrc -Isrc/doom -Isrc/compat -Isrc/nw@@ -49,6 +59,8 @@ build: $(BUILD_DIR)/doom.nwacheck: $(BUILD_DIR)/doom.nwa$(Q) $(NWLINK) nwa-bin --ram-length 133652 $< $(BUILD_DIR)/doom.bin@echo "BIN $(BUILD_DIR)/doom.bin: $$(wc -c < $(BUILD_DIR)/doom.bin) bytes"+ @test $$(wc -c < $(BUILD_DIR)/doom.bin) -le 2031616 || { echo "too big for the calculator's 2031616 bytes \+ (MAPS= fewer maps)"; exit 1; }$(BUILD_DIR)/obj/nw/%.o: src/nw/%.c $(HEADERS)@mkdir -p $(@D)@@ -59,8 +71,10 @@ $(BUILD_DIR)/obj/%.o: src/%.c $(HEADERS)$(BUILD_DIR)/obj/pd_render.o: src/pd_render.cpp $(HEADERS)@mkdir -p $(@D)$(Q) $(CXX) $(CXXFLAGS) -Wall -Wextra $(ENGINE_W) -c $< -o $@-$(BUILD_DIR)/data.o: src/nw/data.S src/doom1.whx | $(BUILD_DIR)- $(Q) $(CC) -c -DDOOM_WHX='"src/doom1.whx"' $< -o $@+$(BUILD_DIR)/data.o: src/nw/data.S $(WHX) $(BUILD_DIR)/whx | $(BUILD_DIR)+ $(Q) $(CC) -c -DDOOM_WHX='"$(WHX)"' $< -o $@+$(BUILD_DIR)/whx: FORCE | $(BUILD_DIR)+ @echo '$(WHX)' | cmp -s - $@ || echo '$(WHX)' > $@$(BUILD_DIR)/doom.nwa: $(OBJ) $(BUILD_DIR)/data.o $(BUILD_DIR)/icon.o@echo "LD $@"@@ -90,15 +104,18 @@ build/host/pd_render.o: src/pd_render.cpp $(HEADERS)build/host/play.o: tests/play.c $(HEADERS)@mkdir -p $(@D)$(Q) cc -std=gnu11 $(HOST_FLAGS) $(HOST_SAN) -Wall -Wextra -c $< -o $@-build/host/data.o: src/nw/data.S src/doom1.whx+build/host/data.o: src/nw/data.S $(WHX) build/host/whx+ @mkdir -p $(@D)+ $(Q) cc -c -DDOOM_WHX='"$(WHX)"' $< -o $@+build/host/whx: FORCE@mkdir -p $(@D)- $(Q) cc -c -DDOOM_WHX='"src/doom1.whx"' $< -o $@+ @echo '$(WHX)' | cmp -s - $@ || echo '$(WHX)' > $@build/play: $(HOST_OBJ)$(Q) c++ $(HOST_SAN) $^ -lm -o $@@echo "HOST $@"test: build/play- python3 tests/check.py build/play+ python3 tests/check.py build/play $(if $(filter build/wad/%,$(WHX)),--wad)media: build/playpython3 tools/media.py build/play@@ -109,7 +126,7 @@ WHD_GEN_C = tools/whd_gen/musx_decoder.c tools/whd_gen/adpcm-lib.c src/tiny_huffWHD_GEN_OBJ = $(patsubst %.cpp,build/whd_gen_obj/%.o,$(notdir $(wildcard tools/whd_gen/*.cpp))) \$(patsubst %.c,build/whd_gen_obj/%.o,$(notdir $(WHD_GEN_C)))WHD_GEN_FLAGS = -O2 -DIS_WHD_GEN=1 -Itools/whd_gen -Isrc -Isrc/doom -w-build/whd_gen_obj/%.o: tools/whd_gen/%.cpp $(wildcard tools/whd_gen/*.h)+build/whd_gen_obj/%.o: tools/whd_gen/%.cpp $(wildcard tools/whd_gen/*.h) src/whddata.h@mkdir -p $(@D)$(Q) c++ -std=gnu++17 $(WHD_GEN_FLAGS) -c $< -o $@build/whd_gen_obj/%.o: tools/whd_gen/%.c@@ -124,5 +141,17 @@ data: $(WHD_GEN)@test -n "$(WAD)" || { echo "make data WAD=<the shareware DOOM1.WAD> (python3 tools/fetch.py downloads it)"; exit 1; }$(WHD_GEN) -silent -lossless $(WAD) src/doom1.whx+# another WAD: its chosen maps and what they show (tools/pick.py), packed as the calculator reads it+PICKED = build/wad/$(shell echo '$(notdir $(WAD))' | tr A-Z a-z)+build/wad/args: FORCE+ @mkdir -p $(@D)+ @echo '$(abspath $(WAD) $(PWAD)) $(MAPS)' | cmp -s - $@ || echo '$(abspath $(WAD) $(PWAD)) $(MAPS)' > $@+build/wad/doom.whx: build/wad/args $(WHD_GEN) tools/pick.py $(WAD) $(PWAD)+ python3 tools/pick.py $(WAD) $(PWAD) --maps "$(MAPS)" --out build/wad+ $(Q) $(WHD_GEN) -silent -lossless -drop-unused $(PICKED) $@ > build/wad/whd_gen.log || \+ { tail -3 build/wad/whd_gen.log; rm -f $@; exit 1; }+ @echo "WHX $@: $$(wc -c < $@) bytes"+.PHONY: FORCE+clean:$(Q) rm -rf $(BUILD_DIR) builddiff --git a/games/doom/src/doom/d_main.c b/games/doom/src/doom/d_main.cindex db2510d6..4777748b 100644--- a/games/doom/src/doom/d_main.c+++ b/games/doom/src/doom/d_main.c@@ -665,6 +665,17 @@ void D_AdvanceDemo (void)// This cycles through the demo sequences.// FIXME - version dependend demo numbers?//+#if NUMWORKS+// (a build of chosen maps, tools/pick.py, leaves out the demos of the maps it leaves out: the next page instead)+static boolean D_NwDemoIn(const char *name)+{+ if (W_CheckNumForName(name) >= 0)+ return true;+ advancedemo = true;+ return false;+}+#define G_DeferedPlayDemo(name) (D_NwDemoIn(name) ? G_DeferedPlayDemo(name) : (void) 0)+#endifvoid D_DoAdvanceDemo (void){players[consoleplayer].playerstate = PST_LIVE; // not reborn@@ -752,6 +763,9 @@ void D_DoAdvanceDemo (void)pagename = DEH_String("INTERPIC");}}+#if NUMWORKS+#undef G_DeferedPlayDemo+#endif@@ -921,6 +935,19 @@ void D_IdentifyVersion(void)#if !DEMO1_ONLY// Make sure gamemode is set up correctly+#if NUMWORKS+ // (a build of chosen maps, tools/pick.py, says which game the full WAD is)+ int nwmode = W_CheckNumForName("NWMODE");+ if (nwmode >= 0)+ {+ const char *m = W_CacheLumpNum(nwmode, PU_STATIC);+ gamemode = m[0] == 'c' ? commercial : m[0] == 'r' && m[1] == 'e' && m[2] == 't' ? retail+ : m[0] == 'r' ? registered : shareware;+ if (gamemode == commercial && gamemission == doom)+ gamemission = doom2;+ return;+ }+#endifif (logical_gamemission == doom){// Doom 1. But which version?@@ -2169,6 +2196,16 @@ void D_DoomMain (void)detailLevel = strchr(v, ' ') ? atoi(strchr(v, ' ')) : 0;M_NwViewLoaded();}+#endif+#if NW_SCAN+ {+ // (the emulator's frame rate build, nw/scan.c: the full size, in high or low detail, frames as they come)+ extern int nw_sync_off;+ screenblocks = 10;+ detailLevel = NW_SCAN == 2;+ M_NwViewLoaded();+ nw_sync_off = 1;+ }#endifif (nw_warp){diff --git a/games/doom/src/doom/g_game.c b/games/doom/src/doom/g_game.cindex a45147bc..d5a49066 100644--- a/games/doom/src/doom/g_game.c+++ b/games/doom/src/doom/g_game.c@@ -1185,6 +1185,9 @@ void G_Ticker (void)#if HOST{ extern void host_tic(void); host_tic(); }#endif+#if NW_SCAN+ { extern void nw_scan_tic(void); nw_scan_tic(); }+#endif}@@ -1506,10 +1509,35 @@ void G_SecretExitLevel (void)gameaction = ga_completed;}-void G_DoCompleted (void)-{- int i;-+#if NUMWORKS+// a build of chosen maps (tools/pick.py) may leave some out: whether a map is in, and the first one in from a map+// to another (0: none)+boolean G_NwMapIn(int episode, int map)+{+ char name[9];+ if (gamemode == commercial)+ M_snprintf(name, sizeof name, "MAP%02d", map);+ else+ M_snprintf(name, sizeof name, "E%dM%d", episode, map);+ return W_CheckNumForName(name) >= 0;+}++int G_NwNextMapIn(int episode, int map, int last)+{+ for (; map <= last; map++)+ if (G_NwMapIn(episode, map))+ return map;+ return 0;+}++// (the last map in is done: Doom II's game ends as after MAP30)+static boolean nw_game_ends;+#endif++void G_DoCompleted (void)+{+ int i;+gameaction = ga_nothing;for (i=0 ; i<MAXPLAYERS ; i++)@@ -1616,10 +1644,30 @@ void G_DoCompleted (void)break;}}- else- wminfo.next = gamemap; // go to next level+ else+ wminfo.next = gamemap; // go to next level}-+#if NUMWORKS+ // (the next map is left out: a secret level's exit goes on as the normal one, then to the next map that is+ // in; with none, the episode ends, as after its eighth map, or Doom II's game as after MAP30)+ nw_game_ends = false;+ if (!G_NwMapIn(gameepisode, wminfo.next + 1))+ {+ int last = gamemode == commercial ? 30 : 8;+ int next = gamemap > last ? 16 : secretexit && gamemap < last ? gamemap + 1 : wminfo.next + 2;+ next = G_NwNextMapIn(gameepisode, next, last);+ if (next)+ wminfo.next = next - 1;+ else if (gamemode != commercial)+ {+ gameaction = ga_victory;+ return;+ }+ else+ nw_game_ends = true;+ }+#endif+wminfo.maxkills = totalkills;wminfo.maxitems = totalitems;wminfo.maxsecret = totalsecret;@@ -1685,6 +1733,14 @@ void G_WorldDone (void)if (secretexit)players[consoleplayer].didsecret = true;+#if NUMWORKS+ if (nw_game_ends)+ {+ gamemap = 30;+ F_StartFinale ();+ return;+ }+#endifif ( gamemode == commercial ){switch (gamemap)@@ -2123,6 +2179,18 @@ G_InitNewif ( (map > 9)&& ( gamemode != commercial) )map = 9;+#if NUMWORKS+ // (a map left out: the first one in from it, or from the episode's first)+ if (!G_NwMapIn(episode, map))+ {+ int last = gamemode == commercial ? 32 : 9;+ int m = G_NwNextMapIn(episode, map, last);+ if (!m)+ m = G_NwNextMapIn(episode, 1, last);+ if (m)+ map = m;+ }+#endifM_ClearRandom ();diff --git a/games/doom/src/doom/g_game.h b/games/doom/src/doom/g_game.hindex d34c1ca4..f0bbacc2 100644--- a/games/doom/src/doom/g_game.h+++ b/games/doom/src/doom/g_game.h@@ -36,6 +36,10 @@ void G_InitNew (skill_t skill, int episode, int map);// Can be called by the startup code or M_Responder.// A normal game starts at map 1,// but a warp test can start elsewhere+#if NUMWORKS+boolean G_NwMapIn(int episode, int map);+int G_NwNextMapIn(int episode, int map, int last);+#endifvoid G_DeferedInitNew (skill_t skill, int episode, int map, boolean net);void G_DeferedPlayDemo (const char* demo);diff --git a/games/doom/src/doom/m_menu.c b/games/doom/src/doom/m_menu.cindex 010d560b..12736029 100644--- a/games/doom/src/doom/m_menu.c+++ b/games/doom/src/doom/m_menu.c@@ -1260,6 +1260,13 @@ void M_Episode(int choice)M_SetupNextMenu(&ReadDef1);return;}+#if NUMWORKS+ if (!G_NwNextMapIn(choice + 1, 1, 9))+ {+ M_StartMessage("THIS EPISODE IS NOT IN THIS\nBUILD OF DOOM.\n\n" PRESSKEY, NULL, false);+ return;+ }+#endifepi = choice;M_SetupNextMenu(&NewDef);diff --git a/games/doom/src/doom/r_state.h b/games/doom/src/doom/r_state.hindex 95f84e67..e0fc1980 100644--- a/games/doom/src/doom/r_state.h+++ b/games/doom/src/doom/r_state.h@@ -269,7 +269,7 @@ boolean is_switched_texture(const side_t *side, int where);#if WHD_SUPER_TINY#define sidenum_to_side(sidenum) (sides_z + (sidenum))static inline uint side_sectornum(side_t *side) {- return side[1];+ return side[1] | (side[0] & 128) << 1; // (the top bit of the first byte: sectors up to 511)}#define side_sector(side) (sectors + side_sectornum(side))@@ -306,6 +306,14 @@ static inline int side_textureoffset16(side_t *side) {return toff;}#define side_textureoffset(side) (side_textureoffset16(side) << FRACBITS)+// a packed side's bytes: its kind, sector, textures, texture offset and row offset (a line's back side follows its+// front one: from a front side of 7 bytes or more, the line does not say how far, nw/pick.py)+static inline int side_bytes(const side_t *side) {+ static const uint8_t textures[16] = {0, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 2, 2, 2, 2, 3};+ int n = 2 + textures[side[0] & 0xf] + ((side[0] >> 4) & 3);+ if (side[0] & 64) n += side[n] & 128 ? 2 : 1;+ return n;+}static inline int side_rowoffset16(side_t *side) {// todo is this shared with the other?static uint8_t offset_offset[16] = {2, 3, 3, 3, 4, 3, 3, 4, 3, 3, 4, 4, 4, 4, 4, 5};@@ -397,13 +405,15 @@ static inline int line_sidenum(const line_t *l, int side) {if (side && line_onesided(l)) return -1;int s;if (line_predict_side(l)) {- s = ((l - lines) * whd_sidemul) >> 16;+ s = ((uint32_t) (l - lines) * whd_sidemul) >> 14; // (whd_gen: a quarter, as big maps need)s += (int8_t)l[2];} else {s = l[2] + (l[3] << 8);}if (side) {- s += l[1] >> 5;+ int d = l[1] >> 5;+ if (d == 7) d = side_bytes(sidenum_to_side(s));+ s += d;}return s;}diff --git a/games/doom/src/doom/st_stuff.c b/games/doom/src/doom/st_stuff.cindex c37c1aa2..66e6cdf3 100644--- a/games/doom/src/doom/st_stuff.c+++ b/games/doom/src/doom/st_stuff.c@@ -1501,6 +1501,10 @@ ST_Responder (event_t* ev)}}+#if NUMWORKS+ if (!G_NwMapIn(epsd, map))+ return false; // (left out of this build)+#endif// So be it.plyr->message = DEH_String(STSTR_CLEV);G_DeferedInitNew(gameskill, epsd, map, false);diff --git a/games/doom/src/doom1.whx b/games/doom/src/doom1.whxindex c3339691..72694b98 100644Binary files a/games/doom/src/doom1.whx and b/games/doom/src/doom1.whx differdiff --git a/games/doom/src/nw/i_video.c b/games/doom/src/nw/i_video.cindex fa7460d6..cf4668f4 100644--- a/games/doom/src/nw/i_video.c+++ b/games/doom/src/nw/i_video.c@@ -128,6 +128,9 @@ void nw_frame_end(void) {perf_frames++;last_ms = now;last_slept = nw_slept_ms;+#if NW_SCAN+ { extern void nw_scan_frame(void); nw_scan_frame(); }+#endif}#endifdiff --git a/games/doom/src/nw/nw_menu.inc b/games/doom/src/nw/nw_menu.incindex 0e2b5f5d..ab78e405 100644--- a/games/doom/src/nw/nw_menu.inc+++ b/games/doom/src/nw/nw_menu.inc@@ -156,7 +156,16 @@ static boolean M_NwCheatSide(int right){int maps = gamemode == commercial ? 32 : 9;if (itemOn == ch_warp)- nw_warp_map = (nw_warp_map + (right ? 1 : maps - 1) - 1) % maps + 1;+ {+ // (the maps of this build)+ int episode = gamemode == commercial ? 0 : gameepisode ? gameepisode : 1;+ for (int k = 0; k < maps; k++)+ {+ nw_warp_map = (nw_warp_map + (right ? 1 : maps - 1) - 1) % maps + 1;+ if (G_NwMapIn(episode, nw_warp_map))+ break;+ }+ }else if (itemOn == ch_power)nw_power = (nw_power + (right ? 1 : 5)) % 6;elsediff --git a/games/doom/src/nw/scan.c b/games/doom/src/nw/scan.cnew file mode 100644index 00000000..eda94c31--- /dev/null+++ b/games/doom/src/nw/scan.c@@ -0,0 +1,64 @@+/* Doom on the NumWorks calculator: a build for measuring the frame rate in the emulator (make EXTRA="-DNW_WARP=11+ * -DNW_SCAN=1", tools/fps.py). The level it starts in is seen from all over: a frame from the middle of each+ * subsector looking 8 ways round, with god mode on, as tests/play.c --scan does on a computer. NW_SCAN=2 draws+ * in low detail. When every view has been drawn, nw_scan_done is set. */+#if NW_SCAN+#include "doomtype.h"+#include "doom/doomstat.h"+#include "doom/p_local.h"+#include "nw.h"++/* the view drawn (the emulator reads them) */+__attribute__((used)) volatile int nw_scan_ss = -1, nw_scan_dir;+__attribute__((used)) volatile uint32_t nw_scan_done;+static fixed_t scan_x, scan_y;++static int scan_middle(int i, fixed_t *x, fixed_t *y) {+ subsector_t *sub = &subsectors[i];+ seg_t *line = subsector_firstline(sub), *limit = subsector_linelimit(sub);+ int64_t sx = 0, sy = 0;+ int n = 0;+ for (; line < limit; line += seg_next_step(line), n++) {+ sx += vertex_x(seg_v1(line));+ sy += vertex_y(seg_v1(line));+ }+ if (!n) return 0;+ *x = (fixed_t)(sx / n);+ *y = (fixed_t)(sy / n);+ sector_t *s = subsector_sector(sub);+ return R_PointInSubsector(*x, *y) == sub && sector_ceilingheight(s) - sector_floorheight(s) >= 56 * FRACUNIT;+}++/* the next view */+static void scan_next(void) {+ if (nw_scan_ss >= 0 && ++nw_scan_dir < 8) return;+ nw_scan_dir = 0;+ do {+ if (++nw_scan_ss >= (int)numsubsectors) {+ nw_scan_done = 1;+ nw_scan_ss = 0;+ }+ } while (!scan_middle(nw_scan_ss, &scan_x, &scan_y));+}++/* each tic: the player where the view is */+void nw_scan_tic(void) {+ player_t *p = &players[consoleplayer];+ mobj_t *mo = p->mo;+ if (gamestate != GS_LEVEL || !mo) return;+ if (nw_scan_ss < 0) scan_next();+ p->cheats |= CF_GODMODE;+ P_UnsetThingPosition(mo);+ mo->xy.x = scan_x;+ mo->xy.y = scan_y;+ P_SetThingPosition(mo);+ mo->z = sector_floorheight(mobj_sector(mo));+ p->viewz = mo->z + VIEWHEIGHT;+ mobj_full(mo)->angle = (angle_t)nw_scan_dir * ANG45;+}++/* each frame drawn: the next view */+void nw_scan_frame(void) {+ if (gamestate == GS_LEVEL && nw_scan_ss >= 0) scan_next();+}+#endifdiff --git a/games/doom/tools/fps.py b/games/doom/tools/fps.pynew file mode 100644index 00000000..3cf68e05--- /dev/null+++ b/games/doom/tools/fps.py@@ -0,0 +1,93 @@+#!/usr/bin/env python3+"""fps.py [--maps 11,12,...] [--low] [--out FILE]: the frame rate in every level, measured in the emulator+(tools/emu.py, with the calculator's 133652 bytes of RAM). A build that starts in the level and draws it from+all over (src/nw/scan.c: the middle of each subsector, looking 8 ways round, monsters awake) is run until every+view has been drawn, and the time of each frame is noted: its instructions (each frame's game tics, drawing and+sending to the display) and 40 ns for each pixel sent.++Times are given for the N0110 (216 MHz) and the N0120 (550 MHz, the same instructions taken 216/550 as long).+A frame within 1/35 s keeps up with the game's 35 tics a second; the frame rate of a view is 35 when it does,+else 1000 / its time (the game runs its tics at their rate, and draws as often as it can).++Needs the ARM toolchain (TOOLCHAIN=<its bin folder>, or on the PATH) and nwlink, as the build does."""+import argparse+import os+import struct+import subprocess+import sys++HERE = os.path.dirname(os.path.abspath(__file__))+GAME = os.path.dirname(HERE)+sys.path.insert(0, os.path.join(GAME, "..", "..", "tools"))+import emu # noqa: E402++ap = argparse.ArgumentParser()+ap.add_argument("--maps", default="11,12,13,14,15,16,17,18,19")+ap.add_argument("--low", action="store_true", help="low detail")+ap.add_argument("--build", default=os.path.join(GAME, "build", "fps"))+ap.add_argument("--out", default="")+ap.add_argument("--toolchain", default=os.environ.get("TOOLCHAIN", ""))+ap.add_argument("--nwlink", default=os.environ.get("NWLINK", "nwlink"))+a = ap.parse_args()++PUSH_MS = 320 * 240 * 40e-6 # a frame's pixels to the display+TIC_MS = 1000 / 35+rows = []+for m in (int(x) for x in a.maps.split(",")):+ bd = os.path.join(a.build, f"{m}{'low' if a.low else ''}")+ args = ["make", "-C", GAME, "-j4", "build", f"NWLINK={a.nwlink}", f"BUILD_DIR={bd}",+ f"EXTRA=-DNW_WARP={m} -DNW_SCAN={2 if a.low else 1}"]+ if a.toolchain:+ args.append(f"TOOLCHAIN={a.toolchain}")+ r = subprocess.run(args, capture_output=True, text=True)+ if r.returncode:+ sys.exit(f"E{m // 10}M{m % 10}: the build failed\n{r.stderr[-2000:]}")+ emu.configure(model="n0110", ram_length=133652)+ c = emu.Calculator(os.path.join(bd, "doom.nwa"))+ sym = c.symbols++ def u32(name):+ return struct.unpack("<I", bytes(c.uc.mem_read(sym[name], 4)))[0]++ chunk = emu.CPU_HZ // 10000 # a tenth of a millisecond+ frames = [] # (work ms at 216 MHz, the view: subsector, direction)+ last_frames = u32("perf_frames")+ last_t = c.now_ms - c.slept_ms+ started = False+ while not c.exited and not u32("nw_scan_done"):+ c.uc.emu_start(c.pc, 0xFFFFFFFF, count=chunk)+ c.pc = c.reg(emu.UC_ARM_REG_PC) | 1+ c.insns += chunk+ c.now_ms += 0.1+ n = u32("perf_frames")+ if n != last_frames:+ t = c.now_ms - c.slept_ms+ ss = struct.unpack("<i", bytes(c.uc.mem_read(sym["nw_scan_ss"], 4)))[0]+ if started:+ frames.append((t - last_t, ss))+ started = started or ss >= 0+ last_frames, last_t = n, t+ if c.now_ms > 3_600_000:+ sys.exit(f"E{m // 10}M{m % 10}: the scan did not end")+ # (each frame's pushes were counted as instructions by the emulator, a pixel for a quarter of one)+ work = sorted(f[0] - 320 * 240 / 4 / 216e3 for f in frames)++ def model(k):+ ms = [w * k + PUSH_MS for w in work]+ fps = [35 if t <= TIC_MS else 1000 / t for t in ms]+ return {"median_ms": ms[len(ms) // 2], "p99_ms": ms[int(len(ms) * 0.99)], "max_ms": ms[-1],+ "at35": sum(1 for t in ms if t <= TIC_MS) / len(ms), "min_fps": min(fps),+ "mean_fps": len(fps) / sum(1 / f for f in fps)}++ row = {"map": f"E{m // 10}M{m % 10}", "frames": len(work), "n0110": model(1.0), "n0120": model(216 / 550)}+ rows.append(row)+ line = f"{row['map']} {'low' if a.low else 'high'} detail, {len(work)} views:"+ for k in ("n0120", "n0110"):+ s = row[k]+ line += (f" {k.upper()} median {s['median_ms']:.1f} ms, 99% within {s['p99_ms']:.1f} ms, slowest "+ f"{s['max_ms']:.1f} ms; {100 * s['at35']:.1f}% at 35 fps, lowest {s['min_fps']:.0f} fps, "+ f"mean {s['mean_fps']:.1f} fps")+ print(line, flush=True)+if a.out:+ import json+ json.dump(rows, open(a.out, "w"), indent=1)diff --git a/games/doom/tools/pick.py b/games/doom/tools/pick.pynew file mode 100644index 00000000..11f50d33--- /dev/null+++ b/games/doom/tools/pick.py@@ -0,0 +1,414 @@+#!/usr/bin/env python3+"""pick.py IWAD [PWAD...] [--maps "E1M1 E1M2 ..."] --out DIR: the WAD a calculator build is made from (the+Makefile's WAD=, PWAD= and MAPS=, see the README).++The PWADs go over the IWAD in order, as Chocolate Doom's -merge puts them: their maps, sprites, flats and wall+patches replace those of the same name (or are added), and their other lumps replace the IWAD's (TEXTURE1,+PNAMES...). Then only the chosen maps are kept, and only what they can show: the wall textures their sides use+(and those vanilla Doom needs by name: switches, animations, skies), the wall patches of those, the flats of their+floors and ceilings (and the animated ones, the sky's and the screens'), and the sprites of the things in them,+of what those things can turn into, fire or leave behind, and of the player and the weapons. What is left out of+the sprites and wall patches becomes an empty picture (a thing that is missing shows nothing, it does not stop+the game). A lump NWMODE says which game the full WAD is (shareware, registered, retail or commercial), as the+maps left out can no longer tell.++MAPS: map names (E2M1, MAP07), episodes (E2: its maps) or ranges (MAP01-MAP06); all of them when not given. The+WAD is written as DIR/<the IWAD's file name> (its name tells the game which one it is: doom.wad, doom2.wad,+freedoom1.wad...)."""+import argparse+import os+import re+import struct+import sys++HERE = os.path.dirname(os.path.abspath(__file__))+SRC = os.path.join(os.path.dirname(HERE), "src")+MAP_LUMPS = ("THINGS", "LINEDEFS", "SIDEDEFS", "VERTEXES", "SEGS", "SSECTORS", "NODES", "SECTORS", "REJECT",+ "BLOCKMAP")+SECTIONS = {"S": ("S_START", "S_END"), "F": ("F_START", "F_END"), "P": ("P_START", "P_END")}+# a 1 x 1 picture with nothing in it+EMPTY_PATCH = struct.pack("<hhhhI", 1, 1, 0, 0, 12) + b"\xff"+# the flats the game draws besides the maps' (the ending's text screens, the keys page, the border of the smaller+# screen sizes, the sky's)+SCREEN_FLATS = {"FLOOR4_8", "SFLR6_1", "MFLR8_4", "MFLR8_3", "SLIME16", "RROCK14", "RROCK07", "RROCK17", "RROCK13",+ "RROCK19", "FLOOR7_2", "GRNROCK", "F_SKY1"}+# the player's own: the weapons, what they fire and hit, teleporting, items coming back, crushed bodies+ALWAYS_SPRITES = {"PLAY", "PUNG", "PISG", "PISF", "SHTG", "SHTF", "SHT2", "CHGG", "CHGF", "MISG", "MISF", "SAWG",+ "PLSG", "PLSF", "BFGG", "BFGF", "MISL", "PLSS", "PLSE", "BFS1", "BFE1", "BFE2", "PUFF", "BLUD",+ "TFOG", "IFOG", "POL5"}+# what action functions spawn (p_enemy.c), and what monsters drop when killed (P_KillMobj)+SPAWNS = {"A_TroopAttack": ["MT_TROOPSHOT"], "A_HeadAttack": ["MT_HEADSHOT"], "A_CyberAttack": ["MT_ROCKET"],+ "A_BruisAttack": ["MT_BRUISERSHOT"], "A_BspiAttack": ["MT_ARACHPLAZ"], "A_FatAttack1": ["MT_FATSHOT"],+ "A_FatAttack2": ["MT_FATSHOT"], "A_FatAttack3": ["MT_FATSHOT"], "A_SkelMissile": ["MT_TRACER"],+ "A_Tracer": ["MT_SMOKE"], "A_VileTarget": ["MT_FIRE"], "A_PainAttack": ["MT_SKULL"],+ "A_PainDie": ["MT_SKULL"], "A_BrainSpit": ["MT_SPAWNSHOT"], "A_SpawnFly": ["MT_SPAWNFIRE"],+ "A_BrainScream": ["MT_ROCKET"], "A_BrainExplode": ["MT_ROCKET"]}+DROPS = {"MT_WOLFSS": "MT_CLIP", "MT_POSSESSED": "MT_CLIP", "MT_SHOTGUY": "MT_SHOTGUN", "MT_CHAINGUY": "MT_CHAINGUN"}+# what the boss brain's cubes turn into (A_SpawnFly)+BRAIN_SPAWNS = ["MT_TROOP", "MT_SERGEANT", "MT_SHADOWS", "MT_PAIN", "MT_HEAD", "MT_VILE", "MT_UNDEAD", "MT_BABY",+ "MT_FATSO", "MT_KNIGHT", "MT_BRUISER"]+++def fail(msg):+ sys.exit(f"pick.py: {msg}")+++def read_wad(path):+ d = open(path, "rb").read()+ if d[:4] not in (b"IWAD", b"PWAD"):+ fail(f"{path} is not a WAD")+ n, off = struct.unpack("<II", d[4:12])+ lumps = []+ for i in range(n):+ o, s, name = struct.unpack("<II8s", d[off + 16 * i:off + 16 * i + 16])+ lumps.append([name.split(b"\0")[0].decode("latin1").upper(), d[o:o + s]])+ return lumps+++def write_wad(path, lumps):+ data = bytearray(b"IWAD" + struct.pack("<II", len(lumps), 0))+ dir_ = bytearray()+ for name, d in lumps:+ dir_ += struct.pack("<II8s", len(data), len(d), name.encode("latin1"))+ data += d+ struct.pack_into("<I", data, 8, len(data))+ open(path, "wb").write(bytes(data + dir_))+++def is_map(name):+ return bool(re.fullmatch(r"E\dM\d|MAP\d\d", name))+++def section_of(lumps, i):+ """the section (S, F, P) lump i is in, or None"""+ for k, (a, b) in SECTIONS.items():+ start = None+ for j in range(i, -1, -1):+ n = lumps[j][0]+ if n in (a, a[0] + a):+ start = j+ break+ if n in (b, b[0] + b):+ break+ if start is not None:+ return k+ return None+++def split(lumps):+ """maps (name -> its lumps), sections (S/F/P -> [name, data] in order), the rest in order"""+ maps, sections, rest = {}, {"S": [], "F": [], "P": []}, []+ i = 0+ sec = None+ while i < len(lumps):+ name, d = lumps[i]+ if is_map(name):+ j = i + 1+ while j < len(lumps) and lumps[j][0] in MAP_LUMPS + ("BEHAVIOR",):+ j += 1+ if any(lumps[k][0] == "BEHAVIOR" for k in range(i + 1, j)):+ fail(f"{name} is a Hexen format map, which Doom does not play")+ maps[name] = lumps[i:j]+ i = j+ continue+ for k, (a, b) in SECTIONS.items():+ if name in (a, k + a):+ sec = k+ break+ if name in (b, k + b):+ sec = None+ break+ else:+ if sec:+ sections[sec].append([name, d])+ else:+ rest.append([name, d])+ i += 1+ return maps, sections, rest+++def merge(iwad, pwads):+ maps, sections, rest = split(iwad)+ for pw in pwads:+ pmaps, psections, prest = split(pw)+ maps.update(pmaps)+ for k, items in psections.items():+ for name, d in items:+ for item in sections[k]:+ if item[0] == name:+ item[1] = d+ break+ else:+ sections[k].append([name, d])+ for name, d in prest:+ for item in reversed(rest):+ if item[0] == name:+ item[1] = d+ break+ else:+ rest.append([name, d])+ return maps, sections, rest+++def map_order(name):+ m = re.fullmatch(r"E(\d)M(\d)", name)+ return (int(m.group(1)), int(m.group(2))) if m else (0, int(name[3:]))+++def choose(maps, spec):+ names = sorted(maps, key=map_order)+ if not spec or spec.strip().lower() == "all":+ return names+ out = []+ for item in spec.replace(",", " ").upper().split():+ if "-" in item:+ a, b = item.split("-")+ if a not in maps or b not in maps:+ fail(f"no map {a if a not in maps else b} in the WAD (it has {' '.join(names)})")+ out += [n for n in names if map_order(a) <= map_order(n) <= map_order(b)]+ elif re.fullmatch(r"E\d", item):+ got = [n for n in names if n.startswith(item + "M")]+ if not got:+ fail(f"no episode {item[1]} in the WAD (it has {' '.join(names)})")+ out += got+ elif item in maps:+ out.append(item)+ else:+ fail(f"no map {item} in the WAD (it has {' '.join(names)})")+ return sorted(set(out), key=map_order)+++def named(pattern, path):+ return set(re.findall(pattern, open(path).read()))+++def info_tables():+ """vanilla's states (sprite, next state, action) and things (doomednum, their states), from src/doom/info.c"""+ text = open(os.path.join(SRC, "doom", "info.c")).read()+ states = {}+ for m in re.finditer(r"STATE\(SPR_(\w+),[^,]+,[^,]+,\s*(\w+),\s*(S_\w+),[^)]*\),?\s*//\s*(S_\w+)", text):+ states[m.group(4)] = (m.group(1), m.group(3), m.group(2))+ things = {}+ body = text[text.index("mobjinfo[NUMMOBJTYPES]"):]+ for m in re.finditer(r"\{\s*//\s*(MT_\w+)(.*?)\n\s*\}", body, re.S):+ fields = dict((k, v) for v, k in re.findall(r"\s*([^\n]+?),?\s*//\s*(\w+)", m.group(2)))+ things[m.group(1)] = fields+ return states, things+++def needed_sprites(kept_maps, maps):+ states, things = info_tables()+ by_num = {}+ for mt, f in things.items():+ try:+ by_num[int(f["doomednum"])] = mt+ except (KeyError, ValueError):+ pass+ todo = []+ for name in kept_maps:+ lumps = dict(maps[name][1:])+ t = lumps.get("THINGS", b"")+ for i in range(0, len(t) - 9, 10):+ num = struct.unpack("<h", t[i + 6:i + 8])[0]+ if num in by_num:+ todo.append(by_num[num])+ todo += ["MT_PLAYER", "MT_BARREL", "MT_PUFF", "MT_BLOOD", "MT_TFOG", "MT_IFOG"]+ seen_mt, seen_st, sprites = set(), set(), set(ALWAYS_SPRITES)+ while todo:+ mt = todo.pop()+ if mt in seen_mt or mt not in things:+ continue+ seen_mt.add(mt)+ if mt in DROPS:+ todo.append(DROPS[mt])+ if mt in ("MT_BOSSBRAIN", "MT_BOSSSPIT", "MT_SPAWNSHOT"):+ todo += BRAIN_SPAWNS+ for field in ("spawnstate", "seestate", "painstate", "meleestate", "missilestate", "deathstate",+ "xdeathstate", "raisestate"):+ st = things[mt].get(field, "S_NULL")+ while st in states and st not in seen_st and st != "S_NULL":+ seen_st.add(st)+ spr, nxt, action = states[st]+ sprites.add(spr)+ todo += SPAWNS.get(action, [])+ st = nxt+ return sprites+++def unpack_sides(lumps):+ """the map with each line's sides its own, the front one then the back one, in the lines' order (as the+ calculator's packed maps keep them: editors share sides that look the same, which vanilla Doom allows)"""+ d = dict(lumps[1:])+ lines, sides = bytearray(d["LINEDEFS"]), d["SIDEDEFS"]+ out = bytearray()+ for i in range(0, len(lines) - 13, 14):+ for k in (10, 12):+ sn = struct.unpack("<H", lines[i + k:i + k + 2])[0]+ if sn != 0xFFFF:+ struct.pack_into("<H", lines, i + k, len(out) // 30)+ out += sides[30 * sn:30 * sn + 30]+ return [[n, bytes(lines) if n == "LINEDEFS" else bytes(out) if n == "SIDEDEFS" else v] for n, v in lumps]+++def renumber_bsp(lumps):+ """the map's BSP tree numbered as id's node builder numbers it (the calculator's packed nodes give their+ children as near numbers): each node after its children, a node child just before its parent (the left one+ when both are nodes, after the right one's subtree), and the subsectors in the same order, with their segs"""+ d = dict(lumps[1:])+ nodes, ss, segs = d["NODES"], d["SSECTORS"], d["SEGS"]+ if not nodes:+ return lumps+ new_nodes, new_ss, new_segs = [], [], bytearray()++ def leaf(c):+ num, first = struct.unpack("<HH", ss[4 * c:4 * c + 4])+ new_ss.append(struct.pack("<HH", num, len(new_segs) // 12))+ new_segs.extend(segs[12 * first:12 * (first + num)])+ return 0x8000 | (len(new_ss) - 1)++ def visit(i):+ n = nodes[28 * i:28 * i + 28]+ right, left = struct.unpack("<HH", n[24:28])+ if not right & 0x8000 and not left & 0x8000:+ r = visit(right)+ l_ = visit(left)+ elif not left & 0x8000:+ l_ = visit(left)+ r = leaf(right & 0x7fff)+ elif not right & 0x8000:+ r = visit(right)+ l_ = leaf(left & 0x7fff)+ else:+ r = leaf(right & 0x7fff)+ l_ = leaf(left & 0x7fff)+ new_nodes.append(n[:24] + struct.pack("<HH", r, l_))+ return len(new_nodes) - 1++ sys.setrecursionlimit(10000)+ visit(len(nodes) // 28 - 1)+ new = {"NODES": b"".join(new_nodes), "SSECTORS": b"".join(new_ss), "SEGS": bytes(new_segs)}+ return [[n, new.get(n, v)] for n, v in lumps]+++def main():+ ap = argparse.ArgumentParser()+ ap.add_argument("wads", nargs="+")+ ap.add_argument("--maps", default="")+ ap.add_argument("--out", required=True)+ a = ap.parse_args()+ iwad = read_wad(a.wads[0])+ pwads = [read_wad(p) for p in a.wads[1:]]+ maps, sections, rest = merge(iwad, pwads)+ if not maps:+ fail("the WAD has no maps")+ every = set(maps)+ mode = ("commercial" if any(n.startswith("MAP") for n in every) else "retail" if "E4M1" in every+ else "registered" if "E3M1" in every else "shareware")+ kept = choose(maps, a.maps)++ # wall textures and flats: the maps' own, with the rest of a switch's pair and of an animation's frames, and+ # the skies of their episodes+ lumps = dict(rest)+ whddata = open(os.path.join(SRC, "whddata.h")).read()+ tex_order = re.findall(r"TEXTURE_NAME\((\w+)\)", whddata[whddata.index("NAMED_TEXTURE_LIST"):])+ flat_order = re.findall(r"FLAT_NAME\((\w+)\)", whddata[whddata.index("NAMED_FLAT_LIST"):])+ groups = [[a_, b_] for a_, b_ in re.findall(r"TEXTURE_NAME\((\w+)\),\s*TEXTURE_NAME\((\w+)\)",+ open(os.path.join(SRC, "doom", "p_switch.c")).read())]+ spec = open(os.path.join(SRC, "doom", "p_spec.c")).read()+ for kind, order in (("TEXANIM_DEF", tex_order), ("FLATANIM_DEF", flat_order)):+ for end, start in re.findall(kind + r"\((\w+),\s*(\w+),", spec):+ if start in order and end in order:+ groups.append(order[order.index(start):order.index(end) + 1])+ used_tex, used_flats = set(), set(SCREEN_FLATS)+ for name in kept:+ ml = dict(maps[name][1:])+ sd = ml.get("SIDEDEFS", b"")+ for i in range(0, len(sd) - 29, 30):+ for k in (4, 12, 20):+ used_tex.add(sd[i + k:i + k + 8].split(b"\0")[0].decode("latin1").upper())+ se = ml.get("SECTORS", b"")+ for i in range(0, len(se) - 25, 26):+ for k in (4, 12):+ used_flats.add(se[i + k:i + k + 8].split(b"\0")[0].decode("latin1").upper())+ e, m = map_order(name)+ used_tex.add(f"SKY{e}" if e else "SKY1" if m < 12 else "SKY2" if m < 21 else "SKY3")+ for g in groups:+ for used in (used_tex, used_flats):+ if used & set(g):+ used |= set(g)+ pnames = lumps.get("PNAMES")+ if pnames is None or "TEXTURE1" not in lumps:+ fail("the WAD has no PNAMES or TEXTURE1")+ npn = struct.unpack("<i", pnames[:4])[0]+ pname = [pnames[4 + 8 * i:12 + 8 * i].split(b"\0")[0].decode("latin1").upper() for i in range(npn)]+ used_patches = set()+ kept_textures = 0+ for tl in ("TEXTURE1", "TEXTURE2"):+ if tl not in lumps:+ continue+ d = lumps[tl]+ n = struct.unpack("<i", d[:4])[0]+ offs = struct.unpack(f"<{n}i", d[4:4 + 4 * n])+ out = []+ for o in offs:+ name = d[o:o + 8].split(b"\0")[0].decode("latin1").upper()+ count = struct.unpack("<h", d[o + 20:o + 22])[0]+ entry = d[o:o + 22 + 10 * count]+ if name in used_tex:+ out.append(entry)+ for p in range(count):+ used_patches.add(pname[struct.unpack("<h", entry[22 + 10 * p + 4:22 + 10 * p + 6])[0]])+ kept_textures += len(out)+ body = bytearray(struct.pack("<i", len(out)))+ o = 4 + 4 * len(out)+ for e in out:+ body += struct.pack("<i", o)+ o += len(e)+ for e in out:+ body += e+ for item in rest:+ if item[0] == tl:+ item[1] = bytes(body)+ if kept_textures > 255:+ fail(f"{kept_textures} wall textures: the calculator's build has room for 255 (choose fewer maps)")+ sprites = needed_sprites(kept, maps)+ # (the markers inside, P1_START..., stay as they are)+ sections["S"] = [[n, d if n[:4] in sprites or n.endswith(("_START", "_END")) else EMPTY_PATCH]+ for n, d in sections["S"]]+ sections["P"] = [[n, d if n in used_patches or n.endswith(("_START", "_END")) else EMPTY_PATCH]+ for n, d in sections["P"]]+ flats = sections["F"]+ sections["F"] = [[n, d] for n, d in flats if n in used_flats or n.endswith(("_START", "_END"))]+ if len(sections["F"]) > 255:+ fail(f"{len(sections['F'])} flats: the calculator's build has room for 255")++ # (a calculator has no sound: the music and sound effects are left out, and so are the demos of maps left+ # out, or recorded by other versions of Doom than 1.9)+ def keep_lump(name, d):+ if re.fullmatch(r"D_\w+|DS\w+|DP\w+|GENMIDI|DMXGUS\w*", name):+ return False+ if re.fullmatch(r"DEMO\d", name):+ if len(d) < 13 or d[0] != 109:+ return False+ demo_map = f"MAP{d[3]:02d}" if mode == "commercial" else f"E{d[2]}M{d[3]}"+ return demo_map in kept+ return True+ rest = [[n, d] for n, d in rest if keep_lump(n, d)]+ out = list(rest)+ for name in kept:+ out += renumber_bsp(unpack_sides(maps[name]))+ out.append(["NWMODE", mode.encode()])+ for k, (a_, b_) in SECTIONS.items():+ out.append([a_, b""])+ out += sections[k]+ out.append([b_, b""])+ os.makedirs(a.out, exist_ok=True)+ path = os.path.join(a.out, os.path.basename(a.wads[0]).lower())+ write_wad(path, out)+ print(f"pick.py: {path}: {mode}, maps {' '.join(kept)}; {kept_textures} wall textures, "+ f"{len(used_patches)} wall patches, {len(sections['F'])} flats, "+ f"{len({n[:4] for n, d in sections['S'] if d != EMPTY_PATCH})} sprites")+++if __name__ == "__main__":+ main()diff --git a/games/doom/tools/whd_gen/whd_gen.cpp b/games/doom/tools/whd_gen/whd_gen.cppindex 3efb6ed0..bf72e05c 100644--- a/games/doom/tools/whd_gen/whd_gen.cpp+++ b/games/doom/tools/whd_gen/whd_gen.cpp@@ -41,6 +41,8 @@ typedef struct __packed {bool super_tiny = true;+// -drop-unused: what nothing converts (music, other ports' lumps...) is left empty (nw/pick.py's WADs)+bool drop_unused = false;// -silent: no sound effects or music (a calculator has no speaker), their lumps kept emptybool silent = false;// -lossless: what RP2040 Doom approximates is kept as in the WAD: the menu, status bar and font graphics@@ -2391,6 +2393,9 @@ void dump_patch(const char *name, int num, lump &patch) {}}+// the packed sides of the level being converted (convert_linedefs reads them)+static std::vector<uint8_t> sides_z_of_level;+std::vector<int> convert_sidedefs(wad &wad, const texture_index &tex_index, lump &lump) {assert(lump.data.size() % sizeof(mapsidedef_t) == 0);int count = lump.data.size() / sizeof(mapsidedef_t);@@ -2492,11 +2497,10 @@ std::vector<int> convert_sidedefs(wad &wad, const texture_index &tex_index, lump} else {toff_size = 2;}- bool onebyte_sector = msd.sector < 256;- bool onebyte = onebyte_sector | onebyte_texture;- sides_z.push_back(enc | (!(onebyte) << 7) | (have_rowoff << 6) | (toff_size << 4));- sides_z.push_back(msd.sector);- assert(onebyte);+ // (the sector's top bit, for up to 511, goes in the first byte's top bit)+ if (msd.sector >= 512) fail("%d sectors: the calculator's build has room for 511", msd.sector + 1);+ sides_z.push_back(enc | ((msd.sector >> 8) << 7) | (have_rowoff << 6) | (toff_size << 4));+ sides_z.push_back(msd.sector & 0xff);if (hasA[enc]) {sides_z.push_back(toptexture);}@@ -2533,7 +2537,7 @@ std::vector<int> convert_sidedefs(wad &wad, const texture_index &tex_index, lumpint texoff = 0;if (sides_z[s0] & 48) {if (sides_z[s0] & 32) {- texoff = (sides_z[s0 + offset_offset[enc2]] << 8) + sides_z[s0 + 1 + offset_offset[enc2]];+ texoff = (int16_t) ((sides_z[s0 + offset_offset[enc2]] << 8) + sides_z[s0 + 1 + offset_offset[enc2]]); // (negative offsets: the engine reads an int16_t too)} else {texoff = sides_z[s0 + offset_offset[enc2]] << 1;}@@ -2555,6 +2559,7 @@ std::vector<int> convert_sidedefs(wad &wad, const texture_index &tex_index, lumpside_meta.record(sizeof(whdsidedef_t));side_metaz.record(sides_z.size() - s0);}+ sides_z_of_level = sides_z;lump.data = sides_z;} else {std::vector<uint8_t> whd_sides;@@ -2599,17 +2604,27 @@ std::vector<int> convert_sidedefs(wad &wad, const texture_index &tex_index, lump#define line_predict_v1(l) ((l[1] & (ML_NO_PREDICT_V1 >> 8)) == 0)#define line_predict_v2(l) ((l[1] & (ML_NO_PREDICT_V2 >> 8)) == 0)+// a side's bytes (as r_state.h works them out)+static inline int side_bytes(const uint8_t *side) {+ static const uint8_t textures[16] = {0, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 2, 2, 2, 2, 3};+ int n = 2 + textures[side[0] & 0xf] + ((side[0] >> 4) & 3);+ if (side[0] & 64) n += side[n] & 128 ? 2 : 1;+ return n;+}+static inline int line_sidenum(const uint8_t *lines, uint16_t whd_sidemul, const uint8_t *l, int side) {if (side && line_onesided(l)) return -1;int s;if (line_predict_side(l)) {- s = ((l - lines) * whd_sidemul) >> 16;+ s = ((uint32_t) (l - lines) * whd_sidemul) >> 14; // (a quarter: big maps have more side bytes than line bytes)s += (int8_t)l[2];} else {s = l[2] + (l[3] << 8);}if (side) {- s += l[1] >> 5;+ int d = l[1] >> 5;+ if (d == 7) d = side_bytes(sides_z_of_level.data() + s);+ s += d;}return s;}@@ -2689,7 +2704,7 @@ std::vector<int> convert_linedefs(wad &wad, lump &lump, const std::vector<int>&printf("Guess %d\n", size_guess);uint vmul = 65536 * max_v / size_guess;assert(vmul < 65536);- uint smul = 65536 * max_side_num / size_guess;+ uint smul = 16384 * max_side_num / size_guess;assert(smul < 65536);offset = 0;std::vector<uint8_t> result;@@ -2722,11 +2737,12 @@ std::vector<int> convert_linedefs(wad &wad, lump &lump, const std::vector<int>&int sidenum = sidedef_mapping[msd.sidenum[0]];if (msd.sidenum[1] != -1) {uint delta = sidedef_mapping[msd.sidenum[1]] - sidenum;- assert(delta > 0 && delta <= 7);- flags |= delta << 13;+ // (7: as far as the front side's own size, which the engine works out, side_bytes)+ assert(delta > 0 && (delta < 7 || delta == side_bytes(sides_z_of_level.data() + sidenum)));+ flags |= std::min(delta, 7u) << 13;}- int s_est = (int)((base * smul) >> 16u);+ int s_est = (int)((base * smul) >> 14u);bool s_in_range = abs(sidenum - s_est) <= 127;side_num_range.record(s_in_range);side_num_range_v.record(sidenum - s_est);@@ -2799,7 +2815,7 @@ std::vector<int> convert_linedefs(wad &wad, lump &lump, const std::vector<int>&offset = 0;const uint8_t *lines = last_encoding.first.data();const auto &offsets = last_encoding.second;- uint smul = 65536 * max_side_num / size_guess;+ uint smul = 16384 * max_side_num / size_guess;uint vmul = 65536 * max_v / size_guess;for (int i = 0; i < count; i++) {auto msd = get_field_inc<maplinedef_t>(lump.data, offset);@@ -4474,6 +4490,7 @@ void convert_flats(wad &wad) {fstart_lump.data = special_to_flat;fstart_lump.data.insert(fstart_lump.data.end(), flat_to_special.begin(), flat_to_special.end());wad.update_lump(fstart_lump);+ touched[fstart_lump.num] = TOUCHED_FLAT; // (the flats' metadata: kept by -drop-unused)}void convert_demo(wad & wad, lump& l) {@@ -4814,6 +4831,7 @@ int main(int argc, const char **argv) {if (!strcmp(argv[argn], "-no-super-tiny")) super_tiny = false;else if (!strcmp(argv[argn], "-silent")) silent = true;else if (!strcmp(argv[argn], "-lossless")) lossless = true;+ else if (!strcmp(argv[argn], "-drop-unused")) drop_unused = true;else usage();argn++;}@@ -4905,6 +4923,11 @@ int main(int argc, const char **argv) {}printf("}; \n");#endif+ lump nwmode; // (nw/pick.py's: which game the full WAD is)+ if (wad.get_lump("NWMODE", nwmode)) {+ touched[nwmode.num] = TOUCHED_ENDOOM;+ name_required.insert("NWMODE");+ }lump endoom;if (wad.get_lump("ENDOOM", endoom)) {touched[endoom.num] = TOUCHED_ENDOOM;@@ -5239,6 +5262,11 @@ int main(int argc, const char **argv) {printf("UNTOUCHED %d %s (%d)\n", e.first, e.second.name.c_str(), (int) e.second.data.size());total += e.second.data.size();touched[e.first] = TOUCHED_UNUSED;+ if (drop_unused) {+ lump l = e.second;+ l.data.clear();+ wad.update_lump(l);+ }}}printf("UNTOUCHED TOTAL %d\n", total);