TableTower/docs/LOGIC.md
RobinFiredrake dbbb9750eb Conditional pipes and router cards
Pipes can carry "when" (Only if) and "otherwise". When an entry is
chosen, its effects run, its text expands, then every pipe condition and
count is evaluated once as a snapshot before any pipe runs; when pipes
run if true, otherwise pipes if no when pipe was true, plain pipes
always. Condition errors count as false and show as error lines.

New router card (kind "router"): its entries are cases with an
entry-level "when"; the first true case is chosen (empty = Otherwise),
and "No case matched" stops the node. One action. JSON round trip for
entry/pipe when and pipe otherwise, including JsonIO.canonical and
edge labels. Roller.lint_pipes() and Roller.lint_router() for board
checks. A pick choice's save_as is now stored before the snapshot.

UI: Only if / Otherwise in the pipe popover with live validation, pipe
labels ("if @str >= 8", "otherwise") and a source-end diamond on
conditional pipes, router card with fork icon, per-case condition rows,
case editing / reordering in Build, router settings, "+ Table" entry,
router cases in table previews. Logic demo now routes through a router.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 17:53:00 -06:00

13 KiB
Raw Blame 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
router Chooses the first of its cases whose condition is true (§3b) A fork icon, one row per case with its condition

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). A pipe may also have a condition, or be an "otherwise" pipe (§3a).

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.

3a. Conditional pipes

A pipe may carry "when": "<expression>" (shown as Only if in the pipe's pop-up) and/or "otherwise": true. A pipe with a when ignores otherwise.

When an entry is chosen (rolled, picked, answered, a loop pass, a router case):

  1. Its effects run, its text expands, and any "save as" (the pipe's that led here, or a pick choice's save_as) is stored.
  2. The snapshot. Every when condition and every count of the entry's pipes is worked out once, now, before any pipe runs.
  3. The pipes run in list order:
    • a pipe with when runs only if its condition was true;
    • an otherwise pipe runs only if none of the entry's when pipes was true;
    • any other pipe always runs.

So an earlier pipe's subtree can change variables without changing whether, or how many times, a later pipe on the same entry runs. Pipes are not "else if": two true conditions both run (use a router, §3b, for first-match). Counts are only worked out for pipes that run; a skipped pipe's count dice are never rolled.

A condition that fails to evaluate counts as false and adds a red line to the node's effect log ("Pipe condition …: …"), the way a bad pipe count does. Only the pipes actually taken light up after a roll. On the board, a conditional pipe has a small diamond at its source end (hollow for otherwise) and a label such as if @str >= 8 ×2 → foe or otherwise.

Change from earlier versions. Pipe counts used to be evaluated lazily: the second pipe's count was read only after the first pipe's whole subtree had run, so a branch that changed a variable could make a later pipe fire too ("both branches fired"). Counts now come from the snapshot. Also, a pick choice's save_as is now stored before the snapshot (it used to be stored after the option's pipes were queued, which made no difference while counts were lazy). No existing test changed.

3b. Router cards

A router (kind: "router", "Router" in the + Table ▾ menu, "if / else if / otherwise") has no dice. Its entries are cases, each with an entry-level when expression.

  • When the chain reaches it, the cases are checked top to bottom and the first true case is chosen. A case with an empty when (shown as Otherwise) always matches, so it should be the last one.
  • The chosen case happens exactly like a rolled entry: its effects run, its text expands and its pipes follow (with the snapshot rule of §3a).
  • If no case matches, the node stops with a visible "No case matched". A case condition that errors counts as false and adds a red line ("Case …: …") to the node.
  • It counts as one action. The node and step have kind: "router" and case (the chosen case's index). Routers have no ranges, weights or card mode.
  • A new router has two cases: "if …" (with the condition 1d6 >= 4, to be edited) and "Otherwise". + Add case on the card adds an if false case before Otherwise; cases can be reordered and removed on the card, and their conditions edited there or in the settings dialog.

Roller.lint_pipes(board) lists problems for a board check: when or case conditions that don't parse, an otherwise pipe listed before a when pipe on the same entry (a hint: it still works), an otherwise pipe on an entry with no when pipe (it always runs), and routers with no cases or whose Otherwise case isn't last. Each item is {table_id, entry_id, message}.

4. Expressions

Expressions appear in loop rules, pipe counts, pipe conditions, router cases, 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, a router choosing a case, 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 awaits 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)
  • when (String, router cases; empty or missing is Otherwise)

Pipe objects may have "count": "<expression>", "when": "<expression>" and "otherwise": true (left out when the pipe has a when).