// Bounded, deterministic close-up projection. Geometry never owns Page identity. (() => { const SPOTS_PER_VIEW = 6, PAGES_PER_VIEW = 24; const ns='http://www.w3.org/2000/svg'; const el=(tag,attrs={},text='')=>{const n=document.createElementNS(ns,tag);for(const [k,v] of Object.entries(attrs))n.setAttribute(k,v);if(text)n.textContent=text;return n;}; const uniquePages=spot=>[...new Map((spot.pages||[]).map(p=>[p.page_uuid,p])).values()]; // Only the actual boundary Page is represented by the central palace. The // API may suggest a descendant as a fallback portal (also in undefined // buckets); such a Page must keep its independently selectable building. const surroundingPages=spot=>uniquePages(spot).filter(p=>!(p.page_uuid===spot.portal_page_uuid&&p.path===spot.path_prefix)); const undefinedSpot=spot=>spot.name?.endsWith('-undefined')||spot.path_prefix?.endsWith('/_undefined'); const pageName=p=>p.label||(p.path||'').split('/').filter(Boolean).pop()||p.page_uuid; const pageURL=p=>p.path?p.path.split('/').map(encodeURIComponent).join('/'):`/o/uuid:${encodeURIComponent(p.page_uuid)}`; const slice=(items,page,size)=>{const pages=Math.max(1,Math.ceil(items.length/size)),index=Math.max(0,Math.min(pages-1,page||0));return{items:items.slice(index*size,(index+1)*size),page:index,pages,total:items.length};}; function layout(count) { const columns=Math.min(3,Math.max(1,count)),rows=Math.ceil(count/columns); const width=Math.max(960,columns*380+120),height=Math.max(680,rows*350+180); return {width,height,plots:Array.from({length:count},(_,i)=>({x:width/2-(columns-1)*190+(i%columns)*380,y:height/2-(rows-1)*175+Math.floor(i/columns)*350,w:336,h:314}))}; } const label=(parent,cls,x,y,text,width=100,size=12)=>{ const n=el('text',{class:cls,x,y,'text-anchor':'middle','font-size':size}); // Bounded horizontal lines; the complete name is always in the tooltip. const chars=[...String(text)],lines=[];let line='',used=0; for(const char of chars){const unit=/[\u0000-\u00ff]/.test(char)?size*.57:size;if(used+unit>width&&line){lines.push(line);line='';used=0;}line+=char;used+=unit;} if(line)lines.push(line); lines.slice(0,2).forEach((part,i)=>n.append(el('tspan',{x,dy:i?size+2:0},i===1&&lines.length>2?part.slice(0,-1)+'…':part))); n.append(el('title',{},text));parent.append(n);return n; }; const use=(parent,id,x,y,width,height,cls='')=>{const n=el('use',{href:`#${id}`,x,y,width,height,class:cls});parent.append(n);return n;}; function definitions() { const defs=el('defs'); const paving=el('pattern',{id:'city-court-stone',width:24,height:16,patternUnits:'userSpaceOnUse'}); paving.append(el('rect',{width:24,height:16,class:'estate-stone'}),el('path',{d:'M0 0H24M0 8H24M0 16H24M12 0V8M6 8V16M24 8V16',class:'estate-mortar'}));defs.append(paving); const roof=el('pattern',{id:'city-terracotta',width:8,height:8,patternUnits:'userSpaceOnUse'}); roof.append(el('rect',{width:8,height:8,class:'estate-roof-fill'}),el('path',{d:'M0 4H8M0 8H8M4 0V4M2 4V8M6 4V8',class:'estate-roof-tile'}));defs.append(roof); const house=el('symbol',{id:'city-page-house',viewBox:'0 0 64 54'}); house.append(el('path',{d:'M7 25 32 8 57 25V49H7Z',class:'estate-house-wall'}),el('path',{d:'M2 26 32 3 62 26 56 28 32 12 8 28Z',class:'estate-house-roof'}),el('path',{d:'M27 49V36Q32 28 37 36V49M13 30H20V39H13ZM44 30H51V39H44Z',class:'estate-window'}),el('path',{d:'M4 49H60M9 43H24M41 43H55',class:'estate-cornice'}));defs.append(house); const hall=el('symbol',{id:'city-palazzo',viewBox:'0 0 140 105'}); hall.append(el('path',{d:'M10 34H130V96H10Z',class:'estate-palazzo-wall'}),el('path',{d:'M3 36 70 3 137 36ZM11 34H129V42H11Z',class:'estate-pediment'}),el('path',{d:'M18 44V90M43 44V90M97 44V90M122 44V90',class:'estate-column'}),el('path',{d:'M56 96V63Q70 43 84 63V96M24 55H36V72H24ZM104 55H116V72H104Z',class:'estate-window'}),el('path',{d:'M4 96H136M0 102H140M16 83H46M94 83H124',class:'estate-cornice'}),el('circle',{cx:70,cy:24,r:6,class:'estate-rosette'}));defs.append(hall); const gate=el('symbol',{id:'city-iron-gate',viewBox:'0 0 100 50'}); gate.append(el('path',{d:'M4 16H14V48H4ZM86 16H96V48H86ZM1 12H17V18H1ZM83 12H99V18H83Z',class:'estate-palazzo-wall'}),el('path',{d:'M15 47V24Q50-6 85 24V47M15 32H85M15 44H85M24 15V47M37 8V47M50 5V47M63 8V47M76 15V47M21 23Q30 13 37 23Q44 32 50 23Q57 13 64 23Q71 32 79 23',class:'estate-iron'}));defs.append(gate); const tree=el('symbol',{id:'city-cypress',viewBox:'0 0 18 44'}); tree.append(el('path',{d:'M9 3Q-4 24 5 36H13Q22 24 9 3Z',class:'estate-cypress'}),el('path',{d:'M9 32V44',class:'estate-tree-trunk'}));defs.append(tree); return defs; } function projectRoads(roads,width,height) { if(!roads.length)return []; const xs=roads.flatMap(r=>[r[0],r[2]]),ys=roads.flatMap(r=>[r[1],r[3]]); const loX=Math.min(...xs),hiX=Math.max(...xs),loY=Math.min(...ys),hiY=Math.max(...ys); const scale=Math.min((width-180)/Math.max(40,hiX-loX),(height-150)/Math.max(40,hiY-loY)); return roads.map(r=>[width/2+(r[0]-(loX+hiX)/2)*scale,height/2+(r[1]-(loY+hiY)/2)*scale,width/2+(r[2]-(loX+hiX)/2)*scale,height/2+(r[3]-(loY+hiY)/2)*scale]); } function nearestRoad(x,y,roads) { let best=null,distance=Infinity; for(const [ax,ay,bx,by] of roads){const dx=bx-ax,dy=by-ay,t=Math.max(0,Math.min(1,((x-ax)*dx+(y-ay)*dy)/(dx*dx+dy*dy||1))),px=ax+t*dx,py=ay+t*dy,d=Math.hypot(x-px,y-py);if(devent.stopPropagation());group.append(a); } function ground(group,width,height) { group.append(el('rect',{x:0,y:0,width,height,rx:16,class:'estate-ground'})); for(const x of [32,width-48])for(let y=80;y{if(event.target.closest('a'))return;event.stopPropagation();if(!focused)onSpot(spot);});return group; } function streetScene(street,roads,page,onSpot) { const available=street.spots.filter(s=>!undefinedSpot(s)||uniquePages(s).length); const batch=slice(available,page,SPOTS_PER_VIEW),space=layout(batch.items.length),root=el('g',{class:'city-estate-scene'}); root.append(definitions());ground(root,space.width,space.height); const projected=projectRoads(roads,space.width,space.height); for(const r of projected)root.append(el('path',{d:`M${r[0]} ${r[1]}L${r[2]} ${r[3]}`,class:'estate-route-edge'}),el('path',{d:`M${r[0]} ${r[1]}L${r[2]} ${r[3]}`,class:'estate-route'})); label(root,'city-generated-street-label',space.width/2,38,street.title||street.name,500,22); batch.items.forEach((spot,i)=>{ const box=space.plots[i],group=spotGroup(spot,onSpot),pages=surroundingPages(spot),isUndefined=undefinedSpot(spot),anchor=nearestRoad(box.x,box.y,projected); if(anchor)root.append(el('path',{d:`M${anchor[0]} ${anchor[1]}L${box.x} ${box.y+box.h/2}`,class:'estate-walkway'})); group.setAttribute('data-estate-box',`${box.x-box.w/2},${box.y-box.h/2},${box.w},${box.h}`); if(isUndefined)group.classList.add('city-spot-undefined','city-undefined-expanded'); courtyard(group,box.x,box.y,box.w,box.h);entry(group,spot,box.x,box.y-9,onSpot,isUndefined); const places=[[-109,-91],[109,-91],[109,91],[-109,91],[0,-110],[0,110]]; pages.slice(0,6).forEach((p,j)=>pageHouse(group,p,box.x+places[j][0],box.y+places[j][1],isUndefined)); if(pages.length>6){const count=el('g',{class:'city-estate-more',role:'button',tabindex:0,'aria-label':`${pages.length} Pages`});count.append(el('rect',{x:box.x+65,y:box.y+box.h/2+5,width:90,height:22,rx:11}),el('text',{x:box.x+110,y:box.y+box.h/2+21,'text-anchor':'middle'},`+ ${pages.length-6} Pages`));group.append(count);} use(group,'city-iron-gate',box.x-31,box.y+box.h/2-6,62,31);root.append(group); }); return {root,...space,...batch,kind:'spots'}; } function spotScene(spot,page,onSpot) { const batch=slice(surroundingPages(spot),page,PAGES_PER_VIEW),width=1120,height=780,root=el('g',{class:'city-estate-scene city-estate-detail'}); root.append(definitions());ground(root,width,height); const group=spotGroup(spot,onSpot,true);group.classList.add('city-estate-focused');courtyard(group,width/2,height/2,990,630); group.append(el('ellipse',{cx:width/2,cy:height/2+118,rx:45,ry:22,class:'estate-fountain'})); entry(group,spot,width/2,height/2-35,onSpot,undefinedSpot(spot),true); const places=[];for(let i=0;i<8;i++)places.push([175+i*110,126]);for(let i=0;i<8;i++)places.push([175+i*110,618]);for(let i=0;i<4;i++)places.push([123,228+i*96]);for(let i=0;i<4;i++)places.push([997,228+i*96]); batch.items.forEach((p,i)=>{const [x,y]=places[i];group.append(el('path',{d:`M${width/2} ${height/2+70}Q${x} ${height/2+70} ${x} ${y}`,class:'estate-detail-walkway'}));}); batch.items.forEach((p,i)=>pageHouse(group,p,...places[i],undefinedSpot(spot))); const portal=spot.portal_path||(spot.portal_page_uuid&&`/o/uuid:${spot.portal_page_uuid}`); if(portal){const text=document.documentElement.lang.startsWith('ja')?'ポータルPageを開く':'Open portal Page',link=el('a',{href:portal,class:'city-portal-link','aria-label':text});link.append(el('rect',{x:width/2-110,y:height/2+48,width:220,height:31,rx:3}));label(link,'city-portal-label',width/2,height/2+69,text,210,13);link.addEventListener('click',e=>e.stopPropagation());group.append(link);} use(group,'city-iron-gate',width/2-60,height-75,120,60);root.append(group); return {root,width,height,...batch,kind:'pages'}; } window.PagesWikiCityEstates={layout,streetScene,spotScene,definitions,uniquePages,pageURL,surroundingPages,undefinedSpot,SPOTS_PER_VIEW,PAGES_PER_VIEW}; })();