TableTower/docs/LOGIC.md

167 lines
9 KiB
Markdown
Raw Normal View History

# TableTower — Logic Cards
**Status:** Draft spec · **Applies to:** branch `logic-cards` · **Extends:** `docs/DESIGN.md`
Plain roll tables answer "what happens?". Logic cards let a board also ask the player,
remember state, repeat steps and print a summary, so a whole procedure such as rolling a
Traveller character can run as one chain.
## 1. Card kinds
Every table has a `kind`. Existing tables are `roll`.
| Kind | What it does when the chain reaches it | Card shows |
|---|---|---|
| `roll` | Rolls an entry (weighted, or ranged with a dice expression). Unchanged, except ranged tables now accept full expressions (§4) | Entries, die button |
| `choice` | Pauses the roll and asks the player (§2) | The question and its options |
| `loop` | Runs its **Each time** entry repeatedly, then its **Afterwards** entry once (§3) | The loop rule and two rows |
| `variable` | Never rolled. Declares a variable with a type, a starting value and a description (§5) | The variable's live value |
| `sheet` | Prints its template with current values (§6) | The template, rendered live |
Every kind except `variable` can be the start of a roll and can be the target of a pipe.
Every entry, of any kind, can carry **effects** (§5.2) and pipes. Pipes keep their count and
"save as" options, and a pipe count may now be an expression (`@terms`, `1d3`).
## 2. Choice cards
A choice card pauses the chain and shows a pop-up. Its `prompt` is BBCode with `{@var}`
expansion, so a question can say "Your EDU is {@edu}. Which career?".
| `choice` | The player… | Result text | Stored as |
|---|---|---|---|
| `pick` (which option) | picks one of the card's entries, or **Random** (always offered) | the picked entry, exactly as if it had been rolled: its effects run, its text expands, its pipes follow | `save_as` (optional) gets the entry's plain text |
| `number` (how many) | types a whole number between `min` and `max` (expressions; defaults 0 and 99) | the number | `save_as`, and the chain variable `answer` |
| `text` (open ended) | types any text | the text | `save_as`, and the chain variable `answer` |
`number` and `text` cards have exactly one entry, named **Answer**. Its effects and pipes run
after the player answers, so `@terms += @answer` style bookkeeping and "go on to the next
step" pipes live there. Random on a `pick` card rolls the card normally, honouring weights
or ranges.
The pop-up also has **Cancel**, which stops the whole roll with a visible "Cancelled" marker.
Fast roll (Shift-click) still asks questions, because they are the player's decisions.
## 3. Loop cards
A loop card has a `loop` mode and a `loop_expr` expression, plus two fixed entries:
**Each time** (index 0) and **Afterwards** (index 1).
| `loop` | Behaviour |
|---|---|
| `times` | Evaluates `loop_expr` once (e.g. `@answer`, `3`, `1d3`) and runs Each time that many times (0 is allowed) |
| `while` | Before each pass, evaluates the condition; stops when it's false |
| `until` | Runs Each time, then evaluates the condition; stops when it's true (runs at least once) |
The chain variable `i` holds the pass number (1, 2, …) during Each time. A loop's node text
reads "Repeated N times". Pipes may also form cycles (a table piping back to an earlier one).
Both are bounded only by the action limit.
## 4. Expressions
Expressions appear in loop rules, pipe counts, `min`/`max`, dice fields and effects. They are
evaluated with Godot's `Expression`, after a small rewrite.
- **Numbers and text:** `12`, `-3`, `"Aslan"`
- **Operators:** `+ - * / %`, comparisons `= == != < <= > >=` (a lone `=` in a condition means
equals), `and or not` (also `&& || !`), parentheses, and `in` for "text contains".
- **Variables:**
- `@name` reads a board variable (§5). If it's unset, it reads as `0` in arithmetic and as
`""` next to text.
- A bare `name` reads a chain variable (`answer`, `i`, or anything saved with `-> name`).
- **Dice:** `2d6`, `d20`, `3D`, `D3`, `2D`. A missing count means 1, and missing sides mean 6, so
`2D` is 2d6. Every die in an expression is rolled fresh.
- **Functions:** `min max clamp abs floor ceil round`, plus:
- `dm(x)`: the 2D-game characteristic modifier. It gives -3 for 0, -2 for 1–2, -1 for 3–5,
0 for 6–8, +1 for 9–11, +2 for 12–14, and +3 for 15 or more.
- `has(list, item)`: whether a comma-separated list contains an item.
- `count(prefix)`: the number of set variables whose names start with `prefix`.
- `if(condition, then, else)`: picks a value. Both branches are evaluated, so dice in
either are rolled. The inline `a if c else b` form is refused, because Godot's
Expression returns the wrong branch for it.
Ranged `roll` tables keep plain dice (`2d6`) and also accept any expression, such as
`2d6 + dm(@end)` or `@terms`. Values beyond the lowest or highest range clamp to that range,
so a check table only needs "2–6 fail" and "7–12 pass". The card labels the ends `≤6` and `7+`
when the expression has modifiers.
## 5. Variables
### 5.1 Board variables (`@name`)
- They belong to the collection and are kept between rolls in local state, never exported.
Values are numbers or text.
- Names are lowercase letters, digits and `_`. A variable exists once it's assigned; no
declaration is needed. A **variable card** (`kind: variable`) is optional and gives a
variable a label, a type (`number`, `text` or `list`), a starting value (an expression) and
details.
- `reset @name` sets a declared variable back to its starting value, or deletes an undeclared
one. `reset @skill_*` resets every variable with that prefix.
- Chain variables (no `@`, e.g. `answer`, `i`, `-> hero`) last for one top-level roll, as
before.
### 5.2 Effects
An entry's `effects` field holds statements, one per line or separated by `;`. They run when
the entry is chosen (rolled, picked or answered), before its text expands and its pipes
follow.
```
@str = 2d6 set
@terms += 1 add (number) · append ", item" if not present (text/list)
@credits -= 1000 subtract (number) · remove the item (text/list)
@age *= 2 multiply (/= divides, rounding down)
@skill_pilot += 1 unset counts as 0, so this gives Pilot 1 or raises it
@skill_vacc_suit = max(@skill_vacc_suit, 0) gain at level 0
@traits += "Psionic"
reset @skill_*
```
Each statement that runs is listed under its step in the result tray (e.g. "STR = 9"). A bad
statement shows a red marker in the tray and in Build mode rather than stopping the roll.
### 5.3 Text
- `{@name}` shows a value. If it's unset, it shows the `{@name?}` marker.
- `{@skill_*}` lists every set variable with that prefix, sorted, as `Gun Combat 1, Pilot 0`.
The name after the prefix is title-cased, with `_` read as a space.
- `{= expr}` shows a computed value, e.g. `STR {@str} ({= dm(@str)})`.
- The existing `{@var|Table}`, `{hero}` and `[[refs]]` work as before.
## 6. Sheet cards
A sheet card holds one template (its single entry's text, multi-line BBCode). On the board it
always shows the template rendered with current values, so a character sheet stays up to
date while the chain runs. When the chain reaches it, the rendered template becomes that
step's result.
## 7. Limits
- **Actions:** one top-level roll may take at most **10,000 actions**. One action is a table
being rolled or answered, or one loop pass. Past that, the roll stops with "Stopped after
10,000 actions", and everything up to that point is kept.
- **Inline references:** `[[refs]]` inside text still nest at most 32 deep.
- **Implementation:** pipes and loops must not use GDScript recursion proportional to chain
length. Long chains (thousands of steps) must not overflow the call stack. That applies to
the roller and to everything that walks a result (flatten, lighting, history).
## 8. Rolling, prompts and display
- The roller is a coroutine. It `await`s `ctx.prompt.call(request)` whenever a choice card is
reached. With no prompt hook (tests, headless), `pick` chooses Random, `number` answers
`min`, and `text` answers `""`.
- **Request:** `{table_id, kind, prompt, options: [{index, text, details}], min, max, default}`.
- **Reply:** `pick` returns an entry index or `-1` for Random. `number` returns an int.
`text` returns a String. `null` means Cancel.
- In the UI, when a prompt is needed, the steps so far appear instantly (lit on the board and
in the tray) before the pop-up opens, so the player can see what led to the question.
After the last answer the rest of the roll is shown at once. A roll with no questions
animates as before, unless it's long (more than 40 steps), when it shows at once.
- The tray shows the last 12 steps of a long chain behind a "+N earlier steps" note. If the
chain reached a sheet card, the result headline is that sheet.
## 9. File format additions
Table keys:
- `kind` (`roll` is the default and is omitted)
- `choice`, `prompt`, `save_as`, `min`, `max` (choice cards)
- `loop`, `loop_expr` (loop cards)
- `var_name`, `var_type`, `initial` (variable cards)
Entry keys:
- `effects` (String)
Pipe objects may have `"count": "<expression>"`.