Adding linkToNextStorylet to Readme and fixing a bug

This commit is contained in:
David Masad
2021-02-28 10:38:24 -05:00
parent a30931691e
commit f3bafb8d35
7 changed files with 79 additions and 41 deletions
+1 -1
View File
@@ -84,7 +84,7 @@ I have a few Twine hobby projects in various stage of completion, and I found my
- [X] Storylet tagging and filtering (i.e. pull from only a subset of storylets) - [X] Storylet tagging and filtering (i.e. pull from only a subset of storylets)
- [X] Widget for displaying storylet links - [X] Widget for displaying storylet links
- [X] Make the widget into a macro - [X] Make the widget into a macro
- [ ] Weighted random choice - [X] Weighted random choice
- [X] Explore replacing storylet generators returning arrays with the `yield` keyword? **Pro:** produces cleaner code; **Con:** requires users to understand `yield` and remember to use the function * notation. - [X] Explore replacing storylet generators returning arrays with the `yield` keyword? **Pro:** produces cleaner code; **Con:** requires users to understand `yield` and remember to use the function * notation.
- [ ] Add storylet code to passages (as comments, a-la Tiny-QBN?) - [ ] Add storylet code to passages (as comments, a-la Tiny-QBN?)
- [ ] CSS styling (probably to go with widgets/macros?) - [ ] CSS styling (probably to go with widgets/macros?)
+23 -11
View File
@@ -124,7 +124,7 @@ StoryManager.getAllStorylets = function(tag=null) {
} }
StoryManager.getStorylets = StoryManager.getStorylets =
function(n=null, tag=null, selection="weighted", respect_interrupt=true) { function(n=null, tag=null, selection="ordered", respect_interrupt=true) {
/* /*
Get n storylets, prioritizing the highest-priority ones first. Get n storylets, prioritizing the highest-priority ones first.
@@ -138,10 +138,14 @@ StoryManager.getStorylets =
// Check for interruptions // Check for interruptions
// TODO: Handle more than one interruption
if (respect_interrupt) { if (respect_interrupt) {
for (let i in allStorylets) let interruptions = [];
if (allStorylets[i].interrupt) return [allStorylets[i]]; for (let storylet of allStorylets)
if (storylet.interrupt) interruptions.push(storylet);
if (interruptions.length > 0) {
interruptions.sort((a, b) => b.priority - a.priority);
return [interruptions[0]];
}
} }
// Return n or the max // Return n or the max
@@ -188,7 +192,7 @@ StoryManager.weightedRandom = function(allStorylets, n) {
counter = 0; counter = 0;
rand = Math.random() * sum; rand = Math.random() * sum;
for (let i=0; i<allStorylets.length; i++) { for (let i=0; i<allStorylets.length; i++) {
if (counter + allStorylets[i].priority) { if (counter + allStorylets[i].priority > rand) {
selectedStorylets.push(allStorylets.splice(i, 1)[0]); selectedStorylets.push(allStorylets.splice(i, 1)[0]);
break; break;
} }
@@ -204,19 +208,27 @@ StoryManager.weightedRandom = function(allStorylets, n) {
// --------------------------------------------------------------- // ---------------------------------------------------------------
Macro.add("getStoryletLinks", { Macro.add("getStoryletLinks", {
handler: function() { handler: function() {
let n, tag; let n, tag, selection;
[n=null, tag=null] = this.args; [n=null, tag=null, selection="ordered"] = this.args;
State.temporary.nextStorylets = StoryManager.getStorylets(n, tag); State.temporary.nextStorylets = StoryManager.getStorylets(n, tag, selection);
$(this.output).wiki(` $(this.output).wiki(`\
<<for _storylet range _nextStorylets>> \ <<for _storylet range _nextStorylets>> \
<<capture _storylet>> \ <<capture _storylet>> \
[[_storylet.description|_storylet.passage][$currentStorylet=_storylet]]<br> [[_storylet.description|_storylet.passage][$currentStorylet=_storylet]]<br>
<</capture>> \ <</capture>> \
<</for>> \ <</for>> \
`); `)
} }
}) });
Macro.add("linkToNextStorylet", {
handler: function() {
let text, tag, selection;
[text, tag=null, selection="weighted"] = this.args;
State.temporary.nextStorylet = StoryManager.getStorylets(1, tag, selection)[0];
$(this.output).wiki(`<<capture _nextStorylet>>[[${text}|_nextStorylet.passage][$currentStorylet=_nextStorylet]]<</capture>>`);
}
});
window.SM = StoryManager; window.SM = StoryManager;
/* twine-user-script #2: "duchess_party.js" */ /* twine-user-script #2: "duchess_party.js" */
+2 -2
View File
@@ -208,7 +208,7 @@ StoryManager.weightedRandom = function(allStorylets, n) {
// --------------------------------------------------------------- // ---------------------------------------------------------------
Macro.add("getStoryletLinks", { Macro.add("getStoryletLinks", {
handler: function() { handler: function() {
let n, tag; let n, tag, selection;
[n=null, tag=null, selection="ordered"] = this.args; [n=null, tag=null, selection="ordered"] = this.args;
State.temporary.nextStorylets = StoryManager.getStorylets(n, tag, selection); State.temporary.nextStorylets = StoryManager.getStorylets(n, tag, selection);
$(this.output).wiki(`\ $(this.output).wiki(`\
@@ -217,7 +217,7 @@ Macro.add("getStoryletLinks", {
[[_storylet.description|_storylet.passage][$currentStorylet=_storylet]]<br> [[_storylet.description|_storylet.passage][$currentStorylet=_storylet]]<br>
<</capture>> \ <</capture>> \
<</for>> \ <</for>> \
`); `)
} }
}); });
+26 -13
View File
@@ -124,7 +124,7 @@ StoryManager.getAllStorylets = function(tag=null) {
} }
StoryManager.getStorylets = StoryManager.getStorylets =
function(n=null, tag=null, selection="weighted", respect_interrupt=true) { function(n=null, tag=null, selection="ordered", respect_interrupt=true) {
/* /*
Get n storylets, prioritizing the highest-priority ones first. Get n storylets, prioritizing the highest-priority ones first.
@@ -138,10 +138,14 @@ StoryManager.getStorylets =
// Check for interruptions // Check for interruptions
// TODO: Handle more than one interruption
if (respect_interrupt) { if (respect_interrupt) {
for (let i in allStorylets) let interruptions = [];
if (allStorylets[i].interrupt) return [allStorylets[i]]; for (let storylet of allStorylets)
if (storylet.interrupt) interruptions.push(storylet);
if (interruptions.length > 0) {
interruptions.sort((a, b) => b.priority - a.priority);
return [interruptions[0]];
}
} }
// Return n or the max // Return n or the max
@@ -188,7 +192,7 @@ StoryManager.weightedRandom = function(allStorylets, n) {
counter = 0; counter = 0;
rand = Math.random() * sum; rand = Math.random() * sum;
for (let i=0; i<allStorylets.length; i++) { for (let i=0; i<allStorylets.length; i++) {
if (counter + allStorylets[i].priority) { if (counter + allStorylets[i].priority > rand) {
selectedStorylets.push(allStorylets.splice(i, 1)[0]); selectedStorylets.push(allStorylets.splice(i, 1)[0]);
break; break;
} }
@@ -204,19 +208,27 @@ StoryManager.weightedRandom = function(allStorylets, n) {
// --------------------------------------------------------------- // ---------------------------------------------------------------
Macro.add("getStoryletLinks", { Macro.add("getStoryletLinks", {
handler: function() { handler: function() {
let n, tag; let n, tag, selection;
[n=null, tag=null] = this.args; [n=null, tag=null, selection="ordered"] = this.args;
State.temporary.nextStorylets = StoryManager.getStorylets(n, tag); State.temporary.nextStorylets = StoryManager.getStorylets(n, tag, selection);
$(this.output).wiki(` $(this.output).wiki(`\
<<for _storylet range _nextStorylets>> \ <<for _storylet range _nextStorylets>> \
<<capture _storylet>> \ <<capture _storylet>> \
[[_storylet.description|_storylet.passage][$currentStorylet=_storylet]]<br> [[_storylet.description|_storylet.passage][$currentStorylet=_storylet]]<br>
<</capture>> \ <</capture>> \
<</for>> \ <</for>> \
`); `)
} }
}) });
Macro.add("linkToNextStorylet", {
handler: function() {
let text, tag, selection;
[text, tag=null, selection="weighted"] = this.args;
State.temporary.nextStorylet = StoryManager.getStorylets(1, tag, selection)[0];
$(this.output).wiki(`<<capture _nextStorylet>>[[${text}|_nextStorylet.passage][$currentStorylet=_nextStorylet]]<</capture>>`);
}
});
window.SM = StoryManager; window.SM = StoryManager;
/* twine-user-script #2: "Story JavaScript" */ /* twine-user-script #2: "Story JavaScript" */
@@ -239,8 +251,9 @@ StoryManager.storylets["Go somewhere"] = {
yield storylet; yield storylet;
} }
} }
}</script><tw-passagedata pid="1" name="Start" tags="" position="100,100" size="100,100">You find yourself in $currentLocation. You should probably [[jump | Jump]].</tw-passagedata><tw-passagedata pid="2" name="Jump" tags="" position="225,100" size="100,100">You prep your ship to jump. }</script><tw-passagedata pid="1" name="Start" tags="" position="100,100" size="100,100">You find yourself in $currentLocation. You should probably [[jump | Jump]].</tw-passagedata><tw-passagedata pid="2" name="Jump" tags="" position="225,100" size="100,100">You prep your ship to jump. You can choose a destination:&lt;br&gt;
&lt;&lt;getStoryletLinks&gt;&gt;</tw-passagedata><tw-passagedata pid="3" name="Orbit" tags="" position="350,100" size="100,100">&lt;&lt;set $currentLocation = $currentStorylet.planet&gt;&gt; &lt;&lt;getStoryletLinks&gt;&gt;
Or you can &lt;&lt;linkToNextStorylet &quot;jump blind&quot;&gt;&gt;.</tw-passagedata><tw-passagedata pid="3" name="Orbit" tags="" position="350,100" size="100,100">&lt;&lt;set $currentLocation = $currentStorylet.planet&gt;&gt;
You orbit around $currentLocation. You orbit around $currentLocation.
[[Explore the surface]] or [[Jump]] somewhere else.</tw-passagedata><tw-passagedata pid="4" name="Explore the surface" tags="" position="475,100" size="100,100">You take your shuttle down to the surface of $currentLocation. [[Explore the surface]] or [[Jump]] somewhere else.</tw-passagedata><tw-passagedata pid="4" name="Explore the surface" tags="" position="475,100" size="100,100">You take your shuttle down to the surface of $currentLocation.
+2 -1
View File
@@ -33,8 +33,9 @@ StoryManager.storylets["Go somewhere"] = {
You find yourself in $currentLocation. You should probably [[jump | Jump]]. You find yourself in $currentLocation. You should probably [[jump | Jump]].
:: Jump :: Jump
You prep your ship to jump. You prep your ship to jump. You can choose a destination:<br>
<<getStoryletLinks>> <<getStoryletLinks>>
Or you can <<linkToNextStorylet "jump blind">>.
:: Orbit :: Orbit
<<set $currentLocation = $currentStorylet.planet>> <<set $currentLocation = $currentStorylet.planet>>
+23 -11
View File
@@ -124,7 +124,7 @@ StoryManager.getAllStorylets = function(tag=null) {
} }
StoryManager.getStorylets = StoryManager.getStorylets =
function(n=null, tag=null, selection="weighted", respect_interrupt=true) { function(n=null, tag=null, selection="ordered", respect_interrupt=true) {
/* /*
Get n storylets, prioritizing the highest-priority ones first. Get n storylets, prioritizing the highest-priority ones first.
@@ -138,10 +138,14 @@ StoryManager.getStorylets =
// Check for interruptions // Check for interruptions
// TODO: Handle more than one interruption
if (respect_interrupt) { if (respect_interrupt) {
for (let i in allStorylets) let interruptions = [];
if (allStorylets[i].interrupt) return [allStorylets[i]]; for (let storylet of allStorylets)
if (storylet.interrupt) interruptions.push(storylet);
if (interruptions.length > 0) {
interruptions.sort((a, b) => b.priority - a.priority);
return [interruptions[0]];
}
} }
// Return n or the max // Return n or the max
@@ -188,7 +192,7 @@ StoryManager.weightedRandom = function(allStorylets, n) {
counter = 0; counter = 0;
rand = Math.random() * sum; rand = Math.random() * sum;
for (let i=0; i<allStorylets.length; i++) { for (let i=0; i<allStorylets.length; i++) {
if (counter + allStorylets[i].priority) { if (counter + allStorylets[i].priority > rand) {
selectedStorylets.push(allStorylets.splice(i, 1)[0]); selectedStorylets.push(allStorylets.splice(i, 1)[0]);
break; break;
} }
@@ -204,19 +208,27 @@ StoryManager.weightedRandom = function(allStorylets, n) {
// --------------------------------------------------------------- // ---------------------------------------------------------------
Macro.add("getStoryletLinks", { Macro.add("getStoryletLinks", {
handler: function() { handler: function() {
let n, tag; let n, tag, selection;
[n=null, tag=null] = this.args; [n=null, tag=null, selection="ordered"] = this.args;
State.temporary.nextStorylets = StoryManager.getStorylets(n, tag); State.temporary.nextStorylets = StoryManager.getStorylets(n, tag, selection);
$(this.output).wiki(` $(this.output).wiki(`\
<<for _storylet range _nextStorylets>> \ <<for _storylet range _nextStorylets>> \
<<capture _storylet>> \ <<capture _storylet>> \
[[_storylet.description|_storylet.passage][$currentStorylet=_storylet]]<br> [[_storylet.description|_storylet.passage][$currentStorylet=_storylet]]<br>
<</capture>> \ <</capture>> \
<</for>> \ <</for>> \
`); `)
} }
}) });
Macro.add("linkToNextStorylet", {
handler: function() {
let text, tag, selection;
[text, tag=null, selection="weighted"] = this.args;
State.temporary.nextStorylet = StoryManager.getStorylets(1, tag, selection)[0];
$(this.output).wiki(`<<capture _nextStorylet>>[[${text}|_nextStorylet.passage][$currentStorylet=_nextStorylet]]<</capture>>`);
}
});
window.SM = StoryManager; window.SM = StoryManager;
/* twine-user-script #2: "Story JavaScript" */ /* twine-user-script #2: "Story JavaScript" */
+2 -2
View File
@@ -105,7 +105,7 @@ StoryManager.weightedRandom = function(allStorylets, n) {
// --------------------------------------------------------------- // ---------------------------------------------------------------
Macro.add("getStoryletLinks", { Macro.add("getStoryletLinks", {
handler: function() { handler: function() {
let n, tag; let n, tag, selection;
[n=null, tag=null, selection="ordered"] = this.args; [n=null, tag=null, selection="ordered"] = this.args;
State.temporary.nextStorylets = StoryManager.getStorylets(n, tag, selection); State.temporary.nextStorylets = StoryManager.getStorylets(n, tag, selection);
$(this.output).wiki(`\ $(this.output).wiki(`\
@@ -114,7 +114,7 @@ Macro.add("getStoryletLinks", {
[[_storylet.description|_storylet.passage][$currentStorylet=_storylet]]<br> [[_storylet.description|_storylet.passage][$currentStorylet=_storylet]]<br>
<</capture>> \ <</capture>> \
<</for>> \ <</for>> \
`); `)
} }
}); });