mudlet-map-renderer 0.9.2-konva → 0.10.0-konva
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Renderer.d.ts +2 -10
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +271 -380
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/Renderer.d.ts
CHANGED
|
@@ -27,15 +27,13 @@ export declare class Settings {
|
|
|
27
27
|
} | null;
|
|
28
28
|
static labelRenderMode: LabelRenderMode;
|
|
29
29
|
static transparentLabels: boolean;
|
|
30
|
-
static cullingDebug: boolean;
|
|
31
30
|
}
|
|
32
31
|
export declare class Renderer {
|
|
33
32
|
private readonly stage;
|
|
34
33
|
private readonly roomLayer;
|
|
35
34
|
private readonly linkLayer;
|
|
36
|
-
private readonly overlayLayer;
|
|
37
35
|
private readonly positionLayer;
|
|
38
|
-
private readonly
|
|
36
|
+
private readonly overlayLayer;
|
|
39
37
|
private mapReader;
|
|
40
38
|
private exitRenderer;
|
|
41
39
|
private pathRenderer;
|
|
@@ -58,9 +56,6 @@ export declare class Renderer {
|
|
|
58
56
|
private visibleStandaloneExitNodes;
|
|
59
57
|
private standaloneExitBoundsRoomSize?;
|
|
60
58
|
private cullingScheduled;
|
|
61
|
-
private cullingViewportDebug?;
|
|
62
|
-
private cullingSearchDebug?;
|
|
63
|
-
private cullingBucketDebug;
|
|
64
59
|
constructor(container: HTMLDivElement, mapReader: MapReader);
|
|
65
60
|
private onResize;
|
|
66
61
|
private initScaling;
|
|
@@ -73,14 +68,12 @@ export declare class Renderer {
|
|
|
73
68
|
private collectRoomCandidates;
|
|
74
69
|
private collectStandaloneExitCandidates;
|
|
75
70
|
private refreshStandaloneExitBoundsIfNeeded;
|
|
76
|
-
private getBucketBounds;
|
|
77
71
|
private emitRoomContextEvent;
|
|
78
72
|
private emitZoomChangeEvent;
|
|
79
73
|
setZoom(zoom: number): boolean;
|
|
80
74
|
getZoom(): number;
|
|
81
75
|
setCullingMode(mode: CullingMode): void;
|
|
82
76
|
getCullingMode(): CullingMode;
|
|
83
|
-
setCullingDebug(enabled: boolean): void;
|
|
84
77
|
getCurrentArea(): Area | undefined;
|
|
85
78
|
setPosition(roomId: number): void;
|
|
86
79
|
renderPath(locations: number[], color?: string): import('konva/lib/shapes/Line').Line<{
|
|
@@ -89,7 +82,7 @@ export declare class Renderer {
|
|
|
89
82
|
strokeWidth: number;
|
|
90
83
|
}> | undefined;
|
|
91
84
|
clearPaths(): void;
|
|
92
|
-
renderHighlight(roomId: number, color: string): import('konva/lib/
|
|
85
|
+
renderHighlight(roomId: number, color: string): import('konva/lib/Shape').Shape<import('konva/lib/Shape').ShapeConfig> | undefined;
|
|
93
86
|
clearHighlights(): void;
|
|
94
87
|
private refreshHighlights;
|
|
95
88
|
private createHighlightShape;
|
|
@@ -97,7 +90,6 @@ export declare class Renderer {
|
|
|
97
90
|
private renderRooms;
|
|
98
91
|
private scheduleRoomCulling;
|
|
99
92
|
private updateRoomCulling;
|
|
100
|
-
private updateCullingDebugVisuals;
|
|
101
93
|
private clearCurrentRoomOverlay;
|
|
102
94
|
private updateCurrentRoomOverlay;
|
|
103
95
|
private createOverlayRoomGroup;
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("konva"),K=["north","south","east","west","northeast","northwest","southeast","southwest"],O={north:"n",south:"s",east:"e",west:"w",northeast:"ne",northwest:"nw",southeast:"se",southwest:"sw",up:"u",down:"d",in:"i",out:"o"},it={north:{x:0,y:-1},south:{x:0,y:1},east:{x:1,y:0},west:{x:-1,y:0},northeast:{x:1,y:-1},northwest:{x:-1,y:-1},southeast:{x:1,y:1},southwest:{x:-1,y:1}},Q=["north","south","east","west","northeast","northwest","southeast","southwest"],dt={north:"south",south:"north",east:"west",west:"east",northeast:"southwest",northwest:"southeast",southeast:"northwest",southwest:"northeast"};function ut(g){return g?Object.prototype.hasOwnProperty.call(it,g):!1}function S(g,t,e,s=1){if(!ut(e))return{x:g,y:t};const i=it[e];return{x:g+i.x*s,y:t+i.y*s}}const A={OPEN_DOOR:"rgb(10, 155, 10)",CLOSED_DOOR:"rgb(226, 205, 59)",LOCKED_DOOR:"rgb(155, 10, 10)",ONE_WAY_FILL:"rgb(155, 10, 10)"},gt={1:"north",2:"northeast",3:"northwest",4:"east",5:"west",6:"south",7:"southeast",8:"southwest",9:"up",10:"down",11:"in",12:"out"},ft=["up","down","in","out"];function mt(g){switch(g){case 1:return A.OPEN_DOOR;case 2:return A.CLOSED_DOOR;default:return A.LOCKED_DOOR}}class pt{constructor(t,e){this.mapReader=t,this.mapRenderer=e}render(t,e){return this.renderWithColor(t,h.lineColor,e)}renderWithColor(t,e,s){return t.aDir&&t.bDir?this.renderTwoWayExit(t,e,s):this.renderOneWayExit(t,e)}renderTwoWayExit(t,e,s){const i=this.mapReader.getRoom(t.a),o=this.mapReader.getRoom(t.b);if(!i||!o||!t.aDir||!t.bDir||!K.includes(t.aDir)||!K.includes(t.bDir)||i.customLines[O[t.aDir]]&&o.customLines[O[t.bDir]]||i.z!==o.z&&(s!==o.z&&i.customLines[O[t.aDir]]||s!==i.z&&o.customLines[O[t.bDir]]))return;const n=new f.Group,r=[];if(r.push(...Object.values(S(i.x,i.y,t.aDir,h.roomSize/2))),r.push(...Object.values(S(o.x,o.y,t.bDir,h.roomSize/2))),i.doors[O[t.aDir]]||o.doors[O[t.bDir]]){const d=this.renderDoor(r,i.doors[O[t.aDir]]??o.doors[O[t.bDir]]);n.add(d)}const a=new f.Line({points:r,stroke:e,strokeWidth:.025,perfectDrawEnabled:!1});return n.add(a),n}renderOneWayExit(t,e){const s=t.aDir?this.mapReader.getRoom(t.a):this.mapReader.getRoom(t.b),i=t.aDir?this.mapReader.getRoom(t.b):this.mapReader.getRoom(t.a),o=t.aDir?t.aDir:t.bDir;if(!o||!s||!i||s.customLines[O[o]||o])return;if(s.area!=i.area&&o)return this.renderAreaExit(s,o,e);let n={x:i.x,y:i.y};(i.area!==s.area||i.z!==s.z)&&(n=S(s.x,s.y,o,h.roomSize/2));const r=S(s.x,s.y,o,.3),a=r.x-(r.x-n.x)/2,d=r.y-(r.y-n.y)/2,u=new f.Group,l=[];l.push(...Object.values(S(s.x,s.y,o,h.roomSize/2))),l.push(n.x,n.y);const y=new f.Line({points:l,stroke:e,strokeWidth:.025,dashEnabled:!0,dash:[.1,.05],perfectDrawEnabled:!1});u.add(y);const c=new f.Arrow({points:[l[0],l[1],a,d],pointerLength:.5,pointerWidth:.35,strokeWidth:.035,stroke:e,fill:A.ONE_WAY_FILL,dashEnabled:!0,dash:[.1,.05]});return u.add(c),u}renderAreaExit(t,e,s){const i=S(t.x,t.y,e,h.roomSize/2),o=S(t.x,t.y,e,h.roomSize*1.5),n=s??this.mapReader.getColorValue(t.env);return new f.Arrow({points:[i.x,i.y,o.x,o.y],pointerLength:.3,pointerWidth:.3,strokeWidth:.035,stroke:n,fill:n})}renderSpecialExits(t,e){return Object.entries(t.customLines).map(([s,i])=>{const o=[t.x,t.y];i.points.reduce((u,l)=>(u.push(l.x,-l.y),u),o);const n=i.attributes.arrow?f.Arrow:f.Line,r=e??`rgb(${i.attributes.color.r}, ${i.attributes.color.g}, ${i.attributes.color.b})`,a=new n({points:o,strokeWidth:.025,stroke:r,fill:e??`rgb(${i.attributes.color.r}, ${i.attributes.color.g} , ${i.attributes.color.b})`,pointerLength:.3,pointerWidth:.2,perfectDrawEnabled:!1});let d=i.attributes.style;return d==="dot line"?(a.dash([.05,.05]),a.dashOffset(.1)):d==="dash line"?a.dash([.4,.2]):d==="solid line"||d!==void 0&&console.log("Brak opisu stylu: "+d),a})}renderStubs(t,e=h.lineColor){return t.stubs.map(s=>{const i=gt[s],o=S(t.x,t.y,i,h.roomSize/2),n=S(t.x,t.y,i,h.roomSize/2+.5),r=[o.x,o.y,n.x,n.y];return new f.Line({points:r,stroke:e,strokeWidth:.025})})}renderInnerExits(t){return ft.map(e=>{if(t.exits[e]){const s=new f.Group,i=new f.RegularPolygon({x:t.x,y:t.y,sides:3,fill:this.mapReader.getSymbolColor(t.env,.6),stroke:this.mapReader.getSymbolColor(t.env),strokeWidth:.025,radius:h.roomSize/5,scaleX:1.4,scaleY:.8,perfectDrawEnabled:!1});s.add(i);let o=t.doors[e];if(o!==void 0)switch(o){case 1:i.stroke(A.OPEN_DOOR);break;case 2:i.stroke(A.CLOSED_DOOR);break;default:i.stroke(A.LOCKED_DOOR)}switch(e){case"up":i.position(S(t.x,t.y,"south",h.roomSize/4));break;case"down":i.rotation(180),i.position(S(t.x,t.y,"north",h.roomSize/4));break;case"in":const n=i.clone();n.rotation(-90),n.position(S(t.x,t.y,"east",h.roomSize/4)),s.add(n),i.rotation(90),i.position(S(t.x,t.y,"west",h.roomSize/4));break;case"out":const r=i.clone();r.rotation(90),r.position(S(t.x,t.y,"east",h.roomSize/4)),s.add(r),i.rotation(-90),i.position(S(t.x,t.y,"west",h.roomSize/4));break}return s}}).filter(e=>e!==void 0)}renderDoor(t,e){const s={x:t[0]+(t[2]-t[0])/2,y:t[1]+(t[3]-t[1])/2};return new f.Rect({x:s.x-h.roomSize/4,y:s.y-h.roomSize/4,width:h.roomSize/2,height:h.roomSize/2,stroke:mt(e),strokeWidth:.025})}}class ot{constructor(t,e){this.rooms=[],this.labels=[],this.rooms=t,this.bounds=this.createBounds(),this.labels=e}getRooms(){return this.rooms}getLabels(){return this.labels}getBounds(){return this.bounds}createBounds(){return this.rooms.reduce((t,e)=>({minX:Math.min(t.minX,e.x),maxX:Math.max(t.maxX,e.x),minY:Math.min(t.minY,e.y),maxY:Math.max(t.maxY,e.y)}),{minX:Number.POSITIVE_INFINITY,maxX:Number.NEGATIVE_INFINITY,minY:Number.POSITIVE_INFINITY,maxY:Number.NEGATIVE_INFINITY})}}class ${constructor(t){this.planes={},this.exits=new Map,this.version=0,this.area=t,this.planes=this.createPlanes(),this.createExits()}getAreaName(){return this.area.areaName}getAreaId(){return parseInt(this.area.areaId)}getVersion(){return this.version}markDirty(){this.version++}getPlane(t){return this.planes[t]}getPlanes(){return Object.values(this.planes)}getRooms(){return this.area.rooms}getLinkExits(t){return Array.from(this.exits.values()).filter(e=>e.zIndex.includes(t))}createPlanes(){const t=this.area.rooms.reduce((e,s)=>(e[s.z]||(e[s.z]=[]),e[s.z].push(s),e),{});return Object.entries(t).reduce((e,[s,i])=>(e[+s]=new ot(i,this.area.labels.filter(o=>o.Z===+s)),e),{})}createExits(){this.area.rooms.forEach(t=>{Object.entries(t.specialExits).forEach(([e,s])=>this.createHalfExit(t.id,s,t.z,e)),Object.entries(t.exits).forEach(([e,s])=>this.createHalfExit(t.id,s,t.z,e))})}createHalfExit(t,e,s,i){if(t===e)return;const o=Math.min(t,e),n=Math.max(t,e),r=`${o}-${n}`;let a=this.exits.get(r);a||(a={a:o,b:n,zIndex:[s]}),o==t?a.aDir=i:a.bDir=i,a.zIndex.push(s),this.exits.set(r,a)}}class U extends ot{constructor(t,e){super(t.getRooms(),t.getLabels()),this.basePlane=t,this.visitedRooms=e}getRooms(){return this.basePlane.getRooms().filter(t=>this.visitedRooms.has(t.id))}getLabels(){return this.basePlane.getLabels()}getBounds(){const t=this.getRooms();return t.length?t.reduce((e,s)=>({minX:Math.min(e.minX,s.x),maxX:Math.max(e.maxX,s.x),minY:Math.min(e.minY,s.y),maxY:Math.max(e.maxY,s.y)}),{minX:Number.POSITIVE_INFINITY,maxX:Number.NEGATIVE_INFINITY,minY:Number.POSITIVE_INFINITY,maxY:Number.NEGATIVE_INFINITY}):this.basePlane.getBounds()}}class B extends ${constructor(t,e){super(t),this.planeCache=new WeakMap,this.visitedRooms=e instanceof Set?e:new Set(e??[]),this.areaRoomIds=new Set(t.rooms.map(s=>s.id))}getPlane(t){const e=super.getPlane(t);if(!e)return e;let s=this.planeCache.get(e);return s||(s=new U(e,this.visitedRooms),this.planeCache.set(e,s)),s}getPlanes(){return super.getPlanes().map(t=>{let e=this.planeCache.get(t);return e||(e=new U(t,this.visitedRooms),this.planeCache.set(t,e)),e})}getLinkExits(t){return super.getLinkExits(t).filter(e=>this.visitedRooms.has(e.a)||this.visitedRooms.has(e.b))}getVisitedRoomCount(){return super.getRooms().reduce((t,e)=>t+(this.visitedRooms.has(e.id)?1:0),0)}getTotalRoomCount(){return this.areaRoomIds.size}hasVisitedRoom(t){return this.areaRoomIds.has(t)&&this.visitedRooms.has(t)}getVisitedRoomIds(){return super.getRooms().filter(t=>this.visitedRooms.has(t.id)).map(t=>t.id)}addVisitedRoom(t){const e=this.visitedRooms.has(t);this.visitedRooms.add(t);const s=!e&&this.areaRoomIds.has(t);return s&&this.markDirty(),s}addVisitedRooms(t){let e=0;for(const s of t){const i=this.visitedRooms.has(s);this.visitedRooms.add(s),!i&&this.areaRoomIds.has(s)&&e++}return e>0&&this.markDirty(),e}}class bt{constructor(t,e){this.paths=[],this.mapReader=t,this.overlayLayer=e}renderPath(t,e,s,i="#66E64D"){if(e===void 0||s===void 0)return;const o=t.map(l=>this.mapReader.getRoom(l)).filter(l=>l!==void 0),n=[];let r=null;const a=()=>{r&&(r.length<4&&n.pop(),r=null)},d=()=>(r||(r=[],n.push(r)),r);o.forEach((l,y)=>{if(!this.isRoomVisible(l,e,s))return;const c=y>0?o[y-1]:void 0,m=y<o.length-1?o[y+1]:void 0;if(this.isRoomVisible(c,e,s))d();else{a();const R=d();if(c){const p=this.getDirectionTowards(l,c);if(p){const D=S(l.x,l.y,p,h.roomSize);R.push(D.x,D.y)}}}if(r==null||r.push(l.x,l.y),!this.isRoomVisible(m,e,s)&&m){const R=this.getDirectionTowards(l,m);if(R){const p=S(l.x,l.y,R,h.roomSize);r==null||r.push(p.x,p.y)}a()}}),a();const u=n.filter(l=>l.length>=4).map(l=>new f.Line({points:l,stroke:i,strokeWidth:.1}));return u.forEach(l=>{this.overlayLayer.add(l),this.paths.push(l)}),u[0]}clearPaths(){this.paths.forEach(t=>{t.destroy()}),this.paths=[]}isRoomVisible(t,e,s){return t?t.area===e&&t.z===s:!1}getDirectionTowards(t,e){for(const s of Q)if(t.exits[s]===e.id)return s;for(const s of Q)if(e.exits[s]===t.id)return dt[s]}}const rt=.6,Y=75,xt="rgb(225, 255, 225)",W="rgb(120, 72, 0)",I=class I{};I.roomSize=rt,I.lineColor=xt,I.instantMapMove=!1,I.highlightCurrentRoom=!0,I.cullingEnabled=!0,I.cullingMode="indexed",I.cullingBounds=null,I.labelRenderMode="image",I.cullingDebug=!1;let h=I;class yt{constructor(t,e){this.highlights=new Map,this.currentZoom=1,this.currentRoomOverlay=[],this.roomNodes=new Map,this.standaloneExitNodes=[],this.spatialBucketSize=5,this.roomSpatialIndex=new Map,this.exitSpatialIndex=new Map,this.visibleRooms=new Set,this.visibleStandaloneExitNodes=new Set,this.cullingScheduled=!1,this.cullingBucketDebug=[],this.stage=new f.Stage({container:t,width:t.clientWidth,height:t.clientHeight,draggable:!0}),window.addEventListener("resize",()=>{this.onResize(t)}),t.addEventListener("resize",()=>{this.onResize(t)}),this.linkLayer=new f.Layer({listening:!1}),this.stage.add(this.linkLayer),this.roomLayer=new f.Layer,this.stage.add(this.roomLayer),this.overlayLayer=new f.Layer({listening:!1}),this.stage.add(this.overlayLayer),this.debugLayer=new f.Layer({listening:!1}),this.stage.add(this.debugLayer),this.positionLayer=new f.Layer({listening:!1}),this.stage.add(this.positionLayer),this.mapReader=e,this.exitRenderer=new pt(e,this),this.pathRenderer=new bt(e,this.overlayLayer),this.initScaling(1.1),this.stage.on("dragmove",()=>this.scheduleRoomCulling()),this.stage.on("dragend",()=>this.scheduleRoomCulling())}onResize(t){this.stage.width(t.clientWidth),this.stage.height(t.clientHeight),this.currentRoomId&&this.centerOnRoom(this.mapReader.getRoom(this.currentRoomId),!1),this.stage.batchDraw(),this.scheduleRoomCulling()}initScaling(t){f.hitOnDragEnabled=!0;let e,s=!1,i=!1;this.stage.on("touchstart",o=>{const n=o.evt.touches;n&&n.length>1?(i=!0,this.stage.isDragging()&&(this.stage.stopDrag(),s=!0),this.stage.draggable(!1)):(i=!1,this.stage.draggable(!0))}),this.stage.on("touchend touchcancel",o=>{e=void 0;const n=o.evt.touches;(!n||n.length<=1)&&(i=!1,this.stage.draggable(!0))}),this.stage.on("wheel",o=>{o.evt.preventDefault();const n=this.stage.scaleX(),r=this.stage.getPointerPosition();if(!r)return;const a={x:(r.x-this.stage.x())/n,y:(r.y-this.stage.y())/n};let d=o.evt.deltaY>0?-1:1;o.evt.ctrlKey&&(d=-d);const u=d>0?this.currentZoom*t:this.currentZoom/t,l=u*Y,y=this.setZoom(u),c={x:r.x-a.x*l,y:r.y-a.y*l};this.stage.position(c),this.scheduleRoomCulling(),y&&this.emitZoomChangeEvent()}),this.stage.on("touchmove",o=>{const n=o.evt.touches,r=n==null?void 0:n[0],a=n==null?void 0:n[1];if(a||i&&(i=!1,this.stage.draggable(!0)),r&&!a&&s&&!this.stage.isDragging()&&(this.stage.startDrag(),s=!1),!r||!a){e=void 0;return}o.evt.preventDefault(),this.stage.isDragging()&&(this.stage.stopDrag(),s=!0),i||(i=!0,this.stage.draggable(!1));const d=this.stage.container().getBoundingClientRect(),u={x:r.clientX-d.left,y:r.clientY-d.top},l={x:a.clientX-d.left,y:a.clientY-d.top},y=Math.hypot(u.x-l.x,u.y-l.y);if(e===void 0){e=y;return}if(e===0)return;const c=this.stage.scaleX(),m=this.stage.x(),w=this.stage.y(),b={x:this.stage.width()/2,y:this.stage.height()/2},R={x:(b.x-m)/c,y:(b.y-w)/c},p=this.currentZoom*(y/e),D=this.setZoom(p),C=this.stage.scaleX(),z={x:b.x-R.x*C,y:b.y-R.y*C};this.stage.position(z),this.stage.batchDraw(),this.scheduleRoomCulling(),e=y,D&&this.emitZoomChangeEvent()})}drawArea(t,e){const s=this.mapReader.getArea(t);if(!s)return;const i=s.getPlane(e);i&&(this.currentArea=t,this.currentAreaInstance=s,this.currentZIndex=e,this.currentAreaVersion=s.getVersion(),this.clearCurrentRoomOverlay(),this.roomLayer.destroyChildren(),this.linkLayer.destroyChildren(),this.debugLayer.destroyChildren(),this.roomNodes.clear(),this.standaloneExitNodes=[],this.standaloneExitBoundsRoomSize=void 0,this.roomSpatialIndex.clear(),this.exitSpatialIndex.clear(),this.visibleRooms.clear(),this.visibleStandaloneExitNodes.clear(),this.cullingViewportDebug=void 0,this.cullingSearchDebug=void 0,this.cullingBucketDebug=[],this.spatialBucketSize=this.computeSpatialBucketSize(),this.stage.scale({x:Y*this.currentZoom,y:Y*this.currentZoom}),this.renderLabels(i.getLabels()),this.renderExits(s.getLinkExits(e)),this.renderRooms(i.getRooms()??[]),this.refreshHighlights(),this.stage.batchDraw(),this.scheduleRoomCulling())}computeSpatialBucketSize(){return Math.max(h.roomSize*10,5)}getBucketKey(t,e){return`${t},${e}`}forEachBucket(t,e,s,i,o){const n=this.spatialBucketSize,r=Math.min(t,s),a=Math.max(t,s),d=Math.min(e,i),u=Math.max(e,i),l=Math.floor(r/n),y=Math.floor(a/n),c=Math.floor(d/n),m=Math.floor(u/n);for(let w=l;w<=y;w++)for(let b=c;b<=m;b++)o(this.getBucketKey(w,b))}addRoomToSpatialIndex(t){const e=h.roomSize/2,s=t.room.x-e,i=t.room.x+e,o=t.room.y-e,n=t.room.y+e;this.forEachBucket(s,o,i,n,r=>{let a=this.roomSpatialIndex.get(r);a||(a=new Set,this.roomSpatialIndex.set(r,a)),a.add(t)})}addStandaloneExitToSpatialIndex(t){const{bounds:e}=t,s=e.x,i=e.x+e.width,o=e.y,n=e.y+e.height;this.forEachBucket(s,o,i,n,r=>{let a=this.exitSpatialIndex.get(r);a||(a=new Set,this.exitSpatialIndex.set(r,a)),a.add(t)})}collectRoomCandidates(t,e,s,i,o){const n=new Set;return this.forEachBucket(t,e,s,i,r=>{o&&o.add(r);const a=this.roomSpatialIndex.get(r);a==null||a.forEach(d=>n.add(d))}),n}collectStandaloneExitCandidates(t,e,s,i,o){const n=new Set;return this.forEachBucket(t,e,s,i,r=>{o&&o.add(r);const a=this.exitSpatialIndex.get(r);a==null||a.forEach(d=>n.add(d))}),n}refreshStandaloneExitBoundsIfNeeded(){this.standaloneExitBoundsRoomSize!==h.roomSize&&(this.exitSpatialIndex.clear(),this.standaloneExitNodes.forEach(t=>{t.bounds=t.node.getClientRect({relativeTo:this.linkLayer}),this.addStandaloneExitToSpatialIndex(t)}),this.standaloneExitBoundsRoomSize=h.roomSize)}getBucketBounds(t){const[e,s]=t.split(","),i=Number.parseInt(e,10),o=Number.parseInt(s,10),n=this.spatialBucketSize;return{x:i*n,y:o*n,width:n,height:n}}emitRoomContextEvent(t,e,s){const i=this.stage.container(),o=i.getBoundingClientRect(),n={roomId:t,position:{x:e-o.left,y:s-o.top}},r=new CustomEvent("roomcontextmenu",{detail:n});i.dispatchEvent(r)}emitZoomChangeEvent(){const t=new CustomEvent("zoom",{detail:{zoom:this.currentZoom}});this.stage.container().dispatchEvent(t)}setZoom(t){return this.currentZoom===t?!1:(this.currentZoom=t,this.stage.scale({x:Y*t,y:Y*t}),this.scheduleRoomCulling(),!0)}getZoom(){return this.currentZoom}setCullingMode(t){h.cullingMode=t,h.cullingEnabled=t!=="none",this.scheduleRoomCulling()}getCullingMode(){return h.cullingMode}setCullingDebug(t){h.cullingDebug=t,this.scheduleRoomCulling()}getCurrentArea(){return this.currentArea?this.mapReader.getArea(this.currentArea):void 0}setPosition(t){const e=this.mapReader.getRoom(t);if(!e)return;const s=this.mapReader.getArea(e.area),i=s==null?void 0:s.getVersion();let o=this.currentArea!==e.area||this.currentZIndex!==e.z;(this.currentArea!==e.area||this.currentZIndex!==e.z||i!==void 0&&this.currentAreaVersion!==i||s!==void 0&&this.currentAreaInstance!==s)&&this.drawArea(e.area,e.z),this.positionRender||(this.positionRender=new f.Circle({x:e.x,y:e.y,radius:rt*.85,stroke:"rgb(0, 229, 178)",strokeWidth:.1,dash:[.05,.05],dashEnabled:!0}),this.positionLayer.add(this.positionRender)),this.centerOnRoom(e,o),this.updateCurrentRoomOverlay(e)}renderPath(t,e){return this.pathRenderer.renderPath(t,this.currentArea,this.currentZIndex,e)}clearPaths(){this.pathRenderer.clearPaths()}renderHighlight(t,e){const s=this.mapReader.getRoom(t);if(!s)return;const i=this.highlights.get(t);i!=null&&i.shape&&(i.shape.destroy(),delete i.shape);const o={color:e,area:s.area,z:s.z};if(this.highlights.set(t,o),s.area===this.currentArea&&s.z===this.currentZIndex){const n=this.createHighlightShape(s,e);return this.overlayLayer.add(n),o.shape=n,this.overlayLayer.batchDraw(),n}return o.shape}clearHighlights(){this.highlights.forEach(({shape:t})=>t==null?void 0:t.destroy()),this.highlights.clear(),this.overlayLayer.batchDraw()}refreshHighlights(){this.highlights.forEach((t,e)=>{var o;if((o=t.shape)==null||o.destroy(),delete t.shape,t.area!==this.currentArea||t.z!==this.currentZIndex)return;const s=this.mapReader.getRoom(e);if(!s)return;const i=this.createHighlightShape(s,t.color);this.overlayLayer.add(i),t.shape=i}),this.overlayLayer.batchDraw()}createHighlightShape(t,e){return new f.Circle({x:t.x,y:t.y,radius:h.roomSize*.9,stroke:e,strokeWidth:.15,dash:[.1,.05],dashEnabled:!0,listening:!1})}centerOnRoom(t,e=!1){var d;this.currentRoomId=t.id;const s={x:t.x,y:t.y};(d=this.positionRender)==null||d.position(t);const o=this.stage.getAbsoluteTransform().point(s),n={x:this.stage.width()/2,y:this.stage.height()/2},r=n.x-o.x,a=n.y-o.y;this.currentTransition&&(this.currentTransition.pause(),this.currentTransition.destroy(),delete this.currentTransition),e||h.instantMapMove?(this.stage.position({x:this.stage.x()+r,y:this.stage.y()+a}),this.scheduleRoomCulling()):(this.currentTransition=new f.Tween({node:this.stage,x:this.stage.x()+r,y:this.stage.y()+a,duration:.2,easing:f.Easings.EaseInOut,onUpdate:()=>this.scheduleRoomCulling(),onFinish:()=>this.scheduleRoomCulling()}),this.currentTransition.play())}renderRooms(t){t.forEach(e=>{const s=new f.Group({x:e.x-h.roomSize/2,y:e.y-h.roomSize/2}),i=new f.Rect({x:0,y:0,width:h.roomSize,height:h.roomSize,fill:this.mapReader.getColorValue(e.env),strokeWidth:.025,stroke:h.lineColor,perfectDrawEnabled:!1}),o=(c,m)=>this.emitRoomContextEvent(e.id,c,m);s.on("mouseenter",()=>{this.stage.container().style.cursor="pointer"}),s.on("mouseleave",()=>{this.stage.container().style.cursor="auto"}),s.on("contextmenu",c=>{c.evt.preventDefault();const m=c.evt;o(m.clientX,m.clientY)});let n,r,a;const d=()=>{a!==void 0&&(this.stage.draggable(a),a=void 0)},u=()=>{n!==void 0&&(window.clearTimeout(n),n=void 0),r=void 0,d()};s.on("touchstart",c=>{var w;if(u(),c.evt.touches&&c.evt.touches.length>1)return;const m=(w=c.evt.touches)==null?void 0:w[0];m&&(r={clientX:m.clientX,clientY:m.clientY},a=this.stage.draggable(),this.stage.draggable(!1),n=window.setTimeout(()=>{r&&o(r.clientX,r.clientY),u()},500))}),s.on("touchend",u),s.on("touchmove",c=>{var D;if(!r)return;const m=(D=c.evt.touches)==null?void 0:D[0];if(!m){u();return}const w=m.clientX-r.clientX,b=m.clientY-r.clientY,R=w*w+b*b,p=10;if(R>p*p){const C=a;u(),C&&this.stage.startDrag()}}),s.on("touchcancel",u),s.add(i),this.renderSymbol(e,s),this.roomLayer.add(s);const l=[];this.exitRenderer.renderSpecialExits(e).forEach(c=>{this.linkLayer.add(c),l.push(c)}),this.exitRenderer.renderStubs(e).forEach(c=>{this.linkLayer.add(c),l.push(c)}),this.exitRenderer.renderInnerExits(e).forEach(c=>{this.roomLayer.add(c)});const y={room:e,group:s,linkNodes:l};this.roomNodes.set(e.id,y),this.addRoomToSpatialIndex(y)})}scheduleRoomCulling(){this.cullingScheduled||(this.cullingScheduled=!0,window.requestAnimationFrame(()=>{this.cullingScheduled=!1,this.updateRoomCulling()}))}updateRoomCulling(){if(this.roomNodes.size===0&&this.standaloneExitNodes.length===0)return;const t=this.stage.scaleX();if(!t)return;const e=this.stage.position(),s=h.roomSize/2,i=h.cullingBounds,o=i?i.x:0,n=i?i.x+i.width:this.stage.width(),r=i?i.y:0,a=i?i.y+i.height:this.stage.height(),d=Math.min(o,n),u=Math.max(o,n),l=Math.min(r,a),y=Math.max(r,a),c=(d-e.x)/t,m=(u-e.x)/t,w=(l-e.y)/t,b=(y-e.y)/t;let R=!1,p=!1;const D=h.cullingEnabled?h.cullingMode??"indexed":"none",C=c-s,z=m+s,T=w-s,N=b+s;if(this.refreshStandaloneExitBoundsIfNeeded(),D==="none"){this.roomNodes.forEach(x=>{x.group.visible()||(x.group.visible(!0),R=!0),x.linkNodes.forEach(E=>{E.visible()||(E.visible(!0),p=!0)})}),this.standaloneExitNodes.forEach(x=>{const{node:E}=x;E.visible()||(p=!0,E.visible(!0))}),R&&this.roomLayer.batchDraw(),p&&this.linkLayer.batchDraw(),this.visibleRooms=new Set(this.roomNodes.values()),this.visibleStandaloneExitNodes=new Set(this.standaloneExitNodes),this.updateCullingDebugVisuals({mode:D,minX:c,minY:w,maxX:m,maxY:b,searchMinX:C,searchMinY:T,searchMaxX:z,searchMaxY:N});return}if(D==="basic"){const x=new Set;this.roomNodes.forEach(v=>{const V=v.room.x-s,L=v.room.x+s,M=v.room.y-s,P=v.room.y+s,k=L>=c&&V<=m&&P>=w&&M<=b;v.group.visible()!==k&&(v.group.visible(k),R=!0),v.linkNodes.forEach(X=>{X.visible()!==k&&(X.visible(k),p=!0)}),k&&x.add(v)});const E=new Set;this.standaloneExitNodes.forEach(v=>{const{node:V,bounds:L}=v,M=L.x,P=L.x+L.width,k=L.y,X=L.y+L.height,Z=P>=c&&M<=m&&X>=w&&k<=b;V.visible()!==Z&&(V.visible(Z),p=!0),Z&&E.add(v)}),this.visibleRooms=x,this.visibleStandaloneExitNodes=E,R&&this.roomLayer.batchDraw(),p&&this.linkLayer.batchDraw(),this.updateCullingDebugVisuals({mode:D,minX:c,minY:w,maxX:m,maxY:b,searchMinX:C,searchMinY:T,searchMaxX:z,searchMaxY:N});return}const j=h.cullingDebug?new Set:void 0,F=h.cullingDebug?new Set:void 0,lt=this.collectRoomCandidates(C,T,z,N,j),_=new Set,G=new Set;lt.forEach(x=>{_.add(x);const E=x.room.x-s,v=x.room.x+s,V=x.room.y-s,L=x.room.y+s,M=v>=c&&E<=m&&L>=w&&V<=b;x.group.visible()!==M&&(x.group.visible(M),R=!0),x.linkNodes.forEach(P=>{P.visible()!==M&&(P.visible(M),p=!0)}),M&&G.add(x)}),this.visibleRooms.forEach(x=>{_.has(x)||(x.group.visible()&&(x.group.visible(!1),R=!0),x.linkNodes.forEach(E=>{E.visible()&&(E.visible(!1),p=!0)}))}),this.visibleRooms=G;const ct=this.collectStandaloneExitCandidates(C,T,z,N,F),H=new Set,q=new Set;ct.forEach(x=>{H.add(x);const{node:E,bounds:v}=x,V=v.x,L=v.x+v.width,M=v.y,P=v.y+v.height,k=L>=c&&V<=m&&P>=w&&M<=b;E.visible()!==k&&(E.visible(k),p=!0),k&&q.add(x)}),this.visibleStandaloneExitNodes.forEach(x=>{const{node:E}=x;!H.has(x)&&E.visible()&&(E.visible(!1),p=!0)}),this.visibleStandaloneExitNodes=q,R&&this.roomLayer.batchDraw(),p&&this.linkLayer.batchDraw(),this.updateCullingDebugVisuals({mode:D,minX:c,minY:w,maxX:m,maxY:b,searchMinX:C,searchMinY:T,searchMaxX:z,searchMaxY:N,roomBuckets:j,exitBuckets:F})}updateCullingDebugVisuals({mode:t,minX:e,minY:s,maxX:i,maxY:o,searchMinX:n,searchMinY:r,searchMaxX:a,searchMaxY:d,roomBuckets:u,exitBuckets:l}){if(!h.cullingDebug){this.debugLayer.children.length>0&&(this.debugLayer.destroyChildren(),this.debugLayer.batchDraw()),this.cullingViewportDebug=void 0,this.cullingSearchDebug=void 0,this.cullingBucketDebug=[];return}this.debugLayer.destroyChildren();const y=Math.max(0,i-e),c=Math.max(0,o-s),m=new f.Rect({x:e,y:s,width:y,height:c,stroke:"rgba(102, 255, 204, 0.9)",strokeWidth:.1,dash:[.4,.2],listening:!1});if(this.debugLayer.add(m),this.cullingViewportDebug=m,n<e||r<s||a>i||d>o){const b=new f.Rect({x:n,y:r,width:Math.max(0,a-n),height:Math.max(0,d-r),stroke:"rgba(80, 160, 255, 0.75)",strokeWidth:.08,dash:[.3,.15],fill:"rgba(80, 160, 255, 0.15)",listening:!1});this.debugLayer.add(b),this.cullingSearchDebug=b}else this.cullingSearchDebug=void 0;if(this.cullingBucketDebug=[],t==="indexed"&&(u!=null&&u.size||l!=null&&l.size)){const b=new Map;u==null||u.forEach(R=>{const p=b.get(R);b.set(R,{rooms:!0,exits:(p==null?void 0:p.exits)??!1})}),l==null||l.forEach(R=>{const p=b.get(R);b.set(R,{rooms:(p==null?void 0:p.rooms)??!1,exits:!0})}),b.forEach(({rooms:R,exits:p},D)=>{const C=this.getBucketBounds(D),z=R&&p?"rgba(255, 196, 102, 0.18)":R?"rgba(102, 255, 204, 0.18)":"rgba(255, 196, 102, 0.12)",T=R&&p?"rgba(255, 196, 102, 0.75)":R?"rgba(102, 255, 204, 0.8)":"rgba(255, 196, 102, 0.6)",N=new f.Rect({...C,stroke:T,strokeWidth:.05,fill:z,listening:!1});this.debugLayer.add(N),this.cullingBucketDebug.push(N)})}this.debugLayer.batchDraw()}clearCurrentRoomOverlay(){this.currentRoomOverlay.forEach(t=>t.destroy()),this.currentRoomOverlay=[],this.overlayLayer.batchDraw()}updateCurrentRoomOverlay(t){if(this.clearCurrentRoomOverlay(),t.area!==this.currentArea||t.z!==this.currentZIndex){this.overlayLayer.batchDraw();return}const e=new Map;e.set(t.id,t);const s=[],i=this.currentAreaInstance instanceof B?this.currentAreaInstance:void 0;this.currentAreaInstance&&this.currentZIndex!==void 0&&this.currentAreaInstance.getLinkExits(this.currentZIndex).filter(a=>a.a===t.id||a.b===t.id).forEach(a=>{const d=h.highlightCurrentRoom?this.exitRenderer.renderWithColor(a,W,this.currentZIndex):this.exitRenderer.render(a,this.currentZIndex);d&&s.push(d)});const o=h.highlightCurrentRoom?W:void 0;this.exitRenderer.renderSpecialExits(t,o).forEach(r=>{s.push(r)}),(h.highlightCurrentRoom?this.exitRenderer.renderStubs(t,W):this.exitRenderer.renderStubs(t)).forEach(r=>{s.push(r)}),[...Object.values(t.exits),...Object.values(t.specialExits)].forEach(r=>{const a=this.mapReader.getRoom(r),d=!i||i.hasVisitedRoom(r);a&&a.area===this.currentArea&&a.z===this.currentZIndex&&d&&e.set(r,a)}),s.forEach(r=>{this.overlayLayer.add(r),this.currentRoomOverlay.push(r)}),e.forEach((r,a)=>{const d=a===t.id,u=this.createOverlayRoomGroup(r,{stroke:d&&h.highlightCurrentRoom?W:h.lineColor});this.overlayLayer.add(u),this.currentRoomOverlay.push(u),this.exitRenderer.renderInnerExits(r).forEach(l=>{this.overlayLayer.add(l),this.currentRoomOverlay.push(l)})}),this.overlayLayer.batchDraw()}createOverlayRoomGroup(t,e){const s=new f.Group({x:t.x-h.roomSize/2,y:t.y-h.roomSize/2,listening:!1}),i=new f.Rect({x:0,y:0,width:h.roomSize,height:h.roomSize,fill:this.mapReader.getColorValue(t.env),stroke:e.stroke,strokeWidth:.025,strokeEnabled:!0});return s.add(i),this.renderSymbol(t,s),s}renderSymbol(t,e){if(t.roomChar!==void 0){const s=new f.Text({x:0,y:0,text:t.roomChar,fontSize:.45,fontStyle:"bold",fill:this.mapReader.getSymbolColor(t.env),align:"center",verticalAlign:"middle",width:h.roomSize,height:h.roomSize});e.add(s)}}renderExits(t){t.forEach(e=>{const s=this.exitRenderer.render(e,this.currentZIndex);if(!s)return;this.linkLayer.add(s);const i=s.getClientRect({relativeTo:this.linkLayer}),o={node:s,bounds:i};this.standaloneExitNodes.push(o),this.addStandaloneExitToSpatialIndex(o)}),this.standaloneExitBoundsRoomSize=h.roomSize}renderLabels(t){t.forEach(e=>{if(h.labelRenderMode==="image"){if(!e.pixMap)return;const s=new Image;s.src=`data:image/png;base64,${e.pixMap}`;const i=new f.Image({x:e.X,y:-e.Y,width:e.Width,height:e.Height,image:s,listening:!1});this.linkLayer.add(i);return}this.renderLabelAsData(e)})}renderLabelAsData(t){var r;const e=new f.Group({listening:!1}),s=new f.Rect({x:t.X,y:-t.Y,width:t.Width,height:t.Height,listening:!1});(((r=t.BgColor)==null?void 0:r.alpha)??0)>0&&!h.transparentLabels?s.fill(this.getLabelColor(t.BgColor)):s.fillEnabled(!1),e.add(s);const i=Math.min(.75,t.Width/Math.max(t.Text.length/2,1)),o=Math.max(.1,Math.min(i,Math.max(t.Height*.9,.1))),n=new f.Text({x:t.X,y:-t.Y,width:t.Width,height:t.Height,text:t.Text,fontSize:o,fillEnabled:!0,fill:this.getLabelColor(t.FgColor),align:"center",verticalAlign:"middle",listening:!1});e.add(n),this.linkLayer.add(e)}getLabelColor(t){const e=((t==null?void 0:t.alpha)??255)/255,s=i=>Math.min(255,Math.max(0,i??0));return`rgba(${s(t==null?void 0:t.r)}, ${s(t==null?void 0:t.g)}, ${s(t==null?void 0:t.b)}, ${e})`}}const J={rgbValue:"rgb(114, 1, 0)",symbolColor:[225,225,225]};function tt(g){const t=g[0]/255,e=g[1]/255,s=g[2]/255,i=Math.max(t,e,s),o=Math.min(t,e,s);return(i+o)/2}class Rt{constructor(t,e){this.rooms={},this.areas={},this.areaSources={},this.explorationEnabled=!1,this.colors={},t.forEach(s=>{s.rooms.forEach(o=>{o.y=-o.y,this.rooms[o.id]=o});const i=parseInt(s.areaId);this.areas[i]=new $(s),this.areaSources[i]=s}),this.colors=e.reduce((s,i)=>({...s,[i.envId]:{rgb:i.colors,rgbValue:`rgb(${i.colors.join(",")})`,symbolColor:tt(i.colors)>.41?[25,25,25]:[225,255,255],symbolColorValue:tt(i.colors)>.41?"rgb(25,25,25)":"rgb(225,255,255)"}}),{})}getArea(t){return this.areas[t]}getExplorationArea(t){const e=this.areas[t];if(e instanceof B)return e}getAreas(){return Object.values(this.areas)}getRooms(){return Object.values(this.rooms)}getRoom(t){return this.rooms[t]}ensureVisitedRooms(){return this.visitedRooms||(this.visitedRooms=new Set),this.visitedRooms}applyExplorationDecoration(){this.visitedRooms&&Object.entries(this.areaSources).forEach(([t,e])=>{const s=parseInt(t,10);this.areas[s]=new B(e,this.visitedRooms)})}decorateWithExploration(t){return t!==void 0?this.setVisitedRooms(t):this.ensureVisitedRooms(),this.applyExplorationDecoration(),this.explorationEnabled=!0,this.visitedRooms}getVisitedRooms(){return this.visitedRooms}clearExplorationDecoration(){Object.entries(this.areaSources).forEach(([t,e])=>{const s=parseInt(t,10);this.areas[s]=new $(e)}),this.explorationEnabled=!1}isExplorationEnabled(){return this.explorationEnabled}setVisitedRooms(t){return this.visitedRooms=t instanceof Set?t:new Set(t),this.explorationEnabled&&this.applyExplorationDecoration(),this.visitedRooms}addVisitedRoom(t){if(this.explorationEnabled){const i=this.getRoom(t);if(i){const o=this.getExplorationArea(i.area);if(o)return o.addVisitedRoom(t)}}const e=this.ensureVisitedRooms(),s=e.has(t);return e.add(t),!s}addVisitedRooms(t){const e=this.ensureVisitedRooms();let s=0;for(const i of t){if(this.explorationEnabled){const n=this.getRoom(i);if(n){const r=this.getExplorationArea(n.area);if(r){r.addVisitedRoom(i)&&s++;continue}}}const o=e.has(i);e.add(i),o||s++}return s}hasVisitedRoom(t){var e;return((e=this.visitedRooms)==null?void 0:e.has(t))??!1}getColorValue(t){var e;return((e=this.colors[t])==null?void 0:e.rgbValue)??J.rgbValue}getSymbolColor(t,e){var n;const s=((n=this.colors[t])==null?void 0:n.symbolColor)??J.symbolColor,i=Math.min(Math.max(e??1,0),1),o=s.join(",");return i!=1?`rgba(${o}, ${i})`:`rgba(${o})`}}function wt(g){return g&&g.__esModule&&Object.prototype.hasOwnProperty.call(g,"default")?g.default:g}class vt{constructor(){this.keys=new Set,this.queue=[]}sort(){this.queue.sort((t,e)=>t.priority-e.priority)}set(t,e){const s=Number(e);if(isNaN(s))throw new TypeError('"priority" must be a number');return this.keys.has(t)?this.queue.map(i=>(i.key===t&&Object.assign(i,{priority:s}),i)):(this.keys.add(t),this.queue.push({key:t,priority:s})),this.sort(),this.queue.length}next(){const t=this.queue.shift();return this.keys.delete(t.key),t}isEmpty(){return this.queue.length===0}has(t){return this.keys.has(t)}get(t){return this.queue.find(e=>e.key===t)}}var Et=vt;function nt(g,t){const e=new Map;for(const[s,i]of g)s!==t&&i instanceof Map?e.set(s,nt(i,t)):s!==t&&e.set(s,i);return e}var St=nt;function Dt(g){const t=Number(g);return!(isNaN(t)||t<=0)}function at(g){const t=new Map;return Object.keys(g).forEach(s=>{const i=g[s];if(i!==null&&typeof i=="object"&&!Array.isArray(i))return t.set(s,at(i));if(!Dt(i))throw new Error(`Could not add node at key "${s}", make sure it's a valid node`,i);return t.set(s,Number(i))}),t}var Ct=at;function ht(g){if(!(g instanceof Map))throw new Error(`Invalid graph: Expected Map instead found ${typeof g}`);g.forEach((t,e)=>{if(typeof t=="object"&&t instanceof Map){ht(t);return}if(typeof t!="number"||t<=0)throw new Error(`Values must be numbers greater than 0. Found value ${t} at ${e}`)})}var Lt=ht;const Mt=Et,kt=St,et=Ct,st=Lt;class It{constructor(t){t instanceof Map?(st(t),this.graph=t):t?this.graph=et(t):this.graph=new Map}addNode(t,e){let s;return e instanceof Map?(st(e),s=e):s=et(e),this.graph.set(t,s),this}addVertex(t,e){return this.addNode(t,e)}removeNode(t){return this.graph=kt(this.graph,t),this}path(t,e,s={}){if(!this.graph.size)return s.cost?{path:null,cost:0}:null;const i=new Set,o=new Mt,n=new Map;let r=[],a=0,d=[];if(s.avoid&&(d=[].concat(s.avoid)),d.includes(t))throw new Error(`Starting node (${t}) cannot be avoided`);if(d.includes(e))throw new Error(`Ending node (${e}) cannot be avoided`);for(o.set(t,0);!o.isEmpty();){const u=o.next();if(u.key===e){a=u.priority;let y=u.key;for(;n.has(y);)r.push(y),y=n.get(y);break}i.add(u.key),(this.graph.get(u.key)||new Map).forEach((y,c)=>{if(i.has(c)||d.includes(c))return null;if(!o.has(c))return n.set(c,u.key),o.set(c,u.priority+y);const m=o.get(c).priority,w=u.priority+y;return w<m?(n.set(c,u.key),o.set(c,w)):null})}return r.length?(s.trim?r.shift():r=r.concat([t]),s.reverse||(r=r.reverse()),s.cost?{path:r,cost:a}:r):s.cost?{path:null,cost:0}:null}shortestPath(...t){return this.path(...t)}}var zt=It;const Ot=wt(zt),Nt={1:"north",2:"northeast",3:"northwest",4:"east",5:"west",6:"south",7:"southeast",8:"southwest",9:"up",10:"down",11:"in",12:"out"};class Vt{constructor(t){this.cache=new Map,this.mapReader=t,this.graph=this.buildGraph()}buildGraph(){const t={};return this.mapReader.getRooms().forEach(e=>{const s={},i=new Set((e.exitLocks??[]).map(n=>Nt[n]).filter(n=>!!n)),o=new Set(e.mSpecialExitLocks??[]);Object.entries(e.exits??{}).forEach(([n,r])=>{i.has(n)||this.mapReader.getRoom(r)&&(s[r.toString()]=1)}),Object.values(e.specialExits??{}).forEach(n=>{o.has(n)||this.mapReader.getRoom(n)&&(s[n.toString()]=1)}),t[e.id.toString()]=s}),new Ot(t)}findPath(t,e){const s=`${t}->${e}`;if(this.cache.has(s))return this.cache.get(s);if(t===e){const r=this.mapReader.getRoom(t)?[t]:null;return this.cache.set(s,r),r}if(!this.mapReader.getRoom(t)||!this.mapReader.getRoom(e))return this.cache.set(s,null),null;const i=this.graph.path(t.toString(),e.toString()),o=Array.isArray(i)?i:i==null?void 0:i.path,n=o?o.map(r=>Number(r)):null;return this.cache.set(s,n),n}}exports.ExplorationArea=B;exports.MapReader=Rt;exports.PathFinder=Vt;exports.Renderer=yt;exports.Settings=h;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const m=require("konva"),H=["north","south","east","west","northeast","northwest","southeast","southwest"],I={north:"n",south:"s",east:"e",west:"w",northeast:"ne",northwest:"nw",southeast:"se",southwest:"sw",up:"u",down:"d",in:"i",out:"o"},et={north:{x:0,y:-1},south:{x:0,y:1},east:{x:1,y:0},west:{x:-1,y:0},northeast:{x:1,y:-1},northwest:{x:-1,y:-1},southeast:{x:1,y:1},southwest:{x:-1,y:1}},q=["north","south","east","west","northeast","northwest","southeast","southwest"],ct={north:"south",south:"north",east:"west",west:"east",northeast:"southwest",northwest:"southeast",southeast:"northwest",southwest:"northeast"};function lt(u){return u?Object.prototype.hasOwnProperty.call(et,u):!1}function S(u,t,e,s=1){if(!lt(e))return{x:u,y:t};const i=et[e];return{x:u+i.x*s,y:t+i.y*s}}const V={OPEN_DOOR:"rgb(10, 155, 10)",CLOSED_DOOR:"rgb(226, 205, 59)",LOCKED_DOOR:"rgb(155, 10, 10)",ONE_WAY_FILL:"rgb(155, 10, 10)"},dt={1:"north",2:"northeast",3:"northwest",4:"east",5:"west",6:"south",7:"southeast",8:"southwest",9:"up",10:"down",11:"in",12:"out"},ut=["up","down","in","out"];function gt(u){switch(u){case 1:return V.OPEN_DOOR;case 2:return V.CLOSED_DOOR;default:return V.LOCKED_DOOR}}class mt{constructor(t,e){this.mapReader=t,this.mapRenderer=e}render(t,e){return this.renderWithColor(t,h.lineColor,e)}renderWithColor(t,e,s){return t.aDir&&t.bDir?this.renderTwoWayExit(t,e,s):this.renderOneWayExit(t,e)}renderTwoWayExit(t,e,s){const i=this.mapReader.getRoom(t.a),o=this.mapReader.getRoom(t.b);if(!i||!o||!t.aDir||!t.bDir||!H.includes(t.aDir)||!H.includes(t.bDir)||i.customLines[I[t.aDir]]&&o.customLines[I[t.bDir]]||i.z!==o.z&&(s!==o.z&&i.customLines[I[t.aDir]]||s!==i.z&&o.customLines[I[t.bDir]]))return;const n=new m.Group,r=[];if(r.push(...Object.values(S(i.x,i.y,t.aDir,h.roomSize/2))),r.push(...Object.values(S(o.x,o.y,t.bDir,h.roomSize/2))),i.doors[I[t.aDir]]||o.doors[I[t.bDir]]){const d=this.renderDoor(r,i.doors[I[t.aDir]]??o.doors[I[t.bDir]]);n.add(d)}const a=new m.Line({points:r,stroke:e,strokeWidth:.025,perfectDrawEnabled:!1});return n.add(a),n}renderOneWayExit(t,e){const s=t.aDir?this.mapReader.getRoom(t.a):this.mapReader.getRoom(t.b),i=t.aDir?this.mapReader.getRoom(t.b):this.mapReader.getRoom(t.a),o=t.aDir?t.aDir:t.bDir;if(!o||!s||!i||s.customLines[I[o]||o])return;if(s.area!=i.area&&o)return this.renderAreaExit(s,o,e);let n={x:i.x,y:i.y};(i.area!==s.area||i.z!==s.z)&&(n=S(s.x,s.y,o,h.roomSize/2));const r=S(s.x,s.y,o,.3),a=r.x-(r.x-n.x)/2,d=r.y-(r.y-n.y)/2,g=new m.Group,l=[];l.push(...Object.values(S(s.x,s.y,o,h.roomSize/2))),l.push(n.x,n.y);const x=new m.Line({points:l,stroke:e,strokeWidth:.025,dashEnabled:!0,dash:[.1,.05],perfectDrawEnabled:!1});g.add(x);const c=new m.Arrow({points:[l[0],l[1],a,d],pointerLength:.5,pointerWidth:.35,strokeWidth:.035,stroke:e,fill:V.ONE_WAY_FILL,dashEnabled:!0,dash:[.1,.05]});return g.add(c),g}renderAreaExit(t,e,s){const i=S(t.x,t.y,e,h.roomSize/2),o=S(t.x,t.y,e,h.roomSize*1.5),n=s??this.mapReader.getColorValue(t.env);return new m.Arrow({points:[i.x,i.y,o.x,o.y],pointerLength:.3,pointerWidth:.3,strokeWidth:.035,stroke:n,fill:n})}renderSpecialExits(t,e){return Object.entries(t.customLines).map(([s,i])=>{const o=[t.x,t.y];i.points.reduce((g,l)=>(g.push(l.x,-l.y),g),o);const n=i.attributes.arrow?m.Arrow:m.Line,r=e??`rgb(${i.attributes.color.r}, ${i.attributes.color.g}, ${i.attributes.color.b})`,a=new n({points:o,strokeWidth:.025,stroke:r,fill:e??`rgb(${i.attributes.color.r}, ${i.attributes.color.g} , ${i.attributes.color.b})`,pointerLength:.3,pointerWidth:.2,perfectDrawEnabled:!1});let d=i.attributes.style;return d==="dot line"?(a.dash([.05,.05]),a.dashOffset(.1)):d==="dash line"?a.dash([.4,.2]):d==="solid line"||d!==void 0&&console.log("Brak opisu stylu: "+d),a})}renderStubs(t,e=h.lineColor){return t.stubs.map(s=>{const i=dt[s],o=S(t.x,t.y,i,h.roomSize/2),n=S(t.x,t.y,i,h.roomSize/2+.5),r=[o.x,o.y,n.x,n.y];return new m.Line({points:r,stroke:e,strokeWidth:.025})})}renderInnerExits(t){return ut.map(e=>{if(t.exits[e]){const s=new m.Group,i=new m.RegularPolygon({x:t.x,y:t.y,sides:3,fill:this.mapReader.getSymbolColor(t.env,.6),stroke:this.mapReader.getSymbolColor(t.env),strokeWidth:.025,radius:h.roomSize/5,scaleX:1.4,scaleY:.8,perfectDrawEnabled:!1});s.add(i);let o=t.doors[e];if(o!==void 0)switch(o){case 1:i.stroke(V.OPEN_DOOR);break;case 2:i.stroke(V.CLOSED_DOOR);break;default:i.stroke(V.LOCKED_DOOR)}switch(e){case"up":i.position(S(t.x,t.y,"south",h.roomSize/4));break;case"down":i.rotation(180),i.position(S(t.x,t.y,"north",h.roomSize/4));break;case"in":const n=i.clone();n.rotation(-90),n.position(S(t.x,t.y,"east",h.roomSize/4)),s.add(n),i.rotation(90),i.position(S(t.x,t.y,"west",h.roomSize/4));break;case"out":const r=i.clone();r.rotation(90),r.position(S(t.x,t.y,"east",h.roomSize/4)),s.add(r),i.rotation(-90),i.position(S(t.x,t.y,"west",h.roomSize/4));break}return s}}).filter(e=>e!==void 0)}renderDoor(t,e){const s={x:t[0]+(t[2]-t[0])/2,y:t[1]+(t[3]-t[1])/2};return new m.Rect({x:s.x-h.roomSize/4,y:s.y-h.roomSize/4,width:h.roomSize/2,height:h.roomSize/2,stroke:gt(e),strokeWidth:.025})}}class st{constructor(t,e){this.rooms=[],this.labels=[],this.rooms=t,this.bounds=this.createBounds(),this.labels=e}getRooms(){return this.rooms}getLabels(){return this.labels}getBounds(){return this.bounds}createBounds(){return this.rooms.reduce((t,e)=>({minX:Math.min(t.minX,e.x),maxX:Math.max(t.maxX,e.x),minY:Math.min(t.minY,e.y),maxY:Math.max(t.maxY,e.y)}),{minX:Number.POSITIVE_INFINITY,maxX:Number.NEGATIVE_INFINITY,minY:Number.POSITIVE_INFINITY,maxY:Number.NEGATIVE_INFINITY})}}class W{constructor(t){this.planes={},this.exits=new Map,this.version=0,this.area=t,this.planes=this.createPlanes(),this.createExits()}getAreaName(){return this.area.areaName}getAreaId(){return parseInt(this.area.areaId)}getVersion(){return this.version}markDirty(){this.version++}getPlane(t){return this.planes[t]}getPlanes(){return Object.values(this.planes)}getRooms(){return this.area.rooms}getLinkExits(t){return Array.from(this.exits.values()).filter(e=>e.zIndex.includes(t))}createPlanes(){const t=this.area.rooms.reduce((e,s)=>(e[s.z]||(e[s.z]=[]),e[s.z].push(s),e),{});return Object.entries(t).reduce((e,[s,i])=>(e[+s]=new st(i,this.area.labels.filter(o=>o.Z===+s)),e),{})}createExits(){this.area.rooms.forEach(t=>{Object.entries(t.specialExits).forEach(([e,s])=>this.createHalfExit(t.id,s,t.z,e)),Object.entries(t.exits).forEach(([e,s])=>this.createHalfExit(t.id,s,t.z,e))})}createHalfExit(t,e,s,i){if(t===e)return;const o=Math.min(t,e),n=Math.max(t,e),r=`${o}-${n}`;let a=this.exits.get(r);a||(a={a:o,b:n,zIndex:[s]}),o==t?a.aDir=i:a.bDir=i,a.zIndex.push(s),this.exits.set(r,a)}}class K extends st{constructor(t,e){super(t.getRooms(),t.getLabels()),this.basePlane=t,this.visitedRooms=e}getRooms(){return this.basePlane.getRooms().filter(t=>this.visitedRooms.has(t.id))}getLabels(){return this.basePlane.getLabels()}getBounds(){const t=this.getRooms();return t.length?t.reduce((e,s)=>({minX:Math.min(e.minX,s.x),maxX:Math.max(e.maxX,s.x),minY:Math.min(e.minY,s.y),maxY:Math.max(e.maxY,s.y)}),{minX:Number.POSITIVE_INFINITY,maxX:Number.NEGATIVE_INFINITY,minY:Number.POSITIVE_INFINITY,maxY:Number.NEGATIVE_INFINITY}):this.basePlane.getBounds()}}class X extends W{constructor(t,e){super(t),this.planeCache=new WeakMap,this.visitedRooms=e instanceof Set?e:new Set(e??[]),this.areaRoomIds=new Set(t.rooms.map(s=>s.id))}getPlane(t){const e=super.getPlane(t);if(!e)return e;let s=this.planeCache.get(e);return s||(s=new K(e,this.visitedRooms),this.planeCache.set(e,s)),s}getPlanes(){return super.getPlanes().map(t=>{let e=this.planeCache.get(t);return e||(e=new K(t,this.visitedRooms),this.planeCache.set(t,e)),e})}getLinkExits(t){return super.getLinkExits(t).filter(e=>this.visitedRooms.has(e.a)||this.visitedRooms.has(e.b))}getVisitedRoomCount(){return super.getRooms().reduce((t,e)=>t+(this.visitedRooms.has(e.id)?1:0),0)}getTotalRoomCount(){return this.areaRoomIds.size}hasVisitedRoom(t){return this.areaRoomIds.has(t)&&this.visitedRooms.has(t)}getVisitedRoomIds(){return super.getRooms().filter(t=>this.visitedRooms.has(t.id)).map(t=>t.id)}addVisitedRoom(t){const e=this.visitedRooms.has(t);this.visitedRooms.add(t);const s=!e&&this.areaRoomIds.has(t);return s&&this.markDirty(),s}addVisitedRooms(t){let e=0;for(const s of t){const i=this.visitedRooms.has(s);this.visitedRooms.add(s),!i&&this.areaRoomIds.has(s)&&e++}return e>0&&this.markDirty(),e}}class ft{constructor(t,e){this.paths=[],this.mapReader=t,this.overlayLayer=e}renderPath(t,e,s,i="#66E64D"){if(e===void 0||s===void 0)return;const o=t.map(l=>this.mapReader.getRoom(l)).filter(l=>l!==void 0),n=[];let r=null;const a=()=>{r&&(r.length<4&&n.pop(),r=null)},d=()=>(r||(r=[],n.push(r)),r);o.forEach((l,x)=>{if(!this.isRoomVisible(l,e,s))return;const c=x>0?o[x-1]:void 0,f=x<o.length-1?o[x+1]:void 0;if(this.isRoomVisible(c,e,s))d();else{a();const E=d();if(c){const y=this.getDirectionTowards(l,c);if(y){const C=S(l.x,l.y,y,h.roomSize);E.push(C.x,C.y)}}}if(r==null||r.push(l.x,l.y),!this.isRoomVisible(f,e,s)&&f){const E=this.getDirectionTowards(l,f);if(E){const y=S(l.x,l.y,E,h.roomSize);r==null||r.push(y.x,y.y)}a()}}),a();const g=n.filter(l=>l.length>=4).map(l=>new m.Line({points:l,stroke:i,strokeWidth:.1}));return g.forEach(l=>{this.overlayLayer.add(l),this.paths.push(l)}),g[0]}clearPaths(){this.paths.forEach(t=>{t.destroy()}),this.paths=[]}isRoomVisible(t,e,s){return t?t.area===e&&t.z===s:!1}getDirectionTowards(t,e){for(const s of q)if(t.exits[s]===e.id)return s;for(const s of q)if(e.exits[s]===t.id)return ct[s]}}const it=.6,P=75,pt="rgb(225, 255, 225)",A="rgb(120, 72, 0)",L=class L{};L.roomSize=it,L.lineColor=pt,L.instantMapMove=!1,L.highlightCurrentRoom=!0,L.cullingEnabled=!0,L.cullingMode="indexed",L.cullingBounds=null,L.labelRenderMode="image";let h=L;class xt{constructor(t,e){this.highlights=new Map,this.currentZoom=1,this.currentRoomOverlay=[],this.roomNodes=new Map,this.standaloneExitNodes=[],this.spatialBucketSize=5,this.roomSpatialIndex=new Map,this.exitSpatialIndex=new Map,this.visibleRooms=new Set,this.visibleStandaloneExitNodes=new Set,this.cullingScheduled=!1,this.stage=new m.Stage({container:t,width:t.clientWidth,height:t.clientHeight,draggable:!0}),window.addEventListener("resize",()=>{this.onResize(t)}),t.addEventListener("resize",()=>{this.onResize(t)}),this.linkLayer=new m.Layer({listening:!1}),this.stage.add(this.linkLayer),this.roomLayer=new m.Layer,this.stage.add(this.roomLayer),this.positionLayer=new m.Layer({listening:!1}),this.stage.add(this.positionLayer),this.overlayLayer=new m.Layer({listening:!1}),this.stage.add(this.overlayLayer),this.mapReader=e,this.exitRenderer=new mt(e,this),this.pathRenderer=new ft(e,this.overlayLayer),this.initScaling(1.1),this.stage.on("dragmove",()=>this.scheduleRoomCulling()),this.stage.on("dragend",()=>this.scheduleRoomCulling())}onResize(t){this.stage.width(t.clientWidth),this.stage.height(t.clientHeight),this.currentRoomId&&this.centerOnRoom(this.mapReader.getRoom(this.currentRoomId),!1),this.stage.batchDraw(),this.scheduleRoomCulling()}initScaling(t){m.hitOnDragEnabled=!0;let e,s=!1,i=!1;this.stage.on("touchstart",o=>{const n=o.evt.touches;n&&n.length>1?(i=!0,this.stage.isDragging()&&(this.stage.stopDrag(),s=!0),this.stage.draggable(!1)):(i=!1,this.stage.draggable(!0))}),this.stage.on("touchend touchcancel",o=>{e=void 0;const n=o.evt.touches;(!n||n.length<=1)&&(i=!1,this.stage.draggable(!0))}),this.stage.on("wheel",o=>{o.evt.preventDefault();const n=this.stage.scaleX(),r=this.stage.getPointerPosition();if(!r)return;const a={x:(r.x-this.stage.x())/n,y:(r.y-this.stage.y())/n};let d=o.evt.deltaY>0?-1:1;o.evt.ctrlKey&&(d=-d);const g=d>0?this.currentZoom*t:this.currentZoom/t,l=g*P,x=this.setZoom(g),c={x:r.x-a.x*l,y:r.y-a.y*l};this.stage.position(c),this.scheduleRoomCulling(),x&&this.emitZoomChangeEvent()}),this.stage.on("touchmove",o=>{const n=o.evt.touches,r=n==null?void 0:n[0],a=n==null?void 0:n[1];if(a||i&&(i=!1,this.stage.draggable(!0)),r&&!a&&s&&!this.stage.isDragging()&&(this.stage.startDrag(),s=!1),!r||!a){e=void 0;return}o.evt.preventDefault(),this.stage.isDragging()&&(this.stage.stopDrag(),s=!0),i||(i=!0,this.stage.draggable(!1));const d=this.stage.container().getBoundingClientRect(),g={x:r.clientX-d.left,y:r.clientY-d.top},l={x:a.clientX-d.left,y:a.clientY-d.top},x=Math.hypot(g.x-l.x,g.y-l.y);if(e===void 0){e=x;return}if(e===0)return;const c=this.stage.scaleX(),f=this.stage.x(),b=this.stage.y(),v={x:this.stage.width()/2,y:this.stage.height()/2},E={x:(v.x-f)/c,y:(v.y-b)/c},y=this.currentZoom*(x/e),C=this.setZoom(y),N=this.stage.scaleX(),Y={x:v.x-E.x*N,y:v.y-E.y*N};this.stage.position(Y),this.stage.batchDraw(),this.scheduleRoomCulling(),e=x,C&&this.emitZoomChangeEvent()})}drawArea(t,e){const s=this.mapReader.getArea(t);if(!s)return;const i=s.getPlane(e);i&&(this.currentArea=t,this.currentAreaInstance=s,this.currentZIndex=e,this.currentAreaVersion=s.getVersion(),this.clearCurrentRoomOverlay(),this.roomLayer.destroyChildren(),this.linkLayer.destroyChildren(),this.roomNodes.clear(),this.standaloneExitNodes=[],this.standaloneExitBoundsRoomSize=void 0,this.roomSpatialIndex.clear(),this.exitSpatialIndex.clear(),this.visibleRooms.clear(),this.visibleStandaloneExitNodes.clear(),this.spatialBucketSize=this.computeSpatialBucketSize(),this.stage.scale({x:P*this.currentZoom,y:P*this.currentZoom}),this.renderLabels(i.getLabels()),this.renderExits(s.getLinkExits(e)),this.renderRooms(i.getRooms()??[]),this.refreshHighlights(),this.stage.batchDraw(),this.scheduleRoomCulling())}computeSpatialBucketSize(){return Math.max(h.roomSize*10,5)}getBucketKey(t,e){return`${t},${e}`}forEachBucket(t,e,s,i,o){const n=this.spatialBucketSize,r=Math.min(t,s),a=Math.max(t,s),d=Math.min(e,i),g=Math.max(e,i),l=Math.floor(r/n),x=Math.floor(a/n),c=Math.floor(d/n),f=Math.floor(g/n);for(let b=l;b<=x;b++)for(let v=c;v<=f;v++)o(this.getBucketKey(b,v))}addRoomToSpatialIndex(t){const e=h.roomSize/2,s=t.room.x-e,i=t.room.x+e,o=t.room.y-e,n=t.room.y+e;this.forEachBucket(s,o,i,n,r=>{let a=this.roomSpatialIndex.get(r);a||(a=new Set,this.roomSpatialIndex.set(r,a)),a.add(t)})}addStandaloneExitToSpatialIndex(t){const{bounds:e}=t,s=e.x,i=e.x+e.width,o=e.y,n=e.y+e.height;this.forEachBucket(s,o,i,n,r=>{let a=this.exitSpatialIndex.get(r);a||(a=new Set,this.exitSpatialIndex.set(r,a)),a.add(t)})}collectRoomCandidates(t,e,s,i){const o=new Set;return this.forEachBucket(t,e,s,i,n=>{const r=this.roomSpatialIndex.get(n);r==null||r.forEach(a=>o.add(a))}),o}collectStandaloneExitCandidates(t,e,s,i){const o=new Set;return this.forEachBucket(t,e,s,i,n=>{const r=this.exitSpatialIndex.get(n);r==null||r.forEach(a=>o.add(a))}),o}refreshStandaloneExitBoundsIfNeeded(){this.standaloneExitBoundsRoomSize!==h.roomSize&&(this.exitSpatialIndex.clear(),this.standaloneExitNodes.forEach(t=>{t.bounds=t.node.getClientRect({relativeTo:this.linkLayer}),this.addStandaloneExitToSpatialIndex(t)}),this.standaloneExitBoundsRoomSize=h.roomSize)}emitRoomContextEvent(t,e,s){const i=this.stage.container(),o=i.getBoundingClientRect(),n={roomId:t,position:{x:e-o.left,y:s-o.top}},r=new CustomEvent("roomcontextmenu",{detail:n});i.dispatchEvent(r)}emitZoomChangeEvent(){const t=new CustomEvent("zoom",{detail:{zoom:this.currentZoom}});this.stage.container().dispatchEvent(t)}setZoom(t){return this.currentZoom===t?!1:(this.currentZoom=t,this.stage.scale({x:P*t,y:P*t}),this.scheduleRoomCulling(),!0)}getZoom(){return this.currentZoom}setCullingMode(t){h.cullingMode=t,h.cullingEnabled=t!=="none",this.scheduleRoomCulling()}getCullingMode(){return h.cullingMode}getCurrentArea(){return this.currentArea?this.mapReader.getArea(this.currentArea):void 0}setPosition(t){const e=this.mapReader.getRoom(t);if(!e)return;const s=this.mapReader.getArea(e.area),i=s==null?void 0:s.getVersion();let o=this.currentArea!==e.area||this.currentZIndex!==e.z;(this.currentArea!==e.area||this.currentZIndex!==e.z||i!==void 0&&this.currentAreaVersion!==i||s!==void 0&&this.currentAreaInstance!==s)&&this.drawArea(e.area,e.z),this.positionRender||(this.positionRender=new m.Circle({x:e.x,y:e.y,radius:it*.85,stroke:"rgb(0, 229, 178)",strokeWidth:.1,dash:[.05,.05],dashEnabled:!0}),this.positionLayer.add(this.positionRender)),this.centerOnRoom(e,o),this.updateCurrentRoomOverlay(e)}renderPath(t,e){return this.pathRenderer.renderPath(t,this.currentArea,this.currentZIndex,e)}clearPaths(){this.pathRenderer.clearPaths()}renderHighlight(t,e){const s=this.mapReader.getRoom(t);if(!s)return;const i=this.highlights.get(t);i!=null&&i.shape&&(i.shape.destroy(),delete i.shape);const o={color:e,area:s.area,z:s.z};if(this.highlights.set(t,o),s.area===this.currentArea&&s.z===this.currentZIndex){const n=this.createHighlightShape(s,e);return this.overlayLayer.add(n),o.shape=n,this.overlayLayer.batchDraw(),n}return o.shape}clearHighlights(){this.highlights.forEach(({shape:t})=>t==null?void 0:t.destroy()),this.highlights.clear(),this.overlayLayer.batchDraw()}refreshHighlights(){this.highlights.forEach((t,e)=>{var o;if((o=t.shape)==null||o.destroy(),delete t.shape,t.area!==this.currentArea||t.z!==this.currentZIndex)return;const s=this.mapReader.getRoom(e);if(!s)return;const i=this.createHighlightShape(s,t.color);this.overlayLayer.add(i),t.shape=i}),this.overlayLayer.batchDraw()}createHighlightShape(t,e){return new m.Rect({x:t.x-h.roomSize/2*1.5,y:t.y-h.roomSize/2*1.5,width:h.roomSize*1.5,height:h.roomSize*1.5,stroke:e,strokeWidth:.1,dash:[.05,.05],dashEnabled:!0,listening:!1})}centerOnRoom(t,e=!1){var d;this.currentRoomId=t.id;const s={x:t.x,y:t.y};(d=this.positionRender)==null||d.position(t);const o=this.stage.getAbsoluteTransform().point(s),n={x:this.stage.width()/2,y:this.stage.height()/2},r=n.x-o.x,a=n.y-o.y;this.currentTransition&&(this.currentTransition.pause(),this.currentTransition.destroy(),delete this.currentTransition),e||h.instantMapMove?(this.stage.position({x:this.stage.x()+r,y:this.stage.y()+a}),this.scheduleRoomCulling()):(this.currentTransition=new m.Tween({node:this.stage,x:this.stage.x()+r,y:this.stage.y()+a,duration:.2,easing:m.Easings.EaseInOut,onUpdate:()=>this.scheduleRoomCulling(),onFinish:()=>this.scheduleRoomCulling()}),this.currentTransition.play())}renderRooms(t){t.forEach(e=>{const s=new m.Group({x:e.x-h.roomSize/2,y:e.y-h.roomSize/2}),i=new m.Rect({x:0,y:0,width:h.roomSize,height:h.roomSize,fill:this.mapReader.getColorValue(e.env),strokeWidth:.025,stroke:h.lineColor,perfectDrawEnabled:!1}),o=(c,f)=>this.emitRoomContextEvent(e.id,c,f);s.on("mouseenter",()=>{this.stage.container().style.cursor="pointer"}),s.on("mouseleave",()=>{this.stage.container().style.cursor="auto"}),s.on("contextmenu",c=>{c.evt.preventDefault();const f=c.evt;o(f.clientX,f.clientY)});let n,r,a;const d=()=>{a!==void 0&&(this.stage.draggable(a),a=void 0)},g=()=>{n!==void 0&&(window.clearTimeout(n),n=void 0),r=void 0,d()};s.on("touchstart",c=>{var b;if(g(),c.evt.touches&&c.evt.touches.length>1)return;const f=(b=c.evt.touches)==null?void 0:b[0];f&&(r={clientX:f.clientX,clientY:f.clientY},a=this.stage.draggable(),this.stage.draggable(!1),n=window.setTimeout(()=>{r&&o(r.clientX,r.clientY),g()},500))}),s.on("touchend",g),s.on("touchmove",c=>{var C;if(!r)return;const f=(C=c.evt.touches)==null?void 0:C[0];if(!f){g();return}const b=f.clientX-r.clientX,v=f.clientY-r.clientY,E=b*b+v*v,y=10;if(E>y*y){const N=a;g(),N&&this.stage.startDrag()}}),s.on("touchcancel",g),s.add(i),this.renderSymbol(e,s),this.roomLayer.add(s);const l=[];this.exitRenderer.renderSpecialExits(e).forEach(c=>{this.linkLayer.add(c);const f=c.getClientRect({relativeTo:this.linkLayer}),b={node:c,bounds:f};this.standaloneExitNodes.push(b),this.addStandaloneExitToSpatialIndex(b)}),this.exitRenderer.renderStubs(e).forEach(c=>{this.linkLayer.add(c),l.push(c)}),this.exitRenderer.renderInnerExits(e).forEach(c=>{this.roomLayer.add(c)});const x={room:e,group:s,linkNodes:l};this.roomNodes.set(e.id,x),this.addRoomToSpatialIndex(x)})}scheduleRoomCulling(){this.cullingScheduled||(this.cullingScheduled=!0,window.requestAnimationFrame(()=>{this.cullingScheduled=!1,this.updateRoomCulling()}))}updateRoomCulling(){if(this.roomNodes.size===0&&this.standaloneExitNodes.length===0)return;const t=this.stage.scaleX();if(!t)return;const e=this.stage.position(),s=h.roomSize/2,i=h.cullingBounds,o=i?i.x:0,n=i?i.x+i.width:this.stage.width(),r=i?i.y:0,a=i?i.y+i.height:this.stage.height(),d=Math.min(o,n),g=Math.max(o,n),l=Math.min(r,a),x=Math.max(r,a),c=(d-e.x)/t,f=(g-e.x)/t,b=(l-e.y)/t,v=(x-e.y)/t;let E=!1,y=!1;const C=h.cullingEnabled?h.cullingMode??"indexed":"none",N=c-s,Y=f+s,Z=b-s,$=v+s;if(this.refreshStandaloneExitBoundsIfNeeded(),C==="none"){this.roomNodes.forEach(p=>{p.group.visible()||(p.group.visible(!0),E=!0),p.linkNodes.forEach(w=>{w.visible()||(w.visible(!0),y=!0)})}),this.standaloneExitNodes.forEach(p=>{const{node:w}=p;w.visible()||(y=!0,w.visible(!0))}),E&&this.roomLayer.batchDraw(),y&&this.linkLayer.batchDraw(),this.visibleRooms=new Set(this.roomNodes.values()),this.visibleStandaloneExitNodes=new Set(this.standaloneExitNodes);return}if(C==="basic"){const p=new Set;this.roomNodes.forEach(R=>{const z=R.room.x-s,D=R.room.x+s,M=R.room.y-s,O=R.room.y+s,k=D>=c&&z<=f&&O>=b&&M<=v;R.group.visible()!==k&&(R.group.visible(k),E=!0),R.linkNodes.forEach(T=>{T.visible()!==k&&(T.visible(k),y=!0)}),k&&p.add(R)});const w=new Set;this.standaloneExitNodes.forEach(R=>{const{node:z,bounds:D}=R,M=D.x,O=D.x+D.width,k=D.y,T=D.y+D.height,B=O>=c&&M<=f&&T>=b&&k<=v;z.visible()!==B&&(z.visible(B),y=!0),B&&w.add(R)}),this.visibleRooms=p,this.visibleStandaloneExitNodes=w,E&&this.roomLayer.batchDraw(),y&&this.linkLayer.batchDraw();return}const at=this.collectRoomCandidates(N,Z,Y,$),j=new Set,F=new Set;at.forEach(p=>{j.add(p);const w=p.room.x-s,R=p.room.x+s,z=p.room.y-s,D=p.room.y+s,M=R>=c&&w<=f&&D>=b&&z<=v;p.group.visible()!==M&&(p.group.visible(M),E=!0),p.linkNodes.forEach(O=>{O.visible()!==M&&(O.visible(M),y=!0)}),M&&F.add(p)}),this.visibleRooms.forEach(p=>{j.has(p)||(p.group.visible()&&(p.group.visible(!1),E=!0),p.linkNodes.forEach(w=>{w.visible()&&(w.visible(!1),y=!0)}))}),this.visibleRooms=F;const ht=this.collectStandaloneExitCandidates(N,Z,Y,$),_=new Set,G=new Set;ht.forEach(p=>{_.add(p);const{node:w,bounds:R}=p,z=R.x,D=R.x+R.width,M=R.y,O=R.y+R.height,k=D>=c&&z<=f&&O>=b&&M<=v;w.visible()!==k&&(w.visible(k),y=!0),k&&G.add(p)}),this.visibleStandaloneExitNodes.forEach(p=>{const{node:w}=p;!_.has(p)&&w.visible()&&(w.visible(!1),y=!0)}),this.visibleStandaloneExitNodes=G,E&&this.roomLayer.batchDraw(),y&&this.linkLayer.batchDraw()}clearCurrentRoomOverlay(){this.currentRoomOverlay.forEach(t=>t.destroy()),this.currentRoomOverlay=[],this.positionLayer.batchDraw()}updateCurrentRoomOverlay(t){if(this.clearCurrentRoomOverlay(),t.area!==this.currentArea||t.z!==this.currentZIndex){this.positionLayer.batchDraw();return}const e=new Map;e.set(t.id,t);const s=[],i=this.currentAreaInstance instanceof X?this.currentAreaInstance:void 0;this.currentAreaInstance&&this.currentZIndex!==void 0&&this.currentAreaInstance.getLinkExits(this.currentZIndex).filter(a=>a.a===t.id||a.b===t.id).forEach(a=>{const d=h.highlightCurrentRoom?this.exitRenderer.renderWithColor(a,A,this.currentZIndex):this.exitRenderer.render(a,this.currentZIndex);d&&s.push(d)});const o=h.highlightCurrentRoom?A:void 0;this.exitRenderer.renderSpecialExits(t,o).forEach(r=>{s.push(r)}),(h.highlightCurrentRoom?this.exitRenderer.renderStubs(t,A):this.exitRenderer.renderStubs(t)).forEach(r=>{s.push(r)}),[...Object.values(t.exits),...Object.values(t.specialExits)].forEach(r=>{const a=this.mapReader.getRoom(r),d=!i||i.hasVisitedRoom(r);a&&a.area===this.currentArea&&a.z===this.currentZIndex&&d&&e.set(r,a)}),s.forEach(r=>{this.positionLayer.add(r),this.currentRoomOverlay.push(r)}),e.forEach((r,a)=>{const d=a===t.id,g=this.createOverlayRoomGroup(r,{stroke:d&&h.highlightCurrentRoom?A:h.lineColor});this.positionLayer.add(g),this.currentRoomOverlay.push(g),this.exitRenderer.renderInnerExits(r).forEach(l=>{this.positionLayer.add(l),this.currentRoomOverlay.push(l)})}),this.positionLayer.batchDraw()}createOverlayRoomGroup(t,e){const s=new m.Group({x:t.x-h.roomSize/2,y:t.y-h.roomSize/2,listening:!1}),i=new m.Rect({x:0,y:0,width:h.roomSize,height:h.roomSize,fill:this.mapReader.getColorValue(t.env),stroke:e.stroke,strokeWidth:.025,strokeEnabled:!0});return s.add(i),this.renderSymbol(t,s),s}renderSymbol(t,e){if(t.roomChar!==void 0){const s=new m.Text({x:0,y:0,text:t.roomChar,fontSize:.45,fontStyle:"bold",fill:this.mapReader.getSymbolColor(t.env),align:"center",verticalAlign:"middle",width:h.roomSize,height:h.roomSize});e.add(s)}}renderExits(t){t.forEach(e=>{const s=this.exitRenderer.render(e,this.currentZIndex);if(!s)return;this.linkLayer.add(s);const i=s.getClientRect({relativeTo:this.linkLayer}),o={node:s,bounds:i};this.standaloneExitNodes.push(o),this.addStandaloneExitToSpatialIndex(o)}),this.standaloneExitBoundsRoomSize=h.roomSize}renderLabels(t){t.forEach(e=>{if(h.labelRenderMode==="image"){if(!e.pixMap)return;const s=new Image;s.src=`data:image/png;base64,${e.pixMap}`;const i=new m.Image({x:e.X,y:-e.Y,width:e.Width,height:e.Height,image:s,listening:!1});this.linkLayer.add(i);return}this.renderLabelAsData(e)})}renderLabelAsData(t){var r;const e=new m.Group({listening:!1}),s=new m.Rect({x:t.X,y:-t.Y,width:t.Width,height:t.Height,listening:!1});(((r=t.BgColor)==null?void 0:r.alpha)??0)>0&&!h.transparentLabels?s.fill(this.getLabelColor(t.BgColor)):s.fillEnabled(!1),e.add(s);const i=Math.min(.75,t.Width/Math.max(t.Text.length/2,1)),o=Math.max(.1,Math.min(i,Math.max(t.Height*.9,.1))),n=new m.Text({x:t.X,y:-t.Y,width:t.Width,height:t.Height,text:t.Text,fontSize:o,fillEnabled:!0,fill:this.getLabelColor(t.FgColor),align:"center",verticalAlign:"middle",listening:!1});e.add(n),this.linkLayer.add(e)}getLabelColor(t){const e=((t==null?void 0:t.alpha)??255)/255,s=i=>Math.min(255,Math.max(0,i??0));return`rgba(${s(t==null?void 0:t.r)}, ${s(t==null?void 0:t.g)}, ${s(t==null?void 0:t.b)}, ${e})`}}const Q={rgbValue:"rgb(114, 1, 0)",symbolColor:[225,225,225]};function U(u){const t=u[0]/255,e=u[1]/255,s=u[2]/255,i=Math.max(t,e,s),o=Math.min(t,e,s);return(i+o)/2}class bt{constructor(t,e){this.rooms={},this.areas={},this.areaSources={},this.explorationEnabled=!1,this.colors={},t.forEach(s=>{s.rooms.forEach(o=>{o.y=-o.y,this.rooms[o.id]=o});const i=parseInt(s.areaId);this.areas[i]=new W(s),this.areaSources[i]=s}),this.colors=e.reduce((s,i)=>({...s,[i.envId]:{rgb:i.colors,rgbValue:`rgb(${i.colors.join(",")})`,symbolColor:U(i.colors)>.41?[25,25,25]:[225,255,255],symbolColorValue:U(i.colors)>.41?"rgb(25,25,25)":"rgb(225,255,255)"}}),{})}getArea(t){return this.areas[t]}getExplorationArea(t){const e=this.areas[t];if(e instanceof X)return e}getAreas(){return Object.values(this.areas)}getRooms(){return Object.values(this.rooms)}getRoom(t){return this.rooms[t]}ensureVisitedRooms(){return this.visitedRooms||(this.visitedRooms=new Set),this.visitedRooms}applyExplorationDecoration(){this.visitedRooms&&Object.entries(this.areaSources).forEach(([t,e])=>{const s=parseInt(t,10);this.areas[s]=new X(e,this.visitedRooms)})}decorateWithExploration(t){return t!==void 0?this.setVisitedRooms(t):this.ensureVisitedRooms(),this.applyExplorationDecoration(),this.explorationEnabled=!0,this.visitedRooms}getVisitedRooms(){return this.visitedRooms}clearExplorationDecoration(){Object.entries(this.areaSources).forEach(([t,e])=>{const s=parseInt(t,10);this.areas[s]=new W(e)}),this.explorationEnabled=!1}isExplorationEnabled(){return this.explorationEnabled}setVisitedRooms(t){return this.visitedRooms=t instanceof Set?t:new Set(t),this.explorationEnabled&&this.applyExplorationDecoration(),this.visitedRooms}addVisitedRoom(t){if(this.explorationEnabled){const i=this.getRoom(t);if(i){const o=this.getExplorationArea(i.area);if(o)return o.addVisitedRoom(t)}}const e=this.ensureVisitedRooms(),s=e.has(t);return e.add(t),!s}addVisitedRooms(t){const e=this.ensureVisitedRooms();let s=0;for(const i of t){if(this.explorationEnabled){const n=this.getRoom(i);if(n){const r=this.getExplorationArea(n.area);if(r){r.addVisitedRoom(i)&&s++;continue}}}const o=e.has(i);e.add(i),o||s++}return s}hasVisitedRoom(t){var e;return((e=this.visitedRooms)==null?void 0:e.has(t))??!1}getColorValue(t){var e;return((e=this.colors[t])==null?void 0:e.rgbValue)??Q.rgbValue}getSymbolColor(t,e){var n;const s=((n=this.colors[t])==null?void 0:n.symbolColor)??Q.symbolColor,i=Math.min(Math.max(e??1,0),1),o=s.join(",");return i!=1?`rgba(${o}, ${i})`:`rgba(${o})`}}function yt(u){return u&&u.__esModule&&Object.prototype.hasOwnProperty.call(u,"default")?u.default:u}class Rt{constructor(){this.keys=new Set,this.queue=[]}sort(){this.queue.sort((t,e)=>t.priority-e.priority)}set(t,e){const s=Number(e);if(isNaN(s))throw new TypeError('"priority" must be a number');return this.keys.has(t)?this.queue.map(i=>(i.key===t&&Object.assign(i,{priority:s}),i)):(this.keys.add(t),this.queue.push({key:t,priority:s})),this.sort(),this.queue.length}next(){const t=this.queue.shift();return this.keys.delete(t.key),t}isEmpty(){return this.queue.length===0}has(t){return this.keys.has(t)}get(t){return this.queue.find(e=>e.key===t)}}var wt=Rt;function ot(u,t){const e=new Map;for(const[s,i]of u)s!==t&&i instanceof Map?e.set(s,ot(i,t)):s!==t&&e.set(s,i);return e}var vt=ot;function Et(u){const t=Number(u);return!(isNaN(t)||t<=0)}function rt(u){const t=new Map;return Object.keys(u).forEach(s=>{const i=u[s];if(i!==null&&typeof i=="object"&&!Array.isArray(i))return t.set(s,rt(i));if(!Et(i))throw new Error(`Could not add node at key "${s}", make sure it's a valid node`,i);return t.set(s,Number(i))}),t}var St=rt;function nt(u){if(!(u instanceof Map))throw new Error(`Invalid graph: Expected Map instead found ${typeof u}`);u.forEach((t,e)=>{if(typeof t=="object"&&t instanceof Map){nt(t);return}if(typeof t!="number"||t<=0)throw new Error(`Values must be numbers greater than 0. Found value ${t} at ${e}`)})}var Dt=nt;const Mt=wt,kt=vt,J=St,tt=Dt;class Ct{constructor(t){t instanceof Map?(tt(t),this.graph=t):t?this.graph=J(t):this.graph=new Map}addNode(t,e){let s;return e instanceof Map?(tt(e),s=e):s=J(e),this.graph.set(t,s),this}addVertex(t,e){return this.addNode(t,e)}removeNode(t){return this.graph=kt(this.graph,t),this}path(t,e,s={}){if(!this.graph.size)return s.cost?{path:null,cost:0}:null;const i=new Set,o=new Mt,n=new Map;let r=[],a=0,d=[];if(s.avoid&&(d=[].concat(s.avoid)),d.includes(t))throw new Error(`Starting node (${t}) cannot be avoided`);if(d.includes(e))throw new Error(`Ending node (${e}) cannot be avoided`);for(o.set(t,0);!o.isEmpty();){const g=o.next();if(g.key===e){a=g.priority;let x=g.key;for(;n.has(x);)r.push(x),x=n.get(x);break}i.add(g.key),(this.graph.get(g.key)||new Map).forEach((x,c)=>{if(i.has(c)||d.includes(c))return null;if(!o.has(c))return n.set(c,g.key),o.set(c,g.priority+x);const f=o.get(c).priority,b=g.priority+x;return b<f?(n.set(c,g.key),o.set(c,b)):null})}return r.length?(s.trim?r.shift():r=r.concat([t]),s.reverse||(r=r.reverse()),s.cost?{path:r,cost:a}:r):s.cost?{path:null,cost:0}:null}shortestPath(...t){return this.path(...t)}}var Lt=Ct;const It=yt(Lt),zt={1:"north",2:"northeast",3:"northwest",4:"east",5:"west",6:"south",7:"southeast",8:"southwest",9:"up",10:"down",11:"in",12:"out"};class Ot{constructor(t){this.cache=new Map,this.mapReader=t,this.graph=this.buildGraph()}buildGraph(){const t={};return this.mapReader.getRooms().forEach(e=>{const s={},i=new Set((e.exitLocks??[]).map(n=>zt[n]).filter(n=>!!n)),o=new Set(e.mSpecialExitLocks??[]);Object.entries(e.exits??{}).forEach(([n,r])=>{i.has(n)||this.mapReader.getRoom(r)&&(s[r.toString()]=1)}),Object.values(e.specialExits??{}).forEach(n=>{o.has(n)||this.mapReader.getRoom(n)&&(s[n.toString()]=1)}),t[e.id.toString()]=s}),new It(t)}findPath(t,e){const s=`${t}->${e}`;if(this.cache.has(s))return this.cache.get(s);if(t===e){const r=this.mapReader.getRoom(t)?[t]:null;return this.cache.set(s,r),r}if(!this.mapReader.getRoom(t)||!this.mapReader.getRoom(e))return this.cache.set(s,null),null;const i=this.graph.path(t.toString(),e.toString()),o=Array.isArray(i)?i:i==null?void 0:i.path,n=o?o.map(r=>Number(r)):null;return this.cache.set(s,n),n}}exports.ExplorationArea=X;exports.MapReader=bt;exports.PathFinder=Ot;exports.Renderer=xt;exports.Settings=h;
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|