Skip to content
VibeCodeBench
Back to the app

The code

Cat Island by GPT-6.2 Astra

Exactly as the AI wrote it. Shown as text, so nothing runs on this page.

index.html

works · auto-checked (Checked by an automated pass. Not hand-played yet.)85.8 KB · 1,323 lines · no libraries
Download
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<meta name="theme-color" content="#9ed3d8">
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath fill='%2335655b' d='M0 0h16v16H0z'/%3E%3Cpath fill='%23ffe8b6' d='M2 3h2v2h2v1h4V5h2V3h2v8h-2v2H4v-2H2z'/%3E%3Cpath fill='%23334846' d='M5 8h1v2H5zm5 0h1v2h-1zM7 10h2v1H7z'/%3E%3C/svg%3E">
<title>Whisker Isle · Floating Cat Town</title>
<style>
:root{--ink:#2b4445;--paper:#fff5db;--line:#c9bc98;--green:#35645b;--rim:polygon(0 6px,3px 6px,3px 3px,6px 3px,6px 0,calc(100% - 6px) 0,calc(100% - 6px) 3px,calc(100% - 3px) 3px,calc(100% - 3px) 6px,100% 6px,100% calc(100% - 6px),calc(100% - 3px) calc(100% - 6px),calc(100% - 3px) calc(100% - 3px),calc(100% - 6px) calc(100% - 3px),calc(100% - 6px) 100%,6px 100%,6px calc(100% - 3px),3px calc(100% - 3px),3px calc(100% - 6px),0 calc(100% - 6px));--small:polygon(0 3px,3px 3px,3px 0,calc(100% - 3px) 0,calc(100% - 3px) 3px,100% 3px,100% calc(100% - 3px),calc(100% - 3px) calc(100% - 3px),calc(100% - 3px) 100%,3px 100%,3px calc(100% - 3px),0 calc(100% - 3px));--edge:clamp(14px,2.1vw,32px);font-family:"DM Sans","Segoe UI",Arial,sans-serif;color:var(--ink);color-scheme:light}
*{box-sizing:border-box}
html,body,main{margin:0;width:100%;height:100%;overflow:hidden}
body{background:#a8d5d6;overscroll-behavior:none;-webkit-tap-highlight-color:transparent}
main{position:relative;isolation:isolate;background:linear-gradient(#82becf,#c3e1d8 66%,#f4e5c1);user-select:none;-webkit-user-select:none}
canvas.scene-layer{position:absolute;inset:0;width:100%;height:100%;display:block;image-rendering:pixelated}
#sky,#effects{pointer-events:none}
#world{touch-action:none;cursor:grab}
#world.dragging{cursor:grabbing}
#world.oncat{cursor:pointer}
.defs{position:absolute;width:0;height:0;overflow:hidden}
.hud{position:absolute;z-index:4;filter:drop-shadow(0 3px 0 #354749) drop-shadow(0 4px 0 #35474924)}
.frame{background:var(--ink);padding:2px;clip-path:var(--rim)}
.surface{background:var(--paper);clip-path:var(--small);box-shadow:inset 0 -3px #e5d7b4,inset 0 2px #fffbed}
button{border:0;background:transparent;color:var(--ink);font:inherit;cursor:pointer;touch-action:manipulation;display:inline-flex;align-items:center;justify-content:center}
button:focus-visible{outline:3px solid #d98434;outline-offset:-4px}
button:active{transform:translateY(1px)}
.i{width:20px;height:20px;flex:none;fill:currentColor;shape-rendering:crispEdges}
.brand{left:calc(var(--edge) + env(safe-area-inset-left,0px));top:calc(var(--edge) + env(safe-area-inset-top,0px))}
.brand .surface{display:flex;align-items:center;gap:13px;padding:10px 17px 11px 10px}
.crest{display:grid;place-items:center;width:46px;height:46px;background:#397065;box-shadow:inset 0 -4px #28564f;clip-path:var(--small)}
.crest canvas{width:42px;height:42px;image-rendering:pixelated}
h1{margin:0 0 6px;font-size:0;line-height:1}
#wordmark{width:187px;height:22px;display:block;color:#2c4445;overflow:visible}
.kicker,.eyebrow,.counter,.role,.tip,.followtext{font-family:"IBM Plex Mono","Courier New",monospace;font-weight:700}
.kicker{display:block;font-size:9px;letter-spacing:2.1px;color:#657264}
.daybar{right:calc(var(--edge) + env(safe-area-inset-right,0px));top:calc(var(--edge) + env(safe-area-inset-top,0px))}
.time-options{display:flex;padding:5px;gap:2px}
.time-options button{height:41px;padding:0 14px;gap:8px;font-size:13px;font-weight:750;letter-spacing:.1px;clip-path:var(--small)}
.time-options button .i{width:19px;height:19px}
.time-options button[data-phase="0"] .i{color:#b98025}
.time-options button[data-phase="1"] .i{color:#c27550}
.time-options button[data-phase="2"] .i{color:#667ba0}
.time-options button[aria-pressed="true"]{background:var(--green);color:#fff6dc;box-shadow:inset 0 -3px #295249,inset 0 1px #4e7a67}
.time-options button[aria-pressed="true"] .i{color:#f7ca70}
.time-options button[aria-pressed="true"][data-phase="2"] .i{color:#efdeaa}
.residents{left:calc(var(--edge) + env(safe-area-inset-left,0px));bottom:calc(var(--edge) + env(safe-area-inset-bottom,0px));width:270px}
.resident-head{display:flex;align-items:center;justify-content:space-between;border-bottom:1px solid #cfbf99;margin:0 10px;padding-left:3px;min-height:36px}
.eyebrow{display:flex;align-items:center;gap:7px;font-size:10px;letter-spacing:1.4px;color:#576755;white-space:nowrap}
.eyebrow .i{width:14px;height:14px;color:#69765e}
.pager{display:flex;align-items:center}
.pager button{height:37px;width:33px}
.pager .i{width:13px;height:13px}
.counter{font-size:10px;letter-spacing:.7px;color:#717568;white-space:nowrap}
.resident-body{width:100%;text-align:left;justify-content:flex-start;gap:12px;padding:12px 13px 14px}
.portrait{background:#dbe3ba;clip-path:var(--small);box-shadow:inset 0 0 0 2px #9fac8e;display:grid;place-items:center;width:48px;height:48px;flex:none}
.portrait canvas{width:44px;height:44px;image-rendering:pixelated}
.bio{min-width:0;flex:1}
.nameline{display:flex;align-items:center;gap:7px;min-height:23px}
#catName{font-family:"Bricolage Grotesque","Trebuchet MS","Segoe UI",sans-serif;font-weight:800;font-size:21px;letter-spacing:-.8px;line-height:1.1}
.role{font-size:9px;background:#eecd86;color:#6d4c2b;padding:3px 5px 2px;text-transform:uppercase;letter-spacing:1px;clip-path:polygon(0 2px,2px 2px,2px 0,100% 0,100% calc(100% - 2px),calc(100% - 2px) calc(100% - 2px),calc(100% - 2px) 100%,0 100%)}
.role[hidden]{display:none}
.activity{margin-top:5px;display:flex;align-items:center;gap:6px;font-size:12px;color:#536453;line-height:1.3}
.dot{width:6px;height:6px;flex:none;background:#779a5b;box-shadow:inset -1px -1px #56804e}
.trackicon{width:16px;height:16px;color:#7e8d74}
.resident-body[aria-pressed="true"] .trackicon{color:#3d7861}
.viewtools{right:calc(var(--edge) + env(safe-area-inset-right,0px));bottom:calc(var(--edge) + env(safe-area-inset-bottom,0px))}
.viewtools .surface{display:flex;align-items:center;padding:3px}
.viewtools button{height:42px;width:44px}
.divider{height:23px;width:1px;background:#cebf9f;margin:0 2px}
.help{position:absolute;z-index:3;bottom:calc(var(--edge) + 7px + env(safe-area-inset-bottom,0px));left:50%;transform:translateX(-50%);display:flex;align-items:center;justify-content:center;gap:13px;white-space:nowrap;pointer-events:none;padding:7px 12px;color:#425d57;background:#fff7de9e;clip-path:var(--small)}
.help span{display:flex;align-items:center;gap:5px;font-weight:600;font-size:11px}
.help .i{width:14px;height:14px}
.help b{width:3px;height:3px;background:#87998b}
.touchword{display:none!important}
#marker{position:absolute;top:0;left:0;z-index:2;pointer-events:none;will-change:transform;color:var(--ink);display:none;filter:drop-shadow(0 2px 0 #293e437d)}
#marker>div{transform:translate(-50%,-100%);display:flex;flex-direction:column;align-items:center}
.mapbadge{background:var(--ink);padding:2px;clip-path:var(--small)}
.mapbadge>span{display:flex;align-items:center;gap:5px;min-height:17px;clip-path:var(--small);background:#ffdd88;padding:1px 5px}
.mapbadge .i{width:12px;height:12px;color:#775631}
#markerText{font:700 10px/1.2 "Courier New",monospace;letter-spacing:.2px}
.pin{height:5px;width:6px;background:#36494a;clip-path:polygon(0 0,100% 0,100% 45%,67% 45%,67% 100%,33% 100%,33% 45%,0 45%)}
.sr{position:absolute;width:1px;height:1px;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap}
@media(hover:hover){button:hover{background-color:#e7e7cb}.resident-body:hover{background-color:#f4ebce}.time-options button[aria-pressed="true"]:hover{background-color:#3a6c61}}
@media(max-width:960px){.help{left:auto;right:calc(var(--edge) + 162px);transform:none;gap:9px}.help span{font-size:10px}.help{padding:6px 8px}.brand .surface{gap:10px}.residents{width:251px}}
@media(max-width:810px){.help{bottom:9px;right:auto;left:50%;transform:translateX(-50%)}.residents,.viewtools{bottom:calc(40px + env(safe-area-inset-bottom,0px))}}
@media(max-width:620px){:root{--edge:12px}.brand .surface{padding:8px 10px 9px 7px;gap:7px}.crest{width:34px;height:35px}.crest canvas{width:32px;height:32px}#wordmark{width:125px;height:16px}h1{margin-bottom:4px}.kicker{font-size:6.8px;letter-spacing:1.3px}.time-options{padding:3px;gap:0}.time-options button{padding:0;width:41px;height:44px}.time-options button span{position:absolute;clip:rect(0,0,0,0);height:1px;width:1px;overflow:hidden}.time-options button .i{width:20px;height:20px}.viewtools .surface{flex-direction:column;padding:2px}.viewtools button{width:43px;height:42px}.viewtools .divider{height:1px;width:24px;margin:0}.residents{width:min(251px,calc(100vw - 91px))}.resident-body{padding:9px 11px 11px}.resident-head{min-height:40px}.pager button{width:34px;height:42px}.help{gap:8px;width:max-content;max-width:96%;bottom:calc(6px + env(safe-area-inset-bottom,0px))}.help span{font-size:9.5px}.help .i{width:12px;height:12px}.deskword{display:none!important}.touchword{display:inline!important}.resident-head .eyebrow{font-size:9px;letter-spacing:1.1px}}
@media(pointer:coarse){.deskword{display:none!important}.touchword{display:inline!important}.pager button{min-width:37px;min-height:44px}.viewtools button,.time-options button{min-height:44px}}
@media(max-width:350px){.crest{display:none}.brand .surface{height:49px}}
@media(max-height:510px) and (min-width:621px){:root{--edge:10px}.brand .surface{padding:5px 9px;gap:8px}.crest{width:32px;height:32px}.crest canvas{width:30px;height:30px}#wordmark{width:138px;height:16px}.kicker{font-size:7px}.residents{bottom:12px;width:215px}.resident-body{padding:5px 8px 7px;gap:8px}.portrait{width:35px;height:35px}.portrait canvas{width:34px;height:34px}.resident-head{min-height:28px}.pager button{height:28px}.viewtools{bottom:12px}.help{left:50%;right:auto;bottom:10px;transform:translateX(-50%);gap:6px}.help .i{display:none}.help span{font-size:9px}}
@media(pointer:coarse){.pager button,.viewtools button,.time-options button{min-width:44px;min-height:44px}}
</style>
</head>
<body>
<svg class="defs" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"><defs>
<symbol id="paw" viewBox="0 0 24 24"><path d="M2 8h4v5H2zM7 3h4v7H7zm6 0h4v7h-4zm5 5h4v5h-4zM9 12h6v2h3v2h2v4h-2v1h-4v-1h-4v1H6v-1H4v-4h2v-2h3z"/></symbol>
<symbol id="sun" viewBox="0 0 24 24"><path d="M11 0h2v4h-2zm0 20h2v4h-2zM0 11h4v2H0zm20 0h4v2h-4zM3 3h3v3H3zm15 0h3v3h-3zM3 18h3v3H3zm15 0h3v3h-3zM9 6h6v2h2v2h1v4h-1v2h-2v2H9v-2H7v-2H6v-4h1V8h2z"/></symbol>
<symbol id="sunset" viewBox="0 0 24 24"><path d="M11 2h2v4h-2zM3 6h3v3H3zm15 0h3v3h-3zM0 13h4v2H0zm20 0h4v2h-4zM9 9h6v2h2v2h1v3H6v-3h1v-2h2zM1 17h22v2H1zm4 4h14v2H5z"/></symbol>
<symbol id="moon" viewBox="0 0 24 24"><path d="M10 2h6v2h-4v2h-2v6h2v3h3v2h5v-2h2v4h-3v2h-4v1H9v-2H5v-3H3v-4H2V9h2V6h2V4h4zM18 3h2v2h2v2h-2v2h-2V7h-2V5h2z"/></symbol>
<symbol id="left" viewBox="0 0 16 16"><path d="M9 2h3v3H9v2H6v2h3v2h3v3H9v-2H6v-2H3V6h3V4h3z"/></symbol>
<symbol id="right" viewBox="0 0 16 16"><path d="M4 2h3v2h3v2h3v4h-3v2H7v2H4v-3h3V9h3V7H7V5H4z"/></symbol>
<symbol id="focus" viewBox="0 0 24 24"><path d="M2 2h7v3H5v4H2zm13 0h7v7h-3V5h-4zM2 15h3v4h4v3H2zm17 0h3v7h-7v-3h4zm-9-6h4v1h1v4h-1v1h-4v-1H9v-4h1z"/></symbol>
<symbol id="plus" viewBox="0 0 24 24"><path d="M10 4h4v6h6v4h-6v6h-4v-6H4v-4h6z"/></symbol>
<symbol id="minus" viewBox="0 0 24 24"><path d="M4 10h16v4H4z"/></symbol>
<symbol id="homeview" viewBox="0 0 24 24"><path d="M2 3h6v2H4v4H2zm14 0h6v6h-2V5h-4zM2 15h2v4h4v2H2zm18 0h2v6h-6v-2h4zM10 7h4v2h3v2h2v3h-3v2h-3v1h-2v-1H8v-2H5v-3h2V9h3zm0 3v1H8v1h3v1h2v-1h3v-1h-2v-1z"/></symbol>
<symbol id="orbit" viewBox="0 0 24 24"><path d="M7 4h10v2H7zM4 6h3v2H4zM2 8h2v5H2zm2 5h3v2H4zm3 2h6v-3h2v2h2v2h2v2h-2v2h-2v2h-2v-4H7zm10-9h3v2h-3zm3 2h2v5h-2z"/></symbol>
<symbol id="zoom" viewBox="0 0 24 24"><path d="M5 2h9v2h3v3h2v7h-2v3h-2v1h2v2h3v2h2v2h-5v-2h-2v-2h-2v-2H6v-2H3v-3H1V7h2V4h2zm1 3v2H4v6h2v2h7v-2h3V7h-3V5z"/></symbol>
<symbol id="star" viewBox="0 0 16 16"><path d="M7 0h2v3h1v2h5v3h-2v2h-2v2h1v3H9v-2H7v2H4v-3h1v-2H3V8H1V5h5V3h1z"/></symbol>
</defs></svg>
<main id="app" aria-label="Whisker Isle, an interactive floating cat town">
<canvas id="sky" class="scene-layer" aria-hidden="true"></canvas>
<canvas id="world" class="scene-layer" tabindex="0" aria-label="Three-dimensional cat island. Drag to rotate, pinch or scroll to zoom, or select a cat to follow it."></canvas>
<canvas id="effects" class="scene-layer" aria-hidden="true"></canvas>
<header class="hud brand"><div class="frame"><div class="surface"><div class="crest"><canvas id="crestCat" width="32" height="32" aria-hidden="true"></canvas></div><div><h1><span class="sr">Whisker Isle</span><svg id="wordmark" aria-hidden="true" preserveAspectRatio="xMinYMid meet"></svg></h1><span class="kicker">FLOATING CAT TOWN</span></div></div></div></header>
<nav class="hud daybar" aria-label="Time of day"><div class="frame"><div class="surface time-options">
<button type="button" data-phase="0" aria-pressed="true" title="Day"><svg class="i"><use href="#sun"/></svg><span>Day</span></button>
<button type="button" data-phase="1" aria-pressed="false" title="Sunset"><svg class="i"><use href="#sunset"/></svg><span>Sunset</span></button>
<button type="button" data-phase="2" aria-pressed="false" title="Night"><svg class="i"><use href="#moon"/></svg><span>Night</span></button>
</div></div></nav>
<div id="marker" aria-hidden="true"><div><div class="mapbadge"><span><svg class="i"><use id="pinIcon" href="#star"/></svg><span id="markerText"></span></span></div><i class="pin"></i></div></div>
<section class="hud residents" aria-label="Island residents"><div class="frame"><div class="surface">
<div class="resident-head"><span class="eyebrow"><svg class="i"><use href="#paw"/></svg>RESIDENTS</span><div class="pager"><button type="button" id="prev" aria-label="Follow previous cat"><svg class="i"><use href="#left"/></svg></button><span class="counter" id="counter">01 / 12</span><button type="button" id="next" aria-label="Follow next cat"><svg class="i"><use href="#right"/></svg></button></div></div>
<button type="button" class="resident-body" id="follow" aria-label="Follow Miso" aria-pressed="false"><span class="portrait"><canvas id="portrait" width="32" height="32"></canvas></span><span class="bio"><span class="nameline"><span id="catName">Miso</span><span class="role" id="role">Mayor</span></span><span class="activity"><span class="dot"></span><span id="activity">Making the rounds</span></span></span><svg class="i trackicon"><use href="#focus"/></svg></button>
</div></div></section>
<div class="help" aria-hidden="true"><span><svg class="i"><use href="#orbit"/></svg>Drag to rotate</span><b></b><span><svg class="i"><use href="#zoom"/></svg><em class="deskword" style="font-style:normal">Scroll to zoom</em><em class="touchword" style="font-style:normal">Pinch to zoom</em></span><b></b><span><svg class="i"><use href="#paw"/></svg>Tap a cat to follow</span></div>
<nav class="hud viewtools" aria-label="Island view"><div class="frame"><div class="surface">
<button type="button" id="overview" title="Reset island view" aria-label="Reset island view"><svg class="i"><use href="#homeview"/></svg></button><span class="divider"></span><button type="button" id="zoomOut" title="Zoom out" aria-label="Zoom out"><svg class="i"><use href="#minus"/></svg></button><button type="button" id="zoomIn" title="Zoom in" aria-label="Zoom in"><svg class="i"><use href="#plus"/></svg></button>
</div></div></nav>
<div class="sr" id="announce" aria-live="polite"></div>
</main>
<script id="vertex" type="x-shader/x-vertex">
precision highp float;
attribute vec3 aPosition,aNormal,aColor;
attribute vec2 aMeta;
uniform mat4 uView,uLightView;
uniform vec4 uObject;
uniform vec2 uYaw,uAnim;
uniform float uClock;
varying vec3 vNormal,vColor,vWorld,vLight;
varying float vMaterial;
void main(){
 vec3 p=aPosition;
 float part=aMeta.y;
 if(abs(abs(part)-1.0)<0.1){
   float stride=sin(uAnim.x)*part*uAnim.y;
   p.z+=stride*.145;p.y+=max(0.0,stride)*.075;
 }else if(part==2.0){p.x+=sin(uAnim.x*.28+p.y*1.7)*.095*max(.15,p.y-.23);}
 else if(part==3.0){p.y*=1.0+.027*sin(uAnim.x*.23);}
 else if(part==5.0){float wing=sin(uAnim.x);p.y+=abs(p.x)*(.45+wing*.85);p.x*=.80+.18*wing;}
 else if(part==7.0){p.y-=mod(uClock*1.85+floor(p.x*3.0)*.31,1.12);}
 vec3 spun=vec3(p.x*uYaw.x+p.z*uYaw.y,p.y,-p.x*uYaw.y+p.z*uYaw.x);
 vec3 w=uObject.xyz+spun*uObject.w;
 vNormal=vec3(aNormal.x*uYaw.x+aNormal.z*uYaw.y,aNormal.y,-aNormal.x*uYaw.y+aNormal.z*uYaw.x);
 vColor=aColor;vMaterial=aMeta.x;vWorld=w;
 vLight=(uLightView*vec4(w,1.0)).xyz*.5+.5;
 gl_Position=uView*vec4(w,1.0);
}
</script>
<script id="fragment" type="x-shader/x-fragment">
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
varying vec3 vNormal,vColor,vWorld,vLight;
varying float vMaterial;
uniform vec3 uLightDir,uAmbient,uSun,uHaze,uLamps[6];
uniform float uGlow,uPass,uShadowOn,uTexel;
uniform sampler2D uShadow;
float lit(vec2 offset,float depth){
 vec2 e=texture2D(uShadow,vLight.xy+offset*uTexel).rg;
 return step(depth,e.x+e.y/255.0);
}
void main(){
 if(uPass>.5){
   vec2 v=fract(gl_FragCoord.z*vec2(1.0,255.0));v.x-=v.y/255.0;gl_FragColor=vec4(v,0.0,1.0);return;
 }
 float d=max(0.0,dot(vNormal,uLightDir));
 float visibility=1.0;
 if(uShadowOn>.5&&d>.005&&vMaterial<3.9&&min(vLight.x,vLight.y)>0.0&&max(vLight.x,vLight.y)<1.0){
   float z=vLight.z-.0010-.00135*(1.0-d);
   visibility=.25*(lit(vec2(-.7,-.7),z)+lit(vec2(.7,-.7),z)+lit(vec2(-.7,.7),z)+lit(vec2(.7,.7),z));
 }
 vec3 rgb=vColor*(uAmbient*(.925+.09*max(0.0,vNormal.y)) +uSun*d*(.20+.80*visibility));
 if(uGlow>.015&&vMaterial<3.9){
   float lightSum=0.0;
   for(int i=0;i<6;i++){vec3 q=vWorld-uLamps[i];float f=max(0.0,1.0-dot(q,q)*.089);lightSum+=f*f;}
   rgb+=vec3(.38,.195,.049)*lightSum*uGlow*(vColor*.55+.45);
 }
 if(vMaterial>.5&&vMaterial<2.5){
   vec3 glow=vMaterial>1.5?vec3(1.0,.83,.47):vec3(1.0,.696,.315);
   rgb=mix(rgb,glow*(.94+.06*max(vNormal.y,0.0)),min(1.0,uGlow*1.13));
 }
 if(vMaterial>3.5){rgb=mix(uHaze,rgb,.58);}
 gl_FragColor=vec4(clamp(rgb,0.0,1.0),1.0);
}
</script>
<script>
'use strict';

const $=id=>document.getElementById(id), TAU=Math.PI*2;
const clamp=(v,a,b)=>Math.max(a,Math.min(b,v)), lerp=(a,b,t)=>a+(b-a)*t;
const fract=x=>x-Math.floor(x);
function noise(a,b=0,c=0){return fract(Math.sin(a*127.1+b*311.7+c*74.7)*43758.5453123)}
let seed=761121;
function random(){seed=(Math.imul(seed,1664525)+1013904223)>>>0;return seed/4294967296}
function rgb(c){return [(c>>16)&255,(c>>8)&255,c&255]}
function pack(r,g,b){return (clamp(Math.round(r),0,255)<<16)|(clamp(Math.round(g),0,255)<<8)|clamp(Math.round(b),0,255)}
function tint(c,k){return pack(((c>>16)&255)*k,((c>>8)&255)*k,(c&255)*k)}
function blend(a,b,t){return pack(lerp(a>>16,b>>16,t),lerp((a>>8)&255,(b>>8)&255,t),lerp(a&255,b&255,t))}
function css(c){return '#'+c.toString(16).padStart(6,'0')}
const dot=(a,b)=>a[0]*b[0]+a[1]*b[1]+a[2]*b[2];
const cross=(a,b)=>[a[1]*b[2]-a[2]*b[1],a[2]*b[0]-a[0]*b[2],a[0]*b[1]-a[1]*b[0]];
const norm=a=>{const n=Math.hypot(...a)||1;return a.map(v=>v/n)};
const sub=(a,b)=>a.map((v,i)=>v-b[i]);
const glyphs={
 W:'10001/10001/10001/10101/10101/11011/10001',I:'111/010/010/010/010/010/111',
 h:'1100/1100/1110/1101/1101/1101/1101',i:'1/0/1/1/1/1/1',
 s:'0000/0000/0111/1000/0110/0001/1110',k:'1000/1000/1001/1010/1100/1010/1001',
 e:'0000/0000/0110/1001/1111/1000/0111',r:'0000/0000/1011/1100/1000/1000/1000',
 l:'11/01/01/01/01/01/01',' ':'00/00/00/00/00/00/00',
 F:'1111/1000/1000/1110/1000/1000/1000',S:'0111/1000/1000/0110/0001/0001/1110',H:'1001/1001/1001/1111/1001/1001/1001'
};
function pixels(word,draw){let x=0;for(const ch of word){const rows=(glyphs[ch]||glyphs[' ']).split('/');rows.forEach((r,y)=>{for(let i=0;i<r.length;i++){if(r[i]==='1')draw(x+i,y)}});x+=rows[0].length+1}return x-1}
{
 let d='';const w=pixels('Whisker Isle',(x,y)=>{d+='M'+x+' '+y+'h1v1h-1z'});
 const svg=$('wordmark');svg.setAttribute('viewBox','0 0 '+w+' 7');svg.setAttribute('shape-rendering','crispEdges');
 const p=document.createElementNS('http://www.w3.org/2000/svg','path');p.setAttribute('d',d);p.setAttribute('fill','currentColor');svg.append(p);
}
function portrait(canvas,coat,hat){
 const g=canvas.getContext('2d'), ink='#344744',f=css(coat.fur),b=css(coat.patch),m=css(coat.bib);
 g.clearRect(0,0,32,32);
 const r=(x,y,w,h,c)=>{g.fillStyle=c;g.fillRect(x,y,w,h)};
 r(3,6,3,11,ink);r(6,8,3,7,ink);r(9,10,2,4,ink);
 r(26,6,3,11,ink);r(23,8,3,7,ink);r(21,10,2,4,ink);
 r(4,8,2,7,f);r(6,10,2,4,'#e7a196');r(26,8,2,7,b);r(24,10,2,4,'#e7a196');
 r(5,11,22,4,ink);r(3,14,26,8,ink);r(5,22,22,3,ink);r(8,25,16,2,ink);
 r(6,12,20,3,f);r(4,15,24,7,f);r(6,21,20,3,f);r(9,24,14,2,f);
 r(6,12,6,4,b);r(5,15,4,3,b);r(13,12,2,3,b);r(17,12,2,2,b);
 r(21,13,4,2,b);r(23,15,4,3,b);r(6,20,6,2,'#edb3a1');r(21,20,5,2,'#edb3a1');
 r(11,20,10,4,m);r(9,21,14,2,m);r(10,17,2,3,ink);r(21,17,2,3,ink);
 r(10,17,1,1,'#fff9e3');r(21,17,1,1,'#fff9e3');r(15,20,3,1,'#aa6d68');r(16,21,1,2,ink);
 r(14,23,2,1,ink);r(17,23,2,1,ink);r(1,19,5,1,ink);r(0,22,5,1,ink);r(26,19,5,1,ink);r(27,22,5,1,ink);
 if(hat){r(10,7,14,3,'#283c42');r(12,2,9,6,'#304f56');r(13,2,7,1,'#49666c');r(12,6,9,2,'#e1b660');r(10,9,14,1,'#26383e')}
}
let vertices=[];
const identity={x:0,y:0,z:0,c:1,s:0,k:1};let pose=identity;
function at(x,y,z,angle,scale,fn){
 const p=pose,c=Math.cos(angle),s=Math.sin(angle);
 pose={x:p.x+(x*p.c+z*p.s)*p.k,y:p.y+y*p.k,z:p.z+(-x*p.s+z*p.c)*p.k,c:p.c*c-p.s*s,s:p.s*c+p.c*s,k:p.k*scale};fn();pose=p;
}
function quad(a,b,c,d,n,color,mat=0,part=0){
 const p=pose,r=(color>>16)/255,g=((color>>8)&255)/255,bl=(color&255)/255;
 const nx=n[0]*p.c+n[2]*p.s,ny=n[1],nz=-n[0]*p.s+n[2]*p.c;
 function v(q){vertices.push(p.x+(q[0]*p.c+q[2]*p.s)*p.k,p.y+q[1]*p.k,p.z+(-q[0]*p.s+q[2]*p.c)*p.k,nx,ny,nz,r,g,bl,mat,part)}
 v(a);v(b);v(c);v(a);v(c);v(d);
}
const UP=[0,1,0],DOWN=[0,-1,0],FR=[0,0,1],RT=[1,0,0],BK=[0,0,-1],LT=[-1,0,0];
function box(x,y,z,w,h,d,col,mat=0,part=0,mask=63){
 if(!mask)return;
 const l=x-w*.5,r=x+w*.5,b=z-d*.5,f=z+d*.5,t=y+h;
 if(mask&1)quad([l,t,b],[l,t,f],[r,t,f],[r,t,b],UP,col,mat,part);
 if(mask&2)quad([l,y,b],[r,y,b],[r,y,f],[l,y,f],DOWN,col,mat,part);
 if(mask&4)quad([l,y,f],[r,y,f],[r,t,f],[l,t,f],FR,col,mat,part);
 if(mask&8)quad([r,y,f],[r,y,b],[r,t,b],[r,t,f],RT,col,mat,part);
 if(mask&16)quad([r,y,b],[l,y,b],[l,t,b],[r,t,b],BK,col,mat,part);
 if(mask&32)quad([l,y,b],[l,y,f],[l,t,f],[l,t,b],LT,col,mat,part);
}
function oct(x,y,z,w,h,d,cut,col,mat=0,part=0){
 const l=x-w/2,r=x+w/2,b=z-d/2,f=z+d/2,e=Math.min(w/2,d/2,cut);
 const ring=[[l+e,b],[l,b+e],[l,f-e],[l+e,f],[r-e,f],[r,f-e],[r,b+e],[r-e,b]];
 for(let i=0;i<8;i++){
  const a=ring[i],v=ring[(i+1)%8],A=[a[0],y,a[1]],B=[v[0],y,v[1]],C=[v[0],y+h,v[1]],D=[a[0],y+h,a[1]];
  quad(A,D,C,B,norm([a[1]-v[1],0,v[0]-a[0]]),col,mat,part);
  quad([x,y+h,z],D,C,C,UP,col,mat,part);
  quad([x,y,z],B,A,A,DOWN,col,mat,part);
 }
}
function beam(a,b,width,col,depth=width){
 const dir=norm(sub(b,a)),u=norm(cross(Math.abs(dir[1])>.88?[0,0,1]:UP,dir)),v=cross(dir,u);
 const q=(p,i,j)=>p.map((val,k)=>val+u[k]*i*width*.5+v[k]*j*depth*.5);
 const A=q(a,-1,-1),B=q(a,1,-1),C=q(a,1,1),D=q(a,-1,1),E=q(b,-1,-1),F=q(b,1,-1),G=q(b,1,1),H=q(b,-1,1);
 quad(A,B,C,D,dir.map(n=>-n),col);quad(E,H,G,F,dir,col);quad(A,E,F,B,v.map(n=>-n),col);quad(C,G,H,D,v,col);quad(B,F,G,C,u,col);quad(D,H,E,A,u.map(n=>-n),col);
}
function voxels(nx,ny,nz,s,cx,by,cz,paint,mat=0){
 const data=new Uint32Array(nx*ny*nz),idx=(i,j,k)=>i+nx*(k+nz*j);
 for(let j=0;j<ny;j++)for(let k=0;k<nz;k++)for(let i=0;i<nx;i++){
  data[idx(i,j,k)]=paint((i-(nx-1)*.5)*s,(j+.5)*s,(k-(nz-1)*.5)*s,i,j,k)||0;
 }
 for(let j=0;j<ny;j++)for(let k=0;k<nz;k++)for(let i=0;i<nx;i++){
  const p=idx(i,j,k),color=data[p];if(!color)continue;
  let mask=0;
  if(j===ny-1||!data[p+nx*nz])mask|=1;if(!j||!data[p-nx*nz])mask|=2;
  if(k===nz-1||!data[p+nx])mask|=4;if(i===nx-1||!data[p+1])mask|=8;
  if(!k||!data[p-nx])mask|=16;if(!i||!data[p-1])mask|=32;
  if(mask)box(cx+(i-(nx-1)*.5)*s,by+j*s,cz+(k-(nz-1)*.5)*s,s,s,s,color,mat,0,mask);
 }
}
function mesh(fn){const first=vertices.length/11;fn();return {first,count:vertices.length/11-first}}
const TILE=.81;
function outline(x,z,scale=1,level=0){
 const px=(x+.15*level)/(12.25*scale),pz=(z+.04*level)/(7.7*scale);
 return Math.pow(Math.abs(px),2.65)+Math.pow(Math.abs(pz),2.45)<1+.045*Math.sin(x*1.1+z*.47)+.046*Math.cos(z*1.6-x*.33)+level*.012*Math.sin(x*2+z*3);
}
function isWater(x,z){
 const a=(x-6.04)/3.13,b=(z-2.88)/2.14;
 return a*a+b*b<1+.075*Math.sin(x*2.3+z)||(z>3.72&&Math.abs(x-(5.35-.15*(z-4)))<.91+.10*Math.sin(z*2));
}
function waterTile(x,z){return isWater(Math.round(x/TILE)*TILE,Math.round(z/TILE)*TILE)}
const paths=[
 [[-10,-.05],[-8.2,.85],[-5.8,1.52],[-2.8,1.55],[-.15,1.17],[2.8,.05],[4.8,-.65],[9.35,-.55]],
 [[-.3,-2.9],[-.15,1.2],[-1.5,3.0],[-2.4,4.9],[-2.25,6.25]],
 [[-1.48,2.9],[.8,3.7],[2.9,5.32],[7.3,5.32],[8.8,4.3]],
 [[-8.0,.8],[-9.35,2.15],[-9.3,4.05],[-7.1,5.4],[-4.0,5.6],[-2.2,4.75]],
 [[8.95,-.5],[9.65,1.25],[9.15,3.9]]
];
function distSegment(x,z,a,b){const dx=b[0]-a[0],dz=b[1]-a[1],t=clamp(((x-a[0])*dx+(z-a[1])*dz)/(dx*dx+dz*dz),0,1);return Math.hypot(x-a[0]-t*dx,z-a[1]-t*dz)}
function road(x,z){if(x>-7.55&&x<-1.65&&z>-.3&&z<2.15)return true;return paths.some(line=>line.some((p,i)=>i&&distSegment(x,z,line[i-1],p)<.63))}
const lampPositions=[],poolsOfLight=[],falls=[];
function vine(x,z,dx,dz,len,key){
 const s=dx?0:1,t=dx?1:0;
 for(let i=0;i<len;i++){const side=(noise(key,i)>.53?1:-1)*.13;
  box(x+s*side,-.06-i*.24-.24,z+t*side,dx?.08:.24,.27,dz?.08:.24,i%3?0x638947:0x7da34d);
  if(i%2===0)box(x+s*(side+.17),-.15-i*.24,z+t*(side+.17),dx?.10:.21,.13,dz?.10:.21,0x91ac56);
 }
}
function terrain(){
 const levels=[[0,-.29,1,0xb2cc75],[-.29,-.62,1.002,0xcaa469],[-.62,-1.37,.992,0xdbad77],[-1.37,-2.15,.947,0xc99a73],[-2.15,-2.91,.855,0xb38870],[-2.91,-3.64,.72,0x9f7d72],[-3.64,-4.32,.53,0x937676],[-4.32,-4.94,.33,0x897278],[-4.94,-5.32,.15,0x837079]];
 function has(i,k,j){return j>=0&&j<levels.length&&outline(i*TILE,k*TILE,levels[j][2],j)}
 for(let j=0;j<levels.length;j++)for(let k=-10;k<=10;k++)for(let i=-16;i<=16;i++){
  if(!has(i,k,j))continue;
  const x=i*TILE,z=k*TILE,wet=!j&&isWater(x,z),path=!j&&!wet&&road(x,z),n=noise(i,k,j+1);
  const bank=!wet&&!j&&(isWater(x+.62,z)||isWater(x-.62,z)||isWater(x,z+.62)||isWater(x,z-.62));
  let top=wet?-.205:levels[j][0],bottom=levels[j][1],color=tint(levels[j][3],.91+.16*n);
  if(!j){
   color=wet?blend(0x48b7ba,0x75d3c3,.42+.19*Math.sin(x*.97+z*1.64)+.25*n):path?tint(0xe0c48f,.97+.055*n):bank?tint(0xc9c890,.95+.07*n):tint(blend(0xa2c16a,0xbad47d,.4+.28*Math.sin(x*.64+z*.39)+.11*Math.cos(z)),.977+.05*n);
  }
  let m=0;if(!has(i,k,j-1))m|=1;if(!has(i,k,j+1))m|=2;
  for(const [di,dk,bit] of [[0,1,4],[1,0,8],[0,-1,16],[-1,0,32]]){
   if(!has(i+di,k+dk,j)||(!j&&!wet&&isWater(x+di*TILE,z+dk*TILE)))m|=bit;
  }
  box(x,bottom,z,TILE,top-bottom,TILE,color,wet?3:0,0,m);
  if(!j){
   if(wet){
    if(n>.46)box(x+.06,-.199,z-.095,.19+.34*n,.012,.055,0x96e2d5,3);
    if(n>.79)box(x-.17,-.198,z+.13,.17,.012,.05,0xb7ebd7,3);
    if(!has(i,k+1,0)&&z>5)falls.push([x,z+TILE/2]);
   }else if(path){
    if(n>.26){
     const pc=tint(n>.66?0xeddab0:0xcfb883,.966+.08*noise(i,k,11));
     box(x+(n-.5)*.18,.007,z+(noise(i,k,32)-.5)*.25,.25+.31*noise(i,k,71),.021,.15+.15*n,pc);
     if(n>.73)box(x-.24,.009,z+.22,.21,.015,.12,0xeedcb1);
    }
   }else if(n>.61&&!bank){
    const gc=n>.84?0xc9da86:0x91b45e;
    box(x+.12,.001,z,.21,.012,.085,gc);box(x-.025,.003,z+.095,.10,.012,.075,gc);
    if(n>.82){box(x-.14,.01,z-.15,.066,.11,.065,0x87ab56);box(x-.07,.01,z-.15,.065,.17,.065,0x9ebd60)}
   }
  }
  if(j>=1&&j<=5){
   for(const [di,dk,bit]of [[0,1,4],[1,0,8],[0,-1,16],[-1,0,32]]){
    if(!(m&bit)||noise(i+di*6,k+dk*7,j)<.49)continue;
    const n2=noise(i,k,j*19+3),off=(n2-.5)*.29,w=.18+.39*noise(i,k,j+81);
    box(x+di*(TILE*.5+.018)+(di?0:off),bottom+(top-bottom)*(.19+.44*n2),z+dk*(TILE*.5+.018)+(dk?0:off),di?.075:w,.08+.12*n2,dk?.075:w,tint(color,n2>.45?1.115:.852));
    if(j===1&&!isWater(x,z)&&n2>.50)vine(x+di*(TILE*.5+.061),z+dk*(TILE*.5+.061),di,dk,3+Math.floor(5*n),i*7+k);
   }
  }
 }
 for(const [x,z]of falls){
  const len=4.8+noise(x,z)*.9;
  box(x,-len,z+.09,TILE+.006,len-.213,.245,0x6bcdd0,3);
  for(let i=0;i<4;i++){
   const cx=x-TILE*.39+i*TILE*.25,l=len*(.7+.3*noise(x,i,8));
   box(cx,-l,z+.22,.08+.04*noise(x,i),l-.25,.045,i%2?0x9fe4dc:0x58baca,3);
   for(let j=0;j<3;j++)box(cx,-.31-j*1.32,z+.251,.056,.16+.29*noise(i,j),.022,0xceeee0,3,7);
  }
  box(x,-.214,z-.03,TILE,.026,.30,0xabe8da,3);
 }
}

const WOOD=0x996d4e,WOODL=0xc39462,BEAM=0x715440,CREAM=0xf2dfb6,TEAL=0x487c76;
const smokeOrigins=[];
function worldPoint(x,y,z){return [pose.x+(x*pose.c+z*pose.s)*pose.k,pose.y+y*pose.k,pose.z+(-x*pose.s+z*pose.c)*pose.k]}
function flower(x,y,z,c,s=.9){
 box(x,y,z,.047*s,.22*s,.047*s,0x54824d);box(x-.045*s,y+.085*s,z,.12*s,.06*s,.07*s,0x75a554);
 box(x,y+.20*s,z,.24*s,.07*s,.102*s,c);box(x,y+.19*s,z,.103*s,.10*s,.235*s,c);box(x,y+.26*s,z,.073*s,.052*s,.07*s,0xf5cb60);
}
function flowerPatch(x,z,r,c,count){
 for(let i=0;i<count;i++){const a=random()*TAU,d=Math.sqrt(random())*r,px=x+Math.cos(a)*d,pz=z+Math.sin(a)*d;if(outline(px,pz)&&!waterTile(px,pz))flower(px,.025,pz,i%4===0?0xffe9b6:c,.70+random()*.61)}
}
function bush(x,z,s,c=0x779c50){
 at(x,0,z,0,s,()=>{
  oct(0,.03,0,.94,.34,.81,.16,tint(c,.89));box(-.10,.29,.02,.74,.21,.60,c);
  box(.25,.24,-.14,.45,.19,.42,tint(c,.98));box(-.13,.47,.06,.41,.15,.39,tint(c,1.08));
  for(let i=0;i<5;i++)box((random()-.5)*.64,.45+random()*.08,(random()-.5)*.43,.11,.08,.12,tint(c,1.12));
 });
}
function tree(x,z,s,seedTree,warm=false){
 at(x,.012,z,0,s,()=>{
  oct(0,0,0,.62,.21,.63,.14,0x8c7150);box(0,.07,0,.43,1.97,.43,0x926743);
  box(-.12,.19,.22,.12,1.2,.055,0xab7c4b);box(.12,.54,.222,.055,.45,.045,0x795940);
  beam([0,1.04,0],[-.67,1.85,.09],.24,WOOD);beam([.06,1.26,-.05],[.55,2.0,-.38],.21,BEAM);
  const lobes=[[-.55,1.09,.23,1.04,.90,.97],[.57,1.23,-.05,.96,.94,1.03],[.02,1.94,-.22,1.0,.82,.91],[-.15,.79,-.45,1.10,.81,1.00],[.22,1.05,.63,.78,.80,.74]];
  voxels(11,10,11,.305,0,1.23,0,(X,Y,Z,i,j,k)=>{
   let density=3;for(const l of lobes)density=Math.min(density,((X-l[0])/l[3])**2+((Y-l[1])/l[4])**2+((Z-l[2])/l[5])**2);
   const n=noise(i+seedTree,k,j);
   if(density>1+(n-.5)*.16)return 0;
   const v=clamp(.16+Y*.22-X*.105+Z*.046+.085*Math.sin(i*1.45+k+seedTree)+(n-.5)*.095,0,1);
   return blend(warm?0x729347:0x4a8e57,warm?0xc5d56a:0x9fca68,v);
  });
  if(warm)for(const f of [[-.83,2.27,1.05],[.56,2.42,1.01],[-1.15,2.12,.64],[1.17,2.54,.60]]){box(f[0],f[1],f[2],.16,.18,.16,0xe8ad5c);box(f[0]-.025,f[1]+.17,f[2],.055,.075,.06,0x5a774d)}
 });
}
function barrel(x,y,z,s=1,open=false){
 at(x,y,z,0,s,()=>{
  oct(0,0,0,.60,.13,.60,.11,0x986d4c);oct(0,.10,0,.68,.52,.68,.15,0xb28758);oct(0,.59,0,.62,.11,.62,.12,0xa78055);
  oct(0,.16,0,.699,.078,.699,.15,0x68756a);oct(0,.49,0,.70,.077,.70,.15,0x68756a);
  oct(0,.70,0,.57,.026,.57,.11,open?0x52645d:0xc59d6a);
  if(!open){box(-.085,.726,0,.018,.009,.49,0x98724c);box(.115,.726,0,.017,.009,.49,0x98724c)}
 });
}
function crate(x,y,z,w=.72,h=.49,d=.62){
 box(x,y+.02,z,w-.075,h-.055,d-.075,0x86664c);
 for(let i=0;i<3;i++){const sy=y+i*h/3;box(x,sy,z+d*.5,w,h*.265,.06,i%2?0xc29663:0xb98c5b);box(x,sy,z-d*.5,w,h*.265,.06,WOODL);for(const sign of [-1,1])box(x+sign*w/2,sy,z,.06,h*.265,d,tint(WOODL,.95+.04*i))}
 for(const sign of [-1,1])for(const side of [-1,1])box(x+sign*(w/2-.035),y,z+side*(d/2-.035),.092,h+.035,.095,0xd4ac75);
 box(x,y+h-.027,z,w-.06,.02,d-.06,0x9d7956);
}
function fish(x,y,z,angle,scale,body=0x76a6ad){
 at(x,y,z,angle,scale,()=>{
  oct(.01,.035,0,.46,.116,.184,.057,body);box(-.238,.059,0,.112,.075,.118,tint(body,.83));
  box(-.30,.046,-.056,.093,.09,.108,body);box(-.30,.046,.056,.093,.09,.108,body);
  box(-.006,.137,-.015,.149,.045,.065,tint(body,.83));box(.176,.138,.038,.041,.026,.048,0x314d55);
  box(.079,.103,.093,.115,.031,.015,0xd7e7d9);
 });
}
function pot(x,y,z,s=1,c=0xba7960){
 at(x,y,z,0,s,()=>{oct(0,0,0,.37,.34,.37,.071,c);oct(0,.28,0,.45,.104,.45,.077,tint(c,1.09));box(0,.385,0,.32,.02,.32,0x695641);flower(-.05,.397,.01,0xf7c450,1.22);flower(.085,.392,.025,0xe89388,1.08)});
}
function flowerbox(w){
 box(0,-.055,.145,w+.12,.21,.30,WOOD);box(0,.116,.158,w+.18,.070,.34,0xc79b6b);
 for(let i=0;i<Math.floor(w/.18);i++){const xx=-w*.44+i*.184;box(xx,.176,.16,.16,.12,.16,i%2?0x729956:0x8dac59);if(i%2===0)flower(xx,.20,.18,i%4?0xf3bc58:0xec9b84,.68)}
}
function windowUnit(x,y,z,w,h,angle=0,shutters=true,flowers=false){
 at(x,y,z,angle,1,()=>{
  box(0,0,0,w+.11,h+.11,.13,0x805f49);box(0,.052,.074,w-.036,h-.015,.058,0x385d61);
  box(0,.115,.112,w-.15,h-.15,.026,0x83c3c1,1);
  box(-w*.21,h*.55,.128,w*.12,h*.27,.014,0xbce1cd,1);
  box(0,.085,.147,.055,h-.08,.052,0xf0d6a6);box(0,h*.52,.146,w-.075,.063,.052,0xf0d6a6);
  box(0,-.037,.07,w+.22,.10,.25,0xe5c898);
  if(shutters)for(const side of [-1,1]){
   const sx=side*(w*.5+.15);box(sx,.035,.003,.203,h+.04,.11,0x598980);
   for(let i=0;i<4;i++)box(sx,.12+h*i*.227,.065,.18,.050,.038,0x78a094);
  }
  if(flowers)flowerbox(w);
 });
}
function doorUnit(x,z,w,h,color){
 at(x,.15,z,0,1,()=>{
  box(0,0,0,w+.21,h+.14,.145,BEAM);box(0,.057,.088,w-.045,h-.032,.073,color);
  for(let i=1;i<5;i++)box(-w*.5+w*i/5,.10,.128,.018,h-.13,.018,tint(color,.79));
  box(0,h*.54,.142,w*.57,h*.33,.035,0x456968);
  box(0,h*.54+.06,.172,w*.46,h*.25,.014,0x9bcbc1,1);box(0,h*.55+.05,.192,.04,h*.26,.02,CREAM);
  box(w*.31,h*.42,.159,.088,.088,.08,0xe4bf67);box(0,h+.073,.022,w+.37,.145,.24,0xa67a54);
  box(0,-.13,.24,w+.41,.15,.57,0xc6b28e);box(0,-.112,.52,w+.58,.065,.28,0xd9c6a1);
 });
}
function roof(w,d,h,peak,color,alongX,wallColor){
 at(0,0,0,alongX?Math.PI/2:0,1,()=>{
  const span=alongX?d:w,len=alongX?w:d,half=span*.5+.34,rows=Math.max(6,Math.round(half/.285)),step=half/rows,rise=peak/rows,full=len+.65;
  box(0,h-.15,0,half*2+.11,.19,full+.09,tint(color,.65));
  for(let j=0;j<rows;j++){
   const remain=half-step*j,yy=h+j*rise;
   box(0,yy-.025,0,Math.max(.12,(span*.5)*(1-j/rows)*2),rise+.045,len,wallColor);
   for(const side of [-1,1]){
    const cx=side*(remain-step*.5);
    box(cx,yy-.035,0,step+.033,rise*.81+.065,full,tint(color,.81));
    const tileSize=.55,start=-full/2,end=full/2;let left=start,idx=0;
    while(left<end-.001){
     const tile=Math.min(idx===0&&j%2?tileSize*.54:tileSize,end-left),cz=left+tile/2,n=noise(idx+side*100,j,Math.floor(h*33));
     const col=tint(color,.91+n*.16+(j%3===0?.018:0));
     box(cx,yy+rise*.38,cz,step+.052,rise*.61+.066,Math.max(.011,tile-.017),col);
     if(n>.66&&tile>.21)box(cx-side*.048,yy+rise+.068,cz+tile*.12,step*.47,.017,tile*.42,tint(col,1.075));
     left+=tile;idx++;
    }
    for(const e of [-1,1])box(cx,yy-.035,e*(len*.5+.052),step+.012,rise+.035,.115,0x846349);
   }
  }
  for(let zz=-full*.5;zz<full*.5-.02;zz+=.48){
   const l=Math.min(.48,full*.5-zz);oct(0,h+peak-.02,zz+l*.5,.38,.157,l+.019,.045,tint(color,1.06+.06*noise(zz,h)));
  }
 });
}
function chimney(x,base,z,s=1){
 at(x,base,z,0,s,()=>{
  box(0,0,0,.5,1.34,.54,0xba8067);
  for(let i=0;i<5;i++){box(0,.11+i*.245,0,.51,.036,.55,0xd9a68b);box(i%2?.11:-.11,.145+i*.245,.274,.036,.17,.018,0xcf9881)}
  box(0,1.24,0,.65,.16,.68,0x9c6656);box(0,1.40,0,.61,.09,.64,0xddad89);box(0,1.49,0,.36,.018,.38,0x6e5851);
  smokeOrigins.push(worldPoint(0,1.53,0));
 });
}
function discFace(x,y,z,r,depth,col){
 const n=9;
 for(let i=0;i<n;i++){
  const t=(i+.5)*2/n-1,ww=Math.max(.18,Math.round(Math.sqrt(1-t*t)*9)/9)*2*r;
  box(x,y-r+i*2*r/n,z,ww,2*r/n+.001,depth,col);
 }
}
function clockFace(x,y,z){
 discFace(x,y,z,.56,.13,0x79573f);discFace(x,y,z+.073,.483,.049,0xe6b863);discFace(x,y,z+.107,.401,.041,0xffe9b5);
 for(let i=0;i<4;i++){const a=i*Math.PI*.5;box(x+Math.sin(a)*.308,y+Math.cos(a)*.305-.03,z+.138,.061,.064,.026,0x6c6756)}
 beam([x,y,z+.162],[x-.14,y+.14,z+.162],.050,0x4c5d59);beam([x,y,z+.166],[x+.17,y+.091,z+.166],.05,0x4c5d59);box(x,y-.034,z+.181,.071,.074,.032,0xb8834b);
}
const houses={
 market:{x:-4.45,z:-2.42,w:4.72,d:2.88,h:2.68,p:1.40,c:0x58848e,wall:0xf1dbc0,ax:true},
 hall:{x:-.05,z:-4.23,w:3.67,d:3.24,h:3.38,p:1.79,c:0xd47b55,wall:0xf2dbb3,ax:false},
 east:{x:5.52,z:-3.03,w:3.47,d:3.04,h:2.66,p:1.43,c:0x4f9d9d,wall:0xeacba0,ax:false},
 west:{x:-8.62,z:-3.65,w:2.62,d:2.52,h:2.22,p:1.14,c:0xdb9b62,wall:0xeac99f,ax:false}
};
function house(key,q){
 const {x,z,w,d,h,p,c,wall,ax}=q;
 at(x,0,z,0,1,()=>{
  box(0,.007,0,w+.16,.30,d+.16,0xa9987d);box(0,.255,0,w+.08,.11,d+.08,0xc8b69a);box(0,.32,0,w,h-.32,d,wall);
  for(const side of [-1,1])for(const e of [-1,1])box(side*(w*.5-.05),.32,e*(d*.5-.035),.13,h-.29,.14,0xa78360);
  box(0,h-.24,0,w+.088,.13,d+.088,0xa78360);
  for(let i=0;i<13;i++){
   const by=.52+noise(i,7,h)*(h-.98);
   box((noise(i,45,h)-.5)*(w-.36),by,d/2+.008,.09+noise(i,4)*.17,.065,.019,0xe1c8a7);
   if(i%2===0)box(w/2+.008,by,(noise(i,61,h)-.5)*(d-.33),.025,.068,.19,0xdcc1a1);
  }
  if(key==='hall'){
   box(0,1.96,0,w+.09,.16,d+.09,0x9e7757);
   doorUnit(-.63,d/2,.85,1.55,0x567d73);windowUnit(.80,.69,d/2,.76,.92,0,true,true);
   windowUnit(-.9,2.23,d/2,.70,.76,0,false);windowUnit(.81,2.23,d/2,.70,.76,0,false);
   for(const z0 of [-.75,.73]){windowUnit(w/2,.71,z0,.66,.89,Math.PI/2,false);windowUnit(w/2,2.20,z0,.61,.73,Math.PI/2,false)}
   box(0,2.03,d*.5+.035,.115,h-1.98,.097,0xa27d58);
  }else if(key==='east'){
   doorUnit(-.69,d/2,.75,1.77,0xb77b55);windowUnit(.72,.86,d/2,.80,.96,0,true,true);
   windowUnit(w/2,.86,.48,.77,.94,Math.PI/2,true,true);windowUnit(0,.79,-d/2,.82,.97,Math.PI,false);
   pot(-1.39,.02,d*.5+.27,.94);pot(1.37,.02,d*.5+.25,.81,0x759791);
  }else if(key==='west'){
   doorUnit(-.55,d/2,.69,1.52,0x8a9a76);windowUnit(.65,.78,d/2,.56,.76,0,false,true);
   windowUnit(-w/2,.72,.11,.69,.79,-Math.PI/2,true);windowUnit(w/2,.72,0,.6,.75,Math.PI/2,false);
  }else{
   windowUnit(-.95,.82,d/2,1.22,.94,0,false);doorUnit(.99,d/2,.82,1.68,0x5e8982);
   windowUnit(w/2,.87,0,.78,1.0,Math.PI/2,true);windowUnit(-w/2,.87,0,.78,1,-Math.PI/2,true);
  }
  if(key!=='east'){
   windowUnit(-w*.22,.79,-d*.5,.63,.80,Math.PI,true,key==='west');
   if(key!=='west')windowUnit(w*.24,.83,-d*.5,.77,.84,Math.PI,false);
   if(key==='hall'){windowUnit(-.48,2.17,-d*.5,.83,.74,Math.PI,true);windowUnit(-w*.5,.77,.21,.73,.89,-Math.PI/2,true,true)}
  }
  roof(w,d,h,p,c,ax,wall);
  if(key==='east'||key==='west'){
   const rr=key==='east'?.29:.23;
   discFace(0,h+.34,d*.5+.045,rr,.093,0x967450);
   discFace(0,h+.34,d*.5+.105,rr*.73,.035,0x8cbfba);
   box(0,h+.34-rr*.68,d*.5+.137,.041,rr*1.37,.036,CREAM);
   box(0,h+.323,d*.5+.137,rr*1.37,.044,.035,CREAM);
  }
  if(key==='hall'){
   clockFace(0,h+.59,d*.5+.087);
   box(.02,h+p+.10,-.84,.067,1.02,.067,0x8e714a);box(.02,h+p+1.1,-.84,.13,.12,.13,0xe8be69);
   for(let i=0;i<5;i++)box(.125+.15*i,h+p+.61+i*.03,-.84+Math.sin(i*.9)*.035,.16,.44-i*.055,.055,i<3?0x559a94:0x72aaa0);
   box(.31,h+p+.79,-.799,.15,.078,.032,0xf2ddb0);
   chimney(-1.15,h+p*.31,-.92,.88);
  }else if(key==='market'){chimney(-1.53,h+p*.69,-.47,.78)}
  else if(key==='east'){chimney(-1.03,h+p*.34,-.85,.83)}
  else{chimney(-.79,h+p*.30,-.57,.65)}
 });
}
function marketStall(){
 const m=houses.market;
 at(m.x,0,m.z,0,1,()=>{
  const back=m.d/2+.05,front=back+1.58,wide=5.08,stripes=11;
  for(const sx of [-1,1]){
   box(sx*(wide*.5-.16),.016,front-.07,.137,2.10,.14,WOOD);
   box(sx*(wide*.5-.16),.04,front-.07,.215,.27,.22,0xa18462);
   beam([sx*(wide*.5-.16),1.63,front-.07],[sx*(wide*.5-.16),2.31,front-.65],.095,WOODL);
  }
  box(0,1.93,front-.07,wide-.08,.12,.15,BEAM);
  for(let i=0;i<stripes;i++){
   const xx=-wide*.5+(i+.5)*wide/stripes,col=i%2?0xffeac4:0xdf705a;
   for(let r=0;r<6;r++){
    const zz=back+(r+.5)*(front-back)/6,yy=2.54-r*.095;
    box(xx,yy,zz,wide/stripes+.002,.123,(front-back)/6+.015,tint(col,1-.006*r));
   }
   box(xx,1.87,front+.006,wide/stripes+.001,.29,.102,tint(col,.96));
   box(xx,1.79,front+.016,wide/stripes-.11,.095,.09,tint(col,.93));
  }
  box(0,.036,front-.40,4.55,.67,.90,0xa27b54);
  for(let i=0;i<17;i++)box(-2.14+i*.268,.091,front+.065,.232,.59,.067,tint(0xc09262,.94+.10*noise(i)));
  box(0,.15,front+.107,4.58,.083,.067,0xac7b53);box(0,.66,front-.38,4.71,.145,1.05,0xd5ae76);
  for(let i=0;i<3;i++){
   const tx=-1.50+i*1.51;box(tx,.80,front-.36,1.36,.09,.80,0x65969b);
   box(tx,.873,front-.35,1.21,.039,.66,0xb5d9cf);
   for(const side of [-1,1]){box(tx+side*.66,.83,front-.36,.075,.15,.84,WOODL);box(tx,.83,front-.36+side*.38,1.34,.13,.058,0xddb584)}
   for(let k=0;k<3;k++)fish(tx-.36+k*.35,.913,front-.34+Math.sin(k+i)*.07,1.06+.15*Math.sin(2*k+i),.84,i===1?0xda9a82:0x6e9da9);
   box(tx+.46,.955,front-.66,.031,.24,.031,BEAM);box(tx+.46,1.111,front-.66,.241,.155,.035,0xffe5ab);box(tx+.46,1.176,front-.636,.109,.024,.02,0x826d50);
  }
  const fy=2.77,sz=back+.14;
  for(const s of [-1,1])box(s*1.18,2.50,sz-.04,.103,.88,.13,WOOD);
  box(0,fy-.059,sz,3.10,.79,.177,0x87684b);box(0,fy,sz+.102,2.98,.666,.086,0x426c70);
  box(0,fy+.612,sz+.153,2.96,.035,.029,0xe4b875);
  pixels('FISH',(xx,yy)=>box(-.47+xx*.078,fy+.072+(6-yy)*.076,sz+.158,.078,.076,.034,0xffeab7));
  const icon=['.......xx','..xxxx.xx','.xxxxxxx.','xxxxxxxx.','.xxxxxxx.','..xxxx.xx','.......xx'];
  icon.forEach((row,j)=>[...row].forEach((p,i)=>{if(p==='x')box(-1.27+i*.074,fy+.07+(6-j)*.075,sz+.159,.074,.076,.033,i===2&&j===2?0x354d4c:0xefb484)}));
  at(-m.w*.5,0,.84,-Math.PI/2,1,()=>{
   for(let i=0;i<5;i++){
    const sx=(i-2)*.285,col=i%2?0xffe9c3:0xde7b64;
    for(let j=0;j<4;j++)box(sx,2.03-j*.061,.12+j*.18,.286,.085,.196,col);
    box(sx,1.72,.764,.285,.186,.069,tint(col,.96));
   }
   for(const side of [-1,1])beam([side*.61,1.37,.056],[side*.61,1.85,.68],.075,WOOD);
  });
  barrel(wide*.5+.24,.018,front-.91,.82);crate(-wide*.5-.21,.02,front-.75,.65,.43,.59);crate(-wide*.5-.13,.46,front-.83,.56,.35,.49);
 });
}
function wallSign(x,z){
 at(x,0,z,-.14,1,()=>{
  for(const s of [-1,1]){beam([s*.28,.032,.16],[s*.28,1.12,0],.085,WOOD);beam([s*.28,.032,-.36],[s*.28,1.07,0],.062,BEAM)}
  box(0,.24,.065,.69,.91,.095,0xbc9163);box(0,.31,.125,.55,.73,.039,0x476762);
  box(0,.74,.153,.28,.108,.018,0xe2dcb3);box(.17,.723,.152,.066,.15,.017,0xe2dcb3);
  box(-.045,.56,.153,.29,.035,.018,0xd0cda6);box(.008,.45,.153,.36,.030,.018,0xd0cda6);
 });
}
function lamp(x,z,scale=1){
 at(x,.018,z,0,scale,()=>{
  oct(0,0,0,.36,.165,.36,.076,0x7e8173);box(0,.15,0,.18,1.79,.19,0x866b4d);box(-.047,.24,.099,.047,1.45,.025,0xb18b5f);
  box(0,1.82,0,.30,.137,.30,0x435e57);box(0,1.96,0,.43,.077,.43,0x445955);
  box(0,2.03,0,.322,.43,.322,0xf3d59a,2);
  for(const sx of [-1,1])for(const sz of [-1,1])box(sx*.17,2.02,sz*.17,.045,.45,.045,0x536760);
  box(0,2.46,0,.52,.10,.52,0x48625d);box(0,2.56,0,.36,.10,.36,0x59716a);box(0,2.65,0,.18,.084,.18,0x9c986c);box(0,2.73,0,.085,.084,.085,0x536760);
  lampPositions.push(worldPoint(0,2.24,0));
 });
}
function fence(a,b,count){
 const dx=b[0]-a[0],dz=b[1]-a[1];
 for(let i=0;i<=count;i++){const t=i/count,x=a[0]+dx*t,z=a[1]+dz*t;box(x,.018,z,.16,.74,.16,0xb89363);box(x,.755,z,.192,.082,.192,0xd9bf86)}
 for(const y of [.26,.55])beam([a[0],y,a[1]],[b[0],y,b[1]],.104,0xc8a46e,.091);
}
function bench(x,z,angle){
 at(x,0,z,angle,1,()=>{
  for(const side of [-1,1]){box(side*.66,.013,-.01,.132,.46,.56,0x5d7568);box(side*.66,.34,-.235,.098,.52,.102,0x5a6e61)}
  for(let i=0;i<3;i++)box(0,.441,-.19+i*.188,1.65,.093,.157,0xcb9a62);
  box(0,.68,-.238,1.66,.143,.109,0xc8945e);box(0,.871,-.238,1.66,.143,.109,0xd2a46e);
 });
}
function garland(){
 const a=[1.58,3.21,-2.56],b=[4.05,2.57,-1.45],colors=[0xd98069,0xe4b54f,0x68a79e,0xf1deac,0x8a96b3];
 const point=t=>[lerp(a[0],b[0],t),lerp(a[1],b[1],t)-.33*Math.sin(Math.PI*t),lerp(a[2],b[2],t)];
 for(let i=0;i<9;i++)beam(point(i/9),point((i+1)/9),.029,0x7c7159);
 for(let i=0;i<7;i++){
  const p=point((i+.5)/7),c=colors[i%5];
  for(let j=0;j<3;j++)box(p[0],p[1]-.081*(j+1),p[2]+.014*j,.24-j*.07,.092,.035,c);
 }
}

function yarn(x,z,r,base,phase){
 const cells=13,s=2*r/cells;
 voxels(cells,cells,cells,s,x,.018,z,(X,Y,Z)=>{
  const a=X/r,b=(Y-r)/r,c=Z/r,L=Math.hypot(a,b,c);
  if(L>1.012)return 0;
  const nx=a/(L||1),ny=b/(L||1),nz=c/(L||1);
  const seam=nx*.87-ny*.36+nz*.18;
  const thread=seam>.34?nx*.29+ny*.91-nz*.23:seam<-.58?nx*.38-ny*.28+nz*.88:nx*.80+ny*.48+nz*.23;
  const turn=fract(thread*5.40+.39*Math.cos(nz*2.1+phase)+phase);
  const shade=turn<.24?.77:turn<.49?1.12:turn<.68?1.045:.93;
  return tint(base,shade*(.98+.025*Math.sin(X*4+Z*2)));
 });
}
function yarnTail(points,color){
 for(let i=1;i<points.length;i++)beam([points[i-1][0],.066,points[i-1][1]],[points[i][0],.066,points[i][1]],.072,color,.069);
}
function knitPark(){
 yarn(-7.68,3.24,1.09,0xe48678,.14);yarn(-4.98,4.21,.83,0xa391bd,.54);yarn(-6.72,5.02,.58,0xe6b658,.34);
 beam([-8.02,1.56,3.20],[-8.51,2.83,3.43],.074,0xcaa36c);oct(-8.51,2.80,3.43,.18,.15,.18,.04,0xe3bd82);
 beam([-7.87,1.62,3.05],[-7.91,2.96,2.78],.065,0xdab77e);oct(-7.91,2.94,2.78,.16,.13,.16,.035,0xdeb27a);
 yarnTail([[-7.59,4.20],[-7.88,4.45],[-8.45,4.42],[-8.74,4.75],[-8.49,5.06],[-8.06,5.09],[-7.97,4.83],[-8.21,4.65],[-8.68,4.62],[-8.99,4.77]],0xcb7c70);
 yarnTail([[-4.7,4.87],[-4.56,5.21],[-4.17,5.33],[-3.91,5.14],[-4.02,4.85],[-3.80,4.66],[-3.51,4.67]],0xa794bc);
 yarnTail([[-6.30,5.35],[-6.12,5.58],[-5.68,5.60],[-5.47,5.82]],0xdcaf5b);
 bench(-9.03,1.68,.21);fence([-8.56,6.03],[-5.72,6.68],3);fence([-5.16,6.72],[-3.6,6.92],2);
}
const BRIDGE={x:5.03,z:5.27,length:3.95,width:1.22};
function bridgeY(x){const t=clamp((x-BRIDGE.x)/BRIDGE.length+.5,0,1);return .095+.53*Math.sin(t*Math.PI)}
function bridge(){
 const b=BRIDGE,n=15,step=b.length/n,start=b.x-b.length*.5;
 for(let i=0;i<n;i++){
  const x=start+(i+.5)*step,y=bridgeY(x);
  box(x,y-.169,b.z,step+.008,.13,b.width+.027,0x946e4e);
  box(x,y-.052,b.z,step-.015,.070,b.width+.078,tint(0xca9b67,.93+.12*noise(i,5)));
  if(i%3===1)box(x+.038,y+.019,b.z+.10,.035,.009,.21,0xaf8157);
 }
 for(const side of [-1,1]){
  const z=b.z+side*b.width*.49;
  for(let i=0;i<=4;i++){
   const x=start+b.length*i/4,y=bridgeY(x);
   box(x,y-.23,z,.142,.94,.155,0x986f4c);box(x,y+.70,z,.206,.108,.205,0xd5ae76);
   if(i<4){
    const nx=start+b.length*(i+1)/4,ny=bridgeY(nx);
    beam([x,y+.64,z],[nx,ny+.64,z],.105,0xc69661,.124);
    beam([x,y+.34,z],[nx,ny+.34,z],.063,0xb38458,.068);
   }
  }
 }
}
function rock(x,y,z,s,color=0xa6a58b){
 oct(x,y,z,s*.91,s*.22,s*.68,s*.16,tint(color,.91));oct(x-.025*s,y+s*.19,z,s*.74,s*.23,s*.53,s*.13,color);box(x-.08*s,y+s*.41,z-.02*s,s*.4,s*.072,s*.34,tint(color,1.08));
}
function lily(x,z,s,bloom){
 at(x,-.181,z,0,s,()=>{
  oct(0,0,0,.49,.042,.41,.105,0x759e65);box(.105,.043,.096,.112,.009,.12,0x6dc3bb,3);
  box(-.069,.044,-.045,.143,.009,.028,0xa1b978);
  if(bloom){box(-.042,.049,0,.19,.087,.104,0xe99a9f);box(-.042,.048,0,.104,.102,.19,0xf0acb0);box(-.042,.132,0,.074,.048,.067,0xeac26b)}
 });
}
function reeds(x,z,s){
 for(let i=0;i<6;i++){
  const px=x+(random()-.5)*.46*s,pz=z+(random()-.5)*.33*s,h=(.39+random()*.36)*s,base=waterTile(px,pz)?-.21:0;
  box(px,base,pz,.046,h,.049,i%2?0x819d57:0x6a9456);
  if(i%3===0)box(px,base+h-.016,pz,.083,.195*s,.08,0x9c8051);
  else box(px+.034,base+h-.13,pz,.054,.17,.045,0xa4b764);
 }
}
function pond(){
 bridge();
 lily(7.14,3.63,1.1,true);lily(7.63,3.44,.76,false);lily(6.94,4.07,.73,false);lily(4.48,2.36,.9,true);lily(4.76,2.06,.63,false);lily(8.11,2.04,.83,false);
 reeds(3.0,2.90,.95);reeds(8.63,3.55,.9);reeds(6.27,6.40,.79);reeds(5.42,1.14,.73);reeds(7.96,4.32,.8);
 for(const [x,z,s]of [[3.0,2.17,.69],[3.12,2.74,.43],[3.25,3.36,.35],[6.64,.98,.62],[7.09,1.06,.39],[8.91,3.38,.52],[7.92,4.70,.40],[3.96,6.47,.58],[3.61,6.84,.38]])rock(x,waterTile(x,z)?-.19:.014,z,s,0xb1aa8b);
}
function createTown(){
 terrain();
 for(const [key,q]of Object.entries(houses))house(key,q);
 marketStall();wallSign(-1.54,.74);
 knitPark();pond();garland();
 tree(-10.12,-.91,.98,12,true);tree(-6.14,-5.59,1.05,22);tree(2.84,-5.70,1.17,31);tree(9.0,-3.83,1.03,44,true);
 tree(10.05,.16,.77,18);tree(-2.89,-6.10,.82,19,true);tree(-.28,5.94,.68,9);
 bench(1.77,2.20,-1.13);
 for(const [x,z,s]of [[-10.13,1.31,.93],[-9.84,1.63,.77],[-7.18,-4.99,.93],[-7.71,-5.33,.64],[2.30,-2.34,.74],[2.80,-2.35,.88],[3.29,-2.51,.68],[7.52,-3.73,1],[7.9,-3.23,.66],[8.36,-4.20,.85],[.65,6.51,.83],[-.38,6.86,.64],[-1.02,7.02,.73],[-4.6,6.29,.47],[9.7,2.51,.50],[-10.8,.55,.54]])bush(x,z,s);
 for(const [x,z,s]of [[-1.84,1.03,.96],[2.14,-.94,.94],[2.83,5.91,.88],[7.18,4.68,.80],[-8.36,1.92,.87],[8.46,-.77,.94]])lamp(x,z,s);
 fence([7.5,-5.62],[9.14,-4.76],2);fence([-4.57,-6.48],[-3.13,-6.74],2);
 barrel(-7.16,.025,-.54,.9);barrel(-7.48,.023,-1.17,.72,true);crate(7.47,.012,-2.02,.72,.46,.59);
 pot(1.46,.02,-2.41,1.07,0xaf785e);pot(-1.62,.02,-2.27,.77,0x749787);
 for(const [x,z,r,c,n]of [[-9.56,4.73,.63,0xf4c659,9],[-5.21,6.02,.64,0xf0aa94,9],[-3.33,3.00,.59,0xecc45d,8],[-.72,6.26,.71,0xed9e96,12],[.99,5.69,.51,0xf5ce72,7],[2.39,4.70,.53,0xe9a595,7],[8.16,5.13,.57,0xc3a6c7,8],[9.53,-1.39,.72,0xf2c05b,9],[2.72,-1.97,.37,0xebac83,5],[-9.66,-.09,.52,0xe7a48e,8],[-8.14,-5.33,.48,0xf8cf6c,7]])flowerPatch(x,z,r,c,n);
 for(const [x,y,z,s]of [[-7.82,-3.34,4.15,.91],[-8.27,-3.82,4.49,.42],[8.78,-2.98,2.83,.82],[9.17,-3.50,2.91,.38],[2.82,-4.54,4.62,.48],[-3.28,-4.89,-3.07,.42]]){box(x,y,z,s,s*.78,s*.85,0xab897b);box(x-.03*s,y+.77*s,z,s*.73,.13*s,s*.64,0xc7a088)}
}
const staticTown=mesh(createTown);
const coats=[
 {name:'Miso',fur:0xe6a15b,patch:0xbc7549,bib:0xffdfa8,type:'tabby',scale:1.02},
 {name:'Mochi',fur:0xf4e4c1,patch:0xcb895b,bib:0xffefd1,type:'calico',scale:.90},
 {name:'Nori',fur:0x414e51,patch:0x303f42,bib:0xf1e4c7,type:'tux',scale:.90},
 {name:'Pip',fur:0xe3bd85,patch:0xb18b67,bib:0xffe7bc,type:'tabby',scale:.87},
 {name:'Tofu',fur:0xf1dfbc,patch:0xcfb999,bib:0xffead0,type:'plain',scale:.86},
 {name:'Yuki',fur:0xe2e2d7,patch:0x8e9a9e,bib:0xf4ecdc,type:'spots',scale:.87},
 {name:'Sumi',fur:0x627782,patch:0x435865,bib:0xc9d0c3,type:'tabby',scale:.92},
 {name:'Biscuit',fur:0xd59b65,patch:0xb77952,bib:0xf6dfb5,type:'spots',scale:.91},
 {name:'Clover',fur:0xe9d7b7,patch:0xa97653,bib:0xf7e6c7,type:'calico',scale:.89},
 {name:'Pebble',fur:0xa2acb0,patch:0x788991,bib:0xe1ded4,type:'tabby',scale:.95},
 {name:'Fig',fur:0xb18a6a,patch:0x876a58,bib:0xe2c6a2,type:'tabby',scale:.90},
 {name:'Cleo',fur:0xe5cfaa,patch:0x746b63,bib:0xf6dfbe,type:'point',scale:.88}
];
function catMesh(c,form,mayor){
 const f=c.fur,b=c.patch,cream=c.bib,dark=0x333f40,pink=0xcd8b81;
 function head(y,z,sleep){
  const half=.39;
  oct(0,y,z,.79,.48,.65,.075,f);box(0,y+.452,z-.008,.66,.075,.56,f);
  for(const side of [-1,1]){
   const ec=c.type==='point'||(side<0&&(c.type==='calico'||c.type==='spots'))?b:f;
   box(side*.265,y+.443,z-.006,.234,.194,.254,ec);box(side*.298,y+.622,z-.027,.167,.102,.20,ec);box(side*.32,y+.712,z-.044,.102,.075,.145,ec);
   box(side*.266,y+.507,z+.132,.119,.098,.024,0xe9a79a);box(side*.294,y+.599,z+.107,.062,.078,.027,0xe9a79a);
  }
  if(c.type==='tabby'){
   for(const off of [-.16,0,.16]){box(off,y+.495,z+.17,.068,.037,.20,b);box(off,y+.362+(off===0?-.039:0),z+.314,.071,off===0?.139:.115,.035,b)}
   for(const side of [-1,1]){box(side*half,y+.199,z+.11,.021,.048,.194,b);box(side*half,y+.298,z+.10,.021,.043,.243,b)}
  }else if(c.type==='calico'||c.type==='spots'){
   box(-.215,y+.218,z+.325,.218,.23,.019,b);box(-.226,y+.47,z+.125,.221,.042,.314,b);
   box(.242,y+.35,z+.319,.146,.122,.028,c.type==='calico'?0x616462:b);
  }else if(c.type==='point'){
   oct(0,y+.09,z+.295,.56,.28,.104,.056,b);box(0,y+.337,z+.32,.24,.071,.061,b);
  }
  for(const side of [-1,1]){
   box(side*.09,y+.09,z+.34,.185,.14,.081,cream);
   if(sleep){box(side*.195,y+.264,z+.346,.127,.031,.028,dark);box(side*.244,y+.292,z+.341,.04,.03,.028,dark)}
   else{
    if(c.type==='tux'||c.type==='point')box(side*.203,y+.258,z+.344,.126,.132,.035,0xe9ce80);
    box(side*.192,y+.253,z+.364,.075,.117,.031,dark);box(side*.18-.012,y+.335,z+.385,.028,.029,.012,0xfff8d5);
   }
   for(let i=0;i<2;i++)box(side*(.341+i*.022),y+.14-i*.056,z+.294,.176,.023,.032,blend(dark,cream,.35));
  }
  box(0,y+.206,z+.388,.105,.062,.046,pink);box(0,y+.174,z+.392,.036,.036,.031,0x956e60);
  if(mayor){
   at(.014,y+.504,z-.006,-.12,1,()=>{
    oct(0,0,0,.66,.091,.53,.086,0x2e424a);box(0,.082,0,.411,.295,.351,0x3b515a);box(-.132,.111,.18,.061,.18,.019,0x4a6267);
    box(0,.092,0,.429,.082,.368,0xe4b75d);box(0,.375,0,.43,.042,.36,0x50666a);
   });
  }
 }
 function body(y,z,w,h,d,part=0){
  oct(0,y,z,w,h,d,.105,f,0,part);
  if(c.type==='tabby'){
   for(let i=0;i<3;i++){
    const zz=z-.30+i*.198;box(-.028,y+h+.002,zz,w*.67,.024,.091,b,0,part);
    for(const side of [-1,1])box(side*(w*.5+.003),y+h*.58,zz,.025,h*.37,.092,b,0,part);
   }
  }else if(c.type==='calico'||c.type==='spots'){
   box(-.10,y+h+.002,z-.15,w*.54,.022,d*.40,b,0,part);
   box(-w*.5-.006,y+.085,z-.11,.028,h*.66,d*.36,b,0,part);
   box(.14,y+h+.004,z+.12,w*.35,.024,d*.23,c.type==='calico'?0x646963:b,0,part);
  }
 }
 function tail(y,z){
  box(-.026,y,z,.211,.305,.218,b,0,2);box(-.032,y+.204,z-.127,.201,.323,.213,f,0,2);
  box(-.032,y+.46,z-.146,.19,.311,.189,b,0,2);box(.022,y+.714,z-.132,.278,.167,.183,f,0,2);
  box(.151,y+.724,z-.101,.133,.155,.152,c.type==='tux'?cream:b,0,2);
 }
 if(form==='sleep'){
  body(.072,-.092,.928,.407,.83,3);oct(-.172,.405,-.104,.582,.114,.55,.112,f,0,3);
  box(.157,.028,.208,.248,.157,.323,cream);box(-.066,.028,.268,.198,.122,.266,cream);
  at(.169,.035,.16,.45,.84,()=>head(.246,.016,true));
  box(-.414,.098,-.18,.183,.18,.50,b);box(-.39,.09,.132,.19,.172,.308,f);box(-.261,.083,.293,.313,.146,.158,b);box(-.079,.081,.303,.145,.125,.133,cream);
 }else if(form==='stretch'){
  for(const side of [-1,1]){
   box(side*.207,.016,-.365,.173,.529,.194,f);box(side*.207,.014,-.354,.186,.106,.219,cream);
   box(side*.223,.084,.46,.164,.173,.553,f);box(side*.223,.022,.729,.180,.13,.282,cream);
  }
  body(.463,-.23,.648,.411,.622);oct(0,.294,.113,.62,.356,.52,.08,f);
  head(.265,.524,false);tail(.615,-.54);
 }else{
  for(const side of [-1,1])for(const end of [-1,1]){
   const part=side*end,z=end>0?.263:-.335;
   box(side*.208,.026,z,.186,.363,.203,c.type==='point'?b:f,0,part);
   box(side*.208,.014,z+.022,.205,.10,.238,c.type==='point'?b:cream,0,part);
  }
  body(.302,-.047,.664,.497,.975);
  box(0,.326,.422,.424,.329,.086,cream);
  if(mayor){box(0,.619,.328,.60,.129,.32,0xb65e4d);box(.118,.506,.498,.158,.186,.059,0xce7059)}
  head(.679,.409,false);tail(.494,-.55);
 }
}
const catMeshes=coats.map((c,i)=>({stand:mesh(()=>catMesh(c,'stand',i===0)),stretch:mesh(()=>catMesh(c,'stretch',i===0)),sleep:mesh(()=>catMesh(c,'sleep',i===0))}));
const butterflyMeshes=[0xf1b64f,0xe9999c,0xd4b0ce].map(col=>mesh(()=>{
 box(0,-.034,0,.050,.067,.19,0x6c614e);
 for(const side of [-1,1]){box(side*.128,.01,.039,.204,.058,.156,col,0,5);box(side*.182,.04,.081,.131,.055,.114,tint(col,1.10),0,5);box(side*.104,0,-.084,.139,.046,.104,tint(col,.92),0,5)}
}));
const koiMeshes=[0xe8a06a,0xf3dcb5].map(col=>mesh(()=>{fish(0,0,0,0,1,col);box(-.043,.154,.008,.113,.022,.092,0xd38962)}));
const cloudMesh=mesh(()=>{
 const lobes=[[-2.05,.51,.02,1.86,.55,.95],[-.96,.87,-.14,1.49,.95,1.10],[.19,1.16,-.16,1.43,1.22,1.19],[1.63,.75,.04,1.54,.81,1.03],[2.80,.44,.02,1.03,.48,.78],[-.48,.39,.57,2.11,.45,.83]];
 voxels(23,8,10,.35,0,0,0,(x,y,z)=>{
  if(!lobes.some(l=>((x-l[0])/l[3])**2+((y-l[1])/l[4])**2+((z-l[2])/l[5])**2<1))return 0;
  return blend(0xc8e2e3,0xfff8e6,clamp(.31+y*.33,0,1));
 },4);
});
const modelData=new Float32Array(vertices);vertices=null;

const point=(x,z,action,wait,yaw)=>({x,z,action,wait,yaw});
const routes=[
 [point(-.55,2.38,'survey',4.5,.29),point(-2.78,1.69,'shop',3.8,3.04),point(-4.26,1.53,'shop',3.1,3.06),point(-2.71,2.12,'survey',1.2,1.02),point(-1.12,3.07,'stretch',2,.32),point(.57,1.49,'survey',2.8,-.55)],
 [point(-5.58,1.60,'shop',4.2,2.76),point(-6.34,1.37,'shop',2.1,3.05),point(-6.63,2.10,'pause',1,.46),point(-4.34,2.08,'stretch',2.5,.72),point(-3.84,1.47,'shop',4,3.04)],
 [point(-8.81,4.38,'butterfly',.65,.72),point(-9.24,3.83,'butterfly',.40,-.25),point(-9.27,2.86,'butterfly',1.15,.63),point(-8.78,2.35,'pause',1.3,1.72),point(-9.16,3.78,'butterfly',.38,-.24),point(-8.44,4.78,'stretch',2.4,1.6)],
 [point(-3.46,4.03,'butterfly',1.1,-.24),point(-3.99,3.12,'butterfly',.5,-.6),point(-5.01,2.68,'pause',.85,-1.35),point(-4.01,3.02,'butterfly',.34,1.42),point(-3.34,4.47,'butterfly',1.14,.34),point(-3.6,5.45,'stretch',1.95,-.67)],
 [point(-.06,-3.67,'nap',20,.34),point(-.05,-4.31,'stretch',3.25,1.09),point(-.04,-4.05,'nap',16,2.48)],
 [point(5.53,-2.49,'nap',13.5,-.62),point(5.54,-3.33,'stretch',3.8,-.9),point(5.52,-2.73,'nap',17,.42)],
 [point(5.47,5.28,'watch',3.5,.92),point(6.94,5.28,'watch',2.3,-1.26),point(5.02,5.27,'pause',1.1,-1.39),point(3.10,5.27,'watch',2.5,-1.94),point(4.39,5.27,'stretch',1.95,1.55)],
 [point(9.32,2.09,'watch',3.3,-.57),point(9.53,1.05,'pause',2,-.67),point(8.98,.21,'stretch',2.3,.3),point(9.31,1.47,'watch',1.6,-1.45),point(9.01,3.23,'watch',3.4,-1.65)],
 [point(1.37,-.25,'pause',1.6,.48),point(.42,.63,'survey',2.2,-.27),point(-.18,-1.43,'pause',2.5,2.70),point(.27,-1.53,'stretch',2.05,.53),point(1.12,-.23,'pause',1.2,.56)],
 [point(-2.15,5.93,'stretch',4.2,.39),point(-2.58,6.30,'loaf',7.0,.49),point(-3.14,5.47,'pause',1.75,-.35),point(-2.61,4.55,'stretch',2.75,2.96),point(-1.89,5.38,'loaf',4.8,-.55)],
 [point(-8.81,.28,'pause',1.80,.42),point(-8.04,.80,'pause',1.12,1.10),point(-7.10,1.31,'shop',2.9,2.68),point(-8.09,.90,'stretch',2.1,-1.19),point(-9.15,.43,'pause',2.3,-1.45)],
 [point(7.66,-.48,'pause',2.2,.47),point(6.64,-.67,'pause',1.2,-1.38),point(5.71,-.57,'stretch',2.35,.53),point(4.68,-.67,'pause',1.9,-.52),point(6.23,-.81,'loaf',3.8,.91)]
];
const cats=coats.map((c,i)=>{
 const p=routes[i][0];return {i,coat:c,x:p.x,y:0,z:p.z,yaw:p.yaw,goalYaw:p.yaw,wp:0,state:p.action,wait:p.wait,phase:i*1.48,cycle:0,speed:(i===2||i===3?1.19:.56)+noise(i,14)*.19};
});
function catHeight(cat){
 if(cat.i===4)return houses.hall.h+houses.hall.p+.144;
 if(cat.i===5)return houses.east.h+houses.east.p+.143;
 if(cat.i===6)return bridgeY(cat.x)+.028;
 return .049;
}
const shortest=(a,b)=>Math.atan2(Math.sin(b-a),Math.cos(b-a));
function updateCats(dt){
 for(const c of cats){
  const route=routes[c.i];
  if(c.state==='walk'){
   const p=route[c.wp],dx=p.x-c.x,dz=p.z-c.z,dist=Math.hypot(dx,dz),speed=c.speed*(c.i===4||c.i===5?.59:1);
   if(dist<speed*dt+.003){c.x=p.x;c.z=p.z;c.state=p.action;c.goalYaw=p.yaw;c.wait=p.wait*(.87+.24*noise(c.i,c.cycle++,c.wp))}
   else{c.goalYaw=Math.atan2(dx,dz);c.x+=dx/dist*speed*dt;c.z+=dz/dist*speed*dt}
  }else{
   c.wait-=dt;
   if(c.wait<=0){c.wp=(c.wp+1)%route.length;c.state='walk'}
  }
  c.yaw+=shortest(c.yaw,c.goalYaw)*(1-Math.exp(-dt*5.7));
  c.phase+=dt*(c.state==='walk'?9.2*c.speed:2.48);c.y=catHeight(c);
 }
}
updateCats(0);
const formOf=c=>c.state==='nap'||c.state==='loaf'?'sleep':c.state==='stretch'?'stretch':'stand';
const actNames={survey:'Making the rounds',shop:'Browsing the fish stall',pause:'People-watching',nap:'Napping on the roof',loaf:'Curled up in the grass',stretch:'Having a good stretch',watch:'Watching the koi',butterfly:'Chasing butterflies'};
function activityOf(c){
 if(c.state==='pause')return c.i===6?'Crossing the bridge':c.i===4||c.i===5?'Taking in the view':'Watching the town';
 if(c.state!=='walk')return actNames[c.state];
 if(c.i===0)return 'Making the rounds';if(c.i===2||c.i===3)return 'Chasing butterflies';if(c.i===6)return 'Crossing the bridge';if(c.i===4||c.i===5)return 'Padding along the roof';return 'Out for a stroll';
}
const themes=[
 {sky:[0x83c2d5,0xb4dbdb,0xf1e6c9],cloud:[0xfff9e6,0xe0eeeb,0xb5d7dc],amb:[.728,.755,.752],sun:[.417,.37,.286],dir:[-.55,.83,.38],glow:0},
 {sky:[0x939ac1,0xefb6aa,0xffdcab],cloud:[0xffe4c0,0xeac0b3,0xb69fae],amb:[.756,.598,.64],sun:[.53,.366,.235],dir:[-.88,.46,.30],glow:.39},
 {sky:[0x253853,0x435c78,0x869bab],cloud:[0xaebed0,0x8a9eb6,0x7188a5],amb:[.385,.448,.632],sun:[.247,.297,.39],dir:[.39,.82,-.41],glow:1}
];
let phase=0,weights=[1,0,0],glow=0;
const weather={sky:[],cloud:[],amb:[],sun:[],dir:[],haze:[]};
const sum3=(a,b,c)=>a*weights[0]+b*weights[1]+c*weights[2];
function weightedColor(a,b,c){return pack(sum3(a>>16,b>>16,c>>16),sum3((a>>8)&255,(b>>8)&255,(c>>8)&255),sum3(a&255,b&255,c&255))}
function updateLight(dt){
 const k=1-Math.exp(-dt*3.5);
 for(let i=0;i<3;i++)weights[i]+=((i===phase?1:0)-weights[i])*k;
 for(const key of ['sky','cloud'])for(let j=0;j<3;j++)weather[key][j]=weightedColor(themes[0][key][j],themes[1][key][j],themes[2][key][j]);
 for(const key of ['amb','sun','dir'])for(let j=0;j<3;j++)weather[key][j]=sum3(themes[0][key][j],themes[1][key][j],themes[2][key][j]);
 weather.dir=norm(weather.dir);weather.haze=rgb(weather.sky[1]).map(c=>c/255);glow=sum3(0,.39,1);
}
updateLight(0);
const HOME_A=.465,HOME_E=.585;
function basis(a,e){const ca=Math.cos(a),sa=Math.sin(a),ce=Math.cos(e),se=Math.sin(e);return {r:[ca,0,-sa],u:[-sa*se,ce,-ca*se],f:[sa*ce,se,ca*ce]}}
const startBasis=basis(HOME_A,HOME_E);
function homeBounds(){
 let l=Infinity,r=-Infinity,b=Infinity,t=-Infinity;
 const R=startBasis.r,U=startBasis.u,end=(staticTown.first+staticTown.count)*11;
 for(let i=staticTown.first*11;i<end;i+=11){const x=modelData[i],y=modelData[i+1],z=modelData[i+2],a=x*R[0]+z*R[2],e=x*U[0]+y*U[1]+z*U[2];l=Math.min(l,a);r=Math.max(r,a);b=Math.min(b,e);t=Math.max(t,e)}
 const midX=(l+r)*.5,midY=(b+t)*.5;
 return {w:r-l,h:t-b,center:[R[0]*midX,midY/startBasis.u[1],R[2]*midX]};
}
const framing=homeBounds(),home=framing.center;
let width=1,height=1,fit=1,sy=0,scale=1;
let yaw=HOME_A,pitch=HOME_E,aimYaw=HOME_A,aimPitch=HOME_E,zoom=1,aimZoom=1,velocity=0;
let target=home.slice(),axes=startBasis,selected=0,following=-1;
let world=$('world');const sky=$('sky'),fx=$('effects'),sk=sky.getContext('2d'),ink=fx.getContext('2d');
function project(x,y,z){
 const dx=x-target[0],dy=y-target[1],dz=z-target[2],r=axes.r,u=axes.u,f=axes.f;
 return {x:width*.5+(dx*r[0]+dz*r[2])*scale,y:sy-(dx*u[0]+dy*u[1]+dz*u[2])*scale,d:dx*f[0]+dy*f[1]+dz*f[2]};
}
function cameraMatrix(o,r,u,f,hw,hh,depth,shift=0){
 return new Float32Array([r[0]/hw,u[0]/hh,-f[0]/depth,0,r[1]/hw,u[1]/hh,-f[1]/depth,0,r[2]/hw,u[2]/hh,-f[2]/depth,0,-dot(r,o)/hw,-dot(u,o)/hh+shift,dot(f,o)/depth,1]);
}
function updateCamera(dt){
 if(!contacts.size&&Math.abs(velocity)>.008&&!demo){aimYaw+=velocity*dt;velocity*=Math.exp(-dt*5.2)}
 const k=1-Math.exp(-dt*10.8);yaw=lerp(yaw,aimYaw,k);pitch=lerp(pitch,aimPitch,k);zoom=lerp(zoom,aimZoom,1-Math.exp(-dt*5.4));
 const c=cats[following],dest=c?[c.x,c.y+(formOf(c)==='sleep'?.37:.68)*c.coat.scale,c.z]:home;
 const followEase=1-Math.exp(-dt*(c?4.85:4.10));for(let i=0;i<3;i++)target[i]=lerp(target[i],dest[i],followEase);
 axes=basis(yaw,pitch);scale=fit*zoom;
}
const clouds=[[-14.8,-4.88,2.55,1.16],[14.74,-4.35,-.75,1.20],[-6.7,-6.96,10.3,.73],[10.3,-6.79,8.65,.79],[-11.40,-4.03,-10.30,1.17],[5.96,-4.2,-12.3,.87],[-17.52,-5.83,12.1,.60]];
function object(m,x,y,z,a=0,s=1,p=0,w=0){return {m,x,y,z,co:Math.cos(a),si:Math.sin(a),s,p,w}}
const townObject=object(staticTown,0,0,0);
function drawObjects(time){
 const above=[townObject],below=[];
 for(let i=0;i<clouds.length;i++){const [x,y,z,s]=clouds[i];below.push(object(cloudMesh,x+Math.sin(time*.031+i*1.9)*2.22,y+Math.sin(time*.13+i)*.086,z+Math.cos(time*.029+i)*.79,i%2?.22:-.13,s))}
 for(const c of cats){
  const f=formOf(c),walk=c.state==='walk'?1:0,bob=walk*Math.abs(Math.cos(c.phase))*.024;
  above.push(object(catMeshes[c.i][f],c.x,c.y+bob,c.z,c.yaw,c.coat.scale,c.phase,walk));
 }
 for(let i=0;i<4;i++){
  const cat=cats[2+i%2],t=time*(i%2?.91:1.16)+i*2.4;
  const x=cat.x+Math.sin(cat.yaw)*.90+Math.cos(t*1.53)*(.34+i*.056),z=cat.z+Math.cos(cat.yaw)*.98+Math.sin(t*1.39)*.42;
  above.push(object(butterflyMeshes[i%3],x,.91+.28*Math.sin(t*2.47)+i*.066,z,cat.yaw+t*.62,.89+(i%2)*.13,time*(12.6+i)+i));
 }
 for(let i=0;i<2;i++){
  const t=time*.22+i*2.96,x=6.20+Math.cos(t)*1.18,z=2.87+Math.sin(t)*.57;
  above.push(object(koiMeshes[i],x,-.278,z,Math.atan2(-Math.cos(t)*.57,-Math.sin(t)*1.18),i?.83:1.03));
 }
 return {above,below};
}

function makeGL(canvas){
 const gl=canvas.getContext('webgl',{alpha:true,antialias:false,depth:true,premultipliedAlpha:false,preserveDrawingBuffer:false,powerPreference:'low-power'});
 if(!gl)throw Error('WebGL unavailable');
 function compile(type,source){const sh=gl.createShader(type);gl.shaderSource(sh,source);gl.compileShader(sh);if(!gl.getShaderParameter(sh,gl.COMPILE_STATUS))throw Error(gl.getShaderInfoLog(sh)||'Shader compilation');return sh}
 const prog=gl.createProgram();gl.attachShader(prog,compile(gl.VERTEX_SHADER,$('vertex').textContent));gl.attachShader(prog,compile(gl.FRAGMENT_SHADER,$('fragment').textContent));gl.linkProgram(prog);
 if(!gl.getProgramParameter(prog,gl.LINK_STATUS))throw Error(gl.getProgramInfoLog(prog)||'Shader linking');
 gl.useProgram(prog);
 const buf=gl.createBuffer();gl.bindBuffer(gl.ARRAY_BUFFER,buf);gl.bufferData(gl.ARRAY_BUFFER,modelData,gl.STATIC_DRAW);
 for(const [name,size,offset]of [['aPosition',3,0],['aNormal',3,12],['aColor',3,24],['aMeta',2,36]]){
  const a=gl.getAttribLocation(prog,name);gl.enableVertexAttribArray(a);gl.vertexAttribPointer(a,size,gl.FLOAT,false,44,offset);
 }
 const U={};for(const n of ['uView','uLightView','uObject','uYaw','uAnim','uClock','uLightDir','uAmbient','uSun','uHaze','uGlow','uPass','uShadowOn','uTexel','uShadow','uLamps[0]'])U[n]=gl.getUniformLocation(prog,n);
 const texSize=Math.min(gl.getParameter(gl.MAX_TEXTURE_SIZE),window.innerWidth<700?1024:1536);
 function texture(w,h,pixels){
  const tex=gl.createTexture();gl.bindTexture(gl.TEXTURE_2D,tex);gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA,w,h,0,gl.RGBA,gl.UNSIGNED_BYTE,pixels);
  gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER,gl.NEAREST);gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MAG_FILTER,gl.NEAREST);gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_S,gl.CLAMP_TO_EDGE);gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_T,gl.CLAMP_TO_EDGE);return tex;
 }
 gl.activeTexture(gl.TEXTURE0);
 const white=texture(1,1,new Uint8Array([255,255,255,255])),shadow=texture(texSize,texSize,null),fb=gl.createFramebuffer(),depth=gl.createRenderbuffer();
 gl.bindFramebuffer(gl.FRAMEBUFFER,fb);gl.framebufferTexture2D(gl.FRAMEBUFFER,gl.COLOR_ATTACHMENT0,gl.TEXTURE_2D,shadow,0);
 gl.bindRenderbuffer(gl.RENDERBUFFER,depth);gl.renderbufferStorage(gl.RENDERBUFFER,gl.DEPTH_COMPONENT16,texSize,texSize);gl.framebufferRenderbuffer(gl.FRAMEBUFFER,gl.DEPTH_ATTACHMENT,gl.RENDERBUFFER,depth);
 const shadows=gl.checkFramebufferStatus(gl.FRAMEBUFFER)===gl.FRAMEBUFFER_COMPLETE;gl.bindFramebuffer(gl.FRAMEBUFFER,null);
 gl.enable(gl.DEPTH_TEST);gl.depthFunc(gl.LEQUAL);gl.disable(gl.CULL_FACE);gl.disable(gl.BLEND);gl.disable(gl.DITHER);
 gl.uniform1i(U.uShadow,0);gl.uniform1f(U.uTexel,1/texSize);gl.uniform1f(U.uShadowOn,shadows?1:0);gl.uniform3fv(U['uLamps[0]'],new Float32Array(lampPositions.flat()));
 let nextShadow=-1;
 function draw(o){
  gl.uniform4f(U.uObject,o.x,o.y,o.z,o.s);gl.uniform2f(U.uYaw,o.co,o.si);gl.uniform2f(U.uAnim,o.p,o.w);gl.drawArrays(gl.TRIANGLES,o.m.first,o.m.count);
 }
 function lightsMatrix(){const L=weather.dir,R=norm(cross(UP,L)),V=cross(L,R);return cameraMatrix([0,.1,0],R,V,L,16.6,15.7,30)}
 gl.uniformMatrix4fv(U.uLightView,false,lightsMatrix());
 return {cpu:false,render(time,objects){
  if(gl.isContextLost())return;
  gl.useProgram(prog);gl.uniform1f(U.uClock,time);
  gl.uniform3fv(U.uLightDir,weather.dir);gl.uniform3fv(U.uAmbient,weather.amb);gl.uniform3fv(U.uSun,weather.sun);gl.uniform3fv(U.uHaze,weather.haze);gl.uniform1f(U.uGlow,glow);
  if(shadows&&time>=nextShadow){
   const m=lightsMatrix();gl.bindTexture(gl.TEXTURE_2D,white);gl.bindFramebuffer(gl.FRAMEBUFFER,fb);gl.viewport(0,0,texSize,texSize);
   gl.clearColor(1,1,1,1);gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT);gl.uniform1f(U.uPass,1);gl.uniformMatrix4fv(U.uLightView,false,m);gl.uniformMatrix4fv(U.uView,false,m);
   objects.above.forEach(draw);gl.bindFramebuffer(gl.FRAMEBUFFER,null);nextShadow=time+.125;
  }
  gl.viewport(0,0,gl.drawingBufferWidth,gl.drawingBufferHeight);
  gl.clearColor(0,0,0,0);gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT);gl.uniform1f(U.uPass,0);gl.bindTexture(gl.TEXTURE_2D,shadows?shadow:white);
  const view=cameraMatrix(target,axes.r,axes.u,axes.f,width/(2*scale),height/(2*scale),86,1-2*sy/height);
  gl.uniformMatrix4fv(U.uView,false,view);objects.below.forEach(draw);
  gl.clear(gl.DEPTH_BUFFER_BIT);objects.above.forEach(draw);
 }};
}
function makePainter(canvas){
 const g=canvas.getContext('2d',{alpha:true}),D=modelData;let cacheKey='',fixed=[];
 function faces(o,time,out){
  const start=o.m.first*11,end=start+o.m.count*11,R=axes.r,U=axes.u,F=axes.f,L=weather.dir;
  const ax=(R[0]*o.co-R[2]*o.si)*o.s,az=(R[0]*o.si+R[2]*o.co)*o.s,bx=(U[0]*o.co-U[2]*o.si)*o.s,bz=(U[0]*o.si+U[2]*o.co)*o.s,cx=(F[0]*o.co-F[2]*o.si)*o.s,cz=(F[0]*o.si+F[2]*o.co)*o.s;
  const p=project(o.x,o.y,o.z),sx=canvas.width/width,sy0=canvas.height/height;
  for(let i=start;i<end;i+=66){
   const nx=D[i+3]*o.co+D[i+5]*o.si,ny=D[i+4],nz=-D[i+3]*o.si+D[i+5]*o.co;
   if(nx*F[0]+ny*F[1]+nz*F[2]<.001)continue;
   const a=[],mat=D[i+9],part=D[i+10],nd=Math.max(0,nx*L[0]+ny*L[1]+nz*L[2]);let dep=0,minX=99999,minY=99999,maxX=-99999,maxY=-99999;
   for(const off of [0,11,22,55]){
    const j=i+off;let x=D[j],y=D[j+1],z=D[j+2];
    if(Math.abs(part)===1){const stride=Math.sin(o.p)*part*o.w;z+=stride*.145;y+=Math.max(0,stride)*.075}
    else if(part===2)x+=Math.sin(o.p*.28+y*1.7)*.095*Math.max(.15,y-.23);
    else if(part===3)y*=1+.027*Math.sin(o.p*.23);
    else if(part===5){y+=Math.abs(x)*(.45+Math.sin(o.p)*.85);x*=.8+.18*Math.sin(o.p)}
    else if(part===7)y-=((time*1.85+Math.floor(x*3)*.31)%1.12+1.12)%1.12;
    const px=(p.x+(ax*x+az*z)*scale)*sx,py=(p.y-(bx*x+U[1]*o.s*y+bz*z)*scale)*sy0;
    a.push(px,py);minX=Math.min(minX,px);minY=Math.min(minY,py);maxX=Math.max(maxX,px);maxY=Math.max(maxY,py);dep+=cx*x+F[1]*o.s*y+cz*z;
   }
   if(maxX<0||maxY<0||minX>canvas.width||minY>canvas.height)continue;
   let col=pack(...[0,1,2].map(k=>D[i+6+k]*255*(weather.amb[k]*(.925+.09*Math.max(0,ny))+weather.sun[k]*nd)));
   if(mat===1||mat===2)col=blend(col,mat===1?0xffb651:0xffd47b,Math.min(1,glow*1.12));
   if(mat===4)col=blend(weather.sky[1],col,.58);
   out.push({a,d:p.d+dep*.25,c:css(col)});
  }
 }
 function paint(f){const a=f.a;g.fillStyle=f.c;g.beginPath();g.moveTo(a[0],a[1]);g.lineTo(a[2],a[3]);g.lineTo(a[4],a[5]);g.lineTo(a[6],a[7]);g.closePath();g.fill()}
 return {cpu:true,render(time,objects){
  g.clearRect(0,0,canvas.width,canvas.height);
  const key=[canvas.width,canvas.height,yaw.toFixed(3),pitch.toFixed(3),scale.toFixed(2),...target.map(x=>x.toFixed(2)),glow.toFixed(2),Math.floor(time*2)].join('|');
  if(key!==cacheKey){fixed=[];faces(townObject,time,fixed);fixed.sort((a,b)=>a.d-b.d);cacheKey=key}
  const low=[];objects.below.forEach(o=>faces(o,time,low));low.sort((a,b)=>a.d-b.d).forEach(paint);
  const mov=[];for(let i=1;i<objects.above.length;i++)faces(objects.above[i],time,mov);mov.sort((a,b)=>a.d-b.d);
  let i=0,j=0;while(i<fixed.length||j<mov.length){if(j>=mov.length||(i<fixed.length&&fixed[i].d<=mov[j].d))paint(fixed[i++]);else paint(mov[j++])}
 }};
}
let renderer;
try{renderer=makeGL(world)}catch(err){const c=world.cloneNode(false);world.replaceWith(c);world=c;renderer=makePainter(world)}
world.addEventListener('webglcontextlost',e=>e.preventDefault());
world.addEventListener('webglcontextrestored',()=>{renderer=makeGL(world)});
function resize(){
 const rect=$('app').getBoundingClientRect();width=Math.max(1,Math.round(rect.width));height=Math.max(1,Math.round(rect.height));
 const mobile=width<=620,low=height<510,dp=window.devicePixelRatio||1;
 const density=renderer.cpu?Math.min(.78,760/width):Math.min(dp,mobile?1.42:.91,1610/width);
 world.width=Math.max(1,Math.round(width*density));world.height=Math.max(1,Math.round(height*density));
 const skyDensity=width<650?.74:.50;
 sky.width=Math.max(1,Math.round(width*skyDensity));sky.height=Math.max(1,Math.round(height*skyDensity));
 fx.width=Math.max(1,Math.round(width*(mobile?1:.84)));fx.height=Math.max(1,Math.round(height*(mobile?1:.84)));
 const top=mobile?66:low?29:Math.min(54,height*.060),bottom=mobile?120:low?31:Math.min(67,height*.071);
 sy=(top+height-bottom)*.5;fit=Math.max(.5,Math.min(width*(mobile?.984:width<970?.92:.86)/framing.w,Math.max(height*.51,height-top-bottom)/framing.h));scale=fit*zoom;
}
resize();window.addEventListener('resize',resize);
if(window.ResizeObserver){const ro=new ResizeObserver(()=>{if($('app').clientWidth!==width||$('app').clientHeight!==height)resize()});ro.observe($('app'))}
const skyBanks=[
 [-.02,.19,.15,.032,.37],[.32,.136,.082,.017,.34],[.61,.105,.11,.019,.24],[.93,.20,.17,.037,.49],
 [.14,.38,.18,.044,.52],[.52,.34,.11,.023,.32],[.83,.425,.20,.039,.46],[-.03,.58,.24,.069,.71],
 [.29,.61,.18,.042,.52],[.72,.625,.17,.038,.48],[.986,.61,.25,.066,.74],[.145,.78,.29,.067,.76],
 [.54,.815,.23,.052,.66],[.89,.83,.28,.078,.78],[-.05,.951,.17,.047,.46],[.67,.97,.25,.051,.53]
];
function skyCloud(ctx,x,y,w,h,index,alpha){
 const palette=weather.cloud,n=40,step=w/n,colors=palette.map(css);
 ctx.globalAlpha=alpha;
 const puffs=[[-.31,.20,.45],[-.16,.22,.70],[.005,.27,.98],[.19,.20,.70],[.33,.165,.40]];
 for(let i=0;i<n;i++){
  const u=(i+.5)/n-.5;let rise=0;
  for(let k=0;k<puffs.length;k++){const p=puffs[k],v=(u-p[0])/(p[1]*(.95+.13*noise(k,index)));if(Math.abs(v)<1)rise=Math.max(rise,p[2]*Math.sqrt(1-v*v)*(.82+.30*noise(index,k+1)))}
  if(!rise)continue;
  const px=Math.round(x-w*.5+i*step),pw=Math.max(1,Math.round(x-w*.5+(i+1)*step)-px);
  const q=Math.max(1,Math.round(h*.09)),up=Math.round(rise*h/q)*q,lower=Math.round((.11+.20*Math.cos(u*Math.PI))*h/q)*q;
  const py=Math.round(y-up),shadowline=Math.round(y-.095*h+Math.floor(Math.sin(i*.35+index)*1.4)*q);
  ctx.fillStyle=colors[2];ctx.fillRect(px,py,pw,up+lower);
  ctx.fillStyle=colors[1];ctx.fillRect(px,py,pw,Math.max(1,shadowline-py));
  if(up>h*.3){ctx.fillStyle=colors[0];ctx.fillRect(px,py,pw,Math.max(q,Math.round(up*.21/q)*q))}
 }
 ctx.globalAlpha=1;
}
const starPoints=Array.from({length:94},(_,i)=>[noise(i,641),noise(i,738)*.93,noise(i,961)]);
function celestial(w,h,time){
 const n=weights[2],d=weights[1],px=Math.round(w*(.815-.07*d)),py=Math.round(h*(.235+.18*d-.012*n)),r=Math.max(5,Math.round(Math.min(w,h)*(.033+.012*d)));
 const halo=sk.createRadialGradient(px,py,2,px,py,r*(3.62+1.9*d));halo.addColorStop(0,'rgba(255,238,189,'+(.21+.13*d-.11*n)+')');halo.addColorStop(1,'rgba(255,235,195,0)');
 sk.fillStyle=halo;sk.fillRect(px-r*6,py-r*6,r*12,r*12);
 sk.fillStyle=css(weightedColor(0xfff3c6,0xffe7ad,0xf7e7bb));sk.globalAlpha=.81+.15*d;
 for(let j=-r;j<=r;j++){
  const q=Math.round(Math.sqrt(Math.max(0,r*r-j*j))/2)*2;
  if(n>.50){
   const cut=(r*.87)**2-(j+r*.34)**2;if(cut>0){const edge=Math.round(r*.5-Math.sqrt(cut));if(edge>-q)sk.fillRect(px-q,py+j,Math.min(2*q,edge+q),1)}else sk.fillRect(px-q,py+j,2*q,1);
  }else sk.fillRect(px-q,py+j,2*q,1);
 }
 sk.globalAlpha=1;
 if(n>.02){
  sk.fillStyle='#fff2d3';
  for(let i=0;i<starPoints.length;i++){
   const s=starPoints[i],x=Math.round(s[0]*w),y=Math.round(s[1]*h),q=s[2]>.88?2:1;sk.globalAlpha=n*(.48+.30*Math.sin(time*1.3+i*1.9)+.20*s[2]);
   sk.fillRect(x,y,q,q);if(s[2]>.95){sk.fillRect(x-2,y,6,1);sk.fillRect(x,y-2,1,6)}
  }
 }
 sk.globalAlpha=1;
}
function drawSky(time){
 const w=sky.width,h=sky.height,grad=sk.createLinearGradient(0,0,0,h);
 grad.addColorStop(0,css(weather.sky[0]));grad.addColorStop(.59,css(weather.sky[1]));grad.addColorStop(1,css(weather.sky[2]));sk.fillStyle=grad;sk.fillRect(0,0,w,h);
 celestial(w,h,time);
 for(let i=0;i<skyBanks.length;i++){
  const p=skyBanks[i],wrap=w*1.46,travel=p[0]*w+time*w*(.00075+(i%4)*.00020)-Math.sin(yaw-HOME_A)*w*(.018+i*.0009);
  const x=((travel+w*.23)%wrap+wrap)%wrap-w*.23;
  skyCloud(sk,x,p[1]*h+Math.sin(time*.058+i)*1.4,p[2]*Math.max(w,h*.67),p[3]*Math.min(h,w*.85),i,p[4]);
 }
 if(weights[2]<.8){
  sk.fillStyle=css(blend(weather.sky[0],0x637f86,.38));sk.globalAlpha=.59*(1-weights[2]);
  for(let i=0;i<5;i++){
   const x=Math.round((.21+i*.096)*w+Math.sin(time*.105+i)*12),y=Math.round((.20+.037*Math.sin(i*4.3))*h+Math.cos(time*.31+i)*1.45);
   const wing=Math.sin(time*2.1+i)>.1?-1:0;sk.fillRect(x-3,y+wing,2,1);sk.fillRect(x-1,y,2,1);sk.fillRect(x+1,y+wing,2,1);
  }
  sk.globalAlpha=1;
 }
}

function hiddenByHouse(x,y,z){
 for(let d=.34;d<18;d+=.40){
  const xx=x+axes.f[0]*d,yy=y+axes.f[1]*d,zz=z+axes.f[2]*d;
  if(yy>6.0)break;
  for(const h of Object.values(houses)){
   const dx=Math.abs(xx-h.x),dz=Math.abs(zz-h.z);
   if(dx>h.w*.5+.13||dz>h.d*.5+.13)continue;
   const q=h.ax?dz/(h.d*.5+.30):dx/(h.w*.5+.30);
   if(yy<h.h+h.p*(1-q)-.08&&yy>0)return true;
  }
 }return false;
}
function headPoint(c){
 const f=formOf(c),s=c.coat.scale,forward=f==='sleep'?.22:.39;
 return [c.x+Math.sin(c.yaw)*forward*s,c.y+(f==='sleep'?.99:f==='stretch'?1.06:c.i===0?1.66:1.48)*s,c.z+Math.cos(c.yaw)*forward*s];
}
function catAt(px,py){
 let best=-1,score=Infinity;
 for(const c of cats){
  const s=c.coat.scale,head=headPoint(c),hp=project(...head),pp=project(c.x,c.y+.10,c.z);
  const midX=(hp.x+pp.x)*.5,midY=(hp.y+pp.y)*.5;
  const rx=Math.max(width<620?16:12,scale*s*.58),ry=Math.max(width<620?17:13,Math.abs(hp.y-pp.y)*.56+scale*.19);
  const test=((px-midX)/rx)**2+((py-midY)/ry)**2;
  if(test<=1.28&&!hiddenByHouse(head[0],head[1]-.09,head[2])){
   const rank=test-.013*hp.d;if(rank<score){score=rank;best=c.i}
  }
 }return best;
}
let priorHUD='';
function hud(){
 const c=cats[selected],description=activityOf(c),key=[selected,following,description].join('-');
 if(key===priorHUD)return;priorHUD=key;
 $('catName').textContent=c.coat.name;$('counter').textContent=String(selected+1).padStart(2,'0')+' / 12';$('role').hidden=selected!==0;
 $('activity').textContent=description;$('follow').setAttribute('aria-pressed',String(following===selected));$('follow').setAttribute('aria-label',following===selected?'Stop following '+c.coat.name:'Follow '+c.coat.name);
 $('follow').title=following===selected?'Return to the whole island':'Follow '+c.coat.name;
 document.querySelector('.eyebrow').lastChild.nodeValue=following>=0?'FOLLOWING':'RESIDENTS';
 portrait($('portrait'),c.coat,selected===0);
 $('markerText').textContent=following>=0?c.coat.name:selected===0?'Mayor':c.coat.name;$('pinIcon').setAttribute('href',selected===0?'#star':'#paw');
}
function setPhase(p,say=true){
 if(p===phase)return;phase=p;
 for(const b of document.querySelectorAll('[data-phase]'))b.setAttribute('aria-pressed',String(+b.dataset.phase===p));
 if(say)$('announce').textContent=['Day on Whisker Isle','Sunset on Whisker Isle','Night on Whisker Isle'][p];
}
function followCat(i,say=true,level){
 selected=(i+cats.length)%cats.length;following=selected;velocity=0;
 aimZoom=level||Math.max(aimZoom,Math.min(width,height)<620?2.37:1.77);
 if(say)$('announce').textContent='Following '+cats[selected].coat.name+'. '+activityOf(cats[selected])+'.';
 hud();
}
function wholeIsland(reset,say=true){
 following=-1;aimZoom=1;velocity=0;
 if(reset){aimYaw=yaw+shortest(yaw,HOME_A);aimPitch=HOME_E;selected=0}
 if(say)$('announce').textContent='Whole island view.';hud();
}
function setZoom(z){aimZoom=clamp(z,.63,3.65)}
function effects(time){
 const rx=fx.width/width,ry=fx.height/height;ink.setTransform(rx,0,0,ry,0,0);ink.clearRect(0,0,width,height);ink.globalAlpha=1;
 for(let i=0;i<smokeOrigins.length;i++){
  const s=smokeOrigins[i];
  for(let j=0;j<3;j++){
   const t=fract(time*.27+j/3+i*.29),p=project(s[0]-.57*t*t+.045*Math.sin(time+i),s[1]+t*1.31,s[2]-.12*t);
   const r=Math.max(1,Math.round(scale*(.067+.105*t))),x=Math.round(p.x),y=Math.round(p.y);
   ink.globalAlpha=(1-t)*(.41-.09*weights[2]);ink.fillStyle=css(weather.cloud[0]);
   ink.fillRect(x-r,y-r*.5,2*r,r);ink.fillRect(x-r*.55,y-r,r*1.15,2*r);
  }
 }
 ink.globalAlpha=1;
 if(axes.f[2]>.06){
  ink.fillStyle=css(weightedColor(0xb5e6df,0xe8c6b4,0x87aec3));
  for(const [x,z] of falls)for(let j=0;j<3;j++){
   const age=fract(time*.79+j/3+noise(x,36)),d=project(x+(noise(x,j)-.5)*.60,-4.72-age*1.25,z+.25+age*.12);
   const u=Math.max(1,Math.round(scale*.048));ink.globalAlpha=(1-age)*.69;
   ink.fillRect(Math.round(d.x),Math.round(d.y),u,u*(j%2+1));
  }
  ink.globalAlpha=1;
 }
 if(glow>.015){
  for(let i=0;i<lampPositions.length;i++){
   const lp=lampPositions[i];if(hiddenByHouse(lp[0],lp[1],lp[2]))continue;
   const p=project(...lp),r=Math.max(7,scale*.86);
   if(p.x< -r||p.x>width+r||p.y< -r||p.y>height+r)continue;
   const g=ink.createRadialGradient(p.x,p.y,0,p.x,p.y,r);
   g.addColorStop(0,'rgba(255,209,118,'+glow*.39+')');g.addColorStop(.37,'rgba(255,183,87,'+glow*.145+')');g.addColorStop(1,'rgba(255,179,71,0)');
   ink.fillStyle=g;ink.fillRect(p.x-r,p.y-r,r*2,r*2);
  }
 }
 if(weights[2]>.045){
  for(let i=0;i<17;i++){
   const xx=(i%2===0?-6.7:6.5)+Math.sin(i*8.1+time*.33)*1.98,zz=3.37+Math.cos(i*3.23+time*.25)*1.69;
   const p=project(xx,.63+noise(i,30)*1.1+.27*Math.sin(time*.73+i*1.54),zz),a=clamp(Math.sin(time*1.88+i*4.11)*1.4+.22,0,1)*weights[2];
   if(a<.04)continue;const u=Math.max(1,Math.round(scale*.043));
   ink.globalAlpha=a*.20;ink.fillStyle='#f8cb6e';ink.fillRect(p.x-u*2,p.y-u*2,u*4,u*4);
   ink.globalAlpha=a;ink.fillStyle='#ffdf89';ink.fillRect(Math.round(p.x),Math.round(p.y),u,u);
  }
 }
 for(const c of cats){
  if(c.state!=='nap'&&c.state!=='loaf')continue;
  const t=fract(time*.33+c.i*.336);if(t<.11||t>.94)continue;
  const p=project(c.x+.24+t*.16,c.y+.98+c.coat.scale*t*.56,c.z+.18),u=clamp(Math.round(scale*.034),1,3);
  ink.globalAlpha=Math.sin(t*Math.PI)*.72;ink.fillStyle=weights[2]>.6?'#e8dfc9':'#fdf2ce';
  const xx=Math.round(p.x),yy=Math.round(p.y);ink.fillRect(xx,yy,4*u,u);ink.fillRect(xx+2*u,yy+u,u,u);ink.fillRect(xx+u,yy+2*u,u,u);ink.fillRect(xx,yy+3*u,4*u,u);
 }
 ink.globalAlpha=1;
 const c=cats[selected],h=headPoint(c),p=project(h[0],h[1]+.12,h[2]),mk=$('marker');
 if(p.x>8&&p.x<width-8&&p.y>18&&p.y<height-20&&!hiddenByHouse(...h)){
  mk.style.display='block';mk.style.transform='translate3d('+Math.round(p.x)+'px,'+Math.round(p.y-2+Math.sin(time*2.3)*1.15)+'px,0)';
 }else mk.style.display='none';
}
let demo=location.hash.toLowerCase()==='#demo',demoOrigin=null,demoStep=-1;
function stopDemo(){if(demo){demo=false;demoStep=-1;velocity=0}}
function demoFrame(stamp){
 if(!demo)return;
 if(demoOrigin===null)demoOrigin=stamp;
 const t=((stamp-demoOrigin)/1000)%20,stage=t<3.9?0:t<8.05?1:t<11.7?2:t<14.85?3:4;
 if(stage!==demoStep){
  demoStep=stage;setPhase(stage<2?0:stage<4?1:2,false);
  if(stage===1)followCat(0,false,width<620?2.12:1.73);
  else if(stage===3)followCat(5,false,width<620?1.99:1.57);
  else{wholeIsland(false,false);selected=0;hud()}
 }
 aimYaw=HOME_A+.40*Math.sin(t/20*TAU-.86)+.10*Math.sin(t/20*TAU*2+.22);
 aimPitch=HOME_E+.040*Math.sin(t/20*TAU+.35);
}
window.addEventListener('hashchange',()=>{demo=location.hash.toLowerCase()==='#demo';demoOrigin=null;demoStep=-1});
document.addEventListener('pointerdown',stopDemo,true);document.addEventListener('touchstart',stopDemo,{capture:true,passive:true});document.addEventListener('keydown',stopDemo,true);
for(const b of document.querySelectorAll('.time-options button'))b.addEventListener('click',()=>{stopDemo();setPhase(+b.dataset.phase)});
$('prev').addEventListener('click',()=>{stopDemo();followCat(selected-1)});
$('next').addEventListener('click',()=>{stopDemo();followCat(selected+1)});
$('follow').addEventListener('click',()=>{stopDemo();if(following===selected)wholeIsland(false);else followCat(selected)});
$('overview').addEventListener('click',()=>{stopDemo();wholeIsland(true)});
$('zoomOut').addEventListener('click',()=>{stopDemo();setZoom(aimZoom/1.24)});
$('zoomIn').addEventListener('click',()=>{stopDemo();setZoom(aimZoom*1.24)});
const contacts=new Map();let pinchGap=0,pinchBase=1;
const pairDistance=()=>{const a=[...contacts.values()];return a.length>=2?Math.hypot(a[0].x-a[1].x,a[0].y-a[1].y):0};
function localPointer(e){const r=world.getBoundingClientRect();return [e.clientX-r.left,e.clientY-r.top]}
world.addEventListener('pointerdown',e=>{
 if(e.pointerType==='mouse'&&e.button!==0)return;e.preventDefault();stopDemo();velocity=0;
 const [x,y]=localPointer(e);contacts.set(e.pointerId,{x,y,sx:x,sy:y,moved:false,multi:false,last:performance.now()});
 try{world.setPointerCapture(e.pointerId)}catch(_){}
 world.classList.add('dragging');world.classList.remove('oncat');
 if(contacts.size>=2){for(const p of contacts.values())p.multi=true;pinchGap=Math.max(3,pairDistance());pinchBase=aimZoom}
});
world.addEventListener('pointermove',e=>{
 const [x,y]=localPointer(e),p=contacts.get(e.pointerId);
 if(!p){if(e.pointerType==='mouse'&&!contacts.size)world.classList.toggle('oncat',catAt(x,y)>=0);return}
 const now=performance.now(),dx=x-p.x,dy=y-p.y,dt=clamp((now-p.last)*.001,.009,.073);
 p.x=x;p.y=y;p.last=now;if(Math.hypot(x-p.sx,y-p.sy)>4)p.moved=true;
 if(contacts.size>=2){if(pinchGap>0)setZoom(pinchBase*pairDistance()/pinchGap);velocity=0;return}
 if(p.moved){
  const sensitivity=width<620?.0090:.0069;aimYaw-=dx*sensitivity;aimPitch=clamp(aimPitch+dy*.0034,.34,.96);
  velocity=clamp(lerp(velocity,-dx*sensitivity/dt,.42),-2.2,2.2);
 }
});
function endPointer(e,canceled){
 const p=contacts.get(e.pointerId);if(!p)return;contacts.delete(e.pointerId);
 if(!contacts.size){world.classList.remove('dragging');pinchGap=0;if(performance.now()-p.last>100||p.multi||canceled)velocity=0}
 else if(contacts.size===1){const other=[...contacts.values()][0];other.sx=other.x;other.sy=other.y;other.moved=true;other.multi=true;pinchGap=0}
 if(!canceled&&!p.moved&&!p.multi&&Math.hypot(e.clientX-world.getBoundingClientRect().left-p.sx,e.clientY-world.getBoundingClientRect().top-p.sy)<8){
  const q=localPointer(e),id=catAt(q[0],q[1]);if(id>=0)followCat(id);else if(following>=0)wholeIsland(false);
 }
}
world.addEventListener('pointerup',e=>endPointer(e,false));world.addEventListener('pointercancel',e=>endPointer(e,true));
world.addEventListener('lostpointercapture',e=>{if(contacts.has(e.pointerId))endPointer(e,true)});
world.addEventListener('pointerleave',()=>world.classList.remove('oncat'));
world.addEventListener('wheel',e=>{e.preventDefault();stopDemo();const unit=e.deltaMode===1?14:e.deltaMode===2?height:1;setZoom(aimZoom*Math.exp(-clamp(e.deltaY*unit,-410,410)*(e.ctrlKey?.008:.00175)))},{passive:false});
world.addEventListener('keydown',e=>{
 if(e.key==='ArrowLeft')aimYaw-=.17;else if(e.key==='ArrowRight')aimYaw+=.17;
 else if(e.key==='ArrowUp')aimPitch=clamp(aimPitch+.075,.34,.96);else if(e.key==='ArrowDown')aimPitch=clamp(aimPitch-.075,.34,.96);
 else if(e.key==='+'||e.key==='=')setZoom(aimZoom*1.2);else if(e.key==='-')setZoom(aimZoom/1.2);else if(e.key==='Home')wholeIsland(true);else return;
 e.preventDefault();
});
document.addEventListener('keydown',e=>{if(e.key==='Escape')wholeIsland(true)});
portrait($('crestCat'),coats[1],false);hud();
let lastStamp=null,elapsed=0,lastSky=-1,lastSkySize='';
function frame(stamp){
 requestAnimationFrame(frame);
 if(document.hidden){lastStamp=null;return}
 const interval=renderer.cpu?62:1000/60;
 if(lastStamp!==null&&stamp-lastStamp<interval-.8)return;
 const dt=lastStamp===null?1/60:clamp((stamp-lastStamp)/1000,.001,.12);
 lastStamp=stamp;elapsed+=dt;
 demoFrame(stamp);updateLight(dt);updateCats(dt);updateCamera(dt);
 const size=width+'x'+height;
 if(elapsed-lastSky>.082||lastSkySize!==size){drawSky(elapsed);lastSky=elapsed;lastSkySize=size}
 renderer.render(elapsed,drawObjects(elapsed));hud();effects(elapsed);
}
requestAnimationFrame(frame);
</script>
</body>
</html>