Make drone build processing more evenly distributed for multiple players

This commit is contained in:
walterhiggins 2014-04-19 17:28:43 +01:00
parent 9bbc3a45ca
commit 3c7f8ae94e
6 changed files with 124 additions and 53 deletions

2
.gitignore vendored
View file

@ -15,3 +15,5 @@ build.local.properties
/src/main/javascript/plugins/.#example-1.js /src/main/javascript/plugins/.#example-1.js
/nbproject/private/private.xml /nbproject/private/private.xml
/nbproject/project.xml /nbproject/project.xml
/src/docs/nbproject/private/
/src/docs/build/

View file

@ -62,7 +62,7 @@ easy addition of 'Mods' and extensions to Minecraft. ScriptCraft is a
difficult but CraftBukkit makes it easy. Follow these steps to difficult but CraftBukkit makes it easy. Follow these steps to
Install ScriptCraft on your computer... Install ScriptCraft on your computer...
1. [Download and install CraftBukkit][dlbuk]. Then follow the [Bukkit 1. [Download and install CraftBukkit][dlbuk2] (choose either Recommended, Beta or Development) . Then follow the [Bukkit
Installation Instructions][bii]. (Tip: You can grab the very latest Installation Instructions][bii]. (Tip: You can grab the very latest
version of bukkit from the [alternative versions list][dlbuk2]) version of bukkit from the [alternative versions list][dlbuk2])
@ -139,8 +139,13 @@ Minecraft server and are ready to connect ...
4. Type any name you like in the name field then type `localhost` in the 4. Type any name you like in the name field then type `localhost` in the
address field. `localhost` is a special internet address that points to address field. `localhost` is a special internet address that points to
your own computer. your own computer.
5. Click 'Join Server' to join the craftbukkit server. 5. Click 'Join Server' to join the craftbukkit server. If the version
If you get a message of client disconnected, check your client version. If it's 1.7.2, you need to edit your profile and select 1.6.4 to match craftbukkit. of Minecraft is incompatible with the version of CraftBukkit you will
not be able to connect to the server. To fix this, you can create a
Minecraft profile in your client. Profiles let you decide which
version of Minecraft client you want to run so that your client and
server are compatible.
6. Once you've joined the game, press the `/` key located at the bottom 6. Once you've joined the game, press the `/` key located at the bottom
right of your keyboard. A prompt will appear. Type the following then right of your keyboard. A prompt will appear. Type the following then
press enter: `js 1 + 1` The number 2 should be displayed. press enter: `js 1 + 1` The number 2 should be displayed.
@ -1269,7 +1274,6 @@ different objects and methods available for use by ScriptCraft.
[buk]: http://wiki.bukkit.org/Setting_up_a_server [buk]: http://wiki.bukkit.org/Setting_up_a_server
[dlbuk]: http://dl.bukkit.org/
[dlbuk2]: http://dl.bukkit.org/downloads/craftbukkit/ [dlbuk2]: http://dl.bukkit.org/downloads/craftbukkit/
[bii]: http://wiki.bukkit.org/Setting_up_a_server [bii]: http://wiki.bukkit.org/Setting_up_a_server
[sc-plugin]: http://scriptcraftjs.org/download/ [sc-plugin]: http://scriptcraftjs.org/download/

