-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstory-javascript.min.js
More file actions
69 lines (48 loc) · 15.7 KB
/
Copy pathstory-javascript.min.js
File metadata and controls
69 lines (48 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
TinyQBN: quality-based narratives in Twine 2 & Sugarcube.
The UUID code and the SugarCube story format have their own licenses, shown below. The rest of this is released under the ISC license:
Copyright 2019 Joshua I. Grams <josh@qualdan.com>
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
////////////////////////////////////////////////////////////////////////
// UUID v4
The MIT License (MIT)
uuidv4.min.js is Copyright (c) 2010-2016 Robert Kieffer and other contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
////////////////////////////////////////////////////////////////////////
// SugarCube
Copyright (c) 2013-2019 Thomas Michael Edwards <thomasmedwards@gmail.com>.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
State.initPRNG();var QBN={meta:{}};window.QBN=QBN,QBN.parseTagsInto=function(a,b){let c,d=new RegExp("("+["[a-zA-Z0-9-_]+(?:\\s+|$)","[a-zA-Z0-9_]+:[^\\n]*(?:\\n|$)\\s*","[^\\n]+(?:\\n|$)\\s*"].join(")|(")+")","g"),e=!1;for(;null!=(c=d.exec(a));)if(c[1]!==void 0)e?(b[b.length-1]+=c[1],!/\\\n/.test(c[1])&&(e=!1)):b.push(c[1].trim());else if(c[2]!==void 0){let a=c[2].replace(":","- "),d=a.replace("\\\n","");d.length!==a.length&&(e=!0),b.push(d)}else if(c[3]!==void 0){if(e)b[b.length-1]+=c[3];else throw new Error("Invalid metadata: "+JSON.stringify(c[3]));/(^|[^\\])\n/.test(c[3])&&(e=!1)}return b};const commentPattern=/\/\*\s*QBN\s([^*]*)\s\*\//g;Story.lookupWith(function(a){let b,c=a.tags.slice();for(;null!=(b=commentPattern.exec(a.text));){let a=b[1].trim();QBN.parseTagsInto(a,c)}return QBN.meta[a.title]=c,!1}),QBN.tags=function(a){return QBN.meta[a.title]||a.tags};var getVar=State.getVar||Wikifier.getValue,setVar=State.setVar||Wikifier.setValue;setVar("$QBN",{type:{},priority:{}}),$(document).on(":passagestart",function(a){QBN.current=a.passage.title,passageType(a.passage,!1)});function toArgument(a){if("object"==typeof a&&(a=a.title),"string"==typeof a)return JSON.stringify(a);throw new Error("Cards must be referred to by title (got "+typeof a+").")}function toPassage(a){if("object"==typeof a)if(a.link&&a.text)a=a.link;else return a;if("string"!=typeof a)throw new Error("Cards must be referred to by title (got "+typeof a+").");else{if(Story.has(a))return Story.get(a);let b=JSON.parse(a),c=b[0],d=b[1],e=b[2];if(passage()===c){let a=getVar(d);if(null!=a&&null!=a[e])return a[e]}throw new Error("No such passage or choice \""+a+"\".")}}QBN.toPassage=toPassage;function passageType(a,b){var c,d=QBN.tags(a);c=0<=d.indexOf("sticky-card")?"sticky-card":!!(0<=d.indexOf("card"))&&"card";var e=getVar("$QBN").type,f=e[a.title];return void 0===f&&(f=c),void 0!==b&&b!==f&&(null===b?e[a.title]=!1:!1===b?"card"===f&&(e[a.title]=!1):b===c?delete e[a.title]:e[a.title]=b),f}function shuffle(b){for(let a=b.length;0<a;){let c,d=Math.floor(a--*Math.random());c=b[a],b[a]=b[d],b[d]=c}return b}QBN.alphabetically=function(c,d){if(null==c||null==d){let a="QBN.alphabetically seems to be missing an argument.";throw a+=" If you wrote .sort(QBN.alphabetically()) you'll want to",a+=" remove the empty set of parentheses so you're passing the",a+=" function itself, not calling it and passing its return value.",new Error(a)}return c=toPassage(c).title.toLowerCase(),d=toPassage(d).title.toLowerCase(),c<d?-1:c>d?1:0};function choose(a,b,c){var d=Math.min;if(null==b||!1===b)return c?a:shuffle(a);b=Math.max(0,d(b,a.length));for(var e=d(b,a.length-b),f=e===b||void 0,g=[],h={},j=0;j<e;){var k=Math.floor(State.random()*a.length);h[k]===void 0&&(h[k]=!0,++j)}for(k=0;k<a.length;++k)h[k]===f&&g.push(a[k]);return c?g:shuffle(g)}function passagePriority(a,b){var c,d=QBN.tags(a);c=0<=d.indexOf("urgent")?"urgent":0<=d.indexOf("important")?"important":"normal";var e=getVar("$QBN").priority,f=e[a.title];return void 0===f&&(f=c),void 0!==b&&b!==f&&(b===c?delete e[a.title]:e[a.title]=b),f}Macro.add("cardpriority",{handler:function(){var a=toPassage(this.args[0]);passagePriority(a,this.args[1])}});function select(a,b,c){let d={};a(function(a){if(a=toPassage(a),QBN.visible(a)){let b=passagePriority(a);d[b]||(d[b]=[]),d[b].push(a)}return!1});let e;d.urgent?e=choose(d.urgent,b,c):(d.important?(e=choose(d.important,b,c),b&&(b-=e.length)):e=[],d.normal&&(e=e.concat(choose(d.normal,b,c))));for(var f=0;f<e.length;++f)e[f]=e[f].title;return e}QBN.filter=function(a,b,c){let d=a.filter.bind(a);return select(d,b,c)},QBN.cards=function(a,b){let c=Story.lookupWith.bind(Story);return select(c,a,b)};var operators={eq:function(c,a){return c==a},neq:function(c,a){return c!=a},ne:function(c,a){return c!=a},lt:function(c,a){return c<a},gt:function(c,a){return c>a},lte:function(c,a){return c<=a},le:function(c,a){return c<=a},gte:function(c,a){return c>=a},ge:function(c,a){return c>=a},eqvar:function(c,a,b,d){return c==QBN.value(d)},neqvar:function(c,a,b,d){return c==QBN.value(d)},ltvar:function(c,a,b,d){return c==QBN.value(d)},gtvar:function(c,a,b,d){return c==QBN.value(d)},ltevar:function(c,a,b,d){return c==QBN.value(d)},gtevar:function(c,a,b,d){return c==QBN.value(d)},before:function(c,a,b){return QBN.progress(b,c)<QBN.progress(b,a)},during:function(c,a,b){return QBN.progress(b,c)===QBN.progress(b,a)},after:function(c,a,b){return QBN.progress(b,c)>QBN.progress(b,a)},endingAt:function(c,a,b){return QBN.progress(b,c)<=QBN.progress(b,a)},startingAt:function(c,a,b){return QBN.progress(b,c)>=QBN.progress(b,a)}},operatorNames={eq:"to be",ne:"not to be",lt:"to be less than",le:"to be at most",gt:"to be greater than",ge:"to be at least"};QBN.functions={twinescript:{match:/^\s.*/,action:function(a){try{return Scripting.evalTwineScript(a[0])}catch(b){throw b.message+=" in: "+a[0],b}},description:function(a){return"\"\"\""+a[0]+"\"\"\""}},not:{match:/^not-(.+)/,action:function(a){return!QBN.requirementMet(a[1])},description:function(a){var b=QBN.description(a[1]);return null==b?b:"not "+b}},passage:{match:/^passage-(.+)/,action:function(a){return passage()===a[1]||passage()===a[1].replace(/_/g," ")},description:function(){return null}},tagged:{match:/^tagged-(.+)/,action:function(a){return tags().includes(a[1])},description:function(){return null}},visited:{match:/^visited-([^-]+)/,action:function(a){return 0<visited(a[1])},description:function(){return null}},random:{match:/^random-([0-9]+)/,action:function(a){return State.random()<a[1]/100},description:function(){return null}},compare:{match:/^(.*)-(eq|ne|lt|gt|le|ge|neq|lte|gte|eqvar|neqvar|ltvar|gtvar|ltevar|gtevar|before|during|after|startingAt|endingAt)-(.*)/,action:function(a){var b=QBN.value(a[1]),c=operators[a[2]],d=a[3];return"number"==typeof b&&(d=d.replace("_",".")),c(b,d,a[1],a[3])},description:function(a){var b=QBN.description(a[1]),c=operatorNames[a[2]],d=a[3];return"number"==typeof b&&(d=+d.replace("_",".")),null==b||null==c?null:`${b} ${c} ${d}`}}};function invalidName(a){if(!/^[$_][_a-zA-Z][_a-zA-Z0-9]*$/.test(a))return"invalid name "+JSON.stringify(a)+"."}QBN.range=function(a,b){var c=b.length;if("number"!=typeof c||2>c){var d="QBN.range: invalid range spec:";throw d+=" must have at least two values",d+=" (got "+JSON.stringify(c)+").",new Error(d)}for(var e,f,g,h,j=0;j<b.length;++j){if(h=b[j],"string"==typeof h&&"string"==typeof g){var d="QBN.range: invalid range spec "+JSON.stringify(b);throw d+=": may not have two consecutive strings.",new Error(d)}if(g=h,"string"==typeof h)e=h;else if("number"==typeof h){if("undefined"!=typeof f&&h<=f){var d="QBN.range: invalid range spec "+JSON.stringify(b);throw d+=": numbers must be strictly increasing.",new Error(d)}if(a<h)if(e)break;else return;f=h,e=void 0}else{var d="QBN.range: invalid range spec "+JSON.stringify(b);throw d+=": may only contain strings and numbers.",new Error(d)}}return e},Macro.add("range",{handler:function(){var a,b,c=this.args[0];try{"string"==typeof c?b=getVar(c):(b=c,c=firstWord(this.args.raw)),a=QBN.range(b,this.args.slice(1))}catch(a){return this.error("<<range>>: "+(a.message||a))}setVar("_"+a+"_"+c.substring(1),!0),setVar("_"+c.substring(1)+"_range",a)}}),QBN.value=function(a){var b=State.variables,c=State.temporary;return null==c[a]?b[a]:c[a]},QBN.requirementMet=function(a){var b=null;for(var c in QBN.functions)if(QBN.functions.hasOwnProperty(c)){var d=QBN.functions[c],e=d.match.exec(a);if(e){b=d.action(e);break}}return null===b&&(b=QBN.value(a)),!!b},QBN.description=function(a){var b=null;for(var c in QBN.functions)if(QBN.functions.hasOwnProperty(c)){var d=QBN.functions[c],e=d.match.exec(a);if(e){b=d.description(e);break}}return null===b&&(b=a),b},QBN.tagsMatch=function(a,b){a=toPassage(a);let c=QBN.current;QBN.current=a.title;let d=QBN.tags(a);for(var e=0;e<d.length;++e){var f=d[e],g=b.exec(f);if(g)f=f.substring(g[0].length);else continue;if(!QBN.requirementMet(f))return QBN.current=c,!1}return QBN.current=c,!0},QBN.requirements=function(a){for(var b=[],c=QBN.tags(a),d=0;d<c.length;++d){var e=c[d],f=/^(req|also)-/.exec(e);if(f)e=e.substring(f[0].length);else continue;var g=QBN.description(e);null!=g&&b.push(g)}return b},QBN.status=function(a){return passageType(toPassage(a))},QBN.visible=function(a){return a=toPassage(a),!!passageType(a)&&QBN.tagsMatch(a,/^req-/)},QBN.available=function(a){return a=toPassage(a),!!passageType(a)&&QBN.tagsMatch(a,/^(req|also)-/)},Macro.add("addcard",{handler:function(){var a=toPassage(this.args[0]),b=this.args[1]?"sticky-card":"card";passageType(a,b)}}),Macro.add("removecard",{handler:function(){var a=toPassage(this.args[0]),b=!1!==this.args[1]&&null;passageType(a,b)}}),Macro.add("includecard",{handler:function(){var a=toPassage(this.args[0]),b=$(this.output);let c=QBN.current;QBN.current=a.title,b.wiki(Passage.prototype.processText.call(a)),QBN.current=c}});function list(a){return"object"!=typeof a||null==a.length?[a]:a}Macro.add("includeall",{handler:function(){var a=list(this.args[0]),b=this.args[1]||"content";if(!Macro.has(b))return this.error("No such widget "+JSON.stringify(b)+".");for(var d=this.args[2],e=$(this.output),f=0;f<a.length;++f){var g=a[f],c=toPassage(g);e.wiki("<<"+b+" "+toArgument(g)+">>"),d&&f<a.length-1&&(Macro.has(d)?e.wiki("<<"+d+" "+(f===a.length-2)+">>"):e.wiki(d))}}}),Macro.add("requirements",{handler:function(){var a=$(this.output),b=toPassage(this.args[0]||QBN.current),c=this.args[1];if(c&&!Macro.has(c))return this.error("No such widget "+JSON.stringify(c)+".");for(var d,e=this.args[2]||"comma",f=QBN.requirements(b),g=0;g<f.length;++g)d=f[g],c?a.wiki("<<"+c+" "+JSON.stringify(d)+">>"):a.wiki(d),e&&g<f.length-1&&(Macro.has(e)?a.wiki("<<"+e+" "+(g===f.length-2)+">>"):a.append(document.createTextNode(e)))}});function addTo(a,b){var c="string"==typeof b?b:b.title,d=!a[c];return d&&(a[c]=!0),d}function firstWord(a){var b=a.indexOf(" ");return a.substring(0,-1==b?a.length:b)}Macro.add("fillhand",{handler:function(){try{var a=this.args[0],b=this.args[1],c=this.args[2];if(!a){a=[];var d=firstWord(this.args.raw).substring(1);if(!setVar(d,a))return this.error("<<fillhand>>: failed to set hand \""+d+"\".")}var e,f={};for(e=0;e<a.length;++e)addTo(f,a[e]);for(c=c.filter(function(a){return addTo(f,a)}),c=choose(c,b-a.length),e=0;e<c.length;++e)a.push(c[e])}catch(a){return this.error("<<fillhand>>: "+("object"==typeof a?a.message:a))}}}),Macro.add("card",{tags:["contents"],handler:function(){var a=$(this.output);if(getVar("_qbn_cover"))a.wiki(this.payload[0].contents.trim());else for(var b=1;b<this.payload.length;++b)a.wiki(this.payload[b].contents.trim())}});function partialChoice(a){return 0<a.length&&null==a[a.length-1].text}function beginChoice(a,b,c){0>c.indexOf("card")&&0>c.indexOf("sticky-card")&&c.push("card"),a.push({title:JSON.stringify([passage(),b,a.length]),tags:c})}Macro.add("choices",{tags:["when","offer"],handler:function(){var a=this.args.raw,b=invalidName(a);if(b)return this.error(b);for(var c=[],d=0;d<this.payload.length;++d){var e=this.payload[d],b=!1;switch(e.name){case"choices":if(""!==e.contents.trim())return this.error("All <<choices>> content must be in sub-tags.");break;case"when":if(partialChoice(c))return this.error("This choice already has a \"when\" clause (section "+d+", "+e.contents.substring(0,30)+").");var f=QBN.parseTagsInto(e.contents.trim(),[]);beginChoice(c,a,f);break;case"offer":partialChoice(c)||beginChoice(c,a,[]),c[c.length-1].text=e.contents.trim(),""!==e.args[0]&&(c[e.args[0]]=c[c.length-1]);}}setVar(a,c)}}),QBN.progress=function(a,b){a=a.replace(/^[$_]/,"");let c=setup[a];if(null==c)throw"No such progress sequence "+JSON.stringify(a)+".";for(let d=0;d<c.length;d+=1)if(c[d]===b||c[d].replace(/[^a-zA-Z0-9_]/g,"")===b)return d;throw"No such progress value "+JSON.stringify(b)+" in "+JSON.stringify(a)+"."},Macro.add("progress",{handler:function(){const a=this.args[0];let b=invalidName(a);if(b)return this.err(b);let c=[];setup[a.replace(/^[$_]/,"")]=c;for(let a=1;a<this.args.length;++a)c.push(this.args[a]);setVar(a,c[0])}}),Macro.add("advance",{handler:function(){var a=Math.max;const b=this.args[0];let c=invalidName(b);if(c)return this.err(c);let d=setup[b.replace(/^[$_]/,"")],e=getVar(b),f=QBN.progress(b,e);if("string"==typeof this.args[1])f=a(f,QBN.progress(b,this.args[1]));else{const b=+this.args[1]||1;f=a(0,Math.min(f+b,d.length-1))}setVar(b,d[f])}});