TableTower/ui/main_rolling_reroll.gd

130 lines
4.8 KiB
GDScript3
Raw Normal View History

class_name MainRollingReroll
extends RefCounted
## "Reroll this" in the result tray: one step's card rolls again, and so do
## the cards after it along its threads. The new steps take the old ones'
## place in the current result; Earlier keeps no copy. The old result never
## happened: the values its branch changed are undone (RollJournal) and its
## deck draws go back, before the new roll. The reroll shows at once.
## Steps that can't be rerolled on their own: text rolls inside a result,
## runs dropped from or added into a total, a choice's answer, and the
## "Cancelled" / "Stopped after …" ends.
const FIXED_KEYS := ["inline", "dropped", "combined", "answer", "spread", "cancelled", "limit"]
## How the old step was reached; the new step keeps it, so its thread lights.
const LINK_KEYS := ["pass", "after", "card_thread"]
var m: Main
func _init(p_main: Main) -> void:
m = p_main
## True when step `index` of the finished current result can be rerolled.
func can_reroll(index: int) -> bool:
if m.current.is_empty() or not m.current.get("done", false):
return false
var steps: Array = m.current.get("all", [])
if index < 0 or index >= steps.size():
return false
var step: Dictionary = steps[index]
for key: String in FIXED_KEYS:
var value: Variant = step.get(key)
if value != null and not (value is bool and value == false):
return false
var table := m.data.get_table(str(step.get("table_id", "")))
return table != null and table.kind != RollTable.KIND_VARIABLE
## Index just past the last step under steps[index] (its whole branch).
static func branch_end(steps: Array, index: int) -> int:
var depth := int((steps[index] as Dictionary).get("depth", 0))
var end := index + 1
while end < steps.size() and int((steps[end] as Dictionary).get("depth", 0)) > depth:
end += 1
return end
## Rolls step `index` of the current result again, with its branch.
func reroll(index: int) -> void:
if not can_reroll(index):
return
var current := m.current
var steps: Array = current["all"]
var step: Dictionary = steps[index]
var end := branch_end(steps, index)
_put_back_draws(steps.slice(index, end))
if m.sound:
m.sound.play("roll")
var ctx := Roller.new_context(m.data, m.rng, m.persistent_vars, m.decks)
ctx.chain_vars = (current.get("chain_vars", {}) as Dictionary).duplicate(true)
var journal: Array = current.get("journal", [])
var span := _journal_span(steps, index, end, journal)
if span.x >= 0:
ctx.persistent_changed = RollJournal.undo(journal, span.x, span.y, m.persistent_vars, ctx.chain_vars)
ctx.prompt = _ask
m.sheet_links.before_roll(Formatter.strip_tags(str(current.get("root_name", ""))))
var depth := int(step.get("depth", 0))
var tree: Dictionary = await Roller.roll_async(m.data, str(step["table_id"]), ctx, depth)
if not is_same(current, m.current):
return # another roll started while this one asked a question
var fresh := Roller.flatten(tree, [], depth)
if not fresh.is_empty():
for key: String in LINK_KEYS:
if step.has(key):
(fresh[0] as Dictionary)[key] = step[key]
var all: Array = steps.slice(0, index) + fresh + steps.slice(end)
if span.x >= 0:
for st: Dictionary in fresh:
if st.has("journal_at"):
st["journal_at"] = int(st["journal_at"]) + span.x
current["journal"] = RollJournal.splice(journal, span.x, span.y, ctx.journal, all, index + fresh.size())
current["all"] = all
current["steps"] = all
current["chain_vars"] = ctx.chain_vars.duplicate()
current["fast"] = true
_show(all)
m.local_state.replace_current_roll(ctx)
## The old branch's writes in the journal, as Vector2i(from, to); x is -1 for
## a roll saved before journals were kept (its values can't be undone).
static func _journal_span(steps: Array, index: int, end: int, journal: Array) -> Vector2i:
var from := int((steps[index] as Dictionary).get("journal_at", -1))
var to := journal.size()
if end < steps.size():
to = int((steps[end] as Dictionary).get("journal_at", -1))
if from < 0 or to < from or to > journal.size():
return Vector2i(-1, -1)
return Vector2i(from, to)
## Deck mode: every card the old branch drew goes back into its deck.
func _put_back_draws(old_steps: Array) -> void:
for st: Dictionary in old_steps:
if st.get("stop", false) or not st.has("entry_id"):
continue
var deck: DeckState = m.decks.get(str(st.get("table_id", "")))
if deck:
deck.put_back(str(st["entry_id"]))
## Questions in the rerolled branch ask as usual; a paused thread goes on,
## since you asked for this branch.
func _ask(req: Dictionary) -> Variant:
if str(req.get("kind", "")) == "pause":
return true
return await m.prompt_hook.call(req)
func _show(all: Array) -> void:
m.lit_entries.clear()
m.lit_pipes.clear()
m.lit_texts.clear()
m.lit_columns.clear()
m.rolling._light_steps(all)
m._refresh_lit()
m.rolling._reveal_steps(all)
m.refresh_live()
m.tray.render()