wasm4
Version:
Build retro games using WebAssembly for a fantasy console.
50 lines • 133 kB
JavaScript
var wasm4=function(t){"use strict";
/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/var e,s;const i=globalThis,r=i.ShadowRoot&&(void 0===i.ShadyCSS||i.ShadyCSS.nativeShadow)&&"adoptedStyleSheets"in Document.prototype&&"replace"in CSSStyleSheet.prototype,n=Symbol(),o=new WeakMap;let a=class{constructor(t,e,s){if(this._$cssResult$=!0,s!==n)throw Error("CSSResult is not constructable. Use `unsafeCSS` or `css` instead.");this.cssText=t,this.t=e}get styleSheet(){let t=this.o;const e=this.t;if(r&&void 0===t){const s=void 0!==e&&1===e.length;s&&(t=o.get(e)),void 0===t&&((this.o=t=new CSSStyleSheet).replaceSync(this.cssText),s&&o.set(e,t))}return t}toString(){return this.cssText}};const l=(t,...e)=>{const s=1===t.length?t[0]:e.reduce(((e,s,i)=>e+(t=>{if(!0===t._$cssResult$)return t.cssText;if("number"==typeof t)return t;throw Error("Value passed to 'css' function must be a 'css' function result: "+t+". Use 'unsafeCSS' to pass non-literal values, but take care to ensure page security.")})(s)+t[i+1]),t[0]);return new a(s,t,n)},h=r?t=>t:t=>t instanceof CSSStyleSheet?(t=>{let e="";for(const s of t.cssRules)e+=s.cssText;return(t=>new a("string"==typeof t?t:t+"",void 0,n))(e)})(t):t
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/,{is:d,defineProperty:c,getOwnPropertyDescriptor:p,getOwnPropertyNames:u,getOwnPropertySymbols:f,getPrototypeOf:m}=Object,g=globalThis,y=g.trustedTypes,b=y?y.emptyScript:"",v=g.reactiveElementPolyfillSupport,w=(t,e)=>t,x={toAttribute(t,e){switch(e){case Boolean:t=t?b:null;break;case Object:case Array:t=null==t?t:JSON.stringify(t)}return t},fromAttribute(t,e){let s=t;switch(e){case Boolean:s=null!==t;break;case Number:s=null===t?null:Number(t);break;case Object:case Array:try{s=JSON.parse(t)}catch(i){s=null}}return s}},_=(t,e)=>!d(t,e),$={attribute:!0,type:String,converter:x,reflect:!1,hasChanged:_};Symbol.metadata??(Symbol.metadata=Symbol("metadata")),g.litPropertyMetadata??(g.litPropertyMetadata=new WeakMap);let A=class extends HTMLElement{static addInitializer(t){this._$Ei(),(this.l??(this.l=[])).push(t)}static get observedAttributes(){return this.finalize(),this._$Eh&&[...this._$Eh.keys()]}static createProperty(t,e=$){if(e.state&&(e.attribute=!1),this._$Ei(),this.elementProperties.set(t,e),!e.noAccessor){const s=Symbol(),i=this.getPropertyDescriptor(t,s,e);void 0!==i&&c(this.prototype,t,i)}}static getPropertyDescriptor(t,e,s){const{get:i,set:r}=p(this.prototype,t)??{get(){return this[e]},set(t){this[e]=t}};return{get(){return null==i?void 0:i.call(this)},set(e){const n=null==i?void 0:i.call(this);r.call(this,e),this.requestUpdate(t,n,s)},configurable:!0,enumerable:!0}}static getPropertyOptions(t){return this.elementProperties.get(t)??$}static _$Ei(){if(this.hasOwnProperty(w("elementProperties")))return;const t=m(this);t.finalize(),void 0!==t.l&&(this.l=[...t.l]),this.elementProperties=new Map(t.elementProperties)}static finalize(){if(this.hasOwnProperty(w("finalized")))return;if(this.finalized=!0,this._$Ei(),this.hasOwnProperty(w("properties"))){const t=this.properties,e=[...u(t),...f(t)];for(const s of e)this.createProperty(s,t[s])}const t=this[Symbol.metadata];if(null!==t){const e=litPropertyMetadata.get(t);if(void 0!==e)for(const[t,s]of e)this.elementProperties.set(t,s)}this._$Eh=new Map;for(const[e,s]of this.elementProperties){const t=this._$Eu(e,s);void 0!==t&&this._$Eh.set(t,e)}this.elementStyles=this.finalizeStyles(this.styles)}static finalizeStyles(t){const e=[];if(Array.isArray(t)){const s=new Set(t.flat(1/0).reverse());for(const t of s)e.unshift(h(t))}else void 0!==t&&e.push(h(t));return e}static _$Eu(t,e){const s=e.attribute;return!1===s?void 0:"string"==typeof s?s:"string"==typeof t?t.toLowerCase():void 0}constructor(){super(),this._$Ep=void 0,this.isUpdatePending=!1,this.hasUpdated=!1,this._$Em=null,this._$Ev()}_$Ev(){var t;this._$ES=new Promise((t=>this.enableUpdating=t)),this._$AL=new Map,this._$E_(),this.requestUpdate(),null==(t=this.constructor.l)||t.forEach((t=>t(this)))}addController(t){var e;(this._$EO??(this._$EO=new Set)).add(t),void 0!==this.renderRoot&&this.isConnected&&(null==(e=t.hostConnected)||e.call(t))}removeController(t){var e;null==(e=this._$EO)||e.delete(t)}_$E_(){const t=new Map,e=this.constructor.elementProperties;for(const s of e.keys())this.hasOwnProperty(s)&&(t.set(s,this[s]),delete this[s]);t.size>0&&(this._$Ep=t)}createRenderRoot(){const t=this.shadowRoot??this.attachShadow(this.constructor.shadowRootOptions);return((t,e)=>{if(r)t.adoptedStyleSheets=e.map((t=>t instanceof CSSStyleSheet?t:t.styleSheet));else for(const s of e){const e=document.createElement("style"),r=i.litNonce;void 0!==r&&e.setAttribute("nonce",r),e.textContent=s.cssText,t.appendChild(e)}})(t,this.constructor.elementStyles),t}connectedCallback(){var t;this.renderRoot??(this.renderRoot=this.createRenderRoot()),this.enableUpdating(!0),null==(t=this._$EO)||t.forEach((t=>{var e;return null==(e=t.hostConnected)?void 0:e.call(t)}))}enableUpdating(t){}disconnectedCallback(){var t;null==(t=this._$EO)||t.forEach((t=>{var e;return null==(e=t.hostDisconnected)?void 0:e.call(t)}))}attributeChangedCallback(t,e,s){this._$AK(t,s)}_$EC(t,e){var s;const i=this.constructor.elementProperties.get(t),r=this.constructor._$Eu(t,i);if(void 0!==r&&!0===i.reflect){const n=(void 0!==(null==(s=i.converter)?void 0:s.toAttribute)?i.converter:x).toAttribute(e,i.type);this._$Em=t,null==n?this.removeAttribute(r):this.setAttribute(r,n),this._$Em=null}}_$AK(t,e){var s;const i=this.constructor,r=i._$Eh.get(t);if(void 0!==r&&this._$Em!==r){const t=i.getPropertyOptions(r),n="function"==typeof t.converter?{fromAttribute:t.converter}:void 0!==(null==(s=t.converter)?void 0:s.fromAttribute)?t.converter:x;this._$Em=r,this[r]=n.fromAttribute(e,t.type),this._$Em=null}}requestUpdate(t,e,s){if(void 0!==t){if(s??(s=this.constructor.getPropertyOptions(t)),!(s.hasChanged??_)(this[t],e))return;this.P(t,e,s)}!1===this.isUpdatePending&&(this._$ES=this._$ET())}P(t,e,s){this._$AL.has(t)||this._$AL.set(t,e),!0===s.reflect&&this._$Em!==t&&(this._$Ej??(this._$Ej=new Set)).add(t)}async _$ET(){this.isUpdatePending=!0;try{await this._$ES}catch(e){Promise.reject(e)}const t=this.scheduleUpdate();return null!=t&&await t,!this.isUpdatePending}scheduleUpdate(){return this.performUpdate()}performUpdate(){var t;if(!this.isUpdatePending)return;if(!this.hasUpdated){if(this.renderRoot??(this.renderRoot=this.createRenderRoot()),this._$Ep){for(const[t,e]of this._$Ep)this[t]=e;this._$Ep=void 0}const t=this.constructor.elementProperties;if(t.size>0)for(const[e,s]of t)!0!==s.wrapped||this._$AL.has(e)||void 0===this[e]||this.P(e,this[e],s)}let e=!1;const s=this._$AL;try{e=this.shouldUpdate(s),e?(this.willUpdate(s),null==(t=this._$EO)||t.forEach((t=>{var e;return null==(e=t.hostUpdate)?void 0:e.call(t)})),this.update(s)):this._$EU()}catch(i){throw e=!1,this._$EU(),i}e&&this._$AE(s)}willUpdate(t){}_$AE(t){var e;null==(e=this._$EO)||e.forEach((t=>{var e;return null==(e=t.hostUpdated)?void 0:e.call(t)})),this.hasUpdated||(this.hasUpdated=!0,this.firstUpdated(t)),this.updated(t)}_$EU(){this._$AL=new Map,this.isUpdatePending=!1}get updateComplete(){return this.getUpdateComplete()}getUpdateComplete(){return this._$ES}shouldUpdate(t){return!0}update(t){this._$Ej&&(this._$Ej=this._$Ej.forEach((t=>this._$EC(t,this[t])))),this._$EU()}updated(t){}firstUpdated(t){}};A.elementStyles=[],A.shadowRootOptions={mode:"open"},A[w("elementProperties")]=new Map,A[w("finalized")]=new Map,null==v||v({ReactiveElement:A}),(g.reactiveElementVersions??(g.reactiveElementVersions=[])).push("2.0.4");
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
const E=globalThis,S=E.trustedTypes,k=S?S.createPolicy("lit-html",{createHTML:t=>t}):void 0,C="$lit$",P=`lit$${(Math.random()+"").slice(9)}$`,U="?"+P,T=`<${U}>`,M=document,I=()=>M.createComment(""),O=t=>null===t||"object"!=typeof t&&"function"!=typeof t,R=Array.isArray,L="[ \t\n\f\r]",D=/<(?:(!--|\/[^a-zA-Z])|(\/?[a-zA-Z][^>\s]*)|(\/?$))/g,B=/-->/g,N=/>/g,z=RegExp(`>|${L}(?:([^\\s"'>=/]+)(${L}*=${L}*(?:[^ \t\n\f\r"'\`<>=]|("|')|))|$)`,"g"),j=/'/g,H=/"/g,F=/^(?:script|style|textarea|title)$/i,V=(K=1,(t,...e)=>({_$litType$:K,strings:t,values:e})),W=Symbol.for("lit-noChange"),G=Symbol.for("lit-nothing"),Y=new WeakMap,q=M.createTreeWalker(M,129);var K;function J(t,e){if(!Array.isArray(t)||!t.hasOwnProperty("raw"))throw Error("invalid template strings array");return void 0!==k?k.createHTML(e):e}let X=class t{constructor({strings:e,_$litType$:s},i){let r;this.parts=[];let n=0,o=0;const a=e.length-1,l=this.parts,[h,d]=((t,e)=>{const s=t.length-1,i=[];let r,n=2===e?"<svg>":"",o=D;for(let a=0;a<s;a++){const e=t[a];let s,l,h=-1,d=0;for(;d<e.length&&(o.lastIndex=d,l=o.exec(e),null!==l);)d=o.lastIndex,o===D?"!--"===l[1]?o=B:void 0!==l[1]?o=N:void 0!==l[2]?(F.test(l[2])&&(r=RegExp("</"+l[2],"g")),o=z):void 0!==l[3]&&(o=z):o===z?">"===l[0]?(o=r??D,h=-1):void 0===l[1]?h=-2:(h=o.lastIndex-l[2].length,s=l[1],o=void 0===l[3]?z:'"'===l[3]?H:j):o===H||o===j?o=z:o===B||o===N?o=D:(o=z,r=void 0);const c=o===z&&t[a+1].startsWith("/>")?" ":"";n+=o===D?e+T:h>=0?(i.push(s),e.slice(0,h)+C+e.slice(h)+P+c):e+P+(-2===h?a:c)}return[J(t,n+(t[s]||"<?>")+(2===e?"</svg>":"")),i]})(e,s);if(this.el=t.createElement(h,i),q.currentNode=this.el.content,2===s){const t=this.el.content.firstChild;t.replaceWith(...t.childNodes)}for(;null!==(r=q.nextNode())&&l.length<a;){if(1===r.nodeType){if(r.hasAttributes())for(const t of r.getAttributeNames())if(t.endsWith(C)){const e=d[o++],s=r.getAttribute(t).split(P),i=/([.?@])?(.*)/.exec(e);l.push({type:1,index:n,name:i[2],strings:s,ctor:"."===i[1]?st:"?"===i[1]?it:"@"===i[1]?rt:et}),r.removeAttribute(t)}else t.startsWith(P)&&(l.push({type:6,index:n}),r.removeAttribute(t));if(F.test(r.tagName)){const t=r.textContent.split(P),e=t.length-1;if(e>0){r.textContent=S?S.emptyScript:"";for(let s=0;s<e;s++)r.append(t[s],I()),q.nextNode(),l.push({type:2,index:++n});r.append(t[e],I())}}}else if(8===r.nodeType)if(r.data===U)l.push({type:2,index:n});else{let t=-1;for(;-1!==(t=r.data.indexOf(P,t+1));)l.push({type:7,index:n}),t+=P.length-1}n++}}static createElement(t,e){const s=M.createElement("template");return s.innerHTML=t,s}};function Z(t,e,s=t,i){var r,n;if(e===W)return e;let o=void 0!==i?null==(r=s._$Co)?void 0:r[i]:s._$Cl;const a=O(e)?void 0:e._$litDirective$;return(null==o?void 0:o.constructor)!==a&&(null==(n=null==o?void 0:o._$AO)||n.call(o,!1),void 0===a?o=void 0:(o=new a(t),o._$AT(t,s,i)),void 0!==i?(s._$Co??(s._$Co=[]))[i]=o:s._$Cl=o),void 0!==o&&(e=Z(t,o._$AS(t,e.values),o,i)),e}let Q=class{constructor(t,e){this._$AV=[],this._$AN=void 0,this._$AD=t,this._$AM=e}get parentNode(){return this._$AM.parentNode}get _$AU(){return this._$AM._$AU}u(t){const{el:{content:e},parts:s}=this._$AD,i=((null==t?void 0:t.creationScope)??M).importNode(e,!0);q.currentNode=i;let r=q.nextNode(),n=0,o=0,a=s[0];for(;void 0!==a;){if(n===a.index){let e;2===a.type?e=new tt(r,r.nextSibling,this,t):1===a.type?e=new a.ctor(r,a.name,a.strings,this,t):6===a.type&&(e=new nt(r,this,t)),this._$AV.push(e),a=s[++o]}n!==(null==a?void 0:a.index)&&(r=q.nextNode(),n++)}return q.currentNode=M,i}p(t){let e=0;for(const s of this._$AV)void 0!==s&&(void 0!==s.strings?(s._$AI(t,s,e),e+=s.strings.length-2):s._$AI(t[e])),e++}},tt=class t{get _$AU(){var t;return(null==(t=this._$AM)?void 0:t._$AU)??this._$Cv}constructor(t,e,s,i){this.type=2,this._$AH=G,this._$AN=void 0,this._$AA=t,this._$AB=e,this._$AM=s,this.options=i,this._$Cv=(null==i?void 0:i.isConnected)??!0}get parentNode(){let t=this._$AA.parentNode;const e=this._$AM;return void 0!==e&&11===(null==t?void 0:t.nodeType)&&(t=e.parentNode),t}get startNode(){return this._$AA}get endNode(){return this._$AB}_$AI(t,e=this){t=Z(this,t,e),O(t)?t===G||null==t||""===t?(this._$AH!==G&&this._$AR(),this._$AH=G):t!==this._$AH&&t!==W&&this._(t):void 0!==t._$litType$?this.$(t):void 0!==t.nodeType?this.T(t):(t=>R(t)||"function"==typeof(null==t?void 0:t[Symbol.iterator]))(t)?this.k(t):this._(t)}S(t){return this._$AA.parentNode.insertBefore(t,this._$AB)}T(t){this._$AH!==t&&(this._$AR(),this._$AH=this.S(t))}_(t){this._$AH!==G&&O(this._$AH)?this._$AA.nextSibling.data=t:this.T(M.createTextNode(t)),this._$AH=t}$(t){var e;const{values:s,_$litType$:i}=t,r="number"==typeof i?this._$AC(t):(void 0===i.el&&(i.el=X.createElement(J(i.h,i.h[0]),this.options)),i);if((null==(e=this._$AH)?void 0:e._$AD)===r)this._$AH.p(s);else{const t=new Q(r,this),e=t.u(this.options);t.p(s),this.T(e),this._$AH=t}}_$AC(t){let e=Y.get(t.strings);return void 0===e&&Y.set(t.strings,e=new X(t)),e}k(e){R(this._$AH)||(this._$AH=[],this._$AR());const s=this._$AH;let i,r=0;for(const n of e)r===s.length?s.push(i=new t(this.S(I()),this.S(I()),this,this.options)):i=s[r],i._$AI(n),r++;r<s.length&&(this._$AR(i&&i._$AB.nextSibling,r),s.length=r)}_$AR(t=this._$AA.nextSibling,e){var s;for(null==(s=this._$AP)||s.call(this,!1,!0,e);t&&t!==this._$AB;){const e=t.nextSibling;t.remove(),t=e}}setConnected(t){var e;void 0===this._$AM&&(this._$Cv=t,null==(e=this._$AP)||e.call(this,t))}},et=class{get tagName(){return this.element.tagName}get _$AU(){return this._$AM._$AU}constructor(t,e,s,i,r){this.type=1,this._$AH=G,this._$AN=void 0,this.element=t,this.name=e,this._$AM=i,this.options=r,s.length>2||""!==s[0]||""!==s[1]?(this._$AH=Array(s.length-1).fill(new String),this.strings=s):this._$AH=G}_$AI(t,e=this,s,i){const r=this.strings;let n=!1;if(void 0===r)t=Z(this,t,e,0),n=!O(t)||t!==this._$AH&&t!==W,n&&(this._$AH=t);else{const i=t;let o,a;for(t=r[0],o=0;o<r.length-1;o++)a=Z(this,i[s+o],e,o),a===W&&(a=this._$AH[o]),n||(n=!O(a)||a!==this._$AH[o]),a===G?t=G:t!==G&&(t+=(a??"")+r[o+1]),this._$AH[o]=a}n&&!i&&this.j(t)}j(t){t===G?this.element.removeAttribute(this.name):this.element.setAttribute(this.name,t??"")}},st=class extends et{constructor(){super(...arguments),this.type=3}j(t){this.element[this.name]=t===G?void 0:t}},it=class extends et{constructor(){super(...arguments),this.type=4}j(t){this.element.toggleAttribute(this.name,!!t&&t!==G)}},rt=class extends et{constructor(t,e,s,i,r){super(t,e,s,i,r),this.type=5}_$AI(t,e=this){if((t=Z(this,t,e,0)??G)===W)return;const s=this._$AH,i=t===G&&s!==G||t.capture!==s.capture||t.once!==s.once||t.passive!==s.passive,r=t!==G&&(s===G||i);i&&this.element.removeEventListener(this.name,this,s),r&&this.element.addEventListener(this.name,this,t),this._$AH=t}handleEvent(t){var e;"function"==typeof this._$AH?this._$AH.call((null==(e=this.options)?void 0:e.host)??this.element,t):this._$AH.handleEvent(t)}},nt=class{constructor(t,e,s){this.element=t,this.type=6,this._$AN=void 0,this._$AM=e,this.options=s}get _$AU(){return this._$AM._$AU}_$AI(t){Z(this,t)}};const ot=E.litHtmlPolyfillSupport;null==ot||ot(X,tt),(E.litHtmlVersions??(E.litHtmlVersions=[])).push("3.1.2");
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
let at=class extends A{constructor(){super(...arguments),this.renderOptions={host:this},this._$Do=void 0}createRenderRoot(){var t;const e=super.createRenderRoot();return(t=this.renderOptions).renderBefore??(t.renderBefore=e.firstChild),e}update(t){const e=this.render();this.hasUpdated||(this.renderOptions.isConnected=this.isConnected),super.update(t),this._$Do=((t,e,s)=>{const i=(null==s?void 0:s.renderBefore)??e;let r=i._$litPart$;if(void 0===r){const t=(null==s?void 0:s.renderBefore)??null;i._$litPart$=r=new tt(e.insertBefore(I(),t),t,void 0,s??{})}return r._$AI(t),r})(e,this.renderRoot,this.renderOptions)}connectedCallback(){var t;super.connectedCallback(),null==(t=this._$Do)||t.setConnected(!0)}disconnectedCallback(){var t;super.disconnectedCallback(),null==(t=this._$Do)||t.setConnected(!1)}render(){return W}};at._$litElement$=!0,at.finalized=!0,null==(e=globalThis.litElementHydrateSupport)||e.call(globalThis,{LitElement:at});const lt=globalThis.litElementPolyfillSupport;null==lt||lt({LitElement:at}),(globalThis.litElementVersions??(globalThis.litElementVersions=[])).push("4.0.4");
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
const ht=t=>(e,s)=>{void 0!==s?s.addInitializer((()=>{customElements.define(t,e)})):customElements.define(t,e)}
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/,dt={attribute:!0,type:String,converter:x,reflect:!1,hasChanged:_},ct=(t=dt,e,s)=>{const{kind:i,metadata:r}=s;let n=globalThis.litPropertyMetadata.get(r);if(void 0===n&&globalThis.litPropertyMetadata.set(r,n=new Map),n.set(s.name,t),"accessor"===i){const{name:i}=s;return{set(s){const r=e.get.call(this);e.set.call(this,s),this.requestUpdate(i,r,t)},init(e){return void 0!==e&&this.P(i,void 0,t),e}}}if("setter"===i){const{name:i}=s;return function(s){const r=this[i];e.call(this,s),this.requestUpdate(i,r,t)}}throw Error("Unsupported decorator location: "+i)};
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
function pt(t){return function(t){return(e,s)=>"object"==typeof s?ct(t,e,s):((t,e,s)=>{const i=e.hasOwnProperty(s);return e.constructor.createProperty(s,i?{...t,wrapped:!0}:t),i?Object.getOwnPropertyDescriptor(e,s):void 0})(t,e,s)}({...t,state:!0,attribute:!1})}
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/const ut=(t,e,s)=>(s.configurable=!0,s.enumerable=!0,Reflect.decorate&&"object"!=typeof e&&Object.defineProperty(t,e,s),s)
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/;function ft(t,e){return(s,i,r)=>{const n=e=>{var s;return(null==(s=e.renderRoot)?void 0:s.querySelector(t))??null};if(e){const{get:t,set:e}="object"==typeof i?s:r??(()=>{const t=Symbol();return{get(){return this[t]},set(e){this[t]=e}}})();return ut(s,i,{get(){let s=t.call(this);return void 0===s&&(s=n(this),(null!==s||this.hasUpdated)&&e.call(this,s)),s}})}return ut(s,i,{get(){return n(this)}})}}const mt=160,gt=160,yt=1024,bt=[14743759,8831084,3172432,464929],vt=16,wt=32,xt=64,_t=128,$t=Uint8Array.of(255,255,255,255,255,255,255,255,199,199,199,207,207,255,207,255,147,147,147,255,255,255,255,255,147,1,147,147,147,1,147,255,239,131,47,131,233,3,239,255,157,91,55,239,217,181,115,255,143,39,39,143,37,51,129,255,207,207,207,255,255,255,255,255,243,231,207,207,207,231,243,255,159,207,231,231,231,207,159,255,255,147,199,1,199,147,255,255,255,231,231,129,231,231,255,255,255,255,255,255,255,207,207,159,255,255,255,129,255,255,255,255,255,255,255,255,255,207,207,255,253,251,247,239,223,191,127,255,199,179,57,57,57,155,199,255,231,199,231,231,231,231,129,255,131,57,241,195,135,31,1,255,129,243,231,195,249,57,131,255,227,195,147,51,1,243,243,255,3,63,3,249,249,57,131,255,195,159,63,3,57,57,131,255,1,57,243,231,207,207,207,255,135,59,27,135,97,121,131,255,131,57,57,129,249,243,135,255,255,207,207,255,207,207,255,255,255,207,207,255,207,207,159,255,243,231,207,159,207,231,243,255,255,255,1,255,1,255,255,255,159,207,231,243,231,207,159,255,131,1,57,243,199,255,199,255,131,125,69,85,65,127,131,255,199,147,57,57,1,57,57,255,3,57,57,3,57,57,3,255,195,153,63,63,63,153,195,255,7,51,57,57,57,51,7,255,1,63,63,3,63,63,1,255,1,63,63,3,63,63,63,255,193,159,63,49,57,153,193,255,57,57,57,1,57,57,57,255,129,231,231,231,231,231,129,255,249,249,249,249,249,57,131,255,57,51,39,15,7,35,49,255,159,159,159,159,159,159,129,255,57,17,1,1,41,57,57,255,57,25,9,1,33,49,57,255,131,57,57,57,57,57,131,255,3,57,57,57,3,63,63,255,131,57,57,57,33,51,133,255,3,57,57,49,7,35,49,255,135,51,63,131,249,57,131,255,129,231,231,231,231,231,231,255,57,57,57,57,57,57,131,255,57,57,57,17,131,199,239,255,57,57,41,1,1,17,57,255,57,17,131,199,131,17,57,255,153,153,153,195,231,231,231,255,1,241,227,199,143,31,1,255,195,207,207,207,207,207,195,255,127,191,223,239,247,251,253,255,135,231,231,231,231,231,135,255,199,147,255,255,255,255,255,255,255,255,255,255,255,255,255,1,239,247,255,255,255,255,255,255,255,255,131,249,129,57,129,255,63,63,3,57,57,57,131,255,255,255,129,63,63,63,129,255,249,249,129,57,57,57,129,255,255,255,131,57,1,63,131,255,241,231,129,231,231,231,231,255,255,255,129,57,57,129,249,131,63,63,3,57,57,57,57,255,231,255,199,231,231,231,129,255,243,255,227,243,243,243,243,135,63,63,49,3,7,35,49,255,199,231,231,231,231,231,129,255,255,255,3,73,73,73,73,255,255,255,3,57,57,57,57,255,255,255,131,57,57,57,131,255,255,255,3,57,57,3,63,63,255,255,129,57,57,129,249,249,255,255,145,143,159,159,159,255,255,255,131,63,131,249,3,255,231,231,129,231,231,231,231,255,255,255,57,57,57,57,129,255,255,255,153,153,153,195,231,255,255,255,73,73,73,73,129,255,255,255,57,1,199,1,57,255,255,255,57,57,57,129,249,131,255,255,1,227,199,143,1,255,243,231,231,207,231,231,243,255,231,231,231,231,231,231,231,255,159,207,207,231,207,207,159,255,255,255,143,69,227,255,255,255,255,255,255,255,255,147,147,255,131,41,41,17,41,41,131,255,131,57,9,17,33,57,131,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,131,17,33,125,33,17,131,255,131,17,9,125,9,17,131,255,131,17,57,85,17,17,131,255,131,17,17,85,57,17,131,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,231,255,231,231,199,199,199,255,239,131,41,47,41,131,239,255,195,153,159,3,159,159,1,255,255,165,219,219,219,165,255,255,153,153,195,129,231,129,231,255,231,231,231,255,231,231,231,255,195,153,135,219,225,153,195,255,147,255,255,255,255,255,255,255,195,189,102,94,94,102,189,195,135,195,147,195,255,255,255,255,255,201,147,39,147,201,255,255,255,255,129,249,249,255,255,255,255,255,255,255,255,255,255,255,195,189,70,90,70,90,189,195,131,255,255,255,255,255,255,255,239,215,239,255,255,255,255,255,231,231,129,231,231,255,129,255,199,243,231,195,255,255,255,255,195,231,243,199,255,255,255,255,247,239,255,255,255,255,255,255,255,255,51,51,51,51,9,63,193,149,181,149,193,245,245,255,255,255,255,207,207,255,255,255,255,255,255,255,255,255,247,207,231,199,231,195,255,255,255,255,199,147,147,199,255,255,255,255,255,39,147,201,147,39,255,255,189,59,183,173,217,177,125,255,189,59,183,169,221,187,113,255,29,187,215,45,217,177,125,255,199,255,199,159,57,1,131,255,223,239,199,147,57,1,57,255,247,239,199,147,57,1,57,255,199,147,199,147,57,1,57,255,203,167,199,147,57,1,57,255,147,255,199,147,57,1,57,255,239,215,199,147,57,1,57,255,193,135,39,33,7,39,33,255,195,153,63,63,153,195,247,207,223,239,1,63,3,63,1,255,247,239,1,63,3,63,1,255,199,147,1,63,3,63,1,255,147,255,1,63,3,63,1,255,239,247,129,231,231,231,129,255,247,239,129,231,231,231,129,255,231,195,129,231,231,231,129,255,153,255,129,231,231,231,129,255,135,147,153,9,153,147,135,255,203,167,25,9,1,33,49,255,223,239,131,57,57,57,131,255,247,239,131,57,57,57,131,255,199,147,131,57,57,57,131,255,203,167,131,57,57,57,131,255,147,255,131,57,57,57,131,255,255,187,215,239,215,187,255,255,131,57,49,41,25,57,131,255,223,239,57,57,57,57,131,255,247,239,57,57,57,57,131,255,199,147,255,57,57,57,131,255,147,255,57,57,57,57,131,255,247,239,153,153,195,231,231,255,63,3,57,57,57,3,63,255,195,153,153,147,153,137,147,255,223,239,131,249,129,57,129,255,247,239,131,249,129,57,129,255,199,147,131,249,129,57,129,255,203,167,131,249,129,57,129,255,147,255,131,249,129,57,129,255,239,215,131,249,129,57,129,255,255,255,131,233,129,47,131,255,255,255,129,63,63,129,247,207,223,239,131,57,1,63,131,255,247,239,131,57,1,63,131,255,199,147,131,57,1,63,131,255,147,255,131,57,1,63,131,255,223,239,255,199,231,231,129,255,247,239,255,199,231,231,129,255,199,147,255,199,231,231,129,255,147,255,199,231,231,231,129,255,155,135,103,131,57,57,131,255,203,167,3,57,57,57,57,255,223,239,131,57,57,57,131,255,247,239,131,57,57,57,131,255,199,147,131,57,57,57,131,255,203,167,131,57,57,57,131,255,147,255,131,57,57,57,131,255,255,231,255,129,255,231,255,255,255,255,131,49,41,25,131,255,223,239,57,57,57,57,129,255,247,239,57,57,57,57,129,255,199,147,255,57,57,57,129,255,147,255,57,57,57,57,129,255,247,239,57,57,57,129,249,131,63,63,3,57,57,3,63,63,147,255,57,57,57,129,249,131),At=new WebSocket(("https:"==location.protocol?"wss":"ws")+"://"+location.host);function Et(t){const e=new URL(location.href),s=e.searchParams.get(t);if(null!=s)return s;return new URL(e.hash.substring(1),"https://x").searchParams.get(t)}function St(){if(null==document.fullscreenElement){let t=function(){const t=window.frameElement;t&&(t.style.position="fixed",t.style.top="0",t.style.left="0",t.style.zIndex="99999",t.style.width="100%",t.style.height="100%")};const e=document.body.requestFullscreen&&document.body.requestFullscreen({navigationUI:"hide"});e?e.catch(t):t()}}const kt="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#".split(""),Ct=[0,68,0,84,83,82,72,0,75,76,70,65,0,63,62,69,0,1,2,3,4,5,6,7,8,9,64,0,73,66,74,71,81,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,77,0,78,67,0,0,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,79,0,80,0,0];function Pt(t){const e=t.length,s=e%4,i=s?e+4-s:e;let r="",n=0,o=0;for(;n<i;){const s=n<e?t[n]:0;if(++n,o=256*o+s,n%4==0){let t=52200625;for(;t>=1;){const e=Math.floor(o/t)%85;r+=kt[e],t/=85}o=0}}return r}function Ut(t,e){let s=0,i=0,r=0;const n=t.length,o=e.length;if(t.length%5==0)for(;i<n;){const n=t.charCodeAt(i++)-32;if(n<0||n>=Ct.length)return s;if(r=85*r+Ct[n],i%5==0){let t=16777216;for(;t>=1;){if(s>=o)return s;e[s++]=r/t%256,t/=256}r=0}}return s}class Tt{constructor(){this.memory=new ArrayBuffer(65536),this.globals={},this.diskBuffer=new ArrayBuffer(yt),this.diskSize=0}read(t){new Uint8Array(this.memory).set(new Uint8Array(t.memory.buffer)),this.globals={};for(const e in t.wasm.exports){const s=t.wasm.exports[e];s instanceof WebAssembly.Global&&(this.globals[e]=s.value.toString())}this.diskSize=t.diskSize,new Uint8Array(this.diskBuffer).set(new Uint8Array(t.diskBuffer,0,t.diskSize))}write(t){new Uint8Array(t.memory.buffer).set(new Uint8Array(this.memory));for(const e in t.wasm.exports){const s=t.wasm.exports[e];if(s instanceof WebAssembly.Global&&e in this.globals)try{s.value=this.globals[e]}catch{}}t.diskSize=this.diskSize,new Uint8Array(t.diskBuffer).set(new Uint8Array(this.diskBuffer,0,this.diskSize))}toBytes(){const t=(new TextEncoder).encode(JSON.stringify(this.globals)),e=new Uint8Array(65544+t.byteLength+this.diskSize),s=new DataView(e.buffer,e.byteOffset,e.byteLength);e.set(new Uint8Array(this.memory),0);let i=65536;return s.setUint32(i,t.byteLength),e.set(t,i+4),i+=4+t.byteLength,s.setUint32(i,this.diskSize),e.set(new Uint8Array(this.diskBuffer,0,this.diskSize),i+4),e}fromBytes(t){const e=new DataView(t.buffer,t.byteOffset,t.byteLength);new Uint8Array(this.memory).set(t.subarray(0,65536));let s=65536;const i=e.getUint32(s),r=t.slice(s+4,s+4+i);this.globals=JSON.parse((new TextDecoder).decode(r)),s+=4+i,this.diskSize=e.getUint32(s),new Uint8Array(this.diskBuffer).set(t.subarray(s+4,s+4+this.diskSize))}}const Mt=20;class It{constructor(){this.frame=-1,this.state=new Tt,this.inputs=new Array(4),this.predicted=new Array(4);for(let t=0;t<4;++t)this.inputs[t]=0,this.predicted[t]=!0}}class Ot{constructor(){this.futureInputs=new Map}}class Rt{constructor(t,e){this.currentFrame=t,this.runtime=e,this.rollbackIdx=Mt,this.history=new Array(Mt);for(let s=0;s<Mt;++s)this.history[s]=new It;this.players=new Array(4);for(let s=0;s<4;++s)this.players[s]=new Ot}addInputs(t,e,s){const i=this.players[t];for(const r of s){if(e>=this.currentFrame)i.futureInputs.has(e)||i.futureInputs.set(e,r);else for(let s=0,i=Mt;s<i;++s){const i=this.history[s];if(i.frame==e){i.predicted[t]&&(i.predicted[t]=!1,i.inputs[t]!=r&&(i.inputs[t]=r,this.rollbackIdx=Math.min(s,this.rollbackIdx)));break}}++e}}update(){if(this.rollbackIdx<Mt){for(let e=this.rollbackIdx+1;e<Mt;++e){const t=this.history[e];for(let s=0;s<4;++s)if(t.predicted[s]){const i=this.history[e-1];t.inputs[s]=i.inputs[s]}}let t=!0;for(;this.rollbackIdx<Mt;){const e=this.history[this.rollbackIdx++];t?(t=!1,e.state.write(this.runtime)):e.state.read(this.runtime);for(let t=0;t<4;++t)this.runtime.setGamepad(t,e.inputs[t]);this.runtime.update()}}const t=this.history[19],e=this.history.shift();this.history.push(e),e.frame=this.currentFrame,e.state.read(this.runtime);for(let s=0;s<4;++s){const i=this.players[s];let r=i.futureInputs.get(this.currentFrame);null!=r?(e.predicted[s]=!1,i.futureInputs.delete(this.currentFrame)):(e.predicted[s]=!0,r=t.inputs[s]),e.inputs[s]=r,this.runtime.setGamepad(s,r)}this.runtime.update(),++this.currentFrame}}class Lt{constructor(t){this.onMessage=t,this.bufferedOutput=[],this.connect(),this.keepaliveInterval=window.setInterval((()=>{this.send("",{type:"KEEPALIVE"},!1)}),15e3)}async connect(){const t=["wss://webrtc-signal-server.wasm4.org","wss://ywc2h85cv1.execute-api.us-east-1.amazonaws.com/production","wss://webrtc-signal-server.wasm4.jwq.moe","wss://iyuavuru2h.execute-api.eu-central-1.amazonaws.com/production"];for(const s of t)try{this.socket=await new Promise(((t,e)=>{const i=new WebSocket(s);i.addEventListener("open",(()=>{t(i)})),i.addEventListener("error",(()=>{e()}))}));break}catch(e){console.error(e)}if(!this.socket)throw new Error("Unable to connect to signal server");this.socket.addEventListener("message",(t=>{const{source:e,message:s}=JSON.parse(t.data);this.onMessage(e,s)}));for(const s of this.bufferedOutput)this.socket.send(s);this.bufferedOutput.length=0}send(t,e,s=!0){var i;const r=JSON.stringify({target:t,message:e});1==(null==(i=this.socket)?void 0:i.readyState)?this.socket.send(r):s&&this.bufferedOutput.push(r)}close(){var t;null==(t=this.socket)||t.close(),window.clearInterval(this.keepaliveInterval)}}class Dt{constructor(t){let e;this.connections=new Map,this.localPeerId=new Promise((t=>{e=t})),this.signalClient=new Lt((async(s,i)=>{switch(i.type){case"WHOAMI_REPLY":this.iceServers=i.iceServers,e(i.yourPeerId);break;case"OFFER":if(this.connections.has(s))throw new Error("Received offer for a connection we already initiated");{const e=this.createConnection(s);await e.setRemoteDescription(i.description),await e.setLocalDescription(await e.createAnswer()),t(e,s),this.signalClient.send(s,{type:"ANSWER",description:e.localDescription.toJSON()})}break;case"ANSWER":{const t=this.connections.get(s);t&&await t.setRemoteDescription(i.description)}break;case"CANDIDATE":{const t=this.connections.get(s);t&&await t.addIceCandidate(new RTCIceCandidate(i.candidate))}break;case"ABORT":{const t=this.connections.get(s);t&&t.close()}}})),this.signalClient.send("",{type:"WHOAMI_REQUEST"})}createConnection(t){const e=new RTCPeerConnection({iceServers:this.iceServers});return this.connections.set(t,e),e.addEventListener("negotiationneeded",(async()=>{await e.setLocalDescription(await e.createOffer()),this.signalClient.send(t,{type:"OFFER",description:e.localDescription.toJSON()})})),e.addEventListener("icecandidate",(({candidate:e})=>{e&&this.signalClient.send(t,{type:"CANDIDATE",candidate:e.toJSON()})})),e.addEventListener("connectionstatechange",(()=>{const s=e.connectionState;"connected"!=s&&"failed"!=s||this.connections.delete(t)})),e}connect(t){return this.connections.get(t)||this.createConnection(t)}close(){this.signalClient.close();for(const[t,e]of this.connections)e.close()}}const Bt=16384;class Nt{constructor(t){this.chunks=[],this.size=0,t.addEventListener("message",(t=>{if(t.data instanceof ArrayBuffer){const e=new Uint8Array(t.data);this.size+=e.byteLength,this.chunks.push(e)}}))}read(){const t=new Uint8Array(this.size);let e=0;for(const s of this.chunks)t.set(s,e),e+=s.byteLength;return this.chunks.length=0,this.size=0,t}}class zt{constructor(t){this.channel=t,this.size=0}write(t){this.buffer||(this.buffer=new Uint8Array(Bt));let e=this.size,s=0;for(;s<t.length;){const i=t.subarray(s,s+Bt-e);this.buffer.set(i,e),s+=i.length,e+=i.length,e>=Bt&&(e-=Bt,this.channel.send(this.buffer))}this.size=e}flush(){this.buffer&&this.size>0&&this.channel.send(this.buffer.subarray(0,this.size)),this.buffer=void 0}}class jt{constructor(t,e=0){this.buffer=t,this.position=e}readBit(){const t=!!(this.buffer[this.position/8>>>0]&1<<(7&this.position));return++this.position,t}readBits(t){let e=0;for(let s=0;s<t;++s)e|=+this.readBit()<<s;return e}}class Ht{constructor(t,e=0){this.buffer=t,this.position=e}write1(){this.buffer[this.position/8>>>0]|=1<<(7&this.position),++this.position}write0(){this.buffer[this.position/8>>>0]&=~(1<<(7&this.position)),++this.position}writeBits(t,e){for(let s=0;s<e;++s)t&1<<s?this.write1():this.write0()}}class Ft{constructor(t=.125){this.discount=t,this.average=0,this.firstUpdate=!0}update(t){if(this.firstUpdate)this.firstUpdate=!1,this.average=t;else{const e=this.discount;this.average=(1-e)*this.average+e*t}}}const Vt=8+Math.ceil(82.5),Wt=new ArrayBuffer(Vt);class Gt{constructor(t,e,s,i){this.peerId=t,this.connection=e,this.reliableChannel=s,this.unreliableChannel=i,this.playerIdx=-1,this.frame=0,this.nextNeededFrame=0,this.outboundFrame=0,this.outboundInputs=[],this.ping=new Ft,this.drift=new Ft,this.chunkReader=new Nt(s),this.chunkWriter=new zt(s)}addOutboundInput(t,e){if(0==this.outboundFrame&&(this.outboundFrame=t),t<this.outboundFrame){for(let s=this.outboundFrame-t;s>0;--s)this.outboundInputs.unshift(e);this.outboundFrame=t}else{const s=t-this.outboundFrame;if(s>=this.outboundInputs.length){for(let t=this.outboundInputs.length;t<s;++t)this.outboundInputs[t]=t>0?this.outboundInputs[t-1]:0;this.outboundInputs[s]=e}}}sendMessage(t){this.reliableChannel.send(JSON.stringify(t))}sendUnreliableBuffer(t){this.unreliableChannel.send(t)}sendPingRequest(){const t=new DataView(Wt);t.setUint8(0,2),t.setUint32(1,performance.now()>>>0),this.sendUnreliableBuffer(new Uint8Array(Wt,0,5))}sendPingReply(t){const e=new DataView(Wt);e.setUint8(0,3),e.setUint32(1,t),this.sendUnreliableBuffer(new Uint8Array(Wt,0,5))}sendTick(t){const e=new DataView(Wt);e.setUint8(0,1),e.setUint32(1,t),e.setInt8(5,0==this.nextNeededFrame?-127:this.nextNeededFrame-t),e.setInt8(6,this.outboundFrame-t),e.setUint8(7,this.outboundInputs.length);const s=new Ht(new Uint8Array(Wt,8));let i=0;for(const n of this.outboundInputs){const t=i^n;i=n;for(let e=0;e<8;++e)t&1<<e&&(s.write1(),s.writeBits(e,3));s.write0()}const r=8+Math.ceil(s.position/8);this.sendUnreliableBuffer(new Uint8Array(Wt,0,r))}close(){this.reliableChannel.close(),this.unreliableChannel.close(),this.connection.close()}}class Yt{constructor(t){this.runtime=t,this.remotePlayers=new Map,this.localPlayerIdx=-1,this.updateCount=0,this.peerMgr=new Dt((async(t,e)=>{const s=Array.from(this.remotePlayers.keys()),i=await this.createRemotePlayer(t,e);i.sendMessage({type:"WELCOME",otherPeers:s}),this.localPlayerIdx>=0&&i.sendMessage({type:"PLAYER_INFO",playerIdx:this.localPlayerIdx})}))}host(){this.runtime.setNetplay(0),this.rollbackMgr=new Rt(1,this.runtime),this.localPlayerIdx=0}join(t){const e=this.peerMgr.connect(t);this.createRemotePlayer(e,t).then((t=>{t.sendMessage({type:"JOIN_REQUEST"})}),(t=>{this.runtime.blueScreen("Failed to connect\nto peer. They may\nbe offline?")}))}async getInviteLink(){const t=await this.peerMgr.localPeerId,e=window.location;if("file:"==e.protocol||"localhost"==e.hostname||"127.0.0.1"==e.hostname||"wasm4.org"==e.hostname)return`https://wasm4.org/netplay/#${t}`;const s=new URL(e.href);return s.searchParams.set("netplay",t),s.href}close(){for(const t of this.remotePlayers.values())t.close();this.peerMgr.close()}async createRemotePlayer(t,e){function s(e){return new Promise(((s,i)=>{const r=t.createDataChannel("WASM-4",e);r.binaryType="arraybuffer",r.onopen=()=>{s(r)},r.onerror=i}))}const[i,r]=await Promise.all([s({negotiated:!0,id:0}),s({negotiated:!0,id:1,ordered:!1,maxRetransmits:0})]),n=new Gt(e,t,i,r);this.remotePlayers.set(e,n);const o=()=>{this.remotePlayers.has(e)&&(this.remotePlayers.delete(e),this.onleave&&n.playerIdx>=0&&this.onleave(n.playerIdx))};return t.addEventListener("connectionstatechange",(()=>{"disconnected"==t.connectionState&&o()})),i.addEventListener("close",o),r.addEventListener("close",o),i.addEventListener("message",(async t=>{if("string"!=typeof t.data)return;const e=JSON.parse(t.data);switch(e.type){case"WELCOME":for(const t of e.otherPeers)if(!this.remotePlayers.has(t)){const e=this.peerMgr.connect(t),s=await this.createRemotePlayer(e,t);this.localPlayerIdx>=0&&s.sendMessage({type:"PLAYER_INFO",playerIdx:this.localPlayerIdx})}break;case"JOIN_REQUEST":{const t=this.nextPlayerIdx();if(t>=4)n.sendMessage({type:"JOIN_REJECT"}),n.close();else{n.playerIdx=t,n.chunkWriter.write(this.runtime.wasmBuffer);const e=new Tt;e.read(this.runtime),n.chunkWriter.write(e.toBytes()),n.chunkWriter.flush(),n.sendMessage({type:"JOIN_REPLY",yourPlayerIdx:n.playerIdx,frame:this.rollbackMgr.currentFrame,stateOffset:this.runtime.wasmBuffer.byteLength}),this.onjoin&&this.onjoin(n.playerIdx)}}break;case"JOIN_REPLY":{this.localPlayerIdx=e.yourPlayerIdx;for(const e of this.remotePlayers.values())e!=n&&e.sendMessage({type:"PLAYER_INFO",playerIdx:this.localPlayerIdx});const t=n.chunkReader.read(),s=t.subarray(0,e.stateOffset),i=t.subarray(e.stateOffset);await this.runtime.load(s,!1);const r=new Tt;r.fromBytes(i),r.write(this.runtime),this.runtime.setNetplay(this.localPlayerIdx),this.rollbackMgr=new Rt(e.frame,this.runtime),this.onstart&&this.onstart(this.localPlayerIdx)}break;case"JOIN_REJECT":this.runtime.blueScreen("Connected, but the\ngame is already\nfull!"),this.close();break;case"PLAYER_INFO":-1==n.playerIdx&&(n.playerIdx=e.playerIdx,this.onjoin&&this.onjoin(n.playerIdx))}})),r.addEventListener("message",(async t=>{const e=t.data,s=new DataView(e);switch(s.getUint8(0)){case 1:if(this.rollbackMgr&&n.playerIdx>=0){const t=s.getUint32(1);if(t>n.frame){const i=s.getInt8(5)+t,r=s.getInt8(6)+t,o=s.getUint8(7),a=new jt(new Uint8Array(e,8)),l=new Array(o);let h=0;for(let t=0;t<o;++t){for(;a.readBit();){h^=1<<a.readBits(3)}l[t]=h}if(n.frame=t,n.nextNeededFrame=r+l.length,0==n.outboundFrame)n.outboundFrame=i;else if(i>n.outboundFrame){const t=i-n.outboundFrame;n.outboundFrame=i,n.outboundInputs.splice(0,t)}const d=t+.5*n.ping.average*60/1e3,c=this.rollbackMgr.currentFrame-d;n.drift.update(c),this.rollbackMgr.addInputs(n.playerIdx,r,l)}}break;case 2:{const t=s.getUint32(1);n.sendPingReply(t)}break;case 3:{const t=s.getUint32(1),e=(performance.now()>>>0)-t;n.ping.update(e)}}})),n}nextPlayerIdx(){t:for(let t=0;t<4;++t)if(this.localPlayerIdx!=t){for(const e of this.remotePlayers.values())if(e.playerIdx==t)continue t;return t}return-1}update(t){if(!this.rollbackMgr)return!1;const e=0==(7&this.updateCount),s=0==(31&this.updateCount);++this.updateCount;const i=this.rollbackMgr.currentFrame,r=i+2;this.rollbackMgr.addInputs(this.localPlayerIdx,r,[t]);let n=!1;for(const o of this.remotePlayers.values())o.addOutboundInput(r,t),o.sendTick(i),(o.nextNeededFrame<i-Mt||o.outboundInputs.length>=20)&&(n=!0),s&&o.sendPingRequest();if(e){let t=0;for(const e of this.remotePlayers.values())t=Math.max(e.drift.average,t);t>=1&&(n=!0)}return n||this.rollbackMgr.update(),!n}getSummary(){const t=[{playerIdx:this.localPlayerIdx,ping:-1}];for(const e of this.remotePlayers.values())t.push({playerIdx:e.playerIdx,ping:e.ping.average});return t.sort(((t,e)=>t.playerIdx-e.playerIdx)),t}}const qt='"use strict";(()=>{var M=Math.floor(44.1),b=class{constructor(){this.freq1=0;this.freq2=0;this.startTime=0;this.attackTime=0;this.decayTime=0;this.sustainTime=0;this.releaseTime=0;this.endTick=0;this.sustainVolume=0;this.peakVolume=0;this.phase=0;this.pan=0;this.pulseDutyCycle=0;this.noiseSeed=1;this.noiseLastRandom=0}};function L(m,e,s){return m+s*(e-m)}function V(m,e){if(m<e){let s=m/e;return s+s-s*s}else if(m>1-e){let s=(m-(1-e))/e;return 1-(s+s-s*s)}else return 1}function x(m,e){return Math.pow(2,(m-69+e/256)/12)*440}var y=class extends AudioWorkletProcessor{constructor(){super(),this.time=0,this.ticks=0,this.channels=new Array(4);for(let e=0;e<4;++e)this.channels[e]=new b;this.port!=null&&(this.port.onmessage=e=>{e.data==="tick"?this.tick():this.tone(...e.data)})}ramp(e,s,f,u){if(this.time>=u)return s;let a=(this.time-f)/(u-f);return L(e,s,a)}getCurrentFrequency(e){return e.freq2>0?this.ramp(e.freq1,e.freq2,e.startTime,e.releaseTime):e.freq1}getCurrentVolume(e){let s=this.time;return s>=e.sustainTime&&e.releaseTime-e.sustainTime>M?this.ramp(e.sustainVolume,0,e.sustainTime,e.releaseTime):s>=e.decayTime?e.sustainVolume:s>=e.attackTime?this.ramp(e.peakVolume,e.sustainVolume,e.attackTime,e.decayTime):this.ramp(0,e.peakVolume,e.startTime,e.attackTime)}tick(){this.ticks++}tone(e,s,f,u){let a=e&65535,p=e>>16&65535,A=s&255,h=s>>8&255,o=s>>16&255,i=s>>24&255,E=Math.min(f&255,100),l=Math.min(f>>8&255,100),n=u&3,r=u>>2&3,c=u>>4&3,d=u&64,t=this.channels[n];this.time>t.releaseTime&&this.ticks!=t.endTick&&(t.phase=n==2?.25:0),d?(t.freq1=x(a&255,a>>8),t.freq2=p==0?0:x(p&255,p>>8)):(t.freq1=a,t.freq2=p),t.startTime=this.time,t.attackTime=t.startTime+(44100*i/60>>>0),t.decayTime=t.attackTime+(44100*o/60>>>0),t.sustainTime=t.decayTime+(44100*A/60>>>0),t.releaseTime=t.sustainTime+(44100*h/60>>>0),t.endTick=this.ticks+i+o+A+h,t.pan=c;let T=n==2?.25:.15;if(t.sustainVolume=T*E/100,t.peakVolume=l?T*l/100:T,n==0||n==1)switch(r){case 0:t.pulseDutyCycle=.125;break;case 1:case 3:default:t.pulseDutyCycle=.25;break;case 2:t.pulseDutyCycle=.5;break}else n==2&&h==0&&(t.releaseTime+=M)}process(e,[[s,f]],u){for(let a=0,p=s.length;a<p;++a,++this.time){let A=0,h=0;for(let o=0;o<4;++o){let i=this.channels[o];if(this.time<i.releaseTime||this.ticks==i.endTick){let E=this.getCurrentFrequency(i),l=this.getCurrentVolume(i),n;if(o==3){for(i.phase+=E*E/(1e6/44100*44100);i.phase>0;){i.phase--;let r=i.noiseSeed;r^=r>>7,r^=r<<9,r^=r>>13,i.noiseSeed=r,i.noiseLastRandom=((r&1)<<1)-1}n=l*i.noiseLastRandom}else{let r=E/44100,c=i.phase+r;if(c>=1&&c--,i.phase=c,o==2)n=l*(2*Math.abs(2*i.phase-1)-1);else{let d,t,T,k=i.pulseDutyCycle;c<k?(d=c/k,t=r/k,T=l):(d=(c-k)/(1-k),t=r/(1-k),T=-l),n=T*V(d,t)}}i.pan!=1&&(h+=n),i.pan!=2&&(A+=n)}}s[a]=A,f[a]=h}return!0}};registerProcessor("wasm4-apu",y);})();\n';class Kt{constructor(){this.audioCtx=new(window.AudioContext||window.webkitAudioContext)({sampleRate:44100})}async init(){const t=this.audioCtx,e=new Blob([qt],{type:"application/javascript"}),s=URL.createObjectURL(e);try{await t.audioWorklet.addModule(s);const e=new AudioWorkletNode(t,"wasm4-apu",{outputChannelCount:[2]});this.processorPort=e.port,e.connect(t.destination)}catch(i){let e;console.warn("AudioWorklet loading failed, falling back to slow audio",i);const s=(t,s)=>{e=new s};new Function("registerProcessor","AudioWorkletProcessor",qt)(s,class{}),this.processor=e;const r=t.createScriptProcessor(1024,0,2);r.onaudioprocess=t=>{const s=t.outputBuffer.getChannelData(0),i=t.outputBuffer.getChannelData(1);e.process(null,[[s,i]],null)},r.connect(t.destination)}}tick(){null!=this.processorPort?this.processorPort.postMessage("tick"):this.processor.tick()}tone(t,e,s,i){null!=this.processorPort?this.processorPort.postMessage([t,e,s,i]):this.processor.tone(t,e,s,i)}unlockAudio(){const t=this.audioCtx;"suspended"==t.state&&t.resume()}pauseAudio(){const t=this.audioCtx;"running"==t.state&&t.suspend()}}class Jt{constructor(t){this.bytes=new Uint8Array(t,160,6400),this.drawColors=new Uint16Array(t,20,1)}clear(){this.bytes.fill(0)}drawPoint(t,e,s){const i=mt*s+e>>>2,r=(3&e)<<1,n=3<<r;this.bytes[i]=t<<r|this.bytes[i]&~n}drawPointUnclipped(t,e,s){e>=0&&e<mt&&s>=0&&s<gt&&this.drawPoint(t,e,s)}drawHLineFast(t,e,s,i){const r=i-(3&i),n=Math.min(e+3&-4,r);if(r-n>3){for(let r=e;r<n;r++)this.drawPoint(t,r,s);const i=mt*s+n>>>2,o=mt*s+r>>>2,a=85*t;this.bytes.fill(a,i,o),e=r}for(let o=e;o<i;o++)this.drawPoint(t,o,s)}drawHLineUnclipped(t,e,s,i){s>=0&&s<gt&&(e<0&&(e=0),i>mt&&(i=mt),e<i&&this.drawHLineFast(t,e,s,i))}drawHLine(t,e,s){const i=15&this.drawColors[0];if(0==i)return;const r=i-1&3;this.drawHLineUnclipped(r,t,e,t+s)}drawVLine(t,e,s){if(e+s<=0||t<0||t>=mt)return;const i=15&this.drawColors[0];if(0==i)return;const r=Math.max(0,e),n=Math.min(gt,e+s),o=i-1&3;for(let a=r;a<n;a++)this.drawPoint(o,t,a)}drawRect(t,e,s,i){const r=Math.max(0,t),n=Math.max(0,e),o=t+s,a=e+i,l=Math.max(0,Math.min(o,mt)),h=Math.max(0,Math.min(a,gt)),d=this.drawColors[0],c=15&d,p=d>>>4&15;if(0!==c){const t=c-1&3;for(let e=n;e<h;++e)this.drawHLineFast(t,r,e,l)}if(0!==p){const s=p-1&3;if(t>=0&&t<mt)for(let e=n;e<h;++e)this.drawPoint(s,t,e);if(o>0&&o<=mt)for(let t=n;t<h;++t)this.drawPoint(s,o-1,t);e>=0&&e<gt&&this.drawHLineFast(s,r,e,l),a>0&&a<=gt&&this.drawHLineFast(s,r,a-1,l)}}drawOval(t,e,s,i){const r=this.drawColors[0],n=15&r,o=r>>>4&15;if(15===o)return;const a=o-1&3,l=n-1&3;let h=s-1;const d=i-1;let c=d%2,p=e+Math.floor(i/2),u=t,f=t+s-1,m=p-c;const g=h*h,y=d*d;let b=4*(1-h)*y,v=4*(c+1)*g,w=b+v+c*g;h=8*g,c=8*y;do{this.drawPointUnclipped(a,f,p),this.drawPointUnclipped(a,u,p),this.drawPointUnclipped(a,u,m),this.drawPointUnclipped(a,f,m);const t=u+1;0!==n&&f-t>0&&(this.drawHLineUnclipped(l,t,p,f),this.drawHLineUnclipped(l,t,m,f));const e=2*w;e<=v&&(p+=1,m-=1,v+=h,w+=v),(e>=b||e>v)&&(u+=1,f-=1,b+=c,w+=b)}while(u<=f);for(;p-m<i;)this.drawPointUnclipped(a,u-1,p),this.drawPointUnclipped(a,f+1,p),p+=1,this.drawPointUnclipped(a,u-1,m),this.drawPointUnclipped(a,f+1,m),m-=1}drawLine(t,e,s,i){const r=15&this.drawColors[0];if(0===r)return;const n=r-1&3;if(e>i){let r=t;t=s,s=r,r=e,e=i,i=r}const o=Math.abs(s-t),a=t<s?1:-1,l=i-e;let h,d=(o>l?o:-l)/2;for(;this.drawPointUnclipped(n,t,e),t!==s||e!==i;)h=d,h>-o&&(d-=l,t+=a),h<l&&(d+=o,e++)}drawText(t,e,s){let i=e;for(let r=0,n=t.length;r<n;++r){const n=t[r];if(0===n)return;10===n?(s+=8,i=e):n>=32&&n<=255?(this.blit($t,i,s,8,8,0,n-32<<3,8),i+=8):i+=8}}blit(t,e,s,i,r,n,o,a,l=!1,h=!1,d=!1,c=!1){const p=this.drawColors[0];let u,f,m,g;c?(h=!h,u=Math.max(0,s)-s,f=Math.max(0,e)-e,m=Math.min(i,gt-s),g=Math.min(r,mt-e)):(u=Math.max(0,e)-e,f=Math.max(0,s)-s,m=Math.min(i,mt-e),g=Math.min(r,gt-s));for(let y=f;y<g;y++)for(let f=u;f<m;f++){const u=e+(c?y:f),m=s+(c?f:y);let g;const b=(o+(d?r-y-1:y))*a+(n+(h?i-f-1:f));if(l){g=t[b>>>2]>>>6-((3&b)<<1)&3}else{g=t[b>>>3]>>>7-(7&b)&1}const v=p>>>(g<<2)&15;0!==v&&this.drawPoint(v-1&3,u,m)}}}const Xt=6409,Zt=3553;class Qt{constructor(t){this.gl=t,this.colorBuffer=new Uint32Array(6400),this.paletteBuffer=new Float32Array(12),this.lastPalette=Array(4),this.paletteLocation=null;const e=new Uint32Array(256);for(let i=0;i<256;++i){const t=i>>6&3,s=i>>4&3,r=i>>2&3,n=3&i;e[i]=t<<30|s<<22|r<<14|n<<6}this.table=e;const s=t.canvas;s.addEventListener("webglcontextlost",(t=>{t.preventDefault()})),s.addEventListener("webglcontextrestored",(()=>{this.initGL()})),this.initGL()}initGL(){const t=this.gl;function e(e,s){const i=t.createShader(e);if(t.shaderSource(i,s),t.compileShader(i),!t.getShaderParameter(i,35713))throw new Error(t.getShaderInfoLog(i)+"");return i}this.lastPalette=Array(4);const s=e(35633,"\n attribute vec2 pos;\n varying vec2 framebufferCoord;\n\n void main () {\n framebufferCoord = pos*vec2(0.5, -0.5) + 0.5;\n gl_Position = vec4(pos, 0, 1);\n }\n "),i=e(35632,`\n precision mediump float;\n uniform vec3 palette[4];\n uniform sampler2D framebuffer;\n varying vec2 framebufferCoord;\n\n vec3 lookup(float index) {\n vec3 p = palette[0];\n ${Array.from({length:3},((t,e)=>`p = mix(p, palette[${e+1}], step(${((e+1)/4).toFixed(2)}, index));`)).join("\n")}\n return p;\n }\n\n void main () {\n gl_FragColor = vec4(lookup(texture2D(framebuffer, framebufferCoord).r), 1.);\n }\n `),r=t.createProgram();if(t.attachShader(r,s),t.attachShader(r,i),t.linkProgram(r),!t.getProgramParameter(r,35714))throw new Error(t.getProgramInfoLog(r)+"");t.useProgram(r),this.paletteLocation=t.getUniformLocation(r,"palette"),t.uniform1i(t.getUniformLocation(r,"framebuffer"),0),t.detachShader(r,s),t.deleteShader(s),t.detachShader(r,i),t.deleteShader(i),function(e){const s=t.createTexture();t.activeTexture(e),t.bindTexture(Zt,s),t.texParameteri(Zt,10242,33071),t.texParameteri(Zt,10243,33071),t.texParameteri(Zt,10240,9728),t.texParameteri(Zt,10241,9728)}(33984),t.texImage2D(Zt,0,Xt,mt,gt,0,Xt,5121,null);const n=t.getAttribLocation(r,"pos"),o=t.createBuffer(),a=new Float32Array([-1,-1,-1,1,1,1,1,1,1,-1,-1,-1]);t.bindBuffer(34962,o),t.bufferData(34962,a,35044),t.enableVertexAttribArray(n),t.vertexAttribPointer(n,2,5126,!1,0,0)}composite(t,e){const s=this.gl,i=e.bytes,r=this.colorBuffer,n=this.table,o=this.lastPalette,a=this.paletteBuffer;let l=!1;for(let h=0,d=0;h<4;++h){const e=t[h];l=l||o[h]!==e,a[d++]=(e>>16&255)/255,a[d++]=(e>>8&255)/255,a[d++]=(255&e)/255,o[h]=e}l&&s.uniform3fv(this.paletteLocation,this.paletteBuffer);for(let h=0;h<6400;++h)r[h]=n[i[h]];s.texImage2D(Zt,0,Xt,mt,gt,0,Xt,5121,new Uint8Array(r.buffer)),s.drawArrays(4,0,6)}}function te(t,e){const s=e.reduce(((t,e)=>t+(0===e[0]?e[2]-e[1]:e[1].length)),0),i=new Uint8Array(s);let r=0;for(const n of e)0===n[0]?(i.set(t.slice(n[1],n[2]),r),r+=n[2]-n[1]):(i.set(n[1],r),r+=n[1].length);return i}function ee(t){const e=[];do{const s=127&t;t>>=7,e.push(t?128|s:s)}while(t);return new Uint8Array(e)}function se(t,e=0){l