# Writing scenarios

### Items and investigation (items)

In `items`, write item name → `{ name, image, description }`. `name` is the name shown, `image` is a picture in the image folder (optional), and `description` is a description (optional).

```json
"items": { "old_key": { "name": "Old key", "image": "items/key.png", "description": "It's rusted." } }
```

| How to write it | Meaning |
|---|---|
| `@item-add old_key` | Add one (`count=`; `notify=false` shows no notification) |
| `@item-remove old_key` | Remove (all of them if `count=` is omitted) |
| `item("old_key")` | Read the count in an expression |
| `@item-select temp.used` | Show the items screen, let the player choose one, and put its name in the variable (an empty string if closed). For letting the player choose what to use at a place being investigated |
| `@open-screen items` | Open the items screen |

- The counts are kept in game variables (with names that expressions can't write), so they come back as they were with save, rewind and load.
- "Items" appears in the menu (only when there are `items`). In the quick menu, you can place it with `items` in `ui.menuBar.items`.
- Places to investigate are made with choice click areas (`x` `y` `width` `height`). You can add `cursor=` (a cursor image on hover) and `tip=` (a short description shown on hover).
  To hide places that have been investigated, use `if=`; to make them unpressable, `locked=`.
- The syntax check confirms the shape of `items`, and that the names in `@item-add` `@item-remove` `item("…")` in the scenario are in `items`.

### Places to investigate (investigation)

Investigation scenes, where the player picks places on the background to examine, are written in `data/investigation.json` and shown with `@investigate name`.

```json
{
  "places": {
    "room": {
      "title": "Examining the room",
      "file": "chapter1",
      "done": "#exam_done",
      "views": [
        {
          "id": "yuika", "label": "Yuika's view", "background": "stills/room_yuika.png",
          "spots": [
            { "id": "shelf", "label": "Check the bookshelf", "rect": { "x": 111, "y": 110, "width": 231, "height": 493 }, "target": "#yuika_shelf", "flag": "exam_shelf" },
            { "id": "window", "label": "Check the window", "rect": { "x": 552, "y": 116, "width": 401, "height": 238 }, "target": "#yuika_window", "required": false }
          ]
        }
      ]
    }
  }
}
```

```
@investigate room
# yuika_shelf
Old astronomy books line the bookshelf.
# yuika_window
The window is locked from the inside.
# exam_done
Yuika "Haru, what do you think?"
```

- Pressing a place calls its scene (`target`; `#label` is a label in `file`). The scene returns to the investigation screen at the next label or the end of the file, without writing `@return` or `@jump` (if the scene does a `@jump`, it doesn't return)
- Whether a place has been investigated is recorded by the command (a game variable that expressions can't write; kept in the save), and if `flag` is written, that variable is also set to `true`. Investigated places get a mark
- Once all required places (those without `required` set to `false`) have been investigated, the game goes to `done` (or to the line after `@investigate` if there is none). With `finish` set to `button`, it goes there when "Finish" is pressed (it can't be pressed until the investigation is done)
- With two or more views (`views`), tabs switch between them (`@investigate room view=haru` sets the first view). A place whose `if` (expression) is false isn't shown, and with `once` it isn't shown after it has been investigated. `tip` is a description shown on hover.
  When returning from a scene, the view that was last shown comes back
- On touch devices, the first tap on a place shows its name (`tip` or `label`) and outline, and tapping the same place again or tapping the name investigates it (a mouse or keyboard investigates with one press).
  On touch devices and in portrait games, text and buttons are made larger (text at least 12px on screen, buttons at least 44px), and small places can still be hit with a finger. The codex and map choices do the same
- A view's `face` (a picture in image) is the face shown on the tab that switches views. With `enter` (a scene), that scene is called the first time the player switches to that view with the tab (returning at the next label shows that view's screen),
  and with `enterFlag`, that variable is also set to `true` (the command also records whether the view was entered)
- When a place is investigated a second time, `again` (a scene), if present, is called instead. `temp.first_visit` in the scene is `true` only the first time the place is investigated (you can also branch with `@if temp.first_visit`)
- Ways to finish without investigating enough: with `incomplete` (a scene) of the whole place to investigate (the level above the views) or a view's `end` (a scene, for finishing in that view; used before `incomplete`), "Finish" can be pressed
  even before the investigation is done, and pressing it jumps there (to have the player come back, write `@investigate room` again where it jumps to; the record of what was investigated stays).
  While required places remain, pressing it first asks "There are places you haven't examined yet. Finish anyway?" (the text key is `investigate.unfinished`;
  it doesn't ask if the player has turned confirmations off in Settings, and a game that replaces the confirmation screen gets `kind` `investigate-finish`)
- `opening` is a title shown large, like a title page, the first time the player enters (it disappears as soon as it's pressed; it isn't shown when returning from a scene, when coming back from a save made partway, or with `@investigate room opening=false`)
- `hitScale` (0.1 to 1) shrinks a place's hit area around the center of its rectangle (so the edges of the picture can't be pressed by accident)
- "Show spots" next to the title lights up every place that can be investigated (press it again to turn it off; the wording is `investigate.reveal`). With `progress` set to `true`, the number of required places investigated (`1 / 3`) appears next to the title
- With `hud` set to `true`, the HUD text (`@hud-text`) stays visible on top while the investigation screen is shown (HUD buttons aren't shown, and it's hidden while the menu or another screen is open). The same goes for map choices
- A view's background stays as the background after the investigation screen is closed, and if it's an image in extra.cg, it's unlocked as a CG (it also counts in the check for CGs that never appear)
- When running without a screen (the all-routes test, route exploration), uninvestigated required places are investigated in order. Studio's flowchart draws arrows to the places' scenes and to `done`
- `@investigate-reset room` erases the record of what was investigated and of the views entered (so the player investigates from the start again)
- While the investigation screen is shown, the player can still open the menu, save, load, the backlog and the settings (Esc, right-click, the shortcut keys and the menu button on phones).
  Closing them returns to the investigation screen, and a save made while it's shown comes back to the same view when loaded. The same goes for map choices, the codex, `@item-select`, `@text-input` and `@dialog`
- The syntax check confirms the shape, jump targets, that backgrounds exist, expressions, flags ([data shapes](../../plugins.md#作品のデータの形data)), and that the names and views in the scenario are in the data. In Studio, you can edit it in "Investigation" under "Data" in "Plugins," dragging rectangles on top of the background

### Choosing a destination on a map (map-choice)

Scenes where the player chooses where to go by pressing a point on a map are written in `maps` in the same `data/investigation.json` and shown with `@map-choice name`. Pressing a point shows its name, picture and description in the description panel,
and "Choose this" chooses it (with `confirm` set to `false`, the point pressed is chosen right away).

```json
{
  "maps": {
    "town": {
      "title": "Destination", "prompt": "Where to?", "file": "chapter3", "background": "maps/town.png",
      "spots": [
        { "id": "station", "label": "Station", "rect": { "x": 220, "y": 300, "width": 140, "height": 60 }, "target": "#station", "desc": "The busy square in front of the station.", "image": "maps/station.png" },
        { "id": "park", "label": "Park", "rect": { "x": 640, "y": 280, "width": 140, "height": 60 }, "target": "#park", "locked": "!game.has_key" }
      ]
    }
  }
}
```

```
@map-choice town result=temp.where
```

- The `id` of the chosen point goes into the `result` variable, and the game jumps to the point's `target` (`#label` is a label in `file`). A point without `target` goes on to the line after `@map-choice` (branch with the `result` variable)
- A point whose `if` (expression) is false isn't shown, and one whose `locked` (expression) is true is shown but can't be chosen. `panel` (`right`, `left`, `bottom`) sets where the description panel goes. The panel sits on top of the map, so a point under it can't be pressed (the syntax check reports it as `map-spot-hidden`). In portrait games the panel goes at the bottom across the full width regardless of `panel` (at most 40% high), and the syntax check looks at that area
- When a locked point is selected, the description panel shows `lockedDesc` (such as "Needs the gate key"; `lockedDescEn` in English) under the description
- The map picture (`background`) is looked up in `bgimage`, then in `image` (the same rule as the codex map, so the same path shows the same picture). A point's `image` comes from `image`
- While the map is shown, the menu, save, load, the backlog and the settings can still be opened (closing them returns to the map). A save made while the map is shown comes back to the same map when loaded. When running without a screen, the first point that can be chosen is chosen (the all-routes test tries every point).
  Studio's flowchart draws arrows to the points' `target`s. In Studio, you can edit it in "Choose on a map" in "Investigation" under "Data," dragging rectangles on top of the map

### Codex (codex)

Records such as descriptions of people, incidents, terms and items are written in `data/codex.json`. "Records" (changeable with `title`) appears in the menu (and the extras), and `@codex` or `@open-screen codex` also opens it.

```json
{
  "title": "Notebook",
  "categories": [{ "id": "people", "label": "People" }, { "id": "terms", "label": "Terms", "sort": "reading" }],
  "entries": [
    {
      "id": "yuika", "category": "people", "title": "Yuika", "reading": "yuika", "image": "codex/faces/yuika.png",
      "if": "game.met_yuika",
      "sections": [
        { "text": "Visits the observatory at the top of the hill." },
        { "id": "past", "title": "Three years ago", "text": "She started watching the stars one summer three years ago.", "if": "game.yuika_past" },
        { "id": "secret", "text": "…", "locked": true }
      ],
      "related": ["haru"]
    }
  ]
}
```

| How to write it | Meaning |
|---|---|
| `if` of an entry (expression) | Unlocks when it becomes true. `@codex-unlock yuika` also unlocks it |
| `item` of an entry | Unlocked while the player has the item (a name from items) (for item descriptions) |
| `if` of a section (`sections`) | Unlocks an entry's text section by section, a little at a time (gradual reveal). Sections without a condition unlock with the entry, and `locked` sections are unlocked with `@codex-unlock yuika section=secret` |
| `sort` of a category | `reading` sorts by reading (for a glossary); otherwise, the order written |
| `type` of a category | How it's shown. `list` (a list; the default), `timeline` (a timeline, with each entry's `date` as a heading, in the order of `order`), `relations` (a relationship chart, placing entries at their `x` `y` and drawing the lines in `links`), `map` (a map, placing marks at `x` `y` on the category's `map` picture. Like map choices, the picture is looked up in `bgimage`, then in `image`, where it used to go) |
| `x` `y` of an entry | The position in a relationship chart or map (a percentage from the top left of the chart, 0 to 100; if not written in a relationship chart, entries are arranged in a circle). In Studio's data form, you can place it by clicking on the category's map picture |
| `links` of an entry | Lines in a relationship chart (`to` is the entry at the other end, `label` the relationship, `tone` `positive` (on good terms), `negative` (in conflict; dashed) or `neutral`, and `if` the condition to show it). Shown when the entries at both ends are unlocked. Lines both ways between the same two (A to B and B to A) are bent into arcs instead of overlapping, so both relationships show |
| `notify` | `false` shows no notification when it unlocks |

- Unlocked entries and sections are kept in this playthrough's record (kept in the save) and in the record shared by all saves (`system.codex_unlocked`). The codex opened from the extras shows the record shared by all saves
- The conditions are checked when a variable they read changes (`@set`, a command's result, choices made and the number of scenes entered, items, and a plugin's `setVariable` and `variablesChanged`),
  and newly unlocked things are announced at the edge of the screen (not for things unlocked from the start, or while skipping). Checks happen only when a variable a condition reads (`game.x` and so on) changes.
  The announcements are the same stacking notices as `@notify` (`.lm-notice`): things unlocked one after another are listed one by one, and two or more unlocked at once are summed up by count ("Recorded: 3 entries").
  If a condition reads something that changes from page to page, such as `read()` or `lang()`, or reads local variables, it's also checked every time a page is shown. A "New" mark stays until it's read
- When the codex is opened without naming an entry (from the menu, or `@codex`) and when the category changes, the category's entry with a "New" mark, or else the first one in the list, is shown on the right.
  Being shown this way doesn't count as read, and the "New" mark stays (an entry counts as read when the player picks it, or when it's opened with `@codex name`)
- The syntax check confirms the shape, assets, expressions, the entries in `related` and `links`, and that the names and sections in `@codex` and `@codex-unlock` are in the data, and warns about flags that a condition reads but no scenario sets
- A project can replace the codex screen (`screens.codex` of `WebPlugin`; [writing plugins](../../plugins.md)). The standard commands decide which entries, sections and lines are unlocked and which have the New mark, and pass them on

### Extras (extra)

When you write lists in `extra` in `liminalia.json`, "Extras" appears on the title screen of the default system screens (once at least one thing has been unlocked).
From a scenario, open it with `@open-screen extra`.

```json
"extra": {
  "cg": [{ "id": "ending_a", "title": "Dawn", "images": ["stills/ending_a.png", "stills/ending_a2.png"] }],
  "replay": [{ "id": "ending_a", "title": "The hill at dawn", "target": "ending#a", "thumb": "stills/ending_a.png", "variables": { "game.route": "a" } }],
  "music": [{ "file": "quiet_night.ogg", "title": "A quiet night" }],
  "achievements": [{ "id": "first_star", "title": "First shooting star", "description": "Found a shooting star", "hidden": false }]
}
```

| Kind | How it's unlocked | Contents |
|---|---|---|
| `cg` | `@cg-unlock`, or showing it with `@bg` (plugin commands too, when shown with a parameter set to unlock CGs) | `images` are paths from `assets/bgimage/`. Only unlocked images can be viewed, and a click moves to the next variation. `thumb` is the image in the list (the first unlocked image if omitted). With two or more `group`s (headings) or `chapter`s (an id in `chapters`; the chapter's number or title becomes the heading), the list is split into tabs. `description` is shown with the title on the screen for viewing an image large (the controls are under extras in docs/player.md) |
| `replay` | `@replay-unlock id` | Plays from `target`, and returns to the replay screen in the extras at `@replay-end` or the end. Saving and loading aren't available during a replay. `@replay-end` does nothing outside a replay, so it can be placed in the flow of the main story. `variables` (`{ "game.route": "a" }`; `game.` and `temp.`) are variables set before the replay starts, to pass values decided in the main story to the replay. The expression `replaying()` tells you whether you're in a replay |
| `music` | Played once with `@bgm` | |
| `achievements` | `@achieve id`. For an achievement with a `goal`, `@achieve-progress id` (`add=` adds, default 1; `set=` sets the count) advances the count, and it unlocks when it reaches `goal`. An achievement with `endings` unlocks when the number of endings of that kind seen reaches `goal` | Only the first unlock is announced. `hidden` hides the name and description until it's unlocked. `icon` is a path from `assets/image/`. Writing `goal` (an integer of 1 or more) makes it a counting achievement, and the list shows "current count / goal". With a kind of ending in `endings` (`"*"` for any kind), each time a command that ends the playthrough runs, the count becomes the number of endings of that kind seen (counted the same way as `endings()` in expressions; the same ending counts once). No `@achieve-progress` is needed; write `goal` too. The desktop version also sends it to Steam achievements, and the mobile version to Game Center and Google Play Games achievements (docs/distribution.md) |
| `movies` | Played once with `@movie` | `{ file, title, thumb, subtitles }`. `file` and `subtitles` are from `assets/video/`, `thumb` from `assets/image/`. Pressing one plays it full screen with a control bar |
| `characters` | Showing the sprite | A list of `{ who (character ID), name }`. Only the variants that have been shown can be viewed large, by sprite set (layered sprites are the stacked picture; `@chara smile+blush` counts as seeing `smile`) |
| `endings` | Reaching an ending scene (a scene with `@title-screen ending=`) | `true` shows a tab with the list of endings. It lists the endings of the flowchart built from the scenario, and shows the name (the kind's wording and the name in `title=`) and scene title only for those reached (they're counted even without writing `extra.flowchart`). Each row has the kind in `data-kind` |

- The unlock records are in system variables (shared by all saves): `cg_unlocked` `replay_unlocked` `bgm_heard` `achievements` `movies_seen` `sprites_seen`. The counts of counting achievements are in `achievement_progress` (in an expression, `system.achievement_progress.read_all`).
  Count the endings seen with `endings()` (all of them) and `endings("bad")` (that kind) in expressions (the same ending counts once however often it's seen; the current playthrough's ending counts once the command that ends the play runs).
- Under the heading of the extras screen, the completion rate is shown (the overall rate, and the counts of read text, CGs, replays, endings and achievements). The overall rate is the average of the shares of the items that have a total (read text, endings, CG images, replays, music, movies, achievements).
  Read text and endings are counted with the flowchart built from the scenario (they're counted even without writing `extra.flowchart`).
  Plugins and project screens read it with `system.completion()` (`SystemController`; `{ read, endings, cg, replay, music, movies, achievements, rate }`, where each item is `{ done, total }`).
- Unlocking when a CG is shown is done by the runtime (the same while skipping, during a replay, and in runs without a screen, such as working out the current position in Studio or the all-routes test).
  What's unlocked are images in `images` of `cg` or in the `cgAuto` folders, shown with a parameter set to unlock CGs (images in `@bg` and `@bg-place`; in plugins, `t.asset("bgimage", "Title page", { cg: true })`).
- The syntax check confirms the shape of the lists and where replays start, and warns if an unlock command in a scenario names something not in the lists (for `@achieve-progress`, it also warns about achievements without a `goal`).
  Images in `cg` that no scenario shows and no `@cg-unlock` unlocks are warned about (because they can't be seen in the extras; if the scenario has a command that names a CG image with an expression, it's info instead of a warning).
  A `@cg-unlock` in the same run of command lines (with no text in between) as a command that shows the same image as a CG isn't needed, so it's reported as info (these can be deleted all at once from the check in Studio).
  `{@cg-unlock …}` inside text is also checked (if the image is shown by `{@bg …}` on the same line or by the commands right before the line, it's info; since the line can't be deleted as a whole, it isn't included in Delete all).
  Unlock commands inside text (`{@cg-unlock}` `{@achieve}` and so on) are also warned about if they name something not in the lists.

### Chapters (chapters)

In `chapters` in `liminalia.json`, write each chapter's number, title, subtitle (such as a title in another language) and its scenario files in one place.

```json
"chapters": [
  { "id": "prologue", "number": "Prologue", "title": "Beginnings", "files": ["prologue_*"] },
  { "id": "ch1", "number": "Chapter 1", "title": "The hill where we wait for stars", "subtitle": "THE HILL OF STARS", "files": ["chapter1_*"], "cover": "covers/ch1.png" }
]
```

| Item | Contents |
|---|---|
| `id` | Name (required). The value returned by the expression `chapter()` |
| `title` | Title (required) |
| `number` | How the number is written (`Chapter 1`, `EPISODE 01` and so on) |
| `subtitle` | A subtitle or a title in another language |
| `files` | The chapter's scenario files (without the extension; `*` matches any run of characters). A file that matches two chapters belongs to the one written first |
| Others | Items your project adds (a picture to show on the chapter's title page and so on). Read by plugin commands |

Where it's read:

- The expression `chapter()`: the `id` of the chapter of the current scenario file (empty if it isn't in any chapter).
- The standard chapter card and chapter end (`@chapter-card`, `@chapter-end`): an omitted number, title or subtitle comes from the current chapter. The look is set with `ui.chapter` in `liminalia.json` (backdrop, picture, font, text color, text size and the rule; [player.md](../../player.md)),
  and otherwise follows the theme's colors and font (`titleBg`, `fg`, `accent` in `ui.system` and the text font). The project's CSS can restyle `.lim-chapter` (`--card`, `--end`).
  When shown, the number and title are passed to screen readers and text-to-speech. The end mark of `@chapter-end` is 完 if omitted (End when playing in English).
  With `leave=mask` (the default for `@chapter-end`), the script continues with the screen covered in black, and the next `@mask-off` opens it (`@staff-roll` takes the same parameter).
  The covered state is kept in saves and stays covered after load, rewind and skipping.
- Plugin commands (a project's own chapter title page or chapter end): `ctx.runtime.chapter` is the current chapter (`{ id, title, number, subtitle, … }`; `null` if none).
  If the command fills in omitted parameters from the chapter information, you don't have to write the same values for every chapter.
- Save slot headings: the default save screen shows the number and title of the chapter of the file where the save was made (`ui.chapterNames`, if written, takes priority). Project screens can look up the same chapter with `chapterOf(chapters, position.unit)` from `@liminalia/lang` (the project information in the player is `project.chapters`).
- CGs in the extras: writing `"chapter": "ch1"` in `extra.cg` lines them up under that chapter's heading (`number`, or `title` if there is none) without writing `group`.
- Chapter select (`extra.chapters`): with the same `id` as in `chapters`, `title` can be omitted (the chapter's title is used).

The syntax check reports malformed entries, duplicate `id`s and chapters without a title (`bad-chapters`), `files` that match no scenario file and files in two chapters (warning `chapter-files`),
and a `chapter` in `extra.cg` that isn't in `chapters` (`bad-extra`).
A `@chapter-card` or `@chapter-end` with no number, title or subtitle in a script file that isn't in any chapter gets a warning (`chapter-card-empty`; a blank card appears.
One inside a macro is checked against the chapter of the script file that defines the macro). In a project that uses chapters, a script file reached from the start that has text but isn't in any chapter
is reported as info (`not-in-chapter`; while it plays, the chapter heading in save slots and `chapter()` are empty. Reaching is followed from the start positions such as `entry` through `@jump`, `@call`, options,
target-type parameters and targets inside called macros. For a script file you keep out of the chapters, write `// lim-ignore-file not-in-chapter` at its top).
