load drone.js

This commit is contained in:
walterhiggins 2013-01-23 00:02:57 +00:00
parent fa03eb6ccd
commit a8f0d0bdf9
7 changed files with 159 additions and 152 deletions

View file

@ -8,10 +8,10 @@
plugin (name, interface, isPersistent) plugin (name, interface, isPersistent)
- defines a new plugin. If isPersistent is true then - defines a new plugin. If isPersistent is true then
the plugin doesn't have to worry about loading and saving the plugin doesn't have to worry about loading and saving
state - that will be done by the framework. Just make sure state - that will be done by the framework. Just make sure
that anything you want to save (and restore) is in the 'store' that anything you want to save (and restore) is in the 'store'
property - this will be created automatically if not already defined. property - this will be created automatically if not already defined.
(its type is object {} ) (its type is object {} )
ready (function) - specifies code to be executed only when all the plugins have loaded. ready (function) - specifies code to be executed only when all the plugins have loaded.
@ -58,8 +58,8 @@ var verbose = verbose || false;
__engine.put("__folder",(parent?_canonize(parent):"")+"/"); __engine.put("__folder",(parent?_canonize(parent):"")+"/");
result = __engine.eval(reader); result = __engine.eval(reader);
}else{ }else{
if (warnOnFileNotFound) if (warnOnFileNotFound)
__plugin.logger.warning(canonizedFilename + " not found"); __plugin.logger.warning(canonizedFilename + " not found");
} }
return result; return result;
}; };
@ -104,7 +104,7 @@ var verbose = verbose || false;
// as dependencies by myMiniGame.js and do not need to be loaded via js reload // as dependencies by myMiniGame.js and do not need to be loaded via js reload
// //
for (var i = 0;i < jsFiles.length; i++){ for (var i = 0;i < jsFiles.length; i++){
load(_canonize(jsFiles[i])); load(_canonize(jsFiles[i]),true);
} }
}; };
@ -113,13 +113,13 @@ var verbose = verbose || false;
*/ */
var _save = function(object, filename){ var _save = function(object, filename){
print(filename); print(filename);
var objectToStr = null; var objectToStr = null;
try{ try{
objectToStr = JSON.stringify(object); objectToStr = JSON.stringify(object);
}catch(e){ }catch(e){
print("ERROR: " + e.getMessage() + " while saving " + filename); print("ERROR: " + e.getMessage() + " while saving " + filename);
return; return;
} }
var f = new java.io.File(filename); var f = new java.io.File(filename);
var out = new java.io.PrintWriter(new java.io.FileWriter(f)); var out = new java.io.PrintWriter(new java.io.FileWriter(f));
out.println("__data = " + objectToStr); out.println("__data = " + objectToStr);
@ -154,7 +154,7 @@ var verbose = verbose || false;
var _ready = function( func ){ var _ready = function( func ){
_deferred.push(func); _deferred.push(func);
}; };
var _cmdInterceptors = []; var _cmdInterceptors = [];
/* /*
command management - allow for non-ops to execute approved javascript code. command management - allow for non-ops to execute approved javascript code.
*/ */
@ -165,36 +165,36 @@ var verbose = verbose || false;
if (__cmdArgs.length === 0) if (__cmdArgs.length === 0)
throw new Error("Usage: jsp command-name command-parameters"); throw new Error("Usage: jsp command-name command-parameters");
var name = __cmdArgs[0]; var name = __cmdArgs[0];
var cmd = _commands[name]; var cmd = _commands[name];
if (typeof cmd === "undefined"){ if (typeof cmd === "undefined"){
// it's not a global command - pass it on to interceptors // it's not a global command - pass it on to interceptors
var intercepted = false; var intercepted = false;
for (var i = 0;i < _cmdInterceptors.length;i++){ for (var i = 0;i < _cmdInterceptors.length;i++){
if (_cmdInterceptors[i](__cmdArgs)) if (_cmdInterceptors[i](__cmdArgs))
intercepted = true; intercepted = true;
} }
if (!intercepted) if (!intercepted)
__self.sendMessage("Command '" + name + "' is not recognised"); __self.sendMessage("Command '" + name + "' is not recognised");
}else{ }else{
func = cmd.callback; func = cmd.callback;
var params = []; var params = [];
for (var i =1; i < __cmdArgs.length;i++){ for (var i =1; i < __cmdArgs.length;i++){
params.push("" + __cmdArgs[i]); params.push("" + __cmdArgs[i]);
} }
return func(params); return func(params);
} }
}else{ }else{
if (typeof options == "undefined") if (typeof options == "undefined")
options = []; options = [];
_commands[name] = {callback: func, options: options}; _commands[name] = {callback: func, options: options};
if (intercepts) if (intercepts)
_cmdInterceptors.push(func); _cmdInterceptors.push(func);
return func; return func;
} }
}; };
var _rmCommand = function(name){ var _rmCommand = function(name){
delete _commands[name]; delete _commands[name];
}; };
/* /*
Tab Completion of the /js and /jsp commands Tab Completion of the /js and /jsp commands
*/ */
@ -250,13 +250,13 @@ var verbose = verbose || false;
var __onTabCompleteJSP = function() { var __onTabCompleteJSP = function() {
var result = global.__onTC_result; var result = global.__onTC_result;
var args = global.__onTC_args; var args = global.__onTC_args;
var cmd = _commands[args[0]]; var cmd = _commands[args[0]];
if (cmd) if (cmd)
for (var i = 0;i < cmd.options.length; i++) for (var i = 0;i < cmd.options.length; i++)
result.add(cmd.options[i]); result.add(cmd.options[i]);
else else
for (var i in _commands) for (var i in _commands)
result.add(i); result.add(i);
return result; return result;
}; };
/* /*
@ -343,13 +343,13 @@ var verbose = verbose || false;
return JSON.stringify([""+location.world.name,location.x, location.y, location.z]); return JSON.stringify([""+location.world.name,location.x, location.y, location.z]);
}; };
var _getPlayerObject = function(player){ var _getPlayerObject = function(player){
if (typeof player == "undefined") if (typeof player == "undefined")
return __self; return __self;
if (typeof player == "string") if (typeof player == "string")
return org.bukkit.Bukkit.getPlayer(player); return org.bukkit.Bukkit.getPlayer(player);
return player; return player;
}; };
global.load = _load; global.load = _load;
global.save = _save; global.save = _save;
global.reload = _reload; global.reload = _reload;
@ -358,7 +358,7 @@ var verbose = verbose || false;
global.command = _command; global.command = _command;
global._onTabComplete = __onTabCompleteJS; global._onTabComplete = __onTabCompleteJS;
global.locationToString = _locToString; global.locationToString = _locToString;
global.getPlayerObject = _getPlayerObject; global.getPlayerObject = _getPlayerObject;
// //
// assumes this was loaded from js-plugins/core/ // assumes this was loaded from js-plugins/core/
// load all of the plugins. // load all of the plugins.

View file

@ -1,49 +1,50 @@
load(__folder + "drone.js");
// //
// a castle is just a big wide fort with 4 taller forts at each corner // a castle is just a big wide fort with 4 taller forts at each corner
// //
Drone.extend('castle', function(side, height) Drone.extend('castle', function(side, height)
{ {
// //
// use sensible default parameter values // use sensible default parameter values
// if no parameters are supplied // if no parameters are supplied
// //
if (typeof side == "undefined") if (typeof side == "undefined")
side = 24; side = 24;
if (typeof height == "undefined") if (typeof height == "undefined")
height = 10; height = 10;
if (height < 8 || side < 20) if (height < 8 || side < 20)
throw new java.lang.RuntimeException("Castles must be at least 20 wide X 8 tall"); throw new java.lang.RuntimeException("Castles must be at least 20 wide X 8 tall");
// //
// remember where the drone is so it can return 'home' // remember where the drone is so it can return 'home'
// //
this.chkpt('castle'); this.chkpt('castle');
// //
// how big the towers at each corner will be... // how big the towers at each corner will be...
// //
var towerSide = 10; var towerSide = 10;
var towerHeight = height+4; var towerHeight = height+4;
// //
// the main castle building will be front and right of the first tower // the main castle building will be front and right of the first tower
// //
this.fwd(towerSide/2).right(towerSide/2); this.fwd(towerSide/2).right(towerSide/2);
// //
// the castle is really just a big fort with 4 smaller 'tower' forts at each corner // the castle is really just a big fort with 4 smaller 'tower' forts at each corner
// //
this.fort(side,height); this.fort(side,height);
// //
// move back to start position // move back to start position
// //
this.move('castle'); this.move('castle');
// //
// now place 4 towers at each corner (each tower is another fort) // now place 4 towers at each corner (each tower is another fort)
// //
for (var corner = 0; corner < 4; corner++) for (var corner = 0; corner < 4; corner++)
{ {
// construct a 'tower' fort // construct a 'tower' fort
this.fort(towerSide,towerHeight); this.fort(towerSide,towerHeight);
// move forward the length of the castle then turn right // move forward the length of the castle then turn right
this.fwd(side+towerSide-1).turn(); this.fwd(side+towerSide-1).turn();
} }
return this.move('castle'); return this.move('castle');
}); });

View file

@ -1,3 +1,4 @@
load(__folder + "drone.js");
// //
// need to use the drone module to create buildings easily // need to use the drone module to create buildings easily
// it can be done using calls to putBlock(), putSign(), getPlayerPos() and getMousePos() // it can be done using calls to putBlock(), putSign(), getPlayerPos() and getMousePos()

View file

@ -1,3 +1,4 @@
load(__folder + "drone.js")
// //
// Create a floor of colored tiles some of which emit light. // Create a floor of colored tiles some of which emit light.
// The tiles change color every second creating a strobe-lit dance-floor. // The tiles change color every second creating a strobe-lit dance-floor.
@ -22,11 +23,11 @@ Drone.extend('dancefloor',function(width,length)
// //
// strobe gets called in a java thread - disco only lasts 30 seconds. // strobe gets called in a java thread - disco only lasts 30 seconds.
// //
var discoTicks = 30; var discoTicks = 30;
var strobe = function() var strobe = function()
{ {
while(discoTicks--) while(discoTicks--)
{ {
disco.rand(floorTiles,width,1,length); disco.rand(floorTiles,width,1,length);
java.lang.Thread.sleep(1000); java.lang.Thread.sleep(1000);
} }

View file

@ -1,60 +1,61 @@
load(__folder + "drone.js");
// //
// constructs a medieval fort // constructs a medieval fort
// //
Drone.extend('fort', function(side, height) Drone.extend('fort', function(side, height)
{ {
if (typeof side == "undefined") if (typeof side == "undefined")
side = 18; side = 18;
if (typeof height == "undefined") if (typeof height == "undefined")
height = 6; height = 6;
// make sure side is even // make sure side is even
if (side%2) if (side%2)
side++; side++;
if (height < 4 || side < 10) if (height < 4 || side < 10)
throw new java.lang.RuntimeException("Forts must be at least 10 wide X 4 tall"); throw new java.lang.RuntimeException("Forts must be at least 10 wide X 4 tall");
var brick = 98; var brick = 98;
// //
// build walls. // build walls.
// //
this.chkpt('fort').box0(brick,side,height-1,side); this.chkpt('fort').box0(brick,side,height-1,side);
// //
// build battlements // build battlements
// //
this.up(height-1); this.up(height-1);
for (i = 0;i <= 3;i++){ for (i = 0;i <= 3;i++){
var turret = []; var turret = [];
this.box(brick) // solid brick corners this.box(brick) // solid brick corners
.up().box('50:5').down() // light a torch on each corner .up().box('50:5').down() // light a torch on each corner
.fwd(); .fwd();
turret.push('109:'+ Drone.PLAYER_STAIRS_FACING[this.dir]); turret.push('109:'+ Drone.PLAYER_STAIRS_FACING[this.dir]);
turret.push('109:'+ Drone.PLAYER_STAIRS_FACING[(this.dir+2)%4]); turret.push('109:'+ Drone.PLAYER_STAIRS_FACING[(this.dir+2)%4]);
this.box(turret,1,1,side-2).fwd(side-2).turn(); this.box(turret,1,1,side-2).fwd(side-2).turn();
} }
// //
// build battlement's floor // build battlement's floor
// //
this.move('fort'); this.move('fort');
this.up(height-2).fwd().right().box('126:0',side-2,1,side-2); this.up(height-2).fwd().right().box('126:0',side-2,1,side-2);
var battlementWidth = 3; var battlementWidth = 3;
if (side <= 12) if (side <= 12)
battlementWidth = 2; battlementWidth = 2;
this.fwd(battlementWidth).right(battlementWidth) this.fwd(battlementWidth).right(battlementWidth)
.box(0,side-((1+battlementWidth)*2),1,side-((1+battlementWidth)*2)); .box(0,side-((1+battlementWidth)*2),1,side-((1+battlementWidth)*2));
// //
// add door // add door
// //
var torch = '50:' + Drone.PLAYER_TORCH_FACING[this.dir]; var torch = '50:' + Drone.PLAYER_TORCH_FACING[this.dir];
this.move('fort').right((side/2)-1).door2() // double doors this.move('fort').right((side/2)-1).door2() // double doors
.back().left().up().box(torch) // left torch .back().left().up().box(torch) // left torch
.right(3).box(torch); // right torch .right(3).box(torch); // right torch
// //
// add ladder up to battlements // add ladder up to battlements
// //
var ladder = '65:' + Drone.PLAYER_SIGN_FACING[(this.dir+2)%4]; var ladder = '65:' + Drone.PLAYER_SIGN_FACING[(this.dir+2)%4];
this.move('fort').right((side/2)-3).fwd(1) // move inside fort this.move('fort').right((side/2)-3).fwd(1) // move inside fort
.box(ladder, 1,height-1,1); .box(ladder, 1,height-1,1);
return this.move('fort'); return this.move('fort');
}); });

View file

@ -1,3 +1,5 @@
load(__folder + "drone.js");
Drone.extend('sphere', function(block,radius) Drone.extend('sphere', function(block,radius)
{ {
var lastRadius = radius; var lastRadius = radius;

View file

@ -1,3 +1,4 @@
load(__folder + "drone.js")
// //
// constructs a mayan temple // constructs a mayan temple
// //