manusoftar

Untitled

Jan 30th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. importPackage(Packages.com.sk89q.worldedit);
  2. importPackage(Packages.com.sk89q.worldedit.blocks);
  3. importPackage(Packages.com.sk89q.worldedit.math);
  4.  
  5.  
  6.  
  7. /*WorldEditPlugin wed = null; */
  8.  
  9. //var wed = (WorldEditPlugin) this.mainPlugin.getServer().getPluginManager().getPlugin("WorldEdit");
  10.  
  11. //sel = Selection;
  12.  
  13. //context.print(getNativeMinimumPoint().getBlockX() + "\n" + getNativeMinimumPoint().getBlockY() + "\n" + getNativeMinimumPoint().getBlockZ());
  14.  
  15. //context.print(player.getBlockOn());
  16. //context.print(player.getBlockIn());
  17. //context.print(player.getCardinalDirection());
  18.  
  19. //context.print(PlayerDirection.NORTH);
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27. //context.print("Hasta acá funcionó!");
  28.  
  29.  
  30. function contarBloques(altura){
  31.      var total = 0;
  32.      for (i=0; i<altura; i++){
  33.           total += i*i;
  34.      }
  35.      return total;
  36. }
  37.  
  38.  
  39. paredes = { north : 0, south : 1, west : 2, east : 3 };
  40. var tiempo = new Date();
  41. var seed = tiempo.getTime();
  42.  
  43. function getRandomInt(min, max) {
  44.          return Math.floor(random() * (max - min)) + min;
  45. }
  46.  
  47. function random() {
  48.     var x = Math.sin(seed++) * 10000;
  49.     return x - Math.floor(x);
  50. }
  51.  
  52.  
  53. //Objeto principal del laberinto
  54. function Maze(){
  55.         this.thickness=1;
  56.     this.width=0;
  57.     this.height=0;
  58.     this.casillas = [];
  59.     this.backtrack = [];
  60.     this.x=0;
  61.     this.y=0;
  62.     this.vcells = 0;
  63. }
  64.  
  65.  
  66. Maze.prototype.setThickness = function(t){
  67.      if (t>=1){
  68.          this.thickness = t;
  69.      }
  70. }
  71.  
  72. Maze.prototype.setHeight = function(h){
  73.      this.height = h;
  74. };
  75.  
  76. Maze.prototype.setWidth = function(w){
  77.      this.width = w;
  78. };
  79.  
  80. //Método para la creación del laberinto
  81. Maze.prototype.generate = function(){
  82.    
  83.      this.vcells = 0;
  84.      for (y=0; y<this.height; y++){
  85.         this.casillas[y] = new Array();
  86.         for (x=0; x<this.width; x++){
  87.              var celda = new Celda();
  88.              celda.x = x;
  89.              celda.y = y;  
  90.              this.casillas[y].push(celda);
  91.         }
  92.      }
  93.  
  94.      this.backtrack = [];
  95.  
  96.      this.x = getRandomInt(0,this.width);
  97.      this.y = getRandomInt(0,this.height);
  98.      
  99.      var cnt = this.height * this.width;
  100.      this.backtrack.push({ x : this.x , y : this.y });
  101.      this.casillas[this.y][this.x].visited=1;
  102.      while (this.backtrack.length > 0) {
  103.            
  104.             vecinos = this.getNeightors(this.x,this.y);
  105.            
  106.             if (vecinos.length>0){
  107.                     var d = getRandomInt(0, vecinos.length);
  108.                     var vx,vy;
  109.                     vx = vecinos[d].x;
  110.                     vy = vecinos[d].y;
  111.  
  112.                     //this.casillas[this.y][this.x].visited=1;
  113.                     this.casillas[vy][vx].visited=1;
  114.  
  115.                     this.backtrack.push({ x : vecinos[d].x , y : vecinos[d].y });              
  116.  
  117.                     if (vx < this.x){ //west
  118.                         this.casillas[vy][vx].east = 0;
  119.                         this.casillas[this.y][this.x].west = 0;
  120.                     }
  121.  
  122.                     if (vx > this.x){ //east
  123.                         this.casillas[vy][vx].west = 0;
  124.                         this.casillas[this.y][this.x].east = 0;
  125.                     }
  126.  
  127.                     if (vy < this.y){ //north
  128.                         this.casillas[vy][vx].south = 0;
  129.                         this.casillas[this.y][this.x].north = 0;
  130.                     }
  131.  
  132.  
  133.                     if (vy > this.y){ //south
  134.                         this.casillas[vy][vx].north = 0;
  135.                         this.casillas[this.y][this.x].south = 0;
  136.                     }
  137.                     this.x = vx;
  138.                     this.y = vy;
  139.  
  140.                     //this.vcells++;
  141.                     //cnt--;
  142.             } else {
  143.                 var celda = this.backtrack.pop();
  144.                 this.x = celda.x;
  145.                 this.y = celda.y;
  146.             }
  147.             //console.log(this.vcells);
  148.             //console.log(this.draw());
  149.      }  
  150.  
  151.      //this.setDoors();
  152.      
  153.  
  154. };
  155.  
  156. Maze.prototype.runDijkstra = function(){
  157.          //this.casillas[0][0].weight = 0;
  158.          this.dijkstra(0,0,0);
  159. };
  160.  
  161. Maze.prototype.dijkstra = function(x,y,weight){
  162.          this.casillas[y][x].weight=weight;
  163.          var vecinos = this.getLinkedNeighbors(x,y);
  164.          
  165.  
  166.          /*for (i=0; i<vecinos.length; i++){
  167.               var vx,vy;
  168.               vx = vecinos[i].X;
  169.               vy = vecinos[i].Y;
  170.  
  171.               if (this.casillas[vy][vx].weight==-1){
  172.                   this.dijkstra(vx,vy,weight+1);
  173.               }
  174.          }*/
  175. };
  176.  
  177. Maze.prototype.getLinkedNeighbors = function(x,y){
  178.      var vecinos = [];
  179.      if (x>0){
  180.          if (this.casillas[y][x].west == 0){
  181.              vecinos.push({ X : x-1, Y : y});
  182.          }
  183.      }
  184.  
  185.      if (x<this.width-1){
  186.          if (this.casillas[y][x].east == 0){
  187.              vecinos.push({ X : x+1, Y : y});
  188.          }
  189.      }
  190.  
  191.      if (y>0){
  192.         if (this.casillas[y][x].north == 0){
  193.             vecinos.push({ X : x, Y : y -1 });
  194.         }
  195.      }
  196.  
  197.      if (y<this.height-1){
  198.         if (this.casillas[y][x].south == 0){
  199.             vecinos.push({ X : x, Y : y + 1 });
  200.         }
  201.      }
  202.  
  203.      while(vecinos.length<4){
  204.         vecinos.push();
  205.      }
  206.      return vecinos;
  207. };
  208.  
  209. Maze.prototype.setDoors = function setEntranceExit(grilla){
  210.          var entranceDirection = getRandomInt(0,4);
  211.          var exitDirection = getRandomInt(0,4);
  212.          while (exitDirection == entranceDirection) {
  213.                 exitDirection = getRandomInt(0,4);
  214.          }
  215.  
  216.          var x,y;
  217.  
  218.  
  219.          var alto, ancho;
  220.          alto = grilla.length;
  221.          ancho = grilla[0].length;
  222.  
  223.          var vertical = [], horizontal = [];
  224.          for (i=1;i<ancho; i+=2){
  225.              horizontal.push(i);
  226.          }
  227.          for (i=1; i<alto; i+=2){
  228.              vertical.push(i);
  229.          }
  230.  
  231.  
  232.  
  233.          switch (entranceDirection){
  234.                  case paredes.north:
  235.                       x = getRandomFromArray(horizontal);
  236.                       y = 0;
  237.                       grilla[y][x]=" ";
  238.                       break;
  239.                  case paredes.south:
  240.                       x = getRandomFromArray(horizontal);
  241.                       y = alto-1;
  242.                       grilla[y][x]=" ";
  243.                       break;
  244.                  case paredes.east:
  245.                       y = getRandomFromArray(vertical);
  246.                       x = ancho-1;
  247.                       grilla[y][x]=" ";
  248.                       break;
  249.                 case paredes.west:
  250.                       y = getRandomFromArray(vertical);
  251.                       x = 0;
  252.                       grilla[y][x]=" ";
  253.                       break;        
  254.  
  255.          }
  256.          console.log("Entrada-> X: " + x + ", Y: " + y);
  257.  
  258.          switch (exitDirection){
  259.                  case paredes.north:
  260.                       x = getRandomFromArray(horizontal);
  261.                       y = 0;
  262.                       grilla[y][x]=" ";
  263.                       break;
  264.                  case paredes.south:
  265.                       x = getRandomFromArray(horizontal);
  266.                       y = alto-1;
  267.                       grilla[y][x]=" ";
  268.                       break;
  269.                  case paredes.east:
  270.                       y = getRandomFromArray(vertical);
  271.                       x = ancho-1;
  272.                       grilla[y][x]=" ";
  273.                       break;
  274.                 case paredes.west:
  275.                       y = getRandomFromArray(vertical);
  276.                       x = 0;
  277.                       grilla[y][x]=" ";
  278.                       break;        
  279.  
  280.          }
  281.          console.log("Salida-> X: " + x + ", Y: " + y);
  282. };
  283.  
  284. function getRandomFromArray(arreglo){
  285.          var pos = getRandomInt(0,arreglo.length);
  286.          return arreglo[pos];
  287. }
  288.  
  289.  
  290. Maze.prototype.draw = function(mode){
  291.      var dibujo = "";
  292.      var resultado = new Array();
  293.      var thickness = parseInt(this.thickness);
  294.      resultado.length = this.height*2+1+(this.height*(thickness-1));   
  295.      context.print("Length: " + resultado.length);
  296.      for (i=0; i<this.height*2+1+(this.height*(thickness-1)); i++){
  297.       resultado[i] = new Array();
  298.       resultado[i].length = this.width*2+1+(this.width*(thickness-1));
  299.      }
  300.  
  301.      var rlength = this.width*2+1+(this.width*(thickness-1));
  302.  
  303.      for (i=0; i<resultado.length; i++){
  304.          for (n=0; n<resultado[i].length; n++){
  305.              resultado[i][n]=" ";
  306.          }
  307.      }
  308.          
  309.      var offy = 0;
  310.      var offx = 0;
  311.      
  312.      for (y=0; y<resultado.length; y++){
  313.          resultado[y][0]="#";
  314.      }
  315.      
  316.      for (x=0; x<rlength; x++){
  317.          resultado[0][x]="#";
  318.      }
  319.      
  320.      for (y=0; y<resultado.length; y+=(thickness+1)){
  321.      context.print("Y: " + y);
  322.          for (x=0; x<resultado[y].length; x+=(thickness+1)){
  323.              resultado[y][x]="#";
  324.          }
  325.      }
  326.    
  327.      
  328.      for (y=0; y<this.height; y++){
  329.        
  330.         for (x=0; x<this.width; x++){
  331.              
  332.              if (this.casillas[y][x].east==1){
  333.                  for (i = 0; i<thickness; i++) {
  334.                resultado[y + 1 + offy + i][x + 1 + thickness + offx] ="#";
  335.              }
  336.              }
  337.              if (this.casillas[y][x].south==1){
  338.                  for (i = 0; i <thickness; i++) {
  339.                resultado[y + 1 + thickness + offy][x + 1 + offx + i] ="#";
  340.          }
  341.              }
  342.              offx+=thickness;
  343.         }
  344.         offx=0;
  345.         offy+= thickness;
  346.      }
  347.  
  348.      //this.setDoors(resultado);
  349.      if (mode == 0){
  350.          for (i=0; i<resultado.length; i++){
  351.              dibujo += resultado[i].join("")+"\n";
  352.          }
  353.          return dibujo;
  354.      } else {
  355.          return resultado;
  356.      }
  357. };
  358.  
  359.  
  360. Maze.prototype.getNeightors = function(x,y){
  361.       var vecinos = [];    
  362.      
  363.       if (x-1 >= 0){
  364.           if (!this.casillas[y][x-1].isVisited()){  
  365.                    vecinos.push(this.casillas[y][x-1]);
  366.           }
  367.       }
  368.  
  369.       if (x+1 < this.width){
  370.           if (!this.casillas[y][x+1].isVisited()){  
  371.                    vecinos.push(this.casillas[y][x+1]);
  372.           }
  373.       }
  374.  
  375.       if (y-1 >= 0){
  376.           if (!this.casillas[y-1][x].isVisited()){  
  377.                    vecinos.push(this.casillas[y-1][x]);
  378.           }
  379.       }
  380.  
  381.       if (y+1 < this.height){
  382.           if (!this.casillas[y+1][x].isVisited()){  
  383.                    vecinos.push(this.casillas[y+1][x]);
  384.           }
  385.       }
  386.  
  387.       return vecinos;
  388. };
  389.  
  390.  
  391.  
  392.  
  393.  
  394.        
  395. //Clase para las celdas
  396.  
  397. function Celda(){
  398.          this.north = 1;
  399.          this.south = 1;
  400.          this.west = 1;
  401.          this.east = 1;
  402.          this.visited = 0;
  403.          this.x = 0;
  404.          this.y = 0;
  405.          this.weight = -1;
  406. }
  407.  
  408. Celda.prototype.isVisited = function(){
  409.       return (this.visited == 1);
  410. };
  411.  
  412.  
  413. Celda.prototype.visit = function(dir){
  414.       this.visited = 1;
  415.       switch (dir){
  416.               case paredes.north:
  417.                    this.north = 0;
  418.                    break;
  419.               case paredes.south:  
  420.                    this.south = 0;
  421.                    break;
  422.               case paredes.west:
  423.                    this.west = 0;
  424.                    break;
  425.               case paredes.east:
  426.                    this.east = 0;
  427.                    break;      
  428.       }
  429. };
  430.  
  431. Celda.prototype.toString = function(){
  432.      var n,s,e,w;
  433.          n = (this.north==1);
  434.          s = (this.south==1);
  435.          e = (this.east==1);
  436.          w = (this.west==1);
  437.              
  438.          if (n&&w&&s&&!e) {
  439.              return "###\n#  \n###";
  440.          }
  441.          
  442.          if (n&&w&&e&&!s) {
  443.              return "###\n#  #\n# #";
  444.          }
  445.              
  446.          if (n&&e&&s&&!w) {
  447.              return "###\n  #\n###";
  448.          }
  449.              
  450.          if (s&&e&&w&&!n) {
  451.              return "# #\n# #\n###";
  452.          }
  453.              
  454.          if (n&&s&&!e&&!w) {
  455.              return "###\n  \n###";
  456.          }
  457.              
  458.          if (e&&w&&!n&&!s) {
  459.              return "# #\n# #\n# #";
  460.          }
  461.              
  462.          if (w&&s&&!e&&!n) {
  463.              return "# #\n#  \n###";
  464.          }
  465.              
  466.          if (w&&n&&!s&&!e) {
  467.              return "###\n#  \n# #";
  468.          }
  469.              
  470.          if (n&&e&&!s&&!w) {
  471.              return "###\n  #\n# #";
  472.          }
  473.              
  474.          if (e&&s&&!n&&!w) {
  475.              return "# #\n  #\n###";
  476.          }
  477.              
  478.          if (s&&!n&&!e&&!w) {
  479.              return "# #\n   \n###";
  480.          }
  481.              
  482.          if (w&&!n&&!s&&!e) {
  483.              return "# #\n#  \n# #";
  484.          }
  485.              
  486.          if (n&&!e&&!s&&!w) {
  487.              return "###\n   \n# #";
  488.          }
  489.              
  490.          if (e&&!n&&!s&&!w) {
  491.              return "# #\n  #\n# #";
  492.          }
  493.              
  494.          return "###\n# #\n###";   
  495. };
  496.  
  497.  
  498.  
  499. var session = context.remember();
  500.  
  501. var initBlock = player.getBlockOn();
  502.  
  503. var initDirection = player.getCardinalDirection();
  504.  
  505. context.checkArgs(3, 6, "<block> [length] [width] [thickness] [height] [floors]");
  506.  
  507. var bloque = context.getBlock(argv[1]);
  508. var techo = context.getBlock(89);
  509.  
  510. var length = argv[2];
  511. var width = argv[3];
  512. var height = argv[5];
  513. height = (height===undefined) ? 1 : height;
  514. var initPos = initBlock;
  515.  
  516. switch (initDirection) {
  517.     //context.print("initDirection:" + initDirection);
  518.     case "SOUTHWEST":
  519.          
  520.          initPos = initPos.add(-2*width,0,0);
  521.          break;
  522.    
  523.     case "NORTHEAST":
  524.          initPos = initPos.add(0,0,-2*length);
  525.          break;
  526.  
  527.     case "NORTHWEST":
  528.          initPos = initPos.add(-2*width,0,-2*length);
  529.          break;
  530.  
  531. }
  532.  
  533. var aire = context.getBlock(0);
  534. var maze = new Maze();
  535. var pisos = 1;
  536. var thickness = 1;
  537. var blocks;
  538. var backupPos = initPos;
  539. maze.setHeight(length);
  540. maze.setWidth(width);
  541.  
  542. pisos = (argv[6]!== undefined) ? argv[6] : 1;
  543. thickness = argv[4];
  544. maze.setThickness(thickness);
  545.  
  546. context.print("Argumentos: " + argv.length);
  547. context.print("Pisos: " + pisos);
  548.  
  549. var step = height + 1;
  550.  
  551.  
  552. for (p=0; p<pisos; p++){
  553.     maze.generate();
  554.     blocks = [];
  555.     blocks = maze.draw(1);
  556.     //for (y=0; y<height; y++){
  557.         for (x=0;x<blocks.length; x++){
  558.             for (z=0; z<blocks[x].length; z++){
  559.                
  560.                 newpos = Array();
  561.                 for (i=0; i<=height; i++){
  562.                     try {
  563.                     newpos.push(initPos.toVector().add(x,i+1,z));
  564.                     } catch (error) {
  565.                     newpos.push(initPos.add(x,i+1,z));
  566.                     }
  567.                 }
  568.                
  569.                                
  570.                 // newpos = initPos.add(x,1,z);
  571.                 // newpos2 = initPos.add(x,2,z);
  572.                 if (blocks[x][z]=="#"){
  573.                     // session.setBlock(newpos,bloque);
  574.                     // session.setBlock(newpos2,bloque);
  575.                     for (i=0; i<newpos.length; i++){
  576.                         try {
  577.                         session.rawSetBlock(newpos[i].toBlockPoint(),bloque);
  578.                         } catch (error) {
  579.                             context.print("newpos["+i+"]: " + newpos[i]);
  580.                             vector = new Vector3(parseInt(newpos[i].x),parseInt(newpos[i].y),parseInt(newpos[i].z));
  581.                             session.rawSetBlock(vector.toBlockPoint(),bloque);
  582.                         }
  583.                     }
  584.                 } else {
  585.                     // session.setBlock(newpos,aire);
  586.                     // session.setBlock(newpos2,aire);
  587.                     for (i=0; i<newpos.length; i++){
  588.                         session.rawSetBlock(newpos[i].toBlockPoint(),aire);
  589.                     }
  590.                 }
  591.                
  592.                 posaux = initPos.toVector().add(x,height,z);
  593.                 //session.rawSetBlock(posaux.toBlockPoint(), techo);
  594.                
  595.             }
  596.            
  597.         }
  598.     //}
  599.  
  600.     initPos = initPos.toVector().add(0,height,0);
  601.     context.print(initPos);
  602. }
Advertisement
Add Comment
Please, Sign In to add comment