mudlet-map-renderer 0.6.1-konva → 0.6.3-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.
@@ -60,6 +60,8 @@ export declare class Renderer {
60
60
  private clearCurrentRoomOverlay;
61
61
  private updateCurrentRoomOverlay;
62
62
  private createOverlayRoomGroup;
63
+ private getRoomsConnectedByCustomLines;
64
+ private findRoomNearPosition;
63
65
  private disableListening;
64
66
  private renderSymbol;
65
67
  private renderExits;
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("konva"),_=["north","south","east","west","northeast","northwest","southeast","southwest"],D={north:"n",south:"s",east:"e",west:"w",northeast:"ne",northwest:"nw",southeast:"se",southwest:"sw",up:"u",down:"d",in:"i",out:"o"},N={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}},P=["north","south","east","west","northeast","northwest","southeast","southwest"],G={north:"south",south:"north",east:"west",west:"east",northeast:"southwest",northwest:"southeast",southeast:"northwest",southwest:"northeast"};function F(l){return l?Object.prototype.hasOwnProperty.call(N,l):!1}function m(l,t,e,s=1){if(!F(e))return{x:l,y:t};const r=N[e];return{x:l+r.x*s,y:t+r.y*s}}const E={OPEN_DOOR:"rgb(10, 155, 10)",CLOSED_DOOR:"rgb(226, 205, 59)",LOCKED_DOOR:"rgb(155, 10, 10)"},B={1:"north",2:"northeast",3:"northwest",4:"east",5:"west",6:"south",7:"southeast",8:"southwest",9:"up",10:"down",11:"in",12:"out"},H=["up","down","in","out"];function q(l){switch(l){case 1:return E.OPEN_DOOR;case 2:return E.CLOSED_DOOR;default:return E.LOCKED_DOOR}}class K{constructor(t){this.mapReader=t}render(t){return this.renderWithColor(t,c.lineColor)}renderWithColor(t,e){return t.aDir&&t.bDir?this.renderTwoWayExit(t,e):this.renderOneWayExit(t,e)}renderTwoWayExit(t,e){const s=this.mapReader.getRoom(t.a),r=this.mapReader.getRoom(t.b);if(!s||!r||!t.aDir||!t.bDir)return;const i=new p.Group,n=[];if(n.push(...Object.values(m(s.x,s.y,t.aDir,c.roomSize/2))),n.push(...Object.values(m(r.x,r.y,t.bDir,c.roomSize/2))),s.doors[D[t.aDir]]||r.doors[D[t.bDir]]){const h=this.renderDoor(n,s.doors[D[t.aDir]]??r.doors[D[t.bDir]],e);i.add(h)}const o=new p.Line({points:n,stroke:e,strokeWidth:.025});return i.add(o),i}renderOneWayExit(t,e){const s=t.aDir?this.mapReader.getRoom(t.a):this.mapReader.getRoom(t.b),r=t.aDir?this.mapReader.getRoom(t.b):this.mapReader.getRoom(t.a),i=t.aDir?t.aDir:t.bDir;if(!s||!r)return;if(s.area!=r.area&&i)return this.renderAreaExit(s,i,e);let n={x:r.x,y:r.y};(r.area!==s.area||r.z!==s.z)&&(n=m(s.x,s.y,i,c.roomSize/2));const o=m(s.x,s.y,i,.3),h=o.x-(o.x-n.x)/2,u=o.y-(o.y-n.y)/2,d=new p.Group,a=[];a.push(...Object.values(m(s.x,s.y,i,c.roomSize/2))),a.push(n.x,n.y);const g=new p.Line({points:a,stroke:e,strokeWidth:.025,dashEnabled:!0,dash:[.1,.05]});d.add(g);const f=new p.Arrow({points:[a[0],a[1],h,u],pointerLength:.5,pointerWidth:.35,strokeWidth:.035,stroke:e,fill:e,dashEnabled:!0,dash:[.1,.05]});return d.add(f),d}renderAreaExit(t,e,s){const r=m(t.x,t.y,e,c.roomSize/2),i=m(t.x,t.y,e,c.roomSize*1.5),n=s??this.mapReader.getColorValue(t.env);return new p.Arrow({points:[r.x,r.y,i.x,i.y],pointerLength:.3,pointerWidth:.3,strokeWidth:.035,stroke:n,fill:n})}renderSpecialExits(t,e){return Object.entries(t.customLines).map(([s,r])=>{const i=[t.x,t.y];r.points.reduce((d,a)=>(d.push(a.x,-a.y),d),i);const n=r.attributes.arrow?p.Arrow:p.Line,o=e??`rgb(${r.attributes.color.r}, ${r.attributes.color.g}, ${r.attributes.color.b})`,h=new n({points:i,strokeWidth:.025,stroke:o,fill:e??`rgb(${r.attributes.color.r}, ${r.attributes.color.g} , ${r.attributes.color.b})`,pointerLength:.3,pointerWidth:.2});let u=r.attributes.style;return u==="dot line"?(h.dash([.05,.05]),h.dashOffset(.1)):u==="dash line"?h.dash([.4,.2]):u==="solid line"||u!==void 0&&console.log("Brak opisu stylu: "+u),h})}renderStubs(t,e=c.lineColor){return t.stubs.map(s=>{const r=B[s],i=m(t.x,t.y,r,c.roomSize/2),n=m(t.x,t.y,r,c.roomSize/2+.5),o=[i.x,i.y,n.x,n.y];return new p.Line({points:o,stroke:e,strokeWidth:.025})})}renderInnerExits(t,e){return H.map(s=>{if(t.exits[s]){const r=new p.Group,i=new p.RegularPolygon({x:t.x,y:t.y,sides:3,fill:e?this.mapReader.getSymbolColor(t.env,.2):this.mapReader.getSymbolColor(t.env,.6),stroke:e??this.mapReader.getSymbolColor(t.env),strokeWidth:.025,radius:c.roomSize/5,scaleX:1.4,scaleY:.8});r.add(i);let n=t.doors[s];if(n!==void 0)if(e)i.stroke(e);else switch(n){case 1:i.stroke(E.OPEN_DOOR);break;case 2:i.stroke(E.CLOSED_DOOR);break;default:i.stroke(E.LOCKED_DOOR)}switch(s){case"up":i.position(m(t.x,t.y,"south",c.roomSize/4));break;case"down":i.rotation(180),i.position(m(t.x,t.y,"north",c.roomSize/4));break;case"in":const o=i.clone();o.rotation(-90),o.position(m(t.x,t.y,"east",c.roomSize/4)),r.add(o),i.rotation(90),i.position(m(t.x,t.y,"west",c.roomSize/4));break;case"out":const h=i.clone();h.rotation(90),h.position(m(t.x,t.y,"east",c.roomSize/4)),r.add(h),i.rotation(-90),i.position(m(t.x,t.y,"west",c.roomSize/4));break}return r}}).filter(s=>s!==void 0)}renderDoor(t,e,s){const r={x:t[0]+(t[2]-t[0])/2,y:t[1]+(t[3]-t[1])/2};return new p.Rect({x:r.x-c.roomSize/4,y:r.y-c.roomSize/4,width:c.roomSize/2,height:c.roomSize/2,stroke:s??q(e),strokeWidth:.025})}}class Q{constructor(t,e){this.paths=[],this.mapReader=t,this.overlayLayer=e}renderPath(t,e,s,r="#66E64D"){if(e===void 0||s===void 0)return;const i=t.map(a=>this.mapReader.getRoom(a)).filter(a=>a!==void 0),n=[];let o=null;const h=()=>{o&&(o.length<4&&n.pop(),o=null)},u=()=>(o||(o=[],n.push(o)),o);i.forEach((a,g)=>{if(!this.isRoomVisible(a,e,s))return;const f=g>0?i[g-1]:void 0,y=g<i.length-1?i[g+1]:void 0;if(this.isRoomVisible(f,e,s))u();else{h();const R=u();if(f){const b=this.getDirectionTowards(a,f);if(b){const L=m(a.x,a.y,b,c.roomSize);R.push(L.x,L.y)}}}if(o==null||o.push(a.x,a.y),!this.isRoomVisible(y,e,s)&&y){const R=this.getDirectionTowards(a,y);if(R){const b=m(a.x,a.y,R,c.roomSize);o==null||o.push(b.x,b.y)}h()}}),h();const d=n.filter(a=>a.length>=4).map(a=>new p.Line({points:a,stroke:r,strokeWidth:.1}));return d.forEach(a=>{this.overlayLayer.add(a),this.paths.push(a)}),d[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 P)if(t.exits[s]===e.id)return s;for(const s of P)if(e.exits[s]===t.id)return G[s]}}const Y=.6,S=75,J="rgb(225, 255, 225)",C="rgb(120, 72, 0)",w=class w{};w.roomSize=Y,w.lineColor=J,w.instantMapMove=!1,w.highlightCurrentRoom=!0;let c=w;class U{constructor(t,e){this.highlights=new Map,this.currentZoom=1,this.currentRoomOverlay=[],this.stage=new p.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 p.Layer({listening:!1}),this.stage.add(this.linkLayer),this.roomLayer=new p.Layer,this.stage.add(this.roomLayer),this.overlayLayer=new p.Layer({listening:!1}),this.stage.add(this.overlayLayer),this.positionLayer=new p.Layer({listening:!1}),this.stage.add(this.positionLayer),this.mapReader=e,this.exitRenderer=new K(e),this.pathRenderer=new Q(e,this.overlayLayer),this.initScaling(1.1)}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()}initScaling(t){p.hitOnDragEnabled=!0;let e,s=!1,r=!1;this.stage.on("touchstart",i=>{const n=i.evt.touches;n&&n.length>1?(r=!0,this.stage.isDragging()&&(this.stage.stopDrag(),s=!0),this.stage.draggable(!1)):(r=!1,this.stage.draggable(!0))}),this.stage.on("touchend touchcancel",i=>{e=void 0;const n=i.evt.touches;(!n||n.length<=1)&&(r=!1,this.stage.draggable(!0))}),this.stage.on("wheel",i=>{i.evt.preventDefault();const n=this.stage.scaleX(),o=this.stage.getPointerPosition();if(!o)return;const h={x:(o.x-this.stage.x())/n,y:(o.y-this.stage.y())/n};let u=i.evt.deltaY>0?-1:1;i.evt.ctrlKey&&(u=-u);const d=u>0?this.currentZoom*t:this.currentZoom/t,a=d*S,g=this.setZoom(d),f={x:o.x-h.x*a,y:o.y-h.y*a};this.stage.position(f),g&&this.emitZoomChangeEvent()}),this.stage.on("touchmove",i=>{const n=i.evt.touches,o=n==null?void 0:n[0],h=n==null?void 0:n[1];if(h||r&&(r=!1,this.stage.draggable(!0)),o&&!h&&s&&!this.stage.isDragging()&&(this.stage.startDrag(),s=!1),!o||!h){e=void 0;return}i.evt.preventDefault(),this.stage.isDragging()&&(this.stage.stopDrag(),s=!0),r||(r=!0,this.stage.draggable(!1));const u=this.stage.container().getBoundingClientRect(),d={x:o.clientX-u.left,y:o.clientY-u.top},a={x:h.clientX-u.left,y:h.clientY-u.top},g=Math.hypot(d.x-a.x,d.y-a.y);if(e===void 0){e=g;return}if(e===0)return;const f=this.stage.scaleX(),y=this.stage.x(),v=this.stage.y(),x={x:this.stage.width()/2,y:this.stage.height()/2},R={x:(x.x-y)/f,y:(x.y-v)/f},b=this.currentZoom*(g/e),L=this.setZoom(b),I=this.stage.scaleX(),j={x:x.x-R.x*I,y:x.y-R.y*I};this.stage.position(j),this.stage.batchDraw(),e=g,L&&this.emitZoomChangeEvent()})}drawArea(t,e){const s=this.mapReader.getArea(t);if(!s)return;const r=s.getPlane(e);r&&(this.currentArea=t,this.currentAreaInstance=s,this.currentZIndex=e,this.currentAreaVersion=s.getVersion(),this.clearCurrentRoomOverlay(),this.roomLayer.destroyChildren(),this.linkLayer.destroyChildren(),this.stage.scale({x:S*this.currentZoom,y:S*this.currentZoom}),this.renderLabels(r.getLabels()),this.renderRooms(r.getRooms()??[]),this.renderExits(s.getLinkExits(e)),this.refreshHighlights(),this.stage.batchDraw())}emitRoomContextEvent(t,e,s){const r=this.stage.container(),i=r.getBoundingClientRect(),n={roomId:t,position:{x:e-i.left,y:s-i.top}},o=new CustomEvent("roomcontextmenu",{detail:n});r.dispatchEvent(o)}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:S*t,y:S*t}),!0)}getZoom(){return this.currentZoom}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),r=s==null?void 0:s.getVersion();let i=this.currentArea!==e.area||this.currentZIndex!==e.z;(this.currentArea!==e.area||this.currentZIndex!==e.z||r!==void 0&&this.currentAreaVersion!==r||s!==void 0&&this.currentAreaInstance!==s)&&this.drawArea(e.area,e.z),this.positionRender||(this.positionRender=new p.Circle({x:e.x,y:e.y,radius:Y*.85,stroke:"rgb(0, 229, 178)",strokeWidth:.1,dash:[.05,.05],dashEnabled:!0}),this.positionLayer.add(this.positionRender)),this.centerOnRoom(e,i),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 r=this.highlights.get(t);r!=null&&r.shape&&(r.shape.destroy(),delete r.shape);const i={color:e,area:s.area,z:s.z};if(this.highlights.set(t,i),s.area===this.currentArea&&s.z===this.currentZIndex){const n=this.createHighlightShape(s,e);return this.overlayLayer.add(n),i.shape=n,this.overlayLayer.batchDraw(),n}return i.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 i;if((i=t.shape)==null||i.destroy(),delete t.shape,t.area!==this.currentArea||t.z!==this.currentZIndex)return;const s=this.mapReader.getRoom(e);if(!s)return;const r=this.createHighlightShape(s,t.color);this.overlayLayer.add(r),t.shape=r}),this.overlayLayer.batchDraw()}createHighlightShape(t,e){return new p.Circle({x:t.x,y:t.y,radius:c.roomSize*.9,stroke:e,strokeWidth:.15,dash:[.1,.05],dashEnabled:!0,listening:!1})}centerOnRoom(t,e=!1){var u;this.currentRoomId=t.id;const s={x:t.x,y:t.y};(u=this.positionRender)==null||u.position(t);const i=this.stage.getAbsoluteTransform().point(s),n={x:this.stage.width()/2,y:this.stage.height()/2},o=n.x-i.x,h=n.y-i.y;this.currentTransition&&(this.currentTransition.pause(),this.currentTransition.destroy(),delete this.currentTransition),console.log(c.instantMapMove),e||c.instantMapMove?this.stage.position({x:this.stage.x()+o,y:this.stage.y()+h}):(this.currentTransition=new p.Tween({node:this.stage,x:this.stage.x()+o,y:this.stage.y()+h,duration:.2,easing:p.Easings.EaseInOut}),this.currentTransition.play())}renderRooms(t){t.forEach(e=>{const s=new p.Group({x:e.x-c.roomSize/2,y:e.y-c.roomSize/2}),r=new p.Rect({x:0,y:0,width:c.roomSize,height:c.roomSize,fill:this.mapReader.getColorValue(e.env),strokeWidth:.025,stroke:c.lineColor}),i=(a,g)=>this.emitRoomContextEvent(e.id,a,g);s.on("mouseenter",()=>{this.stage.container().style.cursor="pointer"}),s.on("mouseleave",()=>{this.stage.container().style.cursor="auto"}),s.on("contextmenu",a=>{a.evt.preventDefault();const g=a.evt;i(g.clientX,g.clientY)});let n,o,h;const u=()=>{h!==void 0&&(this.stage.draggable(h),h=void 0)},d=()=>{n!==void 0&&(window.clearTimeout(n),n=void 0),o=void 0,u()};s.on("touchstart",a=>{var f;if(d(),a.evt.touches&&a.evt.touches.length>1)return;const g=(f=a.evt.touches)==null?void 0:f[0];g&&(o={clientX:g.clientX,clientY:g.clientY},h=this.stage.draggable(),this.stage.draggable(!1),n=window.setTimeout(()=>{o&&i(o.clientX,o.clientY),d()},500))}),s.on("touchend",d),s.on("touchmove",a=>{var R;if(!o)return;const g=(R=a.evt.touches)==null?void 0:R[0];if(!g){d();return}const f=g.clientX-o.clientX,y=g.clientY-o.clientY,v=f*f+y*y,x=10;if(v>x*x){const b=h;d(),b&&this.stage.startDrag()}}),s.on("touchcancel",d),s.add(r),this.renderSymbol(e,s),this.roomLayer.add(s),this.exitRenderer.renderSpecialExits(e).forEach(a=>{this.linkLayer.add(a)}),this.exitRenderer.renderStubs(e).forEach(a=>{this.linkLayer.add(a)}),this.exitRenderer.renderInnerExits(e).forEach(a=>{this.roomLayer.add(a)})})}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=[],r=[];this.currentAreaInstance&&this.currentZIndex!==void 0&&this.currentAreaInstance.getLinkExits(this.currentZIndex).filter(h=>h.a===t.id||h.b===t.id).forEach(h=>{const u=c.highlightCurrentRoom?this.exitRenderer.renderWithColor(h,C):this.exitRenderer.render(h);u&&(this.disableListening(u),s.push(u));const d=h.a===t.id?h.b:h.a,a=this.mapReader.getRoom(d);a&&a.area===this.currentArea&&a.z===this.currentZIndex&&e.set(a.id,a)});const i=c.highlightCurrentRoom?C:void 0;this.exitRenderer.renderSpecialExits(t,i).forEach(o=>{this.disableListening(o),s.push(o)}),(c.highlightCurrentRoom?this.exitRenderer.renderStubs(t,C):this.exitRenderer.renderStubs(t)).forEach(o=>{this.disableListening(o),s.push(o)}),this.exitRenderer.renderInnerExits(t,i).forEach(o=>{this.disableListening(o),r.push(o)}),s.forEach(o=>{this.overlayLayer.add(o),this.currentRoomOverlay.push(o)}),e.forEach((o,h)=>{const u=h===t.id,d=this.createOverlayRoomGroup(o,{stroke:u&&c.highlightCurrentRoom?C:c.lineColor,fillEnabled:!u,strokeEnabled:u?c.highlightCurrentRoom:!1});this.overlayLayer.add(d),this.currentRoomOverlay.push(d),u||(this.exitRenderer.renderSpecialExits(o).forEach(a=>{this.disableListening(a),this.overlayLayer.add(a),this.currentRoomOverlay.push(a)}),this.exitRenderer.renderStubs(o).forEach(a=>{this.disableListening(a),this.overlayLayer.add(a),this.currentRoomOverlay.push(a)}),this.exitRenderer.renderInnerExits(o).forEach(a=>{this.disableListening(a),this.overlayLayer.add(a),this.currentRoomOverlay.push(a)}))}),r.forEach(o=>{this.overlayLayer.add(o),this.currentRoomOverlay.push(o)}),this.overlayLayer.batchDraw()}createOverlayRoomGroup(t,e){const s=new p.Group({x:t.x-c.roomSize/2,y:t.y-c.roomSize/2,listening:!1}),r=new p.Rect({x:0,y:0,width:c.roomSize,height:c.roomSize,fill:this.mapReader.getColorValue(t.env),stroke:e.stroke,strokeWidth:.025,strokeEnabled:e.strokeEnabled});return e.fillEnabled||r.fillEnabled(!1),s.add(r),this.renderSymbol(t,s),s}disableListening(t){t.listening(!1),"getChildren"in t&&typeof t.getChildren=="function"&&t.getChildren().forEach(e=>this.disableListening(e))}renderSymbol(t,e){if(t.roomChar!==void 0){const s=new p.Text({x:0,y:0,text:t.roomChar,fontSize:.45,fontStyle:"bold",fill:this.mapReader.getSymbolColor(t.env),align:"center",verticalAlign:"middle",width:c.roomSize,height:c.roomSize});e.add(s)}}renderExits(t){t.forEach(e=>{const s=this.exitRenderer.render(e);s&&this.linkLayer.add(s)})}renderLabels(t){t.forEach(e=>{if(!e.pixMap)return;const s=new Image;s.src=`data:image/png;base64,${e.pixMap}`;const r=new p.Image({x:e.X,y:-e.Y,width:e.Width,height:e.Height,image:s});this.linkLayer.add(r)})}}class W{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 O{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,r])=>(e[+s]=new W(r,this.area.labels.filter(i=>i.Z===+s)),e),{})}createExits(){this.area.rooms.forEach(t=>{Object.entries(t.exits).filter(([e,s])=>_.indexOf(e)>-1&&!t.customLines.hasOwnProperty(D[e])).forEach(([e,s])=>this.createHalfExit(t.id,s,t.z,e))})}createHalfExit(t,e,s,r){if(t===e)return;const i=Math.min(t,e),n=Math.max(t,e),o=`${i}-${n}`;let h=this.exits.get(o);h||(h={a:i,b:n,zIndex:[s]}),i==t?h.aDir=r:h.bDir=r,h.zIndex.push(s),this.exits.set(o,h)}}class z extends W{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 k extends O{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 z(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 z(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 r=this.visitedRooms.has(s);this.visitedRooms.add(s),!r&&this.areaRoomIds.has(s)&&e++}return e>0&&this.markDirty(),e}}const V={rgbValue:"rgb(114, 1, 0)",symbolColor:[225,225,225]};function M(l){const t=l[0]/255,e=l[1]/255,s=l[2]/255,r=Math.max(t,e,s),i=Math.min(t,e,s);return(r+i)/2}class tt{constructor(t,e){this.rooms={},this.areas={},this.areaSources={},this.explorationEnabled=!1,this.colors={},t.forEach(s=>{s.rooms.forEach(i=>{i.y=-i.y,this.rooms[i.id]=i});const r=parseInt(s.areaId);this.areas[r]=new O(s),this.areaSources[r]=s}),this.colors=e.reduce((s,r)=>({...s,[r.envId]:{rgb:r.colors,rgbValue:`rgb(${r.colors.join(",")}`,symbolColor:M(r.colors)>.41?[25,25,25]:[225,255,255],symbolColorValue:M(r.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 k)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 k(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 O(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 r=this.getRoom(t);if(r){const i=this.getExplorationArea(r.area);if(i)return i.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 r of t){if(this.explorationEnabled){const n=this.getRoom(r);if(n){const o=this.getExplorationArea(n.area);if(o){o.addVisitedRoom(r)&&s++;continue}}}const i=e.has(r);e.add(r),i||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)??V.rgbValue}getSymbolColor(t,e){var n;const s=((n=this.colors[t])==null?void 0:n.symbolColor)??V.symbolColor,r=Math.min(Math.max(e??1,0),1),i=s.join(",");return r!=1?`rgba(${i}, ${r})`:`rgba(${i})`}}function et(l){return l&&l.__esModule&&Object.prototype.hasOwnProperty.call(l,"default")?l.default:l}class st{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(r=>(r.key===t&&Object.assign(r,{priority:s}),r)):(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 rt=st;function Z(l,t){const e=new Map;for(const[s,r]of l)s!==t&&r instanceof Map?e.set(s,Z(r,t)):s!==t&&e.set(s,r);return e}var it=Z;function ot(l){const t=Number(l);return!(isNaN(t)||t<=0)}function X(l){const t=new Map;return Object.keys(l).forEach(s=>{const r=l[s];if(r!==null&&typeof r=="object"&&!Array.isArray(r))return t.set(s,X(r));if(!ot(r))throw new Error(`Could not add node at key "${s}", make sure it's a valid node`,r);return t.set(s,Number(r))}),t}var nt=X;function $(l){if(!(l instanceof Map))throw new Error(`Invalid graph: Expected Map instead found ${typeof l}`);l.forEach((t,e)=>{if(typeof t=="object"&&t instanceof Map){$(t);return}if(typeof t!="number"||t<=0)throw new Error(`Values must be numbers greater than 0. Found value ${t} at ${e}`)})}var at=$;const ht=rt,ct=it,A=nt,T=at;class lt{constructor(t){t instanceof Map?(T(t),this.graph=t):t?this.graph=A(t):this.graph=new Map}addNode(t,e){let s;return e instanceof Map?(T(e),s=e):s=A(e),this.graph.set(t,s),this}addVertex(t,e){return this.addNode(t,e)}removeNode(t){return this.graph=ct(this.graph,t),this}path(t,e,s={}){if(!this.graph.size)return s.cost?{path:null,cost:0}:null;const r=new Set,i=new ht,n=new Map;let o=[],h=0,u=[];if(s.avoid&&(u=[].concat(s.avoid)),u.includes(t))throw new Error(`Starting node (${t}) cannot be avoided`);if(u.includes(e))throw new Error(`Ending node (${e}) cannot be avoided`);for(i.set(t,0);!i.isEmpty();){const d=i.next();if(d.key===e){h=d.priority;let g=d.key;for(;n.has(g);)o.push(g),g=n.get(g);break}r.add(d.key),(this.graph.get(d.key)||new Map).forEach((g,f)=>{if(r.has(f)||u.includes(f))return null;if(!i.has(f))return n.set(f,d.key),i.set(f,d.priority+g);const y=i.get(f).priority,v=d.priority+g;return v<y?(n.set(f,d.key),i.set(f,v)):null})}return o.length?(s.trim?o.shift():o=o.concat([t]),s.reverse||(o=o.reverse()),s.cost?{path:o,cost:h}:o):s.cost?{path:null,cost:0}:null}shortestPath(...t){return this.path(...t)}}var ut=lt;const dt=et(ut),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"};class pt{constructor(t){this.cache=new Map,this.mapReader=t,this.graph=this.buildGraph()}buildGraph(){const t={};return this.mapReader.getRooms().forEach(e=>{const s={},r=new Set((e.exitLocks??[]).map(n=>gt[n]).filter(n=>!!n)),i=new Set(e.mSpecialExitLocks??[]);Object.entries(e.exits??{}).forEach(([n,o])=>{r.has(n)||this.mapReader.getRoom(o)&&(s[o.toString()]=1)}),Object.values(e.specialExits??{}).forEach(n=>{i.has(n)||this.mapReader.getRoom(n)&&(s[n.toString()]=1)}),t[e.id.toString()]=s}),new dt(t)}findPath(t,e){const s=`${t}->${e}`;if(this.cache.has(s))return this.cache.get(s);if(t===e){const o=this.mapReader.getRoom(t)?[t]:null;return this.cache.set(s,o),o}if(!this.mapReader.getRoom(t)||!this.mapReader.getRoom(e))return this.cache.set(s,null),null;const r=this.graph.path(t.toString(),e.toString()),i=Array.isArray(r)?r:r==null?void 0:r.path,n=i?i.map(o=>Number(o)):null;return this.cache.set(s,n),n}}exports.ExplorationArea=k;exports.MapReader=tt;exports.PathFinder=pt;exports.Renderer=U;exports.Settings=c;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const m=require("konva"),_=["north","south","east","west","northeast","northwest","southeast","southwest"],C={north:"n",south:"s",east:"e",west:"w",northeast:"ne",northwest:"nw",southeast:"se",southwest:"sw",up:"u",down:"d",in:"i",out:"o"},N={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}},P=["north","south","east","west","northeast","northwest","southeast","southwest"],G={north:"south",south:"north",east:"west",west:"east",northeast:"southwest",northwest:"southeast",southeast:"northwest",southwest:"northeast"};function F(d){return d?Object.prototype.hasOwnProperty.call(N,d):!1}function p(d,t,e,s=1){if(!F(e))return{x:d,y:t};const r=N[e];return{x:d+r.x*s,y:t+r.y*s}}const E={OPEN_DOOR:"rgb(10, 155, 10)",CLOSED_DOOR:"rgb(226, 205, 59)",LOCKED_DOOR:"rgb(155, 10, 10)"},B={1:"north",2:"northeast",3:"northwest",4:"east",5:"west",6:"south",7:"southeast",8:"southwest",9:"up",10:"down",11:"in",12:"out"},H=["up","down","in","out"];function q(d){switch(d){case 1:return E.OPEN_DOOR;case 2:return E.CLOSED_DOOR;default:return E.LOCKED_DOOR}}class K{constructor(t){this.mapReader=t}render(t){return this.renderWithColor(t,l.lineColor)}renderWithColor(t,e){return t.aDir&&t.bDir?this.renderTwoWayExit(t,e):this.renderOneWayExit(t,e)}renderTwoWayExit(t,e){const s=this.mapReader.getRoom(t.a),r=this.mapReader.getRoom(t.b);if(!s||!r||!t.aDir||!t.bDir)return;const i=new m.Group,o=[];if(o.push(...Object.values(p(s.x,s.y,t.aDir,l.roomSize/2))),o.push(...Object.values(p(r.x,r.y,t.bDir,l.roomSize/2))),s.doors[C[t.aDir]]||r.doors[C[t.bDir]]){const h=this.renderDoor(o,s.doors[C[t.aDir]]??r.doors[C[t.bDir]],e);i.add(h)}const n=new m.Line({points:o,stroke:e,strokeWidth:.025});return i.add(n),i}renderOneWayExit(t,e){const s=t.aDir?this.mapReader.getRoom(t.a):this.mapReader.getRoom(t.b),r=t.aDir?this.mapReader.getRoom(t.b):this.mapReader.getRoom(t.a),i=t.aDir?t.aDir:t.bDir;if(!s||!r)return;if(s.area!=r.area&&i)return this.renderAreaExit(s,i,e);let o={x:r.x,y:r.y};(r.area!==s.area||r.z!==s.z)&&(o=p(s.x,s.y,i,l.roomSize/2));const n=p(s.x,s.y,i,.3),h=n.x-(n.x-o.x)/2,c=n.y-(n.y-o.y)/2,u=new m.Group,a=[];a.push(...Object.values(p(s.x,s.y,i,l.roomSize/2))),a.push(o.x,o.y);const g=new m.Line({points:a,stroke:e,strokeWidth:.025,dashEnabled:!0,dash:[.1,.05]});u.add(g);const f=new m.Arrow({points:[a[0],a[1],h,c],pointerLength:.5,pointerWidth:.35,strokeWidth:.035,stroke:e,fill:e,dashEnabled:!0,dash:[.1,.05]});return u.add(f),u}renderAreaExit(t,e,s){const r=p(t.x,t.y,e,l.roomSize/2),i=p(t.x,t.y,e,l.roomSize*1.5),o=s??this.mapReader.getColorValue(t.env);return new m.Arrow({points:[r.x,r.y,i.x,i.y],pointerLength:.3,pointerWidth:.3,strokeWidth:.035,stroke:o,fill:o})}renderSpecialExits(t,e){return Object.entries(t.customLines).map(([s,r])=>{const i=[t.x,t.y];r.points.reduce((u,a)=>(u.push(a.x,-a.y),u),i);const o=r.attributes.arrow?m.Arrow:m.Line,n=e??`rgb(${r.attributes.color.r}, ${r.attributes.color.g}, ${r.attributes.color.b})`,h=new o({points:i,strokeWidth:.025,stroke:n,fill:e??`rgb(${r.attributes.color.r}, ${r.attributes.color.g} , ${r.attributes.color.b})`,pointerLength:.3,pointerWidth:.2});let c=r.attributes.style;return c==="dot line"?(h.dash([.05,.05]),h.dashOffset(.1)):c==="dash line"?h.dash([.4,.2]):c==="solid line"||c!==void 0&&console.log("Brak opisu stylu: "+c),h})}renderStubs(t,e=l.lineColor){return t.stubs.map(s=>{const r=B[s],i=p(t.x,t.y,r,l.roomSize/2),o=p(t.x,t.y,r,l.roomSize/2+.5),n=[i.x,i.y,o.x,o.y];return new m.Line({points:n,stroke:e,strokeWidth:.025})})}renderInnerExits(t,e){return H.map(s=>{if(t.exits[s]){const r=new m.Group,i=new m.RegularPolygon({x:t.x,y:t.y,sides:3,fill:e?this.mapReader.getSymbolColor(t.env,.2):this.mapReader.getSymbolColor(t.env,.6),stroke:e??this.mapReader.getSymbolColor(t.env),strokeWidth:.025,radius:l.roomSize/5,scaleX:1.4,scaleY:.8});r.add(i);let o=t.doors[s];if(o!==void 0)if(e)i.stroke(e);else switch(o){case 1:i.stroke(E.OPEN_DOOR);break;case 2:i.stroke(E.CLOSED_DOOR);break;default:i.stroke(E.LOCKED_DOOR)}switch(s){case"up":i.position(p(t.x,t.y,"south",l.roomSize/4));break;case"down":i.rotation(180),i.position(p(t.x,t.y,"north",l.roomSize/4));break;case"in":const n=i.clone();n.rotation(-90),n.position(p(t.x,t.y,"east",l.roomSize/4)),r.add(n),i.rotation(90),i.position(p(t.x,t.y,"west",l.roomSize/4));break;case"out":const h=i.clone();h.rotation(90),h.position(p(t.x,t.y,"east",l.roomSize/4)),r.add(h),i.rotation(-90),i.position(p(t.x,t.y,"west",l.roomSize/4));break}return r}}).filter(s=>s!==void 0)}renderDoor(t,e,s){const r={x:t[0]+(t[2]-t[0])/2,y:t[1]+(t[3]-t[1])/2};return new m.Rect({x:r.x-l.roomSize/4,y:r.y-l.roomSize/4,width:l.roomSize/2,height:l.roomSize/2,stroke:s??q(e),strokeWidth:.025})}}class Y{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 L{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,r])=>(e[+s]=new Y(r,this.area.labels.filter(i=>i.Z===+s)),e),{})}createExits(){this.area.rooms.forEach(t=>{Object.entries(t.exits).filter(([e,s])=>_.indexOf(e)>-1&&!t.customLines.hasOwnProperty(C[e])).forEach(([e,s])=>this.createHalfExit(t.id,s,t.z,e))})}createHalfExit(t,e,s,r){if(t===e)return;const i=Math.min(t,e),o=Math.max(t,e),n=`${i}-${o}`;let h=this.exits.get(n);h||(h={a:i,b:o,zIndex:[s]}),i==t?h.aDir=r:h.bDir=r,h.zIndex.push(s),this.exits.set(n,h)}}class z extends Y{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 k extends L{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 z(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 z(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 r=this.visitedRooms.has(s);this.visitedRooms.add(s),!r&&this.areaRoomIds.has(s)&&e++}return e>0&&this.markDirty(),e}}class Q{constructor(t,e){this.paths=[],this.mapReader=t,this.overlayLayer=e}renderPath(t,e,s,r="#66E64D"){if(e===void 0||s===void 0)return;const i=t.map(a=>this.mapReader.getRoom(a)).filter(a=>a!==void 0),o=[];let n=null;const h=()=>{n&&(n.length<4&&o.pop(),n=null)},c=()=>(n||(n=[],o.push(n)),n);i.forEach((a,g)=>{if(!this.isRoomVisible(a,e,s))return;const f=g>0?i[g-1]:void 0,y=g<i.length-1?i[g+1]:void 0;if(this.isRoomVisible(f,e,s))c();else{h();const R=c();if(f){const b=this.getDirectionTowards(a,f);if(b){const O=p(a.x,a.y,b,l.roomSize);R.push(O.x,O.y)}}}if(n==null||n.push(a.x,a.y),!this.isRoomVisible(y,e,s)&&y){const R=this.getDirectionTowards(a,y);if(R){const b=p(a.x,a.y,R,l.roomSize);n==null||n.push(b.x,b.y)}h()}}),h();const u=o.filter(a=>a.length>=4).map(a=>new m.Line({points:a,stroke:r,strokeWidth:.1}));return u.forEach(a=>{this.overlayLayer.add(a),this.paths.push(a)}),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 P)if(t.exits[s]===e.id)return s;for(const s of P)if(e.exits[s]===t.id)return G[s]}}const Z=.6,S=75,J="rgb(225, 255, 225)",D="rgb(120, 72, 0)",v=class v{};v.roomSize=Z,v.lineColor=J,v.instantMapMove=!1,v.highlightCurrentRoom=!0;let l=v;class U{constructor(t,e){this.highlights=new Map,this.currentZoom=1,this.currentRoomOverlay=[],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.overlayLayer=new m.Layer({listening:!1}),this.stage.add(this.overlayLayer),this.positionLayer=new m.Layer({listening:!1}),this.stage.add(this.positionLayer),this.mapReader=e,this.exitRenderer=new K(e),this.pathRenderer=new Q(e,this.overlayLayer),this.initScaling(1.1)}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()}initScaling(t){m.hitOnDragEnabled=!0;let e,s=!1,r=!1;this.stage.on("touchstart",i=>{const o=i.evt.touches;o&&o.length>1?(r=!0,this.stage.isDragging()&&(this.stage.stopDrag(),s=!0),this.stage.draggable(!1)):(r=!1,this.stage.draggable(!0))}),this.stage.on("touchend touchcancel",i=>{e=void 0;const o=i.evt.touches;(!o||o.length<=1)&&(r=!1,this.stage.draggable(!0))}),this.stage.on("wheel",i=>{i.evt.preventDefault();const o=this.stage.scaleX(),n=this.stage.getPointerPosition();if(!n)return;const h={x:(n.x-this.stage.x())/o,y:(n.y-this.stage.y())/o};let c=i.evt.deltaY>0?-1:1;i.evt.ctrlKey&&(c=-c);const u=c>0?this.currentZoom*t:this.currentZoom/t,a=u*S,g=this.setZoom(u),f={x:n.x-h.x*a,y:n.y-h.y*a};this.stage.position(f),g&&this.emitZoomChangeEvent()}),this.stage.on("touchmove",i=>{const o=i.evt.touches,n=o==null?void 0:o[0],h=o==null?void 0:o[1];if(h||r&&(r=!1,this.stage.draggable(!0)),n&&!h&&s&&!this.stage.isDragging()&&(this.stage.startDrag(),s=!1),!n||!h){e=void 0;return}i.evt.preventDefault(),this.stage.isDragging()&&(this.stage.stopDrag(),s=!0),r||(r=!0,this.stage.draggable(!1));const c=this.stage.container().getBoundingClientRect(),u={x:n.clientX-c.left,y:n.clientY-c.top},a={x:h.clientX-c.left,y:h.clientY-c.top},g=Math.hypot(u.x-a.x,u.y-a.y);if(e===void 0){e=g;return}if(e===0)return;const f=this.stage.scaleX(),y=this.stage.x(),w=this.stage.y(),x={x:this.stage.width()/2,y:this.stage.height()/2},R={x:(x.x-y)/f,y:(x.y-w)/f},b=this.currentZoom*(g/e),O=this.setZoom(b),I=this.stage.scaleX(),j={x:x.x-R.x*I,y:x.y-R.y*I};this.stage.position(j),this.stage.batchDraw(),e=g,O&&this.emitZoomChangeEvent()})}drawArea(t,e){const s=this.mapReader.getArea(t);if(!s)return;const r=s.getPlane(e);r&&(this.currentArea=t,this.currentAreaInstance=s,this.currentZIndex=e,this.currentAreaVersion=s.getVersion(),this.clearCurrentRoomOverlay(),this.roomLayer.destroyChildren(),this.linkLayer.destroyChildren(),this.stage.scale({x:S*this.currentZoom,y:S*this.currentZoom}),this.renderLabels(r.getLabels()),this.renderExits(s.getLinkExits(e)),this.renderRooms(r.getRooms()??[]),this.refreshHighlights(),this.stage.batchDraw())}emitRoomContextEvent(t,e,s){const r=this.stage.container(),i=r.getBoundingClientRect(),o={roomId:t,position:{x:e-i.left,y:s-i.top}},n=new CustomEvent("roomcontextmenu",{detail:o});r.dispatchEvent(n)}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:S*t,y:S*t}),!0)}getZoom(){return this.currentZoom}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),r=s==null?void 0:s.getVersion();let i=this.currentArea!==e.area||this.currentZIndex!==e.z;(this.currentArea!==e.area||this.currentZIndex!==e.z||r!==void 0&&this.currentAreaVersion!==r||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:Z*.85,stroke:"rgb(0, 229, 178)",strokeWidth:.1,dash:[.05,.05],dashEnabled:!0}),this.positionLayer.add(this.positionRender)),this.centerOnRoom(e,i),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 r=this.highlights.get(t);r!=null&&r.shape&&(r.shape.destroy(),delete r.shape);const i={color:e,area:s.area,z:s.z};if(this.highlights.set(t,i),s.area===this.currentArea&&s.z===this.currentZIndex){const o=this.createHighlightShape(s,e);return this.overlayLayer.add(o),i.shape=o,this.overlayLayer.batchDraw(),o}return i.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 i;if((i=t.shape)==null||i.destroy(),delete t.shape,t.area!==this.currentArea||t.z!==this.currentZIndex)return;const s=this.mapReader.getRoom(e);if(!s)return;const r=this.createHighlightShape(s,t.color);this.overlayLayer.add(r),t.shape=r}),this.overlayLayer.batchDraw()}createHighlightShape(t,e){return new m.Circle({x:t.x,y:t.y,radius:l.roomSize*.9,stroke:e,strokeWidth:.15,dash:[.1,.05],dashEnabled:!0,listening:!1})}centerOnRoom(t,e=!1){var c;this.currentRoomId=t.id;const s={x:t.x,y:t.y};(c=this.positionRender)==null||c.position(t);const i=this.stage.getAbsoluteTransform().point(s),o={x:this.stage.width()/2,y:this.stage.height()/2},n=o.x-i.x,h=o.y-i.y;this.currentTransition&&(this.currentTransition.pause(),this.currentTransition.destroy(),delete this.currentTransition),console.log(l.instantMapMove),e||l.instantMapMove?this.stage.position({x:this.stage.x()+n,y:this.stage.y()+h}):(this.currentTransition=new m.Tween({node:this.stage,x:this.stage.x()+n,y:this.stage.y()+h,duration:.2,easing:m.Easings.EaseInOut}),this.currentTransition.play())}renderRooms(t){t.forEach(e=>{const s=new m.Group({x:e.x-l.roomSize/2,y:e.y-l.roomSize/2}),r=new m.Rect({x:0,y:0,width:l.roomSize,height:l.roomSize,fill:this.mapReader.getColorValue(e.env),strokeWidth:.025,stroke:l.lineColor}),i=(a,g)=>this.emitRoomContextEvent(e.id,a,g);s.on("mouseenter",()=>{this.stage.container().style.cursor="pointer"}),s.on("mouseleave",()=>{this.stage.container().style.cursor="auto"}),s.on("contextmenu",a=>{a.evt.preventDefault();const g=a.evt;i(g.clientX,g.clientY)});let o,n,h;const c=()=>{h!==void 0&&(this.stage.draggable(h),h=void 0)},u=()=>{o!==void 0&&(window.clearTimeout(o),o=void 0),n=void 0,c()};s.on("touchstart",a=>{var f;if(u(),a.evt.touches&&a.evt.touches.length>1)return;const g=(f=a.evt.touches)==null?void 0:f[0];g&&(n={clientX:g.clientX,clientY:g.clientY},h=this.stage.draggable(),this.stage.draggable(!1),o=window.setTimeout(()=>{n&&i(n.clientX,n.clientY),u()},500))}),s.on("touchend",u),s.on("touchmove",a=>{var R;if(!n)return;const g=(R=a.evt.touches)==null?void 0:R[0];if(!g){u();return}const f=g.clientX-n.clientX,y=g.clientY-n.clientY,w=f*f+y*y,x=10;if(w>x*x){const b=h;u(),b&&this.stage.startDrag()}}),s.on("touchcancel",u),s.add(r),this.renderSymbol(e,s),this.roomLayer.add(s),this.exitRenderer.renderSpecialExits(e).forEach(a=>{this.linkLayer.add(a)}),this.exitRenderer.renderStubs(e).forEach(a=>{this.linkLayer.add(a)}),this.exitRenderer.renderInnerExits(e).forEach(a=>{this.roomLayer.add(a)})})}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=[],r=this.currentAreaInstance instanceof k?this.currentAreaInstance:void 0,i=this.mapReader.getRooms();this.currentAreaInstance&&this.currentZIndex!==void 0&&this.currentAreaInstance.getLinkExits(this.currentZIndex).filter(u=>u.a===t.id||u.b===t.id).forEach(u=>{const a=l.highlightCurrentRoom?this.exitRenderer.renderWithColor(u,D):this.exitRenderer.render(u);a&&(this.disableListening(a),s.push(a));const g=u.a===t.id?u.b:u.a,f=this.mapReader.getRoom(g),y=!r||r.hasVisitedRoom(g);f&&f.area===this.currentArea&&f.z===this.currentZIndex&&y&&e.set(f.id,f)}),this.getRoomsConnectedByCustomLines(t,i).forEach((c,u)=>{const a=!r||r.hasVisitedRoom(u);c.area===this.currentArea&&c.z===this.currentZIndex&&a&&e.set(u,c)});const n=l.highlightCurrentRoom?D:void 0;this.exitRenderer.renderSpecialExits(t,n).forEach(c=>{this.disableListening(c),s.push(c)}),(l.highlightCurrentRoom?this.exitRenderer.renderStubs(t,D):this.exitRenderer.renderStubs(t)).forEach(c=>{this.disableListening(c),s.push(c)}),s.forEach(c=>{this.currentRoomOverlay.push(c)}),e.forEach((c,u)=>{const a=u===t.id,g=this.createOverlayRoomGroup(c,{stroke:a&&l.highlightCurrentRoom?D:l.lineColor,fillEnabled:!a,strokeEnabled:a?l.highlightCurrentRoom:!1});this.currentRoomOverlay.push(g);const f=a&&l.highlightCurrentRoom?D:void 0;this.exitRenderer.renderInnerExits(c,f).forEach(y=>{this.disableListening(y),this.currentRoomOverlay.push(y)})})}createOverlayRoomGroup(t,e){const s=new m.Group({x:t.x-l.roomSize/2,y:t.y-l.roomSize/2,listening:!1}),r=new m.Rect({x:0,y:0,width:l.roomSize,height:l.roomSize,fill:this.mapReader.getColorValue(t.env),stroke:e.stroke,strokeWidth:.025,strokeEnabled:e.strokeEnabled});return e.fillEnabled||r.fillEnabled(!1),s.add(r),this.renderSymbol(t,s),s}getRoomsConnectedByCustomLines(t,e){const s=new Map;return Object.values(t.customLines).forEach(r=>{r.points.length&&r.points.forEach(i=>{const o=this.findRoomNearPosition(t.area,t.z,i.x,-i.y,e);o&&o.id!==t.id&&s.set(o.id,o)})}),s}findRoomNearPosition(t,e,s,r,i){const o=l.roomSize*1.5;let n;return i.forEach(h=>{if(h.area!==t||h.z!==e)return;const c=Math.hypot(h.x-s,h.y-r);c<=o&&(!n||c<n.distance)&&(n={room:h,distance:c})}),n==null?void 0:n.room}disableListening(t){t.listening(!1),"getChildren"in t&&typeof t.getChildren=="function"&&t.getChildren().forEach(e=>this.disableListening(e))}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:l.roomSize,height:l.roomSize});e.add(s)}}renderExits(t){t.forEach(e=>{const s=this.exitRenderer.render(e);s&&this.linkLayer.add(s)})}renderLabels(t){t.forEach(e=>{if(!e.pixMap)return;const s=new Image;s.src=`data:image/png;base64,${e.pixMap}`;const r=new m.Image({x:e.X,y:-e.Y,width:e.Width,height:e.Height,image:s});this.linkLayer.add(r)})}}const V={rgbValue:"rgb(114, 1, 0)",symbolColor:[225,225,225]};function M(d){const t=d[0]/255,e=d[1]/255,s=d[2]/255,r=Math.max(t,e,s),i=Math.min(t,e,s);return(r+i)/2}class tt{constructor(t,e){this.rooms={},this.areas={},this.areaSources={},this.explorationEnabled=!1,this.colors={},t.forEach(s=>{s.rooms.forEach(i=>{i.y=-i.y,this.rooms[i.id]=i});const r=parseInt(s.areaId);this.areas[r]=new L(s),this.areaSources[r]=s}),this.colors=e.reduce((s,r)=>({...s,[r.envId]:{rgb:r.colors,rgbValue:`rgb(${r.colors.join(",")}`,symbolColor:M(r.colors)>.41?[25,25,25]:[225,255,255],symbolColorValue:M(r.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 k)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 k(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 L(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 r=this.getRoom(t);if(r){const i=this.getExplorationArea(r.area);if(i)return i.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 r of t){if(this.explorationEnabled){const o=this.getRoom(r);if(o){const n=this.getExplorationArea(o.area);if(n){n.addVisitedRoom(r)&&s++;continue}}}const i=e.has(r);e.add(r),i||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)??V.rgbValue}getSymbolColor(t,e){var o;const s=((o=this.colors[t])==null?void 0:o.symbolColor)??V.symbolColor,r=Math.min(Math.max(e??1,0),1),i=s.join(",");return r!=1?`rgba(${i}, ${r})`:`rgba(${i})`}}function et(d){return d&&d.__esModule&&Object.prototype.hasOwnProperty.call(d,"default")?d.default:d}class st{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(r=>(r.key===t&&Object.assign(r,{priority:s}),r)):(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 rt=st;function W(d,t){const e=new Map;for(const[s,r]of d)s!==t&&r instanceof Map?e.set(s,W(r,t)):s!==t&&e.set(s,r);return e}var it=W;function ot(d){const t=Number(d);return!(isNaN(t)||t<=0)}function X(d){const t=new Map;return Object.keys(d).forEach(s=>{const r=d[s];if(r!==null&&typeof r=="object"&&!Array.isArray(r))return t.set(s,X(r));if(!ot(r))throw new Error(`Could not add node at key "${s}", make sure it's a valid node`,r);return t.set(s,Number(r))}),t}var nt=X;function $(d){if(!(d instanceof Map))throw new Error(`Invalid graph: Expected Map instead found ${typeof d}`);d.forEach((t,e)=>{if(typeof t=="object"&&t instanceof Map){$(t);return}if(typeof t!="number"||t<=0)throw new Error(`Values must be numbers greater than 0. Found value ${t} at ${e}`)})}var at=$;const ht=rt,ct=it,A=nt,T=at;class lt{constructor(t){t instanceof Map?(T(t),this.graph=t):t?this.graph=A(t):this.graph=new Map}addNode(t,e){let s;return e instanceof Map?(T(e),s=e):s=A(e),this.graph.set(t,s),this}addVertex(t,e){return this.addNode(t,e)}removeNode(t){return this.graph=ct(this.graph,t),this}path(t,e,s={}){if(!this.graph.size)return s.cost?{path:null,cost:0}:null;const r=new Set,i=new ht,o=new Map;let n=[],h=0,c=[];if(s.avoid&&(c=[].concat(s.avoid)),c.includes(t))throw new Error(`Starting node (${t}) cannot be avoided`);if(c.includes(e))throw new Error(`Ending node (${e}) cannot be avoided`);for(i.set(t,0);!i.isEmpty();){const u=i.next();if(u.key===e){h=u.priority;let g=u.key;for(;o.has(g);)n.push(g),g=o.get(g);break}r.add(u.key),(this.graph.get(u.key)||new Map).forEach((g,f)=>{if(r.has(f)||c.includes(f))return null;if(!i.has(f))return o.set(f,u.key),i.set(f,u.priority+g);const y=i.get(f).priority,w=u.priority+g;return w<y?(o.set(f,u.key),i.set(f,w)):null})}return n.length?(s.trim?n.shift():n=n.concat([t]),s.reverse||(n=n.reverse()),s.cost?{path:n,cost:h}:n):s.cost?{path:null,cost:0}:null}shortestPath(...t){return this.path(...t)}}var ut=lt;const dt=et(ut),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"};class ft{constructor(t){this.cache=new Map,this.mapReader=t,this.graph=this.buildGraph()}buildGraph(){const t={};return this.mapReader.getRooms().forEach(e=>{const s={},r=new Set((e.exitLocks??[]).map(o=>gt[o]).filter(o=>!!o)),i=new Set(e.mSpecialExitLocks??[]);Object.entries(e.exits??{}).forEach(([o,n])=>{r.has(o)||this.mapReader.getRoom(n)&&(s[n.toString()]=1)}),Object.values(e.specialExits??{}).forEach(o=>{i.has(o)||this.mapReader.getRoom(o)&&(s[o.toString()]=1)}),t[e.id.toString()]=s}),new dt(t)}findPath(t,e){const s=`${t}->${e}`;if(this.cache.has(s))return this.cache.get(s);if(t===e){const n=this.mapReader.getRoom(t)?[t]:null;return this.cache.set(s,n),n}if(!this.mapReader.getRoom(t)||!this.mapReader.getRoom(e))return this.cache.set(s,null),null;const r=this.graph.path(t.toString(),e.toString()),i=Array.isArray(r)?r:r==null?void 0:r.path,o=i?i.map(n=>Number(n)):null;return this.cache.set(s,o),o}}exports.ExplorationArea=k;exports.MapReader=tt;exports.PathFinder=ft;exports.Renderer=U;exports.Settings=l;
2
2
  //# sourceMappingURL=index.cjs.map