From 191a4f79a2cb8b9357f6552307d6e910e3647e7c Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 22 Sep 2026 23:47:18 +0000 Subject: [PATCH] Port resolveQuests, progression and nextContinuations: 182/182 vectors The last three families the port skipped. With them every vendored conformance vector runs and passes against the v0.14.0 pin. - resolve_quests fires quest stage onComplete and outcome effects whose completeWhen/reachedWhen holds, once each (recorded in quest_fired), to a fixpoint, in quest-id then array order. Unconditioned effects never auto-fire, and quest_stages is never written. - Progression: level_for_xp, points_earned, available_points, effective_skill, skill_cap, recompute_skills and the guarded invest_skill_point. Levels and points derive from total-earned xp; per-skill max comes from an optional skills registry. - next_continuations returns a pending cutscene first, then forced routing (a routed character whose winning offer reads its active_dialogue__ flag at the top level, visited set ignored), else discovery with the visited filter. clear_active_dialogue and clear_pending_cutscene are the helpers that consume them. Mutants on once-only firing, the skill cap guard and the forced-offer flag test each turn the suite red. Dropping the fixpoint loop does not: the vectors' cascade case runs in quest-id order, so a single pass already reaches it. That is a gap in the upstream vectors, not fixed here, because a pinned vector is never hand-edited. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Wc6JEuMmSXJEJSRYwGjn1f --- README.md | 56 ++++---- addons/parlance/runtime.gd | 247 +++++++++++++++++++++++++++++++++++- tests/conformance_runner.gd | 49 ++++++- 3 files changed, 321 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 23a6fce..4f1e27f 100644 --- a/README.md +++ b/README.md @@ -23,32 +23,27 @@ vectors rather than against its author's confidence. PASS chooseChoice 10 vectors PASS advanceNode 12 vectors PASS resolveCharacterDialogue 16 vectors - SKIP resolveQuests 6 vectors — not ported yet - SKIP progression 13 vectors — not ported yet - SKIP nextContinuations 4 vectors — not ported yet + PASS nextContinuations 4 vectors + PASS resolveQuests 6 vectors + PASS progression 13 vectors -159 passed, 0 failed, 23 skipped (not yet ported) +182 passed, 0 failed, 0 skipped (not yet ported) ``` ## Compatibility | parlance-gdscript | Parlance spec | Families | |---|---|---| -| `main` (unreleased) | v0.14.0 — pinned to [`f4a25b0`](conformance/PIN) | 8 of 11 | +| `main` (unreleased) | v0.14.0 — pinned to [`f4a25b0`](conformance/PIN) | 11 of 11 | **Versions here are independent of Parlance's**, deliberately, and this table is -how the two are tied together. Two reasons not to mirror the upstream number: +how the two are tied together. Parlance uses the patch slot itself (`v0.4.3` +exists), so a mirrored version would leave this port no room to release its own +fixes without colliding with a spec release. -- Parlance uses the patch slot itself (`v0.4.3` exists), so a mirrored version - leaves this port no room to release its own fixes without colliding with a - spec release. -- `v0.14.0` on this repo would read as "implements Parlance 0.14.0", and it does - not — `resolveQuests`, `progression` and `nextContinuations` are missing. A - version number is the most visible claim a project makes and it should not be - the least accurate. - -So this stays in `0.x` while families are missing. `v1.0.0` will mean something -checkable: complete against the spec, not merely current with it. +`v1.0.0` means something checkable: complete against the spec, not merely +current with it. As of the v0.14.0 pin every family passes, so `main` meets +that bar; it is not tagged yet. [`conformance/PIN`](conformance/PIN) is the authoritative record of which upstream ref the vectors came from — this table is the human-readable summary of @@ -107,19 +102,32 @@ Three things that are easy to get wrong, and are contract rather than style: ## What's ported -`evaluate`, `applyEffect`, `resolveCheck`, `stepDialogue`, `chooseChoice`, -`advanceNode`, `resolveCharacterDialogue`, and the mulberry32 PRNG — enough to -run dialogue end to end, including gated choices, active skill checks and their -conditional modifiers, effects, and character dialogue offers. `check_bonus`, +Every conformance family: `evaluate`, `applyEffect`, `resolveCheck`, +`stepDialogue`, `chooseChoice`, `advanceNode`, `resolveCharacterDialogue`, +`nextContinuations`, `resolveQuests`, the progression functions, and the +mulberry32 PRNG. That covers dialogue end to end (gated choices, active skill +checks and their conditional modifiers, effects, character dialogue offers), +what to offer when a scene ends, quest effects, and levelling. `check_bonus`, `passive_check_passes` and `condition_specificity` are exported too: `passive_check_passes` is the passive-check reveal threshold (`skill + Σbonus >= difficulty`) an engine should use to show or hide a passive choice, so a modifier means the same thing in both modes. -**Not ported:** `resolveQuests`, `progression` and `nextContinuations`. They -report as SKIP rather than passing by omission. Note that quest *conditions* still work: `questOutcome` -re-evaluates an outcome's own `reachedWhen` against current state rather than -reading a fired-record, so endings gated on outcomes resolve without them. +The rest of the public surface, in the same file: + +- **`next_continuations(state, project, visited, current_dialogue_id)`** — what + to offer when a scene ends. A pending cutscene first; then any character + routed with `set_active_dialogue` whose winning offer reads its flag (queued, + visited set ignored); otherwise each character's best eligible offer. + `clear_active_dialogue` and `clear_pending_cutscene` consume them. +- **`resolve_quests(state, project)`** — fires quest stage `onComplete` and + outcome `effects` whose condition holds, once each, to a fixpoint. Run it + after every state change. It never advances a stage for you. +- **Progression** — `level_for_xp`, `points_earned`, `available_points`, + `recompute_skills`, `invest_skill_point`, `effective_skill`, `skill_cap`. + `xp` is total-earned; levels and points are derived. Investing is a guarded + player action, not an effect. Pass the skills registry to honour per-skill + `max`. **Implemented but not vector-covered:** `resolveSpeaker`, `effectiveSpeakerId`, and `resolvePortrait`. The suite has no family for them, so they come from the diff --git a/addons/parlance/runtime.gd b/addons/parlance/runtime.gd index 931a4af..7ada6f6 100644 --- a/addons/parlance/runtime.gd +++ b/addons/parlance/runtime.gd @@ -3,10 +3,9 @@ extends RefCounted ## The Parlance runtime, ported to GDScript. ## -## Ported families: evaluate, applyEffect, resolveCheck, stepDialogue, -## chooseChoice, advanceNode, resolveCharacterDialogue. NOT ported: -## resolveQuests, progression, nextContinuations. The conformance runner -## reports those as SKIP rather than passing them by omission. +## Ports every conformance family: evaluate, applyEffect, resolveCheck, +## stepDialogue, chooseChoice, advanceNode, resolveCharacterDialogue, +## nextContinuations, resolveQuests and progression. ## ## `project` is a plain Dictionary in the vectors' MinimalProject shape ## (`{"factions": {...}, "quests": {...}}`). Missing sub-objects are legal and @@ -618,6 +617,246 @@ static func _visited_has(visited, id) -> bool: return false +# ------------------------------------------------------ nextContinuations -- + + +## The flag a `set_active_dialogue` effect for `character_id` sets. +static func active_dialogue_flag(character_id: String) -> String: + return "active_dialogue__" + character_id + + +## Clears a character's `active_dialogue__` flag (sets it false) so its forced +## offer stops winning. Call once a forced dialogue has been consumed. +static func clear_active_dialogue(character_id: String, state: State) -> State: + var next: State = state.copy() + next.flags[active_dialogue_flag(character_id)] = false + return next + + +## Clears the queued cutscene once the host has played it. +static func clear_pending_cutscene(state: State) -> State: + var next: State = state.copy() + next.pending_cutscene = null + return next + + +## What to offer the player when the current scene ends (the feed model). +## +## Returns an Array of +## {"kind": "cutscene", "cutscene": {...}} +## {"kind": "dialogue", "characterId": id, "dialogue": {...}, "queued": bool} +## +## A pending cutscene always comes first. Then FORCED routing: every character +## whose `active_dialogue__` flag is set and whose winning offer actually READS +## that flag (a top-level `flag = true` conjunct) is queued, ignoring the +## visited set. If any character is forced, only those are returned. Otherwise +## DISCOVERY: each character's best eligible offer, with the visited filter. +## The current dialogue is always excluded; results are de-duplicated by id. +## +## A routed character whose best offer is an ORDINARY one is not forced: it is +## not queued, does not bypass the visited set, and its flag stays set until +## the forced offer can win. +static func next_continuations(state: State, project: Dictionary, visited, current_dialogue_id: String) -> Array: + var seen := {current_dialogue_id: true} + var dialogues: Dictionary = project.get("dialogues", {}) if project.get("dialogues", null) is Dictionary else {} + var characters: Dictionary = project.get("characters", {}) if project.get("characters", null) is Dictionary else {} + + var pending: Array = [] + if state.pending_cutscene != null: + var cutscenes = project.get("cutscenes", {}) + if cutscenes is Dictionary and cutscenes.get(state.pending_cutscene, null) is Dictionary: + pending.append({"kind": "cutscene", "cutscene": cutscenes[state.pending_cutscene]}) + + var forced: Array = [] + for character in characters.values(): + if not character is Dictionary: + continue + var flag := active_dialogue_flag(str(character.get("id", ""))) + if state.flags.get(flag, false) != true: + continue + var resolved = resolve_character_dialogue(state, character, project) + if resolved == null or not dialogues.get(resolved, null) is Dictionary: + continue + var dialogue: Dictionary = dialogues[resolved] + if not _condition_reads_flag(dialogue.get("offer", {}).get("when", null), flag): + continue + if not seen.has(resolved): + seen[resolved] = true + forced.append({"kind": "dialogue", "characterId": character.get("id"), "dialogue": dialogue, "queued": true}) + if not forced.is_empty(): + return pending + forced + + var discovered: Array = [] + for character in characters.values(): + if not character is Dictionary: + continue + var resolved = resolve_character_dialogue(state, character, project, visited if visited != null else []) + if resolved == null or not dialogues.get(resolved, null) is Dictionary: + continue + if not seen.has(resolved): + seen[resolved] = true + discovered.append({"kind": "dialogue", "characterId": character.get("id"), "dialogue": dialogues[resolved], "queued": false}) + return pending + discovered + + +## Does this gate REQUIRE `flag` to be true — a top-level conjunct +## `flag = true`, with `all` flattened? The test for a forced offer. +static func _condition_reads_flag(condition, flag: String) -> bool: + if not condition is Dictionary: + return false + if condition.get("type", "") == "all": + for c in condition.get("of", []): + if _condition_reads_flag(c, flag): + return true + return false + return condition.get("type", "") == "flag" and condition.get("flag", null) == flag and condition.get("value", null) == true + + +# ---------------------------------------------------------- resolveQuests -- + + +## The questFired record key for a stage or outcome. +static func quest_fired_key(quest_id: String, kind: String, id: String) -> String: + return "%s/%s/%s" % [quest_id, kind, id] + + +## Fires quest stage `onComplete` and outcome `effects` whose condition +## (`completeWhen` / `reachedWhen`) holds, once each per playthrough. +## +## Returns {"state": State, "firings": [{quest, kind, id, effects}]}. With no +## firings the input state is returned as is. +## +## - An item fires only if it HAS effects AND a condition, the condition is +## true, and its key is not already in quest_fired. Effects with no +## condition never auto-fire. +## - Runs to a fixpoint: one firing's effects may satisfy another's +## condition. Terminates because each item fires at most once. +## - Deterministic order: quests by id, then stages, then outcomes, each in +## array order. +## - Never writes quest_stages; advancing a stage stays the author's effect. +static func resolve_quests(state: State, project: Dictionary) -> Dictionary: + var quests: Dictionary = project.get("quests", {}) if project.get("quests", null) is Dictionary else {} + var quest_ids := quests.keys() + quest_ids.sort() + + var current := state + var firings: Array = [] + var changed := true + while changed: + changed = false + for qid in quest_ids: + var quest = quests[qid] + if not quest is Dictionary: + continue + for stage in quest.get("stages", []): + if stage is Dictionary: + var r := _try_fire(current, project, str(qid), "stage", stage, "completeWhen", "onComplete", firings) + if r != null: + current = r + changed = true + for outcome in quest.get("outcomes", []): + if outcome is Dictionary: + var r := _try_fire(current, project, str(qid), "outcome", outcome, "reachedWhen", "effects", firings) + if r != null: + current = r + changed = true + + return {"state": current, "firings": firings} + + +## One item's firing attempt: the new state if it fired, else null. +static func _try_fire(state: State, project: Dictionary, quest_id: String, kind: String, item: Dictionary, when_key: String, effects_key: String, firings: Array) -> State: + var effects = item.get(effects_key, null) + if not (effects is Array and not effects.is_empty()): + return null + if not item.has(when_key): + return null + var key := quest_fired_key(quest_id, kind, str(item.get("id", ""))) + if state.quest_fired.has(key): + return null + if not evaluate(item[when_key], state, project): + return null + # `effects` is non-empty, so apply_effects has already copied. + var next := apply_effects(effects, state, project) + next.quest_fired[key] = true + firings.append({"quest": quest_id, "kind": kind, "id": item.get("id", ""), "effects": effects}) + return next + + +# ------------------------------------------------------------ progression -- +# +# `xp` is total-earned and monotonic; levels and points are DERIVED from it, +# never stored, so they cannot desync. `config` is progression.json. `skills` +# is the optional skills registry (id -> skill), for per-skill `max` caps. + + +## Highest threshold index whose value is <= xp. +static func level_for_xp(xp: float, config: Dictionary) -> int: + var level := 0 + var thresholds: Array = config.get("xpThresholds", []) + for i in thresholds.size(): + if xp >= float(thresholds[i]): + level = i + else: + break + return level + + +## Total skill points ever granted at this xp: level x pointsPerLevel. +static func points_earned(xp: float, config: Dictionary) -> float: + return float(level_for_xp(xp, config)) * float(config.get("pointsPerLevel", 0)) + + +## A skill's ceiling: its own `max` if the registry sets one, else maxSkill. +static func skill_cap(skill_id: String, config: Dictionary, skills := {}) -> float: + var skill = skills.get(skill_id, null) + if skill is Dictionary and skill.has("max"): + return float(skill["max"]) + return float(config.get("maxSkill", 0)) + + +## Preset loadout + points invested, clamped to the skill's ceiling. +static func effective_skill(skill_id: String, state: State, config: Dictionary, skills := {}) -> float: + var preset := float(config.get("startingSkills", {}).get(skill_id, 0)) + var invested := float(state.skill_points_spent.get(skill_id, 0)) + return minf(preset + invested, skill_cap(skill_id, config, skills)) + + +## Unspent points: earned minus everything invested. Derived, never stored. +static func available_points(state: State, config: Dictionary) -> float: + var spent := 0.0 + for v in state.skill_points_spent.values(): + spent += float(v) + return points_earned(state.xp, config) - spent + + +## skills = effective_skill for every preset or invested skill. Skills outside +## progression are left alone. Call on load and after any invest. +static func recompute_skills(state: State, config: Dictionary, skills := {}) -> State: + var next: State = state.copy() + var ids := {} + for id in config.get("startingSkills", {}).keys(): + ids[id] = true + for id in state.skill_points_spent.keys(): + ids[id] = true + for id in ids.keys(): + next.skills[id] = effective_skill(str(id), state, config, skills) + return next + + +## Spends one point on `skill_id`. A player action (level-up UI), not an +## effect. A no-op unless a point is available AND the skill is below its +## ceiling, so a point is never wasted on a capped skill. +static func invest_skill_point(state: State, skill_id: String, config: Dictionary, skills := {}) -> State: + if available_points(state, config) <= 0: + return state + if effective_skill(skill_id, state, config, skills) >= skill_cap(skill_id, config, skills): + return state + var next: State = state.copy() + next.skill_points_spent[skill_id] = float(next.skill_points_spent.get(skill_id, 0)) + 1.0 + return recompute_skills(next, config, skills) + + # ------------------------------------------------------ speaker / portrait -- # # NO CONFORMANCE COVERAGE. The vector suite has no family for these three, so diff --git a/tests/conformance_runner.gd b/tests/conformance_runner.gd index 844972e..f55d29a 100644 --- a/tests/conformance_runner.gd +++ b/tests/conformance_runner.gd @@ -39,9 +39,9 @@ func _init() -> void: _run_family("choose_choice.json", "chooseChoice", _check_choose_choice) _run_family("advance.json", "advanceNode", _check_advance) _run_family("resolveCharacterDialogue.json", "resolveCharacterDialogue", _check_character_dialogue) - _skip_family("resolve_quests.json", "resolveQuests") - _skip_family("progression.json", "progression") - _skip_family("nextContinuations.json", "nextContinuations") + _run_family("nextContinuations.json", "nextContinuations", _check_next_continuations) + _run_family("resolve_quests.json", "resolveQuests", _check_resolve_quests) + _run_family("progression.json", "progression", _check_progression) _report() @@ -245,6 +245,49 @@ func _check_character_dialogue(v: Dictionary) -> Variant: return _diff(got, v["expected"]) +## Continuations are compared by id, as the reference harness does: the +## dialogue/cutscene objects themselves are the project's own data. +func _check_next_continuations(v: Dictionary) -> Variant: + var out := Runtime.next_continuations( + State.from_dict(v["state"]), v["project"], v.get("visited", []), v["currentDialogueId"] + ) + var got: Array = [] + for c in out: + if c["kind"] == "cutscene": + got.append({"kind": "cutscene", "cutscene": c["cutscene"].get("id")}) + else: + got.append({"kind": "dialogue", "characterId": c["characterId"], "dialogue": c["dialogue"].get("id"), "queued": c["queued"]}) + return _diff(got, v["expected"]) + + +func _check_resolve_quests(v: Dictionary) -> Variant: + var out := Runtime.resolve_quests(State.from_dict(v["state"]), v["project"]) + var firings: Array = [] + for f in out["firings"]: + firings.append({"quest": f["quest"], "kind": f["kind"], "id": f["id"]}) + return _diff({"state": out["state"].to_dict(), "firings": firings}, v["expected"]) + + +## progression.json mixes five functions; the vector's `fn` picks one. +func _check_progression(v: Dictionary) -> Variant: + var config: Dictionary = v["config"] + var got: Variant + match v["fn"]: + "levelForXp": + got = Runtime.level_for_xp(float(v["xp"]), config) + "pointsEarned": + got = Runtime.points_earned(float(v["xp"]), config) + "availablePoints": + got = Runtime.available_points(State.from_dict(v["state"]), config) + "investSkillPoint": + got = Runtime.invest_skill_point(State.from_dict(v["state"]), v["skillId"], config).to_dict() + "recomputeSkills": + got = Runtime.recompute_skills(State.from_dict(v["state"]), config).to_dict() + _: + return "unknown progression fn '%s'" % v["fn"] + return _diff(got, v["expected"]) + + ## A vector's `rng` is a single float for a one-die roll, or an array with one ## value per die consumed IN ORDER. Overrunning returns 0.0 rather than ## erroring — a port that calls rng() too many times should fail on the roll it