mudlet-map-renderer 0.1.1-konva → 0.3.0-konva
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Renderer.d.ts +19 -2
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +589 -461
- package/dist/index.mjs.map +1 -1
- package/dist/reader/MapReader.d.ts +7 -1
- package/package.json +1 -1
package/dist/Renderer.d.ts
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
import { default as MapReader } from './reader/MapReader';
|
|
2
|
+
import { default as Area } from './reader/Area';
|
|
3
|
+
export type RoomContextMenuEventDetail = {
|
|
4
|
+
roomId: number;
|
|
5
|
+
position: {
|
|
6
|
+
x: number;
|
|
7
|
+
y: number;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
export type ZoomChangeEventDetail = {
|
|
11
|
+
zoom: number;
|
|
12
|
+
};
|
|
2
13
|
export declare class Settings {
|
|
3
14
|
static roomSize: number;
|
|
4
15
|
static lineColor: string;
|
|
16
|
+
static instantMapMove: false;
|
|
5
17
|
}
|
|
6
18
|
export declare class Renderer {
|
|
7
19
|
private readonly stage;
|
|
@@ -14,17 +26,22 @@ export declare class Renderer {
|
|
|
14
26
|
private pathRenderer;
|
|
15
27
|
private highlights;
|
|
16
28
|
private currentArea?;
|
|
29
|
+
private currentAreaInstance?;
|
|
17
30
|
private currentZIndex?;
|
|
18
31
|
private currentAreaVersion?;
|
|
32
|
+
private currentRoomId?;
|
|
19
33
|
private positionRender?;
|
|
20
34
|
private currentTransition?;
|
|
21
35
|
private currentZoom;
|
|
22
36
|
constructor(container: HTMLDivElement, mapReader: MapReader);
|
|
37
|
+
private onResize;
|
|
23
38
|
private initScaling;
|
|
24
39
|
drawArea(id: number, zIndex: number): void;
|
|
25
|
-
|
|
40
|
+
private emitRoomContextEvent;
|
|
41
|
+
private emitZoomChangeEvent;
|
|
42
|
+
setZoom(zoom: number): boolean;
|
|
26
43
|
getZoom(): number;
|
|
27
|
-
getCurrentArea():
|
|
44
|
+
getCurrentArea(): Area | undefined;
|
|
28
45
|
setPosition(roomId: number): void;
|
|
29
46
|
renderPath(locations: number[]): import('konva/lib/shapes/Line').Line<{
|
|
30
47
|
points: number[];
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("konva"),_=["north","south","east","west","northeast","northwest","southeast","southwest"],w={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}},L=["north","south","east","west","northeast","northwest","southeast","southwest"],j={north:"south",south:"north",east:"west",west:"east",northeast:"southwest",northwest:"southeast",southeast:"northwest",southwest:"northeast"};function X(a){return a?Object.prototype.hasOwnProperty.call(N,a):!1}function p(a,e,t,s=1){if(!X(t))return{x:a,y:e};const r=N[t];return{x:a+r.x*s,y:e+r.y*s}}const f={OPEN_DOOR:"rgb(10, 155, 10)",CLOSED_DOOR:"rgb(226, 205, 59)",LOCKED_DOOR:"rgb(155, 10, 10)"},F={1:"north",2:"northeast",3:"northwest",4:"east",5:"west",6:"south",7:"southeast",8:"southwest",9:"up",10:"down",11:"in",12:"out"},Z=["up","down","in","out"];function G(a){switch(a){case 1:return f.OPEN_DOOR;case 2:return f.CLOSED_DOOR;default:return f.LOCKED_DOOR}}class H{constructor(e){this.mapReader=e}render(e){return e.aDir&&e.bDir?this.renderTwoWayExit(e):this.renderOneWayExit(e)}renderTwoWayExit(e){const t=this.mapReader.getRoom(e.a),s=this.mapReader.getRoom(e.b);if(!t||!s||!e.aDir||!e.bDir)return;const r=new u.Group,i=[];if(i.push(...Object.values(p(t.x,t.y,e.aDir,c.roomSize/2))),i.push(...Object.values(p(s.x,s.y,e.bDir,c.roomSize/2))),t.doors[w[e.aDir]]||s.doors[w[e.bDir]]){const n=this.renderDoor(i,t.doors[w[e.aDir]]??s.doors[w[e.bDir]]);r.add(n)}const o=new u.Line({points:i,stroke:c.lineColor,strokeWidth:.025});return r.add(o),r}renderOneWayExit(e){const t=e.aDir?this.mapReader.getRoom(e.a):this.mapReader.getRoom(e.b),s=e.aDir?this.mapReader.getRoom(e.b):this.mapReader.getRoom(e.a),r=e.aDir?e.aDir:e.bDir;if(!t||!s)return;if(t.area!=s.area&&r)return this.renderAreaExit(t,r);let i={x:s.x,y:s.y};(s.area!==t.area||s.z!==t.z)&&(i=p(t.x,t.y,r,c.roomSize/2));const o=p(t.x,t.y,r,.3),n=o.x-(o.x-i.x)/2,d=o.y-(o.y-i.y)/2,l=new u.Group,h=[];h.push(...Object.values(p(t.x,t.y,r,c.roomSize/2))),h.push(i.x,i.y);const y=new u.Line({points:h,stroke:c.lineColor,strokeWidth:.025,dashEnabled:!0,dash:[.1,.05]});l.add(y);const g=new u.Arrow({points:[h[0],h[1],n,d],pointerLength:.5,pointerWidth:.35,strokeWidth:.035,stroke:c.lineColor,fill:"#FF0000",dashEnabled:!0,dash:[.1,.05]});return l.add(g),l}renderAreaExit(e,t){const s=p(e.x,e.y,t,c.roomSize/2),r=p(e.x,e.y,t,c.roomSize*1.5);return new u.Arrow({points:[s.x,s.y,r.x,r.y],pointerLength:.3,pointerWidth:.3,strokeWidth:.035,stroke:this.mapReader.getColorValue(e.env),fill:this.mapReader.getColorValue(e.env)})}renderSpecialExits(e){return Object.entries(e.customLines).map(([t,s])=>{const r=[e.x,e.y];s.points.reduce((d,l)=>(d.push(l.x,-l.y),d),r);const i=s.attributes.arrow?u.Arrow:u.Line,o=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 n=s.attributes.style;return n==="dot line"?(o.dash([.05,.05]),o.dashOffset(.1)):n==="dash line"?o.dash([.4,.2]):n==="solid line"||n!==void 0&&console.log("Brak opisu stylu: "+n),o})}renderStubs(e){return e.stubs.map(t=>{const s=F[t],r=p(e.x,e.y,s,c.roomSize/2),i=p(e.x,e.y,s,c.roomSize/2+.5),o=[r.x,r.y,i.x,i.y];return new u.Line({points:o,stroke:c.lineColor,strokeWidth:.025})})}renderInnerExits(e){return Z.map(t=>{if(e.exits[t]){const s=new u.Group,r=new u.RegularPolygon({x:e.x,y:e.y,sides:3,fill:this.mapReader.getSymbolColor(e.env,.6),stroke:this.mapReader.getSymbolColor(e.env),strokeWidth:.025,radius:c.roomSize/5,scaleX:1.4,scaleY:.8});s.add(r);let i=e.doors[t];if(i!==void 0)switch(i){case 1:r.stroke(f.OPEN_DOOR);break;case 2:r.stroke(f.CLOSED_DOOR);break;default:r.stroke(f.LOCKED_DOOR)}switch(t){case"up":r.position(p(e.x,e.y,"south",c.roomSize/4));break;case"down":r.rotation(180),r.position(p(e.x,e.y,"north",c.roomSize/4));break;case"in":const o=r.clone();o.rotation(-90),o.position(p(e.x,e.y,"east",c.roomSize/4)),s.add(o),r.rotation(90),r.position(p(e.x,e.y,"west",c.roomSize/4));break;case"out":const n=r.clone();n.rotation(90),n.position(p(e.x,e.y,"east",c.roomSize/4)),s.add(n),r.rotation(-90),r.position(p(e.x,e.y,"west",c.roomSize/4));break}return s}}).filter(t=>t!==void 0)}renderDoor(e,t){const s={x:e[0]+(e[2]-e[0])/2,y:e[1]+(e[3]-e[1])/2};return new u.Rect({x:s.x-c.roomSize/4,y:s.y-c.roomSize/4,width:c.roomSize/2,height:c.roomSize/2,stroke:G(t),strokeWidth:.025})}}class B{constructor(e,t){this.paths=[],this.mapReader=e,this.overlayLayer=t}renderPath(e,t,s){if(t===void 0||s===void 0)return;const r=e.map(h=>this.mapReader.getRoom(h)).filter(h=>h!==void 0),i=[];let o=null;const n=()=>{o&&(o.length<4&&i.pop(),o=null)},d=()=>(o||(o=[],i.push(o)),o);r.forEach((h,y)=>{if(!this.isRoomVisible(h,t,s))return;const g=y>0?r[y-1]:void 0,m=y<r.length-1?r[y+1]:void 0;if(this.isRoomVisible(g,t,s))d();else{n();const R=d();if(g){const b=this.getDirectionTowards(h,g);if(b){const O=p(h.x,h.y,b,c.roomSize);R.push(O.x,O.y)}}}if(o==null||o.push(h.x,h.y),!this.isRoomVisible(m,t,s)&&m){const R=this.getDirectionTowards(h,m);if(R){const b=p(h.x,h.y,R,c.roomSize);o==null||o.push(b.x,b.y)}n()}}),n();const l=i.filter(h=>h.length>=4).map(h=>new u.Line({points:h,stroke:"green",strokeWidth:.1}));return l.forEach(h=>{this.overlayLayer.add(h),this.paths.push(h)}),l[0]}clearPaths(){this.paths.forEach(e=>{e.destroy()}),this.paths=[]}isRoomVisible(e,t,s){return e?e.area===t&&e.z===s:!1}getDirectionTowards(e,t){for(const s of L)if(e.exits[s]===t.id)return s;for(const s of L)if(t.exits[s]===e.id)return j[s]}}const T=.6,P=1,x=75,q="rgb(225, 255, 225)",E=class E{};E.roomSize=T,E.lineColor=q;let c=E;class K{constructor(e,t){this.highlights=new Map,this.currentZoom=1,this.stage=new u.Stage({container:e,width:e.clientWidth,height:e.clientHeight,draggable:!0}),e.addEventListener("resize",()=>{this.stage.width(e.clientWidth),this.stage.height(e.clientHeight),this.stage.batchDraw()}),this.linkLayer=new u.Layer({listening:!1}),this.stage.add(this.linkLayer),this.roomLayer=new u.Layer,this.stage.add(this.roomLayer),this.overlayLayer=new u.Layer({listening:!1}),this.stage.add(this.overlayLayer),this.positionLayer=new u.Layer({listening:!1}),this.stage.add(this.positionLayer),this.mapReader=t,this.exitRenderer=new H(t),this.pathRenderer=new B(t,this.overlayLayer),this.initScaling(1.1)}initScaling(e){this.stage.on("wheel",t=>{t.evt.preventDefault();const s=this.stage.scaleX(),r=this.stage.getPointerPosition();if(!r)return;const i={x:(r.x-this.stage.x())/s,y:(r.y-this.stage.y())/s};let o=t.evt.deltaY>0?-1:1;t.evt.ctrlKey&&(o=-o);const n=o>0?this.currentZoom*e:this.currentZoom/e,d=n*x;this.setZoom(n);const l={x:r.x-i.x*d,y:r.y-i.y*d};this.stage.position(l)})}drawArea(e,t){const s=this.mapReader.getArea(e);if(!s)return;const r=s.getPlane(t);if(!r)return;this.currentArea=e,this.currentZIndex=t,this.currentAreaVersion=s.getVersion(),this.roomLayer.destroyChildren(),this.linkLayer.destroyChildren();const{minX:i,maxX:o,minY:n,maxY:d}=r.getBounds();this.stage.offset({x:i-P,y:n-P}),this.stage.scale({x:x*this.currentZoom,y:x*this.currentZoom}),this.renderLabels(r.getLabels()),this.renderRooms(r.getRooms()??[]),this.renderExits(s.getLinkExits(t)),this.refreshHighlights(),this.stage.batchDraw()}setZoom(e){this.currentZoom=e,this.stage.scale({x:x*e,y:x*e})}getZoom(){return this.currentZoom}getCurrentArea(){return this.currentArea?this.mapReader.getArea(this.currentArea):void 0}setPosition(e){const t=this.mapReader.getRoom(e);if(!t)return;const s=this.mapReader.getArea(t.area),r=s==null?void 0:s.getVersion();let i=this.currentArea!==t.area||this.currentZIndex!==t.z;(this.currentArea!==t.area||this.currentZIndex!==t.z||r!==void 0&&this.currentAreaVersion!==r)&&this.drawArea(t.area,t.z),this.positionRender||(this.positionRender=new u.Circle({x:t.x,y:t.y,radius:T*.85,stroke:"rgb(0, 229, 178)",strokeWidth:.1,dash:[.05,.05],dashEnabled:!0}),this.positionLayer.add(this.positionRender)),this.centerOnRoom(t,i)}renderPath(e){return this.pathRenderer.renderPath(e,this.currentArea,this.currentZIndex)}clearPaths(){this.pathRenderer.clearPaths()}renderHighlight(e,t){const s=this.mapReader.getRoom(e);if(!s)return;const r=this.highlights.get(e);r!=null&&r.shape&&(r.shape.destroy(),delete r.shape);const i={color:t,area:s.area,z:s.z};if(this.highlights.set(e,i),s.area===this.currentArea&&s.z===this.currentZIndex){const o=this.createHighlightShape(s,t);return this.overlayLayer.add(o),i.shape=o,this.overlayLayer.batchDraw(),o}return i.shape}clearHighlights(){this.highlights.forEach(({shape:e})=>e==null?void 0:e.destroy()),this.highlights.clear(),this.overlayLayer.batchDraw()}refreshHighlights(){this.highlights.forEach((e,t)=>{var i;if((i=e.shape)==null||i.destroy(),delete e.shape,e.area!==this.currentArea||e.z!==this.currentZIndex)return;const s=this.mapReader.getRoom(t);if(!s)return;const r=this.createHighlightShape(s,e.color);this.overlayLayer.add(r),e.shape=r}),this.overlayLayer.batchDraw()}createHighlightShape(e,t){return new u.Circle({x:e.x,y:e.y,radius:c.roomSize*.9,stroke:t,strokeWidth:.15,dash:[.1,.05],dashEnabled:!0,listening:!1})}centerOnRoom(e,t=!1){var l;const s={x:e.x,y:e.y};(l=this.positionRender)==null||l.position(e);const i=this.stage.getAbsoluteTransform().point(s),o={x:this.stage.width()/2,y:this.stage.height()/2},n=o.x-i.x,d=o.y-i.y;this.currentTransition&&(this.currentTransition.pause(),this.currentTransition.destroy(),delete this.currentTransition),t?this.stage.position({x:this.stage.x()+n,y:this.stage.y()+d}):(this.currentTransition=new u.Tween({node:this.stage,x:this.stage.x()+n,y:this.stage.y()+d,duration:.2,easing:u.Easings.EaseInOut}),this.currentTransition.play())}renderRooms(e){e.forEach(t=>{const s=new u.Group({x:t.x-c.roomSize/2,y:t.y-c.roomSize/2}),r=new u.Rect({x:0,y:0,width:c.roomSize,height:c.roomSize,fill:this.mapReader.getColorValue(t.env),strokeWidth:.025,stroke:c.lineColor});s.on("mouseenter",()=>{this.stage.container().style.cursor="pointer"}),s.on("mouseleave",()=>{this.stage.container().style.cursor="auto"}),s.add(r),this.renderSymbol(t,s),this.roomLayer.add(s),this.exitRenderer.renderSpecialExits(t).forEach(i=>{this.linkLayer.add(i)}),this.exitRenderer.renderStubs(t).forEach(i=>{this.linkLayer.add(i)}),this.exitRenderer.renderInnerExits(t).forEach(i=>{this.roomLayer.add(i)})})}renderSymbol(e,t){if(e.roomChar!==void 0){const s=new u.Text({x:0,y:0,text:e.roomChar,fontSize:.45,fontStyle:"bold",fill:this.mapReader.getSymbolColor(e.env),align:"center",verticalAlign:"middle",width:c.roomSize,height:c.roomSize});t.add(s)}}renderExits(e){e.forEach(t=>{const s=this.exitRenderer.render(t);s&&this.linkLayer.add(s)})}renderLabels(e){e.forEach(t=>{if(!t.pixMap)return;const s=new Image;s.src=`data:image/png;base64,${t.pixMap}`;const r=new u.Image({x:t.X,y:-t.Y,width:t.Width,height:t.Height,image:s});this.linkLayer.add(r)})}}class A{constructor(e,t){this.rooms=[],this.labels=[],this.rooms=e,this.bounds=this.createBounds(),this.labels=t}getRooms(){return this.rooms}getLabels(){return this.labels}getBounds(){return this.bounds}createBounds(){return this.rooms.reduce((e,t)=>({minX:Math.min(e.minX,t.x),maxX:Math.max(e.maxX,t.x),minY:Math.min(e.minY,t.y),maxY:Math.max(e.maxY,t.y)}),{minX:Number.POSITIVE_INFINITY,maxX:Number.NEGATIVE_INFINITY,minY:Number.POSITIVE_INFINITY,maxY:Number.NEGATIVE_INFINITY})}}class S{constructor(e){this.planes={},this.exits=new Map,this.version=0,this.area=e,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(e){return this.planes[e]}getPlanes(){return Object.values(this.planes)}getRooms(){return this.area.rooms}getLinkExits(e){return Array.from(this.exits.values()).filter(t=>t.zIndex.includes(e))}createPlanes(){const e=this.area.rooms.reduce((t,s)=>(t[s.z]||(t[s.z]=[]),t[s.z].push(s),t),{});return Object.entries(e).reduce((t,[s,r])=>(t[+s]=new A(r,this.area.labels.filter(i=>i.Z===+s)),t),{})}createExits(){this.area.rooms.forEach(e=>{Object.entries(e.exits).filter(([t,s])=>_.indexOf(t)>-1&&!e.customLines.hasOwnProperty(w[t])).forEach(([t,s])=>this.createHalfExit(e.id,s,e.z,t))})}createHalfExit(e,t,s,r){if(e===t)return;const i=Math.min(e,t),o=Math.max(e,t),n=`${i}-${o}`;let d=this.exits.get(n);d||(d={a:i,b:o,zIndex:[s]}),i==e?d.aDir=r:d.bDir=r,d.zIndex.push(s),this.exits.set(n,d)}}class I extends A{constructor(e,t){super(e.getRooms(),e.getLabels()),this.basePlane=e,this.visitedRooms=t}getRooms(){return this.basePlane.getRooms().filter(e=>this.visitedRooms.has(e.id))}getLabels(){return this.basePlane.getLabels()}getBounds(){const e=this.getRooms();return e.length?e.reduce((t,s)=>({minX:Math.min(t.minX,s.x),maxX:Math.max(t.maxX,s.x),minY:Math.min(t.minY,s.y),maxY:Math.max(t.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 S{constructor(e,t){super(e),this.planeCache=new WeakMap,this.visitedRooms=t instanceof Set?t:new Set(t??[]),this.areaRoomIds=new Set(e.rooms.map(s=>s.id))}getPlane(e){const t=super.getPlane(e);if(!t)return t;let s=this.planeCache.get(t);return s||(s=new I(t,this.visitedRooms),this.planeCache.set(t,s)),s}getPlanes(){return super.getPlanes().map(e=>{let t=this.planeCache.get(e);return t||(t=new I(e,this.visitedRooms),this.planeCache.set(e,t)),t})}getLinkExits(e){return super.getLinkExits(e).filter(t=>this.visitedRooms.has(t.a)||this.visitedRooms.has(t.b))}getVisitedRoomCount(){return super.getRooms().reduce((e,t)=>e+(this.visitedRooms.has(t.id)?1:0),0)}getTotalRoomCount(){return this.areaRoomIds.size}hasVisitedRoom(e){return this.areaRoomIds.has(e)&&this.visitedRooms.has(e)}getVisitedRoomIds(){return super.getRooms().filter(e=>this.visitedRooms.has(e.id)).map(e=>e.id)}addVisitedRoom(e){const t=this.visitedRooms.has(e);this.visitedRooms.add(e);const s=!t&&this.areaRoomIds.has(e);return s&&this.markDirty(),s}addVisitedRooms(e){let t=0;for(const s of e){const r=this.visitedRooms.has(s);this.visitedRooms.add(s),!r&&this.areaRoomIds.has(s)&&t++}return t>0&&this.markDirty(),t}}const C={rgbValue:"rgb(114, 1, 0)",symbolColor:[225,225,225]};function z(a){const e=a[0]/255,t=a[1]/255,s=a[2]/255,r=Math.max(e,t,s),i=Math.min(e,t,s);return(r+i)/2}class Q{constructor(e,t){this.rooms={},this.areas={},this.areaSources={},this.explorationEnabled=!1,this.colors={},e.forEach(s=>{s.rooms.forEach(i=>{i.y=-i.y,this.rooms[i.id]=i});const r=parseInt(s.areaId);this.areas[r]=new S(s),this.areaSources[r]=s}),this.colors=t.reduce((s,r)=>({...s,[r.envId]:{rgb:r.colors,rgbValue:`rgb(${r.colors.join(",")}`,symbolColor:z(r.colors)>.41?[25,25,25]:[225,255,255],symbolColorValue:z(r.colors)>.41?"rgb(25,25,25)":"rgb(225,255,255)"}}),{})}getArea(e){return this.areas[e]}getExplorationArea(e){const t=this.areas[e];if(t instanceof k)return t}getAreas(){return Object.values(this.areas)}getRooms(){return Object.values(this.rooms)}getRoom(e){return this.rooms[e]}decorateWithExploration(e){return this.visitedRooms=e instanceof Set?e:new Set(e??[]),Object.entries(this.areaSources).forEach(([t,s])=>{const r=parseInt(t,10);this.areas[r]=new k(s,this.visitedRooms)}),this.explorationEnabled=!0,this.visitedRooms}getVisitedRooms(){return this.visitedRooms}clearExplorationDecoration(){Object.entries(this.areaSources).forEach(([e,t])=>{const s=parseInt(e,10);this.areas[s]=new S(t)}),this.explorationEnabled=!1}isExplorationEnabled(){return this.explorationEnabled}getColorValue(e){var t;return((t=this.colors[e])==null?void 0:t.rgbValue)??C.rgbValue}getSymbolColor(e,t){var o;const s=((o=this.colors[e])==null?void 0:o.symbolColor)??C.symbolColor,r=Math.min(Math.max(t??1,0),1),i=s.join(",");return r!=1?`rgba(${i}, ${r})`:`rgba(${i})`}}function J(a){return a&&a.__esModule&&Object.prototype.hasOwnProperty.call(a,"default")?a.default:a}class U{constructor(){this.keys=new Set,this.queue=[]}sort(){this.queue.sort((e,t)=>e.priority-t.priority)}set(e,t){const s=Number(t);if(isNaN(s))throw new TypeError('"priority" must be a number');return this.keys.has(e)?this.queue.map(r=>(r.key===e&&Object.assign(r,{priority:s}),r)):(this.keys.add(e),this.queue.push({key:e,priority:s})),this.sort(),this.queue.length}next(){const e=this.queue.shift();return this.keys.delete(e.key),e}isEmpty(){return this.queue.length===0}has(e){return this.keys.has(e)}get(e){return this.queue.find(t=>t.key===e)}}var ee=U;function Y(a,e){const t=new Map;for(const[s,r]of a)s!==e&&r instanceof Map?t.set(s,Y(r,e)):s!==e&&t.set(s,r);return t}var te=Y;function se(a){const e=Number(a);return!(isNaN(e)||e<=0)}function $(a){const e=new Map;return Object.keys(a).forEach(s=>{const r=a[s];if(r!==null&&typeof r=="object"&&!Array.isArray(r))return e.set(s,$(r));if(!se(r))throw new Error(`Could not add node at key "${s}", make sure it's a valid node`,r);return e.set(s,Number(r))}),e}var re=$;function W(a){if(!(a instanceof Map))throw new Error(`Invalid graph: Expected Map instead found ${typeof a}`);a.forEach((e,t)=>{if(typeof e=="object"&&e instanceof Map){W(e);return}if(typeof e!="number"||e<=0)throw new Error(`Values must be numbers greater than 0. Found value ${e} at ${t}`)})}var ie=W;const oe=ee,ne=te,V=re,M=ie;class ae{constructor(e){e instanceof Map?(M(e),this.graph=e):e?this.graph=V(e):this.graph=new Map}addNode(e,t){let s;return t instanceof Map?(M(t),s=t):s=V(t),this.graph.set(e,s),this}addVertex(e,t){return this.addNode(e,t)}removeNode(e){return this.graph=ne(this.graph,e),this}path(e,t,s={}){if(!this.graph.size)return s.cost?{path:null,cost:0}:null;const r=new Set,i=new oe,o=new Map;let n=[],d=0,l=[];if(s.avoid&&(l=[].concat(s.avoid)),l.includes(e))throw new Error(`Starting node (${e}) cannot be avoided`);if(l.includes(t))throw new Error(`Ending node (${t}) cannot be avoided`);for(i.set(e,0);!i.isEmpty();){const h=i.next();if(h.key===t){d=h.priority;let g=h.key;for(;o.has(g);)n.push(g),g=o.get(g);break}r.add(h.key),(this.graph.get(h.key)||new Map).forEach((g,m)=>{if(r.has(m)||l.includes(m))return null;if(!i.has(m))return o.set(m,h.key),i.set(m,h.priority+g);const D=i.get(m).priority,v=h.priority+g;return v<D?(o.set(m,h.key),i.set(m,v)):null})}return n.length?(s.trim?n.shift():n=n.concat([e]),s.reverse||(n=n.reverse()),s.cost?{path:n,cost:d}:n):s.cost?{path:null,cost:0}:null}shortestPath(...e){return this.path(...e)}}var he=ae;const ce=J(he),ue={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 de{constructor(e){this.cache=new Map,this.mapReader=e,this.graph=this.buildGraph()}buildGraph(){const e={};return this.mapReader.getRooms().forEach(t=>{const s={},r=new Set((t.exitLocks??[]).map(o=>ue[o]).filter(o=>!!o)),i=new Set(t.mSpecialExitLocks??[]);Object.entries(t.exits??{}).forEach(([o,n])=>{r.has(o)||this.mapReader.getRoom(n)&&(s[n.toString()]=1)}),Object.values(t.specialExits??{}).forEach(o=>{i.has(o)||this.mapReader.getRoom(o)&&(s[o.toString()]=1)}),e[t.id.toString()]=s}),new ce(e)}findPath(e,t){const s=`${e}->${t}`;if(this.cache.has(s))return this.cache.get(s);if(e===t){const n=this.mapReader.getRoom(e)?[e]:null;return this.cache.set(s,n),n}if(!this.mapReader.getRoom(e)||!this.mapReader.getRoom(t))return this.cache.set(s,null),null;const r=this.graph.path(e.toString(),t.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=Q;exports.PathFinder=de;exports.Renderer=K;exports.Settings=c;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("konva"),_=["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}},L=["north","south","east","west","northeast","northwest","southeast","southwest"],j={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,o=[];if(o.push(...Object.values(f(e.x,e.y,t.aDir,d.roomSize/2))),o.push(...Object.values(f(s.x,s.y,t.bDir,d.roomSize/2))),e.doors[S[t.aDir]]||s.doors[S[t.bDir]]){const n=this.renderDoor(o,e.doors[S[t.aDir]]??s.doors[S[t.bDir]]);r.add(n)}const i=new p.Line({points:o,stroke:d.lineColor,strokeWidth:.025});return r.add(i),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 o={x:s.x,y:s.y};(s.area!==e.area||s.z!==e.z)&&(o=f(e.x,e.y,r,d.roomSize/2));const i=f(e.x,e.y,r,.3),n=i.x-(i.x-o.x)/2,u=i.y-(i.y-o.y)/2,g=new p.Group,a=[];a.push(...Object.values(f(e.x,e.y,r,d.roomSize/2))),a.push(o.x,o.y);const l=new p.Line({points:a,stroke:d.lineColor,strokeWidth:.025,dashEnabled:!0,dash:[.1,.05]});g.add(l);const c=new p.Arrow({points:[a[0],a[1],n,u],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((u,g)=>(u.push(g.x,-g.y),u),r);const o=s.attributes.arrow?p.Arrow:p.Line,i=new o({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 n=s.attributes.style;return n==="dot line"?(i.dash([.05,.05]),i.dashOffset(.1)):n==="dash line"?i.dash([.4,.2]):n==="solid line"||n!==void 0&&console.log("Brak opisu stylu: "+n),i})}renderStubs(t){return t.stubs.map(e=>{const s=G[e],r=f(t.x,t.y,s,d.roomSize/2),o=f(t.x,t.y,s,d.roomSize/2+.5),i=[r.x,r.y,o.x,o.y];return new p.Line({points:i,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 o=t.doors[e];if(o!==void 0)switch(o){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 i=r.clone();i.rotation(-90),i.position(f(t.x,t.y,"east",d.roomSize/4)),s.add(i),r.rotation(90),r.position(f(t.x,t.y,"west",d.roomSize/4));break;case"out":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}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){if(e===void 0||s===void 0)return;const r=t.map(a=>this.mapReader.getRoom(a)).filter(a=>a!==void 0),o=[];let i=null;const n=()=>{i&&(i.length<4&&o.pop(),i=null)},u=()=>(i||(i=[],o.push(i)),i);r.forEach((a,l)=>{if(!this.isRoomVisible(a,e,s))return;const c=l>0?r[l-1]:void 0,m=l<r.length-1?r[l+1]:void 0;if(this.isRoomVisible(c,e,s))u();else{n();const y=u();if(c){const x=this.getDirectionTowards(a,c);if(x){const R=f(a.x,a.y,x,d.roomSize);y.push(R.x,R.y)}}}if(i==null||i.push(a.x,a.y),!this.isRoomVisible(m,e,s)&&m){const y=this.getDirectionTowards(a,m);if(y){const x=f(a.x,a.y,y,d.roomSize);i==null||i.push(x.x,x.y)}n()}}),n();const g=o.filter(a=>a.length>=4).map(a=>new p.Line({points:a,stroke:"green",strokeWidth:.1}));return g.forEach(a=>{this.overlayLayer.add(a),this.paths.push(a)}),g[0]}clearPaths(){this.paths.forEach(t=>{t.destroy()}),this.paths=[]}isRoomVisible(t,e,s){return t?t.area===e&&t.z===s:!1}getDirectionTowards(t,e){for(const s of L)if(t.exits[s]===e.id)return s;for(const s of L)if(e.exits[s]===t.id)return j[s]}}const M=.6,E=75,Q="rgb(225, 255, 225)",D=class D{};D.roomSize=M,D.lineColor=Q;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",o=>{o.evt.preventDefault();const i=this.stage.scaleX(),n=this.stage.getPointerPosition();if(!n)return;const u={x:(n.x-this.stage.x())/i,y:(n.y-this.stage.y())/i};let g=o.evt.deltaY>0?-1:1;o.evt.ctrlKey&&(g=-g);const a=g>0?this.currentZoom*t:this.currentZoom/t,l=a*E,c=this.setZoom(a),m={x:n.x-u.x*l,y:n.y-u.y*l};this.stage.position(m),c&&this.emitZoomChangeEvent()}),this.stage.on("touchmove",o=>{const i=o.evt.touches,n=i==null?void 0:i[0],u=i==null?void 0:i[1];if(n&&!u&&r&&!this.stage.isDragging()&&(this.stage.startDrag(),r=!1),!n||!u){e=void 0,s=void 0;return}o.evt.preventDefault(),this.stage.isDragging()&&(this.stage.stopDrag(),r=!0);const g=this.stage.container().getBoundingClientRect(),a={x:n.clientX-g.left,y:n.clientY-g.top},l={x:u.clientX-g.left,y:u.clientY-g.top},c={x:(a.x+l.x)/2,y:(a.y+l.y)/2},m=Math.hypot(a.x-l.x,a.y-l.y);if(s===void 0&&(s=c),e===void 0){e=m;return}if(e===0)return;const b=this.stage.scaleX(),w=this.currentZoom*(m/e),y={x:(c.x-this.stage.x())/b,y:(c.y-this.stage.y())/b},x=this.setZoom(w),R=this.stage.scaleX(),$=c.x-s.x,W=c.y-s.y,Z={x:c.x-y.x*R+$,y:c.y-y.y*R+W};this.stage.position(Z),this.stage.batchDraw(),e=m,s=c,x&&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(),o=r.getBoundingClientRect(),i={roomId:t,position:{x:e-o.left,y:s-o.top}},n=new CustomEvent("roomcontextmenu",{detail:i});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: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 o=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:M*.85,stroke:"rgb(0, 229, 178)",strokeWidth:.1,dash:[.05,.05],dashEnabled:!0}),this.positionLayer.add(this.positionRender)),this.centerOnRoom(e,o)}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 o={color:e,area:s.area,z:s.z};if(this.highlights.set(t,o),s.area===this.currentArea&&s.z===this.currentZIndex){const i=this.createHighlightShape(s,e);return this.overlayLayer.add(i),o.shape=i,this.overlayLayer.batchDraw(),i}return o.shape}clearHighlights(){this.highlights.forEach(({shape:t})=>t==null?void 0:t.destroy()),this.highlights.clear(),this.overlayLayer.batchDraw()}refreshHighlights(){this.highlights.forEach((t,e)=>{var o;if((o=t.shape)==null||o.destroy(),delete t.shape,t.area!==this.currentArea||t.z!==this.currentZIndex)return;const s=this.mapReader.getRoom(e);if(!s)return;const 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 o=this.stage.getAbsoluteTransform().point(s),i={x:this.stage.width()/2,y:this.stage.height()/2},n=i.x-o.x,u=i.y-o.y;this.currentTransition&&(this.currentTransition.pause(),this.currentTransition.destroy(),delete this.currentTransition),e&&d.instantMapMove?this.stage.position({x:this.stage.x()+n,y:this.stage.y()+u}):(this.currentTransition=new p.Tween({node:this.stage,x:this.stage.x()+n,y:this.stage.y()+u,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}),o=(l,c)=>this.emitRoomContextEvent(e.id,l,c);s.on("mouseenter",()=>{this.stage.container().style.cursor="pointer"}),s.on("mouseleave",()=>{this.stage.container().style.cursor="auto"}),s.on("contextmenu",l=>{l.evt.preventDefault();const c=l.evt;o(c.clientX,c.clientY)});let i,n,u;const g=()=>{u!==void 0&&(this.stage.draggable(u),u=void 0)},a=()=>{i!==void 0&&(window.clearTimeout(i),i=void 0),n=void 0,g()};s.on("touchstart",l=>{var m;if(a(),l.evt.touches&&l.evt.touches.length>1)return;const c=(m=l.evt.touches)==null?void 0:m[0];c&&(n={clientX:c.clientX,clientY:c.clientY},u=this.stage.draggable(),this.stage.draggable(!1),i=window.setTimeout(()=>{n&&o(n.clientX,n.clientY),a()},500))}),s.on("touchend",a),s.on("touchmove",l=>{var x;if(!n)return;const c=(x=l.evt.touches)==null?void 0:x[0];if(!c){a();return}const m=c.clientX-n.clientX,b=c.clientY-n.clientY,w=m*m+b*b,y=10;if(w>y*y){const R=u;a(),R&&this.stage.startDrag()}}),s.on("touchcancel",a),s.add(r),this.renderSymbol(e,s),this.roomLayer.add(s),this.exitRenderer.renderSpecialExits(e).forEach(l=>{this.linkLayer.add(l)}),this.exitRenderer.renderStubs(e).forEach(l=>{this.linkLayer.add(l)}),this.exitRenderer.renderInnerExits(e).forEach(l=>{this.roomLayer.add(l)})})}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 A{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 k{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 A(r,this.area.labels.filter(o=>o.Z===+s)),e),{})}createExits(){this.area.rooms.forEach(t=>{Object.entries(t.exits).filter(([e,s])=>_.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 o=Math.min(t,e),i=Math.max(t,e),n=`${o}-${i}`;let u=this.exits.get(n);u||(u={a:o,b:i,zIndex:[s]}),o==t?u.aDir=r:u.bDir=r,u.zIndex.push(s),this.exits.set(n,u)}}class O extends A{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 P extends k{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 O(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 O(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 C={rgbValue:"rgb(114, 1, 0)",symbolColor:[225,225,225]};function I(h){const t=h[0]/255,e=h[1]/255,s=h[2]/255,r=Math.max(t,e,s),o=Math.min(t,e,s);return(r+o)/2}class U{constructor(t,e){this.rooms={},this.areas={},this.areaSources={},this.explorationEnabled=!1,this.colors={},t.forEach(s=>{s.rooms.forEach(o=>{o.y=-o.y,this.rooms[o.id]=o});const r=parseInt(s.areaId);this.areas[r]=new k(s),this.areaSources[r]=s}),this.colors=e.reduce((s,r)=>({...s,[r.envId]:{rgb:r.colors,rgbValue:`rgb(${r.colors.join(",")}`,symbolColor:I(r.colors)>.41?[25,25,25]:[225,255,255],symbolColorValue:I(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 P)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 P(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 k(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 o=this.getExplorationArea(r.area);if(o)return o.addVisitedRoom(t)}}const e=this.ensureVisitedRooms(),s=e.has(t);return e.add(t),!s}addVisitedRooms(t){const e=this.ensureVisitedRooms();let s=0;for(const r of t){if(this.explorationEnabled){const i=this.getRoom(r);if(i){const n=this.getExplorationArea(i.area);if(n){n.addVisitedRoom(r)&&s++;continue}}}const o=e.has(r);e.add(r),o||s++}return s}hasVisitedRoom(t){var e;return((e=this.visitedRooms)==null?void 0:e.has(t))??!1}getColorValue(t){var e;return((e=this.colors[t])==null?void 0:e.rgbValue)??C.rgbValue}getSymbolColor(t,e){var i;const s=((i=this.colors[t])==null?void 0:i.symbolColor)??C.symbolColor,r=Math.min(Math.max(e??1,0),1),o=s.join(",");return r!=1?`rgba(${o}, ${r})`:`rgba(${o})`}}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 N(h,t){const e=new Map;for(const[s,r]of h)s!==t&&r instanceof Map?e.set(s,N(r,t)):s!==t&&e.set(s,r);return e}var rt=N;function it(h){const t=Number(h);return!(isNaN(t)||t<=0)}function Y(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,Y(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=Y;function X(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){X(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=X;const at=st,ht=rt,V=ot,z=nt;class ct{constructor(t){t instanceof Map?(z(t),this.graph=t):t?this.graph=V(t):this.graph=new Map}addNode(t,e){let s;return e instanceof Map?(z(e),s=e):s=V(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,o=new at,i=new Map;let n=[],u=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(o.set(t,0);!o.isEmpty();){const a=o.next();if(a.key===e){u=a.priority;let c=a.key;for(;i.has(c);)n.push(c),c=i.get(c);break}r.add(a.key),(this.graph.get(a.key)||new Map).forEach((c,m)=>{if(r.has(m)||g.includes(m))return null;if(!o.has(m))return i.set(m,a.key),o.set(m,a.priority+c);const b=o.get(m).priority,w=a.priority+c;return w<b?(i.set(m,a.key),o.set(m,w)):null})}return n.length?(s.trim?n.shift():n=n.concat([t]),s.reverse||(n=n.reverse()),s.cost?{path:n,cost:u}:n):s.cost?{path:null,cost:0}:null}shortestPath(...t){return this.path(...t)}}var dt=ct;const ut=tt(dt),lt={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(i=>lt[i]).filter(i=>!!i)),o=new Set(e.mSpecialExitLocks??[]);Object.entries(e.exits??{}).forEach(([i,n])=>{r.has(i)||this.mapReader.getRoom(n)&&(s[n.toString()]=1)}),Object.values(e.specialExits??{}).forEach(i=>{o.has(i)||this.mapReader.getRoom(i)&&(s[i.toString()]=1)}),t[e.id.toString()]=s}),new ut(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()),o=Array.isArray(r)?r:r==null?void 0:r.path,i=o?o.map(n=>Number(n)):null;return this.cache.set(s,i),i}}exports.ExplorationArea=P;exports.MapReader=U;exports.PathFinder=gt;exports.Renderer=J;exports.Settings=d;
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|