14
nbproject/ide-targets.xml Normal file
View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project basedir=".." name="scriptcraft-IDE">
<import file="../build.xml"/>
<!-- TODO: edit the following target according to your needs -->
<!-- (more info: http://www.netbeans.org/kb/articles/freeform-config.html#debugj2se) -->
<target depends="server-setup, package, update-live-cb" description="Starts Bukkit with ScriptCraft" name="debug-nb">
<echo>Starting Bukkit with ScriptCraft</echo>
<nbjpdastart addressproperty="jpda.address" name="scriptcraft" transport="dt_socket"/>
<java dir="${minecraft.dir}" fork="true" jar="${minecraft.dir}/craftbukkit.jar" maxmemory="1024m">
<jvmarg value="-Xdebug"/>
<jvmarg value="-Xrunjdwp:transport=dt_socket,address=${jpda.address}"/>
</java>
</target>
</project>

View file

@ -26,7 +26,7 @@ easy addition of 'Mods' and extensions to Minecraft. ScriptCraft is a
difficult but CraftBukkit makes it easy. Follow these steps to difficult but CraftBukkit makes it easy. Follow these steps to
Install ScriptCraft on your computer... Install ScriptCraft on your computer...
1. [Download and install CraftBukkit][dlbuk]. Then follow the [Bukkit 1. [Download and install CraftBukkit][dlbuk2] (choose either Recommended, Beta or Development) . Then follow the [Bukkit
Installation Instructions][bii]. (Tip: You can grab the very latest Installation Instructions][bii]. (Tip: You can grab the very latest
version of bukkit from the [alternative versions list][dlbuk2]) version of bukkit from the [alternative versions list][dlbuk2])
@ -103,7 +103,13 @@ Minecraft server and are ready to connect ...
4. Type any name you like in the name field then type `localhost` in the 4. Type any name you like in the name field then type `localhost` in the
address field. `localhost` is a special internet address that points to address field. `localhost` is a special internet address that points to
your own computer. your own computer.
5. Click 'Join Server' to join the craftbukkit server. 5. Click 'Join Server' to join the craftbukkit server. If the version
of Minecraft is incompatible with the version of CraftBukkit you will
not be able to connect to the server. To fix this, you can create a
Minecraft profile in your client. Profiles let you decide which
version of Minecraft client you want to run so that your client and
server are compatible.
6. Once you've joined the game, press the `/` key located at the bottom 6. Once you've joined the game, press the `/` key located at the bottom
right of your keyboard. A prompt will appear. Type the following then right of your keyboard. A prompt will appear. Type the following then
press enter: `js 1 + 1` The number 2 should be displayed. press enter: `js 1 + 1` The number 2 should be displayed.
@ -1232,7 +1238,6 @@ different objects and methods available for use by ScriptCraft.
[buk]: http://wiki.bukkit.org/Setting_up_a_server [buk]: http://wiki.bukkit.org/Setting_up_a_server
[dlbuk]: http://dl.bukkit.org/
[dlbuk2]: http://dl.bukkit.org/downloads/craftbukkit/ [dlbuk2]: http://dl.bukkit.org/downloads/craftbukkit/
[bii]: http://wiki.bukkit.org/Setting_up_a_server [bii]: http://wiki.bukkit.org/Setting_up_a_server
[sc-plugin]: http://scriptcraftjs.org/download/ [sc-plugin]: http://scriptcraftjs.org/download/

View file

@ -14,17 +14,20 @@ var Drone = require('../drone').Drone;
Drone.extend('cottage',function ( ) { Drone.extend('cottage',function ( ) {
this.chkpt('cottage') this.chkpt('cottage')
.box0(48,7,2,6) // 4 walls .box0(48,7,2,6) // 4 walls
.right(3).door() // door front and center .right(3)
.up(1).left(2).box(102) // windows to left and right .door() // door front and center
.right(4).box(102) .up(1)
.left(5).up().prism0(53,7,6); .left(2)
// .box(102) // windows to left and right
// put up a sign near door. .right(4)
// .box(102)
this.down().right(4) .left(5)
.sign(['Home','Sweet','Home'],68); .up()
.prism0(53,7,6)
return this.move('cottage'); .down()
.right(4)
.sign(['Home','Sweet','Home'],68)
.move('cottage');
}); });
// //
// a more complex script that builds an tree-lined avenue with // a more complex script that builds an tree-lined avenue with
@ -41,8 +44,9 @@ Drone.extend('cottage_road', function( numberCottages ) {
var cottagesPerSide = Math.floor(numberCottages/2); var cottagesPerSide = Math.floor(numberCottages/2);
this this
.chkpt('cottage_road') // make sure the drone's state is saved. .chkpt('cottage_road') // make sure the drone's state is saved.
.box(43,3,1,cottagesPerSide*(distanceBetweenTrees+1)) // build the road .box( 43, 3, 1, cottagesPerSide * ( distanceBetweenTrees + 1 ) ) // build the road
.up().right() // now centered in middle of road .up()
.right() // now centered in middle of road
.chkpt('cr'); // will be returning to this position later .chkpt('cr'); // will be returning to this position later
// //
@ -50,26 +54,39 @@ Drone.extend('cottage_road', function( numberCottages ) {
// //
for ( ; i < cottagesPerSide+1;i++ ) { for ( ; i < cottagesPerSide+1;i++ ) {
this this
.left(5).oak() .left(5)
.right(10).oak() .oak()
.right(10)
.oak()
.left(5) // return to middle of road .left(5) // return to middle of road
.fwd(distanceBetweenTrees+1); // move forward. .fwd( distanceBetweenTrees + 1 ); // move forward.
} }
this.move('cr').back(6); // move back 1/2 the distance between trees this
.move('cr')
.back(6); // move back 1/2 the distance between trees
// this function builds a path leading to a cottage. // this function builds a path leading to a cottage.
function pathAndCottage( d ) { function pathAndCottage( drone ) {
return d.down().box(43,1,1,5).fwd(5).left(3).up().cottage(); drone
.down()
.box(43,1,1,5)
.fwd(5)
.left(3)
.up()
.cottage();
return drone;
}; };
// //
// step 3 build cottages on each side // step 3 build cottages on each side
// //
for ( i = 0; i < cottagesPerSide; i++ ) { for ( i = 0; i < cottagesPerSide; i++ ) {
this.fwd(distanceBetweenTrees+1).chkpt('r'+i); this
.fwd( distanceBetweenTrees + 1 )
.chkpt('r'+i);
// build cottage on left // build cottage on left
pathAndCottage( this.turn(3) ).move( 'r' + i ); pathAndCottage( this.turn(3) ).move( 'r' + i );
// build cottage on right // build cottage on right
pathAndCottage(this.turn()).move( 'r' + i ); pathAndCottage( this.turn() ).move( 'r' + i );
} }
// return drone to where it was at start of function // return drone to where it was at start of function
return this.move('cottage_road'); return this.move('cottage_road');

View file

@ -658,13 +658,13 @@ var putBlock = function( x, y, z, blockId, metadata, world ) {
} }
}; };
var putSign = function( texts, x, y, z, blockId, meta, world, immediate ) { var putSign = function( drone, x, y, z, world, texts, blockId, meta, immediate ) {
var i, var i,
block, block,
state; state;
if ( !immediate ) { if ( !immediate ) {
Drone.queue.push(function(){ putSign(texts, x, y, z, blockId, meta, world, true); }); getQueue(drone).push(function(){ putSign( drone, x, y, z, world, texts, blockId, meta, true); });
return; return;
} }
if ( blockId != 63 && blockId != 68 ) { if ( blockId != 63 && blockId != 68 ) {
@ -755,21 +755,33 @@ exports.Drone = Drone;
exports.blocks = blocks; exports.blocks = blocks;
Drone.queue = []; Drone.queue = [];
Drone.opsPerSec = 10; Drone.opsPerSec = 10;
Drone.processQueue = function(){ Drone.processQueue = function(){
var process = Drone.queue.shift(); var process,
if (process){ i = 0,
try { queues = getAllQueues();
process();
} catch( e ) { for ( ; i < queues.length; i++ ) {
console.log('Drone build error: %s', e); process = queues[i].shift();
if (process){
try {
process();
} catch( e ) {
console.log('Drone build error: %s', e);
}
} }
} }
setTimeout(Drone.processQueue,1000/Drone.opsPerSec); setTimeout( Drone.processQueue, 1000 / Drone.opsPerSec );
}; };
setTimeout(Drone.processQueue,1000/Drone.opsPerSec); setTimeout( Drone.processQueue, 1000 / Drone.opsPerSec );
addUnloadHandler(function(){
var pendingBuildOps = Drone.queue.length; addUnloadHandler( function() {
var pendingBuildOps = 0;
var allQueues = getAllQueues();
for (var i = 0; i < allQueues.length; i++){
pendingBuildOps += allQueues[i].length;
}
if (pendingBuildOps > 0){ if (pendingBuildOps > 0){
console.warn('There were ' + pendingBuildOps + ' pending build operations which were cancelled'); console.warn('There were ' + pendingBuildOps + ' pending build operations which were cancelled');
} }
@ -792,15 +804,7 @@ Drone.extend = function( name, func ) {
global[name] = function( ) { global[name] = function( ) {
var result = new Drone( self ); var result = new Drone( self );
var len = Drone.queue.length;
result[name].apply( result, arguments ); result[name].apply( result, arguments );
var newLen = Drone.queue.length;
if ( len > (3 * Drone.opsPerSec) || (newLen - len) > (3 * Drone.opsPerSec)) {
if ( result.player && !result.playerNotifiedPending ) {
result.player.sendMessage('Build queue will complete in ' + Math.ceil( newLen / Drone.opsPerSec ) + ' seconds (approx.)');
result.playerNotifiedPending = true;
}
}
return result; return result;
}; };
}; };
@ -1008,12 +1012,37 @@ Drone.extend( 'sign', function( message, block ) {
if ( block == 63 ) { if ( block == 63 ) {
meta = ( 12 + ( ( this.dir + 2 ) * 4 ) ) % 16; meta = ( 12 + ( ( this.dir + 2 ) * 4 ) ) % 16;
} }
putSign( message, this.x, this.y, this.z, block, meta, this.world ); putSign( this, this.x, this.y, this.z, this.world, message, block, meta);
if ( block == 68 ) { if ( block == 68 ) {
this.fwd(); this.fwd();
} }
}); });
var playerQueues = {};
/*
if the drone has an associated player, then use that player's queue otherwise
use the global queue.
*/
function getQueue( drone ){
if ( drone.player ) {
var playerName = ''+drone.player.name;
var result = playerQueues[playerName];
if (result === undefined){
playerQueues[playerName] = [];
return playerQueues[playerName];
}
return result;
} else {
return Drone.queue;
}
}
function getAllQueues() {
var result = [ Drone.queue ];
for (var pq in playerQueues) {
result.push(playerQueues[pq]) ;
}
return result;
}
Drone.prototype.cuboida = function(/* Array */ blocks, w, h, d, overwrite, immediate ) { Drone.prototype.cuboida = function(/* Array */ blocks, w, h, d, overwrite, immediate ) {
var len = blocks.length, var len = blocks.length,
@ -1024,7 +1053,7 @@ Drone.prototype.cuboida = function(/* Array */ blocks, w, h, d, overwrite, immed
blocks[i] = this._getBlockIdAndMeta( blocks[ i ] ); blocks[i] = this._getBlockIdAndMeta( blocks[ i ] );
} }
var clone = Drone.clone(this); var clone = Drone.clone(this);
Drone.queue.push(this.cuboida.bind(clone, blocks, w, h, d, overwrite, true) ); getQueue(this).push(this.cuboida.bind(clone, blocks, w, h, d, overwrite, true) );
return this; return this;
} }
if ( typeof overwrite == 'undefined' ) { if ( typeof overwrite == 'undefined' ) {
@ -1096,7 +1125,7 @@ Drone.prototype.cuboidX = function( blockType, meta, w, h, d, immediate ) {
if ( !immediate ) { if ( !immediate ) {
var clone = Drone.clone(this); var clone = Drone.clone(this);
Drone.queue.push(this.cuboidX.bind(clone, blockType, meta, w, h, d, true)); getQueue(this).push(this.cuboidX.bind(clone, blockType, meta, w, h, d, true));
return this; return this;
} }
var depthFunc = function( ) { var depthFunc = function( ) {
@ -1550,7 +1579,7 @@ var _paste = function( name, immediate )
{ {
if ( !immediate ) { if ( !immediate ) {
Drone.queue.push(function(){ _paste(name, true);}); getQueue(this).push(function(){ _paste(name, true);});
return; return;
} }
var ccContent = Drone.clipBoard[name]; var ccContent = Drone.clipBoard[name];