OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-tps / scenes / game / brain_catalogue.gd

58 lines · gd ref:
extends RefCounted
class_name BrainCatalogue
## WHAT A BOT MAY RUN — the allow-list behind the `bot` object's `brain` property
## (bot MVP, 2026-10-04).
##
## Same argument as PrefabCatalogue: a placed bot's `brain` is a value another peer
## wrote, and the room authority RUNS it. A script path from a peer handed to
## ScriptHost would be "run any script this build has, named by a peer" — so the
## value is an id, and only this table turns an id into a file.
##
## ADDING A BOT TYPE = one .gd in bots/ that declares `const PARAMS` (its knobs, the
## PrefabCatalogue.PROPS spec shape) + one line here. The Object panel builds the
## controls from PARAMS; nothing else changes.
##
## THE ID IS DURABLE (it is in the journal): add freely, never rename.

const BRAINS := {
	"idle": {"label": "Idle (no brain)", "path": ""},
	# The creatures' wander, as a brain (#114 phase 2): the default for a placed bot.
	"roam": {"label": "Roam (wander around home)", "path": "res://bots/roam.gd"},
	"patrol": {"label": "Patrol (circle)", "path": "res://bots/patrol.gd"},
	"pathwalker": {"label": "Walk to / wander (A*)", "path": "res://bots/pathwalker.gd"},
	"builder": {"label": "Builder (stacks blocks)", "path": "res://bots/builder.gd"},
}


static func ids() -> Array[String]:
	var out: Array[String] = []
	for k: String in BRAINS:
		out.append(k)
	return out


static func has(id: String) -> bool:
	return BRAINS.has(id)


static func label_of(id: String) -> String:
	return String((BRAINS.get(id, {}) as Dictionary).get("label", id))


static func path_of(id: String) -> String:
	return String((BRAINS.get(id, {}) as Dictionary).get("path", ""))


## A brain's settings, read from its script's `const PARAMS` (spec dicts, the
## PrefabCatalogue.PROPS shape). [] for a brain with none, or no script.
static func params_of(id: String) -> Array:
	var p := path_of(id)
	if p == "" or not ResourceLoader.exists(p):
		return []
	var s := load(p) as Script
	if s == null:
		return []
	var consts: Dictionary = s.get_script_constant_map()
	var v: Variant = consts.get("PARAMS", [])
	return v as Array if v is Array else []