From de9a0ec966a20540cafdad1019d498530b60700f Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 24 Jul 2014 19:24:03 -0400 Subject: testing wallbuilding with some broken testcases.. --- test/04-test-builder.js | 118 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 test/04-test-builder.js (limited to 'test/04-test-builder.js') diff --git a/test/04-test-builder.js b/test/04-test-builder.js new file mode 100644 index 0000000..f1c0f71 --- /dev/null +++ b/test/04-test-builder.js @@ -0,0 +1,118 @@ +var assert = require("assert") +var vec = require("../public/assets/javascripts/rectangles/models/vec2.js") +var Rect = require("../public/assets/javascripts/rectangles/models/rect.js") +var Room = require("../public/assets/javascripts/rectangles/models/room.js") +var Rooms = require("../public/assets/javascripts/rectangles/engine/rooms/_rooms.js") +var Clipper = require("../public/assets/javascripts/rectangles/engine/rooms/clipper.js") +var Builder = require("../public/assets/javascripts/rectangles/engine/rooms/builder.js") +var FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 +var ALL = FRONT | BACK | LEFT | RIGHT +function sidesToString(sides){ + var s = "" + if (sides & FRONT) s += "front " + if (sides & BACK) s += "back " + if (sides & LEFT) s += "left " + if (sides & RIGHT) s += "right " + if (sides & TOP) s += "top " + if (sides & BOTTOM) s += "bottom " + return s +} +function bitcount(v) { + v = v - ((v >>> 1) & 0x55555555); + v = (v & 0x33333333) + ((v >>> 2) & 0x33333333); + return ((v + (v >>> 4) & 0xF0F0F0F) * 0x1010101) >>> 24; +} + +var rect = new Rect( new vec(1,5), new vec(1,5) ) +var east = new Rect( new vec(2,6), new vec(1,5) ) +var corner = new Rect( new vec(3,7), new vec(3,7) ) +var peninsula = new Rect( new vec(4,6), new vec(6,8) ) + +function report(a) { + console.log( a.join("\n") ) +} +function reportSides(walls) { + console.log(walls.map(function(w){ return sidesToString(w.side) }).join("\n")) +} +function reset(){ + Rooms.list = {} + Rooms.regions = [] +} +function rebuild(){ + Rooms.clipper.reset_rects() + var regions = Rooms.clipper.clip_rects() + var culled = Rooms.clipper.cull_rects_iterative() + return culled +} + +describe('builder', function(){ + + reset() + var rect_room = Rooms.add_with_rect( rect ) + rebuild() + + describe('#build_walls(rect)', function(){ + var walls = Rooms.builder.build_walls(rect_room.regions[0]) + + it("should return 4 walls", function(){ + assert.equal(4, walls.length) + }) + it("should have one side per wall", function(){ + assert.equal(1, bitcount(walls[0].side)) + assert.equal(1, bitcount(walls[1].side)) + assert.equal(1, bitcount(walls[2].side)) + assert.equal(1, bitcount(walls[3].side)) + }) + }) + + describe('#build_floors(rect)', function(){ + var floors = Rooms.builder.build_floors(rect_room) + it("should make 2 floors", function(){ + assert.equal(2, floors.length) + }) + }) + + // + + reset() + var rect_room = Rooms.add_with_rect( rect ) + var rect_east = Rooms.add_with_rect( east ) + var regions = rebuild() + +console.log("\n\n") +console.log(regions.join("\n")) +console.log("\n\n") + + describe('#build_walls(rect, east)', function(){ + + var walls = regions.map(Rooms.builder.build_walls.bind(Rooms.builder)) + walls.map(function(w){ reportSides(w); console.log("--") }) + + it("should return 3 walls", function(){ + assert.equal(4, walls.length) + }) + it("should have one side per wall", function(){ + assert.equal(1, bitcount(walls[0].side)) + assert.equal(1, bitcount(walls[1].side)) + assert.equal(1, bitcount(walls[2].side)) + assert.equal(1, bitcount(walls[3].side)) + }) + }) + + describe('#build_floors(rect, east)', function(){ + var floors = Rooms.builder.build_floors(rect_room) + it("should make 2 floors", function(){ + assert.equal(2, floors.length) + }) + // reportSides(floors) + }) + + + + + + // Rooms.add_with_rect( corner ) + // Rooms.add_with_rect( peninsula ) + +}) + -- cgit v1.2.3-70-g09d2 From c11322210bc5ca2932f8e208d57b9ec3254b8b06 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Fri, 25 Jul 2014 02:01:54 -0400 Subject: half-walls tests and overflow --- .../javascripts/rectangles/engine/rooms/builder.js | 24 ++-- public/assets/stylesheets/app.css | 2 + test/04-test-builder.js | 155 ++++++++++++++++++--- 3 files changed, 147 insertions(+), 34 deletions(-) (limited to 'test/04-test-builder.js') diff --git a/public/assets/javascripts/rectangles/engine/rooms/builder.js b/public/assets/javascripts/rectangles/engine/rooms/builder.js index 492a8c6..c1cba39 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/builder.js +++ b/public/assets/javascripts/rectangles/engine/rooms/builder.js @@ -149,22 +149,17 @@ this.build_floors = function (room){ var list = [], el = null - + var constructed = room.intersects.filter(function(room){ return room.constructed }) sort.rooms_by_height(constructed) - + if (constructed.length > 0) { // render the regions that don't intersect with anything we've already rendered // if the height is different, calculate the overlapping sides and render half-walls room.regions.forEach(function(region){ var intersected = false for (var i = 0; i < constructed.length; i++) { - if (constructed[i].rect.contains(region)) { - intersected = true - // r.sides = 0xf - // half_sides - } - else if (constructed[i].rect.intersects(region)) { + if (constructed[i].rect.overlaps(region)) { intersected = true if (room.height < constructed[i].height) { var ceiling_walls = this.make_ceiling_walls( room, constructed[i], region ) @@ -181,7 +176,7 @@ list.push( el ) room.mx_ceiling.push(el) } - }) + }.bind(this)) } else { @@ -206,7 +201,7 @@ var depth = region.y.length() var height = hi.height - lo.height - if (! (region.half_sides & LEFT) && region.x.a == hi.rect.x.a) { + if (! (region.half_sides & LEFT) && region.x.a != lo.rect.x.a && region.x.a == hi.rect.x.a) { el = this.make_wall('.left') el.rotationY = HALF_PI el.height = height @@ -221,7 +216,8 @@ el.half_side = LEFT } - if (! (region.half_sides & RIGHT) && region.x.b == hi.rect.x.b) { + if (! (region.half_sides & RIGHT) && region.x.b != lo.rect.x.b && region.x.b == hi.rect.x.b) { +console.log("right") el = this.make_wall('.right') el.rotationY = -HALF_PI el.height = height @@ -236,7 +232,8 @@ el.half_side = RIGHT } - if (! (region.half_sides & FRONT) && region.y.a == hi.rect.y.a) { + if (! (region.half_sides & FRONT) && region.y.a != lo.rect.y.a && region.y.a == hi.rect.y.a) { +console.log("front") el = this.make_wall('.front') el.width = width el.height = height @@ -251,7 +248,8 @@ el.half_side = FRONT } - if (! (region.half_sides & BACK) && region.y.b == hi.rect.y.b) { + if (! (region.half_sides & BACK) && region.y.b != lo.rect.y.b && region.y.b == hi.rect.y.b) { +console.log("back") el = this.make_wall('.back') el.width = width el.height = height diff --git a/public/assets/stylesheets/app.css b/public/assets/stylesheets/app.css index 9072542..a5b1733 100755 --- a/public/assets/stylesheets/app.css +++ b/public/assets/stylesheets/app.css @@ -468,6 +468,8 @@ iframe.embed { .profilepage .bio span:last-of-type:after { display: none; } .templates { + overflow: auto; + max-height: 80%; } .no-templates { display: none; diff --git a/test/04-test-builder.js b/test/04-test-builder.js index f1c0f71..6863e5d 100644 --- a/test/04-test-builder.js +++ b/test/04-test-builder.js @@ -28,13 +28,40 @@ var east = new Rect( new vec(2,6), new vec(1,5) ) var corner = new Rect( new vec(3,7), new vec(3,7) ) var peninsula = new Rect( new vec(4,6), new vec(6,8) ) +var rect_room = new Room({ id: "rect", rect: rect, height: 2 }) +var east_room = new Room({ id: "east", rect: east, height: 2 }) +var corner_room = new Room({ id: "corner", rect: corner, height: 2 }) +var peninsula_room = new Room({ id: "peninsula", rect: peninsula, height: 2 }) + +var taller_room = new Room({ id: "taller", rect: rect, height: 3 }) + function report(a) { console.log( a.join("\n") ) } function reportSides(walls) { console.log(walls.map(function(w){ return sidesToString(w.side) }).join("\n")) } +function count_wall_sides (wall_groups) { + var wall_sides = {} + wall_sides[LEFT] = 0 + wall_sides[RIGHT] = 0 + wall_sides[FRONT] = 0 + wall_sides[BACK] = 0 + wall_sides[TOP] = 0 + wall_sides[BOTTOM] = 0 + wall_sides.total = 0 + wall_groups.map(function(walls){ + walls.forEach(function(wall){ + wall_sides[wall.side] += 1 + wall_sides.total += 1 + }) + }) + return wall_sides +} function reset(){ + Rooms.forEach(function(room){ + room.reset() + }) Rooms.list = {} Rooms.regions = [] } @@ -48,7 +75,7 @@ function rebuild(){ describe('builder', function(){ reset() - var rect_room = Rooms.add_with_rect( rect ) + Rooms.add( rect_room ) rebuild() describe('#build_walls(rect)', function(){ @@ -72,42 +99,128 @@ describe('builder', function(){ }) }) - // + // rect vs east reset() - var rect_room = Rooms.add_with_rect( rect ) - var rect_east = Rooms.add_with_rect( east ) + Rooms.add( rect_room ) + Rooms.add( east_room ) var regions = rebuild() -console.log("\n\n") -console.log(regions.join("\n")) -console.log("\n\n") - describe('#build_walls(rect, east)', function(){ - var walls = regions.map(Rooms.builder.build_walls.bind(Rooms.builder)) - walls.map(function(w){ reportSides(w); console.log("--") }) + var wall_groups = regions.map(Rooms.builder.build_walls.bind(Rooms.builder)) + var wall_sides = count_wall_sides(wall_groups) - it("should return 3 walls", function(){ - assert.equal(4, walls.length) + // reportSides(w); console.log("--") + + it("should return 8 walls", function(){ + assert.equal(8, wall_sides.total) }) - it("should have one side per wall", function(){ - assert.equal(1, bitcount(walls[0].side)) - assert.equal(1, bitcount(walls[1].side)) - assert.equal(1, bitcount(walls[2].side)) - assert.equal(1, bitcount(walls[3].side)) + it("should have 3 front walls", function(){ + assert.equal(3, wall_sides[FRONT]) + }) + it("should have 3 back walls", function(){ + assert.equal(3, wall_sides[BACK]) + }) + it("should have 1 left wall", function(){ + assert.equal(1, wall_sides[LEFT]) + }) + it("should have 1 right wall", function(){ + assert.equal(1, wall_sides[RIGHT]) }) }) describe('#build_floors(rect, east)', function(){ - var floors = Rooms.builder.build_floors(rect_room) - it("should make 2 floors", function(){ - assert.equal(2, floors.length) + var fg = Rooms.builder.build_floors(rect_room) + var fg2 = Rooms.builder.build_floors(east_room) + var fg_floors = fg.filter(function(r){ return ! r.half_side }) + var fg_halves = fg.filter(function(r){ return r.half_side }) + var fg2_floors = fg2.filter(function(r){ return ! r.half_side }) + var fg2_halves = fg2.filter(function(r){ return r.half_side }) + + it("should make 4 floors", function(){ + assert.equal(2, fg_floors.length) + assert.equal(2, fg2_floors.length) + }) + it("should make 0 half-walls", function(){ + assert.equal(0, fg_halves.length) + assert.equal(0, fg2_halves.length) }) - // reportSides(floors) }) + // rect vs corner + reset() + Rooms.add( rect_room ) + Rooms.add( corner_room ) + var regions = rebuild() + + describe('#build_walls(rect, corner)', function(){ + + var wall_groups = regions.map(Rooms.builder.build_walls.bind(Rooms.builder)) + var wall_sides = count_wall_sides(wall_groups) + + // reportSides(w); console.log("--") + + it("should return 12 walls", function(){ + assert.equal(12, wall_sides.total) + }) + it("should have 3 front walls", function(){ + assert.equal(3, wall_sides[FRONT]) + }) + it("should have 3 back walls", function(){ + assert.equal(3, wall_sides[BACK]) + }) + it("should have 3 left wall", function(){ + assert.equal(3, wall_sides[LEFT]) + }) + it("should have 3 right wall", function(){ + assert.equal(3, wall_sides[RIGHT]) + }) + }) + + describe('#build_floors(rect, corner)', function(){ + var fg = Rooms.builder.build_floors(rect_room) + var fg2 = Rooms.builder.build_floors(corner_room) + var fg_floors = fg.filter(function(r){ return ! r.half_side }) + var fg_halves = fg.filter(function(r){ return r.half_side }) + var fg2_floors = fg2.filter(function(r){ return ! r.half_side }) + var fg2_halves = fg2.filter(function(r){ return r.half_side }) + + it("should make 4 floors", function(){ + assert.equal(1, fg_floors.length) + assert.equal(3, fg2_floors.length) + }) + it("should make 2 half-walls", function(){ + assert.equal(0, fg_halves.length) + assert.equal(2, fg2_halves.length) + }) + }) + + // taller (rect) vs east + + reset() + Rooms.add( taller_room ) + Rooms.add( east_room ) + var regions = rebuild() + + describe('#build_floors(taller, east)', function(){ + var fg = Rooms.builder.build_floors(taller_room) + var fg2 = Rooms.builder.build_floors(east_room) + var fg_floors = fg.filter(function(r){ return ! r.half_side }) + var fg_halves = fg.filter(function(r){ return r.half_side }) + var fg2_floors = fg2.filter(function(r){ return ! r.half_side }) + var fg2_halves = fg2.filter(function(r){ return r.half_side }) + + it("should make 4 floors", function(){ + assert.equal(2, fg_floors.length) + assert.equal(2, fg2_floors.length) + }) + it("should make 1 half-wall", function(){ + assert.equal(0, fg_halves.length) + assert.equal(1, fg2_halves.length) + }) + }) -- cgit v1.2.3-70-g09d2 From 6d56a504fc11bdb42dab960c08281059a4cc63d5 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Fri, 25 Jul 2014 02:02:58 -0400 Subject: corner tests passing --- .../javascripts/rectangles/engine/rooms/builder.js | 11 ++---- test/04-test-builder.js | 43 +++++++++++++++++----- 2 files changed, 38 insertions(+), 16 deletions(-) (limited to 'test/04-test-builder.js') diff --git a/public/assets/javascripts/rectangles/engine/rooms/builder.js b/public/assets/javascripts/rectangles/engine/rooms/builder.js index c1cba39..dfabc86 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/builder.js +++ b/public/assets/javascripts/rectangles/engine/rooms/builder.js @@ -201,7 +201,7 @@ var depth = region.y.length() var height = hi.height - lo.height - if (! (region.half_sides & LEFT) && region.x.a != lo.rect.x.a && region.x.a == hi.rect.x.a) { + if (! (region.half_sides & LEFT) && region.x.a == hi.rect.x.a) { el = this.make_wall('.left') el.rotationY = HALF_PI el.height = height @@ -216,8 +216,7 @@ el.half_side = LEFT } - if (! (region.half_sides & RIGHT) && region.x.b != lo.rect.x.b && region.x.b == hi.rect.x.b) { -console.log("right") + if (! (region.half_sides & RIGHT) && region.x.b == hi.rect.x.b) { el = this.make_wall('.right') el.rotationY = -HALF_PI el.height = height @@ -232,8 +231,7 @@ console.log("right") el.half_side = RIGHT } - if (! (region.half_sides & FRONT) && region.y.a != lo.rect.y.a && region.y.a == hi.rect.y.a) { -console.log("front") + if (! (region.half_sides & FRONT) && region.y.a == hi.rect.y.a) { el = this.make_wall('.front') el.width = width el.height = height @@ -248,8 +246,7 @@ console.log("front") el.half_side = FRONT } - if (! (region.half_sides & BACK) && region.y.b != lo.rect.y.b && region.y.b == hi.rect.y.b) { -console.log("back") + if (! (region.half_sides & BACK) && region.y.b == hi.rect.y.b) { el = this.make_wall('.back') el.width = width el.height = height diff --git a/test/04-test-builder.js b/test/04-test-builder.js index 6863e5d..021ed8c 100644 --- a/test/04-test-builder.js +++ b/test/04-test-builder.js @@ -171,10 +171,10 @@ describe('builder', function(){ it("should have 3 back walls", function(){ assert.equal(3, wall_sides[BACK]) }) - it("should have 3 left wall", function(){ + it("should have 3 left walls", function(){ assert.equal(3, wall_sides[LEFT]) }) - it("should have 3 right wall", function(){ + it("should have 3 right walls", function(){ assert.equal(3, wall_sides[RIGHT]) }) }) @@ -187,13 +187,13 @@ describe('builder', function(){ var fg2_floors = fg2.filter(function(r){ return ! r.half_side }) var fg2_halves = fg2.filter(function(r){ return r.half_side }) - it("should make 4 floors", function(){ - assert.equal(1, fg_floors.length) - assert.equal(3, fg2_floors.length) + it("should make 8 floors", function(){ + assert.equal(2, fg_floors.length) + assert.equal(6, fg2_floors.length) }) - it("should make 2 half-walls", function(){ + it("should make 0 half-walls", function(){ assert.equal(0, fg_halves.length) - assert.equal(2, fg2_halves.length) + assert.equal(0, fg2_halves.length) }) }) @@ -216,9 +216,34 @@ describe('builder', function(){ assert.equal(2, fg_floors.length) assert.equal(2, fg2_floors.length) }) - it("should make 1 half-wall", function(){ + it("should make 3 half-walls", function(){ assert.equal(0, fg_halves.length) - assert.equal(1, fg2_halves.length) + assert.equal(3, fg2_halves.length) + }) + }) + + // taller vs corner + + reset() + Rooms.add( taller_room ) + Rooms.add( corner_room ) + var regions = rebuild() + + describe('#build_floors(taller, corner)', function(){ + var fg = Rooms.builder.build_floors(taller_room) + var fg2 = Rooms.builder.build_floors(corner_room) + var fg_floors = fg.filter(function(r){ return ! r.half_side }) + var fg_halves = fg.filter(function(r){ return r.half_side }) + var fg2_floors = fg2.filter(function(r){ return ! r.half_side }) + var fg2_halves = fg2.filter(function(r){ return r.half_side }) + + it("should make 8 floors", function(){ + assert.equal(2, fg_floors.length) + assert.equal(6, fg2_floors.length) + }) + it("should make 2 half-walls", function(){ + assert.equal(0, fg_halves.length) + assert.equal(2, fg2_halves.length) }) }) -- cgit v1.2.3-70-g09d2 From 4388c9ce2ae680862adb8abaf9d5b34088591a81 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Fri, 25 Jul 2014 02:36:49 -0400 Subject: document crossed half-wall gotcha --- test/04-test-builder.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'test/04-test-builder.js') diff --git a/test/04-test-builder.js b/test/04-test-builder.js index 021ed8c..fa624f8 100644 --- a/test/04-test-builder.js +++ b/test/04-test-builder.js @@ -247,10 +247,14 @@ describe('builder', function(){ }) }) - - - // Rooms.add_with_rect( corner ) - // Rooms.add_with_rect( peninsula ) + // TODO ... test half-wall generation on rooms intersecting each other crossways: + // right now this generates extra half-walls from cross on the box side of bulge + // this could be fixed by cropping regions during the cull stage? +/* + var box = new Rect( new vec(1,6), new vec(1,5) ) + var bulge = new Rect( new vec(2,5), new vec(4,6) ) + var cross = new Rect( new vec(3,4), new vec(3,7) ) +*/ }) -- cgit v1.2.3-70-g09d2