TableTower/core/table_entry.gd
RobinFiredrake 85b8a99ef5 Logic cards engine: expressions, effects, choice/loop/variable/sheet cards, coroutine roller
- Expr: @vars, chain vars, dice tokens, dm/has/count, lone = as ==, errors as values, parse cache
- Effects: = += -= *= /= and reset (with prefix), number vs list semantics, logs, validation
- RollTable kinds and per-kind fields with factories; expression dice with end clamping
- TableEntry effects and expression pipe counts; JSON round trip through JsonIO
- TextExpander: {@prefix_*}, {= expr}, number display, expand_async and live render()
- Roller: coroutine with an explicit work stack, prompts for choice cards, loops,
  10,000-action limit, partial tree on ctx.root, iterative flatten/collect_lit
- Variables keep numbers as numbers through StateStore
- Main: awaits the roll with a default (headless) prompt hook, iterative playback,
  long chains show at once

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

173 lines
5.1 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class_name TableEntry
extends RefCounted
## One possible result on a table. An entry may pipe into other tables, and on a
## ranged (dice) table it covers a span of die values such as 01–30.
const MAX_PIPE_COUNT := 99
## Most times an expression pipe count (such as "@terms") may roll.
const MAX_EXPR_COUNT := 10000
var id: String = ""
var text: String = ""
## Optional longer description (may contain BBCode).
var details: String = ""
## Relative chance on a plain table; also the number of copies in card mode.
## 0 means it never comes up. Ignored on ranged tables.
var weight: int = 1
## Effect statements run when this entry is chosen (see Effects), before its
## text expands and its pipes follow. "" for none.
var effects: String = ""
## IDs of the tables this entry rolls on next (unique; one pipe per target).
var pipes: Array[String] = []
## target table id -> how many times to roll it (missing = 1): an int, or a
## String expression such as "@terms" or "1d3" evaluated when the pipe runs.
var pipe_count: Dictionary = {}
## target table id -> variable name to save the result under (missing = none).
var pipe_save: Dictionary = {}
var has_range: bool = false
var range_lo: int = 1
var range_hi: int = 1
func _init(p_id: String = "", p_text: String = "") -> void:
id = p_id
text = p_text
## How many die faces this entry covers. Plain entries cover exactly one.
func span() -> int:
return range_hi - range_lo + 1 if has_range else 1
## How many times the pipe to `target` rolls. An expression count reads as 1
## here; see count_expr() and count_value().
func count_for(target: String) -> int:
var c: Variant = pipe_count.get(target, 1)
if c is String:
return 1
return clampi(int(c), 1, MAX_PIPE_COUNT)
## The pipe's count expression, or "" when the count is a plain number.
func count_expr(target: String) -> String:
var c: Variant = pipe_count.get(target, 1)
return c if c is String else ""
## The count as typed: "3", or the expression ("@terms").
func count_text(target: String) -> String:
var e := count_expr(target)
return e if e != "" else str(count_for(target))
## How many times the pipe rolls in this roll: a plain count, or the
## expression evaluated now (0 and up; errors give 0). `log` (optional) gets an
## error line when the expression fails.
func count_value(target: String, ctx: RollContext, log: Array = []) -> int:
var e := count_expr(target)
if e == "":
return count_for(target)
var r := Expr.evaluate(e, ctx)
if r["error"] != "":
log.append({"text": "Pipe count %s: %s" % [e, r["error"]], "error": true})
return 0
return clampi(Expr.as_int(r, 0), 0, MAX_EXPR_COUNT)
## Variable name the pipe to `target` saves into, or "".
func save_for(target: String) -> String:
return str(pipe_save.get(target, ""))
## Sets the count: an int (clamped 1..99), or a String that is either a whole
## number or an expression.
func set_pipe_count(target: String, n: Variant) -> void:
if n is String:
var s := (n as String).strip_edges()
if s == "" or s.is_valid_int():
n = s.to_int() if s != "" else 1
else:
pipe_count[target] = s
return
if n is float:
n = int(n)
var k := clampi(int(n), 1, MAX_PIPE_COUNT)
if k == 1:
pipe_count.erase(target)
else:
pipe_count[target] = k
func set_pipe_save(target: String, name: String) -> void:
name = name.strip_edges()
if name == "":
pipe_save.erase(target)
else:
pipe_save[target] = name
## Adds a pipe if it isn't there already. `count` is an int or an expression.
func add_pipe(target: String, count: Variant = 1, save_as: String = "") -> void:
if not pipes.has(target):
pipes.append(target)
set_pipe_count(target, count)
set_pipe_save(target, save_as)
## Removes a pipe along with its options.
func remove_pipe(target: String) -> void:
pipes.erase(target)
pipe_count.erase(target)
pipe_save.erase(target)
func to_dict() -> Dictionary:
var d := {"id": id, "text": text}
if details != "":
d["details"] = details
if weight != 1:
d["weight"] = weight
if effects != "":
d["effects"] = effects
if not pipes.is_empty():
var list: Array = []
for p in pipes:
var expr := count_expr(p)
var n := count_for(p)
var s := save_for(p)
if expr == "" and n == 1 and s == "":
list.append(p)
else:
var po := {"table": p}
if expr != "":
po["count"] = expr
elif n != 1:
po["count"] = n
if s != "":
po["save_as"] = s
list.append(po)
d["pipes"] = list
if has_range:
d["range"] = [range_lo, range_hi]
return d
static func from_dict(d: Dictionary) -> TableEntry:
var e := TableEntry.new(str(d.get("id", "")), str(d.get("text", "")))
e.details = str(d.get("details", ""))
e.weight = maxi(0, int(d.get("weight", 1)))
e.effects = str(d.get("effects", ""))
for p: Variant in d.get("pipes", []):
if p is Dictionary:
var target := str(p.get("table", ""))
if target != "":
var c: Variant = p.get("count", 1)
e.add_pipe(target, c if c is String else int(c), str(p.get("save_as", "")))
elif str(p) != "":
e.add_pipe(str(p))
var r: Variant = d.get("range")
if r is Array and r.size() == 2:
e.has_range = true
e.range_lo = int(r[0])
e.range_hi = int(r[1])
return e