UNPKG

@drincs/pixi-vn-ink

Version:

Pixi'VN gives you the ability to write your own narrative using Ink

3 lines 14.9 kB
import {getCharacterById}from'@drincs/pixi-vn';var T="_|_";var _="___external_value___";function O(n,e){if(n.endsWith(".s"))return;if(new RegExp(/.*\.[0-9]+\..*$/).test(n)&&(n=n.split(".").filter(i=>!i.match(/^[0-9]+$/)).join("."),!n.startsWith(".")))return n.replaceAll(".",T);if(!new RegExp(/^\.\^.*$/).test(n)&&n.includes("g-")){let t=n.split("g-");return j(e)+T+"g-"+t[t.length-1]}let s=0;for(;new RegExp(/^\.\^.*$/).test(n);)s++,n=n.substring(2);if(s=s-1,new RegExp(/^\..*$/).test(n)&&e){let t=n.substring(1);return j(e,s)+T+t.replaceAll(".",T)}return n.replaceAll(".",T)||j(e,s).replaceAll(".",T)}function j(n,e=0){let s=n.split(T);for(;s.length>1&&e>0;)s.pop()?.includes("g-")&&e--,e--;return s.join(T)}var $="\xA7DOUBLE_SLASH_CONVERTOR\xA7";function b(n){return n.startsWith("^")&&(n=n.substring(1)),n=n.replaceAll("\\\\",$),n=n.replaceAll("\\n",` `),n=n.replaceAll("\\t"," "),n=n.replaceAll(" "," "),n=n.replaceAll($,"\\\\"),n}var V;(i=>(i.log=(p,...a)=>console.log(`[Pixi\u2019VN Ink] ${p}`,...a),i.warn=(p,...a)=>console.warn(`[Pixi\u2019VN Ink] ${p}`,...a),i.error=(p,...a)=>console.error(`[Pixi\u2019VN Ink] ${p}`,...a),i.info=(p,...a)=>console.info(`[Pixi\u2019VN Ink] ${p}`,...a)))(V||={});var H=["+","-","/","*","%","_","==",">","<",">=","<=","!=","!","&&","||","MIN","MAX"],W=["+","-","/","*","%","POW","RANDOM"],Y=["INT","FLOOR","FLOAT"],q=["==",">","<",">=","<=","!=","CONTAINS"];function v(n,e,s){n=n.map(i=>{if(typeof i=="string"){if(i==="rnd")return "RANDOM";if(i==="?")return "CONTAINS"}return i});let t=[];return n.forEach(i=>{if(typeof i=="object"&&"CNT?"in i)if(new RegExp(/.*\.[0-9]\..*/).test(i["CNT?"])){let p=i["CNT?"].split("."),a=p.pop(),u=p.pop();if(u===void 0||a===void 0){V.error("Error parsing ink file: Conditional statement is not valid",n);return}let y=parseInt(u),l=p.join(".");if(l.includes("^.")){let g=l.split("."),f=g[g.length-1].replaceAll(".",T);g.pop(),l=g.join(".")+"."+f,a.includes("c-")&&(l=l+T+a);}else l=l.replaceAll(".",T);let r=O(l,e);if(!r)return;t.push({type:"compare",leftValue:X(r),operator:">=",rightValue:{type:"value",value:y}});}else {let p=O(i["CNT?"],e);if(!p)return;t.push(X(p));}else if(typeof i=="object"&&"VAR?"in i)t.push(R(i["VAR?"],s));else if(i==="&&"||i==="||"){if(t.length>=2){let p={type:"union",unionType:i==="&&"?"and":"or",conditions:[t[t.length-2],t[t.length-1]]};t.pop(),t.pop(),t.push(p);}}else if(i==="!")if(t.length===0)V.error("Error parsing ink file: Conditional statement is not valid",n);else {let p={type:"union",unionType:"not",condition:t[t.length-1]};t[t.length-1]=p;}else if(i&&typeof i=="string"&&q.includes(i))if(t.length<2)V.error("Error parsing ink file: Conditional statement is not valid",n);else {let p={type:"compare",operator:i,rightValue:t[t.length-1],leftValue:t[t.length-2]};t.pop(),t.pop(),t.push(p);}else if(i&&typeof i=="string"&&W.includes(i))if(t.length<2)V.error("Error parsing ink file: Conditional statement is not valid",n);else {let p={type:"arithmetic",operator:i,rightValue:t[t.length-1],leftValue:t[t.length-2]};t.pop(),t.pop(),t.push(p);}else if(i&&typeof i=="string"&&Y.includes(i)){let p={type:"arithmeticsingle",operator:i,leftValue:t[t.length-1]};t.pop(),t.push(p);}else i&&typeof i=="string"?i.startsWith("^")&&t.push(b(i)):typeof i=="object"&&"^->"in i?i["^->"].includes("$r")||t.push(i["^->"]):t.push(i);}),t}function X(n){return {type:"value",storageType:"label",storageOperationType:"get",label:n}}function U(n,e,s){return v(n,e,s)}function R(n,e,s="storage"){let t=e.indexOf(n);return t>=0?{type:"value",storageType:"params",storageOperationType:"get",key:t}:{type:"value",storageOperationType:"get",storageType:s,key:n}}function k(n,e,s,t="storage"){let i=e.indexOf(n);return i>=0?{type:"value",storageType:"params",storageOperationType:"set",key:i,value:s}:{type:"value",storageOperationType:"set",storageType:t,key:n,value:s}}function F(n,e){e&&(typeof e=="string"?e.startsWith("^")&&n.push(b(e)):typeof e=="object"&&"type"in e&&e.type!=="value"&&n.push(e));}function L(n,e,s,t,i=true,p=false){return ne(n,e,s,t,i,p)}function ne(n,e,s,t,i,p=false){if(e){if(p)Array.isArray(e)&&e.length>0&&n.push({operations:[{type:"operationtoconvert",values:e}],goNextStep:true});else if(typeof e=="string"&&e.startsWith("^")||e&&typeof e=="object"&&"type"in e&&e.type=="value"&&e.storageOperationType==="get"){if(!i&&n.length>0){let a=n[n.length-1];!a.glueEnabled&&!a.operations&&(a.glueEnabled=true,a.labelToOpen||(a.goNextStep=true)),n[n.length-1]=a;}typeof e=="string"?n.push(te(b(e))):n.push({dialogue:e});}else if(typeof e=="string"){if(e==="end")n.push({end:"game_end"});else if(e==="done")n.push({end:"label_end",goNextStep:true});else if(e=="<>")if(n.length>0){let a=n[n.length-1];a.glueEnabled=true,a.labelToOpen||(a.goNextStep=true),n[n.length-1]=a;}else n.push({glueEnabled:true,goNextStep:false});}else if(typeof e=="object"){if("type"in e&&e.type!=="value"){if(!i&&n.length>0){let a=n[n.length-1];!a.glueEnabled&&!a.operations&&(a.glueEnabled=true,a.labelToOpen||(a.goNextStep=true)),n[n.length-1]=a;}n.push({conditionalStep:e});}else if("->"in e){let a=i?void 0:true;if(!i&&n.length>0){let u=n[n.length-1];u.labelToOpen||(u.goNextStep=true),n[n.length-1]=u;}if(e.params&&e.params.length===0&&(e.params=void 0),e.var)n.push({labelToOpen:{label:R(e["->"],t),type:"jump",params:e.params},glueEnabled:a});else {let u=O(e["->"],s);if(!u)return;n.push({labelToOpen:{label:u,type:"jump",params:e.params},glueEnabled:a});}}"type"in e&&e.type=="value"&&e.storageOperationType==="set"&&(typeof e.value=="string"&&e.value.startsWith("^")&&(e.value=b(e.value)),n.push({goNextStep:true,operations:[e]}));}}}function te(n){let e;if(n.includes(": ")){let s=n.split(": "),t=s[0],i=s.slice(1).join(": ");getCharacterById(t)&&(e=t,n=i);}return e?{dialogue:{character:e,text:n}}:{dialogue:n}}function w(n,e,s,t=true,i=false){return ie(n,e,s,t,i)}function ie(n,e,s,t,i=false){e&&(typeof e=="string"&&e.startsWith("^")||e&&typeof e=="object"&&"type"in e&&e.type=="value"&&e.storageOperationType==="get"?typeof e=="string"?n.push(b(e)):n.push(e):typeof e=="object"&&"type"in e&&n.push(e));}function D(n,e,s){if(n.length===0){V.error("Error parsing ink file: Arithmetic statement is not valid",n);return}let t=v(n,e,s);if(t.length===1){let i=t[0];return i&&typeof i=="object"&&"type"in i?{type:"value",storageType:"logic",storageOperationType:"get",operation:i}:i}}function N(n,e,s,t="",i,p,a=void 0){let u=[],y="sequential",l=false,r=true;n.forEach(x=>{x==="%"&&(y="loop"),x==="seq"&&(y="random"),x==="MIN"&&(l=true);});let g=n[n.length-1];return Object.keys(g).forEach(x=>{let h=g[x];if(Array.isArray(h)&&h.length>3){h=h.slice(1,h.length-2);let S=[];C(h,t,i,S,e,e,s,E,a,true,p),S.length===1?u.push(S[0]):u.push({type:"resulttocombine",combine:"cross",secondConditionalItem:S});}else Array.isArray(h)&&h.length===3&&(r=false);}),y==="sequential"?{type:"stepswitch",elements:u,choiceType:y,end:r?"lastItem":void 0,nestedId:a}:l&&y==="random"?{type:"stepswitch",elements:u,choiceType:"sequentialrandom",end:r?"lastItem":void 0,nestedId:a}:{type:"stepswitch",elements:u,choiceType:y}}function B(n="",e=""){let s=Array.isArray(n)?n:[n],t=Array.isArray(e)?e:[e];return [...new Set([...s,...t])]}function E(n,e,s,t,i){if(n.length>0){let p={};z(n,p,i);for(let[a,u]of Object.entries(p)){let y=s+T+a,l={text:u.text.length===1?u.text[0]:u.text,label:y,props:{},type:"jump",oneTime:u.onetime};u.text.length===0&&(l.onlyHaveNoChoice=true,l.autoSelect=true),l.oneTime===false&&delete l.oneTime;let r=M(l,u.conditions,i,s)||l,g=e[e.length-1];if(typeof g=="object"&&g&&"type"in g&&(g={conditionalStep:g}),e.length>0&&typeof g=="object"&&g&&"choices"in g&&g.choices){let f=g.choices;f&&Array.isArray(f)?f.push(r):V.error("Unhandled case: choices is PixiVNJsonConditionalStatements<PixiVNJsonChoices> | undefined",u,f),g.choices=f.sort((x,h)=>{try{let S=x.label.split("."),m=S[S.length-1].replaceAll(".",T),J=h.label.split("."),o=J[J.length-1].replaceAll(".",T);if(m.includes("c-")&&o.includes("c-")){let P=m.split("c-")[1],c=parseInt(P),d=o.split("c-")[1],A=parseInt(d);return c-A}}catch{}return 0});}else e.push({choices:[r]});u.preDialog&&(t.preDialog[y]={...u.preDialog});}}}function z(n,e,s,t){let i=[],p="",a="",u=false,y=[];for(let l=0;l<n.length;l++){let r=n[l];if(typeof r=="string")r.startsWith("^")?i.push(b(r)):H.includes(r)&&y.push(r);else if(Array.isArray(r)&&r.includes("visit")){let g=N(r,F,(f,x,h)=>{},t,{preDialog:{}},s);i.push(g);}else if(r&&typeof r=="object")if("*"in r&&typeof r["*"]&&typeof r["*"]=="string")r["*"].includes("c")&&(p="c"+r["*"].split("c")[1],r.flg&16&&(u=true));else if("s"in r&&r.s instanceof Array){let g=Q(r.s),f=r.s.includes("<>");g&&(t&&e[t]?(e[t].preDialog={text:g,glue:f},e[t].text=B(g,e[t].text)):(i.push(g),a=g));}else "CNT?"in r&&y.push(r);else y.push(r);if(p){e[p]?e[p].text=B(i,e[p].text):e[p]={text:i,onetime:u,conditions:y},a&&(e[p].preDialog={text:a,glue:false});let g=n.slice(l+1);z(g,e,s,p);return}}}function Q(n){for(let e of n)if(typeof e=="string"){if(e.startsWith("^"))return b(e)}else if(e instanceof Array){let s=Q(e);if(s)return s}}function M(n,e,s,t,i){if(e.length===0)return;let p=v(e,t,s);if(p.length===1){let a={type:"ifelse",condition:p[0],then:n,else:i};return a.else||delete a.else,a}else if(p.length>1){let a={type:"ifelse",condition:{type:"union",unionType:"and",conditions:p},then:n,else:i};return a.else||delete a.else,a}}function Z(n,e,s,t,i,p,a=void 0){if(n.length===0){V.error("Error parsing ink file: Conditional statement is not valid",n);return}let u=[],y=[];if(n.forEach(g=>{Array.isArray(g)?y.push(g):typeof g=="string"&&g==="du"?i.du?u.push(i.du):i.du=u[u.length-1]:(typeof g!="string"||g!=="/ev")&&u.push(g);}),y.length===0){V.error("Error parsing ink file: Conditional statement is not valid",y);return}let l=G(y[0],e,s,t+T+"then",i,p,`${a||""}then`),r;return y.length===2?r=G(y[1],e,s,t+T+"else",i,p,`${a||""}else`):y.length>2&&(y.shift(),y.push("nop"),y=[{b:y}],r=G(y,e,s,t+T+"else",i,p,`${a||""}else`)),i.du=void 0,M(l,u,p,t,r)}function G(n,e,s,t,i,p,a=void 0){let u=[];for(let l of n)typeof l=="object"&&"b"in l&&(l.b=l.b.filter(r=>r!==null&&!(typeof r=="object"&&"->"in r&&new RegExp(/.*\.[0-9]/).test(r["->"]))),C(l.b,t,i,u,e,e,s,E,a,true,p));return u.length===1?u[0]:{type:"resulttocombine",combine:"cross",secondConditionalItem:u}}function C(n,e,s,t=[],i,p,a,u,y=void 0,l=true,r=[]){let g=false,f=[],x=false,h=false,S=[],m=[];if(s.preDialog[e]&&(l=false,i(t,"^"+s.preDialog[e].text,e,r,l,h),s.preDialog[e].glue&&i(t,"<>",e,r,l,h),delete s.preDialog[e]),n.includes("visit")){let o=N(n,p,a,e,s,r,y);o&&(!l&&t.length>0&&i(t,"<>",e,r,l,h),i(t,o,e,r,l,h));return}let J=n[0];if(J&&typeof J=="object"&&"temp="in J){for(;n[0]&&typeof n[0]=="object"&&"temp="in n[0];)r.push(n[0]["temp="]),n.shift();r=r.reverse();}if(n.forEach((o,P)=>{if(h){if(typeof o=="string"&&o=="/#"){let c=[];C(m,e,s,c,w,w,a,u,y,l),i(t,c,e,r,l,h),h=false,m=[];}else m.push(o);return}else if(g)if(Array.isArray(o))f.push(o);else if(o&&typeof o=="object")if("CNT?"in o)P>0&&n[P-1]=="ev"?(x=true,S.push(o)):x?S.push(o):(f.push(o),l=false);else if("VAR="in o||"temp="in o){let c=k("VAR="in o?o["VAR="]:o["temp="],r,n[P-1],"VAR="in o?"storage":"tempstorage");if(c.value&&typeof c.value=="string"&&c.value=="/str"&&(c.value=n[P-2]),c.value&&typeof c.value=="object"&&"^->"in c.value&&(c.value=c.value["^->"]),f.length>1){let d=D(f,e,r);f=[],d&&typeof d=="object"&&"type"in d&&d.type=="value"&&"storageType"in d&&d.storageType=="logic"&&(c.value=d.operation);}(typeof c.key!="string"||!c.key.includes("$r"))&&(i(t,c,e,r,l,h),l=true);}else "VAR?"in o?f.push(o):"^->"in o&&(o["^->"].includes("$r")||f.push(o));else if(typeof o=="string"&&o=="/ev")x&&S.push(o),g=false,f.push(o);else if(typeof o=="string"&&o=="out"){if(f.length>0){let c=f[f.length-1];if(c&&typeof c=="object"&&"VAR?"in c){f.pop();let d=R(c["VAR?"],r,"storage");i(t,d,e,r,l,h);}else {let d=[];for(;f.length>0&&f[f.length-1]!="/ev";)d.push(f.pop());d=d.reverse();let A=D(d,e,r);f=[],A&&typeof A=="object"&&"type"in A&&A.type=="value"&&"storageType"in A&&A.storageType=="logic"?i(t,{storageOperationType:"get",storageType:"logic",operation:A.operation,type:"value"},e,r,l,h):(i(t,"<>",e,r,l,h),A=`^${A}`,i(t,A,e,r,l,h));}l=false;}}else f.push(o);else if(typeof o=="string")if(o.startsWith("^"))i(t,o,e,r,l,h),l=false;else if(o=="ev")g=true;else if(o==` `)l=true;else if(o=="done"||o=="end")i(t,o,e,r,l,h),l=false;else if(o=="<>")i(t,o,e,r,l,h),l=false;else if(o=="nop"&&x){let c=Z(S,p,a,e,s,r,y);c&&i(t,c,e,r,l,h),x=false,S=[];}else o=="#"&&(h=true);else if(o instanceof Array)if(x)S.push(o);else if(o.length>1&&typeof o[o.length-2]=="object"&&o[o.length-2]&&"c"in o[o.length-2]&&typeof o[o.length-1]=="object"&&o[o.length-1]&&"b"in o[o.length-1]){f.pop();let c=[],d=[];for(;f.length>0&&f[f.length-1]!="/ev";)c.push(f.pop());S=[...S,...c.reverse()],x=true,d.push(o.pop()),d.push(o.pop()),S=[...S,...o],S.push(d);}else if(o.length>1&&typeof o[o.length-1]=="object"&&o[o.length-1]&&"#n"in o[o.length-1]){let c=o.pop();if(!c){V.error("Error parsing ink file: el is undefined");return}let d=c["#n"];delete c["#n"],o.push(c),i(t,{"->":e?e+T+d:d},e,r,l,h),a({[d]:o},e,s);}else C(o,e,s,t,i,p,a,u,y,l);else if(o&&typeof o=="object")if("->"in o&&typeof o["->"]=="string"&&!new RegExp(/^\.\^\.\^\.\d\.s$/).test(o["->"])){let c=[];f.length>0&&(c=U(["ev",...f],e,r)),o.params=c,i(t,o,e,r,l,h),l=false;}else if("*"in o&&typeof o["*"]=="string")o["*"].includes("c")&&(f.push(o),l=false);else if("s"in o&&o.s instanceof Array)f.push(o),l=false;else if("CNT?"in o)f.push(o),l=false;else if("VAR="in o||"temp="in o){let c=[],d=k("VAR="in o?o["VAR="]:o["temp="],r,void 0,"VAR="in o?"storage":"tempstorage");if(d.key!=="$r"){for(f.pop(),f[f.length-1]=="/ev"&&f.pop();f.length>0&&f[f.length-1]!="/ev";)c.push(f.pop());c=c.reverse(),d.value=D(c,e,r),f=[],(d.value!==void 0||d.value!==null)&&i(t,d,e,r,l,h),l=false;}}else a(o,e,s);}),u(f,t,e,s,r),e.includes(T)&&t.length==2){let o=t[0],P=t[1];o&&P&&typeof o=="object"&&"dialogue"in o&&typeof P=="object"&&"labelToOpen"in P&&o.dialogue==" "&&P.labelToOpen&&(t.shift(),delete P.glueEnabled,t[0]=P);}}function dn(n){try{let e={};return I(n,e),e}catch(e){V.error("Error parsing ink file",e);}}function I(n,e,s={externalSwitch:void 0}){for(let t of n)if(t)if(t instanceof Array)if(t.includes("visit")){let i=N(t,F,(p,a,u)=>{},"",{preDialog:{}},[]);i&&(s.externalSwitch=i);}else I(t,e,s);else typeof t=="object"&&(t&&"VAR="in t&&s.externalSwitch&&(e[_]||(e[_]=[]),e[_].push({operations:[{type:"value",value:s.externalSwitch,key:t["VAR="],storageType:"storage",storageOperationType:"set"}]})),K(t,e));}function K(n,e,s="",t={preDialog:{}}){if(n!==null){for(let[i,p]of Object.entries(n))if(p instanceof Array){let a=[],u={},y=(s?s+T:"")+i;C(p,y,t,a,L,L,(l,r,g)=>{K(l,u,r,g);},E);for(let[l,r]of Object.entries(u))e[l]=r;a.length>0&&(e[y]=a);}}}export{K as addLabels,dn as getInkLabels};