simplified delayed execution of drone building.

This commit is contained in:
walterhiggins 2015-01-06 22:31:49 +00:00
parent 8d8ea69dda
commit ea84afde09
4 changed files with 44 additions and 40 deletions

View file

@ -1,3 +1,4 @@
/*global load, args, Packages*/
/* /*
This script is run at build time to generate api.md - a single Markdown document containing documentation for ScriptCraft's API This script is run at build time to generate api.md - a single Markdown document containing documentation for ScriptCraft's API
*/ */

View file

@ -319,7 +319,7 @@ function getDirFromRotation( location ) {
low-level function to place a block in the world - all drone methods which low-level function to place a block in the world - all drone methods which
place blocks ultimately invoke this function. place blocks ultimately invoke this function.
*/ */
function putBlock( x, y, z, blockId, metadata, world, dir, update ) { function putBlock( x, y, z, blockId, metadata, world, update ) {
if ( typeof metadata == 'undefined' ) { if ( typeof metadata == 'undefined' ) {
metadata = 0; metadata = 0;
} }
@ -576,7 +576,7 @@ Drone.prototype.setBlock = function(blockType, data, ow, oh, od, update){
.right(ow) .right(ow)
.up(oh) .up(oh)
.fwd(od); .fwd(od);
var result = putBlock(this.x, this.y, this.z, blockType, data, this.world, this.dir, update); var result = putBlock(this.x, this.y, this.z, blockType, data, this.world, update);
this this
.left(ow) .left(ow)
.down(oh) .down(oh)
@ -621,23 +621,7 @@ function getAllQueues() {
} }
return result; return result;
} }
Drone.prototype.cuboida = function(/* Array */ blocks, w, h, d, overwrite, immediate ) { Drone.prototype.cuboida = function(/* Array */ blocks, w, h, d, overwrite) {
//
// wph 20140823 make a copy because don't want to modify array in background
//
var blocksForBuild = blocks.slice();
var len = blocksForBuild.length,
i = 0;
if ( !immediate ) {
for ( ; i < len; i++ ) {
blocksForBuild[i] = this.getBlockIdAndMeta( blocksForBuild[ i ] );
}
var clone = Drone.clone(this);
var impl = this.cuboida.bind(clone, blocksForBuild, w, h, d, overwrite, true);
getQueue(this).push( function cuboida(){ impl(); });
return this;
}
if ( typeof overwrite == 'undefined' ) { if ( typeof overwrite == 'undefined' ) {
overwrite = true; overwrite = true;
} }
@ -650,13 +634,23 @@ Drone.prototype.cuboida = function(/* Array */ blocks, w, h, d, overwrite, immed
if ( typeof w == 'undefined' ) { if ( typeof w == 'undefined' ) {
w = 1; w = 1;
} }
//
// wph 20140823 make a copy because don't want to modify array in background
//
var blocksForBuild = blocks.slice();
var len = blocksForBuild.length,
i = 0;
for ( ; i < len; i++ ) {
blocksForBuild[i] = this.getBlockIdAndMeta( blocksForBuild[ i ] );
}
this.then(function(){
var bi = 0; var bi = 0;
traverseDHW( this, d,h,w, function traverseWidthCallback( ) { traverseDHW( this, d,h,w, function traverseWidthCallback( ) {
var properBlock = blocksForBuild[ bi % len ]; var properBlock = blocksForBuild[ bi % len ];
this.setBlock(properBlock[0], properBlock[1]); this.setBlock(properBlock[0], properBlock[1]);
bi++; bi++;
}); });
});
return this; return this;
}; };
Drone.MAX_VOLUME = 1 * MILLION; Drone.MAX_VOLUME = 1 * MILLION;
@ -694,22 +688,36 @@ Drone.prototype.cuboidX = function( blockType, meta, w, h, d, immediate ) {
return this; return this;
} }
if ( !immediate ) { if ( !immediate ) {
var clone = Drone.clone(this); this.then(function(){
var impl = this.cuboidX.bind(clone, blockType, meta, w, h, d, true);
getQueue(this).push(function cuboidX(){ impl(); });
return this;
}
traverseDHW( this, d,h,w, function( ) { traverseDHW( this, d,h,w, function( ) {
this.setBlock( blockType, meta ); this.setBlock( blockType, meta );
}); });
});
} else {
traverseDHW( this, d,h,w, function( ) {
this.setBlock( blockType, meta );
});
}
return this; return this;
}; };
/* /*
deferred execution of a drone method deferred execution of a drone method
*/ */
var thenID = 0;
Drone.prototype.then = function( next ){ Drone.prototype.then = function( next ){
getQueue(this).push( next.bind( Drone.clone(this) ) ); var chkptThen = '_now' + (thenID++);
this.chkpt(chkptThen);
var thisNext = next.bind(this);
function wrapperFn(){
var chkNow = '_now' + (thenID++);
this.chkpt(chkNow);
this.move(chkptThen);
thisNext();
this.move(chkNow);
}
getQueue(this).push( wrapperFn.bind(this) );
return this;
}; };
Drone.prototype.cuboid = function( block, w, h, d, immediate ) { Drone.prototype.cuboid = function( block, w, h, d, immediate ) {
var bm = this.getBlockIdAndMeta( block ); var bm = this.getBlockIdAndMeta( block );
@ -871,10 +879,6 @@ function traverseDHW( drone, d,h,w, callback ){
}); });
} }
Drone.clone = function(origin) {
var result = new Drone(origin.x,origin.y,origin.z, origin.dir, origin.world);
return result;
};
// //
// wph 20130130 - make this a method - extensions can use it. // wph 20130130 - make this a method - extensions can use it.
// //

View file

@ -42,8 +42,8 @@ function stairs(blockType, width, height){
if (typeof blockType === 'undefined'){ if (typeof blockType === 'undefined'){
blockType = blocks.stairs.oak; blockType = blocks.stairs.oak;
} }
this.then(function(){
var bm = this.getBlockIdAndMeta(blockType); var bm = this.getBlockIdAndMeta(blockType);
this.then(function(){
this.chkpt('_stairs'); this.chkpt('_stairs');
while (height > 0) { while (height > 0) {
this.traverseWidth(width, function(){ this.traverseWidth(width, function(){
@ -55,7 +55,6 @@ function stairs(blockType, width, height){
} }
this.move('_stairs'); this.move('_stairs');
}); });
} }
module.exports = function(Drone){ module.exports = function(Drone){
Drone.extend(stairs); Drone.extend(stairs);

View file

@ -1,4 +1,4 @@
/*global __plugin, require, org, setTimeout, addUnloadHandler, exports, global, Packages, server*/ /*global require, exports*/
var blocks = require('blocks'); var blocks = require('blocks');
exports.Drone = require('drone'); exports.Drone = require('drone');
exports.blocks = blocks; exports.blocks = blocks;