SOURCE / PINNED RELEASE
Made of little things.
Gem Warden
- Release
- d7ee3a74bdb5…
- Author-recorded commit
- 1bfb3fd0904c…
- License
- Check the original source
- Author’s source reference
- nostr://npub1eg5pfnws6tg6rtp33tugpppqtv5u05crmf983qp438cdycwzngys0gxzcs/wss%3A%2F%2Fgit.napplet.soy%2F/n-3929629ef1d
The hash-verified playable HTML, which may contain bundled or minified code. This view does not execute it.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<title>Gem Warden</title>
<meta name="description" content="Gem tower defense: forge and combine gems, socket them into towers and traps, and keep monsters away from your mana orb across six fields." />
<script type="module" crossorigin>var zo=Object.defineProperty;var Bo=(t,e,a)=>e in t?zo(t,e,{enumerable:!0,configurable:!0,writable:!0,value:a}):t[e]=a;var Et=(t,e,a)=>Bo(t,typeof e!="symbol"?e+"":e,a);function No(){const t=new Map;function e(s,o){if(t.has(s))throw new Error(`NAP domain "${s}" is already registered`);t.set(s,o)}function a(s){const o=s.type.indexOf(".");if(o<=0)return!1;const l=s.type.slice(0,o),r=t.get(l);return r?(r(s),!0):!1}function n(){return Array.from(t.keys())}return{registerNap:e,dispatch:a,getRegisteredDomains:n}}var Ho=No(),Ba=Ho.registerNap,Qn="storage";Ba(Qn,t=>{});var jn="media";Ba(jn,t=>{});function qo(){const t=window;if(!t.napplet)throw new Error("window.napplet is unavailable -- runtime did not inject the napplet namespace");return t.napplet}function lt(t){const a=qo()[t];if(!a)throw new Error(`window.napplet.${String(t)} is unavailable -- runtime did not inject this domain`);return a}var Na={getItem(t){return lt("storage").getItem(t)},setItem(t,e){return lt("storage").setItem(t,e)},removeItem(t){return lt("storage").removeItem(t)},keys(){return lt("storage").keys()},instance:{getItem(t){return lt("storage").instance.getItem(t)},setItem(t,e){return lt("storage").instance.setItem(t,e)},removeItem(t){return lt("storage").instance.removeItem(t)},keys(){return lt("storage").instance.keys()}}},_o={createSession(t){return lt("media").createSession(t)},updateSession(t,e){lt("media").updateSession(t,e)},destroySession(t){lt("media").destroySession(t)},reportState(t,e){lt("media").reportState(t,e)},reportCapabilities(t,e){lt("media").reportCapabilities(t,e)},sendCommand(t,e,a){lt("media").sendCommand(t,e,a)},onCommand(t,e){return lt("media").onCommand(t,e)},onState(t,e){return lt("media").onState(t,e)},onCapabilities(t,e){return lt("media").onCapabilities(t,e)},onControls(t,e){return lt("media").onControls(t,e)}};function Vo(t,e){return!!(t!=null&&t[e])}function ln(t){return Vo(window.napplet,t)}let C=null,ne,se,ke,ae,tt,Wn,fe=0,_e=84,Se=!0,Ha=!0,Ko=0;function qt(){return!!C&&C.state!=="closed"}function V(){return C.currentTime+Ko}function Lo(t,e=t.destination){if(C)return C===t;C=t;const a=C.createDynamicsCompressor();a.threshold.value=-12,a.knee.value=10,a.ratio.value=4,a.attack.value=.015,a.release.value=.25;const n=C.createDynamicsCompressor();n.threshold.value=-4,n.knee.value=0,n.ratio.value=20,n.attack.value=.001,n.release.value=.08;const s=C.createGain();s.gain.value=.85,ne=C.createGain(),ne.gain.value=1.6,ne.connect(a).connect(n).connect(s).connect(e),se=C.createGain(),se.gain.value=Se?1:0,se.connect(ne),ae=C.createBiquadFilter(),ae.type="lowpass",ae.frequency.value=16e3,ae.connect(ne),ke=C.createGain(),ke.gain.value=0,ke.connect(ae);const o=Math.floor(C.sampleRate*2.3),l=C.createBuffer(2,o,C.sampleRate);for(let u=0;u<2;u++){const w=l.getChannelData(u);for(let x=0;x<o;x++)w[x]=(Math.random()*2-1)*Math.pow(1-x/o,3.2)}const r=C.createConvolver();r.buffer=l;const c=C.createGain();c.gain.value=.5,r.connect(c).connect(ne);const f=C.createGain();f.gain.value=.2,se.connect(f).connect(r);const h=C.createGain();h.gain.value=.34,ae.connect(h).connect(r),tt=qa(),Wn=C.createBuffer(1,C.sampleRate,C.sampleRate);const d=Wn.getChannelData(0);for(let u=0;u<d.length;u++)d[u]=Math.random()*2-1;return!0}function qa(){const t=C,e=t.createGain();e.connect(ke);const a=t.createBiquadFilter();a.type="lowpass",a.frequency.value=2400,a.Q.value=.6,a.connect(e);const n=t.createGain();n.gain.value=.5;const s=t.createDelay(1);s.delayTime.value=.32;const o=t.createGain();o.gain.value=.36;const l=t.createBiquadFilter();return l.type="lowpass",l.frequency.value=2200,n.connect(s),s.connect(l).connect(o).connect(s),s.connect(e),{out:e,soft:a,echo:n}}function Yo(t,e){const a=C,n=a.createOscillator(),s=a.createGain();s.gain.value=0,n.connect(s).connect(ne),n.onended=e,n.start(V()),n.stop(V()+t)}function Xo(t,e){Se=t,Ha=e,qt()&&(se.gain.setTargetAtTime(t?1:0,V(),.03),e||(ke.gain.setTargetAtTime(0,V(),.08),On=0))}function Jn(t){const e=t.dest??se,a=C;if(t.pan===void 0||typeof a.createStereoPanner!="function")return e;const n=a.createStereoPanner();return n.pan.value=Math.max(-1,Math.min(1,t.pan)),n.connect(e),n}function rn(t){fe++,t.onended=()=>{fe--}}function Un(t,e,a,n,s){t.gain.value=0,t.gain.setValueAtTime(1e-4,e),t.gain.exponentialRampToValueAtTime(Math.max(2e-4,n),e+a),t.gain.exponentialRampToValueAtTime(1e-4,e+Math.max(a+.01,s))}function A(t,e,a,n={}){if(!C||!qt()||fe>_e)return;const s=n.at??V(),o=C.createOscillator(),l=C.createGain();o.type=n.type??"sine",o.frequency.setValueAtTime(t,s),n.slide&&o.frequency.exponentialRampToValueAtTime(Math.max(20,t*n.slide),s+e),n.detune&&(o.detune.value=n.detune),Un(l,s,n.attack??.004,a,e),o.connect(l).connect(Jn(n)),o.start(s),o.stop(s+e+.03),rn(o)}function J(t,e,a){if(!C||!qt()||fe>_e)return;const n=a.at??V(),s=C.createBufferSource();s.buffer=Wn,s.loop=!0;const o=C.createBiquadFilter();o.type=a.type??"lowpass",o.frequency.setValueAtTime(a.f0,n),a.f1&&o.frequency.exponentialRampToValueAtTime(a.f1,n+t),o.Q.value=a.q??.8;const l=C.createGain();Un(l,n,a.attack??.003,e,t),s.connect(o).connect(l).connect(Jn(a)),s.start(n,Math.random()*.9),s.stop(n+t+.03),rn(s)}function U(t,e,a,n={}){if(!C||!qt()||fe>_e)return;const s=n.at??V(),o=C.createOscillator(),l=C.createOscillator(),r=C.createGain(),c=C.createGain();o.frequency.setValueAtTime(t,s),n.slide&&o.frequency.exponentialRampToValueAtTime(Math.max(20,t*n.slide),s+e),l.frequency.setValueAtTime(t*(n.ratio??3.01),s);const f=t*(n.index??1.6);r.gain.setValueAtTime(f,s),r.gain.exponentialRampToValueAtTime(Math.max(.01,f*.04),s+e*.75),l.connect(r).connect(o.frequency),Un(c,s,n.attack??.003,a,e),o.connect(c).connect(Jn(n)),o.start(s),l.start(s),o.stop(s+e+.03),l.stop(s+e+.03),rn(o)}const Ma=new Map;function L(t,e){if(!qt()||!Se||fe>_e-24)return!1;const a=V();return a-(Ma.get(t)??-9)<e?!1:(Ma.set(t,a),!0)}function q(t){return 440*Math.pow(2,(t-69)/12)}let wt=45,te={root:0,minor:!0};function Mt(t){const a=[0,te.minor?3:4,7],n=(t%3+3)%3;return wt+te.root+a[n]+12*Math.floor(t/3)}const Sn={red:0,orange:1,yellow:2,green:3,blue:4,purple:5},_={shot(t,e,a){if(!L("shot",.05))return;const n=Sn[t]??0,s=q(Mt(n%4)+24+(e>=5?12:0)),o=.06;switch(n){case 0:A(s/2,.08,o,{type:"sawtooth",slide:.6,pan:a});break;case 1:A(s,.1,o*1.3,{type:"triangle",pan:a});break;case 2:U(s*2,.09,o*.9,{ratio:2,index:1.2,pan:a});break;case 3:A(s*.75,.09,o*1.3,{type:"sine",slide:1.5,pan:a});break;case 4:A(s*2,.12,o,{type:"sine",slide:.5,pan:a});break;default:U(s/2,.11,o*1.3,{ratio:1.41,index:2.4,pan:a})}},hit(t){L("hit",.045)&&J(.03,.04,{type:"bandpass",f0:2600,q:1.5,pan:t})},crit(t){L("crit",.07)&&(U(q(Mt(2)+36),.22,.1,{ratio:3.5,index:2.2,pan:t}),J(.07,.07,{type:"highpass",f0:5e3,pan:t}))},ward(t){L("ward",.09)&&U(1900,.09,.06,{ratio:5.4,index:1,pan:t})},splash(t){L("splash",.1)&&(J(.2,.1,{f0:1100,f1:180,pan:t}),A(120,.14,.1,{slide:.5,pan:t}))},trap(t,e){if(!L("trap",.09))return;const a=Sn[t]??0;A(q(Mt(a%3)+12),.14,.08,{type:"sawtooth",slide:.45,pan:e}),J(.1,.09,{type:"bandpass",f0:3200,f1:900,q:3,pan:e})},kill(t,e,a){L("kill",.05)&&(A(380-a*300,.09,.11,{type:"triangle",slide:.4,pan:t}),U(q(Mt(Math.min(e,7))+24),.18,.07,{ratio:2,index:.8,pan:t}))},bigKill(t){if(L("bigkill",.2)){A(120,.8,.5,{slide:.28,pan:t}),J(.7,.3,{f0:2400,f1:90,pan:t});for(let e=0;e<4;e++)U(q(Mt(e+3)+24),.5,.04,{at:V()+.05+e*.07,pan:t})}},mana(){L("mana",.07)&&A(q(Mt(4)+36),.06,.03,{slide:1.3})},build(t,e){L("build",.05)&&(A(150,.18,.3,{slide:.33,pan:e}),J(.14,.14,{f0:1400,f1:260,pan:e}),J(.025,.06,{type:"highpass",f0:3500,pan:e}),t&&U(q(Mt(1)+24),.4,.05,{ratio:1.5,index:2,at:V()+.06,pan:e}))},forge(t,e){if(!L("forge",.04))return;const a=Mt(Sn[t]??0)+24,n=V();U(q(a),.5,.1,{ratio:3.01,index:1.4+e*.15}),U(q(a+12),.4,.04,{at:n+.06}),U(q(a+19),.35,.03,{at:n+.12}),J(.05,.03,{type:"highpass",f0:6e3})},forgeBatch(){if(!L("forge",.04))return;const t=V();for(let e=0;e<3;e++)U(q(Mt(e)+24),.45,.08,{at:t+e*.08});J(.3,.03,{type:"bandpass",f0:2500,f1:7e3,q:2})},combine(t){if(!L("combine",.05))return;const e=V();J(.22,.06,{type:"bandpass",f0:400,f1:3600,q:2}),A(220,.2,.05,{type:"triangle",slide:3});const a=Math.min(6,2+Math.floor(t/2));for(let n=0;n<a;n++)U(q(Mt(n)+24),.6+t*.08,.07,{at:e+.19+n*.035,index:1.2+t*.2});t>=4&&A(70,.4,.2,{slide:.5,at:e+.19})},place(){L("place",.04)&&(U(q(Mt(0)+24),.14,.05,{ratio:2,index:.7}),J(.03,.03,{type:"highpass",f0:3e3}))},sell(){if(!L("sell",.05))return;const t=V();[1568,1319,1047].forEach((e,a)=>U(e,.2,.05,{ratio:2.76,index:1.1,at:t+a*.055}))},pool(){if(!L("pool",.1))return;const t=V();A(110,.7,.14,{type:"triangle",slide:2,attack:.25}),J(.7,.07,{type:"bandpass",f0:300,f1:3200,q:1.5,attack:.3});for(let e=0;e<5;e++)U(q(Mt(e)+24),.5,.05,{at:t+.3+e*.06})},wave(t,e){if(!L("wave",.25))return;const a=V(),n=wt+(t?0:12),s=t?1.5:.9;for(const[o,l]of t?[[0,0],[1,.25]]:e?[[0,0],[7,.16],[12,.32]]:[[0,0],[7,.16]])for(const r of[-9,9])A(q(n+o),s-l,.06,{type:"sawtooth",attack:.08,detune:r,at:a+l,dest:Tn()});A(90,.45,.35,{slide:.6}),t&&J(1.5,.22,{f0:160,f1:60,attack:.3})},banish(t){if(!L("banish",.08))return;const e=V();A(190,.5,.45,{slide:.2,pan:t}),J(.3,.2,{type:"bandpass",f0:2400,f1:300,q:1.2,pan:t}),A(660,.22,.045,{type:"square",slide:.5,at:e+.02}),A(660,.22,.035,{type:"square",slide:.5,at:e+.2})},heartbeat(){if(!L("heart",.85))return;const t=V();A(64,.14,.34,{slide:.6}),A(58,.16,.24,{slide:.6,at:t+.17})},overflow(){if(!L("overflow",2.2))return;const t=V();A(2093,.05,.03,{}),A(2093,.05,.03,{at:t+.08})},error(){if(!L("error",.12))return;const t=V();A(160,.09,.05,{type:"square"}),A(115,.12,.05,{type:"square",at:t+.09})},click(){L("click",.03)&&A(880,.035,.035,{slide:.6})},bonus(){if(!L("bonus",.3))return;const t=V();for(let e=0;e<7;e++)U(q(Mt(e)+24),.35,.06,{ratio:2.76,at:t+e*.06})},start(){L("start",.2)&&(J(.6,.07,{type:"bandpass",f0:250,f1:2800,q:1.4,attack:.25}),U(q(wt+24),.9,.07,{ratio:3.5,index:2}))},star(t){!Se||!qt()||U(q(wt+36+[0,4,7][t%3]),.5,.09,{ratio:3.01,index:1.5})},win(){if(!Se||!qt())return;const t=V();[0,4,7,12].forEach((e,a)=>{A(q(wt+24+e),.3,.06,{type:"triangle",at:t+a*.13}),U(q(wt+36+e),.4,.035,{at:t+a*.13})});for(const e of[0,4,7,12,16])for(const a of[-8,8])A(q(wt+12+e),1.9,.016,{type:"sawtooth",attack:.06,detune:a,at:t+.52,dest:Tn()});A(82,.9,.2,{slide:.6,at:t+.52}),J(1.2,.04,{type:"highpass",f0:6e3,at:t+.52})},firework(t){if(!L("firework",.1))return;const e=V();J(.4,.13,{type:"bandpass",f0:1900,f1:500,q:.9,pan:t}),A(150,.25,.15,{slide:.4,pan:t});for(let a=0;a<7;a++)J(.03,.035,{type:"highpass",f0:5e3,at:e+.1+Math.random()*.45,pan:t})},lose(){if(!Se||!qt())return;const t=V();J(.6,.16,{type:"highpass",f0:2200}),A(140,1.2,.25,{slide:.2});for(let e=0;e<12;e++)U(1400+Math.random()*3800,.25,.04,{ratio:4.1,index:1.3,at:t+Math.random()*.7,pan:Math.random()*2-1});for(const e of[-10,10])A(q(wt+12),2.4,.04,{type:"sawtooth",slide:.5,attack:.1,detune:e,dest:Tn()})}};let ee=null;function Tn(){return ee||(ee=C.createBiquadFilter(),ee.type="lowpass",ee.frequency.value=1100,ee.Q.value=1.2,ee.connect(se)),ee}const wa=[[[0,!0],[8,!1],[3,!1],[10,!1]],[[0,!0],[5,!0],[8,!1],[7,!0]],[[0,!0],[10,!1],[8,!1],[10,!1]],[[0,!0],[3,!1],[5,!0],[8,!1]],[[0,!0],[7,!0],[8,!1],[5,!0]],[[0,!0],[8,!1],[5,!0],[7,!1]]],ga=[45,43,47,40,42,38],Qo=[0,3,5,7,10],jo=[0,2,4,2,3,2,4,5,0,2,4,2,6,4,3,2];let Ft={mode:"off",field:0,intensity:0,danger:0,boss:!1,speed:1},Pn="",$n=0,Me=0,En=[],On=-1,ba=-1;function Jo(t){let e=t>>>0;return()=>{e=e+1831565813>>>0;let a=e;return a=Math.imul(a^a>>>15,a|1),a^=a+Math.imul(a^a>>>7,a|61),((a^a>>>14)>>>0)/4294967296}}function va(t,e){const a=[];let n=e;for(let s=0;s<16;s++){if(!(s%4===0)&&t()<.38){a.push(-1);continue}n=Math.max(0,Math.min(9,n+[-2,-1,-1,0,1,1,2][Math.floor(t()*7)])),a.push(n)}return a}function Uo(t){let e=Qo[t%5];return!te.minor&&te.root===7&&e===10&&(e=11),wt+24+e+12*Math.floor(t/5)}function Zo(t,e=.22){if(Ft=t,!qt())return;const a=Ha&&t.mode!=="off",n=a?(t.mode==="menu"?.62:.5)*(t.paused?.6:1):0;n!==On&&(ke.gain.setTargetAtTime(n,V(),a?.25:.12),On=n);const s=t.paused?520:16e3;if(s!==ba&&(ae.frequency.setTargetAtTime(s,V(),.12),ba=s),!a)return;const o=t.mode==="menu"?"menu":`field${t.field}`,l=V();if(o!==Pn){if(Pn){const f=tt;f.out.gain.setTargetAtTime(0,l,.07),Yo(.8,()=>f.out.disconnect()),tt=qa()}Pn=o,$n=0,Me=l+.08,wt=t.mode==="menu"?45:ga[t.field%ga.length];const c=Jo(777+(t.mode==="menu"?99:t.field)*31);En=[va(c,5),va(c,7)]}Me<l-.05&&(Me=l+.06);let r=0;for(;Me<l+e&&r++<4e3;)ts($n,Me),Me+=_a(),$n++}function _a(){return 60/(Ft.mode==="menu"?74:(92+Ft.field*3)*(1+(Ft.speed-1)*.11))/4}function ts(t,e){const a=Ft.mode==="menu",n=t%16,s=Math.floor(t/16),o=a?0:Ft.intensity,l=_a(),r=wa[a?0:Ft.field%wa.length],c=a?2:1;if(n===0&&s%c===0){const d=r[Math.floor(s/c)%r.length];te={root:d[0],minor:d[1]},es(e,l*16*c,750+1700*o,.03+.012*o)}const f=wt+te.root;if(!a&&o>.12&&(o>.65?[0,3,6,8,11,14]:o>.35?[0,6,8,14]:[0,8]).includes(n)){let u=f;for(;u>45;)u-=12;for(;u<33;)u+=12;A(q(u),l*2.6,.14,{type:"triangle",at:e,dest:tt.out}),A(q(u+12),l*2,.05,{type:"sawtooth",at:e,dest:tt.soft})}if(a?n%4===0:o<.3?n%4===0||n===6:o<.6?n%2===0:!0){const d=q(Mt(jo[n])+24),u=(a?.07:.055)*(n%2?.55:1);U(d,a?.6:.26,u,{ratio:2,index:.9,at:e,dest:tt.out}),A(d,.2,u*.7,{type:"triangle",at:e,dest:tt.echo})}if(n%2===0&&(a?s%8>=4:o>.32)){const u=(Math.floor(s/2)%4===2?En[1]:En[0])[s%2*8+n/2];if(u>=0){const w=q(Uo(u)),x=a?.03:.035+.02*o;A(w,l*2.4,x,{type:"triangle",attack:.012,at:e,dest:tt.out}),A(w*2,l*1.6,x*.25,{at:e,dest:tt.echo}),A(w,l*2,x*.5,{type:"sawtooth",attack:.02,at:e,dest:tt.soft})}}if(!a&&o>.25&&((n===0||n===8||o>.6&&n===10||o>.85&&n===14)&&(A(135,.17,.4,{slide:.3,at:e,dest:tt.out}),J(.02,.12,{type:"highpass",f0:2500,at:e,dest:tt.out})),(o>.75||(o>.5?n%2===0:n%4===2))&&J(.04,n%2?.05:.1,{type:"highpass",f0:6500,at:e,dest:tt.out}),o>.45&&(n===4||n===12)&&(J(.13,.16,{type:"bandpass",f0:1900,q:.7,at:e,dest:tt.out}),A(190,.09,.09,{type:"triangle",slide:.6,at:e,dest:tt.out})),o>.6&&s%4===3&&n>=12&&A(190-(n-12)*26,.14,.22,{slide:.6,at:e,dest:tt.out})),!a&&Ft.danger>.4&&n%2===1){const d=n%4===1?19:20;A(q(f+12+d),l*.9,.028*Ft.danger,{type:"sawtooth",at:e,dest:tt.soft})}if(!a&&Ft.boss&&n===0&&s%2===0){A(84,.6,.35,{slide:.6,at:e,dest:tt.out});for(const d of[-7,7])A(q(wt-12),l*32,.05,{type:"sawtooth",attack:.4,detune:d,at:e,dest:tt.soft})}}function es(t,e,a,n){if(!C||!qt()||fe>_e)return;const s=C.createBiquadFilter();s.type="lowpass",s.Q.value=.7,s.frequency.setValueAtTime(a*.6,t),s.frequency.linearRampToValueAtTime(a,t+e*.5),s.frequency.linearRampToValueAtTime(a*.7,t+e);const o=C.createGain();o.gain.value=0,o.gain.setValueAtTime(1e-4,t),o.gain.linearRampToValueAtTime(n,t+Math.min(.5,e*.3)),o.gain.setValueAtTime(n,t+e*.8),o.gain.linearRampToValueAtTime(1e-4,t+e+.35),s.connect(o).connect(tt.out);const l=te.minor?3:4;for(const r of[0,l,7,12]){let c=wt+12+te.root+r;for(;c>wt+26;)c-=12;for(const f of[-8,8]){const h=C.createOscillator();h.type="sawtooth",h.frequency.value=q(c),h.detune.value=f,h.connect(s),h.start(t),h.stop(t+e+.4),rn(h)}}}class ns{constructor(e,a,n={}){Et(this,"context");Et(this,"output");Et(this,"session","");Et(this,"pending",!1);Et(this,"blocked",!1);Et(this,"disposed",!1);Et(this,"muted",!1);Et(this,"volume",1);Et(this,"stopped",!1);Et(this,"listener");this.media=e,this.title=a,this.options=n}activate(){var e,a;if(!this.media||this.disposed||this.blocked)return!1;try{return this.context||(this.context=((a=(e=this.options).createContext)==null?void 0:a.call(e))??new AudioContext,this.output=this.context.createGain(),this.output.gain.value=0,this.output.connect(this.context.destination),this.context.onstatechange=()=>this.report()),!this.session&&!this.pending&&(this.pending=!0,this.register()),this.context.state!=="running"&&this.resume(),this.audible}catch{return this.dispose(),!1}}get audible(){var e;return!!this.session&&!this.disposed&&!this.blocked&&!this.muted&&((e=this.context)==null?void 0:e.state)==="running"}setMuted(e){this.muted=e,this.applyGain(),this.report()}applyGain(){this.output&&(this.output.gain.value=this.audible?this.volume:0)}report(){var e,a;if(this.applyGain(),(a=(e=this.options).onChange)==null||a.call(e),!(!this.session||this.disposed))try{this.media.reportState(this.session,{status:this.audible?"playing":this.stopped?"stopped":"paused",volume:this.muted?0:this.volume})}catch{this.dispose()}}async resume(){var e;try{await((e=this.context)==null?void 0:e.resume()),this.report()}catch{this.applyGain()}}async register(){try{const e=await this.media.createSession({owner:"napplet",metadata:{title:this.title,mediaType:"audio"},capabilities:["play","pause","stop","volume"]});if(this.disposed){e.sessionId&&this.media.destroySession(e.sessionId);return}if(e.error||!e.sessionId||e.owner!=="napplet"){e.sessionId&&this.media.destroySession(e.sessionId),this.dispose();return}this.session=e.sessionId,this.listener=this.media.onCommand(this.session,(a,n)=>{var s;this.disposed||(a==="pause"||a==="stop"?(this.blocked=!0,this.stopped=a==="stop",this.applyGain(),(s=this.context)==null||s.suspend().then(()=>this.report()).catch(()=>{})):a==="play"?(this.blocked=!1,this.stopped=!1,this.resume()):a==="volume"&&typeof n=="number"&&Number.isFinite(n)&&(this.volume=Math.max(0,Math.min(1,n))),this.report())}),this.report()}catch{this.dispose()}finally{this.pending=!1}}dispose(){var e;if(!this.disposed){this.disposed=!0,this.applyGain();try{(e=this.listener)==null||e.close()}catch{}if(this.context&&(this.context.onstatechange=null,this.context.close().catch(()=>{})),this.session)try{this.media.destroySession(this.session)}catch{}this.session=""}}}const v=0,gt=1,Zn=0,Va=1,Ka=2,La=3,ta=4,Je=5,Ya=6,Xa=7,Qa=8;let vt=[],be=[],Ut=0,ja=0,Ja=0,Ua="#fff",he=0,Gn=!1;function Be(t){Gn=!0;try{t()}finally{Gn=!1}}let Qt=1,de=!1;function cn(t,e){Qt=t,de=e}function fn(t){return Math.max(1,Math.round(t*Qt))}function as(){vt=[],be=[],Ut=0,he=0}function Kt(t){const e={vx:0,vy:0,max:t.life,delay:0,gravity:0,drag:1,rot:0,spin:0,r1:0,tx:0,ty:0,real:Gn,...t};return e.max=e.life,vt.push(e),e}function yt(t,e,a,n,s,o){for(let l=0;l<fn(n);l++){const r=o.angle===void 0?Math.random()*Math.PI*2:o.angle+(Math.random()-.5)*(o.spread??1),c=o.speed*(.35+Math.random()*.9),f=o.radius??0;Kt({kind:Zn,space:t,x:e+Math.cos(r)*f,y:a+Math.sin(r)*f,vx:Math.cos(r)*c,vy:Math.sin(r)*c,life:o.life*(.6+Math.random()*.7),color:s,size:o.size*(.6+Math.random()*.8),gravity:o.gravity??0,drag:o.drag??.93,delay:o.delay??0})}}function hn(t,e,a,n,s,o,l,r){for(let c=0;c<fn(n);c++){const f=Math.random()*Math.PI*2,h=o*(.7+Math.random()*.5);Kt({kind:Zn,space:t,x:e+Math.cos(f)*h,y:a+Math.sin(f)*h,vx:-Math.cos(f)*h/l,vy:-Math.sin(f)*h/l,life:l,color:s,size:r,drag:1})}}function ie(t,e,a,n,s,o){for(let l=0;l<fn(n);l++){const r=Math.random()*Math.PI*2,c=o.speed*(.3+Math.random());Kt({kind:Ka,space:t,x:e,y:a,vx:Math.cos(r)*c,vy:Math.sin(r)*c-o.speed*.3,life:o.life*(.6+Math.random()*.7),color:s,size:o.size*(.5+Math.random()),gravity:o.gravity??0,drag:o.drag??.95,rot:Math.random()*6.28,spin:(Math.random()-.5)*16})}}function $e(t,e,a,n,s,o,l=0){Kt({kind:Va,space:t,x:e,y:a,life:s,color:o,size:n,delay:l})}function ot(t,e,a,n,s,o,l,r,c=0){Kt({kind:La,space:t,x:e,y:a,life:o,color:l,size:n,r1:s,tx:r,delay:c})}function An(t,e,a,n,s,o,l,r){Kt({kind:ta,space:t,x:e,y:a,vy:r,life:l,color:s,size:o,text:n})}function Xe(t,e,a,n,s,o,l,r,c,f=0){if(vt.length>900){c==null||c();return}const h=Math.random()*Math.PI*2;Kt({kind:Je,space:t,x:e,y:a,vx:Math.cos(h)*r,vy:Math.sin(h)*r,tx:n,ty:s,life:2.2,color:o,size:l,arrive:c,delay:f})}function ka(t,e,a,n,s,o,l,r){Kt({kind:Ya,space:t,x:e,y:a,tx:n,ty:s,life:l,color:o,size:r})}function Za(t,e,a,n,s,o,l){Kt({kind:Xa,space:t,x:e,y:a,vx:(Math.random()-.5)*.3,vy:l,life:o,color:n,size:s})}function dn(t,e,a,n,s,o,l=0){Kt({kind:Qa,space:t,x:e,y:a,life:s,color:o,size:n,rot:Math.random()*.6,spin:1.5,delay:l})}function Oe(t){de||(Ut=Math.min(.6,Math.max(Ut,t)))}function Ue(t,e){de||e>=he&&(Ua=t,he=e)}function to(t,e){be.push({t,fn:e})}function os(t,e){var s;if(Ut*=Math.exp(-e*8),Ut<.004&&(Ut=0),ja=(Math.random()*2-1)*Ut,Ja=(Math.random()*2-1)*Ut,he=Math.max(0,he-e*2.6),be.length){const o=be.filter(l=>(l.t-=e)<=0);be=be.filter(l=>l.t>0);for(const l of o)l.fn()}let a=0;for(const o of vt){const l=o.space===v&&!o.real?t:e;if(l<=0){vt[a++]=o;continue}if(o.delay>0){o.delay-=l,vt[a++]=o;continue}if(o.life-=l,o.kind===Je){const c=o.tx-o.x,f=o.ty-o.y,h=Math.hypot(c,f)||.001,d=o.max-o.life,u=(o.space===v?5:260)*(.4+d*5),w=Math.min(1,l*(2+d*14));o.vx+=(c/h*u-o.vx)*w,o.vy+=(f/h*u-o.vy)*w;const x=Math.hypot(o.vx,o.vy)*l;if(h<=Math.max(x,o.space===v?.2:8)||o.life<=0){(s=o.arrive)==null||s.call(o);continue}}else if(o.life<=0)continue;const r=Math.pow(o.drag,l*60);o.vx*=r,o.vy=o.vy*r+o.gravity*l,o.x+=o.vx*l,o.y+=o.vy*l,o.rot+=o.spin*l,vt[a++]=o}vt.length=a;const n=Math.round(300+700*Qt);if(vt.length>n){let o=vt.length-n;vt=vt.filter(l=>l.kind===ta||l.kind===Je||o--<=0)}}const Ye=new Map;function ss(t){let e=Ye.get(t);if(e)return e;Ye.size>120&&Ye.clear();const a=64;e=document.createElement("canvas"),e.width=a,e.height=a;const n=e.getContext("2d"),s=n.createRadialGradient(a/2,a/2,0,a/2,a/2,a/2);s.addColorStop(0,"rgba(255,255,255,1)"),s.addColorStop(.3,"rgba(255,255,255,0.5)"),s.addColorStop(.6,"rgba(255,255,255,0.14)"),s.addColorStop(1,"rgba(255,255,255,0)"),n.fillStyle=s,n.fillRect(0,0,a,a),n.globalCompositeOperation="source-in",n.fillStyle=t,n.fillRect(0,0,a,a),n.globalCompositeOperation="source-over";const o=n.createRadialGradient(a/2,a/2,0,a/2,a/2,a*.2);return o.addColorStop(0,"rgba(255,255,255,0.85)"),o.addColorStop(1,"rgba(255,255,255,0)"),n.fillStyle=o,n.fillRect(0,0,a,a),Ye.set(t,e),e}function X(t,e,a,n,s,o){if(o<=.003||n<=.3)return;const l=t.globalCompositeOperation,r=t.globalAlpha;t.globalCompositeOperation="lighter",t.globalAlpha=Math.min(1,o)*r,t.drawImage(ss(s),e-n,a-n,n*2,n*2),t.globalAlpha=r,t.globalCompositeOperation=l}function eo(t,e,a,n,s,o,l=0){const r=Math.sin(Math.PI*Math.max(0,Math.min(1,s)));if(r<=.02)return;const c=t.globalCompositeOperation;t.save(),t.globalCompositeOperation="lighter",t.translate(e,a),t.rotate(l),t.globalAlpha*=r,t.fillStyle=o;const f=n*r,h=f*.09;t.beginPath(),t.moveTo(0,-f),t.lineTo(h,-h),t.lineTo(f,0),t.lineTo(h,h),t.lineTo(0,f),t.lineTo(-h,h),t.lineTo(-f,0),t.lineTo(-h,-h),t.closePath(),t.fill(),t.restore(),t.globalCompositeOperation=c}function is(t,e,a,n,s,o,l,r){const c=n-e,f=s-a,h=Math.hypot(c,f);if(h<1)return;const d=-f/h,u=c/h,w=Math.max(3,Math.min(9,Math.round(h/14)));t.beginPath(),t.moveTo(e,a);for(let I=1;I<w;I++){const S=I/w,B=(Math.random()-.5)*h*.22*Math.sin(Math.PI*S);t.lineTo(e+c*S+d*B,a+f*S+u*B)}t.lineTo(n,s);const x=t.globalCompositeOperation,g=t.globalAlpha;t.globalCompositeOperation="lighter",t.lineJoin="round",t.lineCap="round",t.globalAlpha=g*r*.55,t.strokeStyle=o,t.lineWidth=l*3,t.stroke(),t.globalAlpha=g*r,t.strokeStyle="#ffffff",t.lineWidth=Math.max(1,l),t.stroke(),t.globalAlpha=g,t.globalCompositeOperation=x}function ls(){return[ja,Ja]}function rs(){return he>.01?{color:Ua,alpha:he}:null}function Dn(t,e,a){const n=t.ctx,s=t.unit;n.save(),n.lineCap="round";for(const o of vt){if(o.space!==e||o.delay>0)continue;const l=Math.max(0,Math.min(1,o.life/o.max)),r=t.ox+o.x*s,c=t.oy+o.y*s;switch(o.kind){case Zn:{n.globalCompositeOperation="lighter",n.globalAlpha=l,n.strokeStyle=o.color,n.lineWidth=Math.max(1,o.size*s*(.4+.6*l));const f=.045*s;n.beginPath(),n.moveTo(r,c),n.lineTo(r-o.vx*f-.01,c-o.vy*f),n.stroke();break}case Va:n.globalAlpha=1,X(n,r,c,o.size*s*(.5+.9*(1-l)),o.color,l*l);break;case Ka:{n.globalCompositeOperation="source-over",n.globalAlpha=Math.min(1,l*1.6),n.fillStyle=o.color;const f=Math.max(1.2,o.size*s),h=Math.cos(o.rot),d=Math.sin(o.rot);n.beginPath(),n.moveTo(r+h*f,c+d*f),n.lineTo(r-h*f*.6-d*f*.7,c-d*f*.6+h*f*.7),n.lineTo(r-h*f*.6+d*f*.5,c-d*f*.6-h*f*.5),n.closePath(),n.fill();break}case La:{const f=1-l*l*l;n.globalCompositeOperation="lighter",n.globalAlpha=l,n.strokeStyle=o.color,n.lineWidth=Math.max(1,o.tx*s*l),n.beginPath(),n.arc(r,c,Math.max(.5,(o.size+(o.r1-o.size)*f)*s),0,Math.PI*2),n.stroke();break}case ta:{n.globalCompositeOperation="source-over",n.globalAlpha=Math.min(1,l*2.2);const f=1+.6*Math.max(0,(l-.82)/.18);n.font=`800 ${o.size*s*f}px ${a}`,n.textAlign="center",n.textBaseline="middle",n.lineWidth=3,n.strokeStyle="#000",n.strokeText(o.text,r,c),n.fillStyle=o.color,n.fillText(o.text,r,c);break}case Je:{n.globalCompositeOperation="lighter",n.globalAlpha=.7,n.strokeStyle=o.color,n.lineWidth=Math.max(1,o.size*s*.45),n.beginPath(),n.moveTo(r,c),n.lineTo(r-o.vx*.05*s,c-o.vy*.05*s),n.stroke(),n.globalAlpha=1,X(n,r,c,o.size*s*1.6,o.color,.95);break}case Ya:n.globalAlpha=1,is(n,r,c,t.ox+o.tx*s,t.oy+o.ty*s,o.color,Math.max(1,o.size*s),l);break;case Xa:{n.globalCompositeOperation="source-over",n.globalAlpha=l*.85,n.strokeStyle=o.color,n.lineWidth=1,n.beginPath(),n.arc(r,c,Math.max(1,o.size*s*(.5+.7*(1-l))),0,Math.PI*2),n.stroke();break}case Qa:n.globalAlpha=1,eo(n,r,c,o.size*s,1-l,o.color,o.rot);break}}n.restore()}const Ct=22,Yt=14,Rt=["red","orange","yellow","green","blue","purple"],ue={red:{name:"Ruby",hex:"#ff4a4a",special:"Splash damage around the target",short:"Splash"},orange:{name:"Amber",hex:"#ff9a2e",special:"Leeches mana on every hit",short:"Mana leech"},yellow:{name:"Topaz",hex:"#ffe14d",special:"Critical hits",short:"Crits"},green:{name:"Jade",hex:"#4fe06a",special:"Poison damage over time",short:"Poison"},blue:{name:"Sapphire",hex:"#4aa8ff",special:"Slows monsters",short:"Slow"},purple:{name:"Amethyst",hex:"#c06bff",special:"Tears away armor",short:"Armor tear"}},no={crawler:{name:"Crawler",trait:"Plain and steady",counter:"Anything"},swarm:{name:"Swarm",trait:"Many small ones, close together",counter:"Ruby splash"},brute:{name:"Brute",trait:"Heavy armour blunts every hit",counter:"Amethyst, jade"},giant:{name:"Ice giant",trait:"Huge health, slow, cannot be slowed",counter:"Topaz crits, big gems"},runner:{name:"Runner",trait:"Twice as fast",counter:"Sapphire slow"},warded:{name:"Warded",trait:"Each ward charge eats one hit",counter:"Ruby splash, traps"},mender:{name:"Mender",trait:"Heals itself unless poisoned",counter:"Jade poison"}},Vt=[{name:"Mossy Hollow",path:[[0,2],[6,2],[6,11],[14,11],[14,4],[19,4],[19,9]],waves:10,colors:["red","yellow"],rocks:[[2,7],[10,6],[17,12],[21,1],[11,1]]},{name:"Ravine of Echoes",path:[[0,7],[4,7],[4,1],[10,1],[10,12],[16,12],[16,6],[20,6]],waves:12,colors:["red","yellow","blue"],rocks:[[1,2],[7,5],[13,3],[19,11],[7,10]]},{name:"Switchback Pass",path:[[0,1],[18,1],[18,4],[3,4],[3,8],[18,8],[18,12],[10,12]],waves:14,colors:["red","yellow","blue","green"],rocks:[[20,6],[1,11],[8,10],[13,6],[21,13]]},{name:"Coiled Serpent",path:[[0,1],[20,1],[20,12],[2,12],[2,4],[16,4],[16,9],[7,9]],waves:16,colors:["red","yellow","green","blue","purple"],rocks:[[10,6],[12,7],[5,7],[18,7],[21,13]]},{name:"Crossroads",path:[[0,6],[15,6],[15,1],[8,1],[8,12],[19,12],[19,3]],waves:18,colors:[...Rt],rocks:[[3,2],[3,10],[12,9],[21,7],[17,8]]},{name:"The Gauntlet",path:[[0,10],[9,10],[9,3],[17,3],[17,8],[20,8]],waves:22,colors:[...Rt],rocks:[[4,5],[13,6],[13,12],[20,1],[2,13]]}],ao=1.15,oo=12,cs=5,fs=1,hs=1,ds=24,us=40,Ze=320,ps=2;function Ve(t){let e=t>>>0;return()=>{e=e+1831565813>>>0;let a=e;return a=Math.imul(a^a>>>15,a|1),a^=a+Math.imul(a^a>>>7,a|61),((a^a>>>14)>>>0)/4294967296}}function Fn(t,e){return`${t},${e}`}function ms(t){const e=new Set;let a=0;for(let n=0;n<t.length-1;n++){const[s,o]=t[n],[l,r]=t[n+1],c=Math.sign(l-s),f=Math.sign(r-o);let h=s,d=o;for(e.add(Fn(h,d));h!==l||d!==r;)h+=c,d+=f,e.add(Fn(h,d));a+=Math.abs(l-s)+Math.abs(r-o)}return{tiles:e,length:a}}function Ke(t,e){let a=Math.max(0,e);const n=t.pathPoints;for(let o=0;o<n.length-1;o++){const[l,r]=n[o],[c,f]=n[o+1],h=Math.abs(c-l)+Math.abs(f-r);if(a<=h){const d=h===0?0:a/h;return[l+(c-l)*d+.5,r+(f-r)*d+.5]}a-=h}const s=n[n.length-1];return[s[0]+.5,s[1]+.5]}const ys=[1,.7,1,1.5,1.6,.8];function xs(t,e,a){const n=e+a*2.5,s=9*Math.pow(1.185,n)*ys[a],o=Math.floor(e*.3+a*.8+Math.max(0,s*.03-4)),l=s*(a>=2?.08:.04);switch(t){case"swarm":return{type:t,count:18+Math.floor(e*.8),hp:s*.38,armor:Math.floor(o*.5),speed:1.45,interval:.28};case"brute":return{type:t,count:7+Math.floor(e/4),hp:s*1.2,armor:Math.floor(o+2+l),speed:.8,interval:.9};case"giant":return{type:t,count:2+Math.floor((e+a)/6),hp:s*5.5,armor:Math.floor(o*1.5)+1,speed:.55,interval:2.2};case"runner":return{type:t,count:10+Math.floor(e/3),hp:s*.8,armor:Math.floor(o*.5),speed:2.1,interval:.55};case"warded":return{type:t,count:7+Math.floor(e/4),hp:s*.8,armor:o,speed:.9,interval:.8,ward:2+Math.floor(n/4)};case"mender":return{type:t,count:8+Math.floor(e/4),hp:s*1.3,armor:o,speed:.9,interval:.8,regen:.04};default:return{type:t,count:10+Math.floor(e/3),hp:s,armor:o,speed:1,interval:.6}}}function Ms(t,e){const a=Ve(1e3+t*77),n=[],s=["swarm","brute"];t>=1&&s.push("runner","warded"),t>=2&&s.push("mender"),t>=3&&s.push("brute","warded");let o="crawler";for(let l=0;l<e;l++){let r="crawler";if(l>0&&(l+1)%5===0)r="giant";else if(l>=2){const c=s.filter(f=>f!==o&&(f!=="brute"||t>0||l>=5));r=a()<.2?"crawler":c[Math.floor(a()*c.length)]??"crawler"}o=r,n.push(xs(r,l,t))}return n}function ws(t){const e=Vt[t],{tiles:a,length:n}=ms(e.path),s=e.path[e.path.length-1];return{fieldIndex:t,field:e,pathTiles:a,pathPoints:e.path,pathLength:n,orb:s,mana:560+t*320,manaCap:900+t*400,poolLevel:0,manaMult:1,structures:[],inventory:new Array(oo).fill(null),monsters:[],shots:[],waves:Ms(t,e.waves),nextWave:0,waveTimer:us,spawnQueue:[],bonusPot:0,time:0,banished:0,built:0,state:"playing",events:[],nextId:1,rng:Ve(4242+t),manaGained:0,furthest:0,killedHp:0,killPoints:0,harvested:0,buildMana:0,wasted:0}}function zt(t){return Math.floor(Math.log2(t.mass)+1e-9)+1}function zn(t){return t.mass/Math.pow(2,zt(t)-1)-1}function gs(t){return Math.log2(t.mass)+1+ps}function Ae(t){return Ze*Math.pow(2,t-1)}function ea(t){return Math.round(Ze*t.mass*.7)}function Bn(t){return 90+30*t.structures.filter(a=>a.kind==="tower").length}function Nn(t){return 70+25*t.structures.filter(a=>a.kind==="trap").length}const so=Math.sqrt(1.6);function Hn(t){return Math.round(t.manaCap*.6/(1+so))}function _t(t){let e=0,a=0,n=0,s=0;for(const l of Rt){const r=t.comp[l]??0;if(!r)continue;const c=ue[l].hex;e+=parseInt(c.slice(1,3),16)*r,a+=parseInt(c.slice(3,5),16)*r,n+=parseInt(c.slice(5,7),16)*r,s+=r}const o=l=>Math.round(l/s).toString(16).padStart(2,"0");return`#${o(e)}${o(a)}${o(n)}`}function un(t){let e="red",a=-1;for(const n of Rt){const s=t.comp[n]??0;s>a&&(a=s,e=n)}return e}function io(t){let e=0;for(const a of Rt)e+=(t.comp[a]??0)**2;return e}function pn(t,e){const a=gs(t),n=io(t),s=.5+.5*n,o=h=>(t.comp[h]??0)*s,l=e==="trap",r=l?hs:1,c=4*Math.pow(1.85,a-1);return{damage:c*(l?.45:1)*(.8+.2*n),range:l?.85:Math.min(5.2,2.7+.16*a),rate:(1+.07*a)*(l?fs:1),splash:l?0:Math.min(.9,o("red")*(.35+.04*a)),leech:o("orange")*(.6+.55*a)*r,crit:Math.min(.6,o("yellow")*(.12+.015*a)*r),critMult:2+.12*a,poison:c*o("green")*(.18+.03*a)*(l?1.3:1),slow:Math.min(.75,o("blue")*(.25+.025*a)*r),tear:c*o("purple")*.3*r}}function Le(t){return t.nextId++}function mn(t,e,a){return t.mana<e?(t.events.push({type:"error",message:`Not enough mana for ${a}`}),!1):(t.mana-=e,!0)}function tn(t,e){const a=e*t.manaMult;t.manaGained+=a;const n=Math.max(0,t.manaCap-t.mana);t.harvested+=Math.min(a,n),t.wasted+=Math.max(0,a-n),t.mana=Math.min(t.manaCap,t.mana+a)}function lo(t){return t.inventory.findIndex(e=>e===null)}function bs(t,e,a){if(!t.field.colors.includes(e))return-1;const n=lo(t);return n<0?(t.events.push({type:"error",message:"Inventory is full"}),-1):mn(t,Ae(a),"that gem")?(t.inventory[n]={id:Le(t),mass:Math.pow(2,a-1),comp:{[e]:1},kills:0},n):-1}const le=3;function na(t){return Ae(t)*2.2}function vs(t,e){if(t.inventory.filter(n=>n===null).length<le)return t.events.push({type:"error",message:`Random batch needs ${le} free slots`}),[];if(!mn(t,na(e),"a random batch"))return[];const a=[];for(let n=0;n<le;n++){const s=t.field.colors[Math.floor(t.rng()*t.field.colors.length)],o=lo(t);t.inventory[o]={id:Le(t),mass:Math.pow(2,e-1),comp:{[s]:1},kills:0},a.push(o)}return a}function aa(t,e,a,n){if(a<0||n<0||a>=Ct||n>=Yt||t.structures.some(r=>r.x===a&&r.y===n)||a===t.orb[0]&&n===t.orb[1])return!1;const[s,o]=t.pathPoints[0];if(a===s&&n===o||t.field.rocks.some(([r,c])=>r===a&&c===n))return!1;const l=t.pathTiles.has(Fn(a,n));return e==="trap"?l:!l}function ks(t,e,a,n){if(!aa(t,e,a,n))return null;const s=e==="tower"?Bn(t):Nn(t);if(!mn(t,s,`a ${e}`))return null;const o={id:Le(t),kind:e,x:a,y:n,gem:null,cooldown:0,aim:-Math.PI/2};return t.structures.push(o),t.built++,t.buildMana+=s,t.events.push({type:"build",x:a+.5,y:n+.5,kind:e,id:o.id}),o}function Ss(t){return mn(t,Hn(t),"expanding the pool")?(t.poolLevel++,t.manaCap=Math.round(t.manaCap*so),t.manaMult=1+.075*t.poolLevel,!0):!1}function $t(t,e){var a;return e.kind==="inv"?t.inventory[e.index]??null:((a=t.structures.find(n=>n.id===e.id))==null?void 0:a.gem)??null}function Ge(t,e,a){if(e.kind==="inv")t.inventory[e.index]=a;else{const n=t.structures.find(s=>s.id===e.id);n&&(n.gem=a,n.cooldown=Math.max(n.cooldown,.4))}}function Fe(t,e){return t.kind===e.kind&&(t.kind==="inv"?t.index===e.index:t.id===e.id)}function ro(t,e,a){const n=t.mass+e.mass,s={};for(const o of Rt){const l=((t.comp[o]??0)*t.mass+(e.comp[o]??0)*e.mass)/n;l>.001&&(s[o]=l)}return{id:a,mass:n,comp:s,kills:t.kills+e.kills}}function Ts(t,e,a){if(Fe(e,a))return!1;const n=$t(t,e);if(!n||a.kind==="struct"&&!t.structures.some(l=>l.id===a.id))return!1;const s=$t(t,a);if(!s)return Ge(t,e,null),Ge(t,a,n),!0;const o=ro(n,s,Le(t));return Ge(t,e,null),Ge(t,a,o),t.events.push({type:"combine",grade:zt(o)}),!0}function Ps(t,e){const a=$t(t,e);return a?(Ge(t,e,null),t.mana=Math.min(t.manaCap,t.mana+ea(a)),!0):!1}function co(t){return t.nextWave>=t.waves.length?0:Math.round(t.waveTimer*(1.5+t.nextWave*.6+t.fieldIndex))}function fo(t){if(t.nextWave>=t.waves.length||t.state!=="playing")return 0;const e=co(t);return t.bonusPot+=e,ho(t),e}function ho(t){const e=t.waves[t.nextWave];t.spawnQueue.push({def:e,left:e.count,timer:0,wave:t.nextWave}),t.events.push({type:"wave",index:t.nextWave,monster:e.type,count:e.count}),t.nextWave++,t.waveTimer=ds}function $s(t,e,a){const n=e.hp*(.92+t.rng()*.16),[s,o]=Ke(t,0);t.events.push({type:"spawn",monster:e.type}),t.monsters.push({id:Le(t),type:e.type,dist:0,walked:0,hp:n,maxHp:n,armor:e.armor,speed:e.speed*(.95+t.rng()*.1),slowF:0,slowT:0,poisonDps:0,poisonT:0,ward:e.ward??0,regen:(e.regen??0)*n,mana:4+n*.3+e.armor*.1+(e.ward??0)*2,wobble:t.rng()*Math.PI*2,x:s,y:o,wave:a})}function uo(t,e){const a=e.maxHp*sa(t,e.walked);t.killedHp+=e.maxHp,t.killPoints+=a,tn(t,e.mana),t.events.push({type:"kill",x:e.x,y:e.y,mana:e.mana*t.manaMult,points:a,monster:e.type})}function Sa(t,e,a,n){if(e.hp<=0)return;if(e.ward>0){e.ward--;return}const s=Math.max(a*.15,a-e.armor);e.hp-=s,e.hp<=0&&(n!=null&&n.gem&&n.gem.kills++,uo(t,e))}function Ta(t,e,a){const n=e.gem;if(!n||a.hp<=0)return;const s=pn(n,e.kind),o={type:"hit",x:a.x,y:a.y,id:a.id,sx:e.x+.5,sy:e.y+.5,trap:e.kind==="trap",gem:un(n)};if(a.ward>0){a.ward--,t.events.push({...o,color:"#cfd8ff",crit:!1,ward:!0,splash:!1,slow:!1,poison:!1,leech:!1,tear:!1});return}let l=s.damage*(.8+t.rng()*.4);const r=t.rng()<s.crit;if(r&&(l*=s.critMult),s.tear>0&&(a.armor=Math.max(0,a.armor-s.tear)),s.slow>0&&a.type!=="giant"&&(a.slowF=Math.max(a.slowF,s.slow),a.slowT=e.kind==="trap"?5:2.5),s.poison>0&&(a.poisonDps=Math.max(a.poisonDps,s.poison),a.poisonT=e.kind==="trap"?6:4),s.leech>0&&tn(t,s.leech),Sa(t,a,l,e),s.splash>0)for(const c of t.monsters){if(c===a||c.hp<=0)continue;Math.hypot(c.x-a.x,c.y-a.y)<ao&&Sa(t,c,l*s.splash,e)}t.events.push({...o,color:_t(n),crit:r,ward:!1,splash:s.splash>0,slow:s.slow>0&&a.type!=="giant",poison:s.poison>0,leech:s.leech>0,tear:s.tear>0})}function oa(t,e,a=0){return Math.round(e.maxHp*.6+10+(t.banished+a)*2)}function As(t,e){if(t.state!=="playing")return;t.time+=e,t.nextWave<t.waves.length&&(t.waveTimer-=e,t.waveTimer<=0&&ho(t));for(const n of t.spawnQueue)for(n.timer-=e;n.left>0&&n.timer<=0;)$s(t,n.def,n.wave),n.left--,n.timer+=n.def.interval;t.spawnQueue=t.spawnQueue.filter(n=>n.left>0),tn(t,e*(1+t.fieldIndex*.4));for(const n of t.monsters){if(n.hp<=0)continue;if(n.poisonT>0){if(n.poisonT-=e,n.hp-=n.poisonDps*e,n.poisonT<=0&&(n.poisonDps=0),n.hp<=0){uo(t,n);continue}}else n.regen>0&&(n.hp=Math.min(n.maxHp,n.hp+n.regen*e));n.slowT>0&&(n.slowT-=e,n.slowT<=0&&(n.slowF=0));const s=n.speed*(1-n.slowF)*e;if(n.dist+=s,n.walked+=s,n.walked>t.furthest&&(t.furthest=n.walked),n.dist>=t.pathLength){const o=oa(t,n);if(t.mana-=o,t.banished++,t.events.push({type:"banish",x:n.x,y:n.y,cost:o,monster:n.type}),t.mana<0){t.mana=0,t.state="lost";return}n.dist=0}[n.x,n.y]=Ke(t,n.dist)}t.monsters=t.monsters.filter(n=>n.hp>0);for(const n of t.structures){if(!n.gem||(n.cooldown-=e,n.cooldown>0))continue;const s=pn(n.gem,n.kind),o=n.x+.5,l=n.y+.5;let r=null;for(const c of t.monsters)c.hp<=0||Math.hypot(c.x-o,c.y-l)>s.range||(!r||c.dist>r.dist)&&(r=c);if(r)if(n.cooldown=1/s.rate,n.aim=Math.atan2(r.y-l,r.x-o),n.kind==="trap"){const c=t.monsters.filter(f=>f.hp>0&&Math.hypot(f.x-o,f.y-l)<=s.range);c.sort((f,h)=>h.dist-f.dist);for(const f of c.slice(0,cs))Ta(t,n,f)}else{const c=_t(n.gem);t.shots.push({from:n,target:r,x:o,y:l,color:c}),t.events.push({type:"shot",x:o,y:l,color:c,gem:un(n.gem),grade:zt(n.gem)})}}const a=14;t.shots=t.shots.filter(n=>{if(n.target.hp<=0||!t.monsters.includes(n.target))return!1;const s=n.target.x-n.x,o=n.target.y-n.y,l=Math.hypot(s,o);return l<a*e?(Ta(t,n.from,n.target),!1):(n.x+=s/l*a*e,n.y+=o/l*a*e,!0)}),t.monsters=t.monsters.filter(n=>n.hp>0),t.bonusPot>0&&t.spawnQueue.length===0&&t.monsters.length===0&&(t.events.push({type:"bonus",mana:t.bonusPot*t.manaMult}),tn(t,t.bonusPot),t.bonusPot=0),t.nextWave>=t.waves.length&&t.spawnQueue.length===0&&t.monsters.length===0?t.state="won":t.nextWave>0&&t.spawnQueue.length===0&&t.monsters.length===0&&fo(t)}function po(t){if(t.state!=="won")return 0;let e=1;return t.banished===0&&e++,t.built<=5&&e++,e}const Cs=60,Rs=2,Pa=new Map;function Is(t){let e=Pa.get(t.fieldIndex);if(!e){e={hp:0,mana:0,count:0};for(const a of t.waves)e.count+=a.count,e.hp+=a.count*a.hp,e.mana+=a.count*(4+a.hp*.3+a.armor*.1+(a.ward??0)*2);Pa.set(t.fieldIndex,e)}return e}function Ws(t,e){return t<=0||e<=0?1:t/e}function Es(t,e){const a=t.pathLength;return e<=a?Math.max(0,(e-1)/(a-1)):1+(e-a)/a}function sa(t,e){return Math.pow(10,1-Es(t,e))}const Os=1e3;function Gs(t){let e=0;for(const a of t.inventory)a&&(e+=Ze*a.mass);for(const a of t.structures)a.gem&&(e+=Ze*a.gem.mass);return e}function yn(t,e,a){const n=Is(t),s=t.killedHp,o=s>0?t.killPoints/s:1,l=Gs(t),r=Rs*(l+t.buildMana)/n.mana,c=1+Math.max(0,15-t.built)**2/Cs,f=Ws(t.time,e),h=a?2:1,d=sa(t,t.furthest),u=Os*t.waves.length*d,w=Math.round(s*o*r*c*f*h+u);return{base:s,distance:o,forge:r,gems:l,domination:c,focus:f,noPause:h,early:u,earlyMult:d,fieldMana:n.mana,total:w}}const p={night:"#0b0f1a",earth:"#141b2b",earth2:"#18213a",path:"#3a3346",pathEdge:"#241f30",stone:"#5b5470",stoneHi:"#8c84a6",rock:"#2a2f45",panel:"#10131f",panelEdge:"#2b2f48",gold:"#f2c66d",goldDim:"#9c8350",text:"#e9e4f5",textDim:"#9a93b5",mana:"#47b8ff",manaDeep:"#1d4f87",danger:"#ff5a6e",ok:"#7cf0a0"},xt='ui-rounded, "Trebuchet MS", system-ui, sans-serif',St=document.querySelector("#game");if(!St)throw new Error("Missing #game canvas");const i=St.getContext("2d");let N=0,z=0,Bt=1;function mo(){Bt=Math.min(2,window.devicePixelRatio||1),N=Math.max(1,window.innerWidth),z=Math.max(1,window.innerHeight),St.width=Math.round(N*Bt),St.height=Math.round(z*Bt),St.style.width=`${N}px`,St.style.height=`${z}px`,sn=""}window.addEventListener("resize",mo);const yo=5;let H={stars:Vt.map(()=>0),best:Vt.map(()=>0),scoring:yo,sound:!0,music:!0};const xo="gem-warden-progress";async function Ds(){if(ln(Qn))try{const t=await wo(Na.getItem(xo),3e3);if(!t)return;const e=JSON.parse(t);Array.isArray(e.stars)&&(H.stars=Vt.map((a,n)=>{var s;return Math.max(0,Math.min(3,Number((s=e.stars)==null?void 0:s[n])||0))})),Array.isArray(e.best)&&e.scoring===yo&&(H.best=Vt.map((a,n)=>{var s;return Math.max(0,Math.round(Number((s=e.best)==null?void 0:s[n])||0))})),typeof e.sound=="boolean"&&(H.sound=e.sound),typeof e.music=="boolean"&&(H.music=e.music&&H.sound),ia()}catch{}}function Mo(){ln(Qn)&&wo(Na.setItem(xo,JSON.stringify(H)),3e3).catch(()=>{})}function wo(t,e){return Promise.race([t,new Promise((a,n)=>setTimeout(()=>n(new Error("timeout")),e))])}const go=new Set,$a=new Map;function bo(t){return t===0||H.stars[t-1]>0||go.has(t)}function Fs(){return ln(jn)}let pt=null;function zs(){const t=ln(jn)?_o:void 0;return pt??(pt=new ns(t,"Gem Warden"))}function xn(){var t;return Fs()&&((t=pt==null?void 0:pt.context)==null?void 0:t.state)!=="closed"}function Mn(){if(!H.sound&&!H.music)return;const t=zs();t.activate(),t.context&&t.output&&Lo(t.context,t.output)&&ia()}function ia(){Xo(H.sound,H.music),pt==null||pt.setMuted(!H.sound&&!H.music)}function la(){xn()&&(H.sound&&H.music?H.music=!1:H.sound?H.sound=!1:H.sound=H.music=!0,Mn(),ia(),_.click(),Mo())}function Bs(){return xn()?H.sound&&H.music?"Sound + music":H.sound?"Sound only":"Silent":"No sound here"}function Ee(t){return(t/Ct-.5)*1.2}document.addEventListener("visibilitychange",()=>{document.hidden&&It==="play"&&(k==null?void 0:k.state)==="playing"&&(Z=!0),!(!(pt!=null&&pt.context)||pt.context.state==="closed")&&(document.hidden?pt.context.suspend().catch(()=>{}):pt.activate())});window.addEventListener("pagehide",()=>pt==null?void 0:pt.dispose());let It="menu",k=null,At=1,Z=!1,j=null,Q=1;const vo=8;let R=null,Jt={text:"",t:0},qn=!1,pe=0,me=!0,Ce=!1,Pt={amount:0,t:0,seen:0},Ne=!1,Gt=0,Y=0;const Zt=typeof matchMedia=="function"?matchMedia("(prefers-reduced-motion: reduce)"):null;let Re=!!(Zt!=null&&Zt.matches);const ra=()=>Re?.4:1;cn(ra(),Re);var za;(za=Zt==null?void 0:Zt.addEventListener)==null||za.call(Zt,"change",t=>{Re=t.matches,cn(ra(),Re)});const ve=new Map,en=new Map,ca=new Map,ye=new Map,Aa=new WeakMap;let kt=0,Ie=0,re=0,Nt=0,We=0,De={x:0,y:0,w:0,h:0},Ot=null,fa=0,_n=0,Cn=0,Vn=0,ha=!1,Ht=!1,we={n:0,t:0},Ca=0,Kn={id:"",t:0},Qe=0,Te=0,Ln=[];function Dt(t){return Math.max(0,Math.min(1,t))}function nn(t){return 1+(1.70158+1)*Math.pow(t-1,3)+1.70158*Math.pow(t-1,2)}function an(t){return 1-Math.pow(1-t,3)}function Ra(t,e){return Math.floor(e)*Ct+Math.floor(t)}function on(t){k=ws(t),It="play",et=[],At=1,Z=!1,j=null,R=null,Q=1,qn=!1,pe=0,me=!0,Ce=!1,Pt={amount:0,t:0,seen:0},Ne=!1,Ht=!1,sn="",as(),cn(ra(),Re),ge=0,ve.clear(),en.clear(),ca.clear(),ye.clear(),kt=Ie=Nt=We=0,re=1,Ot=null,fa=Y,we={n:0,t:0},Qe=Te=0;const e=Ve(31+t);Ln=Array.from({length:22},()=>({x:e()*Ct,y:e()*Yt,phase:e()*6.28,pace:.25+e()*.5,color:["#9fe8b0","#ffe9a8","#8fd0ff"][Math.floor(e()*3)]})),_.start()}function xe(t){Jt={text:t,t:2.2}}function wn(){const t=N<z*1.15;let e,a;if(t){const l=N/Ct*Yt,r=Math.round(Math.min(Math.max(z-l,210),380));e={x:0,y:z-r,w:N,h:r},a={x:0,y:0,w:N,h:z-r}}else{const l=Math.round(Math.min(Math.max(N*.27,200),300));e={x:N-l,y:0,w:l,h:z},a={x:0,y:0,w:N-l,h:z}}const n=Math.max(4,Math.min(a.w/Ct,a.h/Yt)),s=n*Ct,o=n*Yt;return{field:{x:a.x+(a.w-s)/2,y:a.y+(a.h-o)/2,w:s,h:o},tile:n,panel:e,portrait:t}}let et=[];function Xt(t,e,a){return e>=t.x&&a>=t.y&&e<=t.x+t.w&&a<=t.y+t.h}function rt(t,e){const a=Math.max(0,Math.min(e,t.w/2,t.h/2));i.beginPath(),i.moveTo(t.x+a,t.y),i.arcTo(t.x+t.w,t.y,t.x+t.w,t.y+t.h,a),i.arcTo(t.x+t.w,t.y+t.h,t.x,t.y+t.h,a),i.arcTo(t.x,t.y+t.h,t.x,t.y,a),i.arcTo(t.x,t.y,t.x+t.w,t.y,a),i.closePath()}function $(t,e,a,n,s,o="left",l=600){i.font=`${l} ${n}px ${xt}`,i.fillStyle=s,i.textAlign=o,i.textBaseline="middle",i.fillText(t,e,a)}function nt(t,e,a,n=600){i.font=`${n} ${a}px ${xt}`;const s=i.measureText(t).width;return s>e?Math.max(7,a*(e/s)):a}function ht(t,e,a,n={}){const s=y.inside&&Xt(t,y.x,y.y);rt(t,Math.min(10,t.h*.3)),i.fillStyle=n.active?"#2c2650":s&&!n.disabled?"#1d2138":"#161a2c",i.fill(),i.lineWidth=n.active?2:1,i.strokeStyle=n.active?p.gold:n.accent??p.panelEdge,i.stroke();const o=Kn.id===e?1-(Y-Kn.t)/.22:0;o>0&&(i.fillStyle=`rgba(242,198,109,${.35*o})`,i.fill());const l=n.disabled?p.textDim:p.text;if(n.sub){const r=nt(a,t.w-8,Math.min(15,t.h*.36));$(a,t.x+t.w/2,t.y+t.h*.37,r,l,"center",700);const c=nt(n.sub,t.w-8,Math.min(12,t.h*.28),500);$(n.sub,t.x+t.w/2,t.y+t.h*.72,c,n.disabled?p.danger:p.goldDim,"center",500)}else{const r=nt(a,t.w-8,Math.min(15,t.h*.42));$(a,t.x+t.w/2,t.y+t.h/2,r,l,"center",700)}et.push({...t,id:e,tap:n.tap})}function Ia(t,e,a,n){if(i.beginPath(),t>=5){i.arc(e,a,n*.92,0,Math.PI*2);return}const s=t+2,o=s===4?-Math.PI/4:-Math.PI/2,l=s===3?n*1.12:n,r=s===3?n*.12:0,c=[];for(let u=0;u<s;u++){const w=u/s*Math.PI*2+o;c.push([e+Math.cos(w)*l,a+r+Math.sin(w)*l])}const f=l*(s===3?.3:.18),h=u=>{const w=c[u%s],x=c[(u+1)%s];return[(w[0]+x[0])/2,(w[1]+x[1])/2]},d=h(s-1);i.moveTo(d[0],d[1]);for(let u=0;u<s;u++){const w=h(u);i.arcTo(c[u][0],c[u][1],w[0],w[1],f)}i.closePath()}function ce(t,e,a,n,s=!1,o=!0){const l=_t(t),r=zt(t),c=ye.get(t.id);if(c!==void 0){const h=(Y-c)/.35;h>=1?ye.delete(t.id):n*=.35+.65*nn(Math.max(0,h))}if(i.save(),s){X(i,e,a,n*2.5,l,.5+.12*Math.sin(Y*2.4+t.id));const h=Qt<.5?0:Math.min(4,r-2);for(let d=0;d<h;d++){const u=Y*(1.4+r*.12)+d*Math.PI*2/h+t.id;X(i,e+Math.cos(u)*n*1.5,a+Math.sin(u)*n*1.25,Math.max(2,n*.4),l,.9)}}Ia(r,e,a,n);const f=i.createRadialGradient(e-n*.3,a-n*.35,n*.1,e,a,n);if(f.addColorStop(0,"#ffffff"),f.addColorStop(.25,l),f.addColorStop(1,He(l,-.55)),i.fillStyle=f,i.fill(),i.lineWidth=Math.max(1,n*.08),i.strokeStyle=He(l,.35),i.stroke(),Ia(r,e,a,n*.5),i.strokeStyle="rgba(255,255,255,0.35)",i.lineWidth=Math.max(.6,n*.05),i.stroke(),i.restore(),t.id>0&&n>=5){const h=Y*(.3+r*.04)+t.id*.618,d=h-Math.floor(h);d<.2&&eo(i,e-n*.3,a-n*.38,n*1.1,d/.2,"#ffffff",.3)}if(o&&n>=7){const h=Math.max(8,n*.75);i.font=`800 ${h}px ${xt}`,i.textAlign="center",i.textBaseline="middle",i.lineWidth=Math.max(2,h*.22),i.strokeStyle="rgba(8,8,16,0.85)",i.strokeText(String(r),e+n*.62,a+n*.58),i.fillStyle="#fff",i.fillText(String(r),e+n*.62,a+n*.58)}}function He(t,e){const a=parseInt(t.slice(1),16);let n=a>>16&255,s=a>>8&255,o=a&255;return e<0?(n*=1+e,s*=1+e,o*=1+e):(n+=(255-n)*e,s+=(255-s)*e,o+=(255-o)*e),`rgb(${Math.round(n)},${Math.round(s)},${Math.round(o)})`}let sn="",oe=null;function Ns(t,e){const a=`${N}x${z}:${Bt}:${e.fieldIndex}`;if(a===sn&&oe)return;sn=a,oe=document.createElement("canvas"),oe.width=Math.round(t.field.w*Bt),oe.height=Math.round(t.field.h*Bt);const n=oe.getContext("2d");n.scale(Bt,Bt);const s=t.tile,o=Ve(99+e.fieldIndex);n.fillStyle=p.earth,n.fillRect(0,0,t.field.w,t.field.h);for(let d=0;d<Ct;d++)for(let u=0;u<Yt;u++){n.fillStyle=(d+u)%2?p.earth:p.earth2,n.globalAlpha=.55,n.fillRect(d*s,u*s,s,s),n.globalAlpha=1;for(let w=0;w<3;w++)n.fillStyle=`rgba(120,170,140,${.05+o()*.07})`,n.fillRect(d*s+o()*s,u*s+o()*s,Math.max(1,s*.06),Math.max(1,s*.14))}for(const d of e.pathTiles){const[u,w]=d.split(",").map(Number);n.fillStyle=p.pathEdge,n.fillRect(u*s-s*.06,w*s-s*.06,s*1.12,s*1.12)}for(const d of e.pathTiles){const[u,w]=d.split(",").map(Number);n.fillStyle=p.path,n.fillRect(u*s+.5,w*s+.5,s-1,s-1);for(let x=0;x<3;x++){n.fillStyle=`rgba(255,255,255,${.03+o()*.04})`;const g=s*(.15+o()*.2);n.beginPath(),n.ellipse(u*s+o()*s,w*s+o()*s,g,g*.6,o()*3,0,Math.PI*2),n.fill()}}for(const[d,u]of e.field.rocks){const w=(d+.5)*s,x=(u+.5)*s;n.fillStyle=p.rock,n.beginPath(),n.ellipse(w,x+s*.08,s*.42,s*.34,0,0,Math.PI*2),n.fill(),n.fillStyle="#3b4160",n.beginPath(),n.ellipse(w-s*.08,x-s*.04,s*.26,s*.2,-.4,0,Math.PI*2),n.fill()}const[l,r]=e.pathPoints[0],c=n.createRadialGradient((l+.5)*s,(r+.5)*s,1,(l+.5)*s,(r+.5)*s,s*.7);c.addColorStop(0,"#000"),c.addColorStop(.6,"#3a1030"),c.addColorStop(1,"rgba(120,20,80,0)"),n.fillStyle=c,n.beginPath(),n.arc((l+.5)*s,(r+.5)*s,s*.7,0,Math.PI*2),n.fill();const f=Math.hypot(t.field.w,t.field.h)/2,h=n.createRadialGradient(t.field.w/2,t.field.h/2,f*.6,t.field.w/2,t.field.h/2,f);h.addColorStop(0,"rgba(3,4,10,0)"),h.addColorStop(1,"rgba(3,4,10,0.55)"),n.fillStyle=h,n.fillRect(0,0,t.field.w,t.field.h)}const y={shift:!1,x:0,y:0,inside:!1,down:!1,startX:0,startY:0,dragging:!1,dragRef:null,downWidget:null};St.addEventListener("pointermove",t=>{y.x=t.offsetX,y.y=t.offsetY,y.inside=!0,y.down&&y.dragRef&&!y.dragging&&Math.hypot(y.x-y.startX,y.y-y.startY)>8&&(y.dragging=!0)});St.addEventListener("pointerleave",()=>{y.inside=!1});St.addEventListener("pointerdown",t=>{var e;Mn(),St.setPointerCapture(t.pointerId),y.x=t.offsetX,y.y=t.offsetY,y.inside=!0,y.down=!0,y.startX=y.x,y.startY=y.y,y.dragging=!1,y.downWidget=gn(y.x,y.y),["pause","sound"].includes(((e=y.downWidget)==null?void 0:e.id)??"")||So(),y.dragRef=da(y.downWidget)??Hs(y.x,y.y)});St.addEventListener("pointerup",t=>{Mn(),y.shift=t.shiftKey,y.x=t.offsetX,y.y=t.offsetY;const e=y.dragging,a=y.dragRef;if(y.down=!1,y.dragging=!1,y.dragRef=null,e&&a){qs(a,y.x,y.y);return}_s(y.x,y.y)});St.addEventListener("pointercancel",()=>{y.down=!1,y.dragging=!1,y.dragRef=null});St.addEventListener("contextmenu",t=>t.preventDefault());function da(t){return!t||!k||typeof t.drop!="object"||t.drop.kind!=="inv"?t==null?void 0:t.gemRef:k.inventory[t.drop.index]?t.drop:void 0}function gn(t,e){for(let a=et.length-1;a>=0;a--)if(Xt(et[a],t,e))return et[a];return null}function ua(t,e){const a=wn();return Xt(a.field,t,e)?[Math.floor((t-a.field.x)/a.tile),Math.floor((e-a.field.y)/a.tile)]:null}function bn(t,e){if(!k)return null;const a=ua(t,e);return a?k.structures.find(n=>n.x===a[0]&&n.y===a[1])??null:null}function Hs(t,e){if(It!=="play")return null;const a=bn(t,e);return a!=null&&a.gem?{kind:"struct",id:a.id}:null}function ko(t,e){const a=gn(t,e);if(a!=null&&a.drop)return a.drop;const n=bn(t,e);return n?{kind:"struct",id:n.id}:null}function je(t){if(t.kind==="struct"){const a=k==null?void 0:k.structures.find(n=>n.id===t.id);return a?{space:v,x:a.x+.5,y:a.y+.5,r:.5}:null}const e=et.find(a=>a.id===`inv-${t.index}`);return e?{space:gt,x:e.x+e.w/2,y:e.y+e.h/2,r:e.w/2}:null}function pa(t,e,a){const n=je(t);n&&Be(()=>{ot(n.space,n.x,n.y,n.r*.3,n.r*(1.5+a*.4),.4,e,n.r*.16),$e(n.space,n.x,n.y,n.r*(1.8+a*.7),.35,e),yt(n.space,n.x,n.y,8+a*5,e,{speed:n.r*(5+a*1.5),life:.45,size:n.r*.1}),dn(n.space,n.x,n.y,n.r*(1+Math.min(a,5)*.1),.4,"#ffffff")})}function Pe(t,e){if(!k)return!1;if(e==="sell"){const o=$t(k,t),l=je(t);if(o&&Ps(k,t)){if(xe(`Sold for ${ea(o)} mana`),_.sell(),l){const r=wn(),c=l.space===gt?l.x:r.field.x+l.x*r.tile,f=l.space===gt?l.y:r.field.y+l.y*r.tile,h=_t(o);ie(gt,c,f,9,h,{speed:150,life:.6,size:4,gravity:500}),$e(gt,c,f,30,.3,h);for(let d=0;d<fn(6);d++)Xe(gt,c,f,De.x+De.w*Math.min(1,k.mana/k.manaCap),De.y+De.h/2,p.gold,3,160,()=>Nt=1,d*.04)}return!0}return!1}const a=$t(k,t),n=$t(k,e);if(!Ts(k,t,e))return!1;const s=$t(k,e);if(a&&n&&s){const o=zt(s),l=Math.round(zn(s)*100);xe(l>0?`Combined: G${o} ${ze(s)}, ${l}% to G${o+1}`:`Combined into G${o} ${ze(s)}`);const r=_t(s),c=je(e);c&&Be(()=>hn(c.space,c.x,c.y,12+o*2,r,c.r*2.4,.16,c.r*.1)),ye.set(s.id,Y+.1),to(.14,()=>pa(e,r,o))}else if(s){ye.set(s.id,Y);const o=je(e);o&&Be(()=>ot(o.space,o.x,o.y,o.r*.4,o.r*1.3,.3,_t(s),o.r*.12)),_.place()}return!0}function qs(t,e,a){const n=ko(e,a);n&&Pe(t,n),R=null}function _s(t,e){var o;if(It==="play"&&Y-fa<.3)return;const a=gn(t,e);if(a&&((o=y.downWidget)==null?void 0:o.id)===a.id){const l=da(a);if(l){R&&!Fe(R,l)?(Pe(R,l),R=null):R=R&&Fe(R,l)?null:l;return}if(a.drop&&R){Pe(R,a.drop),R=null;return}a.tap&&(Kn={id:a.id,t:Y},!a.forge&&!a.random&&!["pool","next","sound"].includes(a.id)&&_.click(),a.tap());return}if(It!=="play"||!k||k.state!=="playing")return;const n=ua(t,e);if(!n){R=null,j=null;return}const s=bn(t,e);if(j&&!s){aa(k,j,n[0],n[1])?ks(k,j,n[0],n[1])&&!y.shift&&(j=null):xe(j==="trap"?"Traps go on the path":"Towers go beside the path");return}if(s){const l={kind:"struct",id:s.id};R&&!Fe(R,l)?(Pe(R,l),R=null):s.gem&&(R=R?null:l);return}R=null}window.addEventListener("keydown",t=>{if(Mn(),t.key.toLowerCase()==="m"){la();return}if(It==="menu"){const a=Number(t.key);a>=1&&a<=Vt.length&&bo(a-1)&&on(a-1);return}if(!k)return;const e=t.key.toLowerCase();if(k.state!=="playing"){if(!ha)return;e==="?"||e==="h"?Ht=!Ht:e==="escape"&&Ht?Ht=!1:e==="enter"||e===" "?Co():e==="escape"&&(It="menu");return}if(/^([trpnfs0-6+=-]|delete|backspace|escape)$/.test(e)&&So(),e==="t")j=j==="tower"?null:"tower";else if(e==="r")j=j==="trap"?null:"trap";else if(e==="p")$o();else if(e==="n")Ao();else if(e===" ")Z=!Z,t.preventDefault();else if(e==="f")At=At===3?1:At+1;else if(e==="+"||e==="=")Q=Math.min(vo,Q+1);else if(e==="-")Q=Math.max(1,Q-1);else if((e==="s"||e==="delete"||e==="backspace")&&R)Pe(R,"sell"),R=null;else if(e==="escape")j=null,R=null;else if(/^[1-6]$/.test(e)){const a=k.field.colors[Number(e)-1];a&&Po(a)}else e==="0"&&To()});function So(){It!=="play"||!Z||Ce||(k==null?void 0:k.state)!=="playing"||(Ce=!0,me=!1)}function Yn(t){return{id:0,mass:Math.pow(2,Q-1),comp:{[t]:1},kills:0}}function To(){if(!k)return;const t=vs(k,Q);t.length&&(_.forgeBatch(),t.forEach((e,a)=>{const n=k==null?void 0:k.inventory[e];n&&(ye.set(n.id,Y+a*.08),to(a*.08,()=>pa({kind:"inv",index:e},_t(n),0)))}),R=null)}function Po(t){if(!k)return;const e=bs(k,t,Q);if(e>=0){const a=k.inventory[e];a&&ye.set(a.id,Y),pa({kind:"inv",index:e},qe(t),Q/3),_.forge(t,Q),R=null}}function $o(){if(k&&Ss(k)){xe(`Mana pool expanded · gains ×${k.manaMult.toFixed(2)}`),_.pool();const[t,e]=[k.orb[0]+.5,k.orb[1]+.5];kt=1,Nt=1,Be(()=>{ot(v,t,e,.4,2.6,.7,p.mana,.16),ot(v,t,e,.4,1.8,.6,"#eaf8ff",.1,.12),hn(v,t,e,26,p.mana,2.4,.35,.07)})}}function Ao(){if(!k)return;const t=fo(k);t>0&&xe(`+${Math.round(t*k.manaMult)} to the pot · paid when all are dead`)}function Co(){k&&(k.state==="won"&&k.fieldIndex+1<Vt.length?on(k.fieldIndex+1):on(k.fieldIndex))}let Wa=performance.now(),Rn=16,ge=0;function Ro(t){const e=(t-Wa)/1e3,a=Math.min(.1,e);Wa=t,Y=t/1e3;const n=It==="play"?k:null;if(Z||(Ce=!1),n&&n.state==="playing"&&(!Z||Ce)&&(pe+=a),n&&!Z){let o=a*At;for(;o>0;){const l=Math.min(.016666666666666666,o);As(n,l),o-=l}}const s=n?n.state!=="playing"?a:Z?0:a*At:0;if(Gt+=s,n){Xs(n);const o=n.wasted-Pt.seen;Pt.seen=n.wasted,o>0?(Pt.amount+=o,Pt.t=1.5):!Z&&(Pt.t-=a)<=0&&(Pt.amount=0),Pt.t>0&&Pt.amount>=1&&!Z&&n.state==="playing"&&_.overflow()}if(n&&n.state!=="playing"&&!qn){qn=!0,_n=Y,Vn=0,ha=!1;const[o,l]=[n.orb[0]+.5,n.orb[1]+.5];if(n.state==="won"){const r=po(n);H.stars[n.fieldIndex]=Math.max(H.stars[n.fieldIndex],r);const c=yn(n,pe,me).total;Ne=c>H.best[n.fieldIndex],Ne&&(H.best[n.fieldIndex]=c),Mo(),_.win(),Ue("#fff2c4",.3),ot(v,o,l,.5,5,.9,p.gold,.2),Cn=.4}else{ie(v,o,l,46,p.mana,{speed:7,life:1.5,size:.14,gravity:5}),ie(v,o,l,18,"#eaf8ff",{speed:5,life:1.2,size:.1,gravity:5}),yt(v,o,l,40,"#bfeaff",{speed:9,life:.9,size:.08});for(let r=0;r<3;r++)ot(v,o,l,.4,4+r*2.5,.8,r?p.mana:"#ffffff",.25,r*.12);$e(v,o,l,5,.7,"#eaf8ff"),Oe(.6),Ue("#ffffff",.5),_.lose()}}n&&n.state==="playing"&&!Z&&(Ys(n,a*At),Io(n)>=1&&_.heartbeat()),n&&n.state==="won"&&!de&&(Cn-=a)<=0&&(Ls(),Cn=Y-_n<3.5?.22+Math.random()*.25:.9+Math.random()*.9),kt=Math.max(0,kt-a*2.5),Ie=Math.max(0,Ie-a*2),re=Math.max(0,re-a*2.5),Nt=Math.max(0,Nt-a*2.5),We=Math.max(0,We-a),os(s,a),Ks(n,a),Vs(e),Jt.t-=a,Qs(Y),requestAnimationFrame(Ro)}function Vs(t){t>.25||(Rn=Rn*.95+t*1e3*.05,ge=Rn>40?ge+1:Math.max(0,ge-1),ge>120&&Qt>.2&&(cn(Qt*.6,Re),ge=0))}function Io(t){const e=t.monsters.filter(n=>n.hp>0&&t.pathLength-n.dist<=Do).sort((n,s)=>s.dist-n.dist);if(!e.length)return 0;let a=t.mana;return e.forEach((n,s)=>a-=oa(t,n,s)),a<0?1:.55}function Ks(t,e){let a="menu",n=0,s=0,o=!1;if(t)if(t.state!=="playing")a="off";else{a="play";const l=t.monsters.length;n=l===0&&t.spawnQueue.length===0?.08:Math.min(1,.26+l/30*.5+t.nextWave/t.waves.length*.3),s=Io(t),o=t.monsters.some(r=>r.type==="giant")}Qe+=(n-Qe)*Math.min(1,e*.8),Te+=(s-Te)*Math.min(1,e*2.5),Zo({mode:a,field:(t==null?void 0:t.fieldIndex)??0,intensity:Qe,danger:Te,boss:o,speed:Z?1:At,paused:Z&&a==="play"})}function Ls(){const t=wn(),e=t.field.x+t.field.w*(.1+Math.random()*.8),a=t.field.y+t.field.h*(.08+Math.random()*.5),n=qe(Rt[Math.floor(Math.random()*Rt.length)]),s=Math.max(50,t.field.w*.15);$e(gt,e,a,s*.9,.35,n),ot(gt,e,a,2,s*.7,.5,n,3),yt(gt,e,a,46,n,{speed:s*2.6,life:1.1,size:2.4,gravity:s*1.2,drag:.94}),yt(gt,e,a,14,"#ffffff",{speed:s*1.4,life:.7,size:1.6,gravity:s,drag:.93}),_.firework((e/N-.5)*1.4)}function Ys(t,e){const a=Qt;for(const n of t.monsters){const s=jt[n.type].size,o=(Math.random()-.5)*s*1.6;n.poisonT>0&&Math.random()<e*3*a&&Za(v,n.x+o,n.y-s*.4,"#4fe06a",.1,.6,-.8),n.slowT>0&&Math.random()<e*2.5*a&&yt(v,n.x+o,n.y,1,"#bfe6ff",{speed:.4,life:.5,size:.04,gravity:1.2}),n.regen>0&&n.poisonT<=0&&n.hp<n.maxHp&&Math.random()<e*2.5*a&&dn(v,n.x+o,n.y-s,.22,.45,"#7cf0a0"),n.type==="giant"&&Math.random()<e*5*a&&yt(v,n.x+o,n.y+o*.5,1,"#d8f2ff",{speed:.3,life:.8,size:.04,gravity:-.7})}if(Math.random()<e*6*a){const[n,s]=t.pathPoints[0];hn(v,n+.5,s+.5,1,"#d070f0",.8,.55,.05)}}function Xs(t){const e=t.orb[0]+.5,a=t.orb[1]+.5,n=t.pathPoints[0][0]+.5,s=t.pathPoints[0][1]+.5;for(const o of t.events)switch(o.type){case"shot":en.set(Ra(o.x,o.y),Gt),_.shot(o.gem,o.grade,Ee(o.x));break;case"hit":{const l=Ee(o.x);if(ve.set(o.id,Gt),o.trap&&(en.set(Ra(o.sx,o.sy),Gt),ka(v,o.sx,o.sy,o.x,o.y,o.color,.16,.05),_.trap(o.gem,l)),o.ward){ot(v,o.x,o.y,.2,.55,.25,"#cfd8ff",.07),yt(v,o.x,o.y,4,"#cfd8ff",{speed:2.5,life:.25,size:.05}),_.ward(l);break}const r=Math.atan2(o.y-o.sy,o.x-o.sx);yt(v,o.x,o.y,o.crit?10:4,o.color,{speed:o.crit?5.5:3,life:.3,size:o.crit?.09:.06,angle:r,spread:o.crit?6.3:1.7}),$e(v,o.x,o.y,o.crit?.95:.45,.18,o.color),o.crit?(dn(v,o.x,o.y,.8,.3,"#fff7c2"),ot(v,o.x,o.y,.1,.7,.25,"#ffe14d",.08),o.trap||ka(v,o.sx,o.sy,o.x,o.y,"#ffe14d",.12,.035),Oe(.03),_.crit(l)):_.hit(l),o.splash&&(ot(v,o.x,o.y,.15,ao,.3,"#ff7a3a",.13),yt(v,o.x,o.y,5,"#ffb347",{speed:3.5,life:.4,size:.05,gravity:2}),_.splash(l)),o.slow&&ie(v,o.x,o.y,2,"#bfe6ff",{speed:1.6,life:.4,size:.06,gravity:2}),o.tear&&ie(v,o.x,o.y,2,"#d6dbe8",{speed:1.8,life:.45,size:.07,gravity:5}),o.poison&&Za(v,o.x,o.y,"#4fe06a",.14,.5,-.9),o.leech&&Gt-Ca>.1&&(Ca=Gt,Xe(v,o.x,o.y,e,a,"#ff9a2e",.07,2.5,()=>kt=Math.max(kt,.35)));break}case"kill":{const l=jt[o.monster],r=l.size,c=o.monster==="giant",f=Ee(o.x);An(v,o.x,o.y-.2,`+${Math.round(o.mana)}`,p.mana,.32,.9,-.9),o.points>=.5&&An(v,o.x,o.y+.15,`+${Math.round(o.points)}`,p.gold,.26,.9,-.7),ie(v,o.x,o.y,5+r*30,l.col,{speed:2.5+r*5,life:.6,size:r*.4,gravity:4}),yt(v,o.x,o.y,4+r*24,He(l.col,.45),{speed:3+r*6,life:.4,size:.06}),ot(v,o.x,o.y,r*.5,r*3.2,.35,l.col,.08),$e(v,o.x,o.y,r*3.6,.25,l.col);for(let h=0;h<(c?6:1);h++)Xe(v,o.x,o.y,e,a,p.mana,.08,3,()=>{kt=Math.max(kt,.6),_.mana()},h*.05);Gt-we.t>1.6&&(we.n=0),we={n:we.n+1,t:Gt},c?(ot(v,o.x,o.y,.3,3.2,.6,"#d8f2ff",.2),ot(v,o.x,o.y,.3,2,.5,"#ffffff",.12,.08),Oe(.3),Ue("#bfe6ff",.22),_.bigKill(f)):_.kill(f,we.n,r);break}case"spawn":re=1,yt(v,n,s,3,"#e58cff",{speed:2.2,life:.4,size:.05});break;case"banish":An(v,o.x,o.y-.3,`−${o.cost}`,p.danger,.4,1.3,-.6),ot(v,e,a,.4,2.4,.5,p.danger,.2),ot(v,e,a,.4,1.5,.4,"#ffffff",.1,.06),yt(v,e,a,18,p.danger,{speed:6,life:.6,size:.08}),Xe(v,e,a,n,s,p.danger,.12,5,()=>re=1),Ie=1,Oe(.22),Ue(p.danger,.3),_.banish(Ee(o.x));break;case"build":ca.set(o.id,Y),Be(()=>{ot(v,o.x,o.y,.2,.95,.35,o.kind==="trap"?"#b9a5ff":p.stoneHi,.09),ie(v,o.x,o.y,8,p.stone,{speed:2.4,life:.5,size:.07,gravity:6}),yt(v,o.x,o.y,7,"#cfc8e6",{speed:2,life:.35,size:.05})}),_.build(o.kind==="trap",Ee(o.x));break;case"combine":_.combine(o.grade);break;case"wave":{const l=o.monster==="giant",r=o.index===t.waves.length-1,c=no[o.monster];Ot={title:r?"Final wave":`Wave ${o.index+1}`,sub:`${o.count} × ${c.name} · ${c.trait}`,color:jt[o.monster].col,t0:Y},re=1,ot(v,n,s,.3,l?4:2.6,.7,"#d070f0",.16),ot(v,n,s,.3,l?2.6:1.6,.6,"#ff9ae8",.1,.1),yt(v,n,s,16,"#e58cff",{speed:4,life:.6,size:.06}),l&&Oe(.12),_.wave(l,r);break}case"bonus":xe(`Field clear · pot paid +${Math.round(o.mana)} mana`),kt=1,Nt=1,ot(v,e,a,.4,2.2,.6,p.gold,.14),hn(v,e,a,24,p.gold,3,.4,.07),_.bonus();break;case"error":xe(o.message),o.message.includes("mana")&&(We=.35),_.error();break}if(t.events.length=0,ve.size>600)for(const[o,l]of ve)Gt-l>1&&ve.delete(o)}function Qs(t){if(i.setTransform(Bt,0,0,Bt,0,0),i.fillStyle=p.night,i.fillRect(0,0,N,z),et=[],N<280||z<200){Wo("Gem Warden needs a bit more room. Make the frame larger or go full screen.",N/2,z/2-12,N-16,12,p.gold);return}It==="menu"||!k?js(t):Us(k,t)}function js(t){const e=N/2,a=Math.max(N,z);X(i,N*(.3+.1*Math.sin(t*.11)),z*.3,a*.6,"#3b2a7a",.55),X(i,N*(.72+.1*Math.cos(t*.09)),z*.64,a*.55,"#123a66",.55);const n=Ve(7);i.fillStyle="#dfe6ff";for(let O=0;O<70;O++){const W=n()*N,M=n()*z,G=.3+.4*Math.sin(t*(.6+n()*1.8)+n()*6.28);G<=.05||(i.globalAlpha=G,i.fillRect(W,M,1.4,1.4))}i.globalAlpha=1;for(let O=0;O<14;O++){const W=Rt[O%6],M=(O*137+t*12*(1+O%3))%(N+80)-40,G=z*(O*61%100)/100+Math.sin(t*.7+O)*6,D=10+O%4*4;X(i,M,G,D*2.6,qe(W),.14),i.globalAlpha=.24,ce({id:0,mass:1<<O%4,comp:{[W]:1}},M,G,D,!1,!1),i.globalAlpha=1}const s=Math.min(54,N*.09,z*.1);i.font=`800 ${s}px ${xt}`;const o=i.measureText("Gem Warden").width;X(i,e,z*.12,o*.75,p.gold,.16+.04*Math.sin(t*1.3));const l=t*.3%1.7-.35,r=i.createLinearGradient(e-o/2,0,e+o/2,0);r.addColorStop(0,p.gold),r.addColorStop(Dt(l-.14),p.gold),r.addColorStop(Dt(l),"#fffbe6"),r.addColorStop(Dt(l+.14),p.gold),r.addColorStop(1,p.gold),i.fillStyle=r,i.textAlign="center",i.textBaseline="middle",i.fillText("Gem Warden",e,z*.12);const c=s*.3;N>o+c*17&&Rt.forEach((O,W)=>{const M=W<3?e-o/2-c*2-(2-W)*c*2.5:e+o/2+c*2+(W-3)*c*2.5,G=z*.12+Math.sin(t*1.6+W*1.1)*c*.3;ce({id:W+.5,mass:1<<W,comp:{[O]:1}},M,G,c,!0,!1)}),$("Forge gems, guard the orb.",e,z*.12+s*.85,Math.max(11,s*.32),p.textDim,"center",500);const f=N>z*1.2?3:2,h=Math.ceil(Vt.length/f),d=z*.12+s*1.4,u=Math.max(64,z*.14),w=Math.max(8,Math.min(N,z)*.02),x=Math.min(260,(N-w*(f+1))/f),g=Math.min(120,(z-d-u-w*(h+1))/h),I=e-(x*f+w*(f-1))/2;Vt.forEach((O,W)=>{const M={x:I+W%f*(x+w),y:d+w+Math.floor(W/f)*(g+w),w:x,h:g},G=bo(W),D=y.inside&&Xt(M,y.x,y.y)&&G;rt(M,12),i.fillStyle=G?D?"#1f2440":"#151a2e":"#0f1220",i.fill(),i.strokeStyle=G?D?p.gold:p.panelEdge:"#1a1d2c",i.lineWidth=D?2:1,i.stroke(),D&&X(i,M.x+M.w/2,M.y+M.h/2,M.w*.7,p.gold,.1),Js(O,{x:M.x+8,y:M.y+8,w:M.h-16,h:M.h-16},G,t+W*1.7);const m=M.x+M.h,b=M.w-M.h-8;$(`${W+1}. ${O.name}`,m,M.y+M.h*.26,nt(`${W+1}. ${O.name}`,b,Math.min(16,g*.17),700),G?p.text:p.textDim,"left",700),$(G?`${O.waves} waves`+(H.best[W]?` · best ${H.best[W]}`:""):"Locked",m,M.y+M.h*.5,Math.min(12,g*.13),p.textDim,"left",500);const E=H.stars[W];for(let F=0;F<3;F++)Eo(m+9+F*Math.min(22,g*.2),M.y+M.h*.76,Math.min(8,g*.075),F<E);let T=m+9+3*Math.min(22,g*.2);for(const F of O.colors)i.fillStyle=qe(F),i.beginPath(),i.arc(T,M.y+M.h*.76,Math.min(4,g*.04),0,Math.PI*2),i.fill(),T+=Math.min(10,g*.09);G?et.push({...M,id:`field-${W}`,tap:()=>on(W)}):et.push({...M,id:`field-${W}`,tap:()=>{const F=($a.get(W)??0)+1;$a.set(W,F),F>=5&&go.add(W)}})}),Wo("Build towers beside the path, forge gems and drop them in. Drop a gem onto another to combine; pure gems beat mixes. Monsters that reach the orb cost mana; run out and you lose.",e,z-u+6,Math.min(N-24,640),Math.max(10,Math.min(13,N/50)),p.textDim);const B={x:N-120,y:z-38,w:112,h:30};ht(B,"sound",Bs(),{tap:la,disabled:!xn()})}function qe(t){return ue[t].hex}function Wo(t,e,a,n,s,o){i.font=`500 ${s}px ${xt}`;const l=t.split(" ");let r="",c=a;for(const f of l){const h=r?`${r} ${f}`:f;i.measureText(h).width>n&&r?($(r,e,c,s,o,"center",500),r=f,c+=s*1.35):r=h}r&&$(r,e,c,s,o,"center",500)}function Eo(t,e,a,n){i.beginPath();for(let s=0;s<10;s++){const o=s/10*Math.PI*2-Math.PI/2,l=s%2?a*.45:a;i.lineTo(t+Math.cos(o)*l,e+Math.sin(o)*l)}i.closePath(),i.fillStyle=n?p.gold:"#2a2d42",i.fill()}function Js(t,e,a,n){const s=Math.min(e.w/Ct,e.h/Yt),o=e.x+(e.w-s*Ct)/2,l=e.y+(e.h-s*Yt)/2;i.fillStyle=a?p.earth2:"#11141f",i.fillRect(o,l,s*Ct,s*Yt),i.strokeStyle=a?p.stoneHi:"#2a2d42",i.lineWidth=Math.max(1.5,s*.8),i.lineJoin="round",i.beginPath(),t.path.forEach(([d,u],w)=>{const x=o+(d+.5)*s,g=l+(u+.5)*s;w===0?i.moveTo(x,g):i.lineTo(x,g)}),i.stroke();const[r,c]=t.path[t.path.length-1];if(i.fillStyle=a?p.mana:"#2a2d42",i.beginPath(),i.arc(o+(r+.5)*s,l+(c+.5)*s,s*1.1,0,Math.PI*2),i.fill(),!a)return;X(i,o+(r+.5)*s,l+(c+.5)*s,s*3.2,p.mana,.5+.2*Math.sin(n*2));let f=0;for(let d=1;d<t.path.length;d++)f+=Math.abs(t.path[d][0]-t.path[d-1][0])+Math.abs(t.path[d][1]-t.path[d-1][1]);let h=n*7%f;for(let d=1;d<t.path.length;d++){const[u,w]=t.path[d-1],[x,g]=t.path[d],I=Math.abs(x-u)+Math.abs(g-w);if(h<=I){X(i,o+(u+(x-u)*h/I+.5)*s,l+(w+(g-w)*h/I+.5)*s,s*2.4,"#ff9a8a",.95);break}h-=I}}function Us(t,e){var D;const a=wn();Ns(a,t);const n=a.tile,[s,o]=ls(),l=a.field.x+s*n,r=a.field.y+o*n;oe&&i.drawImage(oe,l,r,a.field.w,a.field.h);const c=m=>l+m*n,f=m=>r+m*n,h=Math.round(Math.max(3,t.pathLength/9)*Math.min(1,Qt));for(let m=0;m<h;m++){const[b,E]=Ke(t,(e*1.6+m*t.pathLength/h)%t.pathLength);X(i,c(b),f(E),n*.55,"#8f7fd0",.11)}ti(c(t.pathPoints[0][0]+.5),f(t.pathPoints[0][1]+.5),n,e);const d=y.inside?ua(y.x,y.y):null;if(j&&d&&t.state==="playing"){const m=aa(t,j,d[0],d[1]);i.fillStyle=m?"rgba(124,240,160,0.22)":"rgba(255,90,110,0.22)",i.fillRect(c(d[0]),f(d[1]),n,n),m&&j==="tower"&&(i.strokeStyle="rgba(124,240,160,0.35)",i.setLineDash([4,4]),i.lineDashOffset=-e*10,i.beginPath(),i.arc(c(d[0]+.5),f(d[1]+.5),n*2.9,0,Math.PI*2),i.stroke(),i.setLineDash([]))}const[u,w]=t.orb;ei(t,c(u+.5),f(w+.5),n,e);const x=Pt.t>0&&Pt.amount>=1;x&&(i.globalAlpha=de?1:.55+.45*Math.sin(e*10),i.strokeStyle=p.danger,i.lineWidth=Math.max(2,n*.1),i.beginPath(),i.arc(c(u+.5),f(w+.5),n*.95,0,Math.PI*2),i.stroke(),i.globalAlpha=1);const g=R&&R.kind==="struct"?R.id:-1;for(const m of t.structures){const b=c(m.x),E=f(m.y),T=b+n/2,F=E+n/2,ct=ca.get(m.id),st=ct===void 0?1:nn(Dt((Y-ct)/.32)),ft=en.get(m.y*Ct+m.x),it=ft===void 0?0:Math.max(0,1-(Gt-ft)/.2),Wt=m.gem?_t(m.gem):"";if(i.save(),st!==1&&(i.translate(T,F),i.scale(st,st),i.translate(-T,-F)),m.kind==="tower"){i.fillStyle="#00000055",i.fillRect(b+n*.12,E+n*.16,n*.84,n*.84),rt({x:b+n*.08,y:E+n*.08,w:n*.84,h:n*.84},n*.14);const mt=i.createLinearGradient(b,E,b,E+n);mt.addColorStop(0,p.stoneHi),mt.addColorStop(1,p.stone),i.fillStyle=mt,i.fill(),i.fillStyle="#1b1828",i.beginPath(),i.arc(T,F,n*.3,0,Math.PI*2),i.fill(),m.gem&&(i.strokeStyle=Wt,i.globalAlpha=.45+.2*Math.sin(e*3+m.id)+.35*it,i.lineWidth=Math.max(1,n*.05),i.beginPath(),i.arc(T,F,n*.33,0,Math.PI*2),i.stroke(),i.globalAlpha=1)}else{m.gem&&X(i,T,F,n*(.7+.5*it),Wt,.25+.6*it),i.strokeStyle=m.gem?Wt:p.stoneHi,i.globalAlpha=.75+.25*it,i.lineWidth=Math.max(1,n*.06*(1+it)),i.beginPath(),i.arc(T,F,n*.4,0,Math.PI*2),i.stroke(),i.beginPath();for(let mt=0;mt<5;mt++){const bt=mt/5*Math.PI*2*2-Math.PI/2+e*.3;i.lineTo(T+Math.cos(bt)*n*.36,F+Math.sin(bt)*n*.36)}i.closePath(),i.stroke(),i.globalAlpha=1}const vn=y.dragging&&((D=y.dragRef)==null?void 0:D.kind)==="struct"&&y.dragRef.id===m.id;m.gem&&!vn&&ce(m.gem,T,F,n*(m.kind==="trap"?.24:.27)*(1+.3*it),!0),i.restore(),m.id===g&&(i.strokeStyle=p.gold,i.lineWidth=2,i.strokeRect(b+1,E+1,n-2,n-2))}const I=d?t.structures.find(m=>m.x===d[0]&&m.y===d[1]):void 0,S=t.structures.find(m=>m.id===g)??(I!=null&&I.gem?I:void 0);if(S!=null&&S.gem){const m=pn(S.gem,S.kind);i.strokeStyle="rgba(242,198,109,0.55)",i.fillStyle="rgba(242,198,109,0.06)",i.lineWidth=1.5,i.beginPath(),i.arc(c(S.x+.5),f(S.y+.5),m.range*n,0,Math.PI*2),i.fill(),i.stroke()}for(const m of t.monsters)ni(t,m,c,f,n,e);oi(t,c,f,n);const B=t.monsters.filter(m=>m.hp>0&&t.pathLength-m.dist<=Do).sort((m,b)=>b.dist-m.dist);let O=t.mana;B.forEach((m,b)=>{O-=oa(t,m,b);const E=Math.floor(O),T=E<0?`−${-E}`:`${E}`,F=n*jt[m.type].size;i.font=`800 ${Math.max(10,n*.36)}px ${xt}`,i.textAlign="center",i.lineWidth=3,i.strokeStyle="#000",i.strokeText(T,c(m.x),f(m.y)-F-n*.22),i.fillStyle=E<0?p.danger:"#fff",i.fillText(T,c(m.x),f(m.y)-F-n*.22)}),i.save(),i.lineCap="round";for(const m of t.shots){let b=Aa.get(m);b||Aa.set(m,b=[]),Z||(b.push(m.x,m.y),b.length>12&&b.splice(0,2)),i.globalCompositeOperation="lighter",i.strokeStyle=m.color;const E=b.length/2;for(let T=1;T<E;T++)i.globalAlpha=T/E*.7,i.lineWidth=Math.max(1,n*.13*(T/E)),i.beginPath(),i.moveTo(c(b[T*2-2]),f(b[T*2-1])),i.lineTo(c(b[T*2]),f(b[T*2+1])),i.stroke();i.globalAlpha=1,X(i,c(m.x),f(m.y),Math.max(5,n*.34),m.color,1)}i.restore(),Dn({ctx:i,ox:l,oy:r,unit:n},v,xt);const W=Math.round(Ln.length*Math.min(1,Qt));for(let m=0;m<W;m++){const b=Ln[m],E=.3+.3*Math.sin(e*1.7+b.phase*3);X(i,c(b.x+Math.sin(e*b.pace+b.phase)*.9),f(b.y+Math.cos(e*b.pace*.7+b.phase*1.3)*.7),Math.max(2.5,n*.16),b.color,E)}if(Te>.03&&t.state==="playing"){const m=a.field,b=n*1.6;i.globalAlpha=Te*(de?.7:.55+.3*Math.sin(e*7));for(const[E,T,F,ct,st]of[[m.x,0,m.x+b,0,{x:m.x,y:m.y,w:b,h:m.h}],[m.x+m.w,0,m.x+m.w-b,0,{x:m.x+m.w-b,y:m.y,w:b,h:m.h}],[0,m.y,0,m.y+b,{x:m.x,y:m.y,w:m.w,h:b}],[0,m.y+m.h,0,m.y+m.h-b,{x:m.x,y:m.y+m.h-b,w:m.w,h:b}]]){const ft=i.createLinearGradient(E,T,F,ct);ft.addColorStop(0,"rgba(255,40,70,0.55)"),ft.addColorStop(1,"rgba(255,40,70,0)"),i.fillStyle=ft,i.fillRect(st.x,st.y,st.w,st.h)}i.globalAlpha=1}const M=rs();if(M&&(i.globalAlpha=M.alpha,i.fillStyle=M.color,i.fillRect(a.field.x,a.field.y,a.field.w,a.field.h),i.globalAlpha=1),x){const m=`−${Math.round(Pt.amount)} lost, orb full`,b=Math.max(11,n*.34);i.font=`800 ${b}px ${xt}`;const E=i.measureText(m).width/2+4,T=Math.max(a.field.x+E,Math.min(a.field.x+a.field.w-E,a.field.x+(u+.5)*n)),F=a.field.y+(w<2?(w+1.5)*n+b*.6:(w-.5)*n-b*.4);i.textAlign="center",i.lineWidth=3,i.strokeStyle="#000",i.strokeText(m,T,F),i.fillStyle=p.danger,i.fillText(m,T,F)}if(ai(a),si(t,a),ii(t,a),li(t,a),fi(t,a),t.state==="playing"&&Dn({ctx:i,ox:0,oy:0,unit:1},gt,xt),y.dragging&&y.dragRef){const m=$t(t,y.dragRef);m&&ce(m,y.x,y.y,Math.max(14,n*.4),!0)}if(Jt.t>0&&Jt.text){i.globalAlpha=Math.min(1,Jt.t*2);const m=Math.max(12,Math.min(16,a.field.w/40));i.font=`700 ${m}px ${xt}`;const b=i.measureText(Jt.text).width+24,E=(1-an(Dt((2.2-Jt.t)/.18)))*m,T={x:a.field.x+a.field.w/2-b/2,y:a.field.y+a.field.h-m*3.2+E,w:b,h:m*2};rt(T,10),i.fillStyle="#0d1020ee",i.fill(),i.strokeStyle=p.goldDim,i.stroke(),$(Jt.text,T.x+T.w/2,T.y+T.h/2,m,p.text,"center",700),i.globalAlpha=1}Z&&!Ce&&t.state==="playing"&&Zs(a,e),t.state!=="playing"&&pi(t,a);const G=1-(Y-fa)/.5;G>0&&(i.globalAlpha=G,i.fillStyle=p.night,i.fillRect(0,0,N,z),i.globalAlpha=1)}function Zs(t,e){i.fillStyle="rgba(8,10,22,0.55)",i.fillRect(0,0,N,z);const a=Math.min(40,t.field.w/10);$("Paused",t.field.x+t.field.w/2,t.field.y+t.field.h/2,a,p.goldDim,"center",800);const n=et.find(s=>s.id==="pause");n&&(X(i,n.x+n.w/2,n.y+n.h/2,Math.max(n.w,n.h)*1.4,p.gold,.22+.08*Math.sin(e*3)),ht(n,"pause","Resume",{active:!0,tap:n.tap}))}function ti(t,e,a,n){const s=re;X(i,t,e,a*(1.25+.5*s),"#a02fc0",.6+.4*s),i.fillStyle="#05020a",i.beginPath(),i.arc(t,e,a*.34,0,Math.PI*2),i.fill(),i.save(),i.globalCompositeOperation="lighter",i.lineCap="round";const o=["#ff6ad5","#b04ae8","#6a3bff"];for(let l=0;l<6;l++){const r=a*(.2+l*.075)*(1+.15*s),c=-n*(2.6-l*.3)+l*1.1;i.strokeStyle=o[l%3],i.globalAlpha=.75-l*.08,i.lineWidth=Math.max(1,a*.05),i.beginPath(),i.arc(t,e,r,c,c+1.9),i.stroke()}i.restore()}function ei(t,e,a,n,s){if(t.state==="lost"){i.fillStyle="#0a0f1c",i.beginPath(),i.arc(e,a,n*.5,0,Math.PI*2),i.fill(),i.strokeStyle="#2b3a58",i.lineWidth=Math.max(1,n*.05),i.stroke(),i.beginPath();for(let x=0;x<5;x++){const g=x*1.31+.4;i.moveTo(e,a),i.lineTo(e+Math.cos(g+.5)*n*.25,a+Math.sin(g+.5)*n*.25),i.lineTo(e+Math.cos(g)*n*.5,a+Math.sin(g)*n*.5)}i.stroke();return}const o=Dt(t.mana/t.manaCap),l=o<.18,r=l?Math.pow(Math.max(0,Math.sin(s*9)),6)*.1:0,c=n*.6*(1+Math.sin(s*2.2)*.04+kt*.16+r),f=l?p.danger:p.mana;X(i,e,a,c*(2.5+kt),f,.55+.3*kt),Ie>0&&X(i,e,a,c*3.4,p.danger,Ie),i.save(),i.beginPath(),i.arc(e,a,c,0,Math.PI*2),i.clip(),i.fillStyle="#071226",i.fillRect(e-c,a-c,c*2,c*2);const h=a+c-2*c*(.08+.92*o),d=c*(.05+.12*kt),u=i.createLinearGradient(0,a-c,0,a+c);u.addColorStop(0,l?"#ff9aa6":"#bfeaff"),u.addColorStop(.45,f),u.addColorStop(1,l?"#5a1626":p.manaDeep),i.fillStyle=u,i.beginPath(),i.moveTo(e-c,a+c);for(let x=0;x<=8;x++)i.lineTo(e-c+x/8*2*c,h+Math.sin(x*.9+s*3.2)*d);i.lineTo(e+c,a+c),i.closePath(),i.fill(),i.globalCompositeOperation="lighter",i.strokeStyle="rgba(255,255,255,0.22)",i.lineWidth=Math.max(1,c*.09);for(let x=0;x<3;x++){const g=s*(.9+x*.5)+x*2.1;i.beginPath(),i.arc(e,a,c*(.3+x*.24),g,g+1.5),i.stroke()}i.restore(),i.strokeStyle=l?"#ffb3bd":"#cfeeff",i.globalAlpha=.7,i.lineWidth=Math.max(1,n*.04),i.beginPath(),i.arc(e,a,c,0,Math.PI*2),i.stroke(),i.globalAlpha=1,i.fillStyle="rgba(255,255,255,0.5)",i.beginPath(),i.ellipse(e-c*.35,a-c*.42,c*.24,c*.13,-.6,0,Math.PI*2),i.fill();const w=n*.78+(c-n*.6);i.strokeStyle=p.gold,i.lineWidth=Math.max(1.5,n*.07),i.beginPath(),i.arc(e,a,w,-Math.PI/2,-Math.PI/2+Math.PI*2*o),i.stroke();for(let x=0;x<2;x++){const g=s*1.4+x*Math.PI;X(i,e+Math.cos(g)*w,a+Math.sin(g)*w,Math.max(2.5,n*.17),p.gold,.9)}}function Ea(t,e,a,n,s){t==="brute"?rt({x:e-n*(1+s),y:a-n*(1-s),w:n*2*(1+s),h:n*2*(1-s)},n*.35):t==="runner"?ma(e,a,n*1.2):(i.beginPath(),i.ellipse(e,a,n*(1+s),n*(1-s),0,0,Math.PI*2))}function ni(t,e,a,n,s,o){var I;const l=Math.sin(o*9+e.wobble)*s*.04,r=a(e.x),c=n(e.y)+l,f=s*jt[e.type].size*(e.dist<.5?.35+1.3*e.dist:1),h=jt[e.type].col,d=Math.sin(o*9+e.wobble+1.2)*.07,u=((I=t.waves[e.wave])==null?void 0:I.armor)??0,w=u>0?Math.min(1,e.armor/u):0;if(e.type==="giant"&&X(i,r,c,f*2.5,"#9fd4ff",.24+.07*Math.sin(o*2+e.wobble)),e.type==="runner"){i.fillStyle=h;for(let S=2;S>=1;S--){const[B,O]=Ke(t,e.dist-S*.24);i.globalAlpha=.34/S,ma(a(B),n(O)+l,f*1.2*(1-S*.12)),i.fill()}i.globalAlpha=1}if(i.fillStyle="#00000066",i.beginPath(),i.ellipse(r,c+f*.8,f*.9,f*.35,0,0,Math.PI*2),i.fill(),i.fillStyle=e.slowT>0?He(h,.3):h,Ea(e.type,r,c,f,d),i.fill(),e.type==="brute")i.strokeStyle="#d6dbe8",i.lineWidth=Math.max(.5,s*.07*w),i.stroke();else if(e.armor>.5&&w>0){const S=Math.max(1,s*.07*w);i.strokeStyle="#d6dbe8",i.lineWidth=S,i.beginPath(),i.arc(r,c,Math.max(1,f-S/2),0,Math.PI*2),i.stroke()}const x=ve.get(e.id),g=x===void 0?0:1-(Gt-x)/.12;if(g>0&&(i.globalAlpha=g*.8,i.fillStyle="#ffffff",Ea(e.type,r,c,f,d),i.fill(),i.globalAlpha=1),e.ward>0&&(i.strokeStyle="rgba(207,216,255,0.9)",i.lineWidth=Math.max(1,s*.025*Math.min(4,1+e.ward/3)),i.setLineDash([f*.7,f*.28]),i.lineDashOffset=-o*f*2.5,i.beginPath(),i.arc(r,c,f+s*.09,0,Math.PI*2),i.stroke(),i.setLineDash([])),e.regen>0&&e.poisonT<=0&&Fo(r,c-f*1.05,Math.max(2,f*.35),"#d8fff0"),e.slowT>0){i.strokeStyle="#7cc4ff",i.lineWidth=Math.max(1,s*.05),i.beginPath(),i.arc(r,c,f+s*.05,0,Math.PI*2);for(let S=0;S<6;S++){const B=S/6*Math.PI*2+e.wobble;i.moveTo(r+Math.cos(B)*(f+s*.05),c+Math.sin(B)*(f+s*.05)),i.lineTo(r+Math.cos(B)*(f+s*.15),c+Math.sin(B)*(f+s*.15))}i.stroke()}if(e.poisonT>0&&(i.fillStyle="rgba(79,224,106,0.8)",i.beginPath(),i.arc(r+f*.5,c-f*.6,Math.max(1.5,s*.06),0,Math.PI*2),i.fill()),i.fillStyle="#ffe9a8",i.fillRect(r-f*.4,c-f*.2,Math.max(1,f*.22),Math.max(1,f*.22)),i.fillRect(r+f*.18,c-f*.2,Math.max(1,f*.22),Math.max(1,f*.22)),e.hp<e.maxHp){const S=Math.max(s*.6,f*2);i.fillStyle="#000a",i.fillRect(r-S/2,c-f-s*.16,S,Math.max(2,s*.08)),i.fillStyle=e.hp/e.maxHp>.4?p.ok:p.danger,i.fillRect(r-S/2,c-f-s*.16,S*Math.max(0,e.hp/e.maxHp),Math.max(2,s*.08))}}function ai(t){if(!Ot)return;const e=1.9,a=Y-Ot.t0;if(a>e){Ot=null;return}const n=Dt(a/.3),s=Dt((e-a)/.4),o=t.field,l=Math.max(13,Math.min(28,o.w/18)),r=o.y+o.h*.3,c=de?0:(an(n)-1)*o.w*.35+(1-an(s))*o.w*.35;i.save(),i.beginPath(),i.rect(o.x,o.y,o.w,o.h),i.clip(),i.globalAlpha=Math.min(n,s);const f=i.createLinearGradient(o.x,0,o.x+o.w,0);f.addColorStop(0,"rgba(8,9,18,0)"),f.addColorStop(.3,"rgba(8,9,18,0.35)"),f.addColorStop(.7,"rgba(8,9,18,0.35)"),f.addColorStop(1,"rgba(8,9,18,0)"),i.fillStyle=f,i.fillRect(o.x,r-l*1.25,o.w,l*2.5),X(i,o.x+o.w/2+c,r,l*5,Ot.color,.3),i.fillStyle=Ot.color,i.fillRect(o.x+o.w*.2+c*.6,r-l*1.25,o.w*.6,1.5),i.fillRect(o.x+o.w*.2-c*.6,r+l*1.25-1.5,o.w*.6,1.5),$(Ot.title,o.x+o.w/2+c,r-l*.32,l,"#ffffff","center",800),$(Ot.sub,o.x+o.w/2-c*.5,r+l*.6,nt(Ot.sub,o.w-16,l*.5,600),Ot.color,"center",600),i.restore()}function Oo(t){return`×${t>=9.95?"10":t>=1?t.toFixed(1):t.toFixed(2)}`}function oi(t,e,a,n){if(t.furthest<=0)return;const s=Math.floor(t.furthest/t.pathLength),[o,l]=Ke(t,t.furthest-s*t.pathLength),r=sa(t,t.furthest),c=r>=1?p.gold:p.danger,f=e(o),h=a(l),d=n*.16;i.strokeStyle=c,i.lineWidth=Math.max(1.5,n*.06),i.beginPath(),i.moveTo(f,h),i.lineTo(f,h-n*.55),i.stroke(),i.fillStyle=c,i.beginPath(),i.arc(f,h-n*.55,d,0,Math.PI*2),i.fill();const u=Oo(r);i.font=`800 ${Math.max(10,n*.34)}px ${xt}`,i.textAlign="center",i.textBaseline="middle",i.lineWidth=3,i.strokeStyle="#000",i.strokeText(u,f,h-n*.95),i.fillStyle=c,i.fillText(u,f,h-n*.95)}let Lt={x:0,y:0,w:0,h:0};function si(t,e){const a=yn(t,pe,me),n=Math.max(10,Math.min(14,e.field.w/46)),s=`Score ${ut(a.total)}`,o=`${ut(a.base)} ×${a.distance.toFixed(2)} ×${a.forge.toFixed(2)} ×${a.domination.toFixed(2)} ×${a.focus.toFixed(2)} ×${a.noPause} + ${ut(a.early)}`,l=nt(o,e.field.w*.62,n*.8,600);i.font=`700 ${n}px ${xt}`;let r=i.measureText(s).width;if(i.font=`600 ${l}px ${xt}`,r=Math.max(r,i.measureText(o).width)+16,Lt={x:e.field.x+e.field.w-r-6,y:e.field.y+6,w:r,h:n*1.6+l*1.5},t.state!=="playing")return;rt(Lt,8),i.fillStyle="#0d1020cc",i.fill();const c=Lt.x+Lt.w/2;$(s,c,Lt.y+n*.95,n,p.gold,"center",700);const f=a.focus<.995||a.noPause<2;$(o,c,Lt.y+n*1.6+l*.55,l,f?p.danger:p.textDim,"center",600)}function ii(t,e){if(t.state!=="playing")return;let a="";const n=t.structures.filter(c=>c.kind==="tower"),s=t.inventory.some(c=>c);if(j?a=j==="tower"?"Tap a grass tile to build a tower":"Tap a path tile to build a trap":n.length===0?a="Tap Tower, then tap grass beside the path":!n.some(c=>c.gem)&&!s?a="Tap a gem colour to forge a gem":s&&n.some(c=>!c.gem)&&t.nextWave<3?a="Drag the gem onto a tower (or tap gem, then tower)":t.nextWave===0?a=`First wave in ${Math.ceil(t.waveTimer)}s · tap Next wave for bonus mana`:t.fieldIndex===0&&t.nextWave<4&&t.time<160&&(a="Drop a gem on an equal gem to combine into a higher grade"),!a)return;const o=Math.max(10,Math.min(14,e.field.w/46));i.font=`600 ${o}px ${xt}`;const l=i.measureText(a).width+20,r={x:e.field.x+e.field.w/2-l/2,y:e.field.y+6,w:l,h:o*1.9};r.x+r.w>Lt.x-4&&(r.y=Lt.y+Lt.h+4),rt(r,8),i.fillStyle="#0d1020cc",i.fill(),$(a,r.x+r.w/2,r.y+r.h/2,o,p.gold,"center",600)}function li(t,e){var xa;const a=e.panel;i.fillStyle=p.panel,i.fillRect(a.x,a.y,a.w,a.h),i.strokeStyle=p.panelEdge,i.beginPath(),e.portrait?(i.moveTo(a.x,a.y+.5),i.lineTo(a.x+a.w,a.y+.5)):(i.moveTo(a.x+.5,a.y),i.lineTo(a.x+.5,a.y+a.h)),i.stroke();const n=8,s=6;let o,l;if(e.portrait){const P=Math.round(a.w*.56);o={x:a.x+n,y:a.y+n,w:P-n*1.5,h:a.h-n*2},l={x:a.x+P+n*.5,y:a.y+n,w:a.w-P-n*1.5,h:a.h-n*2}}else o={x:a.x+n,y:a.y+n,w:a.w-n*2,h:a.h-n*2},l=o;const r=3,c=4,f=e.portrait?5.4:6.25+r*1.2,h=Math.max(18,Math.min(e.portrait?56:46,(o.h-s*(e.portrait?5:8))/f));let d=o.y;const u={x:o.x+Math.sin(Y*55)*We*12,y:d,w:o.w,h:h*.8};De=u,rt(u,6),i.fillStyle="#0a0d18",i.fill();const w=Math.max(0,Math.min(1,t.mana/t.manaCap)),x={x:u.x,y:u.y,w:Math.max(6,u.w*w),h:u.h},g=w<.18&&t.state==="playing",I=i.createLinearGradient(u.x,0,u.x+u.w,0);I.addColorStop(0,g?"#6a1a2c":p.manaDeep),I.addColorStop(1,g?p.danger:p.mana),i.save(),rt(x,6),i.fillStyle=I,i.fill(),i.clip();const S=u.x+(Y*.3%1.5-.25)*u.w,B=i.createLinearGradient(S-u.h*2,0,S+u.h*2,0);B.addColorStop(0,"rgba(255,255,255,0)"),B.addColorStop(.5,"rgba(255,255,255,0.28)"),B.addColorStop(1,"rgba(255,255,255,0)"),i.fillStyle=B,i.fillRect(u.x,u.y,u.w,u.h),Nt>0&&(i.fillStyle=`rgba(255,255,255,${.45*Nt})`,i.fillRect(u.x,u.y,u.w,u.h)),i.restore(),Nt>0&&X(i,x.x+x.w,u.y+u.h/2,u.h*1.6,"#eaf8ff",Nt),i.strokeStyle=We>0||g?p.danger:p.panelEdge,rt(u,6),i.stroke();const O=Math.round(t.bonusPot*t.manaMult),W=`${Math.floor(t.mana)} / ${t.manaCap} mana`+(O>0?` · pot +${O}`:"");$(W,u.x+u.w/2,u.y+u.h/2,nt(W,u.w-10,Math.min(14,u.h*.55),800),"#fff","center",800),d+=u.h+s;const M={x:o.x,y:d,w:o.w,h:h*.8},G=t.nextWave>=t.waves.length?`All ${t.waves.length} waves out`:`Wave ${t.nextWave}/${t.waves.length} · next ${Math.ceil(t.waveTimer)}s`;$(G,M.x,M.y+M.h/2,nt(G,M.w*.62,Math.min(13,M.h*.5)),p.text,"left",600),Xn=[];for(let P=0;P<3;P++){const at=t.waves[t.nextWave+P];if(!at)break;const K=M.x+M.w-(2.5-P)*Math.min(M.h,M.w*.12),dt=Math.min(M.h*.42,M.w*.05);ci(at,K,M.y+M.h/2,dt,P===0),Xn.push({x:K,y:M.y+M.h/2,r:Math.max(dt*1.3,10),def:at,index:t.nextWave+P})}d+=M.h+s;const D=(o.w-s*2)/3;ht({x:o.x,y:d,w:D,h},"tower","Tower",{sub:`${Bn(t)}`,active:j==="tower",disabled:t.mana<Bn(t),tap:()=>j=j==="tower"?null:"tower"}),ht({x:o.x+D+s,y:d,w:D,h},"trap","Trap",{sub:`${Nn(t)}`,active:j==="trap",disabled:t.mana<Nn(t),tap:()=>j=j==="trap"?null:"trap"}),ht({x:o.x+(D+s)*2,y:d,w:D,h},"pool","Pool +",{sub:`${Hn(t)}`,disabled:t.mana<Hn(t),tap:$o}),d+=h+s;const m=t.field.colors,b=m.length+1,E=Math.min(h*1.6,(o.w-s*(b-1))/b),T=o.x+(o.w-(E*b+s*(b-1)))/2,F=Ae(Q);m.forEach((P,at)=>{const K={x:T+at*(E+s),y:d,w:E,h},dt=!0,Tt=y.inside&&Xt(K,y.x,y.y);rt(K,8),i.fillStyle=Tt&&dt?"#1d2138":"#141828",i.fill(),i.strokeStyle=He(qe(P),-.3),i.stroke(),i.globalAlpha=t.mana>=F?1:.4,ce(Yn(P),K.x+K.w/2,K.y+K.h/2,Math.min(K.w,K.h)*.32),i.globalAlpha=1,$(String(at+1),K.x+4,K.y+7,9,p.textDim,"left",600),et.push({...K,id:`gem-${P}`,forge:P,tap:()=>Po(P)})});{const P={x:T+m.length*(E+s),y:d,w:E,h},at=y.inside&&Xt(P,y.x,y.y);rt(P,8),i.fillStyle=at?"#1d2138":"#141828",i.fill(),i.strokeStyle=p.stoneHi,i.stroke(),i.globalAlpha=t.mana>=na(Q)?1:.4;const K=Math.min(P.w,P.h)*.2;for(let dt=0;dt<3;dt++){const Tt=-Math.PI/2+dt*Math.PI*2/3,kn=m[dt%m.length];ce(Yn(kn),P.x+P.w/2+Math.cos(Tt)*K,P.y+P.h/2+Math.sin(Tt)*K*.9,K*.85,!1,!1)}i.globalAlpha=1,$("0",P.x+4,P.y+7,9,p.textDim,"left",600),et.push({...P,id:"gem-random",random:!0,tap:To})}d+=h+s;const ct=h*.8,st=Math.min(ct*1.3,o.w*.18);ht({x:o.x,y:d,w:st,h:ct},"grade-","−",{tap:()=>Q=Math.max(1,Q-1)}),ht({x:o.x+o.w-st,y:d,w:st,h:ct},"grade+","+",{tap:()=>Q=Math.min(vo,Q+1)});const ft=`Forge grade ${Q} · ${F} mana`;$(ft,o.x+o.w/2,d+ct/2,nt(ft,o.w-st*2-10,Math.min(13,ct*.5)),t.mana>=F?p.text:p.danger,"center",600),d+=ct+s;let it;e.portrait?it={x:l.x,y:l.y,w:l.w,h:l.h-h-s}:(it={x:o.x,y:d,w:o.w,h:r*h*1.2+s*(r-1)},d+=it.h+s);const Wt=Math.min((it.w-s*(c-1))/c,(it.h-s*(r-1))/r),vn=it.x+(it.w-(Wt*c+s*(c-1)))/2;for(let P=0;P<oo;P++){const at={x:vn+P%c*(Wt+s),y:it.y+Math.floor(P/c)*(Wt+s),w:Wt,h:Wt},K={kind:"inv",index:P},dt=t.inventory[P],Tt=(R==null?void 0:R.kind)==="inv"&&R.index===P;rt(at,8),i.fillStyle="#0c0f1b",i.fill(),i.strokeStyle=Tt?p.gold:y.dragging&&Xt(at,y.x,y.y)?p.ok:p.panelEdge,i.lineWidth=Tt?2:1,i.stroke(),i.lineWidth=1;const kn=y.dragging&&((xa=y.dragRef)==null?void 0:xa.kind)==="inv"&&y.dragRef.index===P;dt&&!kn&&ce(dt,at.x+at.w/2,at.y+at.h/2,Wt*.32),et.push({...at,id:`inv-${P}`,gemRef:dt?K:void 0,drop:K})}const mt=e.portrait?l.y+l.h-h:d,bt=e.portrait?l:o;if(e.portrait){const P=(bt.w-s)/2;ht({x:bt.x,y:mt,w:P,h},"sell","Sell",{sub:Oa(t),disabled:!R,accent:p.goldDim,tap:Ga}),et[et.length-1].drop="sell",Da(t,{x:bt.x+P+s,y:mt,w:P,h});const at=mt-s-(it.y+r*Wt+(r-1)*s)-s,K=at>=40;if(K){const Tt=Math.min(48,at);Go({x:bt.x+bt.w-Tt,y:mt-s-Tt,w:Tt,h:Tt})}const dt=o.y+o.h-h;dt>=d-2?In({x:o.x,y:dt,w:o.w,h},s,!K):In({x:o.x,y:d,w:o.w,h:Math.max(20,o.y+o.h-d)},s,!K)}else{const P=(bt.w-s)/2;ht({x:bt.x,y:mt,w:P,h},"sell","Sell",{sub:Oa(t),disabled:!R,accent:p.goldDim,tap:Ga}),et[et.length-1].drop="sell",Da(t,{x:bt.x+P+s,y:mt,w:P,h}),In({x:bt.x,y:mt+h+s,w:bt.w,h:h*.85},s,!0)}}function Oa(t){const e=R?$t(t,R):null;return e?`+${ea(e)}`:"drop gem"}function Ga(){R&&Pe(R,"sell"),R=null}function Da(t,e){const a=t.waves.length-t.nextWave,n=Math.round(co(t)*t.manaMult);ht(e,"next","Next wave",{sub:a>0?`+${n}`:"none left",disabled:a===0,accent:p.manaDeep,tap:Ao})}function Go(t){ht(t,"sound","",{tap:la}),ri(t)}function In(t,e,a){const n=a?Math.min(t.h*1.1,t.w*.16):-e,s=(t.w-n-e*3)/3;a&&Go({x:t.x+(s+e)*3,y:t.y,w:n,h:t.h}),ht({x:t.x,y:t.y,w:s,h:t.h},"speed",`${At}× speed`,{tap:()=>At=At===3?1:At+1}),ht({x:t.x+s+e,y:t.y,w:s,h:t.h},"pause",Z?"Resume":"Pause",{active:Z,tap:()=>Z=!Z}),ht({x:t.x+(s+e)*2,y:t.y,w:s,h:t.h},"menu","Fields",{tap:()=>{It="menu",et=[],Z=!1}})}function ri(t){const e=Math.min(t.w,t.h)*.2,a=t.x+t.w/2-e*.5,n=t.y+t.h/2;i.save();const s=H.sound&&xn();i.fillStyle=i.strokeStyle=s?p.text:p.textDim,i.lineWidth=Math.max(1.2,e*.3),i.lineCap="round",i.beginPath(),i.moveTo(a-e*1.2,n-e*.5),i.lineTo(a-e*.5,n-e*.5),i.lineTo(a+e*.4,n-e*1.2),i.lineTo(a+e*.4,n+e*1.2),i.lineTo(a-e*.5,n+e*.5),i.lineTo(a-e*1.2,n+e*.5),i.closePath(),i.fill(),s?(i.beginPath(),i.arc(a+e*.5,n,e*.9,-.8,.8),i.stroke(),H.music&&(i.beginPath(),i.arc(a+e*.5,n,e*1.8,-.8,.8),i.stroke())):(i.strokeStyle=p.danger,i.beginPath(),i.moveTo(a+e*.9,n-e*.7),i.lineTo(a+e*2.1,n+e*.7),i.moveTo(a+e*2.1,n-e*.7),i.lineTo(a+e*.9,n+e*.7),i.stroke()),i.restore()}const Do=4,jt={crawler:{col:"#c9604a",size:.26},swarm:{col:"#d16ad8",size:.16},brute:{col:"#8a93a8",size:.3},giant:{col:"#7fb4d6",size:.42},runner:{col:"#e0a63a",size:.2},warded:{col:"#6f86d6",size:.26},mender:{col:"#3fb6a8",size:.27}};function ma(t,e,a){i.beginPath(),i.moveTo(t,e-a),i.lineTo(t+a,e),i.lineTo(t,e+a),i.lineTo(t-a,e),i.closePath()}function Fo(t,e,a,n){i.fillStyle=n;const s=Math.max(1,a*.4);i.fillRect(t-a,e-s/2,a*2,s),i.fillRect(t-s/2,e-a,s,a*2)}let Xn=[];function ci(t,e,a,n,s){if(i.globalAlpha=s?1:.55,i.fillStyle=jt[t.type].col,t.type==="brute")i.fillRect(e-n*.8,a-n*.8,n*1.6,n*1.6);else if(t.type==="runner")ma(e,a,n*.85),i.fill();else if(t.type==="swarm")for(let o=0;o<3;o++)i.beginPath(),i.arc(e+(o-1)*n*.6,a+(o%2?-n*.3:n*.3),n*.4,0,Math.PI*2),i.fill();else i.beginPath(),i.arc(e,a,t.type==="giant"?n:n*.7,0,Math.PI*2),i.fill(),t.type==="warded"?(i.strokeStyle="#cfd8ff",i.lineWidth=Math.max(1,n*.18),i.beginPath(),i.arc(e,a,n*.95,0,Math.PI*2),i.stroke()):t.type==="mender"&&Fo(e,a,n*.4,"#d8fff0");i.globalAlpha=1}function ya(t,e,a){const n={x:t.field.x+6,y:t.field.y+t.field.h-a-6,w:e,h:a},s=t.tile;return y.inside&&Xt(t.field,y.x,y.y)&&y.x<=n.x+e+s&&y.y>=n.y-s&&(n.x=t.field.x+t.field.w-e-6),n}function fi(t,e){let a=null,n="tower",s="",o="";const l=[],r=M=>{var G;return M.kind==="struct"?((G=t.structures.find(D=>D.id===M.id))==null?void 0:G.kind)??"tower":"tower"},c=y.dragging?y.dragRef:R,f=y.inside?ko(y.x,y.y):null;if(c){a=$t(t,c),n=r(c);const M=f&&f!=="sell"&&!Fe(f,c)?$t(t,f):null;if(a&&M){const G=zt(a),D=zt(M);n=r(f),a=ro(a,M,0),a.kills=0,s=`G${G} + G${D} → G${zt(a)} ${ze(a)}`,G!==D&&l.push(["Unequal grades",`${Math.round(zn(a)*100)}% to G${zt(a)+1}`])}}else if(y.inside&&!y.down){const M=gn(y.x,y.y),G=da(M);if(G)a=$t(t,G);else if(M!=null&&M.random){di(t,e);return}else if(M!=null&&M.forge){a=Yn(M.forge);const D=Ae(Q);s=`Forge G${Q} ${ze(a)}`,l.push(["Cost",`${D} mana`]),t.mana<D&&(o=p.danger)}else{const D=bn(y.x,y.y);D!=null&&D.gem&&(a=D.gem,n=D.kind)}}if(!a){y.inside&&!y.down&&!c&&hi(t,e);return}const h=pn(a,n),d=[...l];d.push(["Damage",`${Math.round(h.damage*.8)}–${Math.round(h.damage*1.2)}`]),d.push(["Rate",`${h.rate.toFixed(2)}/s`]),d.push(["Range",h.range.toFixed(1)]),h.splash>.005?d.push(["Splash",`${Math.round(h.splash*100)}%`]):n==="trap"&&(a.comp.red??0)>.005&&d.push(["Splash","none in traps"]),h.leech>.05&&d.push(["Mana/hit",h.leech.toFixed(1)]),h.crit>.005&&d.push(["Crit",`${Math.round(h.crit*100)}% ×${h.critMult.toFixed(1)}`]),h.poison>.05&&d.push(["Poison",`${h.poison.toFixed(1)}/s`]),h.slow>.005&&d.push(["Slow",`${Math.round(h.slow*100)}%`]),h.tear>.01&&d.push(["Armor tear",h.tear.toFixed(1)]),d.push(["Worth",`${+a.mass.toFixed(2)} × G1`]);const u=io(a);u<.999&&d.push(["Purity",`${Math.round(u*100)}%`]);const w=zn(a);w>.005&&!l.length&&d.push(["Next grade",`${Math.round(w*100)}%`]),s||d.push(["Kills",String(a.kills)]);const x=Math.max(10,Math.min(13,e.field.w/55)),g=x*1.45,I=x*15.5,S=g*(d.length+2.6),B=ya(e,I,S);rt(B,8),i.fillStyle="#0b0e1aee",i.fill(),i.strokeStyle=_t(a),i.stroke();const O=(s||`G${zt(a)} ${ze(a)}`)+(n==="trap"?" (trap)":"");$(O,B.x+8,B.y+g*.8,nt(O,I-14,x+1,800),o||_t(a),"left",800);const W=ui(a);$(W,B.x+8,B.y+g*1.85,nt(W,I-14,x,600),p.gold,"left",600),d.forEach(([M,G],D)=>{$(M,B.x+8,B.y+g*(D+2.9),x,p.textDim,"left",500),$(G,B.x+B.w-8,B.y+g*(D+2.9),x,p.text,"right",700)})}function hi(t,e){var w;const a=Xn.find(x=>Math.hypot(y.x-x.x,y.y-x.y)<=x.r);let n=null;if(!a&&Xt(e.field,y.x,y.y)){let x=Math.max(10,e.tile*.5);for(const g of t.monsters){const I=Math.hypot(e.field.x+g.x*e.tile-y.x,e.field.y+g.y*e.tile-y.y);I<x&&(x=I,n=g)}}const s=(a==null?void 0:a.def.type)??(n==null?void 0:n.type);if(!s)return;const o=no[s],l=[];if(a){const x=a.def;l.push(["Wave",`${a.index+1} · ${x.count} monsters`]),l.push(["Health",String(Math.round(x.hp))]),x.armor>0&&l.push(["Armour at start",String(x.armor)]),x.ward&&l.push(["Ward",`${x.ward} hits`]),x.regen&&l.push(["Heals",`${Math.round(x.regen*100)}% health/s`])}else if(n){l.push(["Health",`${Math.ceil(n.hp)} / ${Math.round(n.maxHp)}`]);const x=((w=t.waves[n.wave])==null?void 0:w.armor)??0;x>0&&l.push(["Armour",n.armor<x?`${Math.round(n.armor)} of ${x}`:String(x)]),n.ward>0&&l.push(["Ward",`${n.ward} hits`]),n.regen>0&&l.push(["Heals",n.poisonT>0?"stopped by poison":`${Math.round(n.regen)}/s`]),n.slowT>0&&l.push(["Slowed",`${Math.round(n.slowF*100)}%`])}const r=Math.max(10,Math.min(13,e.field.w/55)),c=r*1.45,f=r*17,h=c*(l.length+3.6),d=ya(e,f,h);rt(d,8),i.fillStyle="#0b0e1aee",i.fill(),i.strokeStyle=jt[s].col,i.stroke(),$(o.name,d.x+8,d.y+c*.8,r+1,jt[s].col,"left",800),$(o.trait,d.x+8,d.y+c*1.85,nt(o.trait,f-14,r,600),p.text,"left",600);const u=`Beat it with: ${o.counter}`;$(u,d.x+8,d.y+c*2.9,nt(u,f-14,r,600),p.gold,"left",600),l.forEach(([x,g],I)=>{$(x,d.x+8,d.y+c*(I+3.9),r,p.textDim,"left",500),$(g,d.x+d.w-8,d.y+c*(I+3.9),r,p.text,"right",700)})}function di(t,e){const a=na(Q),n=t.field.colors.map(u=>ue[u].name).join(", "),s=[["Cost",`${a} mana`],["Forged singly",`${Ae(Q)*le} mana`],["Free slots needed",String(le)]],o=Math.max(10,Math.min(13,e.field.w/55)),l=o*1.45,r=o*15.5,c=l*(s.length+3.6),f=ya(e,r,c);rt(f,8),i.fillStyle="#0b0e1aee",i.fill(),i.strokeStyle=p.goldDim,i.stroke();const h=`${le} random G${Q} gems`;$(h,f.x+8,f.y+l*.8,nt(h,r-14,o+1,800),t.mana>=a?p.gold:p.danger,"left",800);const d=`Saves ${Ae(Q)*le-a} mana`;$(d,f.x+8,f.y+l*1.85,nt(d,r-14,o,600),p.gold,"left",600),$(n,f.x+8,f.y+l*2.9,nt(n,r-14,o,500),p.textDim,"left",500),s.forEach(([u,w],x)=>{$(u,f.x+8,f.y+l*(x+3.9),o,p.textDim,"left",500),$(w,f.x+f.w-8,f.y+l*(x+3.9),o,p.text,"right",700)})}function ui(t){const e=Rt.filter(a=>(t.comp[a]??0)>=.15).sort((a,n)=>(t.comp[n]??0)-(t.comp[a]??0));return e.length<=1?ue[un(t)].special:e.map(a=>ue[a].short).join(" + ")}function ze(t){const e=Rt.filter(a=>(t.comp[a]??0)>=.15).sort((a,n)=>(t.comp[n]??0)-(t.comp[a]??0));return e.length<=1?ue[un(t)].name:e.slice(0,2).map(a=>ue[a].name).join("-")}function pi(t,e){const a=t.state==="won",n=Y-_n-(a?.35:1),s=Dt(n/.5);if(i.fillStyle=`rgba(5,6,12,${.72*s})`,i.fillRect(0,0,N,z),Dn({ctx:i,ox:0,oy:0,unit:1},gt,xt),s<=0)return;const o=e.field.x+e.field.w/2,l=Math.min(44,e.field.w/9),r=Math.max(15,l*.5),c=Math.max(34,l*.9),f=a?6:0;let h=z/2-(l*(a?2.6:2)+f*r+c)/2;i.save(),i.globalAlpha=s;const d=a?"Field defended!":"The orb shattered",u=nt(d,(e.field.w-12)/1.03,l,800)*(.75+.25*nn(s));if(X(i,o,h+l*.5,l*5,a?p.gold:p.danger,.22+.06*Math.sin(Y*2.5)),$(d,o,h+l*.5,u,a?p.gold:p.danger,"center",800),h+=l*1.2,a){const g=po(t);for(let T=0;T<3;T++){const F=o+(T-1)*l*1.1,ct=h+l*.35,st=(n-.35-T*.32)/.3,ft=T<g&&st>0;ft&&Vn===T&&(Vn++,_.star(T),yt(gt,F,ct,14,p.gold,{speed:l*4,life:.5,size:2}),dn(gt,F,ct,l*1.2,.45,"#fff8dc")),Eo(F,ct,l*.45*(ft?nn(Dt(st))*.4+.6:1),ft)}h+=l*.9;const I=`★ won ${t.banished===0?"★":"☆"} nothing reached the orb ${t.built<=5?"★":"☆"} five or fewer buildings`;$(I,o,h+l*.2,nt(I,e.field.w-20,Math.max(10,l*.3),500),p.textDim,"center",500),h+=l*.7;const S=yn(t,pe,me),B=Math.min(360,e.field.w-40),O=o-B/2,W=o+B/2,M=Math.max(11,Math.min(15,r*.7)),G=T=>`×${T.toFixed(2)}`,D=[["Kills","health killed",ut(S.base)],["Distance","how early they died, by health",G(S.distance)],["Forge",`${ut(S.gems+t.buildMana)} mana in gems and buildings`,G(S.forge)],["Domination",`${t.built} built`,G(S.domination)],["Focus",`${S.focus.toFixed(2)}× average speed`,G(S.focus)],["No pause",me?"never played during a pause":"played during a pause",`×${S.noPause}`],["Early kills",`+ furthest monster at ${Oo(S.earlyMult)} · ${t.waves.length} waves`,ut(S.early)]];rt({x:O-10,y:h-4,w:B+20,h:r*(D.length+1.2)+8},8),i.fillStyle="#0b0e1aee",i.fill(),D.forEach(([T,F,ct],st)=>{const ft=h+r*(st+.5);$(T,O,ft,M,p.text,"left",700),$(F,O+M*6.9,ft,nt(F,B-M*11.1,M*.85,500),p.textDim,"left",500);const it=T==="Focus"&&S.focus<.995||T==="No pause"&&S.noPause<2;$(ct,W,ft,M,it?p.danger:p.text,"right",700)}),h+=r*D.length,i.strokeStyle=p.panelEdge,i.beginPath(),i.moveTo(O,h+1),i.lineTo(W,h+1),i.stroke();const m=Math.round(S.total*an(Dt((n-.5)/1.1))),b=Ne?"new best!":`best ${H.best[t.fieldIndex]}`;$("Score",O,h+r*.55,M*1.15,p.gold,"left",800),$(b,O+M*6,h+r*.55,M*.85,Ne?p.gold:p.textDim,"left",600),$(ut(m),W,h+r*.55,M*1.15,p.gold,"right",800);const E=Math.max(26,r*1.1);s>=.7&&ht({x:o-E/2+M*2,y:h+r*.55-E/2,w:E,h:E},"formula","?",{active:Ht,tap:()=>Ht=!Ht}),h+=r*1.4}else{const g=`Reached wave ${t.nextWave} of ${t.waves.length}`;$(g,o,h+l*.2,Math.max(12,l*.4),p.textDim,"center",500),h+=l*.8}if(i.restore(),s<.7)return;ha=!0;const w=Math.min(150,e.field.w*.3),x=a?t.fieldIndex+1<Vt.length?"Next field":"Play again":"Retry";ht({x:o-w-6,y:h,w,h:c},"again",x,{active:!0,tap:Co}),ht({x:o+6,y:h,w,h:c},"fields","Fields",{tap:()=>{It="menu",et=[]}}),a&&Ht&&mi(t,e)}function ut(t){return String(Math.round(t)).replace(/\B(?=(\d{3})+(?!\d))/g," ")}function mi(t,e){const a=yn(t,pe,me),n=u=>`×${u.toFixed(2)}`,s=[["Kills","health of every monster killed"],["",`${ut(a.base)} health`],["Distance","each kill’s 10^(1 − d), weighted by health"],["","d: 0 at the portal, 1 at the orb, 2 there a lap later"],["",`×10 at the portal, ×1 at the orb: ${n(a.distance)}`],["Forge","2 × (gems at forge cost + buildings) ÷ field mana"],["",`2 × (${ut(a.gems)} + ${ut(t.buildMana)}) ÷ ${ut(a.fieldMana)} = ${n(a.forge)}`],["Domination","1 + (15 − built)² ÷ 60, ×1 from 15 built"],["",`1 + (15 − ${Math.min(15,t.built)})² ÷ 60 = ${n(a.domination)}`],["Focus","game time ÷ real time, so faster speeds pay"],["",`${Fa(t.time)} ÷ ${Fa(pe)} = ${n(a.focus)}`],["","a pause counts once you play during it"],["No pause","×2, or ×1 once you play during a pause"],["",`×${a.noPause}`],["Early kills","1000 × waves × 10^(1 − d) of the furthest monster"],["",`1000 × ${t.waves.length} × ${a.earlyMult.toFixed(2)} = ${ut(a.early)}`],["Score","kills × the multipliers above + early kills"],["",`${ut(a.base)} × ${a.distance.toFixed(2)} × ${a.forge.toFixed(2)} × ${a.domination.toFixed(2)} × ${a.focus.toFixed(2)} × ${a.noPause} + ${ut(a.early)} = ${ut(a.total)}`],["",`Field mana ${ut(a.fieldMana)}: all its kills, no orange gems.`]],o=12,l=Math.min(560,e.field.w-16),r=Math.max(10,Math.min(14,l/38)),c=r*1.55,f=s.length*c+o*2,h={x:e.field.x+e.field.w/2-l/2,y:Math.max(4,z/2-f/2),w:l,h:f};rt(h,10),i.fillStyle="#0b0e1afa",i.fill(),i.strokeStyle=p.goldDim,i.lineWidth=1,i.stroke();const d=r*7.3;s.forEach(([u,w],x)=>{const g=h.y+o+c*(x+.5);u&&$(u,h.x+o,g,r,u==="Score"?p.gold:p.text,"left",700);const I=u!=="",S=h.w-o*2-d;$(w,h.x+o+d,g,nt(w,S,r*(I?.95:.9),I?600:500),I?p.text:p.textDim,"left",I?600:500)}),et.push({...h,id:"formula-close",tap:()=>Ht=!1})}function Fa(t){const e=Math.round(t);return`${Math.floor(e/60)}:${String(e%60).padStart(2,"0")}`}mo();Ds();requestAnimationFrame(Ro);
</script>
<style>:root{color-scheme:dark;--night: #0b0f1a}html,body,#app{height:100%;margin:0}body{background:var(--night);overflow:hidden;user-select:none;-webkit-user-select:none;-webkit-tap-highlight-color:transparent}#game{display:block;width:100%;height:100%;touch-action:none;cursor:default}
</style>
<meta name="napplet-type" content="gem-warden">
<meta name="napplet-config-schema" content="{"$schema":"http://json-schema.org/draft-07/schema#","$version":1,"type":"object","additionalProperties":false,"properties":{}}">
</head>
<body>
<main id="app" class="app">
<canvas id="game" aria-label="Gem Warden game board"></canvas>
</main>
</body>
</html>
