// The quest scripts read once (quest audit of 2026-10-30, docs/quest-audit.md): // what the failure restart (questfail.cpp) and the giver respawn // (questgiver.cpp) need to know about a quest. // // The compiled script code (FunkCode.bin, open as the quest manager's file) is // a list of labels (the label table, game/engine/quests.h) whose code is a // sequence of instructions {u16 opcode, u16 size, tagged args}; the argument // sizes per tag are those of the game's reader (0x472eb0, as tools/ // questscript.mjs decodes them). From it, per quest: // - the giver: the NPC given a dialog whose answer starts the quest (a spawn // instruction carrying it, e.g. Region15Init "Res:17013" dialog auftrag1501; // else an NPC state instruction, as the Underworld's QIS_OnSetUp do); // - its variables: those only its labels set, and the one named after it; // - the NPCs it needs: those its labels set states on but don't create, as // 1302's opponent Res:17173 (Region13Init); they come back from their spawn // instruction elsewhere when the quest restarts; // - what QIS_OnEnter creates (deleted on a restart, accepting again // creates it anew); // - the failures that end its story: a LoseQuest label that also sets up the // next quest (2402), calls another quest's label (2502 finalestart), hands // out a reward (9520), or turns hostile or kills an NPC offering it (5003, // 1701, 1703). #include "mods/questscript.h" #include #include #include #include #include #include #include #include #include "game/engine/events.h" #include "game/engine/objects.h" #include "game/engine/orders.h" #include "game/engine/quests.h" #include "re/log.h" namespace mods::questscript { namespace { constexpr size_t kInstructionHeader = 4; constexpr size_t kReadChunk = 1 << 20; constexpr DWORD kBodyWaitMs = 30000; // spawn anyway if the body is still there constexpr int32_t kPositionNamed = -2; // position args: -2 + a name, else 3 ints constexpr int32_t kNumberNamed = -0x12d687; // 0x0b args: this + a name (DefNum) constexpr uint8_t kArgHostile = 0x12; // flag after kScriptArgAttitude // Argument payloads by tag (0x472eb0). enum class Payload : uint8_t { kUnknown, kString, kU16, kI32, kPosition, kCondition, kFlag, kBytes }; struct ArgTable { std::array payload{}; std::array bytes{}; ArgTable() { for (int tag : {0x01, 0x05, 0x09, 0x16, 0x1e, 0x29, 0x3a, 0x41, 0x60, 0x63, 0x67, 0x68, 0x69, 0x6a, 0x71, 0x83, 0x9d}) { payload[tag] = Payload::kString; } for (int tag : {0x03, 0x0a, 0x6b, 0x6c, 0x9b, 0x9c}) { payload[tag] = Payload::kU16; } for (int tag : {0x02, 0x0b, 0x11, 0x19, 0x1c, 0x1d, 0x1f, 0x36, 0x38, 0x53, 0x54, 0x55, 0x56, 0x57, 0x5f, 0x73, 0x75, 0x84, 0x86}) { payload[tag] = Payload::kI32; } for (int tag : {0x04, 0x0c, 0x0d}) { payload[tag] = Payload::kPosition; } for (int tag : {0x47, 0x48, 0x49, 0x4a}) { payload[tag] = Payload::kCondition; } for (int tag : {0x06, 0x07, 0x08, 0x0e, 0x0f, 0x10, 0x12, 0x13, 0x14, 0x1a, 0x1b, 0x23, 0x24, 0x25, 0x26, 0x27, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x39, 0x3f, 0x42, 0x43, 0x44, 0x45, 0x46, 0x4c, 0x4e, 0x4f, 0x50, 0x51, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x61, 0x62, 0x64, 0x65, 0x66, 0x70, 0x72, 0x74, 0x78, 0x7b, 0x7c, 0x80, 0x85, 0x8a, 0x8d, 0x8e, 0x91, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9e, 0xa0, 0xa1}) { payload[tag] = Payload::kFlag; } for (auto [tag, size] : std::initializer_list>{ {0x15, 8}, {0x20, 1}, {0x28, 1}, {0x2a, 1}, {0x33, 8}, {0x34, 8}, {0x35, 12}, {0x37, 1}, {0x4b, 1}, {0x4d, 1}, {0x79, 8}, {0x87, 8}, {0x88, 8}, {0x89, 8}, {0x8b, 1}}) { payload[tag] = Payload::kBytes; bytes[tag] = static_cast(size); } } }; const ArgTable kArgs; struct Arg { uint8_t tag = 0; const char* text = nullptr; // string payloads, in g_code int32_t number = 0; }; struct Label { std::string name; int32_t quest = -1; size_t code = 0; size_t size = 0; }; struct Spawn { int32_t quest; const Label* label; std::vector code; }; struct Pending { uint32_t scriptId; std::vector spawn; std::string dialog; void* body; uint32_t bodyId; DWORD since; std::string why; }; bool g_scanned = false; std::vector g_code; std::vector