Underworld, modes and raids
This guide covers content that goes beyond a single map button: pages on the Underworld map, modes that chain fights into a run, scheduled events, local raids with a stacked boss shield, and fights your script generates when the player enters. Start with Battles and bosses if you have not built a fight yet. Examples assume local sf2 = require("sf2").
The Underworld map
Section titled “The Underworld map”The Underworld is the game’s second map: a column of boss pages the player opens with the round toggle on the story map. Its pages hold single boss fights and survival gauntlets. A Power Mode checkbox on that map swaps each boss for a harder variant.
Your mod can add pages to it. They appear next to the game’s own raid pages and use ordinary battles, fights, opponents and rewards.
Build a boss with a Power Mode twin
Section titled “Build a boss with a Power Mode twin”local sf2 = require("sf2")
local depths = sf2.map.register { id = "depths", art = "Raid1.1", underworld = true }
local ember = sf2.fighters.register { id = "ember_boss", fighter = "core:warrior-templates/default", first_name = "Ember", voice = "Male", skeleton = "SkeletonHeavy", items = { sf2.items.get("core:items/weapon/WEAPON_KATANA") }, health_bars = 10, visible_health_bars = 10,}
local function boss(id, power_mode, level, bars) return sf2.battles.register { id = id, page = depths, type = "final", x = 360, y = 820, power_mode = power_mode, title = "Ember", fights = { { rounds = 1, round_time = 300, opponents = { { fighter = ember, level = level, health_bars = bars } }, rewards = { [1] = { gems = 5, currencies = { { currency = "ForgeMaterial1", expected = 40 } } } }, } }, }end
local normal = boss("ember", "normal", 40, 10)local power = boss("ember_power", "power", 60, 20)
sf2.story.on("session", function() sf2.map.reveal { battle = normal } sf2.map.reveal { battle = power }end)The steps:
- Register a page with
underworld = true. Only pages your mod registers can carry the flag; the game’s raid pages keep their content. Raid art such as"Raid1.1"fits, or pass your own sprite handle. - Register each boss as a battle on that page, usually
type = "final"(or"survival"for a gauntlet), with fight, opponent and rewards inline.currenciespays forge materials. - For a pair, register two battles at the same position, one with
power_mode = "normal"and one with"power". Entries withoutpower_modeshow in both states. The field works only on Underworld pages. - Give each button your own art with
icons = { base = ..., active = ... }(see Battles and bosses). - Reveal the entries from a
sessionhandler, as on the story map. A saved game does not show new battles on its own.
A fight in darkness is one rule away: add { kind = "light_in_the_darkness", radius = 0.2 } to the fight’s rules (Battle rules).
The raid shield
Section titled “The raid shield”A raid boss has one big health pool shown as a stack of bars. Two fields describe it:
| Field | Range | Default | Meaning |
|---|---|---|---|
health_bars |
0 to 10,000 | 0 (inherit) |
The whole pool, measured in normal health bars. health_bars = 20 takes as much damage as twenty normal fighters. |
visible_health_bars |
0 to 100 | 0 (inherit) |
How many stacked bars the HUD draws. Each holds health_bars / visible_health_bars. |
health_marks |
1 to 16 values | none | Health values, counted in bars from empty, where thin dividers are drawn on the boss bar. A mark shows while the stacked bar that holds it is the one drawn. Not inherited: set it on the battle’s opponent entry. |
Each is inherited separately. In the example the Power Mode boss raises the pool to 20 but keeps the fighter’s 10 visible bars, so each bar holds two normal bars. This is how the original game builds its Power Mode bosses. If nothing in the chain sets visible_health_bars, the whole shield draws as a single bar.
Damage uses the same units as for any fighter, and spills over to the next bar. A fighter snapshot reports health and max_health in shield units, so health / max_health is the fraction left. For a long fight use rounds = 1 and round_time = 300, and size the shield to the gear the player is expected to have.
To split a long boss fight into stages, each with its own rules, mark the splits with health_marks and let a round controller change the fight’s phase; see Split a fight into phases. With visible_health_bars = 1 the whole shield is one bar, so every mark is visible at once.
Toggle button and colours
Section titled “Toggle button and colours”Your story may want the Underworld hidden until the player earns it, or tinted to match your theme. Both are set while the map is open and last until the map is rebuilt, so apply them on every map entry.
local sf2 = require("sf2")
sf2.state.register { version = 1, fields = { depths_found = false } }
sf2.story.on("scene_enter", function(event) if event.scene ~= "map" then return end if not sf2.state.get("depths_found") then sf2.map.show_underworld(false) -- hides the toggle, and the way to the game's own raid pages end sf2.map.underworld_colors { normal = "#FFFFFF", power = "#BA8A82", duration = 0.8 }end)sf2.map.show_underworld(visible)hides or shows the round toggle. Hiding it does not lock any battle.sf2.map.underworld_colorsmultiplies the map art by one colour for normal and one for Power Mode, fading between them overdurationseconds (0 to 5, default 0.8).#FFFFFFkeeps the original colours.sf2.map.focus(battle)with an Underworld boss chooses the boss the Underworld opens on. It does not reveal, unlock or switch maps.sf2.map.open_underworld()switches the map to the Underworld now, for example after an act screen that introduces it. Callfocusfirst to open it on a particular boss.
Boss introductions and after-fight lines use Story events.
Modes: chain fights into a run
Section titled “Modes: chain fights into a run”A mode is an ordered list of fights you already registered. It decides which step is available, what a win or loss does, and optionally what entry costs. One function, sf2.modes.register, makes all of them. Its kind picks which: "mode" (the default), "event", "raid" or "encounter".
The first three take the same table:
| Field | Default | Meaning |
|---|---|---|
id |
Required | Local ID. |
fights |
Required | 1 to 100 distinct fight handles from your own battles. A fight belongs to one step of one sequence. |
repeatable |
false |
Restart at the first step after completing the last. |
reset_on_loss |
false |
Restart at the first step after a loss. |
minimum_level |
1 |
Player level needed to enter. |
starts_at, ends_at |
0 |
UTC Unix timestamps (seconds). Start inclusive, end exclusive; 0 means no limit. |
entry_item, entry_count |
None | A consumable item of your mod charged per fight attempt. entry_count is required with entry_item. |
entry_per |
"fight" |
"fight" charges the entry item for every step; "run" charges it only when a run starts at step 1, so the rest of the run is free. Needs entry_item. |
entry_dialog |
None | { title, portrait?, text?, icon? }: ask before a fight that charges the entry item, showing how many the player has, with LEAVE, FIGHT! and a button that buys the item’s gem pack. Needs entry_item. See sf2.modes.register. |
on_result |
None | Function that picks the next fight; see below. |
on_prepare |
None | Function that generates or chooses the fight at entry; see below. |
hard_mode |
false |
kind = "raid" only: the Power Mode filter. |
Without on_result, a win advances to the next step, a completed repeatable run starts again, and a loss keeps or resets progress according to reset_on_loss. The map shows each owned battle as the sequence’s current fight.
Here first, second, middle and final stand for fight handles you got from earlier sf2.battles.register calls (battle.fights[1] and so on).
sf2.modes.register { id = "training_trial", fights = { first, second, final }, -- fight handles from sf2.battles.register repeatable = true, reset_on_loss = true,}
sf2.modes.register { -- same shape, gated by level (add starts_at/ends_at for a schedule) kind = "event", id = "open_training", fights = { first, final }, minimum_level = 6, repeatable = true,}Entry cost is charged per attempt (or per run with entry_per = "run"). Re-entering an interrupted attempt does not charge again, and a scene launch that fails is refunded. Progress and reservations survive saving and temporary removal of the mod.
Branching with on_result
Section titled “Branching with on_result”on_result(result) runs once after a mode fight is settled and returns where to go next: a fight handle from the roster (a retry, a skip, a detour), "complete" to end the run, or nil for the default. It cannot change the fight’s outcome or rewards.
sf2.modes.register { id = "branching_trial", fights = { first, middle, final }, repeatable = true, on_result = function(result) if not result.won then return first end if result.step == 3 then return "complete" end if result.step == 1 and result.completions % 2 == 0 then return final end return nil end,}result has won, step (1-based position just played), total, completions (finished runs before this one) and fight_id. Because a route can jump, the map hides the linear “fights won” bricks for modes with this callback. The chosen fight and completion count are saved. Keep the callback small and do not open a menu from it. For repeatable randomness use saved random streams.
The one-fight shortcut
Section titled “The one-fight shortcut”For a single fight with its own map entry, kind = "encounter" creates the page (unless you pass one), battle, fight, one-fight mode and a quest that reveals it, all at once:
local duel = sf2.modes.register { kind = "encounter", id = "rooftop_duel", title = "Rooftop Duel", x = 300, y = 120, rounds = 2, opponents = { { fighter = "core:warrior-templates/default", level = 12 } }, rewards = { [1] = { gems = 5 } },}-- duel.page, duel.battle, duel.fight and duel.quest are handles.It takes opponents, player, rules, rewards, rounds and round_time exactly as a fight does, plus the presentation fields of a battle and repeatable (default true).
Offline raids
Section titled “Offline raids”An offline raid is a single-player boss fight with a raid shield. The player wins by bringing the whole shield to zero in one fight. Surviving until time runs out is not a win, every attempt starts with a full shield, and nothing is shared with other players or a server.
- Register a page and a battle with
type = "raid". Its fight lists the boss withhealth_barsandvisible_health_barsas above. - Put the victory reward at
rewards[1](an optional loss reward at[0]). - Register
sf2.modes.register { kind = "raid", id = ..., fights = { battle.fights[1] }, ... }. A raid battle without its raid registration is rejected.
local raid = sf2.battles.register { id = "stormcaller", page = depths, type = "raid", x = 500, y = 600, title = "Stormcaller", fights = { { rounds = 1, round_time = 300, opponents = { { fighter = ember, level = 45, health_bars = 15, visible_health_bars = 15 } }, rewards = { [1] = { gems = 500 } }, } },}sf2.modes.register { kind = "raid", id = "stormcaller", fights = { raid.fights[1] }, repeatable = true, reset_on_loss = true, hard_mode = false,}sf2.story.on("session", function() sf2.map.reveal { battle = raid } end)Raid events (raid_enter, raid_end, raid_fight_start, raid_fight_end, mode_reset, raid_map_enter, raid_floor_changed) let you react; see Story events. Entry can be free or cost an entry_item.
Generate a fight at entry
Section titled “Generate a fight at entry”on_prepare(request, event) runs when the player attempts to enter a mode step that has no saved plan yet, before anything is charged. It returns a plan, a table that tweaks that step’s registered fight for this attempt, or nil to wait while you show your own screen. Use it for random opponents, a chosen character, or difficulty picked on the spot.
local rivals = { rival_a, rival_b, rival_c } -- fighter handles from sf2.fighters.registersf2.state.register { version = 1, fields = { encounter_rng = 4242 } }
sf2.modes.register { id = "expedition", fights = { first, second, third }, repeatable = true, on_prepare = function(_, event) return { opponents = { rivals[sf2.random.integer("encounter_rng", 1, #rivals)] }, level = event.step + 2, rounds = 1, round_time = 60, } end,}All plan fields are optional:
| Field | Meaning |
|---|---|
opponents |
1 to 64 fighter handles, in fight order. Omit to keep the registered opponents. |
player |
A fighter handle to play as in this attempt. Saved equipment is not touched. |
level |
1 to 1000, applied to the opponents only. |
rounds, round_time |
1 to 99 rounds; 1 to 3600 seconds. |
rules |
Rule handles taken from your fights’ rules. {} removes all rules. |
description |
Text up to 1024 characters. |
A plan can only name content that already exists, so register the fighters and rules while loading and refer to them by handle. The registered fight supplies arena, music and rewards.
To let the player choose, open a screen and finish later with sf2.modes.resolve(request, plan). Close it with sf2.modes.cancel if they back out. Cancelling after a resolve does nothing, so closing the screen cannot undo the choice.
sf2.modes.register { id = "picker", fights = { first }, repeatable = true, on_prepare = function(request) sf2.ui.open { id = "pick", mount = "modal", root = { id = "go", kind = "button", width = 320, height = 80, text = "Fight the Hermit" }, on_click = function(view) sf2.modes.resolve(request, { opponents = { rivals[1] }, level = 8 }) sf2.ui.close(view) end, on_close = function() sf2.modes.cancel(request) end, } return nil -- keep the request pending until the player clicks end,}A finished plan is saved with the step, so reloading or retrying reuses it without drawing random numbers again. Leaving the scene, switching profile or disabling scripts cancels a pending choice; the player presses Fight again. Draw random numbers only after the choice is committed if cancelling should not advance the stream. The menus guide covers screens.
Limits
Section titled “Limits”- Modes use fights your own mod registered. Core fights cannot be put in a sequence.
- Keep a published mode’s id, roster and order stable. A saved sequence that no longer matches its definition is rejected, not reinterpreted, so test updates on a separate save.
on_resultandon_prepareare synchronous: they cannot wait on a coroutine or a menu (useresolvefor that), and they have a bounded instruction budget. An error uses the default progression or cancels entry.- Offline only: nothing here is matchmaking, multiplayer or a server leaderboard.
Where next
Section titled “Where next”- Story events: reveal battles, run dialogue, react to wins.
- Saving and options: state, seeds and the choices modes often depend on.
- Battle rules: difficulty plans and round objectives for these fights.