diff --git a/Readme.md b/Readme.md index 0374995..66abea7 100644 --- a/Readme.md +++ b/Readme.md @@ -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] Widget for displaying storylet links - [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. - [ ] Add storylet code to passages (as comments, a-la Tiny-QBN?) - [ ] CSS styling (probably to go with widgets/macros?) diff --git a/examples/duchess_party.html b/examples/duchess_party.html index e0b99cf..ef992d8 100644 --- a/examples/duchess_party.html +++ b/examples/duchess_party.html @@ -124,7 +124,7 @@ StoryManager.getAllStorylets = function(tag=null) { } 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. @@ -138,10 +138,14 @@ StoryManager.getStorylets = // Check for interruptions - // TODO: Handle more than one interruption if (respect_interrupt) { - for (let i in allStorylets) - if (allStorylets[i].interrupt) return [allStorylets[i]]; + let interruptions = []; + 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 @@ -188,7 +192,7 @@ StoryManager.weightedRandom = function(allStorylets, n) { counter = 0; rand = Math.random() * sum; for (let i=0; i rand) { selectedStorylets.push(allStorylets.splice(i, 1)[0]); break; } @@ -204,19 +208,27 @@ StoryManager.weightedRandom = function(allStorylets, n) { // --------------------------------------------------------------- Macro.add("getStoryletLinks", { handler: function() { - let n, tag; - [n=null, tag=null] = this.args; - State.temporary.nextStorylets = StoryManager.getStorylets(n, tag); - $(this.output).wiki(` + let n, tag, selection; + [n=null, tag=null, selection="ordered"] = this.args; + State.temporary.nextStorylets = StoryManager.getStorylets(n, tag, selection); + $(this.output).wiki(`\ <> \ <> \ [[_storylet.description|_storylet.passage][$currentStorylet=_storylet]]
<
> \ <
> \ - `); + `) } -}) +}); +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(`<>[[${text}|_nextStorylet.passage][$currentStorylet=_nextStorylet]]<>`); + } +}); window.SM = StoryManager; /* twine-user-script #2: "duchess_party.js" */ diff --git a/examples/progress_example.html b/examples/progress_example.html index c178d72..8facf5c 100644 --- a/examples/progress_example.html +++ b/examples/progress_example.html @@ -208,7 +208,7 @@ StoryManager.weightedRandom = function(allStorylets, n) { // --------------------------------------------------------------- Macro.add("getStoryletLinks", { handler: function() { - let n, tag; + let n, tag, selection; [n=null, tag=null, selection="ordered"] = this.args; State.temporary.nextStorylets = StoryManager.getStorylets(n, tag, selection); $(this.output).wiki(`\ @@ -217,7 +217,7 @@ Macro.add("getStoryletLinks", { [[_storylet.description|_storylet.passage][$currentStorylet=_storylet]]
<> \ <> \ - `); + `) } }); diff --git a/examples/simple_space_example.html b/examples/simple_space_example.html index fee05fe..aeef52d 100644 --- a/examples/simple_space_example.html +++ b/examples/simple_space_example.html @@ -124,7 +124,7 @@ StoryManager.getAllStorylets = function(tag=null) { } 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. @@ -138,10 +138,14 @@ StoryManager.getStorylets = // Check for interruptions - // TODO: Handle more than one interruption if (respect_interrupt) { - for (let i in allStorylets) - if (allStorylets[i].interrupt) return [allStorylets[i]]; + let interruptions = []; + 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 @@ -188,7 +192,7 @@ StoryManager.weightedRandom = function(allStorylets, n) { counter = 0; rand = Math.random() * sum; for (let i=0; i rand) { selectedStorylets.push(allStorylets.splice(i, 1)[0]); break; } @@ -204,19 +208,27 @@ StoryManager.weightedRandom = function(allStorylets, n) { // --------------------------------------------------------------- Macro.add("getStoryletLinks", { handler: function() { - let n, tag; - [n=null, tag=null] = this.args; - State.temporary.nextStorylets = StoryManager.getStorylets(n, tag); - $(this.output).wiki(` + let n, tag, selection; + [n=null, tag=null, selection="ordered"] = this.args; + State.temporary.nextStorylets = StoryManager.getStorylets(n, tag, selection); + $(this.output).wiki(`\ <> \ <> \ [[_storylet.description|_storylet.passage][$currentStorylet=_storylet]]
<
> \ <
> \ - `); + `) } -}) +}); +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(`<>[[${text}|_nextStorylet.passage][$currentStorylet=_nextStorylet]]<>`); + } +}); window.SM = StoryManager; /* twine-user-script #2: "Story JavaScript" */ @@ -239,8 +251,9 @@ StoryManager.storylets["Go somewhere"] = { yield storylet; } } -}You find yourself in $currentLocation. You should probably [[jump | Jump]].You prep your ship to jump. -<<getStoryletLinks>><<set $currentLocation = $currentStorylet.planet>> +}You find yourself in $currentLocation. You should probably [[jump | Jump]].You prep your ship to jump. You can choose a destination:<br> +<<getStoryletLinks>> +Or you can <<linkToNextStorylet "jump blind">>.<<set $currentLocation = $currentStorylet.planet>> You orbit around $currentLocation. [[Explore the surface]] or [[Jump]] somewhere else.You take your shuttle down to the surface of $currentLocation. diff --git a/examples/simple_space_example.tw b/examples/simple_space_example.tw index e48c604..727f911 100644 --- a/examples/simple_space_example.tw +++ b/examples/simple_space_example.tw @@ -33,8 +33,9 @@ StoryManager.storylets["Go somewhere"] = { You find yourself in $currentLocation. You should probably [[jump | Jump]]. :: Jump -You prep your ship to jump. +You prep your ship to jump. You can choose a destination:
<> +Or you can <>. :: Orbit <> diff --git a/examples/tutorial.html b/examples/tutorial.html index d0e391a..1c2067e 100644 --- a/examples/tutorial.html +++ b/examples/tutorial.html @@ -124,7 +124,7 @@ StoryManager.getAllStorylets = function(tag=null) { } 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. @@ -138,10 +138,14 @@ StoryManager.getStorylets = // Check for interruptions - // TODO: Handle more than one interruption if (respect_interrupt) { - for (let i in allStorylets) - if (allStorylets[i].interrupt) return [allStorylets[i]]; + let interruptions = []; + 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 @@ -188,7 +192,7 @@ StoryManager.weightedRandom = function(allStorylets, n) { counter = 0; rand = Math.random() * sum; for (let i=0; i rand) { selectedStorylets.push(allStorylets.splice(i, 1)[0]); break; } @@ -204,19 +208,27 @@ StoryManager.weightedRandom = function(allStorylets, n) { // --------------------------------------------------------------- Macro.add("getStoryletLinks", { handler: function() { - let n, tag; - [n=null, tag=null] = this.args; - State.temporary.nextStorylets = StoryManager.getStorylets(n, tag); - $(this.output).wiki(` + let n, tag, selection; + [n=null, tag=null, selection="ordered"] = this.args; + State.temporary.nextStorylets = StoryManager.getStorylets(n, tag, selection); + $(this.output).wiki(`\ <> \ <> \ [[_storylet.description|_storylet.passage][$currentStorylet=_storylet]]
<
> \ <
> \ - `); + `) } -}) +}); +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(`<>[[${text}|_nextStorylet.passage][$currentStorylet=_nextStorylet]]<>`); + } +}); window.SM = StoryManager; /* twine-user-script #2: "Story JavaScript" */ diff --git a/storymanager.js b/storymanager.js index 5ca0c89..9c909ae 100644 --- a/storymanager.js +++ b/storymanager.js @@ -105,7 +105,7 @@ StoryManager.weightedRandom = function(allStorylets, n) { // --------------------------------------------------------------- Macro.add("getStoryletLinks", { handler: function() { - let n, tag; + let n, tag, selection; [n=null, tag=null, selection="ordered"] = this.args; State.temporary.nextStorylets = StoryManager.getStorylets(n, tag, selection); $(this.output).wiki(`\ @@ -114,7 +114,7 @@ Macro.add("getStoryletLinks", { [[_storylet.description|_storylet.passage][$currentStorylet=_storylet]]
<> \ <> \ - `); + `) } });