Skip to content

Mission scripts

A level's mission is a Lua file in the level mod, levels/<level id>/mission.lua. The port's Lua mod runtime runs it while the level is played, and a standard library, obm.mission.define, turns a table of targets, exits, objectives and events into a working mission. The base mission’s scene scripts are compiled into the port from src/scripts/.

See Level missions for the Lua runtime and API reference for individual functions.

A complete mission

The Contract (tools/port/scene/specs/contract.json, mission file tools/port/scene/levels/contract/mission.lua):

obm.mission.define {
  targets = { "Don", "DonSon", "Broker" },
  exit = {
    objects = { "Exit" },                   -- the cloned PlaneExit with its Escape action
    message = "M01_ExitPlane",              -- what that action sends to the level control
    cutsequence = "Controllers!EscapeCut",  -- M01's escape; its end calls MissionCompleted
  },
  on = {
    target_killed = { text = "Target down: {killed} of {total}" },
    all_targets_killed = {
      sound = { file = "sounds/openblood_alarm.wav", at = "AlarmSpeaker", range = 3000 },
      warning = "Alarm! Get to the exit in the hall.",
    },
    enter_room = {
      { room = "Office", text = "Victor Kane's office." },
    },
  },
}

The Gallery Contract runs as follows.

  1. When the level's scene loads, the three targets are found by name and registered with the level control (target numbers 1 to 3), and the exit's Escape action is hidden.
  2. Each kill ticks off its objective (the OSD tick-off mark) and shows "Target down: n of 3".
  3. The last kill shows the exit, plays the alarm at the AlarmSpeaker marker in the hall and shows the warning.
  4. Using Escape completes objective 4 and starts the escape sequence; its end reaches the base mission's C++ level control, which calls MissionCompleted: MISSION ACCOMPLISHED, the statistics, the newspaper, save and continue.
  5. If 47 dies, the engine fails the mission ("Agent 47 died.").

How a level declares its mission

In mod.json, the level's entry names the file:

"levels": [
  { "id": "contract", "title": "Contract", "base_scene": "M01/Contract_main",
    "briefing": "...", "mission": "levels/contract/mission.lua" }
]

The runtime loads the file when the active custom level has a mission and the scene loading is its base_scene: with the scene's first script, before scene_loaded. It runs in an environment of its own (globals fall back to the mod's), and everything it registers is removed when the scene goes. A stock mission, or another level, never sees it: a stock M01 played with the level active and its mod enabled runs exactly as without it (below). For a test run without the launcher, OPENBLOOD_CUSTOM_LEVEL=<level id> makes the level the active one.

