SOURCE / PINNED RELEASE
Made of little things.
Gem Warden
- Release
- 7c457bfe5427…
- Author-recorded commit
- fb5453b5fc06…
- 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>function Oe(){const t=new Map;function a(s,i){if(t.has(s))throw new Error(`NAP domain "${s}" is already registered`);t.set(s,i)}function n(s){const i=s.type.indexOf(".");if(i<=0)return!1;const l=s.type.slice(0,i),r=t.get(l);return r?(r(s),!0):!1}function e(){return Array.from(t.keys())}return{registerNap:a,dispatch:n,getRegisteredDomains:e}}var ze=Oe(),Be=ze.registerNap,Vt="storage";Be(Vt,t=>{});function _e(){const t=window;if(!t.napplet)throw new Error("window.napplet is unavailable -- runtime did not inject the napplet namespace");return t.napplet}function nt(t){const n=_e()[t];if(!n)throw new Error(`window.napplet.${String(t)} is unavailable -- runtime did not inject this domain`);return n}var ce={getItem(t){return nt("storage").getItem(t)},setItem(t,a){return nt("storage").setItem(t,a)},removeItem(t){return nt("storage").removeItem(t)},keys(){return nt("storage").keys()},instance:{getItem(t){return nt("storage").instance.getItem(t)},setItem(t,a){return nt("storage").instance.setItem(t,a)},removeItem(t){return nt("storage").instance.removeItem(t)},keys(){return nt("storage").instance.keys()}}};function qe(t,a){return!!(t!=null&&t[a])}function fe(t){return qe(window.napplet,t)}const ot=22,st=14,L=["red","orange","yellow","green","blue","purple"],ut={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"}},Ye={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:"Giant",trait:"Huge health, slow",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"}},lt=[{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:[...L],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:[...L],rocks:[[4,5],[13,6],[13,12],[20,1],[2,13]]}],he=12,Ve=24,Xe=40,ue=80;function Xt(t){let a=t>>>0;return()=>{a=a+1831565813>>>0;let n=a;return n=Math.imul(n^n>>>15,n|1),n^=n+Math.imul(n^n>>>7,n|61),((n^n>>>14)>>>0)/4294967296}}function Ft(t,a){return`${t},${a}`}function je(t){const a=new Set;let n=0;for(let e=0;e<t.length-1;e++){const[s,i]=t[e],[l,r]=t[e+1],f=Math.sign(l-s),p=Math.sign(r-i);let u=s,d=i;for(a.add(Ft(u,d));u!==l||d!==r;)u+=f,d+=p,a.add(Ft(u,d));n+=Math.abs(l-s)+Math.abs(r-i)}return{tiles:a,length:n}}function de(t,a){let n=Math.max(0,a);const e=t.pathPoints;for(let i=0;i<e.length-1;i++){const[l,r]=e[i],[f,p]=e[i+1],u=Math.abs(f-l)+Math.abs(p-r);if(n<=u){const d=u===0?0:n/u;return[l+(f-l)*d+.5,r+(p-r)*d+.5]}n-=u}const s=e[e.length-1];return[s[0]+.5,s[1]+.5]}const Ke=[1,.7,1,1.5,1.6,.8];function Qe(t,a,n){const e=a+n*2.5,s=9*Math.pow(1.185,e)*Ke[n],i=Math.floor(a*.3+n*.8+Math.max(0,s*.03-4)),l=s*(n>=2?.08:.04);switch(t){case"swarm":return{type:t,count:18+Math.floor(a*.8),hp:s*.38,armor:Math.floor(i*.5),speed:1.45,interval:.28};case"brute":return{type:t,count:7+Math.floor(a/4),hp:s*1.2,armor:Math.floor(i+2+l),speed:.8,interval:.9};case"giant":return{type:t,count:2+Math.floor((a+n)/6),hp:s*6.5,armor:Math.floor(i*1.5)+1,speed:.55,interval:2.2};case"runner":return{type:t,count:10+Math.floor(a/3),hp:s*.8,armor:Math.floor(i*.5),speed:2.1,interval:.55};case"warded":return{type:t,count:7+Math.floor(a/4),hp:s*.8,armor:i,speed:.9,interval:.8,ward:2+Math.floor(e/4)};case"mender":return{type:t,count:8+Math.floor(a/4),hp:s*1.3,armor:i,speed:.9,interval:.8,regen:.04};default:return{type:t,count:10+Math.floor(a/3),hp:s,armor:i,speed:1,interval:.6}}}function Je(t,a){const n=Xt(1e3+t*77),e=[],s=["swarm","brute"];t>=1&&s.push("runner","warded"),t>=2&&s.push("mender"),t>=3&&s.push("brute","warded");let i="crawler";for(let l=0;l<a;l++){let r="crawler";if(l>0&&(l+1)%5===0)r="giant";else if(l>=2){const f=s.filter(p=>p!==i&&(p!=="brute"||t>0||l>=5));r=n()<.2?"crawler":f[Math.floor(n()*f.length)]??"crawler"}i=r,e.push(Qe(r,l,t))}return e}function Ue(t){const a=lt[t],{tiles:n,length:e}=je(a.path),s=a.path[a.path.length-1];return{fieldIndex:t,field:a,pathTiles:n,pathPoints:a.path,pathLength:e,orb:s,mana:560+t*320,manaCap:900+t*400,poolLevel:0,manaMult:1,structures:[],inventory:new Array(he).fill(null),monsters:[],shots:[],waves:Je(t,a.waves),nextWave:0,waveTimer:Xe,spawnQueue:[],time:0,banished:0,built:0,state:"playing",events:[],nextId:1,rng:Xt(4242+t),manaGained:0}}function Z(t){return Math.floor(Math.log2(t.mass)+1e-9)+1}function Nt(t){return t.mass/Math.pow(2,Z(t)-1)-1}function Ze(t){return Math.log2(t.mass)+1}function yt(t){return ue*Math.pow(2,t-1)}function jt(t){return Math.round(ue*t.mass*.7)}function Ot(t){return 90+30*t.structures.filter(n=>n.kind==="tower").length}function zt(t){return 70+25*t.structures.filter(n=>n.kind==="trap").length}function Bt(t){return Math.round(t.manaCap*.6)}function wt(t){let a=0,n=0,e=0,s=0;for(const l of L){const r=t.comp[l]??0;if(!r)continue;const f=ut[l].hex;a+=parseInt(f.slice(1,3),16)*r,n+=parseInt(f.slice(3,5),16)*r,e+=parseInt(f.slice(5,7),16)*r,s+=r}const i=l=>Math.round(l/s).toString(16).padStart(2,"0");return`#${i(a)}${i(n)}${i(e)}`}function me(t){let a="red",n=-1;for(const e of L){const s=t.comp[e]??0;s>n&&(n=s,a=e)}return a}function pe(t){let a=0;for(const n of L)a+=(t.comp[n]??0)**2;return a}function At(t,a){const n=Ze(t),e=pe(t),s=.5+.5*e,i=u=>(t.comp[u]??0)*s,l=a==="trap",r=l?2.5:1,f=4*Math.pow(1.85,n-1);return{damage:f*(l?.45:1)*(.8+.2*e),range:l?.85:Math.min(5.2,2.7+.16*n),rate:(1+.07*n)*(l?1.4:1),splash:Math.min(.9,i("red")*(.35+.04*n)*r),leech:i("orange")*(.6+.55*n)*r,crit:Math.min(.6,i("yellow")*(.12+.015*n)*r),critMult:2+.12*n,poison:f*i("green")*(.18+.03*n)*(l?1.3:1),slow:Math.min(.75,i("blue")*(.25+.025*n)*r),tear:f*i("purple")*.3*r}}function Pt(t){return t.nextId++}function Wt(t,a,n){return t.mana<a?(t.events.push({type:"error",message:`Not enough mana for ${n}`}),!1):(t.mana-=a,!0)}function kt(t,a){const n=a*t.manaMult;t.manaGained+=n,t.mana=Math.min(t.manaCap,t.mana+n)}function xe(t){return t.inventory.findIndex(a=>a===null)}function Le(t,a,n){if(!t.field.colors.includes(a))return-1;const e=xe(t);return e<0?(t.events.push({type:"error",message:"Inventory is full"}),-1):Wt(t,yt(n),"that gem")?(t.inventory[e]={id:Pt(t),mass:Math.pow(2,n-1),comp:{[a]:1},kills:0},e):-1}const ht=3;function Kt(t){return yt(t)*2.5}function tn(t,a){if(t.inventory.filter(e=>e===null).length<ht)return t.events.push({type:"error",message:`Random batch needs ${ht} free slots`}),[];if(!Wt(t,Kt(a),"a random batch"))return[];const n=[];for(let e=0;e<ht;e++){const s=t.field.colors[Math.floor(t.rng()*t.field.colors.length)],i=xe(t);t.inventory[i]={id:Pt(t),mass:Math.pow(2,a-1),comp:{[s]:1},kills:0},n.push(i)}return n}function Qt(t,a,n,e){if(n<0||e<0||n>=ot||e>=st||t.structures.some(r=>r.x===n&&r.y===e)||n===t.orb[0]&&e===t.orb[1])return!1;const[s,i]=t.pathPoints[0];if(n===s&&e===i||t.field.rocks.some(([r,f])=>r===n&&f===e))return!1;const l=t.pathTiles.has(Ft(n,e));return a==="trap"?l:!l}function en(t,a,n,e){if(!Qt(t,a,n,e))return null;const s=a==="tower"?Ot(t):zt(t);if(!Wt(t,s,`a ${a}`))return null;const i={id:Pt(t),kind:a,x:n,y:e,gem:null,cooldown:0,aim:-Math.PI/2};return t.structures.push(i),t.built++,t.events.push({type:"build",x:n+.5,y:e+.5}),i}function nn(t){return Wt(t,Bt(t),"expanding the pool")?(t.poolLevel++,t.manaCap=Math.round(t.manaCap*1.6),t.manaMult=1+.15*t.poolLevel,!0):!1}function X(t,a){var n;return a.kind==="inv"?t.inventory[a.index]??null:((n=t.structures.find(e=>e.id===a.id))==null?void 0:n.gem)??null}function bt(t,a,n){if(a.kind==="inv")t.inventory[a.index]=n;else{const e=t.structures.find(s=>s.id===a.id);e&&(e.gem=n,e.cooldown=Math.max(e.cooldown,.4))}}function vt(t,a){return t.kind===a.kind&&(t.kind==="inv"?t.index===a.index:t.id===a.id)}function ye(t,a,n){const e=t.mass+a.mass,s={};for(const i of L){const l=((t.comp[i]??0)*t.mass+(a.comp[i]??0)*a.mass)/e;l>.001&&(s[i]=l)}return{id:n,mass:e,comp:s,kills:t.kills+a.kills}}function an(t,a,n){if(vt(a,n))return!1;const e=X(t,a);if(!e||n.kind==="struct"&&!t.structures.some(l=>l.id===n.id))return!1;const s=X(t,n);if(!s)return bt(t,a,null),bt(t,n,e),!0;const i=ye(e,s,Pt(t));return bt(t,a,null),bt(t,n,i),t.events.push({type:"combine",grade:Z(i)}),!0}function on(t,a){const n=X(t,a);return n?(bt(t,a,null),t.mana=Math.min(t.manaCap,t.mana+jt(n)),!0):!1}function sn(t){if(t.nextWave>=t.waves.length||t.state!=="playing")return 0;const a=Math.round(t.waveTimer*(1.5+t.nextWave*.6+t.fieldIndex));return kt(t,a),we(t),a}function we(t){const a=t.waves[t.nextWave];t.spawnQueue.push({def:a,left:a.count,timer:0}),t.events.push({type:"wave",index:t.nextWave}),t.nextWave++,t.waveTimer=Ve}function ln(t,a){const n=a.hp*(.92+t.rng()*.16),[e,s]=de(t,0);t.monsters.push({id:Pt(t),type:a.type,dist:0,hp:n,maxHp:n,armor:a.armor,speed:a.speed*(.95+t.rng()*.1),slowF:0,slowT:0,poisonDps:0,poisonT:0,ward:a.ward??0,regen:(a.regen??0)*n,mana:4+n*.3+a.armor*.1+(a.ward??0)*2,wobble:t.rng()*Math.PI*2,x:e,y:s})}function ee(t,a,n,e){if(a.hp<=0)return;if(a.ward>0){a.ward--;return}const s=Math.max(n*.15,n-a.armor);a.hp-=s,a.hp<=0&&(kt(t,a.mana),e!=null&&e.gem&&e.gem.kills++,t.events.push({type:"kill",x:a.x,y:a.y,mana:a.mana*t.manaMult}))}function ne(t,a,n){const e=a.gem;if(!e||n.hp<=0)return;const s=At(e,a.kind);if(n.ward>0){n.ward--,t.events.push({type:"hit",x:n.x,y:n.y,color:"#cfd8ff",crit:!1});return}let i=s.damage*(.8+t.rng()*.4);const l=t.rng()<s.crit;if(l&&(i*=s.critMult),s.tear>0&&(n.armor=Math.max(0,n.armor-s.tear)),s.slow>0&&(n.slowF=Math.max(n.slowF,s.slow),n.slowT=a.kind==="trap"?5:2.5),s.poison>0&&(n.poisonDps=Math.max(n.poisonDps,s.poison),n.poisonT=a.kind==="trap"?6:4),s.leech>0&&kt(t,s.leech),ee(t,n,i,a),s.splash>0)for(const r of t.monsters){if(r===n||r.hp<=0)continue;Math.hypot(r.x-n.x,r.y-n.y)<1.15&&ee(t,r,i*s.splash,a)}t.events.push({type:"hit",x:n.x,y:n.y,color:wt(e),crit:l})}function rn(t,a){if(t.state!=="playing")return;t.time+=a,t.nextWave<t.waves.length&&(t.waveTimer-=a,t.waveTimer<=0&&we(t));for(const e of t.spawnQueue)for(e.timer-=a;e.left>0&&e.timer<=0;)ln(t,e.def),e.left--,e.timer+=e.def.interval;t.spawnQueue=t.spawnQueue.filter(e=>e.left>0),kt(t,a*(1+t.fieldIndex*.4));for(const e of t.monsters)if(!(e.hp<=0)){if(e.poisonT>0){if(e.poisonT-=a,e.hp-=e.poisonDps*a,e.poisonT<=0&&(e.poisonDps=0),e.hp<=0){kt(t,e.mana),t.events.push({type:"kill",x:e.x,y:e.y,mana:e.mana*t.manaMult});continue}}else e.regen>0&&(e.hp=Math.min(e.maxHp,e.hp+e.regen*a));if(e.slowT>0&&(e.slowT-=a,e.slowT<=0&&(e.slowF=0)),e.dist+=e.speed*(1-e.slowF)*a,e.dist>=t.pathLength){const s=Math.round(e.maxHp*.6+10+t.banished*2);if(t.mana-=s,t.banished++,t.events.push({type:"banish",x:e.x,y:e.y,cost:s,monster:e.type}),t.mana<0){t.mana=0,t.state="lost";return}e.dist=0}[e.x,e.y]=de(t,e.dist)}t.monsters=t.monsters.filter(e=>e.hp>0);for(const e of t.structures){if(!e.gem||(e.cooldown-=a,e.cooldown>0))continue;const s=At(e.gem,e.kind),i=e.x+.5,l=e.y+.5;let r=null;for(const f of t.monsters)f.hp<=0||Math.hypot(f.x-i,f.y-l)>s.range||(!r||f.dist>r.dist)&&(r=f);if(r)if(e.cooldown=1/s.rate,e.aim=Math.atan2(r.y-l,r.x-i),e.kind==="trap")for(const f of t.monsters)f.hp>0&&Math.hypot(f.x-i,f.y-l)<=s.range&&ne(t,e,f);else t.shots.push({from:e,target:r,x:i,y:l,color:wt(e.gem)})}const n=14;t.shots=t.shots.filter(e=>{if(e.target.hp<=0||!t.monsters.includes(e.target))return!1;const s=e.target.x-e.x,i=e.target.y-e.y,l=Math.hypot(s,i);return l<n*a?(ne(t,e.from,e.target),!1):(e.x+=s/l*n*a,e.y+=i/l*n*a,!0)}),t.monsters=t.monsters.filter(e=>e.hp>0),t.nextWave>=t.waves.length&&t.spawnQueue.length===0&&t.monsters.length===0&&(t.state="won")}function Me(t){if(t.state!=="won")return 0;let a=1;return t.banished===0&&a++,t.built<=5&&a++,a}const m={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"},dt='ui-rounded, "Trebuchet MS", system-ui, sans-serif',Y=document.querySelector("#game");if(!Y)throw new Error("Missing #game canvas");const o=Y.getContext("2d");let A=0,C=0,J=1;function ge(){J=Math.min(2,window.devicePixelRatio||1),A=Math.max(1,window.innerWidth),C=Math.max(1,window.innerHeight),Y.width=Math.round(A*J),Y.height=Math.round(C*J),Y.style.width=`${A}px`,Y.style.height=`${C}px`,Ct=""}window.addEventListener("resize",ge);let q={stars:lt.map(()=>0),sound:!0};const be="gem-warden-progress";async function cn(){if(fe(Vt))try{const t=await Se(ce.getItem(be),3e3);if(!t)return;const a=JSON.parse(t);Array.isArray(a.stars)&&(q.stars=lt.map((n,e)=>{var s;return Math.max(0,Math.min(3,Number((s=a.stars)==null?void 0:s[e])||0))})),typeof a.sound=="boolean"&&(q.sound=a.sound)}catch{}}function ve(){fe(Vt)&&Se(ce.setItem(be,JSON.stringify(q)),3e3).catch(()=>{})}function Se(t,a){return Promise.race([t,new Promise((n,e)=>setTimeout(()=>e(new Error("timeout")),a))])}function ke(t){return t===0||q.stars[t-1]>0}let at=null,ae=0;function $e(){if(!(at||!q.sound))try{at=new AudioContext}catch{at=null}}function F(t,a,n,e,s=0){if(!at||!q.sound)return;const i=at.currentTime,l=at.createOscillator(),r=at.createGain();l.type=n,l.frequency.setValueAtTime(t,i),s&&l.frequency.exponentialRampToValueAtTime(Math.max(30,t*s),i+a),r.gain.setValueAtTime(e,i),r.gain.exponentialRampToValueAtTime(1e-4,i+a),l.connect(r).connect(at.destination),l.start(i),l.stop(i+a+.02)}let Q="menu",g=null,U=1,K=!1,D=null,W=1,S=null,ct={text:"",t:0},_t=!1,_=[];function Rt(t){g=Ue(t),Q="play",H=[],U=1,K=!1,D=null,S=null,W=1,_=[],_t=!1,Ct=""}function Mt(t){ct={text:t,t:2.2}}function Jt(){const t=A<C*1.15;let a,n;if(t){const l=A/ot*st,r=Math.round(Math.min(Math.max(C-l,210),380));a={x:0,y:C-r,w:A,h:r},n={x:0,y:0,w:A,h:C-r}}else{const l=Math.round(Math.min(Math.max(A*.27,200),300));a={x:A-l,y:0,w:l,h:C},n={x:0,y:0,w:A-l,h:C}}const e=Math.max(4,Math.min(n.w/ot,n.h/st)),s=e*ot,i=e*st;return{field:{x:n.x+(n.w-s)/2,y:n.y+(n.h-i)/2,w:s,h:i},tile:e,panel:a,portrait:t}}let H=[];function it(t,a,n){return a>=t.x&&n>=t.y&&a<=t.x+t.w&&n<=t.y+t.h}function N(t,a){const n=Math.max(0,Math.min(a,t.w/2,t.h/2));o.beginPath(),o.moveTo(t.x+n,t.y),o.arcTo(t.x+t.w,t.y,t.x+t.w,t.y+t.h,n),o.arcTo(t.x+t.w,t.y+t.h,t.x,t.y+t.h,n),o.arcTo(t.x,t.y+t.h,t.x,t.y,n),o.arcTo(t.x,t.y,t.x+t.w,t.y,n),o.closePath()}function T(t,a,n,e,s,i="left",l=600){o.font=`${l} ${e}px ${dt}`,o.fillStyle=s,o.textAlign=i,o.textBaseline="middle",o.fillText(t,a,n)}function O(t,a,n,e=600){o.font=`${e} ${n}px ${dt}`;const s=o.measureText(t).width;return s>a?Math.max(7,n*(a/s)):n}function z(t,a,n,e={}){const s=h.inside&&it(t,h.x,h.y);N(t,Math.min(10,t.h*.3)),o.fillStyle=e.active?"#2c2650":s&&!e.disabled?"#1d2138":"#161a2c",o.fill(),o.lineWidth=e.active?2:1,o.strokeStyle=e.active?m.gold:e.accent??m.panelEdge,o.stroke();const i=e.disabled?m.textDim:m.text;if(e.sub){const l=O(n,t.w-8,Math.min(15,t.h*.36));T(n,t.x+t.w/2,t.y+t.h*.37,l,i,"center",700);const r=O(e.sub,t.w-8,Math.min(12,t.h*.28),500);T(e.sub,t.x+t.w/2,t.y+t.h*.72,r,e.disabled?m.danger:m.goldDim,"center",500)}else{const l=O(n,t.w-8,Math.min(15,t.h*.42));T(n,t.x+t.w/2,t.y+t.h/2,l,i,"center",700)}H.push({...t,id:a,tap:e.tap})}function oe(t,a,n,e){if(o.beginPath(),t>=5){o.arc(a,n,e*.92,0,Math.PI*2);return}const s=t+2,i=s===4?-Math.PI/4:-Math.PI/2,l=s===3?e*1.12:e,r=s===3?e*.12:0,f=[];for(let w=0;w<s;w++){const y=w/s*Math.PI*2+i;f.push([a+Math.cos(y)*l,n+r+Math.sin(y)*l])}const p=l*(s===3?.3:.18),u=w=>{const y=f[w%s],P=f[(w+1)%s];return[(y[0]+P[0])/2,(y[1]+P[1])/2]},d=u(s-1);o.moveTo(d[0],d[1]);for(let w=0;w<s;w++){const y=u(w);o.arcTo(f[w][0],f[w][1],y[0],y[1],p)}o.closePath()}function pt(t,a,n,e,s=!1,i=!0){const l=wt(t),r=Z(t);o.save(),s&&(o.shadowColor=l,o.shadowBlur=e*.9),oe(r,a,n,e);const f=o.createRadialGradient(a-e*.3,n-e*.35,e*.1,a,n,e);if(f.addColorStop(0,"#ffffff"),f.addColorStop(.25,l),f.addColorStop(1,It(l,-.55)),o.fillStyle=f,o.fill(),o.shadowBlur=0,o.lineWidth=Math.max(1,e*.08),o.strokeStyle=It(l,.35),o.stroke(),oe(r,a,n,e*.5),o.strokeStyle="rgba(255,255,255,0.35)",o.lineWidth=Math.max(.6,e*.05),o.stroke(),o.restore(),i&&e>=7){const p=Math.max(8,e*.75);o.font=`800 ${p}px ${dt}`,o.textAlign="center",o.textBaseline="middle",o.lineWidth=Math.max(2,p*.22),o.strokeStyle="rgba(8,8,16,0.85)",o.strokeText(String(r),a+e*.62,n+e*.58),o.fillStyle="#fff",o.fillText(String(r),a+e*.62,n+e*.58)}}function It(t,a){const n=parseInt(t.slice(1),16);let e=n>>16&255,s=n>>8&255,i=n&255;return a<0?(e*=1+a,s*=1+a,i*=1+a):(e+=(255-e)*a,s+=(255-s)*a,i+=(255-i)*a),`rgb(${Math.round(e)},${Math.round(s)},${Math.round(i)})`}let Ct="",ft=null;function fn(t,a){const n=`${A}x${C}:${J}:${a.fieldIndex}`;if(n===Ct&&ft)return;Ct=n,ft=document.createElement("canvas"),ft.width=Math.round(t.field.w*J),ft.height=Math.round(t.field.h*J);const e=ft.getContext("2d");e.scale(J,J);const s=t.tile,i=Xt(99+a.fieldIndex);e.fillStyle=m.earth,e.fillRect(0,0,t.field.w,t.field.h);for(let p=0;p<ot;p++)for(let u=0;u<st;u++){e.fillStyle=(p+u)%2?m.earth:m.earth2,e.globalAlpha=.55,e.fillRect(p*s,u*s,s,s),e.globalAlpha=1;for(let d=0;d<3;d++)e.fillStyle=`rgba(120,170,140,${.05+i()*.07})`,e.fillRect(p*s+i()*s,u*s+i()*s,Math.max(1,s*.06),Math.max(1,s*.14))}for(const p of a.pathTiles){const[u,d]=p.split(",").map(Number);e.fillStyle=m.pathEdge,e.fillRect(u*s-s*.06,d*s-s*.06,s*1.12,s*1.12)}for(const p of a.pathTiles){const[u,d]=p.split(",").map(Number);e.fillStyle=m.path,e.fillRect(u*s+.5,d*s+.5,s-1,s-1);for(let w=0;w<3;w++){e.fillStyle=`rgba(255,255,255,${.03+i()*.04})`;const y=s*(.15+i()*.2);e.beginPath(),e.ellipse(u*s+i()*s,d*s+i()*s,y,y*.6,i()*3,0,Math.PI*2),e.fill()}}for(const[p,u]of a.field.rocks){const d=(p+.5)*s,w=(u+.5)*s;e.fillStyle=m.rock,e.beginPath(),e.ellipse(d,w+s*.08,s*.42,s*.34,0,0,Math.PI*2),e.fill(),e.fillStyle="#3b4160",e.beginPath(),e.ellipse(d-s*.08,w-s*.04,s*.26,s*.2,-.4,0,Math.PI*2),e.fill()}const[l,r]=a.pathPoints[0],f=e.createRadialGradient((l+.5)*s,(r+.5)*s,1,(l+.5)*s,(r+.5)*s,s*.7);f.addColorStop(0,"#000"),f.addColorStop(.6,"#3a1030"),f.addColorStop(1,"rgba(120,20,80,0)"),e.fillStyle=f,e.beginPath(),e.arc((l+.5)*s,(r+.5)*s,s*.7,0,Math.PI*2),e.fill()}const h={shift:!1,x:0,y:0,inside:!1,down:!1,startX:0,startY:0,dragging:!1,dragRef:null,downWidget:null};Y.addEventListener("pointermove",t=>{h.x=t.offsetX,h.y=t.offsetY,h.inside=!0,h.down&&h.dragRef&&!h.dragging&&Math.hypot(h.x-h.startX,h.y-h.startY)>8&&(h.dragging=!0)});Y.addEventListener("pointerleave",()=>{h.inside=!1});Y.addEventListener("pointerdown",t=>{$e(),Y.setPointerCapture(t.pointerId),h.x=t.offsetX,h.y=t.offsetY,h.inside=!0,h.down=!0,h.startX=h.x,h.startY=h.y,h.dragging=!1,h.downWidget=Dt(h.x,h.y),h.dragRef=Ut(h.downWidget)??hn(h.x,h.y)});Y.addEventListener("pointerup",t=>{h.shift=t.shiftKey,h.x=t.offsetX,h.y=t.offsetY;const a=h.dragging,n=h.dragRef;if(h.down=!1,h.dragging=!1,h.dragRef=null,a&&n){un(n,h.x,h.y);return}dn(h.x,h.y)});Y.addEventListener("pointercancel",()=>{h.down=!1,h.dragging=!1,h.dragRef=null});Y.addEventListener("contextmenu",t=>t.preventDefault());function Ut(t){return!t||!g||typeof t.drop!="object"||t.drop.kind!=="inv"?t==null?void 0:t.gemRef:g.inventory[t.drop.index]?t.drop:void 0}function Dt(t,a){for(let n=H.length-1;n>=0;n--)if(it(H[n],t,a))return H[n];return null}function Zt(t,a){const n=Jt();return it(n.field,t,a)?[Math.floor((t-n.field.x)/n.tile),Math.floor((a-n.field.y)/n.tile)]:null}function Et(t,a){if(!g)return null;const n=Zt(t,a);return n?g.structures.find(e=>e.x===n[0]&&e.y===n[1])??null:null}function hn(t,a){if(Q!=="play")return null;const n=Et(t,a);return n!=null&&n.gem?{kind:"struct",id:n.id}:null}function Pe(t,a){const n=Dt(t,a);if(n!=null&&n.drop)return n.drop;const e=Et(t,a);return e?{kind:"struct",id:e.id}:null}function xt(t,a){if(!g)return!1;if(a==="sell"){const i=X(g,t);return i&&on(g,t)?(Mt(`Sold for ${jt(i)} mana`),F(520,.15,"triangle",.08,.6),!0):!1}const n=X(g,t),e=X(g,a);if(!an(g,t,a))return!1;const s=X(g,a);if(n&&e&&s){const i=Z(s),l=Math.round(Nt(s)*100);Mt(l>0?`Combined: G${i} ${St(s)}, ${l}% to G${i+1}`:`Combined into G${i} ${St(s)}`)}return!0}function un(t,a,n){const e=Pe(a,n);e&&xt(t,e),S=null}function dn(t,a){var i,l;const n=Dt(t,a);if(n&&((i=h.downWidget)==null?void 0:i.id)===n.id){const r=Ut(n);if(r){S&&!vt(S,r)?(xt(S,r),S=null):S=S&&vt(S,r)?null:r;return}if(n.drop&&S){xt(S,n.drop),S=null;return}(l=n.tap)==null||l.call(n);return}if(Q!=="play"||!g||g.state!=="playing")return;const e=Zt(t,a);if(!e){S=null,D=null;return}const s=Et(t,a);if(D&&!s){Qt(g,D,e[0],e[1])?en(g,D,e[0],e[1])&&!h.shift&&(D=null):Mt(D==="trap"?"Traps go on the path":"Towers go beside the path");return}if(s){const r={kind:"struct",id:s.id};S&&!vt(S,r)?(xt(S,r),S=null):s.gem&&(S=S?null:r);return}S=null}window.addEventListener("keydown",t=>{if(Q==="menu"){const n=Number(t.key);n>=1&&n<=lt.length&&ke(n-1)&&Rt(n-1);return}if(!g)return;const a=t.key.toLowerCase();if(g.state!=="playing"){(a==="enter"||a===" ")&&Ae(),a==="escape"&&(Q="menu");return}if(a==="t")D=D==="tower"?null:"tower";else if(a==="r")D=D==="trap"?null:"trap";else if(a==="p")Ie();else if(a==="n")Ce();else if(a===" ")K=!K,t.preventDefault();else if(a==="f")U=U===3?1:U+1;else if(a==="+"||a==="=")W=Math.min(10,W+1);else if(a==="-")W=Math.max(1,W-1);else if((a==="s"||a==="delete"||a==="backspace")&&S)xt(S,"sell"),S=null;else if(a==="escape")D=null,S=null;else if(/^[1-6]$/.test(a)){const n=g.field.colors[Number(a)-1];n&&Re(n)}else a==="0"&&Te()});function qt(t){return{id:0,mass:Math.pow(2,W-1),comp:{[t]:1},kills:0}}function Te(){g&&tn(g,W).length&&(F(620+W*40,.1,"sine",.06,1.5),setTimeout(()=>F(780+W*40,.12,"sine",.05,1.5),80),S=null)}function Re(t){if(!g)return;Le(g,t,W)>=0&&(F(660+W*40,.12,"sine",.06,1.5),S=null)}function Ie(){g&&nn(g)&&(Mt(`Mana pool expanded · gains ×${g.manaMult.toFixed(2)}`),F(300,.4,"sine",.08,2.5))}function Ce(){if(!g)return;const t=sn(g);t>0&&Mt(`Wave called early · +${Math.round(t*g.manaMult)} mana`)}function Ae(){g&&(g.state==="won"&&g.fieldIndex+1<lt.length?Rt(g.fieldIndex+1):Rt(g.fieldIndex))}let se=performance.now();function We(t){const a=Math.min(.1,(t-se)/1e3);if(se=t,Q==="play"&&g&&!K){let n=a*U;for(;n>0;){const e=Math.min(.016666666666666666,n);rn(g,e),n-=e}}if(Q==="play"&&g&&mn(g),g&&g.state!=="playing"&&!_t)if(_t=!0,g.state==="won"){const n=Me(g);q.stars[g.fieldIndex]=Math.max(q.stars[g.fieldIndex],n),ve(),F(523,.25,"triangle",.1),setTimeout(()=>F(784,.4,"triangle",.1),180)}else F(200,.8,"sawtooth",.08,.4);ct.t-=a,pn(Q==="play"&&!K?a*U:0),xn(t/1e3),requestAnimationFrame(We)}function mn(t){const n=Jt().tile;for(const e of t.events)switch(e.type){case"hit":for(let s=0;s<(e.crit?7:3);s++){const i=Math.random()*Math.PI*2,l=(e.crit?3:1.6)*(.5+Math.random());_.push({x:e.x,y:e.y,vx:Math.cos(i)*l,vy:Math.sin(i)*l,life:.35,max:.35,color:e.color,size:n*(e.crit?.12:.08)})}break;case"kill":_.push({x:e.x,y:e.y-.2,vx:0,vy:-.9,life:.9,max:.9,color:m.mana,size:n*.32,text:`+${Math.round(e.mana)}`}),performance.now()-ae>70&&(ae=performance.now(),F(880+Math.random()*200,.07,"square",.025,.5));break;case"banish":_.push({x:e.x,y:e.y-.3,vx:0,vy:-.6,life:1.3,max:1.3,color:m.danger,size:n*.4,text:`−${e.cost}`});for(let s=0;s<14;s++){const i=s/14*Math.PI*2;_.push({x:e.x,y:e.y,vx:Math.cos(i)*3,vy:Math.sin(i)*3,life:.6,max:.6,color:m.danger,size:n*.1})}F(160,.35,"sawtooth",.07,.5);break;case"build":F(220,.18,"triangle",.08,1.6);for(let s=0;s<10;s++){const i=Math.random()*Math.PI*2;_.push({x:e.x,y:e.y,vx:Math.cos(i)*1.4,vy:Math.sin(i)*1.4,life:.5,max:.5,color:m.stoneHi,size:n*.07})}break;case"combine":F(500,.12,"sine",.07,2),setTimeout(()=>F(750+e.grade*30,.18,"sine",.06,1.3),90);break;case"wave":F(110,.5,"triangle",.09,1.4);break;case"error":Mt(e.message),F(140,.12,"square",.04);break}t.events.length=0}function pn(t){for(const a of _)a.life-=t||.016,a.x+=a.vx*t,a.y+=a.vy*t,a.text||(a.vx*=.92,a.vy*=.92);_=_.filter(a=>a.life>0),_.length>600&&_.splice(0,_.length-600)}function xn(t){if(o.setTransform(J,0,0,J,0,0),o.fillStyle=m.night,o.fillRect(0,0,A,C),H=[],A<280||C<200){Ee("Gem Warden needs a bit more room. Make the frame larger or go full screen.",A/2,C/2-12,A-16,12,m.gold);return}Q==="menu"||!g?yn(t):Mn(g,t)}function yn(t){const a=A/2;for(let y=0;y<14;y++){const P=L[y%6],b=(y*137+t*12*(1+y%3))%(A+80)-40,k=C*(y*61%100)/100;o.globalAlpha=.18,pt({mass:1,comp:{[P]:1}},b,k,10+y%4*4),o.globalAlpha=1}const n=Math.min(54,A*.09,C*.1);o.save(),o.shadowColor=m.gold,o.shadowBlur=18,T("Gem Warden",a,C*.12,n,m.gold,"center",800),o.restore(),T("Forge gems, guard the orb.",a,C*.12+n*.85,Math.max(11,n*.32),m.textDim,"center",500);const e=A>C*1.2?3:2,s=Math.ceil(lt.length/e),i=C*.12+n*1.4,l=Math.max(64,C*.14),r=Math.max(8,Math.min(A,C)*.02),f=Math.min(260,(A-r*(e+1))/e),p=Math.min(120,(C-i-l-r*(s+1))/s),u=a-(f*e+r*(e-1))/2;lt.forEach((y,P)=>{const b={x:u+P%e*(f+r),y:i+r+Math.floor(P/e)*(p+r),w:f,h:p},k=ke(P),V=h.inside&&it(b,h.x,h.y)&&k;N(b,12),o.fillStyle=k?V?"#1f2440":"#151a2e":"#0f1220",o.fill(),o.strokeStyle=k?V?m.gold:m.panelEdge:"#1a1d2c",o.lineWidth=V?2:1,o.stroke(),wn(y,{x:b.x+8,y:b.y+8,w:b.h-16,h:b.h-16},k);const c=b.x+b.h,R=b.w-b.h-8;T(`${P+1}. ${y.name}`,c,b.y+b.h*.26,O(`${P+1}. ${y.name}`,R,Math.min(16,p*.17),700),k?m.text:m.textDim,"left",700),T(k?`${y.waves} waves`:"Locked",c,b.y+b.h*.5,Math.min(12,p*.13),m.textDim,"left",500);const $=q.stars[P];for(let x=0;x<3;x++)Ge(c+9+x*Math.min(22,p*.2),b.y+b.h*.76,Math.min(8,p*.075),x<$);let v=c+9+3*Math.min(22,p*.2);for(const x of y.colors)o.fillStyle=De(x),o.beginPath(),o.arc(v,b.y+b.h*.76,Math.min(4,p*.04),0,Math.PI*2),o.fill(),v+=Math.min(10,p*.09);k&&H.push({...b,id:`field-${P}`,tap:()=>Rt(P)})}),Ee("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.",a,C-l+6,Math.min(A-24,640),Math.max(10,Math.min(13,A/50)),m.textDim);const w={x:A-108,y:C-36,w:100,h:28};z(w,"sound",q.sound?"Sound on":"Sound off",{tap:()=>{q.sound=!q.sound,q.sound&&$e(),ve()}})}function De(t){return ut[t].hex}function Ee(t,a,n,e,s,i){o.font=`500 ${s}px ${dt}`;const l=t.split(" ");let r="",f=n;for(const p of l){const u=r?`${r} ${p}`:p;o.measureText(u).width>e&&r?(T(r,a,f,s,i,"center",500),r=p,f+=s*1.35):r=u}r&&T(r,a,f,s,i,"center",500)}function Ge(t,a,n,e){o.beginPath();for(let s=0;s<10;s++){const i=s/10*Math.PI*2-Math.PI/2,l=s%2?n*.45:n;o.lineTo(t+Math.cos(i)*l,a+Math.sin(i)*l)}o.closePath(),o.fillStyle=e?m.gold:"#2a2d42",o.fill()}function wn(t,a,n){const e=Math.min(a.w/ot,a.h/st),s=a.x+(a.w-e*ot)/2,i=a.y+(a.h-e*st)/2;o.fillStyle=n?m.earth2:"#11141f",o.fillRect(s,i,e*ot,e*st),o.strokeStyle=n?m.stoneHi:"#2a2d42",o.lineWidth=Math.max(1.5,e*.8),o.lineJoin="round",o.beginPath(),t.path.forEach(([f,p],u)=>{const d=s+(f+.5)*e,w=i+(p+.5)*e;u===0?o.moveTo(d,w):o.lineTo(d,w)}),o.stroke();const[l,r]=t.path[t.path.length-1];o.fillStyle=n?m.mana:"#2a2d42",o.beginPath(),o.arc(s+(l+.5)*e,i+(r+.5)*e,e*1.1,0,Math.PI*2),o.fill()}function Mn(t,a){var V;const n=Jt();fn(n,t);const e=n.tile,s=n.field.x,i=n.field.y;ft&&o.drawImage(ft,s,i,n.field.w,n.field.h);const l=c=>s+c*e,r=c=>i+c*e,f=h.inside?Zt(h.x,h.y):null;if(D&&f&&t.state==="playing"){const c=Qt(t,D,f[0],f[1]);o.fillStyle=c?"rgba(124,240,160,0.22)":"rgba(255,90,110,0.22)",o.fillRect(l(f[0]),r(f[1]),e,e),c&&D==="tower"&&(o.strokeStyle="rgba(124,240,160,0.35)",o.setLineDash([4,4]),o.beginPath(),o.arc(l(f[0]+.5),r(f[1]+.5),e*2.9,0,Math.PI*2),o.stroke(),o.setLineDash([]))}const[p,u]=t.orb,d=t.mana/t.manaCap,w=1+Math.sin(a*2.2)*.05;o.save(),o.shadowColor=m.mana,o.shadowBlur=e*.9;const y=o.createRadialGradient(l(p+.4),r(u+.35),e*.05,l(p+.5),r(u+.5),e*.62*w);y.addColorStop(0,"#eaf8ff"),y.addColorStop(.35,m.mana),y.addColorStop(1,m.manaDeep),o.fillStyle=y,o.beginPath(),o.arc(l(p+.5),r(u+.5),e*.6*w,0,Math.PI*2),o.fill(),o.restore(),o.strokeStyle=m.gold,o.lineWidth=Math.max(1.5,e*.07),o.beginPath(),o.arc(l(p+.5),r(u+.5),e*.78,-Math.PI/2,-Math.PI/2+Math.PI*2*d),o.stroke();const P=S&&S.kind==="struct"?S.id:-1;for(const c of t.structures){const R=l(c.x),$=r(c.y);if(c.kind==="tower"){o.fillStyle="#00000055",o.fillRect(R+e*.12,$+e*.16,e*.84,e*.84),N({x:R+e*.08,y:$+e*.08,w:e*.84,h:e*.84},e*.14);const x=o.createLinearGradient(R,$,R,$+e);x.addColorStop(0,m.stoneHi),x.addColorStop(1,m.stone),o.fillStyle=x,o.fill(),o.fillStyle="#1b1828",o.beginPath(),o.arc(R+e/2,$+e/2,e*.3,0,Math.PI*2),o.fill()}else{o.strokeStyle=c.gem?wt(c.gem):m.stoneHi,o.globalAlpha=.75,o.lineWidth=Math.max(1,e*.06),o.beginPath(),o.arc(R+e/2,$+e/2,e*.4,0,Math.PI*2),o.stroke(),o.beginPath();for(let x=0;x<5;x++){const I=x/5*Math.PI*2*2-Math.PI/2+a*.3;o.lineTo(R+e/2+Math.cos(I)*e*.36,$+e/2+Math.sin(I)*e*.36)}o.closePath(),o.stroke(),o.globalAlpha=1}const v=h.dragging&&((V=h.dragRef)==null?void 0:V.kind)==="struct"&&h.dragRef.id===c.id;c.gem&&!v&&pt(c.gem,R+e/2,$+e/2,e*(c.kind==="trap"?.24:.27),!0),c.id===P&&(o.strokeStyle=m.gold,o.lineWidth=2,o.strokeRect(R+1,$+1,e-2,e-2))}const b=f?t.structures.find(c=>c.x===f[0]&&c.y===f[1]):void 0,k=t.structures.find(c=>c.id===P)??(b!=null&&b.gem?b:void 0);if(k!=null&&k.gem){const c=At(k.gem,k.kind);o.strokeStyle="rgba(242,198,109,0.55)",o.fillStyle="rgba(242,198,109,0.06)",o.lineWidth=1.5,o.beginPath(),o.arc(l(k.x+.5),r(k.y+.5),c.range*e,0,Math.PI*2),o.fill(),o.stroke()}for(const c of t.monsters){const R=Math.sin(a*9+c.wobble)*e*.04,$=l(c.x),v=r(c.y)+R,x=e*$t[c.type].size,I=$t[c.type].col;if(o.fillStyle="#00000066",o.beginPath(),o.ellipse($,v+x*.8,x*.9,x*.35,0,0,Math.PI*2),o.fill(),o.fillStyle=c.slowT>0?It(I,.15).replace("rgb","rgb"):I,c.type==="brute"?(N({x:$-x,y:v-x,w:x*2,h:x*2},x*.35),o.fill(),o.strokeStyle="#d6dbe8",o.lineWidth=Math.max(1,c.armor>0?e*.05:.5),o.stroke()):c.type==="runner"?(He($,v,x*1.2),o.fill()):(o.beginPath(),o.arc($,v,x,0,Math.PI*2),o.fill()),c.ward>0&&(o.strokeStyle="rgba(207,216,255,0.9)",o.lineWidth=Math.max(1,e*.025*Math.min(4,1+c.ward/3)),o.beginPath(),o.arc($,v,x+e*.09,0,Math.PI*2),o.stroke()),c.regen>0&&c.poisonT<=0&&Fe($,v-x*1.05,Math.max(2,x*.35),"#d8fff0"),c.slowT>0&&(o.strokeStyle="#7cc4ff",o.lineWidth=Math.max(1,e*.05),o.beginPath(),o.arc($,v,x+e*.05,0,Math.PI*2),o.stroke()),c.poisonT>0&&(o.fillStyle="rgba(79,224,106,0.8)",o.beginPath(),o.arc($+x*.5,v-x*.6,Math.max(1.5,e*.06),0,Math.PI*2),o.fill()),o.fillStyle="#ffe9a8",o.fillRect($-x*.4,v-x*.2,Math.max(1,x*.22),Math.max(1,x*.22)),o.fillRect($+x*.18,v-x*.2,Math.max(1,x*.22),Math.max(1,x*.22)),c.hp<c.maxHp){const j=Math.max(e*.6,x*2);o.fillStyle="#000a",o.fillRect($-j/2,v-x-e*.16,j,Math.max(2,e*.08)),o.fillStyle=c.hp/c.maxHp>.4?m.ok:m.danger,o.fillRect($-j/2,v-x-e*.16,j*Math.max(0,c.hp/c.maxHp),Math.max(2,e*.08))}}for(const c of t.shots)o.save(),o.shadowColor=c.color,o.shadowBlur=e*.4,o.fillStyle=c.color,o.beginPath(),o.arc(l(c.x),r(c.y),Math.max(2,e*.09),0,Math.PI*2),o.fill(),o.restore();for(const c of _){const R=Math.max(0,c.life/c.max);o.globalAlpha=R,c.text?(o.font=`800 ${c.size}px ${dt}`,o.textAlign="center",o.lineWidth=3,o.strokeStyle="#000",o.strokeText(c.text,l(c.x),r(c.y)),o.fillStyle=c.color,o.fillText(c.text,l(c.x),r(c.y))):(o.fillStyle=c.color,o.fillRect(l(c.x)-c.size/2,r(c.y)-c.size/2,c.size,c.size))}if(o.globalAlpha=1,gn(t,n),bn(t,n),Sn(t,n),h.dragging&&h.dragRef){const c=X(t,h.dragRef);c&&pt(c,h.x,h.y,Math.max(14,e*.4),!0)}if(ct.t>0&&ct.text){o.globalAlpha=Math.min(1,ct.t*2);const c=Math.max(12,Math.min(16,n.field.w/40));o.font=`700 ${c}px ${dt}`;const R=o.measureText(ct.text).width+24,$={x:n.field.x+n.field.w/2-R/2,y:n.field.y+n.field.h-c*3.2,w:R,h:c*2};N($,10),o.fillStyle="#0d1020ee",o.fill(),o.strokeStyle=m.goldDim,o.stroke(),T(ct.text,$.x+$.w/2,$.y+$.h/2,c,m.text,"center",700),o.globalAlpha=1}K&&t.state==="playing"&&(o.fillStyle="rgba(5,6,12,0.45)",o.fillRect(n.field.x,n.field.y,n.field.w,n.field.h),T("Paused",n.field.x+n.field.w/2,n.field.y+n.field.h/2,Math.min(40,n.field.w/10),m.gold,"center",800)),t.state!=="playing"&&Tn(t,n)}function gn(t,a){if(t.state!=="playing")return;let n="";const e=t.structures.filter(f=>f.kind==="tower"),s=t.inventory.some(f=>f);if(D?n=D==="tower"?"Tap a grass tile to build a tower":"Tap a path tile to build a trap":e.length===0?n="Tap Tower, then tap grass beside the path":!e.some(f=>f.gem)&&!s?n="Tap a gem colour to forge a gem":s&&e.some(f=>!f.gem)&&t.nextWave<3?n="Drag the gem onto a tower (or tap gem, then tower)":t.nextWave===0?n=`First wave in ${Math.ceil(t.waveTimer)}s · tap Next wave for bonus mana`:t.fieldIndex===0&&t.nextWave<4&&t.time<160&&(n="Drop a gem on an equal gem to combine into a higher grade"),!n)return;const i=Math.max(10,Math.min(14,a.field.w/46));o.font=`600 ${i}px ${dt}`;const l=o.measureText(n).width+20,r={x:a.field.x+a.field.w/2-l/2,y:a.field.y+6,w:l,h:i*1.9};N(r,8),o.fillStyle="#0d1020cc",o.fill(),T(n,r.x+r.w/2,r.y+r.h/2,i,m.gold,"center",600)}function bn(t,a){var te;const n=a.panel;o.fillStyle=m.panel,o.fillRect(n.x,n.y,n.w,n.h),o.strokeStyle=m.panelEdge,o.beginPath(),a.portrait?(o.moveTo(n.x,n.y+.5),o.lineTo(n.x+n.w,n.y+.5)):(o.moveTo(n.x+.5,n.y),o.lineTo(n.x+.5,n.y+n.h)),o.stroke();const e=8,s=6;let i,l;if(a.portrait){const M=Math.round(n.w*.56);i={x:n.x+e,y:n.y+e,w:M-e*1.5,h:n.h-e*2},l={x:n.x+M+e*.5,y:n.y+e,w:n.w-M-e*1.5,h:n.h-e*2}}else i={x:n.x+e,y:n.y+e,w:n.w-e*2,h:n.h-e*2},l=i;const r=3,f=4,p=a.portrait?5.4:6.25+r*1.2,u=Math.max(18,Math.min(a.portrait?56:46,(i.h-s*(a.portrait?5:8))/p));let d=i.y;const w={x:i.x,y:d,w:i.w,h:u*.8};N(w,6),o.fillStyle="#0a0d18",o.fill();const y=Math.max(0,Math.min(1,t.mana/t.manaCap));N({x:w.x,y:w.y,w:Math.max(6,w.w*y),h:w.h},6);const P=o.createLinearGradient(w.x,0,w.x+w.w,0);P.addColorStop(0,m.manaDeep),P.addColorStop(1,m.mana),o.fillStyle=P,o.fill(),o.strokeStyle=m.panelEdge,N(w,6),o.stroke();const b=`${Math.floor(t.mana)} / ${t.manaCap} mana`;T(b,w.x+w.w/2,w.y+w.h/2,O(b,w.w-10,Math.min(14,w.h*.55),800),"#fff","center",800),d+=w.h+s;const k={x:i.x,y:d,w:i.w,h:u*.8},V=t.nextWave>=t.waves.length?`All ${t.waves.length} waves out`:`Wave ${t.nextWave}/${t.waves.length} · next ${Math.ceil(t.waveTimer)}s`;T(V,k.x,k.y+k.h/2,O(V,k.w*.62,Math.min(13,k.h*.5)),m.text,"left",600),Yt=[];for(let M=0;M<3;M++){const G=t.waves[t.nextWave+M];if(!G)break;const E=k.x+k.w-(2.5-M)*Math.min(k.h,k.w*.12),B=Math.min(k.h*.42,k.w*.05);vn(G,E,k.y+k.h/2,B,M===0),Yt.push({x:E,y:k.y+k.h/2,r:Math.max(B*1.3,10),def:G,index:t.nextWave+M})}d+=k.h+s;const c=(i.w-s*2)/3;z({x:i.x,y:d,w:c,h:u},"tower","Tower",{sub:`${Ot(t)}`,active:D==="tower",disabled:t.mana<Ot(t),tap:()=>D=D==="tower"?null:"tower"}),z({x:i.x+c+s,y:d,w:c,h:u},"trap","Trap",{sub:`${zt(t)}`,active:D==="trap",disabled:t.mana<zt(t),tap:()=>D=D==="trap"?null:"trap"}),z({x:i.x+(c+s)*2,y:d,w:c,h:u},"pool","Pool +",{sub:`${Bt(t)}`,disabled:t.mana<Bt(t),tap:Ie}),d+=u+s;const R=t.field.colors,$=R.length+1,v=Math.min(u*1.6,(i.w-s*($-1))/$),x=i.x+(i.w-(v*$+s*($-1)))/2,I=yt(W);R.forEach((M,G)=>{const E={x:x+G*(v+s),y:d,w:v,h:u},B=!0,rt=h.inside&&it(E,h.x,h.y);N(E,8),o.fillStyle=rt&&B?"#1d2138":"#141828",o.fill(),o.strokeStyle=It(De(M),-.3),o.stroke(),o.globalAlpha=t.mana>=I?1:.4,pt(qt(M),E.x+E.w/2,E.y+E.h/2,Math.min(E.w,E.h)*.32),o.globalAlpha=1,T(String(G+1),E.x+4,E.y+7,9,m.textDim,"left",600),H.push({...E,id:`gem-${M}`,forge:M,tap:()=>Re(M)})});{const M={x:x+R.length*(v+s),y:d,w:v,h:u},G=h.inside&&it(M,h.x,h.y);N(M,8),o.fillStyle=G?"#1d2138":"#141828",o.fill(),o.strokeStyle=m.stoneHi,o.stroke(),o.globalAlpha=t.mana>=Kt(W)?1:.4;const E=Math.min(M.w,M.h)*.2;for(let B=0;B<3;B++){const rt=-Math.PI/2+B*Math.PI*2/3,Gt=R[B%R.length];pt(qt(Gt),M.x+M.w/2+Math.cos(rt)*E,M.y+M.h/2+Math.sin(rt)*E*.9,E*.85,!1,!1)}o.globalAlpha=1,T("0",M.x+4,M.y+7,9,m.textDim,"left",600),H.push({...M,id:"gem-random",random:!0,tap:Te})}d+=u+s;const j=u*.8,Tt=Math.min(j*1.3,i.w*.18);z({x:i.x,y:d,w:Tt,h:j},"grade-","−",{tap:()=>W=Math.max(1,W-1)}),z({x:i.x+i.w-Tt,y:d,w:Tt,h:j},"grade+","+",{tap:()=>W=Math.min(10,W+1)});const Lt=`Forge grade ${W} · ${I} mana`;T(Lt,i.x+i.w/2,d+j/2,O(Lt,i.w-Tt*2-10,Math.min(13,j*.5)),t.mana>=I?m.text:m.danger,"center",600),d+=j+s;let tt;a.portrait?tt={x:l.x,y:l.y,w:l.w,h:l.h-u-s}:(tt={x:i.x,y:d,w:i.w,h:r*u*1.2+s*(r-1)},d+=tt.h+s);const mt=Math.min((tt.w-s*(f-1))/f,(tt.h-s*(r-1))/r),Ne=tt.x+(tt.w-(mt*f+s*(f-1)))/2;for(let M=0;M<he;M++){const G={x:Ne+M%f*(mt+s),y:tt.y+Math.floor(M/f)*(mt+s),w:mt,h:mt},E={kind:"inv",index:M},B=t.inventory[M],rt=(S==null?void 0:S.kind)==="inv"&&S.index===M;N(G,8),o.fillStyle="#0c0f1b",o.fill(),o.strokeStyle=rt?m.gold:h.dragging&&it(G,h.x,h.y)?m.ok:m.panelEdge,o.lineWidth=rt?2:1,o.stroke(),o.lineWidth=1;const Gt=h.dragging&&((te=h.dragRef)==null?void 0:te.kind)==="inv"&&h.dragRef.index===M;B&&!Gt&&pt(B,G.x+G.w/2,G.y+G.h/2,mt*.32),H.push({...G,id:`inv-${M}`,gemRef:B?E:void 0,drop:E})}const gt=a.portrait?l.y+l.h-u:d,et=a.portrait?l:i;if(a.portrait){const M=(et.w-s)/2;z({x:et.x,y:gt,w:M,h:u},"sell","Sell",{sub:ie(t),disabled:!S,accent:m.goldDim,tap:le}),H[H.length-1].drop="sell",re(t,{x:et.x+M+s,y:gt,w:M,h:u});const G=i.y+i.h-u;G>=d-2?Ht({x:i.x,y:G,w:i.w,h:u},s):Ht({x:i.x,y:d,w:i.w,h:Math.max(20,i.y+i.h-d)},s)}else{const M=(et.w-s)/2;z({x:et.x,y:gt,w:M,h:u},"sell","Sell",{sub:ie(t),disabled:!S,accent:m.goldDim,tap:le}),H[H.length-1].drop="sell",re(t,{x:et.x+M+s,y:gt,w:M,h:u}),Ht({x:et.x,y:gt+u+s,w:et.w,h:u*.85},s)}}function ie(t){const a=S?X(t,S):null;return a?`+${jt(a)}`:"drop gem"}function le(){S&&xt(S,"sell"),S=null}function re(t,a){const n=t.waves.length-t.nextWave,e=n>0?Math.round(t.waveTimer*(1.5+t.nextWave*.6+t.fieldIndex)*t.manaMult):0;z(a,"next","Next wave",{sub:n>0?`+${e}`:"none left",disabled:n===0,accent:m.manaDeep,tap:Ce})}function Ht(t,a){const n=(t.w-a*2)/3;z({x:t.x,y:t.y,w:n,h:t.h},"speed",`${U}× speed`,{tap:()=>U=U===3?1:U+1}),z({x:t.x+n+a,y:t.y,w:n,h:t.h},"pause",K?"Resume":"Pause",{active:K,tap:()=>K=!K}),z({x:t.x+(n+a)*2,y:t.y,w:n,h:t.h},"menu","Fields",{tap:()=>{Q="menu",H=[],K=!1}})}const $t={crawler:{col:"#c9604a",size:.26},swarm:{col:"#d16ad8",size:.16},brute:{col:"#8a93a8",size:.3},giant:{col:"#9b3b3b",size:.42},runner:{col:"#e0a63a",size:.2},warded:{col:"#6f86d6",size:.26},mender:{col:"#3fb6a8",size:.27}};function He(t,a,n){o.beginPath(),o.moveTo(t,a-n),o.lineTo(t+n,a),o.lineTo(t,a+n),o.lineTo(t-n,a),o.closePath()}function Fe(t,a,n,e){o.fillStyle=e;const s=Math.max(1,n*.4);o.fillRect(t-n,a-s/2,n*2,s),o.fillRect(t-s/2,a-n,s,n*2)}let Yt=[];function vn(t,a,n,e,s){if(o.globalAlpha=s?1:.55,o.fillStyle=$t[t.type].col,t.type==="brute")o.fillRect(a-e*.8,n-e*.8,e*1.6,e*1.6);else if(t.type==="runner")He(a,n,e*.85),o.fill();else if(t.type==="swarm")for(let i=0;i<3;i++)o.beginPath(),o.arc(a+(i-1)*e*.6,n+(i%2?-e*.3:e*.3),e*.4,0,Math.PI*2),o.fill();else o.beginPath(),o.arc(a,n,t.type==="giant"?e:e*.7,0,Math.PI*2),o.fill(),t.type==="warded"?(o.strokeStyle="#cfd8ff",o.lineWidth=Math.max(1,e*.18),o.beginPath(),o.arc(a,n,e*.95,0,Math.PI*2),o.stroke()):t.type==="mender"&&Fe(a,n,e*.4,"#d8fff0");o.globalAlpha=1}function Sn(t,a){let n=null,e="tower",s="",i="";const l=[],r=v=>{var x;return v.kind==="struct"?((x=t.structures.find(I=>I.id===v.id))==null?void 0:x.kind)??"tower":"tower"},f=h.dragging?h.dragRef:S,p=h.inside?Pe(h.x,h.y):null;if(f){n=X(t,f),e=r(f);const v=p&&p!=="sell"&&!vt(p,f)?X(t,p):null;if(n&&v){const x=Z(n),I=Z(v);e=r(p),n=ye(n,v,0),n.kills=0,s=`G${x} + G${I} → G${Z(n)} ${St(n)}`,x!==I&&l.push(["Unequal grades",`${Math.round(Nt(n)*100)}% to G${Z(n)+1}`])}}else if(h.inside&&!h.down){const v=Dt(h.x,h.y),x=Ut(v);if(x)n=X(t,x);else if(v!=null&&v.random){$n(t,a);return}else if(v!=null&&v.forge){n=qt(v.forge);const I=yt(W);s=`Forge G${W} ${St(n)}`,l.push(["Cost",`${I} mana`]),t.mana<I&&(i=m.danger)}else{const I=Et(h.x,h.y);I!=null&&I.gem&&(n=I.gem,e=I.kind)}}if(!n){h.inside&&!h.down&&!f&&kn(t,a);return}const u=At(n,e),d=[...l];d.push(["Damage",`${Math.round(u.damage*.8)}–${Math.round(u.damage*1.2)}`]),d.push(["Rate",`${u.rate.toFixed(2)}/s`]),d.push(["Range",u.range.toFixed(1)]),u.splash>.005&&d.push(["Splash",`${Math.round(u.splash*100)}%`]),u.leech>.05&&d.push(["Mana/hit",u.leech.toFixed(1)]),u.crit>.005&&d.push(["Crit",`${Math.round(u.crit*100)}% ×${u.critMult.toFixed(1)}`]),u.poison>.05&&d.push(["Poison",`${u.poison.toFixed(1)}/s`]),u.slow>.005&&d.push(["Slow",`${Math.round(u.slow*100)}%`]),u.tear>.01&&d.push(["Armor tear",u.tear.toFixed(1)]);const w=pe(n);w<.999&&d.push(["Purity",`${Math.round(w*100)}%`]);const y=Nt(n);y>.005&&!l.length&&d.push(["Next grade",`${Math.round(y*100)}%`]),s||d.push(["Kills",String(n.kills)]);const P=Math.max(10,Math.min(13,a.field.w/55)),b=P*1.45,k=P*15.5,V=b*(d.length+2.6),c={x:a.field.x+6,y:a.field.y+a.field.h-V-6,w:k,h:V};N(c,8),o.fillStyle="#0b0e1aee",o.fill(),o.strokeStyle=wt(n),o.stroke();const R=(s||`G${Z(n)} ${St(n)}`)+(e==="trap"?" (trap)":"");T(R,c.x+8,c.y+b*.8,O(R,k-14,P+1,800),i||wt(n),"left",800);const $=Pn(n);T($,c.x+8,c.y+b*1.85,O($,k-14,P,600),m.gold,"left",600),d.forEach(([v,x],I)=>{T(v,c.x+8,c.y+b*(I+2.9),P,m.textDim,"left",500),T(x,c.x+c.w-8,c.y+b*(I+2.9),P,m.text,"right",700)})}function kn(t,a){const n=Yt.find(y=>Math.hypot(h.x-y.x,h.y-y.y)<=y.r);let e=null;if(!n&&it(a.field,h.x,h.y)){let y=Math.max(10,a.tile*.5);for(const P of t.monsters){const b=Math.hypot(a.field.x+P.x*a.tile-h.x,a.field.y+P.y*a.tile-h.y);b<y&&(y=b,e=P)}}const s=(n==null?void 0:n.def.type)??(e==null?void 0:e.type);if(!s)return;const i=Ye[s],l=[];if(n){const y=n.def;l.push(["Wave",`${n.index+1} · ${y.count} monsters`]),l.push(["Health",String(Math.round(y.hp))]),y.armor>0&&l.push(["Armour",String(y.armor)]),y.ward&&l.push(["Ward",`${y.ward} hits`]),y.regen&&l.push(["Heals",`${Math.round(y.regen*100)}% health/s`])}else e&&(l.push(["Health",`${Math.ceil(e.hp)} / ${Math.round(e.maxHp)}`]),e.armor>.5&&l.push(["Armour",String(Math.round(e.armor))]),e.ward>0&&l.push(["Ward",`${e.ward} hits`]),e.regen>0&&l.push(["Heals",e.poisonT>0?"stopped by poison":`${Math.round(e.regen)}/s`]),e.slowT>0&&l.push(["Slowed",`${Math.round(e.slowF*100)}%`]));const r=Math.max(10,Math.min(13,a.field.w/55)),f=r*1.45,p=r*17,u=f*(l.length+3.6),d={x:a.field.x+6,y:a.field.y+a.field.h-u-6,w:p,h:u};N(d,8),o.fillStyle="#0b0e1aee",o.fill(),o.strokeStyle=$t[s].col,o.stroke(),T(i.name,d.x+8,d.y+f*.8,r+1,$t[s].col,"left",800),T(i.trait,d.x+8,d.y+f*1.85,O(i.trait,p-14,r,600),m.text,"left",600);const w=`Beat it with: ${i.counter}`;T(w,d.x+8,d.y+f*2.9,O(w,p-14,r,600),m.gold,"left",600),l.forEach(([y,P],b)=>{T(y,d.x+8,d.y+f*(b+3.9),r,m.textDim,"left",500),T(P,d.x+d.w-8,d.y+f*(b+3.9),r,m.text,"right",700)})}function $n(t,a){const n=Kt(W),e=t.field.colors.map(w=>ut[w].name).join(", "),s=[["Cost",`${n} mana`],["Forged singly",`${yt(W)*ht} mana`],["Free slots needed",String(ht)]],i=Math.max(10,Math.min(13,a.field.w/55)),l=i*1.45,r=i*15.5,f=l*(s.length+3.6),p={x:a.field.x+6,y:a.field.y+a.field.h-f-6,w:r,h:f};N(p,8),o.fillStyle="#0b0e1aee",o.fill(),o.strokeStyle=m.goldDim,o.stroke();const u=`${ht} random G${W} gems`;T(u,p.x+8,p.y+l*.8,O(u,r-14,i+1,800),t.mana>=n?m.gold:m.danger,"left",800);const d=`Saves ${yt(W)*ht-n} mana`;T(d,p.x+8,p.y+l*1.85,O(d,r-14,i,600),m.gold,"left",600),T(e,p.x+8,p.y+l*2.9,O(e,r-14,i,500),m.textDim,"left",500),s.forEach(([w,y],P)=>{T(w,p.x+8,p.y+l*(P+3.9),i,m.textDim,"left",500),T(y,p.x+p.w-8,p.y+l*(P+3.9),i,m.text,"right",700)})}function Pn(t){const a=L.filter(n=>(t.comp[n]??0)>=.15).sort((n,e)=>(t.comp[e]??0)-(t.comp[n]??0));return a.length<=1?ut[me(t)].special:a.map(n=>ut[n].short).join(" + ")}function St(t){const a=L.filter(n=>(t.comp[n]??0)>=.15).sort((n,e)=>(t.comp[e]??0)-(t.comp[n]??0));return a.length<=1?ut[me(t)].name:a.slice(0,2).map(n=>ut[n].name).join("-")}function Tn(t,a){o.fillStyle="rgba(5,6,12,0.72)",o.fillRect(0,0,A,C);const n=t.state==="won",e=a.field.x+a.field.w/2,s=a.field.y+a.field.h/2,i=Math.min(44,a.field.w/9);if(T(n?"Field defended!":"The orb shattered",e,s-i*1.6,i,n?m.gold:m.danger,"center",800),n){const p=Me(t);for(let d=0;d<3;d++)Ge(e+(d-1)*i*1.1,s-i*.3,i*.45,d<p);const u=`★ won ${t.banished===0?"★":"☆"} nothing reached the orb ${t.built<=5?"★":"☆"} five or fewer buildings`;T(u,e,s+i*.5,O(u,a.field.w-20,Math.max(10,i*.3),500),m.textDim,"center",500)}else{const p=`Reached wave ${t.nextWave} of ${t.waves.length}`;T(p,e,s-i*.3,Math.max(12,i*.4),m.textDim,"center",500)}const l=Math.min(150,a.field.w*.3),r=Math.max(34,i*.9),f=n?t.fieldIndex+1<lt.length?"Next field":"Play again":"Retry";z({x:e-l-6,y:s+i*1.1,w:l,h:r},"again",f,{active:!0,tap:Ae}),z({x:e+6,y:s+i*1.1,w:l,h:r},"fields","Fields",{tap:()=>{Q="menu",H=[]}})}ge();cn();requestAnimationFrame(We);
</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>
