Finished fallback behavior

This commit is contained in:
2019-01-25 10:35:32 -05:00
parent e67f68a51b
commit 712852673a
2 changed files with 50 additions and 15 deletions

View File

@@ -14,6 +14,7 @@ Sometimes
>>> 1
and everything
* you try everything
>>> 0
you try everything
This is what fallback choices are for!
* One other thing

View File

@@ -727,7 +727,7 @@ class Story {
else {
var fallback = collectFallbackChoice();
if (fallback != null) {
return HasText(evaluateFallbackChoice(fallback));
return chooseFallbackChoice(fallback);
} else {
throw 'Ran out of available choices at ${currentLineMapKey()}.';
}
@@ -877,20 +877,8 @@ class Story {
case Some(target):
divert(target);
case None:
// Otherwise, find the line where the choice taken occurs
for (i in currentLineIdx...scriptLines.length) {
switch (scriptLines[i].type) {
case DeclareChoice(choice):
if (choice.id == choiceTaken.id) {
gotoLine(i);
break;
}
default:
}
}
// Move to the line following this choice.
stepLine();
// Otherwise, follow the choice's branch.
gotoChoiceBranch(choiceTaken);
}
// Log the choice's index to the transcript
@@ -899,6 +887,23 @@ class Story {
return choiceTaken.text;
}
private function gotoChoiceBranch(choiceTaken: Choice) {
// find the line where the choice taken occurs
for (i in currentLineIdx...scriptLines.length) {
switch (scriptLines[i].type) {
case DeclareChoice(choice):
if (choice.id == choiceTaken.id) {
gotoLine(i);
break;
}
default:
}
}
// Move to the line following this choice.
stepLine();
}
/**
Search for the next gather that applies to the current choice depth, and follow it.
@return If a suitable gather is found, Empty. Otherwise, an Error frame.
@@ -984,6 +989,35 @@ class Story {
return choices;
}
private function collectFallbackChoice() {
var choices = collectRawChoices();
for (choice in choices) {
if (choice.text.length == 0) {
return choice;
}
}
return null;
}
/**
Return the first frame following a fallback choice, pushing the flow forward down that branch.
**/
private function chooseFallbackChoice(choice: Choice): StoryFrame {
switch (choice.divertTarget) {
case Some(target):
// "choice then arrow syntax" should simply evaluate the lines following this choice
if (target.length == 0) {
gotoChoiceBranch(choice);
return nextFrame();
} else {
return divert(target);
}
default:
throw 'Syntax error in fallback choice: no ->';
}
}
/**
Check if a choice's display condition is satisfied
**/