From 756a13d6fc190987c0add57f3702eb13e30c7867 Mon Sep 17 00:00:00 2001 From: Alex Barnes Date: Mon, 4 Feb 2013 21:24:42 -0600 Subject: [PATCH] Add some useful tools --- .gitignore | 2 ++ src/main/javascript/drone/chessboard.js | 30 ++++++++++++++++++++++ src/main/javascript/drone/partial.js | 14 +++++++++++ src/main/javascript/drone/rboxcall.js | 33 +++++++++++++++++++++++++ src/main/javascript/drone/streamer.js | 21 ++++++++++++++++ 5 files changed, 100 insertions(+) create mode 100644 .gitignore create mode 100644 src/main/javascript/drone/chessboard.js create mode 100644 src/main/javascript/drone/partial.js create mode 100644 src/main/javascript/drone/rboxcall.js create mode 100644 src/main/javascript/drone/streamer.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6f38f30 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target +*.class diff --git a/src/main/javascript/drone/chessboard.js b/src/main/javascript/drone/chessboard.js new file mode 100644 index 0000000..917c238 --- /dev/null +++ b/src/main/javascript/drone/chessboard.js @@ -0,0 +1,30 @@ +/** +* Creates a tile pattern of given block types and size +* +* Paramters: +* whiteBlock - blockId used for the traditional white portion of the chessboard +* blackBlock - blockId used for the traditional black portion of the chessboard +* width - width of the chessboard +* height - height of the chessboard +*/ +load(__folder + "drone.js"); + +Drone.extend("chessboard", function(whiteBlock, blackBlock, width, depth) { + this.chkpt('chessboard-start'); + + var dep = depth || width; + + for(var i = 0; i < width; ++i) { + for(var j = 0; j < dep; ++j) { + var block = blackBlock; + if((i+j)%2 == 1) { + block = whiteBlock; + } + this.box(block); + this.right(); + } + this.move('chessboard-start').fwd(i+1); + } + + return this.move('chessboard-start'); +}); diff --git a/src/main/javascript/drone/partial.js b/src/main/javascript/drone/partial.js new file mode 100644 index 0000000..8adbeac --- /dev/null +++ b/src/main/javascript/drone/partial.js @@ -0,0 +1,14 @@ +/** +* Create a partial function +* +* Parameters: +* func - base function +* [remaining arguments] - arguments bound to the partial function +*/ +function partial(func /*, 0..n args */) { + var args = Array.prototype.slice.call(arguments, 1); + return function() { + var allArguments = args.concat(Array.prototype.slice.call(arguments)); + return func.apply(this, allArguments); + }; +} diff --git a/src/main/javascript/drone/rboxcall.js b/src/main/javascript/drone/rboxcall.js new file mode 100644 index 0000000..af7019d --- /dev/null +++ b/src/main/javascript/drone/rboxcall.js @@ -0,0 +1,33 @@ +/** +* Iterates over each cube in a cubic region. For each cube has a chance to callback your +* function and provide a new drone to it. +* +* Parameters: +* callback - any function that accepts a drone as its first argument +* probability - chance to invoke your callback on each iteration +* width - width of the region +* height - (Optional) height of the region, defaults to width +* depth - (Optional) depth of the cube, defaults to width +*/ +load(__folder + "drone.js"); + +Drone.extend("rboxcall", function(callback, probability, width, height, depth) { + this.chkpt('rboxcall-start'); + + for(var i = 0; i < width; ++i) { + this.move('rboxcall-start').right(i); + for(var j = 0; j < depth; ++j) { + this.move('rboxcall-start').right(i).fwd(j); + for(var k = 0; k < height; ++k) { + if(Math.random()*100 < probability) { + callback.call(null, new Drone(this.x, this.y, this.z)); + } + this.up(); + } + } + } + + this.move('rboxcall-start'); + + return this; +}); diff --git a/src/main/javascript/drone/streamer.js b/src/main/javascript/drone/streamer.js new file mode 100644 index 0000000..3bf9b59 --- /dev/null +++ b/src/main/javascript/drone/streamer.js @@ -0,0 +1,21 @@ +/** +* Creates a stream of blocks in a given direction until it hits something other than air +* +* Parameters: +* block - blockId +* dir - "up", "down", "left", "right", "fwd", "back +* maxIterations - (Optional) maximum number of cubes to generate, defaults to 1000 +*/ +load(__folder + "drone.js"); + +Drone.extend("streamer", function(block, dir, maxIterations) { + for(var i = 0; i < maxIterations||1000; ++i) { + this.box(block); + this[dir].call(this); + if(getBlock(this.x, this.y, this.z) !== "0:0") { + break; + } + } + + return this; +});