mirror of
https://github.com/danbulant/flying-squid
synced 2026-07-06 11:40:46 +00:00
some fixes in findBorder, add more tests
This commit is contained in:
parent
adcd35b985
commit
703b42abea
2 changed files with 111 additions and 45 deletions
|
|
@ -49,13 +49,14 @@ async function findBorder(world,{line,direction},directionV)
|
||||||
return [];
|
return [];
|
||||||
var left=await findLineInDirection(world,bottom[0].plus(direction.scaled(-1).plus(directionV)),'obsidian',directionV,direction);
|
var left=await findLineInDirection(world,bottom[0].plus(direction.scaled(-1).plus(directionV)),'obsidian',directionV,direction);
|
||||||
var right=await findLineInDirection(world,bottom[line.length-1].plus(direction).plus(directionV),'obsidian',
|
var right=await findLineInDirection(world,bottom[line.length-1].plus(direction).plus(directionV),'obsidian',
|
||||||
directionV,direction);
|
directionV,direction.scaled(-1));
|
||||||
if(left.length!=right.length)
|
if(left.length==0 || left.length!=right.length)
|
||||||
return null;
|
return null;
|
||||||
var top=await findLineInDirection(world,left[left.length-1].plus(direction).plus(directionV),'obsidian',
|
var top=await findLineInDirection(world,left[left.length-1].plus(direction).plus(directionV),'obsidian',
|
||||||
direction,directionV);
|
direction,directionV.scaled(-1));
|
||||||
if(bottom.length!=top.length)
|
if(bottom.length!=top.length)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
left=positiveOrder(left,directionV);
|
left=positiveOrder(left,directionV);
|
||||||
right=positiveOrder(right,directionV);
|
right=positiveOrder(right,directionV);
|
||||||
top=positiveOrder(top,direction);
|
top=positiveOrder(top,direction);
|
||||||
|
|
@ -68,6 +69,9 @@ async function findBorder(world,{line,direction},directionV)
|
||||||
var horDir=direction.x!=0 || directionV.x!=0 ? 'x' :'z';
|
var horDir=direction.x!=0 || directionV.x!=0 ? 'x' :'z';
|
||||||
[left,right]=direction[horDir]<0 || directionV[horDir]<0 ? [right,left] : [left,right];
|
[left,right]=direction[horDir]<0 || directionV[horDir]<0 ? [right,left] : [left,right];
|
||||||
|
|
||||||
|
if(bottom.length<2 || top.length<2 || left.length<3 || right.length<3)
|
||||||
|
return null;
|
||||||
|
|
||||||
return [bottom,left,right,top];
|
return [bottom,left,right,top];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,25 +2,95 @@ var {detectFrame,findPotentialLines,findBorder,getAir}=require("flying-squid").p
|
||||||
var World = require('prismarine-world');
|
var World = require('prismarine-world');
|
||||||
var Chunk = require('prismarine-chunk')(require("flying-squid").version);
|
var Chunk = require('prismarine-chunk')(require("flying-squid").version);
|
||||||
var Vec3 = require("vec3").Vec3;
|
var Vec3 = require("vec3").Vec3;
|
||||||
var assert = require('assert');
|
var assert = require('chai').assert;
|
||||||
var range = require('range').range;
|
var range = require('range').range;
|
||||||
|
var flatMap = require('flatmap');
|
||||||
|
|
||||||
|
function generateLine(startingPoint,direction,length) {
|
||||||
|
return range(0,length).map(i => startingPoint.plus(direction.scaled(i)));
|
||||||
|
}
|
||||||
|
|
||||||
|
function generatePortal(bottomLeft,direction,width,height){
|
||||||
|
var directionV=new Vec3(0,1,0);
|
||||||
|
return {
|
||||||
|
bottom:generateLine(bottomLeft.plus(direction),direction,width-2),
|
||||||
|
left:generateLine(bottomLeft.plus(directionV),directionV,height-2),
|
||||||
|
right:generateLine(bottomLeft.plus(direction.scaled(width-1)).plus(directionV),directionV,height-2),
|
||||||
|
top:generateLine(bottomLeft.plus(directionV.scaled(height-1).plus(direction)),direction,width-2),
|
||||||
|
air:flatMap(generateLine(bottomLeft.plus(direction).plus(directionV),direction,width-2),
|
||||||
|
p => generateLine(p,directionV,height-2))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("Generate portal",function(){
|
||||||
|
it("generate a line",() => {
|
||||||
|
assert.deepEqual(generateLine(new Vec3(3,1,1),new Vec3(1,0,0),2),[new Vec3(3, 1, 1), new Vec3(4, 1, 1)])
|
||||||
|
});
|
||||||
|
it("generate a portal", () => {
|
||||||
|
assert.deepEqual(generatePortal(new Vec3(2,1,1),new Vec3(1,0,0),4,5),{
|
||||||
|
bottom:generateLine(new Vec3(3,1,1),new Vec3(1,0,0),2),
|
||||||
|
left:generateLine(new Vec3(2,2,1),new Vec3(0,1,0),3),
|
||||||
|
right:generateLine(new Vec3(5,2,1),new Vec3(0,1,0),3),
|
||||||
|
top:generateLine(new Vec3(3,5,1),new Vec3(1,0,0),2),
|
||||||
|
air:generateLine(new Vec3(3,2,1),new Vec3(0,1,0),3).concat(generateLine(new Vec3(4,2,1),new Vec3(0,1,0),3))
|
||||||
|
})
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("Detect portal", function() {
|
describe("Detect portal", function() {
|
||||||
|
|
||||||
|
|
||||||
var portalData=[];
|
var portalData=[];
|
||||||
portalData.push({
|
portalData.push({
|
||||||
name:"simple portal frame x",
|
name:"simple portal frame x",
|
||||||
bottom:[new Vec3(3, 1, 1), new Vec3(4, 1, 1)],
|
bottomLeft:new Vec3(2,1,1),
|
||||||
left:[new Vec3(2, 2, 1), new Vec3(2, 3, 1), new Vec3(2, 4, 1)],
|
direction:new Vec3(1,0,0),
|
||||||
right:[new Vec3(5, 2, 1), new Vec3(5, 3, 1), new Vec3(5, 4, 1)],
|
width:4,
|
||||||
top:[new Vec3(3, 5, 1), new Vec3(4, 5, 1)],
|
height:5,
|
||||||
air:[new Vec3(3, 2, 1),new Vec3(3, 3, 1),new Vec3(3, 4, 1),new Vec3(4, 2, 1),new Vec3(4, 3, 1),new Vec3(4, 4, 1)],
|
|
||||||
additionalAir:[],
|
additionalAir:[],
|
||||||
additionalObsidian:[]
|
additionalObsidian:[]
|
||||||
});
|
});
|
||||||
|
portalData.push({
|
||||||
|
name:"simple portal frame z",
|
||||||
|
bottomLeft:new Vec3(2,1,1),
|
||||||
|
direction:new Vec3(0,0,1),
|
||||||
|
width:4,
|
||||||
|
height:5,
|
||||||
|
additionalAir:[],
|
||||||
|
additionalObsidian:[]
|
||||||
|
});
|
||||||
|
portalData.push({
|
||||||
|
name:"big simple portal frame x",
|
||||||
|
bottomLeft:new Vec3(2,1,1),
|
||||||
|
direction:new Vec3(1,0,0),
|
||||||
|
width:10,
|
||||||
|
height:10,
|
||||||
|
additionalAir:[],
|
||||||
|
additionalObsidian:[]
|
||||||
|
});
|
||||||
|
portalData.push({
|
||||||
|
name:"simple portal frame x with borders",
|
||||||
|
bottomLeft:new Vec3(2,1,1),
|
||||||
|
direction:new Vec3(1,0,0),
|
||||||
|
width:4,
|
||||||
|
height:5,
|
||||||
|
additionalAir:[],
|
||||||
|
additionalObsidian:[new Vec3(2,1,1),new Vec3(5,1,1),new Vec3(2,6,1),new Vec3(5,6,1)]
|
||||||
|
});
|
||||||
|
var {bottom,left,right,top,air}=generatePortal(new Vec3(2,1,2),new Vec3(1,0,0),4,5);
|
||||||
|
|
||||||
portalData.forEach(({name,bottom,left,right,top,air,additionalAir,additionalObsidian}) => {
|
portalData.push({
|
||||||
|
name:"2 portals",
|
||||||
|
bottomLeft:new Vec3(2,1,1),
|
||||||
|
direction:new Vec3(1,0,0),
|
||||||
|
width:4,
|
||||||
|
height:5,
|
||||||
|
additionalAir:air,
|
||||||
|
additionalObsidian:[].concat.apply([], [bottom, left, right,top])
|
||||||
|
});
|
||||||
|
|
||||||
|
portalData.forEach(({name,bottomLeft,direction,width,height,additionalAir,additionalObsidian}) => {
|
||||||
|
var {bottom,left,right,top,air}=generatePortal(bottomLeft,direction,width,height);
|
||||||
describe("Detect "+name,() => {
|
describe("Detect "+name,() => {
|
||||||
var expectedBorder=[
|
var expectedBorder=[
|
||||||
bottom,
|
bottom,
|
||||||
|
|
@ -48,63 +118,51 @@ describe("Detect portal", function() {
|
||||||
describe("detect potential first lines",function(){
|
describe("detect potential first lines",function(){
|
||||||
it("detect potential first lines from bottom left", async function() {
|
it("detect potential first lines from bottom left", async function() {
|
||||||
let potentialLines=await findPotentialLines(world,bottom[0],new Vec3(0,1,0));
|
let potentialLines=await findPotentialLines(world,bottom[0],new Vec3(0,1,0));
|
||||||
assert.deepEqual(potentialLines,[
|
assert.include(potentialLines,{
|
||||||
{
|
"direction": direction,
|
||||||
"direction": new Vec3(1,0,0),
|
|
||||||
"line": bottom
|
"line": bottom
|
||||||
}
|
});
|
||||||
]);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("detect potential first lines from bottom right", async function() {
|
it("detect potential first lines from bottom right", async function() {
|
||||||
let potentialLines=await findPotentialLines(world,bottom[bottom.length-1],new Vec3(0,1,0));
|
let potentialLines=await findPotentialLines(world,bottom[bottom.length-1],new Vec3(0,1,0));
|
||||||
assert.deepEqual(potentialLines,[
|
assert.include(potentialLines,{
|
||||||
{
|
"direction": direction,
|
||||||
"direction": new Vec3(1,0,0),
|
|
||||||
"line": bottom
|
"line": bottom
|
||||||
}
|
});
|
||||||
]);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it("detect potential first lines from top left", async function() {
|
it("detect potential first lines from top left", async function() {
|
||||||
let potentialLines=await findPotentialLines(world,top[0],new Vec3(0,-1,0));
|
let potentialLines=await findPotentialLines(world,top[0],new Vec3(0,-1,0));
|
||||||
assert.deepEqual(potentialLines,[
|
assert.include(potentialLines,{
|
||||||
{
|
"direction": direction,
|
||||||
"direction": new Vec3(1,0,0),
|
|
||||||
"line": top
|
"line": top
|
||||||
}
|
});
|
||||||
]);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("detect potential first lines from top right", async function() {
|
it("detect potential first lines from top right", async function() {
|
||||||
let potentialLines=await findPotentialLines(world,top[top.length-1],new Vec3(0,-1,0));
|
let potentialLines=await findPotentialLines(world,top[top.length-1],new Vec3(0,-1,0));
|
||||||
assert.deepEqual(potentialLines,[
|
assert.include(potentialLines,{
|
||||||
{
|
"direction": direction,
|
||||||
"direction": new Vec3(1,0,0),
|
|
||||||
"line": top
|
"line": top
|
||||||
}
|
});
|
||||||
]);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("detect potential first lines from left top", async function() {
|
it("detect potential first lines from left top", async function() {
|
||||||
let potentialLines=await findPotentialLines(world,left[left.length-1],new Vec3(1,0,0));
|
let potentialLines=await findPotentialLines(world,left[left.length-1],direction);
|
||||||
assert.deepEqual(potentialLines,[
|
assert.include(potentialLines,{
|
||||||
{
|
|
||||||
"direction": new Vec3(0,1,0),
|
"direction": new Vec3(0,1,0),
|
||||||
"line": left
|
"line": left
|
||||||
}
|
});
|
||||||
]);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("detect potential first lines from right bottom", async function() {
|
it("detect potential first lines from right bottom", async function() {
|
||||||
let potentialLines=await findPotentialLines(world,right[0],new Vec3(-1,0,0));
|
let potentialLines=await findPotentialLines(world,right[0],direction.scaled(-1));
|
||||||
assert.deepEqual(potentialLines,[
|
assert.include(potentialLines,{
|
||||||
{
|
|
||||||
"direction": new Vec3(0,1,0),
|
"direction": new Vec3(0,1,0),
|
||||||
"line": right
|
"line": right
|
||||||
}
|
});
|
||||||
]);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -112,7 +170,7 @@ describe("Detect portal", function() {
|
||||||
describe("find borders",function() {
|
describe("find borders",function() {
|
||||||
it("find borders from bottom", async function () {
|
it("find borders from bottom", async function () {
|
||||||
var border = await findBorder(world, {
|
var border = await findBorder(world, {
|
||||||
"direction": new Vec3(1, 0, 0),
|
"direction": direction,
|
||||||
"line": bottom
|
"line": bottom
|
||||||
}, new Vec3(0, 1, 0));
|
}, new Vec3(0, 1, 0));
|
||||||
assert.deepEqual(border, expectedBorder)
|
assert.deepEqual(border, expectedBorder)
|
||||||
|
|
@ -120,7 +178,7 @@ describe("Detect portal", function() {
|
||||||
|
|
||||||
it("find borders from top", async function () {
|
it("find borders from top", async function () {
|
||||||
var border = await findBorder(world, {
|
var border = await findBorder(world, {
|
||||||
"direction": new Vec3(1, 0, 0),
|
"direction": direction,
|
||||||
"line": top
|
"line": top
|
||||||
}, new Vec3(0, -1, 0));
|
}, new Vec3(0, -1, 0));
|
||||||
assert.deepEqual(border, expectedBorder)
|
assert.deepEqual(border, expectedBorder)
|
||||||
|
|
@ -130,14 +188,14 @@ describe("Detect portal", function() {
|
||||||
var border = await findBorder(world, {
|
var border = await findBorder(world, {
|
||||||
"direction": new Vec3(0, 1, 0),
|
"direction": new Vec3(0, 1, 0),
|
||||||
"line": left
|
"line": left
|
||||||
}, new Vec3(1, 0, 0));
|
},direction);
|
||||||
assert.deepEqual(border, expectedBorder)
|
assert.deepEqual(border, expectedBorder)
|
||||||
});
|
});
|
||||||
it("find borders from right", async function () {
|
it("find borders from right", async function () {
|
||||||
var border = await findBorder(world, {
|
var border = await findBorder(world, {
|
||||||
"direction": new Vec3(0, 1, 0),
|
"direction": new Vec3(0, 1, 0),
|
||||||
"line": right
|
"line": right
|
||||||
}, new Vec3(-1, 0, 0));
|
}, direction.scaled(-1));
|
||||||
assert.deepEqual(border, expectedBorder)
|
assert.deepEqual(border, expectedBorder)
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
@ -147,8 +205,12 @@ describe("Detect portal", function() {
|
||||||
var portals=await detectFrame(world,bottom[0],new Vec3(0,1,0));
|
var portals=await detectFrame(world,bottom[0],new Vec3(0,1,0));
|
||||||
assert.deepEqual(portals,[expectedBorder])
|
assert.deepEqual(portals,[expectedBorder])
|
||||||
});
|
});
|
||||||
|
it("detect portals from top left",async function() {
|
||||||
|
var portals=await detectFrame(world,top[0],new Vec3(0,-1,0));
|
||||||
|
assert.deepEqual(portals,[expectedBorder])
|
||||||
|
});
|
||||||
it("detect portals from right top",async function() {
|
it("detect portals from right top",async function() {
|
||||||
var portals=await detectFrame(world,right[right.length-1],new Vec3(-1,0,0));
|
var portals=await detectFrame(world,right[right.length-1],direction.scaled(-1));
|
||||||
assert.deepEqual(portals,[expectedBorder])
|
assert.deepEqual(portals,[expectedBorder])
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue