Performance improvements and fix for issue #47

This commit is contained in:
walterhiggins 2013-01-30 22:48:37 +00:00
parent 661ae37503
commit 35e0910b8e
4 changed files with 104 additions and 52 deletions

View file

@ -309,6 +309,12 @@ load(__folder + "drone.js");
Drone.extend('blocktype', function(message,fg,bg){ Drone.extend('blocktype', function(message,fg,bg){
this.chkpt('blocktext'); this.chkpt('blocktext');
var bmfg = this._getBlockIdAndMeta(fg);
var bmbg = null;
if (typeof bg != "undefined")
bmbg = this._getBlockIdAndMeta(bg);
var lines = message.split("\n"); var lines = message.split("\n");
var lineCount = lines.length; var lineCount = lines.length;
for (var h = 0;h < lineCount; h++) { for (var h = 0;h < lineCount; h++) {
@ -324,12 +330,12 @@ load(__folder + "drone.js");
} }
var charWidth = bits.width; var charWidth = bits.width;
if (typeof bg != "undefined") if (typeof bg != "undefined")
this.box(bg,charWidth,7,1); this.cuboidX(bmbg[0],bmbg[1],charWidth,7,1);
for (var j = 0;j < bits.pixels.length;j++){ for (var j = 0;j < bits.pixels.length;j++){
this.chkpt('btbl'); this.chkpt('btbl');
var x = bits.pixels[j][0]; var x = bits.pixels[j][0];
var y = bits.pixels[j][1]; var y = bits.pixels[j][1];
this.up(6-y).right(x).box(fg); this.up(6-y).right(x).cuboidX(bmfg[0],bmfg[1]);
this.move('btbl'); this.move('btbl');
} }
this.right(charWidth-1); this.right(charWidth-1);

View file

@ -356,41 +356,79 @@ var Drone = Drone || {
} }
return this; return this;
}; };
Drone.prototype.cuboida = function(/* Array */ blocks,w,h,d){
Drone.prototype.cuboid = function(block,w,h,d){ var properBlocks = [];
var md = 0; var len = blocks.length;
if (typeof h == "undefined"){ for (var i = 0;i < len;i++){
var bm = _getBlockIdAndMeta(blocks[i]);
properBlocks.push([bm[0],bm[1]]);
}
if (typeof h == "undefined")
h = 1; h = 1;
} if (typeof d == "undefined")
if (typeof d == "undefined"){
d = 1; d = 1;
} if (typeof w == "undefined")
if (typeof w == "undefined"){
w = 1; w = 1;
}
var bi = 0;
var that = this; var that = this;
_traverse[this.dir].width(that,w,function(){ var dir = this.dir;
_traverseHeight(that,h,function(){ var pl = org.bukkit.entity.Player;
_traverse[that.dir].depth(that,d,function(){ var cs = org.bukkit.command.BlockCommandSender;
// var world = (self instanceof pl)?self.location.world:(self instanceof cs)?self.block.location.world:null;
// wph 20121229 - slower but supports random blocks var bi = 0;
// var depthFunc = function(){
var cb = 0; var block = world.getBlockAt(that.x,that.y,that.z);
if (block.constructor == Array){ var properBlock = properBlocks[bi%len];
cb = block[bi%block.length]; block.setTypeIdAndData(properBlock[0],properBlock[1],false);
}else{
cb = block;
}
var bm = _getBlockIdAndMeta(cb);
cb = bm[0];
md = bm[1];
putBlock(that.x,that.y,that.z,cb,md);
bi++; bi++;
}); };
}); var heightFunc = function(){
}); _traverse[dir].depth(that,d,depthFunc);
};
var widthFunc = function(){
_traverseHeight(that,h,heightFunc);
};
_traverse[dir].width(that,w,widthFunc);
return this; return this;
};
/*
faster cuboid because blockid and meta must be provided
*/
Drone.prototype.cuboidX = function(blockId, meta, w, h, d){
if (typeof h == "undefined")
h = 1;
if (typeof d == "undefined")
d = 1;
if (typeof w == "undefined")
w = 1;
var that = this;
var dir = this.dir;
var pl = org.bukkit.entity.Player;
var cs = org.bukkit.command.BlockCommandSender;
var world = (self instanceof pl)?self.location.world:(self instanceof cs)?self.block.location.world:null;
var depthFunc = function(){
var block = world.getBlockAt(that.x,that.y,that.z);
block.setTypeIdAndData(blockId,meta,false);
};
var heightFunc = function(){
_traverse[dir].depth(that,d,depthFunc);
};
var widthFunc = function(){
_traverseHeight(that,h,heightFunc);
};
_traverse[dir].width(that,w,widthFunc);
return this;
};
Drone.prototype.cuboid = function(block,w,h,d){
var bm = _getBlockIdAndMeta(block);
return this.cuboidX(bm[0],bm[1],w,h,d);
}; };
Drone.prototype.cuboid0 = function(block,w,h,d){ Drone.prototype.cuboid0 = function(block,w,h,d){
return this.cuboid(block,w,h,d).fwd().right().cuboid(0,w-2,h,d-2).back().left(); return this.cuboid(block,w,h,d).fwd().right().cuboid(0,w-2,h,d-2).back().left();
@ -527,6 +565,7 @@ var Drone = Drone || {
Drone.prototype.box = Drone.prototype.cuboid; Drone.prototype.box = Drone.prototype.cuboid;
Drone.prototype.box0 = Drone.prototype.cuboid0; Drone.prototype.box0 = Drone.prototype.cuboid0;
Drone.prototype.boxa = Drone.prototype.cuboida;
// //
// show the Drone's position and direction // show the Drone's position and direction
// //
@ -535,7 +574,7 @@ var Drone = Drone || {
return "x: " + this.x + " y: "+this.y + " z: " + this.z + " dir: " + this.dir + " "+dirs[this.dir]; return "x: " + this.x + " y: "+this.y + " z: " + this.z + " dir: " + this.dir + " "+dirs[this.dir];
}; };
Drone.prototype.debug = function(){ Drone.prototype.debug = function(){
print(this); print(this.toString());
return this; return this;
}; };
@ -632,18 +671,22 @@ var Drone = Drone || {
return _cylinderX(block,radius,height,this,true); return _cylinderX(block,radius,height,this,true);
}; };
var _getDirFromRotation = function(r){ var _getDirFromRotation = function(r){
var result = 1; // 0 = east, 1 = south, 2 = west, 3 = north
r = Math.abs(Math.ceil(r)); // 46 to 135 = west
if (r >= 45 && r < 135){ // 136 to 225 = north
result = 0; // 226 to 315 = east
} // 316 to 45 = south
if (r >= 135 && r < 225){
result = 3; r = (r + 360) % 360; // east could be 270 or -90
}
if (r >= 225 && r < 315){ if (r > 45 && r <= 135)
result = 2; return 2; // west
} if (r > 135 && r <= 225)
return result; return 3; // north
if (r > 225 && r <= 315)
return 0; // east
if (r > 315 || r < 45)
return 1; // south
}; };
var _getBlockIdAndMeta = function(b){ var _getBlockIdAndMeta = function(b){
if (typeof b == 'string'){ if (typeof b == 'string'){
@ -762,7 +805,7 @@ var Drone = Drone || {
}; };
Drone.prototype.rand = function(dist,w,h,d){ Drone.prototype.rand = function(dist,w,h,d){
var randomized = _rand(dist); var randomized = _rand(dist);
return this.box(randomized,w,h,d); return this.boxa(randomized,w,h,d);
}; };
var _trees = {oak: org.bukkit.TreeType.BIG_TREE , var _trees = {oak: org.bukkit.TreeType.BIG_TREE ,
spruce: org.bukkit.TreeType.REDWOOD , spruce: org.bukkit.TreeType.REDWOOD ,
@ -911,7 +954,7 @@ var Drone = Drone || {
// //
var ops = ['up','down','left','right','fwd','back','turn', var ops = ['up','down','left','right','fwd','back','turn',
'chkpt','move', 'chkpt','move',
'box','box0','prism','prism0','cylinder','cylinder0', 'box','box0','boxa','prism','prism0','cylinder','cylinder0',
'door','door2','sign','oak','spruce','birch','jungle', 'door','door2','sign','oak','spruce','birch','jungle',
'rand','garden', 'rand','garden',
'copy','paste' 'copy','paste'
@ -925,5 +968,9 @@ var Drone = Drone || {
}; };
}(ops[i]); }(ops[i]);
} }
//
// wph 20130130 - make this a method - extensions can use it.
//
Drone.prototype._getBlockIdAndMeta = _getBlockIdAndMeta;
}()); }());

View file

@ -53,8 +53,7 @@ SnowBallFight.prototype.start = function(){};
for (var teamName in gameState.teams) { for (var teamName in gameState.teams) {
gameState.teamScores[teamName] = 0; gameState.teamScores[teamName] = 0;
var team = gameState.teams[teamName]; var team = gameState.teams[teamName];
for (var i = 0;i < team.length;i++) for (var i = 0;i < team.length;i++) {
{
var player = server.getPlayer(team[i]); var player = server.getPlayer(team[i]);
gameState.savedModes[player.name] = player.gameMode; gameState.savedModes[player.name] = player.gameMode;
player.gameMode = org.bukkit.GameMode.SURVIVAL; player.gameMode = org.bukkit.GameMode.SURVIVAL;