mudlet-map-renderer 0.3.2-konva → 0.3.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.
@@ -43,7 +43,7 @@ export declare class Renderer {
43
43
  getZoom(): number;
44
44
  getCurrentArea(): Area | undefined;
45
45
  setPosition(roomId: number): void;
46
- renderPath(locations: number[]): import('konva/lib/shapes/Line').Line<{
46
+ renderPath(locations: number[], color?: string): import('konva/lib/shapes/Line').Line<{
47
47
  points: number[];
48
48
  stroke: string;
49
49
  strokeWidth: number;
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("konva"),j=["north","south","east","west","northeast","northwest","southeast","southwest"],S={north:"n",south:"s",east:"e",west:"w",northeast:"ne",northwest:"nw",southeast:"se",southwest:"sw",up:"u",down:"d",in:"i",out:"o"},T={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}},O=["north","south","east","west","northeast","northwest","southeast","southwest"],_={north:"south",south:"north",east:"west",west:"east",northeast:"southwest",northwest:"southeast",southeast:"northwest",southwest:"northeast"};function F(h){return h?Object.prototype.hasOwnProperty.call(T,h):!1}function f(h,t,e,s=1){if(!F(e))return{x:h,y:t};const r=T[e];return{x:h+r.x*s,y:t+r.y*s}}const v={OPEN_DOOR:"rgb(10, 155, 10)",CLOSED_DOOR:"rgb(226, 205, 59)",LOCKED_DOOR:"rgb(155, 10, 10)"},G={1:"north",2:"northeast",3:"northwest",4:"east",5:"west",6:"south",7:"southeast",8:"southwest",9:"up",10:"down",11:"in",12:"out"},B=["up","down","in","out"];function H(h){switch(h){case 1:return v.OPEN_DOOR;case 2:return v.CLOSED_DOOR;default:return v.LOCKED_DOOR}}class q{constructor(t){this.mapReader=t}render(t){return t.aDir&&t.bDir?this.renderTwoWayExit(t):this.renderOneWayExit(t)}renderTwoWayExit(t){const e=this.mapReader.getRoom(t.a),s=this.mapReader.getRoom(t.b);if(!e||!s||!t.aDir||!t.bDir)return;const r=new p.Group,i=[];if(i.push(...Object.values(f(e.x,e.y,t.aDir,d.roomSize/2))),i.push(...Object.values(f(s.x,s.y,t.bDir,d.roomSize/2))),e.doors[S[t.aDir]]||s.doors[S[t.bDir]]){const o=this.renderDoor(i,e.doors[S[t.aDir]]??s.doors[S[t.bDir]]);r.add(o)}const n=new p.Line({points:i,stroke:d.lineColor,strokeWidth:.025});return r.add(n),r}renderOneWayExit(t){const e=t.aDir?this.mapReader.getRoom(t.a):this.mapReader.getRoom(t.b),s=t.aDir?this.mapReader.getRoom(t.b):this.mapReader.getRoom(t.a),r=t.aDir?t.aDir:t.bDir;if(!e||!s)return;if(e.area!=s.area&&r)return this.renderAreaExit(e,r);let i={x:s.x,y:s.y};(s.area!==e.area||s.z!==e.z)&&(i=f(e.x,e.y,r,d.roomSize/2));const n=f(e.x,e.y,r,.3),o=n.x-(n.x-i.x)/2,l=n.y-(n.y-i.y)/2,g=new p.Group,u=[];u.push(...Object.values(f(e.x,e.y,r,d.roomSize/2))),u.push(i.x,i.y);const a=new p.Line({points:u,stroke:d.lineColor,strokeWidth:.025,dashEnabled:!0,dash:[.1,.05]});g.add(a);const c=new p.Arrow({points:[u[0],u[1],o,l],pointerLength:.5,pointerWidth:.35,strokeWidth:.035,stroke:d.lineColor,fill:"#FF0000",dashEnabled:!0,dash:[.1,.05]});return g.add(c),g}renderAreaExit(t,e){const s=f(t.x,t.y,e,d.roomSize/2),r=f(t.x,t.y,e,d.roomSize*1.5);return new p.Arrow({points:[s.x,s.y,r.x,r.y],pointerLength:.3,pointerWidth:.3,strokeWidth:.035,stroke:this.mapReader.getColorValue(t.env),fill:this.mapReader.getColorValue(t.env)})}renderSpecialExits(t){return Object.entries(t.customLines).map(([e,s])=>{const r=[t.x,t.y];s.points.reduce((l,g)=>(l.push(g.x,-g.y),l),r);const i=s.attributes.arrow?p.Arrow:p.Line,n=new i({points:r,strokeWidth:.025,stroke:`rgb(${s.attributes.color.r}, ${s.attributes.color.g}, ${s.attributes.color.b})`,fill:`rgb(${s.attributes.color.r}, ${s.attributes.color.g} , ${s.attributes.color.b})`,pointerLength:.3,pointerWidth:.2});let o=s.attributes.style;return o==="dot line"?(n.dash([.05,.05]),n.dashOffset(.1)):o==="dash line"?n.dash([.4,.2]):o==="solid line"||o!==void 0&&console.log("Brak opisu stylu: "+o),n})}renderStubs(t){return t.stubs.map(e=>{const s=G[e],r=f(t.x,t.y,s,d.roomSize/2),i=f(t.x,t.y,s,d.roomSize/2+.5),n=[r.x,r.y,i.x,i.y];return new p.Line({points:n,stroke:d.lineColor,strokeWidth:.025})})}renderInnerExits(t){return B.map(e=>{if(t.exits[e]){const s=new p.Group,r=new p.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:d.roomSize/5,scaleX:1.4,scaleY:.8});s.add(r);let i=t.doors[e];if(i!==void 0)switch(i){case 1:r.stroke(v.OPEN_DOOR);break;case 2:r.stroke(v.CLOSED_DOOR);break;default:r.stroke(v.LOCKED_DOOR)}switch(e){case"up":r.position(f(t.x,t.y,"south",d.roomSize/4));break;case"down":r.rotation(180),r.position(f(t.x,t.y,"north",d.roomSize/4));break;case"in":const n=r.clone();n.rotation(-90),n.position(f(t.x,t.y,"east",d.roomSize/4)),s.add(n),r.rotation(90),r.position(f(t.x,t.y,"west",d.roomSize/4));break;case"out":const o=r.clone();o.rotation(90),o.position(f(t.x,t.y,"east",d.roomSize/4)),s.add(o),r.rotation(-90),r.position(f(t.x,t.y,"west",d.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 p.Rect({x:s.x-d.roomSize/4,y:s.y-d.roomSize/4,width:d.roomSize/2,height:d.roomSize/2,stroke:H(e),strokeWidth:.025})}}class K{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 l=()=>{o&&(o.length<4&&n.pop(),o=null)},g=()=>(o||(o=[],n.push(o)),o);i.forEach((a,c)=>{if(!this.isRoomVisible(a,e,s))return;const m=c>0?i[c-1]:void 0,y=c<i.length-1?i[c+1]:void 0;if(this.isRoomVisible(m,e,s))g();else{l();const b=g();if(m){const x=this.getDirectionTowards(a,m);if(x){const k=f(a.x,a.y,x,d.roomSize);b.push(k.x,k.y)}}}if(o==null||o.push(a.x,a.y),!this.isRoomVisible(y,e,s)&&y){const b=this.getDirectionTowards(a,y);if(b){const x=f(a.x,a.y,b,d.roomSize);o==null||o.push(x.x,x.y)}l()}}),l();const u=n.filter(a=>a.length>=4).map(a=>new p.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 O)if(t.exits[s]===e.id)return s;for(const s of O)if(e.exits[s]===t.id)return _[s]}}const A=.6,E=75,Q="rgb(225, 255, 225)",D=class D{};D.roomSize=A,D.lineColor=Q,D.instantMapMove=!1;let d=D;class J{constructor(t,e){this.highlights=new Map,this.currentZoom=1,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 q(e),this.pathRenderer=new K(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,r=!1;this.stage.on("touchend touchcancel",()=>{e=void 0,s=void 0}),this.stage.on("wheel",i=>{i.evt.preventDefault();const n=this.stage.scaleX(),o=this.stage.getPointerPosition();if(!o)return;const l={x:(o.x-this.stage.x())/n,y:(o.y-this.stage.y())/n};let g=i.evt.deltaY>0?-1:1;i.evt.ctrlKey&&(g=-g);const u=g>0?this.currentZoom*t:this.currentZoom/t,a=u*E,c=this.setZoom(u),m={x:o.x-l.x*a,y:o.y-l.y*a};this.stage.position(m),c&&this.emitZoomChangeEvent()}),this.stage.on("touchmove",i=>{const n=i.evt.touches,o=n==null?void 0:n[0],l=n==null?void 0:n[1];if(o&&!l&&r&&!this.stage.isDragging()&&(this.stage.startDrag(),r=!1),!o||!l){e=void 0,s=void 0;return}i.evt.preventDefault(),this.stage.isDragging()&&(this.stage.stopDrag(),r=!0);const g=this.stage.container().getBoundingClientRect(),u={x:o.clientX-g.left,y:o.clientY-g.top},a={x:l.clientX-g.left,y:l.clientY-g.top},c={x:(u.x+a.x)/2,y:(u.y+a.y)/2},m=Math.hypot(u.x-a.x,u.y-a.y);if(s===void 0&&(s=c),e===void 0){e=m;return}if(e===0)return;const y=this.stage.scaleX(),w=this.currentZoom*(m/e),R={x:(c.x-this.stage.x())/y,y:(c.y-this.stage.y())/y},b=this.setZoom(w),x=this.stage.scaleX(),k=c.x-s.x,W=c.y-s.y,Z={x:c.x-R.x*x+k,y:c.y-R.y*x+W};this.stage.position(Z),this.stage.batchDraw(),e=m,s=c,b&&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.roomLayer.destroyChildren(),this.linkLayer.destroyChildren(),this.stage.scale({x:E*this.currentZoom,y:E*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:E*t,y:E*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:A*.85,stroke:"rgb(0, 229, 178)",strokeWidth:.1,dash:[.05,.05],dashEnabled:!0}),this.positionLayer.add(this.positionRender)),this.centerOnRoom(e,i)}renderPath(t){return this.pathRenderer.renderPath(t,this.currentArea,this.currentZIndex)}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:d.roomSize*.9,stroke:e,strokeWidth:.15,dash:[.1,.05],dashEnabled:!0,listening:!1})}centerOnRoom(t,e=!1){var g;this.currentRoomId=t.id;const s={x:t.x,y:t.y};(g=this.positionRender)==null||g.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,l=n.y-i.y;this.currentTransition&&(this.currentTransition.pause(),this.currentTransition.destroy(),delete this.currentTransition),console.log(d.instantMapMove),e||d.instantMapMove?this.stage.position({x:this.stage.x()+o,y:this.stage.y()+l}):(this.currentTransition=new p.Tween({node:this.stage,x:this.stage.x()+o,y:this.stage.y()+l,duration:.2,easing:p.Easings.EaseInOut}),this.currentTransition.play())}renderRooms(t){t.forEach(e=>{const s=new p.Group({x:e.x-d.roomSize/2,y:e.y-d.roomSize/2}),r=new p.Rect({x:0,y:0,width:d.roomSize,height:d.roomSize,fill:this.mapReader.getColorValue(e.env),strokeWidth:.025,stroke:d.lineColor}),i=(a,c)=>this.emitRoomContextEvent(e.id,a,c);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 c=a.evt;i(c.clientX,c.clientY)});let n,o,l;const g=()=>{l!==void 0&&(this.stage.draggable(l),l=void 0)},u=()=>{n!==void 0&&(window.clearTimeout(n),n=void 0),o=void 0,g()};s.on("touchstart",a=>{var m;if(u(),a.evt.touches&&a.evt.touches.length>1)return;const c=(m=a.evt.touches)==null?void 0:m[0];c&&(o={clientX:c.clientX,clientY:c.clientY},l=this.stage.draggable(),this.stage.draggable(!1),n=window.setTimeout(()=>{o&&i(o.clientX,o.clientY),u()},500))}),s.on("touchend",u),s.on("touchmove",a=>{var b;if(!o)return;const c=(b=a.evt.touches)==null?void 0:b[0];if(!c){u();return}const m=c.clientX-o.clientX,y=c.clientY-o.clientY,w=m*m+y*y,R=10;if(w>R*R){const x=l;u(),x&&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)})})}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:d.roomSize,height:d.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 N{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 P{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 N(r,this.area.labels.filter(i=>i.Z===+s)),e),{})}createExits(){this.area.rooms.forEach(t=>{Object.entries(t.exits).filter(([e,s])=>j.indexOf(e)>-1&&!t.customLines.hasOwnProperty(S[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 l=this.exits.get(o);l||(l={a:i,b:n,zIndex:[s]}),i==t?l.aDir=r:l.bDir=r,l.zIndex.push(s),this.exits.set(o,l)}}class C extends N{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 L extends P{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 C(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 C(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 I={rgbValue:"rgb(114, 1, 0)",symbolColor:[225,225,225]};function V(h){const t=h[0]/255,e=h[1]/255,s=h[2]/255,r=Math.max(t,e,s),i=Math.min(t,e,s);return(r+i)/2}class U{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 P(s),this.areaSources[r]=s}),this.colors=e.reduce((s,r)=>({...s,[r.envId]:{rgb:r.colors,rgbValue:`rgb(${r.colors.join(",")}`,symbolColor:V(r.colors)>.41?[25,25,25]:[225,255,255],symbolColorValue:V(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 L)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 L(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 P(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)??I.rgbValue}getSymbolColor(t,e){var n;const s=((n=this.colors[t])==null?void 0:n.symbolColor)??I.symbolColor,r=Math.min(Math.max(e??1,0),1),i=s.join(",");return r!=1?`rgba(${i}, ${r})`:`rgba(${i})`}}function tt(h){return h&&h.__esModule&&Object.prototype.hasOwnProperty.call(h,"default")?h.default:h}class et{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 st=et;function Y(h,t){const e=new Map;for(const[s,r]of h)s!==t&&r instanceof Map?e.set(s,Y(r,t)):s!==t&&e.set(s,r);return e}var rt=Y;function it(h){const t=Number(h);return!(isNaN(t)||t<=0)}function X(h){const t=new Map;return Object.keys(h).forEach(s=>{const r=h[s];if(r!==null&&typeof r=="object"&&!Array.isArray(r))return t.set(s,X(r));if(!it(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 ot=X;function $(h){if(!(h instanceof Map))throw new Error(`Invalid graph: Expected Map instead found ${typeof h}`);h.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 nt=$;const at=st,ht=rt,z=ot,M=nt;class ct{constructor(t){t instanceof Map?(M(t),this.graph=t):t?this.graph=z(t):this.graph=new Map}addNode(t,e){let s;return e instanceof Map?(M(e),s=e):s=z(e),this.graph.set(t,s),this}addVertex(t,e){return this.addNode(t,e)}removeNode(t){return this.graph=ht(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 at,n=new Map;let o=[],l=0,g=[];if(s.avoid&&(g=[].concat(s.avoid)),g.includes(t))throw new Error(`Starting node (${t}) cannot be avoided`);if(g.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){l=u.priority;let c=u.key;for(;n.has(c);)o.push(c),c=n.get(c);break}r.add(u.key),(this.graph.get(u.key)||new Map).forEach((c,m)=>{if(r.has(m)||g.includes(m))return null;if(!i.has(m))return n.set(m,u.key),i.set(m,u.priority+c);const y=i.get(m).priority,w=u.priority+c;return w<y?(n.set(m,u.key),i.set(m,w)):null})}return o.length?(s.trim?o.shift():o=o.concat([t]),s.reverse||(o=o.reverse()),s.cost?{path:o,cost:l}:o):s.cost?{path:null,cost:0}:null}shortestPath(...t){return this.path(...t)}}var dt=ct;const lt=tt(dt),ut={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 gt{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=>ut[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 lt(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=L;exports.MapReader=U;exports.PathFinder=gt;exports.Renderer=J;exports.Settings=d;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("konva"),j=["north","south","east","west","northeast","northwest","southeast","southwest"],S={north:"n",south:"s",east:"e",west:"w",northeast:"ne",northwest:"nw",southeast:"se",southwest:"sw",up:"u",down:"d",in:"i",out:"o"},T={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}},O=["north","south","east","west","northeast","northwest","southeast","southwest"],_={north:"south",south:"north",east:"west",west:"east",northeast:"southwest",northwest:"southeast",southeast:"northwest",southwest:"northeast"};function F(h){return h?Object.prototype.hasOwnProperty.call(T,h):!1}function f(h,t,e,s=1){if(!F(e))return{x:h,y:t};const r=T[e];return{x:h+r.x*s,y:t+r.y*s}}const v={OPEN_DOOR:"rgb(10, 155, 10)",CLOSED_DOOR:"rgb(226, 205, 59)",LOCKED_DOOR:"rgb(155, 10, 10)"},G={1:"north",2:"northeast",3:"northwest",4:"east",5:"west",6:"south",7:"southeast",8:"southwest",9:"up",10:"down",11:"in",12:"out"},B=["up","down","in","out"];function H(h){switch(h){case 1:return v.OPEN_DOOR;case 2:return v.CLOSED_DOOR;default:return v.LOCKED_DOOR}}class q{constructor(t){this.mapReader=t}render(t){return t.aDir&&t.bDir?this.renderTwoWayExit(t):this.renderOneWayExit(t)}renderTwoWayExit(t){const e=this.mapReader.getRoom(t.a),s=this.mapReader.getRoom(t.b);if(!e||!s||!t.aDir||!t.bDir)return;const r=new p.Group,i=[];if(i.push(...Object.values(f(e.x,e.y,t.aDir,d.roomSize/2))),i.push(...Object.values(f(s.x,s.y,t.bDir,d.roomSize/2))),e.doors[S[t.aDir]]||s.doors[S[t.bDir]]){const o=this.renderDoor(i,e.doors[S[t.aDir]]??s.doors[S[t.bDir]]);r.add(o)}const n=new p.Line({points:i,stroke:d.lineColor,strokeWidth:.025});return r.add(n),r}renderOneWayExit(t){const e=t.aDir?this.mapReader.getRoom(t.a):this.mapReader.getRoom(t.b),s=t.aDir?this.mapReader.getRoom(t.b):this.mapReader.getRoom(t.a),r=t.aDir?t.aDir:t.bDir;if(!e||!s)return;if(e.area!=s.area&&r)return this.renderAreaExit(e,r);let i={x:s.x,y:s.y};(s.area!==e.area||s.z!==e.z)&&(i=f(e.x,e.y,r,d.roomSize/2));const n=f(e.x,e.y,r,.3),o=n.x-(n.x-i.x)/2,l=n.y-(n.y-i.y)/2,g=new p.Group,u=[];u.push(...Object.values(f(e.x,e.y,r,d.roomSize/2))),u.push(i.x,i.y);const a=new p.Line({points:u,stroke:d.lineColor,strokeWidth:.025,dashEnabled:!0,dash:[.1,.05]});g.add(a);const c=new p.Arrow({points:[u[0],u[1],o,l],pointerLength:.5,pointerWidth:.35,strokeWidth:.035,stroke:d.lineColor,fill:"#FF0000",dashEnabled:!0,dash:[.1,.05]});return g.add(c),g}renderAreaExit(t,e){const s=f(t.x,t.y,e,d.roomSize/2),r=f(t.x,t.y,e,d.roomSize*1.5);return new p.Arrow({points:[s.x,s.y,r.x,r.y],pointerLength:.3,pointerWidth:.3,strokeWidth:.035,stroke:this.mapReader.getColorValue(t.env),fill:this.mapReader.getColorValue(t.env)})}renderSpecialExits(t){return Object.entries(t.customLines).map(([e,s])=>{const r=[t.x,t.y];s.points.reduce((l,g)=>(l.push(g.x,-g.y),l),r);const i=s.attributes.arrow?p.Arrow:p.Line,n=new i({points:r,strokeWidth:.025,stroke:`rgb(${s.attributes.color.r}, ${s.attributes.color.g}, ${s.attributes.color.b})`,fill:`rgb(${s.attributes.color.r}, ${s.attributes.color.g} , ${s.attributes.color.b})`,pointerLength:.3,pointerWidth:.2});let o=s.attributes.style;return o==="dot line"?(n.dash([.05,.05]),n.dashOffset(.1)):o==="dash line"?n.dash([.4,.2]):o==="solid line"||o!==void 0&&console.log("Brak opisu stylu: "+o),n})}renderStubs(t){return t.stubs.map(e=>{const s=G[e],r=f(t.x,t.y,s,d.roomSize/2),i=f(t.x,t.y,s,d.roomSize/2+.5),n=[r.x,r.y,i.x,i.y];return new p.Line({points:n,stroke:d.lineColor,strokeWidth:.025})})}renderInnerExits(t){return B.map(e=>{if(t.exits[e]){const s=new p.Group,r=new p.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:d.roomSize/5,scaleX:1.4,scaleY:.8});s.add(r);let i=t.doors[e];if(i!==void 0)switch(i){case 1:r.stroke(v.OPEN_DOOR);break;case 2:r.stroke(v.CLOSED_DOOR);break;default:r.stroke(v.LOCKED_DOOR)}switch(e){case"up":r.position(f(t.x,t.y,"south",d.roomSize/4));break;case"down":r.rotation(180),r.position(f(t.x,t.y,"north",d.roomSize/4));break;case"in":const n=r.clone();n.rotation(-90),n.position(f(t.x,t.y,"east",d.roomSize/4)),s.add(n),r.rotation(90),r.position(f(t.x,t.y,"west",d.roomSize/4));break;case"out":const o=r.clone();o.rotation(90),o.position(f(t.x,t.y,"east",d.roomSize/4)),s.add(o),r.rotation(-90),r.position(f(t.x,t.y,"west",d.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 p.Rect({x:s.x-d.roomSize/4,y:s.y-d.roomSize/4,width:d.roomSize/2,height:d.roomSize/2,stroke:H(e),strokeWidth:.025})}}class K{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 l=()=>{o&&(o.length<4&&n.pop(),o=null)},g=()=>(o||(o=[],n.push(o)),o);i.forEach((a,c)=>{if(!this.isRoomVisible(a,e,s))return;const m=c>0?i[c-1]:void 0,y=c<i.length-1?i[c+1]:void 0;if(this.isRoomVisible(m,e,s))g();else{l();const b=g();if(m){const x=this.getDirectionTowards(a,m);if(x){const k=f(a.x,a.y,x,d.roomSize);b.push(k.x,k.y)}}}if(o==null||o.push(a.x,a.y),!this.isRoomVisible(y,e,s)&&y){const b=this.getDirectionTowards(a,y);if(b){const x=f(a.x,a.y,b,d.roomSize);o==null||o.push(x.x,x.y)}l()}}),l();const u=n.filter(a=>a.length>=4).map(a=>new p.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 O)if(t.exits[s]===e.id)return s;for(const s of O)if(e.exits[s]===t.id)return _[s]}}const A=.6,E=75,Q="rgb(225, 255, 225)",D=class D{};D.roomSize=A,D.lineColor=Q,D.instantMapMove=!1;let d=D;class J{constructor(t,e){this.highlights=new Map,this.currentZoom=1,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 q(e),this.pathRenderer=new K(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,r=!1;this.stage.on("touchend touchcancel",()=>{e=void 0,s=void 0}),this.stage.on("wheel",i=>{i.evt.preventDefault();const n=this.stage.scaleX(),o=this.stage.getPointerPosition();if(!o)return;const l={x:(o.x-this.stage.x())/n,y:(o.y-this.stage.y())/n};let g=i.evt.deltaY>0?-1:1;i.evt.ctrlKey&&(g=-g);const u=g>0?this.currentZoom*t:this.currentZoom/t,a=u*E,c=this.setZoom(u),m={x:o.x-l.x*a,y:o.y-l.y*a};this.stage.position(m),c&&this.emitZoomChangeEvent()}),this.stage.on("touchmove",i=>{const n=i.evt.touches,o=n==null?void 0:n[0],l=n==null?void 0:n[1];if(o&&!l&&r&&!this.stage.isDragging()&&(this.stage.startDrag(),r=!1),!o||!l){e=void 0,s=void 0;return}i.evt.preventDefault(),this.stage.isDragging()&&(this.stage.stopDrag(),r=!0);const g=this.stage.container().getBoundingClientRect(),u={x:o.clientX-g.left,y:o.clientY-g.top},a={x:l.clientX-g.left,y:l.clientY-g.top},c={x:(u.x+a.x)/2,y:(u.y+a.y)/2},m=Math.hypot(u.x-a.x,u.y-a.y);if(s===void 0&&(s=c),e===void 0){e=m;return}if(e===0)return;const y=this.stage.scaleX(),w=this.currentZoom*(m/e),R={x:(c.x-this.stage.x())/y,y:(c.y-this.stage.y())/y},b=this.setZoom(w),x=this.stage.scaleX(),k=c.x-s.x,W=c.y-s.y,Z={x:c.x-R.x*x+k,y:c.y-R.y*x+W};this.stage.position(Z),this.stage.batchDraw(),e=m,s=c,b&&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.roomLayer.destroyChildren(),this.linkLayer.destroyChildren(),this.stage.scale({x:E*this.currentZoom,y:E*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:E*t,y:E*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:A*.85,stroke:"rgb(0, 229, 178)",strokeWidth:.1,dash:[.05,.05],dashEnabled:!0}),this.positionLayer.add(this.positionRender)),this.centerOnRoom(e,i)}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:d.roomSize*.9,stroke:e,strokeWidth:.15,dash:[.1,.05],dashEnabled:!0,listening:!1})}centerOnRoom(t,e=!1){var g;this.currentRoomId=t.id;const s={x:t.x,y:t.y};(g=this.positionRender)==null||g.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,l=n.y-i.y;this.currentTransition&&(this.currentTransition.pause(),this.currentTransition.destroy(),delete this.currentTransition),console.log(d.instantMapMove),e||d.instantMapMove?this.stage.position({x:this.stage.x()+o,y:this.stage.y()+l}):(this.currentTransition=new p.Tween({node:this.stage,x:this.stage.x()+o,y:this.stage.y()+l,duration:.2,easing:p.Easings.EaseInOut}),this.currentTransition.play())}renderRooms(t){t.forEach(e=>{const s=new p.Group({x:e.x-d.roomSize/2,y:e.y-d.roomSize/2}),r=new p.Rect({x:0,y:0,width:d.roomSize,height:d.roomSize,fill:this.mapReader.getColorValue(e.env),strokeWidth:.025,stroke:d.lineColor}),i=(a,c)=>this.emitRoomContextEvent(e.id,a,c);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 c=a.evt;i(c.clientX,c.clientY)});let n,o,l;const g=()=>{l!==void 0&&(this.stage.draggable(l),l=void 0)},u=()=>{n!==void 0&&(window.clearTimeout(n),n=void 0),o=void 0,g()};s.on("touchstart",a=>{var m;if(u(),a.evt.touches&&a.evt.touches.length>1)return;const c=(m=a.evt.touches)==null?void 0:m[0];c&&(o={clientX:c.clientX,clientY:c.clientY},l=this.stage.draggable(),this.stage.draggable(!1),n=window.setTimeout(()=>{o&&i(o.clientX,o.clientY),u()},500))}),s.on("touchend",u),s.on("touchmove",a=>{var b;if(!o)return;const c=(b=a.evt.touches)==null?void 0:b[0];if(!c){u();return}const m=c.clientX-o.clientX,y=c.clientY-o.clientY,w=m*m+y*y,R=10;if(w>R*R){const x=l;u(),x&&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)})})}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:d.roomSize,height:d.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 N{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 P{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 N(r,this.area.labels.filter(i=>i.Z===+s)),e),{})}createExits(){this.area.rooms.forEach(t=>{Object.entries(t.exits).filter(([e,s])=>j.indexOf(e)>-1&&!t.customLines.hasOwnProperty(S[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 l=this.exits.get(o);l||(l={a:i,b:n,zIndex:[s]}),i==t?l.aDir=r:l.bDir=r,l.zIndex.push(s),this.exits.set(o,l)}}class C extends N{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 L extends P{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 C(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 C(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 I={rgbValue:"rgb(114, 1, 0)",symbolColor:[225,225,225]};function V(h){const t=h[0]/255,e=h[1]/255,s=h[2]/255,r=Math.max(t,e,s),i=Math.min(t,e,s);return(r+i)/2}class U{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 P(s),this.areaSources[r]=s}),this.colors=e.reduce((s,r)=>({...s,[r.envId]:{rgb:r.colors,rgbValue:`rgb(${r.colors.join(",")}`,symbolColor:V(r.colors)>.41?[25,25,25]:[225,255,255],symbolColorValue:V(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 L)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 L(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 P(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)??I.rgbValue}getSymbolColor(t,e){var n;const s=((n=this.colors[t])==null?void 0:n.symbolColor)??I.symbolColor,r=Math.min(Math.max(e??1,0),1),i=s.join(",");return r!=1?`rgba(${i}, ${r})`:`rgba(${i})`}}function tt(h){return h&&h.__esModule&&Object.prototype.hasOwnProperty.call(h,"default")?h.default:h}class et{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 st=et;function Y(h,t){const e=new Map;for(const[s,r]of h)s!==t&&r instanceof Map?e.set(s,Y(r,t)):s!==t&&e.set(s,r);return e}var rt=Y;function it(h){const t=Number(h);return!(isNaN(t)||t<=0)}function X(h){const t=new Map;return Object.keys(h).forEach(s=>{const r=h[s];if(r!==null&&typeof r=="object"&&!Array.isArray(r))return t.set(s,X(r));if(!it(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 ot=X;function $(h){if(!(h instanceof Map))throw new Error(`Invalid graph: Expected Map instead found ${typeof h}`);h.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 nt=$;const at=st,ht=rt,z=ot,M=nt;class ct{constructor(t){t instanceof Map?(M(t),this.graph=t):t?this.graph=z(t):this.graph=new Map}addNode(t,e){let s;return e instanceof Map?(M(e),s=e):s=z(e),this.graph.set(t,s),this}addVertex(t,e){return this.addNode(t,e)}removeNode(t){return this.graph=ht(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 at,n=new Map;let o=[],l=0,g=[];if(s.avoid&&(g=[].concat(s.avoid)),g.includes(t))throw new Error(`Starting node (${t}) cannot be avoided`);if(g.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){l=u.priority;let c=u.key;for(;n.has(c);)o.push(c),c=n.get(c);break}r.add(u.key),(this.graph.get(u.key)||new Map).forEach((c,m)=>{if(r.has(m)||g.includes(m))return null;if(!i.has(m))return n.set(m,u.key),i.set(m,u.priority+c);const y=i.get(m).priority,w=u.priority+c;return w<y?(n.set(m,u.key),i.set(m,w)):null})}return o.length?(s.trim?o.shift():o=o.concat([t]),s.reverse||(o=o.reverse()),s.cost?{path:o,cost:l}:o):s.cost?{path:null,cost:0}:null}shortestPath(...t){return this.path(...t)}}var dt=ct;const lt=tt(dt),ut={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 gt{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=>ut[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 lt(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=L;exports.MapReader=U;exports.PathFinder=gt;exports.Renderer=J;exports.Settings=d;
2
2
  //# sourceMappingURL=index.cjs.map