108 lines
3.7 KiB
GDScript3
108 lines
3.7 KiB
GDScript3
|
|
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. Deck draws the old
|
||
|
|
## steps made go back into their decks first. 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)
|
||
|
|
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)
|
||
|
|
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)
|
||
|
|
|
||
|
|
|
||
|
|
## 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()
|