files-game/engine/util.gd
2026-10-01 17:22:18 -04:00

109 lines
3.1 KiB
GDScript

class_name Util
## Helpers for the places where GDScript and TypeScript differ (godot-plan.md §3, "Porting
## hazards"): errors, stable sorting, ordered sets, floor division and JS-style formatting.
## Engine errors so far. TS throws; GDScript has no exceptions, so failures are recorded here and
## pushed to the debugger, and tests check this is still empty.
static var errors: Array[String] = []
## Set while the bot tries out a card on a copy of its view (GreedyBot simulate): failures are still
## recorded, so the bot can tell, but not pushed to the debugger.
static var quiet := false
## Reports an engine error (where TS throws). Returns null so callers can `return Util.fail(...)`.
static func fail(msg: String) -> Variant:
if not quiet:
push_error(msg)
errors.append(msg)
return null
## A copy of `items` sorted by `key` (a Callable returning an int for an item), keeping the order of
## ties. TS's Array.sort is stable; sort_custom isn't.
static func sorted_by(items: Array, key: Callable) -> Array:
var keyed := []
for i in items.size():
keyed.append([key.call(items[i]), i, items[i]])
keyed.sort_custom(func(a, b): return a[0] < b[0] or (a[0] == b[0] and a[1] < b[1]))
var out := []
for k in keyed:
out.append(k[2])
return out
## A copy of `items` sorted like TS's `[...items].sort(cmp)`: `cmp(a, b)` returns a number, negative
## when a goes first, and ties keep their order.
static func sorted_cmp(items: Array, cmp: Callable) -> Array:
var keyed := []
for i in items.size():
keyed.append([i, items[i]])
keyed.sort_custom(func(a, b):
var c: float = cmp.call(a[1], b[1])
return c < 0 or (c == 0 and a[0] < b[0]))
var out := []
for k in keyed:
out.append(k[1])
return out
## Distinct values in first-seen order, like [...new Set(items)].
static func uniq(items: Array) -> Array:
var seen := {}
var out := []
for x in items:
if not seen.has(x):
seen[x] = true
out.append(x)
return out
static func sum(items: Array) -> int:
var t := 0
for x in items:
t += x
return t
## Floor division, like Math.floor(a / b). GDScript's int division truncates toward zero.
static func floordiv(a: int, b: int) -> int:
return (a - posmod(a, b)) / b
static func popcount(n: int) -> int:
var c := 0
while n > 0:
c += n & 1
n >>= 1
return c
## A value as a JS template string shows it: null is "null", not "<null>".
static func js(v: Variant) -> String:
if v == null:
return "null"
return str(v)
## Concatenates arrays into a new one, like [...a, ...b].
static func concat(a: Array, b: Array) -> Array:
var out := a.duplicate()
out.append_array(b)
return out
## JSON numbers as ints. JSON.parse_string returns every number as a float, while the engine's state
## holds only ints; whole floats become ints, recursively. Use on traces, records and saves.
static func ints(v: Variant) -> Variant:
match typeof(v):
TYPE_FLOAT:
return int(v) if v == floorf(v) and absf(v) < 9.0e15 else v
TYPE_ARRAY:
var out := []
for x in v:
out.append(ints(x))
return out
TYPE_DICTIONARY:
var out := {}
for k in v:
out[k] = ints(v[k])
return out
return v