- column_join: a card with columns reads as one phrase ("Ash Canyons"), which threads save
- TableEntry.repeat: a result that rolls its column again N times and combines them
- Saving a result into a declared list value adds an item (RollContext.save_result)
- A pick with only one available option picks it without asking
- Lint, why facts, inspector controls, tests, LOGIC.md
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
410 lines
14 KiB
GDScript
410 lines
14 KiB
GDScript
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 = {}
|
||
## target table id -> condition expression: the pipe runs only when it's true
|
||
## (docs/LOGIC.md, Conditional pipes). Missing = always.
|
||
var pipe_when: Dictionary = {}
|
||
## target table id -> true: the pipe runs only when none of this entry's
|
||
## conditional pipes did. Ignored while the pipe has a condition.
|
||
var pipe_otherwise: Dictionary = {}
|
||
## target table id -> {input name: expression}: values passed to the target
|
||
## card's inputs (docs/PLAIN.md §4.2), worked out in the entry's snapshot.
|
||
## Missing = none.
|
||
var pipe_with: Dictionary = {}
|
||
## target table id -> the thread's own colour "#hex" (missing = the theme's).
|
||
var pipe_color: Dictionary = {}
|
||
## The Book page this result is linked to ("" = none). Its text and details
|
||
## follow the page (TowerBoard.sync_page).
|
||
var page: String = ""
|
||
## target table id -> how repeated rolls combine: "sum", "max", "min" or
|
||
## "choose" (RollCombine). Missing = keep every roll.
|
||
var pipe_combine: Dictionary = {}
|
||
## target table id -> true: the roll waits before following this thread until
|
||
## the player continues (Roller.OP_PAUSE). Missing = no pause.
|
||
var pipe_pause: Dictionary = {}
|
||
## target table id -> dice to roll the target with instead of its own
|
||
## ("3d10+70"; a ranged target only). Missing = the card's own dice.
|
||
var pipe_dice: Dictionary = {}
|
||
## target table id -> Array of the target's result ids to roll again on
|
||
## ("re-roll any result of 96+"). Missing = keep whatever comes up.
|
||
var pipe_reroll: Dictionary = {}
|
||
## Cards with columns: this result's text in columns 1.. (column 0 is `text`).
|
||
var cells: Array[String] = []
|
||
## Cards with columns: a "roll twice and combine" row. When this result comes
|
||
## up in a column, that column is rolled `count` more times and the results are
|
||
## combined (RollColumns). {} = off; else {"count": int >= 1, "reroll":
|
||
## [result ids skipped on those rolls]?}.
|
||
var repeat: Dictionary = {}
|
||
## Choice options: what picking this costs (RollCosts): a value name and an
|
||
## amount (an expression). "" = free.
|
||
var cost_value: String = ""
|
||
var cost_amount: String = ""
|
||
## A picture (BoardImages id, kept in the spread file). "" = none.
|
||
var image: String = ""
|
||
## Router cards: this case's condition. "" is the Otherwise case (always
|
||
## matches). Ignored on other kinds.
|
||
var when: String = ""
|
||
## Pick choices: the option is offered (in the pop-up and to Random) only when
|
||
## this expression is true when the card is reached. "" means always.
|
||
var show_if: String = ""
|
||
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": "Thread 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
|
||
|
||
|
||
## The pipe's condition, or "".
|
||
func when_for(target: String) -> String:
|
||
return str(pipe_when.get(target, ""))
|
||
|
||
|
||
## True when the pipe is an "otherwise" pipe (and has no condition).
|
||
func is_otherwise(target: String) -> bool:
|
||
return bool(pipe_otherwise.get(target, false)) and when_for(target) == ""
|
||
|
||
|
||
## True when the pipe has a condition or is an otherwise pipe.
|
||
func is_conditional(target: String) -> bool:
|
||
return when_for(target) != "" or is_otherwise(target)
|
||
|
||
|
||
## Sets the pipe's condition ("" for none). A condition clears "otherwise".
|
||
func set_pipe_when(target: String, expr: String) -> void:
|
||
expr = expr.strip_edges()
|
||
if expr == "":
|
||
pipe_when.erase(target)
|
||
else:
|
||
pipe_when[target] = expr
|
||
pipe_otherwise.erase(target)
|
||
|
||
|
||
func set_pipe_otherwise(target: String, on: bool) -> void:
|
||
if on and when_for(target) == "":
|
||
pipe_otherwise[target] = true
|
||
else:
|
||
pipe_otherwise.erase(target)
|
||
|
||
|
||
## True when any of this entry's pipes has a condition or is an otherwise pipe.
|
||
func has_conditional_pipes() -> bool:
|
||
for p in pipes:
|
||
if is_conditional(p):
|
||
return true
|
||
return false
|
||
|
||
|
||
## The pipe's input values: {input name: expression}, or {} (a copy).
|
||
func with_for(target: String) -> Dictionary:
|
||
var w: Variant = pipe_with.get(target)
|
||
return (w as Dictionary).duplicate() if w is Dictionary else {}
|
||
|
||
|
||
## Sets the pipe's input values ({} for none). Names are trimmed (a leading
|
||
## "@" dropped); empty names and empty expressions are left out; numbers
|
||
## become expression text.
|
||
func set_pipe_with(target: String, values: Variant) -> void:
|
||
var clean := {}
|
||
if values is Dictionary:
|
||
for k: Variant in values:
|
||
var nm := str(k).strip_edges().trim_prefix("@")
|
||
var ex := RollTable.expr_text(values[k], "").strip_edges()
|
||
if nm != "" and ex != "":
|
||
clean[nm] = ex
|
||
if clean.is_empty():
|
||
pipe_with.erase(target)
|
||
else:
|
||
pipe_with[target] = clean
|
||
|
||
|
||
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)
|
||
|
||
|
||
## How a thread's repeated rolls combine (RollCombine.MODES).
|
||
func combine_for(target: String) -> String:
|
||
return str(pipe_combine.get(target, "all"))
|
||
|
||
|
||
## The dice the thread rolls its card with instead of the card's own, or "".
|
||
func dice_for(target: String) -> String:
|
||
return str(pipe_dice.get(target, "")).strip_edges()
|
||
|
||
|
||
## The target's result ids the thread rolls again on (a copy; [] for none).
|
||
func reroll_for(target: String) -> Array[String]:
|
||
var out: Array[String] = []
|
||
var v: Variant = pipe_reroll.get(target)
|
||
if v is Array:
|
||
for x: Variant in v:
|
||
if str(x) != "" and not out.has(str(x)):
|
||
out.append(str(x))
|
||
return out
|
||
|
||
|
||
func set_pipe_dice(target: String, dice: String) -> void:
|
||
if dice.strip_edges() == "":
|
||
pipe_dice.erase(target)
|
||
else:
|
||
pipe_dice[target] = dice.strip_edges()
|
||
|
||
|
||
func set_pipe_reroll(target: String, ids: Array) -> void:
|
||
var clean: Array[String] = []
|
||
for x: Variant in ids:
|
||
if str(x) != "" and not clean.has(str(x)):
|
||
clean.append(str(x))
|
||
if clean.is_empty():
|
||
pipe_reroll.erase(target)
|
||
else:
|
||
pipe_reroll[target] = clean
|
||
|
||
|
||
## Removes a pipe along with its options.
|
||
func remove_pipe(target: String) -> void:
|
||
pipes.erase(target)
|
||
pipe_count.erase(target)
|
||
pipe_save.erase(target)
|
||
pipe_when.erase(target)
|
||
pipe_otherwise.erase(target)
|
||
pipe_with.erase(target)
|
||
pipe_color.erase(target)
|
||
pipe_combine.erase(target)
|
||
pipe_pause.erase(target)
|
||
pipe_dice.erase(target)
|
||
pipe_reroll.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 when.strip_edges() != "":
|
||
d["when"] = when.strip_edges()
|
||
if show_if.strip_edges() != "":
|
||
d["show_if"] = show_if.strip_edges()
|
||
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)
|
||
var w := when_for(p)
|
||
var other := is_otherwise(p)
|
||
var wv: Variant = pipe_with.get(p)
|
||
var col := str(pipe_color.get(p, ""))
|
||
var comb := combine_for(p)
|
||
var paused := pipe_pause.has(p)
|
||
var dice := dice_for(p)
|
||
var again := reroll_for(p)
|
||
if expr == "" and n == 1 and s == "" and w == "" and not other and wv == null and col == "" and comb == "all" and not paused and dice == "" and again.is_empty():
|
||
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
|
||
if w != "":
|
||
po["when"] = w
|
||
elif other:
|
||
po["otherwise"] = true
|
||
if wv is Dictionary and not (wv as Dictionary).is_empty():
|
||
po["with"] = (wv as Dictionary).duplicate()
|
||
if col != "":
|
||
po["color"] = col
|
||
if comb != "all":
|
||
po["combine"] = comb
|
||
if paused:
|
||
po["pause"] = true
|
||
if dice != "":
|
||
po["dice"] = dice
|
||
if not again.is_empty():
|
||
po["reroll"] = again
|
||
list.append(po)
|
||
d["pipes"] = list
|
||
if has_range:
|
||
d["range"] = [range_lo, range_hi]
|
||
if not cells.is_empty():
|
||
d["cells"] = cells.duplicate()
|
||
if not repeat.is_empty():
|
||
d["repeat"] = repeat.duplicate(true)
|
||
if page != "":
|
||
d["page"] = page
|
||
if image != "":
|
||
d["image"] = image
|
||
if cost_value.strip_edges() != "":
|
||
d["cost"] = {"value": cost_value.strip_edges(), "amount": cost_amount.strip_edges()}
|
||
return d
|
||
|
||
|
||
## A "repeat" value from JSON or the UI: {count >= 1, reroll?: [ids]}, or {}.
|
||
static func clean_repeat(v: Variant) -> Dictionary:
|
||
if not (v is Dictionary):
|
||
return {}
|
||
var n := int(JsonIO._num((v as Dictionary).get("count", 2), 2))
|
||
if n < 1:
|
||
return {}
|
||
var out := {"count": n}
|
||
var ids: Array[String] = []
|
||
if (v as Dictionary).get("reroll") is Array:
|
||
for x: Variant in v["reroll"]:
|
||
if str(x) != "" and not ids.has(str(x)):
|
||
ids.append(str(x))
|
||
if not ids.is_empty():
|
||
out["reroll"] = ids
|
||
return out
|
||
|
||
|
||
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", ""))
|
||
e.page = str(d.get("page", ""))
|
||
e.image = str(d.get("image", ""))
|
||
var cost: Variant = d.get("cost")
|
||
if cost is Dictionary:
|
||
e.cost_value = str(cost.get("value", ""))
|
||
e.cost_amount = str(cost.get("amount", ""))
|
||
e.when = str(d.get("when", "")).strip_edges() if d.get("when") != null else ""
|
||
e.show_if = str(d.get("show_if", "")).strip_edges() if d.get("show_if") != null else ""
|
||
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", "")))
|
||
e.set_pipe_when(target, str(p.get("when", "")) if p.get("when") != null else "")
|
||
e.set_pipe_otherwise(target, bool(p.get("otherwise", false)))
|
||
if p.get("with") is Dictionary:
|
||
e.set_pipe_with(target, p["with"])
|
||
if str(p.get("color", "")) != "":
|
||
e.pipe_color[target] = str(p["color"])
|
||
if RollCombine.MODES.has(str(p.get("combine", "all"))) and str(p.get("combine", "all")) != "all":
|
||
e.pipe_combine[target] = str(p["combine"])
|
||
if bool(p.get("pause", false)):
|
||
e.pipe_pause[target] = true
|
||
e.set_pipe_dice(target, str(p.get("dice", "")) if p.get("dice") != null else "")
|
||
if p.get("reroll") is Array:
|
||
e.set_pipe_reroll(target, p["reroll"])
|
||
elif str(p) != "":
|
||
e.add_pipe(str(p))
|
||
if d.get("cells") is Array:
|
||
for c: Variant in d["cells"]:
|
||
e.cells.append(str(c) if c != null else "")
|
||
e.repeat = clean_repeat(d.get("repeat"))
|
||
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
|