196 lines
5.6 KiB
GDScript
196 lines
5.6 KiB
GDScript
class_name RollTable
|
||
extends RefCounted
|
||
## A named list of entries placed on the board.
|
||
##
|
||
## Plain tables pick by entry weight (6 equal entries = d6). If any entry has a
|
||
## range, the table becomes a ranged dice table (1d100 by default, or any dice
|
||
## expression) where the probability mass of each range sets its chance.
|
||
|
||
const DEFAULT_DICE := "1d100"
|
||
|
||
var id: String = ""
|
||
var name: String = ""
|
||
var position: Vector2 = Vector2.ZERO
|
||
var collapsed: bool = false
|
||
## Longer description (may contain BBCode).
|
||
var details: String = ""
|
||
var tags: Array[String] = []
|
||
## Dice expression for ranged tables, e.g. "1d100", "2d6", "1d8+2".
|
||
var dice: String = DEFAULT_DICE
|
||
## Card mode: entries are drawn without replacement until the deck is empty.
|
||
var card_mode: bool = false
|
||
## What happens when the deck runs out: "reshuffle" or "stop".
|
||
var deck_empty: String = "reshuffle"
|
||
var entries: Array[TableEntry] = []
|
||
|
||
var _dice_cache: Dice = null
|
||
var _dice_cache_text: String = ""
|
||
|
||
|
||
func _init(p_id: String = "", p_name: String = "") -> void:
|
||
id = p_id
|
||
name = p_name
|
||
|
||
|
||
func is_ranged() -> bool:
|
||
for e in entries:
|
||
if e.has_range:
|
||
return true
|
||
return false
|
||
|
||
|
||
## The parsed dice expression (falls back to 1d100 if `dice` is invalid).
|
||
func dice_obj() -> Dice:
|
||
if _dice_cache == null or _dice_cache_text != dice:
|
||
var d := Dice.parse(dice)
|
||
_dice_cache = d if d.valid else Dice.make(1, 100)
|
||
_dice_cache_text = dice
|
||
return _dice_cache
|
||
|
||
|
||
## Label for the dice: "d100", "2d6", "1d8+2". Plain tables label by entry count.
|
||
func dice_label() -> String:
|
||
if is_ranged():
|
||
return dice_obj().label()
|
||
return "d%d" % die_size()
|
||
|
||
|
||
## Lowest value that can be rolled (1 on plain tables).
|
||
func min_value() -> int:
|
||
return dice_obj().min_value() if is_ranged() else 1
|
||
|
||
|
||
## Highest value that can be rolled. Plain tables: the entry count.
|
||
func die_size() -> int:
|
||
if is_ranged():
|
||
return dice_obj().max_value()
|
||
return entries.size()
|
||
|
||
|
||
func total_weight() -> int:
|
||
var w := 0
|
||
for e in entries:
|
||
w += maxi(0, e.weight)
|
||
return w
|
||
|
||
|
||
## Formats a die value the way printed tables do: d100 values are two digits
|
||
## and 100 is written "00".
|
||
func fmt(value: int) -> String:
|
||
var two_digit := dice_obj().is_d100() if is_ranged() else entries.size() == 100
|
||
if two_digit:
|
||
return "00" if value == 100 else "%02d" % value
|
||
return str(value)
|
||
|
||
|
||
func num_label(e: TableEntry, index: int) -> String:
|
||
if not e.has_range:
|
||
return fmt(index + 1)
|
||
if e.range_lo == e.range_hi:
|
||
return fmt(e.range_lo)
|
||
return "%s–%s" % [fmt(e.range_lo), fmt(e.range_hi)]
|
||
|
||
|
||
## Chance (0–1) that a roll lands on this entry.
|
||
func chance(e: TableEntry) -> float:
|
||
if is_ranged():
|
||
if not e.has_range:
|
||
return 0.0
|
||
return dice_obj().probability_between(e.range_lo, e.range_hi)
|
||
var total := total_weight()
|
||
return float(maxi(0, e.weight)) / float(total) if total > 0 else 0.0
|
||
|
||
|
||
func entry_index_for(value: int) -> int:
|
||
for i in entries.size():
|
||
var e := entries[i]
|
||
if e.has_range and value >= e.range_lo and value <= e.range_hi:
|
||
return i
|
||
return -1
|
||
|
||
|
||
func find_entry(eid: String) -> TableEntry:
|
||
for e in entries:
|
||
if e.id == eid:
|
||
return e
|
||
return null
|
||
|
||
|
||
## Describes gaps, overlaps and out-of-span ranges in a ranged table, or "" when clean.
|
||
func range_problems() -> String:
|
||
if not is_ranged():
|
||
return ""
|
||
var lo := min_value()
|
||
var hi := die_size()
|
||
var hits := PackedInt32Array()
|
||
hits.resize(hi + 1)
|
||
var outside := PackedStringArray()
|
||
for e in entries:
|
||
if not e.has_range:
|
||
continue
|
||
for v in range(maxi(lo, e.range_lo), mini(hi, e.range_hi) + 1):
|
||
hits[v] += 1
|
||
if e.range_lo < lo or e.range_hi > hi:
|
||
outside.append("%s–%s" % [fmt(e.range_lo), fmt(e.range_hi)])
|
||
var gaps := _runs(hits, lo, hi, func(h: int) -> bool: return h == 0)
|
||
var overlaps := _runs(hits, lo, hi, func(h: int) -> bool: return h > 1)
|
||
var parts := PackedStringArray()
|
||
if gaps != "":
|
||
parts.append("No result for %s." % gaps)
|
||
if overlaps != "":
|
||
parts.append("%s matches more than one result." % overlaps)
|
||
if not outside.is_empty():
|
||
parts.append("%s is outside %s–%s." % [", ".join(outside), fmt(lo), fmt(hi)])
|
||
return " ".join(parts)
|
||
|
||
|
||
func _runs(hits: PackedInt32Array, lo: int, n: int, test: Callable) -> String:
|
||
var out := PackedStringArray()
|
||
var start := -1
|
||
for v in range(lo, n + 2):
|
||
var on: bool = v <= n and test.call(hits[v])
|
||
if on and start < 0:
|
||
start = v
|
||
elif not on and start >= 0:
|
||
out.append(fmt(start) if start == v - 1 else "%s–%s" % [fmt(start), fmt(v - 1)])
|
||
start = -1
|
||
return ", ".join(out)
|
||
|
||
|
||
func to_dict() -> Dictionary:
|
||
var list: Array = []
|
||
for e in entries:
|
||
list.append(e.to_dict())
|
||
var d := {"id": id, "name": name, "x": roundi(position.x), "y": roundi(position.y), "entries": list}
|
||
if collapsed:
|
||
d["collapsed"] = true
|
||
if details != "":
|
||
d["details"] = details
|
||
if not tags.is_empty():
|
||
d["tags"] = tags.duplicate()
|
||
if is_ranged():
|
||
d["dice"] = dice
|
||
if card_mode:
|
||
d["card_mode"] = true
|
||
if deck_empty != "reshuffle":
|
||
d["deck_empty"] = deck_empty
|
||
return d
|
||
|
||
|
||
static func from_dict(d: Dictionary) -> RollTable:
|
||
var t := RollTable.new(str(d.get("id", "")), str(d.get("name", "")))
|
||
t.position = Vector2(float(d.get("x", 0)), float(d.get("y", 0)))
|
||
t.collapsed = bool(d.get("collapsed", false))
|
||
t.details = str(d.get("details", ""))
|
||
for tag: Variant in d.get("tags", []):
|
||
t.tags.append(str(tag))
|
||
if d.has("dice"):
|
||
t.dice = str(d["dice"])
|
||
elif int(d.get("die", 0)) > 1:
|
||
t.dice = "1d%d" % int(d["die"])
|
||
t.card_mode = bool(d.get("card_mode", false))
|
||
t.deck_empty = "stop" if str(d.get("deck_empty", "reshuffle")) == "stop" else "reshuffle"
|
||
for ed: Variant in d.get("entries", []):
|
||
if ed is Dictionary:
|
||
t.entries.append(TableEntry.from_dict(ed))
|
||
return t
|