TableTower/ui/main_rolling_seed.gd

76 lines
3.1 KiB
GDScript3
Raw Normal View History

class_name MainRollingSeed
extends RefCounted
## Opens the roll session when a board loads. It brings back the saved history,
## or, on a fresh start, makes up a game already under way (a few earlier rolls
## and some saved variables) so the board doesn't look empty. MainRolling uses it.
var m: Main
func _init(p_main: Main) -> void:
m = p_main
## Opens the session: the collection's saved history, decks and variables. With
## no saved history (a fresh start, or --sample / --screenshot), it seeds a game
## already under way: a couple of earlier rolls, saved variables for the
## Variables tab, and a current roll that runs through the d100 travel table
## into the collapsed trinket table.
func seed_session() -> void:
m.local_state.load_local_state()
if not m.current.is_empty():
m.rolling._light_steps(m.current.get("all", []))
m._refresh_lit()
m.refresh_live()
m.tray.render()
return
var now := Time.get_unix_time_from_system()
# Seeded rolls draw from scratch decks so they don't use up real cards.
var scratch := {}
for seed_roll in [["monsters", 9], ["loot", 4]]:
if m.data.get_table(seed_roll[0]):
var tree := Roller.roll(m.data, seed_roll[0], m.rng, 0, scratch)
var steps := Roller.flatten(tree, [])
m.history.push_front({"collection": m.data.id, "root": seed_roll[0], "root_name": m.data.get_table(seed_roll[0]).name,
"time": now - seed_roll[1] * 60, "steps": steps, "all": steps, "archived": true})
# A quest-style chain earlier in the session saved who guides the party and
# the day's weather, so {@guide} and {@weather} have values.
if m.persistent_vars.is_empty():
var ctx := Roller.new_context(m.data, m.rng, m.persistent_vars, scratch)
for pair in [["@guide", "names"], ["@weather", "weather"]]:
if m.data.get_table(pair[1]):
var node := Roller.roll(m.data, pair[1], m.rng, 0, scratch, ctx)
if not node.is_empty() and not node.has("stop"):
ctx.save(pair[0], Formatter.to_export_bbcode(str(node["summary"])), pair[1])
var start := "wilderness" if m.data.get_table("wilderness") else _first_rollable()
if start == "":
m.refresh_live()
m.tray.render()
return
var best: Dictionary
if m.data.get_table("trinkets"):
best = Roller.roll(m.data, start, m.rng, 0, scratch)
for i in 400:
if Roller.reaches(best, "trinkets"):
break
best = Roller.roll(m.data, start, m.rng, 0, scratch)
else:
# Any other board: one roll with the headless answers, through the real
# variables, so variable and sheet cards have values to show.
best = Roller.roll(m.data, start, m.rng, 0, scratch, Roller.new_context(m.data, m.rng, m.persistent_vars, scratch))
var all := Roller.flatten(best, [])
m.current = {"collection": m.data.id, "root": start, "root_name": m.data.get_table(start).name, "time": now - 60,
"steps": all, "all": all, "done": true, "chain_vars": {}}
Roller.collect_lit(best, m.lit_entries, m.lit_pipes, m.lit_texts)
m._refresh_lit()
m.refresh_live()
m.tray.render()
## The first table that can start a roll (not a variable card), or "".
func _first_rollable() -> String:
for table in m.data.tables:
if table.kind != RollTable.KIND_VARIABLE:
return table.id
return ""