Something went wrong. Try again.
Zork (Dungeon) built with Zig 0.17, wrapping the C port of the MIT mainframe game in one executable with its game data embedded.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
const std = @import("std");
pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{});
const allow_gdt = b.option(bool, "allow-gdt", "Enable if you want to have access to the game debugging tool. This is invoked by typing \"gdt\". It is not much use except for debugging.") orelse false; const upstream = b.dependency("zork", .{});
// Upstream's declarations of the game's global state and functions, for // src/dinit.zig, which replaces upstream's dinit.c. const translate_c = b.addTranslateC(.{ .root_source_file = b.path("src/dungeon.h"), .target = target, .optimize = optimize, }); translate_c.addIncludePath(upstream.path("."));
const build_options = b.addOptions(); build_options.addOption(bool, "allow_gdt", allow_gdt);
// An object of its own rather than the executable's root module: `main` // is upstream's, in dmain.c, and a Zig root module would bring Zig's. const dinit_mod = b.createModule(.{ .root_source_file = b.path("src/dinit.zig"), .target = target, .optimize = optimize, .link_libc = true, }); dinit_mod.addImport("c", translate_c.createModule()); dinit_mod.addOptions("build_options", build_options); dinit_mod.addAnonymousImport("dtextc.dat", .{ .root_source_file = upstream.path("dtextc.dat") }); const dinit = b.addObject(.{ .name = "dinit", .root_module = dinit_mod });
const mod = b.createModule(.{ .target = target, .optimize = optimize, .link_libc = true, }); mod.addObject(dinit); mod.linkSystemLibrary("termcap", .{});
mod.addCSourceFiles(.{ .root = upstream.path("."), .files = &.{ "actors.c", "ballop.c", "clockr.c", "demons.c", "dgame.c", "dmain.c", "dso1.c", "dso2.c", "dso3.c", "dso4.c", "dso5.c", "dso6.c", "dso7.c", "dsub.c", "dverb1.c", "dverb2.c", "gdt.c", "lightp.c", "local.c", "nobjs.c", "np.c", "np1.c", "np2.c", "np3.c", "nrooms.c", "objcts.c", "rooms.c", "sobjs.c", "supp.c", "sverbs.c", "verbs.c", "villns.c", }, .flags = &.{if (allow_gdt) "-DALLOW_GDT" else ""}, });
const exe = b.addExecutable(.{ .name = "zig-zork", .root_module = mod, });
b.installArtifact(exe);
const manpage = b.addInstallFile(upstream.path("dungeon.6"), "share/man/man6/dungeon.6"); b.getInstallStep().dependOn(&manpage.step);
const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); run_cmd.addPassthruArgs();
const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step);
// Play a few moves and check the game answers them, which needs both the // index and the message text read out of the embedded database. const play = b.addRunArtifact(exe); play.setStdIn(.{ .bytes = "open mailbox\ntake leaflet\nread leaflet\nquit\ny\n" }); play.expectStdOutMatch("There is a small mailbox here."); play.expectStdOutMatch("Opening the mailbox reveals:"); play.expectStdOutMatch("Welcome to Dungeon!");
const test_step = b.step("test", "Play a few moves of the game"); test_step.dependOn(&play.step);}