/* 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 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 . 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;0d?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;ja},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)},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 0c){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>: "+(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&&f>"):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>"):a.wiki(d),e&&g>"):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("<>: failed to set hand \""+d+"\".")}var e,f={};for(e=0;e>: "+("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;bc.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> 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