Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions Parlance.Tests/ConformanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,20 @@ public void StepDialogue()
Assert.Fail($"unexpected error: {outDict["error"]} - {(string)v["description"]}");
}

var expected = (Dictionary<string, object>)v["expected"];
var visibleChoices = (List<object>)outDict["visibleChoices"];
var ids = new List<object>();
var tags = new List<object>();
foreach (var choiceObj in visibleChoices)
{
var choice = (Dictionary<string, object>)choiceObj;
ids.Add(choice.ContainsKey("id") ? choice["id"] : null);
tags.Add(choice.TryGetValue("tags", out var t) ? t : null);
}
var lockedIds = new List<object>();
foreach (Dictionary<string, object> choice in (List<object>)outDict["lockedChoices"])
{
lockedIds.Add(choice.ContainsKey("id") ? choice["id"] : null);
}
var onEnterEffects = (IEnumerable<object>)outDict["onEnterEffects"];

Expand All @@ -139,12 +147,18 @@ public void StepDialogue()

var got = new Dictionary<string, object>
{
{ "nodeId", outNode.TryGetValue("id", out var idObj) ? idObj : null },
{ "visibleChoiceIds", ids },
{ "lockedChoiceIds", lockedIds },
{ "onEnterEffectCount", onEnterList.Count },
{ "onEnterEffects", onEnterList }
};
AssertDeepEqual((Dictionary<string, object>)v["expected"], got, (string)v["description"]);
// Optional fields are checked whenever the vector states them.
if (expected.ContainsKey("nodeId")) got["nodeId"] = outNode.TryGetValue("id", out var idObj) ? idObj : null;
if (expected.ContainsKey("textHidden")) got["textHidden"] = outDict["textHidden"];
if (expected.ContainsKey("text")) got["text"] = outNode.TryGetValue("text", out var textObj) ? textObj : null;
if (expected.ContainsKey("nodeTags")) got["nodeTags"] = outNode.TryGetValue("tags", out var nodeTags) ? nodeTags : null;
if (expected.ContainsKey("visibleChoiceTags")) got["visibleChoiceTags"] = tags;
AssertDeepEqual(expected, got, (string)v["description"]);
}
}

Expand All @@ -166,6 +180,13 @@ public void ChooseChoice()
var rng = RngFrom(rngSpec);

var outDict = Runtime.ChooseChoice(diag, nodeId, choiceId, state, project, rng);

if (v.ContainsKey("expectedError"))
{
AssertExpectedError(v, outDict);
continue;
}

if (outDict.ContainsKey("error"))
{
Assert.Fail($"unexpected error: {outDict["error"]} - {(string)v["description"]}");
Expand Down Expand Up @@ -197,15 +218,13 @@ public void AdvanceNode()
var nodeId = (string)v["nodeId"];
var state = State.FromDict((Dictionary<string, object>)v["state"]);

var outDict = Runtime.AdvanceNode(diag, nodeId, state);
var project = v.ContainsKey("project") ? (Dictionary<string, object>)v["project"] : new Dictionary<string, object>();

var outDict = Runtime.AdvanceNode(diag, nodeId, state, project);

if (v.ContainsKey("expectedError"))
{
string expectedErr = (string)v["expectedError"];
if (!outDict.ContainsKey("error"))
Assert.Fail($"expected error containing {expectedErr}, but succeeded - {(string)v["description"]}");
string errStr = outDict["error"].ToString();
Assert.IsTrue(errStr.Contains(expectedErr), $"error {errStr} does not contain {expectedErr}");
AssertExpectedError(v, outDict);
continue;
}

Expand Down Expand Up @@ -329,6 +348,17 @@ public void Progression()
}
}

// The reference throws; this port returns { error }. Either way the
// message must contain the vector's expectedError substring.
private void AssertExpectedError(Dictionary<string, object> v, Dictionary<string, object> outDict)
{
string expectedErr = (string)v["expectedError"];
if (!outDict.ContainsKey("error"))
Assert.Fail($"expected error containing {expectedErr}, but succeeded - {(string)v["description"]}");
string errStr = outDict["error"].ToString();
Assert.IsTrue(errStr.Contains(expectedErr), $"error {errStr} does not contain {expectedErr} - {(string)v["description"]}");
}