obm.mission.define also works from an ordinary entry script; then give it level (a level id) or scene (a scene's base name) so it acts only there.

What the scene needs

The builder (tools/port/scene/mission_script.py) prepares the scene; a hand-built scene needs the same:

Why
The level control's ScriptC runs alllevels\levelcontrol. The shared base level control is in every disc module and has no mission logic (it relays light-switch messages to the actors). The base mission's own script (M01's registers Don and his son, hides and shows M01's exits, completes M01's objectives) would fight the Lua. A script must stay on the object: the exits' action messages are delivered to it, and that is where the Lua sees them.
ScriptCModule stays the base mission's (M01). The port runs the base level’s built-in native scripts; the actors' scripts (alllevels\guard, civilian) come from it. No module is shipped.
The targets are actors whose script gives them characterType 3 (target). ZHM3LevelControl::CharacterKilled counts a target by its character type: the debriefing's kills and the rating.
Each exit is an object with a ZAction (M01's PlaneExit) whose message goes to the level control. The library hides and shows it (ActionHide, ActionShow) and listens for its message.
An escape sequence (M01's EscapeCut, a ZVideoPlayer) whose end message goes to the level control, or none. Its CutSequence_End reaches the C++ level control (ZHM3LevelControlM01::Command), which calls MissionCompleted. Without one, the library calls obm.mission.complete() itself.
Objective and briefing entries in the scene LOC under the scene code. Targets come first, then extra tasks, the exit and optional entries. Set exit.objective when extra tasks move the exit past its default index. Lua can update an existing objective’s text.
Event sounds as WAV files in the mod, and named marker objects where they play. obm.audio plays mod files; a marker gives a position the Lua names. Sounds that loop from an emitter in a room stay in the scene's .SND/.WAV as before.

obm.mission.define(def)

Every field is optional except targets.

Field Meaning
targets 1 to 8 entries: an actor's name, or { name, actor, objective, on_killed }. name is the name obm.world.actors() reports (an actor called Ground takes its group's name, so the actor of ROOT\Level\Don\Ground is Don); actor a different name or a scene path; objective the objective it completes (default: its position in the list). Several targets may share an objective, which completes when the last of them dies. The first 6 are registered with the level control (Silevelcontrol__Settargetnum), which keeps the weapon each was killed with for the debriefing.
exit { objects, message, cutsequence, cutsequence_message, objective, show, at, radius }, or false for a mission that completes when the last target dies. objects: the exits (names or paths), hidden at the start and shown when the last target dies (show = "always" shows them at once, "never" leaves them alone). message: the message the exits' actions send (default M01_ExitPlane; a list is allowed). cutsequence: the escape sequence to start (with cutsequence_message, default CutSequence_StartScene2); without it, using the exit completes the mission at once. objective: the exit's objective (default: the highest target objective + 1). at and radius (default 150): an object; once the exits are shown, 47 coming within radius of it counts as using the exit, for an exit without an action.
optional Optional objectives, checked when 47 uses the exit: a list of { type = "no_bodies_found" }, { type = "no_witnesses" }, { type = "suit_only" } or { check = function(mission) ... end }, each with objective (default: the exit objective + its position).
fail { protect = { names }, time_limit = seconds, reason = locale key }: the mission fails when a protected actor dies or the time runs out. 47's death fails it through the engine as always.
on Events, below.
level, scene Only act while this custom level, or this scene (base name), is played. A level's own mission file does not need them.

Events

Event When Context fields
start the scene has loaded and the mission has started
target_killed a target dies (after its objective is ticked off) name, index, killed, total, remaining
a target's on_killed that target dies the same
all_targets_killed the last target dies (after the exits are shown) the same
actor_killed an actor that is neither a target nor protected dies name, ref
protected_killed a protected actor dies (then the mission fails) name
enter_room a list of { room, once = true, ... }: 47 enters that room (checked four times a second; once = false repeats) room
time a list of { at = seconds, ... }: that long after the start time
body_found the level control counts another body found bodies
exit 47 uses the exit, before the escape starts source
complete, failed the level control ends the mission reason

Actions

An event's value is an action table, a list of them, or a function function(mission, ctx).

Key Action
text, warning an OSD info or warning line; {name}-style fields are filled from the context
subtitle, seconds a subtitle for seconds (default 4)
sound a WAV path (relative to the mission file's folder, then to the mod), or { file, at = object, pos = {x, y, z}, range = 2500, volume = 1, loop = false } (3D when at or pos is given, default volume 1.0 in 3D and 0.85 in 2D), or { scene = Sound index of the scene's .SND, at = object } (the engine's own sound). The default volume is 1 for a positional sound and 0.85 for a 2D sound.
objective complete that objective
send { to = object, msg = "MessageName" } or a list of them
run a function function(mission, ctx)
fail true or a locale key: fail the mission
complete complete the mission

The mission object

define returns the mission. Its fields: targets (each { name, ref, objective, dead }), exits, exit_objective, time, escaped, ended. Its methods: mission:complete_objective(n), mission:complete(), mission:fail([reason]), mission:show_exits(), mission:remaining().

Helpers

obm.mission.actor(name), obm.mission.object(name), obm.mission.play(sound), obm.mission.say(text, kind, seconds) and obm.mission.dir() (the mission file's folder) are the library's own helpers, for missions that do more than define covers. Everything the library does goes through the public API, so a mission can always do it by hand: obm.on, obm.scripts.on_message, obm.scripts.send, obm.mission.complete_objective, obm.mission.complete, obm.mission.fail, obm.world.ref, obm.audio.play_3d and obm.api.<Interface> (any script interface with integer, float and string arguments, such as obm.api.Silevelcontrol__Settargetnum(ref, 1)).

Every step logs a [mod:<id>] mission: ... line, which is the first place to look.

Limits the engine sets

  • 8 objectives in all: ZHM3BriefingControl keeps the completed and hidden objectives as bits of a byte. Targets, the exit and the optional objectives share them.
  • 6 target slots in ZHM3LevelControl (m_arTargets): targets 7 and 8 work and count as kills, but the debriefing does not record their weapons.
  • The level control is the base mission's C++ class. M01's (ZHM3LevelControlM01) starts M01's music for objectives 1 and 2 and calls MissionCompleted at the end of the escape.
  • Text the menus show comes from the .LOC; Lua can change an objective's text at run time (obm.mission.set_objective_text) but cannot add objectives.

Building a level with a Lua mission

The scene builder takes the mission file in the spec's mission:

"mission": {
  "level_control": "ROOT\\M01/M01_CHARACTERS.zip\\Controllers!Levelcontroller",
  "lua": "tools/port/scene/levels/contract/mission.lua",
  "targets": [ { "actor": "@Don", "text": { "who": "...", "objective": "..." } }, ... ],
  "exit_objective": { ... }, "optional": [ { "text": { ... } } ],
  "text": { ... }, "sounds": [ ... ]
}

mission.py hands such a spec to mission_script.py, which:

  1. sets the level control's ScriptC to alllevels\levelcontrol and leaves ScriptCModule alone;
  2. copies the file to levels/<level id>/mission.lua and adds "mission" to the level's mod.json entry;
  3. puts sounds with emitters into the scene's .SND/.WHD/.WAV (they loop from their emitter, as before) and writes every other sound to levels/<level id>/sounds/<name>.wav; an at position adds a marker object named source_name (default <name>_Source) in the room there;
  4. writes the objectives, target pages, briefing, location and hints into the .LOC.

python -I tools/port/scene/build_scene.py tools/port/scene/specs/contract.json --out <mods>/contract takes about 3 seconds and needs no WSL, Docker or PS2 compiler. Two builds give the same bytes. The mod holds no script module.

Set mission.script in level.json to mission.lua. The builder supplies mission.lua to the scene spec and writes the resulting mission path in the built mod’s manifest.

The compiled script (retired)

Until 24 November 2026 a level could instead run a level script written with the script SDK and compiled with ee-gcc into a copy of M01's module (mission.script in a spec). The port now runs the scene scripts as native code built from src/scripts and loads no script module from a mod, so that route is gone: mission.py refuses a spec with mission.script, and the C file was removed. Level logic is Lua.

What the C++ builder (phase C) must reproduce

The Python builder stays the reference. For a level with a Lua mission, the C++ builder must:

  • set the level control's ScriptC to alllevels\levelcontrol with no imports, and leave ScriptCModule as the base's;
  • write the objectives as T (targets), extra tasks (R, O, P), E (exit), O (optional) in that order, and the rest of the text, as mission.apply_text does;
  • put emitter sounds into the .SND/.WHD/.WAV (snd.add_sound) and add a named marker ZSTDOBJ in the room for each positioned event sound;
  • write no script module. The mission file and the event WAVs stay in the mod; the port reads them from there, so the cache needs only the scene files.