diff --git a/src/main/js/plugins/drone/contrib/mazegen.js b/src/main/js/plugins/drone/contrib/mazegen.js new file mode 100644 index 0000000..1531504 --- /dev/null +++ b/src/main/js/plugins/drone/contrib/mazegen.js @@ -0,0 +1,107 @@ +// Maze generation based on http://rosettacode.org/wiki/Maze_generation#JavaScript + +var Drone = require('../drone').Drone; + +function maze_make(x,y) { + var n=x*y-1; + if (n<0) { + echo("illegal maze dimensions"); + return ({x: 0, y: 0}); + } + var horiz=[]; for (var j= 0; j0 && j0 && (j != here[0]+1 || k != here[1]+1)); + } + while (00 && m.verti[j/2-1][Math.floor(k/4)]) + line[k]= ' '; + else + line[k]= '-'; + else + for (var k=0; k0 && m.horiz[(j-1)/2][k/4-1]) + line[k]= ' '; + else + line[k]= '|'; + else + line[k]= ' '; + if (0 == j) line[1]= line[2]= line[3]= ' '; + if (m.x*2-1 == j) line[4*m.y]= ' '; + text.push(line.join('')+' \r\n'); // TWEAKED: space added to get an even number of columns + } + return text.join(''); +} + +// ScriptCraft stuff starts here +// Helper function to parse the ASCII art into Drone actions +// You might also consider creating a new maze_display but for now this will do the work +function maze_draw(maze_string, d) { + // d is the Drone to use + d.up().chkpt('maze-start'); + for (var j = 0; j < maze_string.length; j += 2) { + switch(maze_string.substr(j, 2)) { + case ' ': + d.box(0).fwd(); // Make sure to empty this position + break; + case '\r\n': + d.move('maze-start'); + d.right().chkpt('maze-start'); + break; + default: // '+ ', '+-', '--', '| ' + if (j == 0) { + d.box(blocks.glowstone); // highlight the maze entry and exit + } else if (j == maze_string.length - 4) { + d.box(blocks.glass); + } else { + d.box(blocks.oak); + } + d.fwd(); + } + } +} + +// User-facing code starts here +// Example: Try /js amazing(5,7) +Drone.extend('amazing', function(size_x, size_y) { + m = maze_make(size_x, size_y); + if (m.x > 0 && m.y > 0) { + maze_draw(maze_display(m), this); + } +});