Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- importPackage(Packages.com.sk89q.worldedit);
- importPackage(Packages.com.sk89q.worldedit.blocks);
- importPackage(Packages.com.sk89q.worldedit.math);
- /*WorldEditPlugin wed = null; */
- //var wed = (WorldEditPlugin) this.mainPlugin.getServer().getPluginManager().getPlugin("WorldEdit");
- //sel = Selection;
- //context.print(getNativeMinimumPoint().getBlockX() + "\n" + getNativeMinimumPoint().getBlockY() + "\n" + getNativeMinimumPoint().getBlockZ());
- //context.print(player.getBlockOn());
- //context.print(player.getBlockIn());
- //context.print(player.getCardinalDirection());
- //context.print(PlayerDirection.NORTH);
- //context.print("Hasta acá funcionó!");
- function contarBloques(altura){
- var total = 0;
- for (i=0; i<altura; i++){
- total += i*i;
- }
- return total;
- }
- paredes = { north : 0, south : 1, west : 2, east : 3 };
- var tiempo = new Date();
- var seed = tiempo.getTime();
- function getRandomInt(min, max) {
- return Math.floor(random() * (max - min)) + min;
- }
- function random() {
- var x = Math.sin(seed++) * 10000;
- return x - Math.floor(x);
- }
- //Objeto principal del laberinto
- function Maze(){
- this.thickness=1;
- this.width=0;
- this.height=0;
- this.casillas = [];
- this.backtrack = [];
- this.x=0;
- this.y=0;
- this.vcells = 0;
- }
- Maze.prototype.setThickness = function(t){
- if (t>=1){
- this.thickness = t;
- }
- }
- Maze.prototype.setHeight = function(h){
- this.height = h;
- };
- Maze.prototype.setWidth = function(w){
- this.width = w;
- };
- //Método para la creación del laberinto
- Maze.prototype.generate = function(){
- this.vcells = 0;
- for (y=0; y<this.height; y++){
- this.casillas[y] = new Array();
- for (x=0; x<this.width; x++){
- var celda = new Celda();
- celda.x = x;
- celda.y = y;
- this.casillas[y].push(celda);
- }
- }
- this.backtrack = [];
- this.x = getRandomInt(0,this.width);
- this.y = getRandomInt(0,this.height);
- var cnt = this.height * this.width;
- this.backtrack.push({ x : this.x , y : this.y });
- this.casillas[this.y][this.x].visited=1;
- while (this.backtrack.length > 0) {
- vecinos = this.getNeightors(this.x,this.y);
- if (vecinos.length>0){
- var d = getRandomInt(0, vecinos.length);
- var vx,vy;
- vx = vecinos[d].x;
- vy = vecinos[d].y;
- //this.casillas[this.y][this.x].visited=1;
- this.casillas[vy][vx].visited=1;
- this.backtrack.push({ x : vecinos[d].x , y : vecinos[d].y });
- if (vx < this.x){ //west
- this.casillas[vy][vx].east = 0;
- this.casillas[this.y][this.x].west = 0;
- }
- if (vx > this.x){ //east
- this.casillas[vy][vx].west = 0;
- this.casillas[this.y][this.x].east = 0;
- }
- if (vy < this.y){ //north
- this.casillas[vy][vx].south = 0;
- this.casillas[this.y][this.x].north = 0;
- }
- if (vy > this.y){ //south
- this.casillas[vy][vx].north = 0;
- this.casillas[this.y][this.x].south = 0;
- }
- this.x = vx;
- this.y = vy;
- //this.vcells++;
- //cnt--;
- } else {
- var celda = this.backtrack.pop();
- this.x = celda.x;
- this.y = celda.y;
- }
- //console.log(this.vcells);
- //console.log(this.draw());
- }
- //this.setDoors();
- };
- Maze.prototype.runDijkstra = function(){
- //this.casillas[0][0].weight = 0;
- this.dijkstra(0,0,0);
- };
- Maze.prototype.dijkstra = function(x,y,weight){
- this.casillas[y][x].weight=weight;
- var vecinos = this.getLinkedNeighbors(x,y);
- /*for (i=0; i<vecinos.length; i++){
- var vx,vy;
- vx = vecinos[i].X;
- vy = vecinos[i].Y;
- if (this.casillas[vy][vx].weight==-1){
- this.dijkstra(vx,vy,weight+1);
- }
- }*/
- };
- Maze.prototype.getLinkedNeighbors = function(x,y){
- var vecinos = [];
- if (x>0){
- if (this.casillas[y][x].west == 0){
- vecinos.push({ X : x-1, Y : y});
- }
- }
- if (x<this.width-1){
- if (this.casillas[y][x].east == 0){
- vecinos.push({ X : x+1, Y : y});
- }
- }
- if (y>0){
- if (this.casillas[y][x].north == 0){
- vecinos.push({ X : x, Y : y -1 });
- }
- }
- if (y<this.height-1){
- if (this.casillas[y][x].south == 0){
- vecinos.push({ X : x, Y : y + 1 });
- }
- }
- while(vecinos.length<4){
- vecinos.push();
- }
- return vecinos;
- };
- Maze.prototype.setDoors = function setEntranceExit(grilla){
- var entranceDirection = getRandomInt(0,4);
- var exitDirection = getRandomInt(0,4);
- while (exitDirection == entranceDirection) {
- exitDirection = getRandomInt(0,4);
- }
- var x,y;
- var alto, ancho;
- alto = grilla.length;
- ancho = grilla[0].length;
- var vertical = [], horizontal = [];
- for (i=1;i<ancho; i+=2){
- horizontal.push(i);
- }
- for (i=1; i<alto; i+=2){
- vertical.push(i);
- }
- switch (entranceDirection){
- case paredes.north:
- x = getRandomFromArray(horizontal);
- y = 0;
- grilla[y][x]=" ";
- break;
- case paredes.south:
- x = getRandomFromArray(horizontal);
- y = alto-1;
- grilla[y][x]=" ";
- break;
- case paredes.east:
- y = getRandomFromArray(vertical);
- x = ancho-1;
- grilla[y][x]=" ";
- break;
- case paredes.west:
- y = getRandomFromArray(vertical);
- x = 0;
- grilla[y][x]=" ";
- break;
- }
- console.log("Entrada-> X: " + x + ", Y: " + y);
- switch (exitDirection){
- case paredes.north:
- x = getRandomFromArray(horizontal);
- y = 0;
- grilla[y][x]=" ";
- break;
- case paredes.south:
- x = getRandomFromArray(horizontal);
- y = alto-1;
- grilla[y][x]=" ";
- break;
- case paredes.east:
- y = getRandomFromArray(vertical);
- x = ancho-1;
- grilla[y][x]=" ";
- break;
- case paredes.west:
- y = getRandomFromArray(vertical);
- x = 0;
- grilla[y][x]=" ";
- break;
- }
- console.log("Salida-> X: " + x + ", Y: " + y);
- };
- function getRandomFromArray(arreglo){
- var pos = getRandomInt(0,arreglo.length);
- return arreglo[pos];
- }
- Maze.prototype.draw = function(mode){
- var dibujo = "";
- var resultado = new Array();
- var thickness = parseInt(this.thickness);
- resultado.length = this.height*2+1+(this.height*(thickness-1));
- context.print("Length: " + resultado.length);
- for (i=0; i<this.height*2+1+(this.height*(thickness-1)); i++){
- resultado[i] = new Array();
- resultado[i].length = this.width*2+1+(this.width*(thickness-1));
- }
- var rlength = this.width*2+1+(this.width*(thickness-1));
- for (i=0; i<resultado.length; i++){
- for (n=0; n<resultado[i].length; n++){
- resultado[i][n]=" ";
- }
- }
- var offy = 0;
- var offx = 0;
- for (y=0; y<resultado.length; y++){
- resultado[y][0]="#";
- }
- for (x=0; x<rlength; x++){
- resultado[0][x]="#";
- }
- for (y=0; y<resultado.length; y+=(thickness+1)){
- context.print("Y: " + y);
- for (x=0; x<resultado[y].length; x+=(thickness+1)){
- resultado[y][x]="#";
- }
- }
- for (y=0; y<this.height; y++){
- for (x=0; x<this.width; x++){
- if (this.casillas[y][x].east==1){
- for (i = 0; i<thickness; i++) {
- resultado[y + 1 + offy + i][x + 1 + thickness + offx] ="#";
- }
- }
- if (this.casillas[y][x].south==1){
- for (i = 0; i <thickness; i++) {
- resultado[y + 1 + thickness + offy][x + 1 + offx + i] ="#";
- }
- }
- offx+=thickness;
- }
- offx=0;
- offy+= thickness;
- }
- //this.setDoors(resultado);
- if (mode == 0){
- for (i=0; i<resultado.length; i++){
- dibujo += resultado[i].join("")+"\n";
- }
- return dibujo;
- } else {
- return resultado;
- }
- };
- Maze.prototype.getNeightors = function(x,y){
- var vecinos = [];
- if (x-1 >= 0){
- if (!this.casillas[y][x-1].isVisited()){
- vecinos.push(this.casillas[y][x-1]);
- }
- }
- if (x+1 < this.width){
- if (!this.casillas[y][x+1].isVisited()){
- vecinos.push(this.casillas[y][x+1]);
- }
- }
- if (y-1 >= 0){
- if (!this.casillas[y-1][x].isVisited()){
- vecinos.push(this.casillas[y-1][x]);
- }
- }
- if (y+1 < this.height){
- if (!this.casillas[y+1][x].isVisited()){
- vecinos.push(this.casillas[y+1][x]);
- }
- }
- return vecinos;
- };
- //Clase para las celdas
- function Celda(){
- this.north = 1;
- this.south = 1;
- this.west = 1;
- this.east = 1;
- this.visited = 0;
- this.x = 0;
- this.y = 0;
- this.weight = -1;
- }
- Celda.prototype.isVisited = function(){
- return (this.visited == 1);
- };
- Celda.prototype.visit = function(dir){
- this.visited = 1;
- switch (dir){
- case paredes.north:
- this.north = 0;
- break;
- case paredes.south:
- this.south = 0;
- break;
- case paredes.west:
- this.west = 0;
- break;
- case paredes.east:
- this.east = 0;
- break;
- }
- };
- Celda.prototype.toString = function(){
- var n,s,e,w;
- n = (this.north==1);
- s = (this.south==1);
- e = (this.east==1);
- w = (this.west==1);
- if (n&&w&&s&&!e) {
- return "###\n# \n###";
- }
- if (n&&w&&e&&!s) {
- return "###\n# #\n# #";
- }
- if (n&&e&&s&&!w) {
- return "###\n #\n###";
- }
- if (s&&e&&w&&!n) {
- return "# #\n# #\n###";
- }
- if (n&&s&&!e&&!w) {
- return "###\n \n###";
- }
- if (e&&w&&!n&&!s) {
- return "# #\n# #\n# #";
- }
- if (w&&s&&!e&&!n) {
- return "# #\n# \n###";
- }
- if (w&&n&&!s&&!e) {
- return "###\n# \n# #";
- }
- if (n&&e&&!s&&!w) {
- return "###\n #\n# #";
- }
- if (e&&s&&!n&&!w) {
- return "# #\n #\n###";
- }
- if (s&&!n&&!e&&!w) {
- return "# #\n \n###";
- }
- if (w&&!n&&!s&&!e) {
- return "# #\n# \n# #";
- }
- if (n&&!e&&!s&&!w) {
- return "###\n \n# #";
- }
- if (e&&!n&&!s&&!w) {
- return "# #\n #\n# #";
- }
- return "###\n# #\n###";
- };
- var session = context.remember();
- var initBlock = player.getBlockOn();
- var initDirection = player.getCardinalDirection();
- context.checkArgs(3, 6, "<block> [length] [width] [thickness] [height] [floors]");
- var bloque = context.getBlock(argv[1]);
- var techo = context.getBlock(89);
- var length = argv[2];
- var width = argv[3];
- var height = argv[5];
- height = (height===undefined) ? 1 : height;
- var initPos = initBlock;
- switch (initDirection) {
- //context.print("initDirection:" + initDirection);
- case "SOUTHWEST":
- initPos = initPos.add(-2*width,0,0);
- break;
- case "NORTHEAST":
- initPos = initPos.add(0,0,-2*length);
- break;
- case "NORTHWEST":
- initPos = initPos.add(-2*width,0,-2*length);
- break;
- }
- var aire = context.getBlock(0);
- var maze = new Maze();
- var pisos = 1;
- var thickness = 1;
- var blocks;
- var backupPos = initPos;
- maze.setHeight(length);
- maze.setWidth(width);
- pisos = (argv[6]!== undefined) ? argv[6] : 1;
- thickness = argv[4];
- maze.setThickness(thickness);
- context.print("Argumentos: " + argv.length);
- context.print("Pisos: " + pisos);
- var step = height + 1;
- for (p=0; p<pisos; p++){
- maze.generate();
- blocks = [];
- blocks = maze.draw(1);
- //for (y=0; y<height; y++){
- for (x=0;x<blocks.length; x++){
- for (z=0; z<blocks[x].length; z++){
- newpos = Array();
- for (i=0; i<=height; i++){
- try {
- newpos.push(initPos.toVector().add(x,i+1,z));
- } catch (error) {
- newpos.push(initPos.add(x,i+1,z));
- }
- }
- // newpos = initPos.add(x,1,z);
- // newpos2 = initPos.add(x,2,z);
- if (blocks[x][z]=="#"){
- // session.setBlock(newpos,bloque);
- // session.setBlock(newpos2,bloque);
- for (i=0; i<newpos.length; i++){
- try {
- session.rawSetBlock(newpos[i].toBlockPoint(),bloque);
- } catch (error) {
- context.print("newpos["+i+"]: " + newpos[i]);
- vector = new Vector3(parseInt(newpos[i].x),parseInt(newpos[i].y),parseInt(newpos[i].z));
- session.rawSetBlock(vector.toBlockPoint(),bloque);
- }
- }
- } else {
- // session.setBlock(newpos,aire);
- // session.setBlock(newpos2,aire);
- for (i=0; i<newpos.length; i++){
- session.rawSetBlock(newpos[i].toBlockPoint(),aire);
- }
- }
- posaux = initPos.toVector().add(x,height,z);
- //session.rawSetBlock(posaux.toBlockPoint(), techo);
- }
- }
- //}
- initPos = initPos.toVector().add(0,height,0);
- context.print(initPos);
- }
Advertisement
Add Comment
Please, Sign In to add comment