PlotDirector/PlotLine.Tests/Browser/maps-settlements-engine.cjs

32 lines
5.0 KiB
JavaScript

const assert=require('node:assert/strict'),{performance}=require('node:perf_hooks');
const E=require('../../PlotLine/wwwroot/js/maps-engine.js'),A=require('../../PlotLine/wwwroot/js/maps-appearance-engine.js');
for(const type of E.settlements){const f=E.makeFeature(type,10,20,'new-'+type);assert.equal(f.geometryClass,'Marker');assert(E.hasBoundary(f));assert(E.areaGeometry(f).points.length>=3);assert.deepEqual([f.x,f.y],[10,20]);}
const legacy=E.makeFeature('Town',50,60,'town');const g=E.geometry(legacy);delete g.boundary;legacy.geometryJson=JSON.stringify(g);legacy.locationID=123;
const original=E.copy(legacy),road=E.makeFeature('Road',500,200,'road');road.startFeatureID=legacy.featureID;
const endpoints=E.pathPoints(road,[legacy,road]);assert(E.convertToArea(legacy));assert.equal(legacy.featureID,original.featureID);assert.equal(legacy.locationID,123);assert.deepEqual([legacy.x,legacy.y],[50,60]);assert.deepEqual(E.pathPoints(road,[legacy,road]),endpoints);
const controls=JSON.stringify(E.areaGeometry(legacy).points),outlines=[];
for(const coast of ['Natural','Rugged','Gentle','Natural']){E.updateArea(legacy,b=>b.coast=coast);outlines.push(E.outline(legacy));assert.equal(JSON.stringify(E.areaGeometry(legacy).points),controls);assert.deepEqual(E.pathPoints(road,[legacy,road]),endpoints);}
assert.deepEqual(outlines[0],outlines[3]);assert.notDeepEqual(outlines[0],outlines[1]);
E.updateArea(legacy,b=>{b.width=450;b.height=300;b.rotation=23;b.points[0][0]-=.1;});assert.deepEqual(E.pathPoints(road,[legacy,road]),endpoints,'boundary editing never touches anchored connections');
const boundaryBefore=E.outline(legacy);E.moveAnchor(legacy,150,200);const fixed=E.outline(legacy);
for(let i=0;i<fixed.length;i++){assert(Math.abs(fixed[i][0]-boundaryBefore[i][0])<1e-8);assert(Math.abs(fixed[i][1]-boundaryBefore[i][1])<1e-8);}
assert.deepEqual(E.pathPoints(road,[legacy,road])[0],[150,200]);E.translate(legacy,30,40);const moved=E.outline(legacy);assert.deepEqual(E.pathPoints(road,[legacy,road])[0],[180,240]);
for(let i=0;i<moved.length;i++){assert(Math.abs(moved[i][0]-fixed[i][0]-30)<1e-8);assert(Math.abs(moved[i][1]-fixed[i][1]-40)<1e-8);}
const town=E.makeFeature('Town',0,0,'area'),overlap=E.makeFeature('Village',40,0,'overlap'),forest=E.makeFeature('Forest',0,0,'forest'),house=E.makeFeature('House',10,10,'house');
const things=[town,overlap,forest,house],beforeHouse=JSON.stringify(house);
let contained=E.containment(things);assert.deepEqual(new Set(contained.inside.get('house')),new Set(['area','overlap','forest']),'overlaps all retained');assert(contained.inside.get('overlap').includes('area'),'nested area anchor recognised');
E.updateArea(town,b=>b.offsetX=2000);contained=E.containment(things);assert(!contained.inside.get('house').includes('area'));assert.equal(JSON.stringify(house),beforeHouse,'reshaping/moving boundary never moves buildings');
E.translate(house,900,900);assert.equal(E.containment(things).inside.get('house').length,0,'moving a building out updates geometry');
const houseSnapshot=JSON.stringify(house),anchor=[town.x,town.y],routeBefore=E.pathPoints(road,[town,road]);
assert(E.fitBoundary(town,[house],things));assert.deepEqual([town.x,town.y],anchor);assert.equal(JSON.stringify(house),houseSnapshot);assert(E.pointInPolygon([house.x,house.y],E.outline(town)),'fit encloses contents with margin');
assert.equal(E.fitBoundary(town,[],things),false);town.isLocked=true;assert.equal(E.fitBoundary(town,[house],things),false);assert.equal(E.moveAnchor(town,3,4),false);
const loaded=E.copy(things);assert.deepEqual(loaded,things,'geometry/style survive the existing JSON document format');
for(const preset of ['light','paper','dark','custom']){const palette=A.palette({appearanceJson:JSON.stringify({preset,backgroundColor:'#102030',landColor:'#aabbcc'})});assert.match(palette.settlement,/^#[0-9a-f]{6}$/i);assert.match(palette.settlementLine,/^#[0-9a-f]{6}$/i);}
const many=Array.from({length:2000},(_,i)=>E.makeFeature('House',(i%100)*40,Math.floor(i/100)*40,'h-'+i));
for(let i=0;i<20;i++)many.push(E.makeFeature(i%2?'Forest':'Town',100+i*150,200,'a-'+i));
const start=performance.now();const result=E.containment(many);for(const f of many.slice(0,60))result.inside.get(f.featureID);for(const f of many.filter(E.isArea))result.contents.get(f.featureID);const duration=performance.now()-start;assert.equal(result.inside.size,2020);assert(duration<1500,'bounded end-of-edit containment should be interactive');
console.log('PASS settlement defaults/conversion, stable anchors/IDs/roads, separate boundary/anchor movement, smoothing reversibility, overlap/nesting, fit, locks, serialization and themes.');
console.log(`Containment: 2,020 pieces / 20 areas in ${Math.round(duration)}ms; no pointer-move recalculation.`);
const overlapping=Array.from({length:1200},(_,i)=>E.makeFeature('Town',0,0,'overlap-'+i));const tick=performance.now(),lazy=E.containment(overlapping);assert.equal(lazy.inside.get('overlap-0').length,1199);assert.equal(lazy.contents.get('overlap-0').length,1199);assert(performance.now()-tick<1500,'dense overlap resolves selected lists without an eager quadratic graph');