private Func<float> RngFrom(object spec)
{
List<object> values = spec is List<object> l ? l : new List<object> { spec };
Expand Down
62 changes: 45 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ A C# runtime for [Parlance](https://github.com/Orbitope/parlance), a git-native

**Parlance is the authoring tool; this is the Unity runtime for what it produces.** You write your story in Parlance's visual editor — dialogue and quest canvases, a searchable reference index, live playtest — and it saves as human-readable JSON directly in your repo. No database, no import/export step, git as the single source of truth.

This package reads that JSON and runs it in Unity: dialogues, conditions, effects, skill checks (with conditional modifiers), character dialogue offers, quests, endings. It is verified against Parlance's published conformance vectors rather than against its author's confidence.
This package reads that JSON and runs it in Unity: dialogues (conditional lines, fallback and locked choices, line and choice tags, engine commands), conditions, effects, skill checks (with conditional modifiers), character dialogue offers, quests, endings. It is verified against Parlance's published conformance vectors rather than against its author's confidence.

## Compatibility

| parlance-unity | Parlance spec | Families |
|---|---|---|
| `main` (unreleased) | v0.14.0 — pinned to [`f4a25b0`](conformance/PIN) | 11 of 11 |
| `main` (unreleased) | v0.15.0 — pinned to [`a3c6454`](conformance/PIN) | 11 of 11 — 207 / 207 vectors |

[`conformance/PIN`](conformance/PIN) is the authoritative record of which upstream ref the vectors came from.
[`conformance/PIN`](conformance/PIN) is the authoritative record of which upstream ref the vectors came from. The v0.15.0 pin names the release-prep commit the tag will be cut from; it moves to the tag commit once v0.15.0 is tagged.

## Install

Expand All @@ -33,40 +33,68 @@ State is immutable: every entry point returns a new state and never mutates its
```csharp
using Parlance;

// 1. Initialize your project JSON and Game State
// 1. Initialize your project JSON and Game State. The project is the whole
// data set (rules, factions, quests, …); a dialogue is one entry in it.
var projectDict = ParseMyJson(myParlanceJsonTextAsset.text);
var dialogues = (Dictionary<string, object>)projectDict["dialogues"];
var dialogue = (Dictionary<string, object>)dialogues["d_intro"];
var state = State.FromDict(mySavedGameStateJson);

// 2. Present a node: filters choices by showIf, interpolates {placeholders}.
var step = Runtime.StepDialogue(projectDict, "node_start", state);
Debug.Log(step["node"]["text"]);
foreach (var choice in (IEnumerable<object>)step["visibleChoices"])
// 2. Present a node: resolves conditional nodes, partitions the choices,
// interpolates {placeholders}. Every entry point returns { "error": … }
// instead of throwing when the call is invalid.
var step = Runtime.StepDialogue(dialogue, "node_start", state, projectDict);
var node = (Dictionary<string, object>)step["node"];
string nodeId = (string)node["id"]; // the RESOLVED node: may differ from the id you asked for
if (!(bool)step["textHidden"] && node.TryGetValue("text", out var line))
{
Debug.Log(choice["text"]);
Debug.Log(line); // no line when hidden, or on a text-less choice node
}
foreach (Dictionary<string, object> choice in (List<object>)step["visibleChoices"])
{
Debug.Log(choice["text"]); // selectable
}
foreach (Dictionary<string, object> choice in (List<object>)step["lockedChoices"])
{
// Opt-in: failing choices authored whenLocked: "show". Render greyed out,
// with lockedText in place of text when set. Never selectable.
Debug.Log(choice.TryGetValue("lockedText", out var lt) ? lt : choice["text"]);
}

// 3. onEnter effects are RETURNED, not applied. You decide when they fire —
// on first arrival, not on replay.
state = Runtime.ApplyEffects(step["onEnterEffects"], state, projectDict);
// on first arrival, not on replay. Walk them in order and dispatch any
// `engine` effect yourself: ApplyEffect leaves state unchanged for it.
var onEnter = (IEnumerable<object>)step["onEnterEffects"];
foreach (Dictionary<string, object> effect in onEnter)
{
if ((string)effect["type"] == "engine") MyEngine.Dispatch((string)effect["command"], effect.GetValueOrDefault("args"));
}
state = Runtime.ApplyEffects(onEnter, state, projectDict);

// 4. Take a choice. Pass a seeded RNG so checks are reproducible.
var outcome = Runtime.ChooseChoice(
projectDict,
"node_start",
"ch_ask",
state,
dialogue,
nodeId,
"ch_ask",
state,
projectDict,
Rng.ForStep(mySeed, stepIndex)
);
state = outcome["newState"];
if (outcome.ContainsKey("error")) throw new InvalidOperationException((string)outcome["error"]);
state = (State)outcome["newState"];

if (outcome.ContainsKey("checkResult"))
{
Debug.Log(outcome["checkResult"]); // passed, roll, total, skillValue, dice (+ bonus, appliedModifiers)
}
var nextNodeId = outcome.GetValueOrDefault("nextNodeId"); // null on a terminal choice
var nextNodeId = outcome.GetValueOrDefault("nextNodeId"); // null on a terminal choice; resolve it with the next StepDialogue
```

### Important Notes
- **A gated node with choices or `isEnd` is not skipped — only its line is hidden.** `StepDialogue` returns it with `textHidden: true` and `node.text == ""`; its choices are offered, an `isEnd` node still ends the dialogue, and its `onEnter` still fires. Only an interstitial gated node (no choices, not `isEnd`) is skipped, through `next`. `StepDialogue` and `AdvanceNode` share one `ResolveNode` walk.
- **Resolve once per arrival, on the arrival state.** If you apply `onEnter` before presenting, resolve with `ResolveNode` and present with `StepResolvedNode(node, postEffectState, project, arrivalState)` so `textHidden` is judged on arrival while choices filter on the post-effect state. Never re-resolve a node the player has already seen.
- **`ChooseChoice` only accepts a choice that was offered.** A hidden choice, a locked choice, or a `fallback` while a non-fallback choice is visible returns an `error` containing `not selectable`. A fallback is offered only when no other choice is visible.
- **Tags pass through untouched.** `node["tags"]` and each visible choice's `tags` come back exactly as authored; hang animation, audio or camera cues off them.
- **`onEnter` effects are not applied for you.** `StepDialogue` returns them; firing them is the caller's job, on first arrival only. Applying them on every render double-counts on a rewind.
- **A node with no choices is not necessarily over.** If it has `next`, call `AdvanceNode` — that's a listen-only beat. Treating it as the end silently truncates ambient chains.
- **Passive checks never roll.** Show or hide a passive-check choice with `PassiveCheckPasses` (`skill + Σbonus >= difficulty`), so a check modifier means the same thing in both modes.
Expand Down
Loading
Loading