Add some useful tools

This commit is contained in:
Alex Barnes 2013-02-04 21:24:42 -06:00
parent 22add98d15
commit 756a13d6fc
5 changed files with 100 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
target
*.class

View file

@ -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');
});

View file

@ -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);
};
}

View file

@ -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;
});

View file

@ -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;
});