Files
Papay-Shooter/characters/tuning_store.gd
Nicholas ButzkeandClaude Opus 5 08ae85b286 feat(characters): rig anchors, and a lab that edits any tuning group
The weapon lab could tune how a character HOLDS a gun. It could not tune where
the gun sits in the hand, and that is a different question with a different
scope: a hold is per character and weapon, an anchor is a fact about the hand.

RigAnchors adds it. A hand bone's origin is the WRIST, not the palm, and how far
down the palm a grip should sit — and how the gun rolls in the fingers — depends
on how big that character's hand is and how the artist posed the thumb. It
cannot be derived, it differs per character, and it is small. So it is an offset
that defaults to identity, and identity means exactly what the code derived
before anchors existed: an untuned character is bit-for-bit unchanged.

Deliberately NOT a fixed rotation on the mount. There used to be one, and a bone
attachment is expressed in the BONE's axes, which no two rigs agree on — that
constant is why the hand mount points were wrong on every character. The derived
mount stays derived; the anchor is a nudge on top of it, and nothing here spells
a bone name.

The layering, the JSON round trip and the res://-then-user:// write are now
TuningStore, because none of that was ever specific to weapons and two copies of
it would mean two places for "an exported build's tuning pass is silently
discarded" to come back. WeaponHoldTuning is built on it with its file format
unchanged.

The lab is a rig lab now, and it grew three things:

  ANCHORS   a second knob group. It cost a spec table — everything in the lab is
            written against "which specs, which file, keyed on what" rather than
            twice against the two groups, so anchors got sliders, live preview,
            reset, save and clipboard for free. A third group is a third row in
            GROUPS.
  CLIP      audition one animation on its own. The four pose buttons are the
            states the game drives; watching a whole clip end to end is how you
            see where a retarget went wrong, and there was no way to do it.
  SURFACES  what the importer decided each surface IS, and a click to isolate a
            class. Isolating is how the decision gets CHECKED: click `hair` and
            anything else still standing was misclassified. Hidden by swapping
            in a transparent material rather than hiding the node, because a
            mesh is not one class — Miku's body, face and hair are three
            surfaces of one mesh.

And it takes the game's theme, applied to its own panel rather than only to the
Window, for the same reason the pause menu needed it: a CanvasLayer is not a
Control, so theme inheritance stops at one.

debug/rig_anchor_check.gd asserts the physical consequence rather than the
plumbing — an anchor system is easy to build so that the sliders move, the file
saves, the JSON round-trips and the gun does not budge. All six characters move
their weapon by exactly the offset asked for, measured in the attachment's frame
because a world-space delta on an animating skeleton is mostly the idle
animation, and all six return exactly to the derived mount when it is cleared.

Also fixes BoltRule, whose zigzag was self-intersecting: draw_colored_polygon
triangulates, and a crossing outline fails triangulation and draws nothing at
all except a console full of "Invalid polygon data". Same silhouette, walked as
a closed loop, with the ink edge as a polyline rather than a grown polygon —
growing a concave shape from its centroid reintroduces the crossing.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-07-27 14:56:03 -04:00

113 lines
4.1 KiB
GDScript

extends Object
class_name TuningStore
## Per-character art direction on disk, layered so a number can be set once for
## everyone and then contradicted exactly where it matters.
##
## defaults every character, every subject
## skins.<skin>._all this character, every subject
## skins.<skin>.<key> this character, this subject
##
## "Subject" is whatever the caller is keying on — a weapon id for how a gun is
## held, an anchor set for where it is held. The store does not care.
##
## This is the shape WeaponHoldTuning arrived at, pulled out so it is not the
## only thing that can have it. Every knob in the rifle hold started as a
## constant tuned against one rig and was wrong on the next character imported;
## the ones that can be derived from the skeleton now are, and what is left is
## genuinely an artist's judgement — how high a stock rides, where in the palm a
## grip sits. Judgement wants a slider and a file, not another guess in code.
##
## An absent or empty file means "use the built-in defaults", so the game runs
## perfectly well with nothing tuned at all. This only ever ADDS information.
## Where a tuning file is read from and written to.
##
## The project copy is preferred on save so a tuning pass lands in version
## control beside the character it belongs to. `user://` is the fallback for an
## exported build, where res:// is read-only — and it WINS on load, so a pass
## made in a shipped build is not silently discarded.
static func read(res_path: String, user_path: String) -> Dictionary:
var base := _read_one(res_path)
var over := _read_one(user_path)
if over.is_empty():
return base
if base.is_empty():
return over
# Shallow is enough: the layers below are merged per key anyway.
for k in over:
base[k] = over[k]
return base
static func _read_one(path: String) -> Dictionary:
if not FileAccess.file_exists(path):
return {}
var parsed = JSON.parse_string(FileAccess.get_file_as_string(path))
return parsed if typeof(parsed) == TYPE_DICTIONARY else {}
## The resolved table for one character and one subject, most general first.
##
## Vectors survive the JSON round trip as three-element arrays and are rebuilt
## here rather than at every read site — a caller that forgot would get an Array
## where it expected a Vector3, which fails somewhere else entirely.
static func resolve(all: Dictionary, skin_id: String, subject: String) -> Dictionary:
var out := {}
var skins: Dictionary = all.get("skins", {})
var mine: Dictionary = skins.get(skin_id, {})
for layer in [all.get("defaults", {}), mine.get("_all", {}),
mine.get(subject, {})]:
if typeof(layer) != TYPE_DICTIONARY:
continue
for k in layer:
out[k] = layer[k]
return revive(out)
## Three-element arrays back into Vector3s, in place. Anything else is left
## alone, so a knob that is genuinely a list of three numbers would need its own
## handling — none is, and one that was would be a Vector3 anyway.
static func revive(table: Dictionary) -> Dictionary:
for k in table.keys():
var v = table[k]
if v is Array and v.size() == 3:
table[k] = Vector3(float(v[0]), float(v[1]), float(v[2]))
return table
static func flatten(table: Dictionary) -> Dictionary:
var flat := {}
for k in table:
var v = table[k]
flat[k] = [v.x, v.y, v.z] if v is Vector3 else v
return flat
## Store one character+subject's table and write the file. Returns where it went.
static func write(all: Dictionary, skin_id: String, subject: String,
table: Dictionary, res_path: String, user_path: String) -> String:
if not all.has("skins"):
all["skins"] = {}
if not all["skins"].has(skin_id):
all["skins"][skin_id] = {}
all["skins"][skin_id][subject] = flatten(table)
var text := JSON.stringify(all, " ")
for path in [res_path, user_path]:
var f := FileAccess.open(path, FileAccess.WRITE)
if f:
f.store_string(text)
f.close()
return path
return "<could not write>"
## The default for a knob, from a spec table shaped
## `[key, label, minimum, maximum, is_vector, default]`.
static func default_for(specs: Array, key: String):
for spec in specs:
if spec[0] == key:
return spec[5]
return 0.0