Its_Patchi

Need to convert this to lua.

Aug 9th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // $Id$
  2. /*
  3.  * Maze generator CraftScript for WorldEdit
  4.  * Copyright (C) 2010, 2011 sk89q <http://www.sk89q.com>
  5.  *
  6.  * This program is free software: you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation, either version 3 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19.  
  20. importPackage(Packages.com.sk89q.worldedit);
  21. importPackage(Packages.com.sk89q.worldedit.blocks);
  22.  
  23. usage = "<block> [width] [length] [height] [size] [thickness] flags\n";
  24. usage += "\n";
  25. usage += "Maze Flags:\n";
  26. usage += "• i adds an entry and an exit\n";
  27. usage += "• y places the entry and the exit randomly\n";
  28. usage += "• f adds a floor\n";
  29. usage += "• c adds a ceiling\n";
  30. usage += "• e places air blocks\n";
  31. usage += "• a places air blocks only\n";
  32. usage += "• v creates a vertical maze\n";
  33. usage += "\n";
  34. usage += "Solver Flags:\n";
  35. usage += "• s enables the maze solver\n";
  36. usage += "• g places glass if wrong or unvisited\n";
  37. usage += "• r places red wool if wrong\n";
  38. usage += "• b places blue wool if unvisited";
  39.  
  40. context.checkArgs(1, -1, usage);
  41.  
  42. sess = context.remember();
  43. origin = player.getBlockIn();
  44.  
  45. // This may throw an exception that is caught by the script processor
  46. block = context.getBlock(argv[1]);
  47.  
  48. if (argv.length > 7) flags = String(argv[7]);
  49. else flags = false;
  50.  
  51. if (argv.length > 6) {
  52.     if (parseInt(argv[6], 10)) wa = parseInt(argv[6], 10);
  53.     else flags = String(argv[6]), wa = 1;
  54. } else wa = 1;
  55.  
  56. if (argv.length > 5) {
  57.     if (parseInt(argv[5], 10)) s = parseInt(argv[5], 10);
  58.     else flags = String(argv[5]), s = 1, wa = 1;
  59. } else s = 1;
  60.  
  61. if (argv.length > 4) {
  62.     if (parseInt(argv[4], 10)) h = parseInt(argv[4], 10);
  63.     else flags = String(argv[4]), h = 2, s = 1, wa = 1;
  64. } else h = 2;
  65.  
  66. if (argv.length > 3) {
  67.     if (parseInt(argv[3], 10)) l = parseInt(argv[3], 10);
  68.     else flags = String(argv[3]), l = 5, h = 2, s = 1, wa = 1;
  69. } else l = 5;
  70.  
  71. if (argv.length > 2) {
  72.     if (parseInt(argv[2], 10)) w = parseInt(argv[2], 10);
  73.     else flags = String(argv[2]), w = 5, l = 5, h = 2, s = 1, wa = 1;
  74. } else w = 5;
  75.  
  76. if (flags) {
  77.     ee = flags.search("i") != -1 ? true : false;
  78.     r = flags.search("y") != -1 ? true : false;
  79.     if (r) ee = true;
  80.     f = flags.search("f") != -1 ? true : false;
  81.     c = flags.search("c") != -1 ? true : false;
  82.     e = flags.search("e") != -1 ? true : false;
  83.     ao = flags.search("a") != -1 ? true : false;
  84.     if (ao) f = c = false, e = true;
  85.     v = flags.search("v") != -1 ? true : false;
  86.     so = flags.search("s") != -1 ? true : false;
  87.     if (so) ee = true;
  88.     g = flags.search("g") != -1 ? true : false;
  89.     re = flags.search("r") != -1 ? true : false;
  90.     bl = flags.search("b") != -1 ? true : false;
  91.     if (g || re || bl) so = ee = true;
  92. } else ee = r = f = c = e = ao = v = so = g = re = bl = false;
  93.  
  94. function id(x, y) {
  95.     return y * (w + 1) + x;
  96. }
  97.  
  98. function $x(i) {
  99.     return i % (w + 1);
  100. }
  101.  
  102. function $y(i) {
  103.     return Math.floor(i / (w + 1));
  104. }
  105.  
  106. function shuffle(arr) {
  107.     i = arr.length;
  108.     if (i === 0) return false;
  109.     while (--i) {
  110.         j = Math.floor(Math.random() * (i + 1));
  111.         tempi = arr[i];
  112.         tempj = arr[j];
  113.         arr[i] = tempj;
  114.         arr[j] = tempi;
  115.     }
  116. }
  117.  
  118. if (f || c) {
  119.     for (z = 1; z <= wa; z++) for (y = -wa; y <= l * (s + wa) - 1; y++) for (x = -wa; x <= w * (s + wa) - 1; x++) {
  120.         if (f) {
  121.             if (!v) sess.setBlock(origin.add(x, -z, y), block);
  122.             else sess.setBlock(origin.add(x, y, +z), block);
  123.         }
  124.  
  125.         if (c) {
  126.             if (!v) sess.setBlock(origin.add(x, z - 1 + h, y), block);
  127.             else sess.setBlock(origin.add(x, y, -z + 1 - h), block);
  128.         }
  129.     }
  130. }
  131.  
  132. stack = [];
  133. visited = [];
  134. noWallLeft = [];
  135. noWallAbove = [];
  136.  
  137. stack.push(id(Math.floor(Math.random() * w), Math.floor(Math.random() * l)));
  138.  
  139. while (stack.length > 0) {
  140.     cell = stack.pop();
  141.     x = $x(cell);
  142.     y = $y(cell);
  143.     visited[cell] = true;
  144.     neighbors = [];
  145.  
  146.     if (x > 0) neighbors.push(id(x - 1, y));
  147.     if (x < w - 1) neighbors.push(id(x + 1, y));
  148.     if (y > 0) neighbors.push(id(x, y - 1));
  149.     if (y < l - 1) neighbors.push(id(x, y + 1));
  150.  
  151.     shuffle(neighbors);
  152.  
  153.     while (neighbors.length > 0) {
  154.         neighbor = neighbors.pop();
  155.         nx = $x(neighbor);
  156.         ny = $y(neighbor);
  157.  
  158.         if (!visited[neighbor]) {
  159.             stack.push(cell);
  160.  
  161.             if (y == ny) {
  162.                 if (nx < x) noWallLeft[cell] = true;
  163.                 else noWallLeft[neighbor] = true;
  164.             } else {
  165.                 if (ny < y) noWallAbove[cell] = true;
  166.                 else noWallAbove[neighbor] = true;
  167.             }
  168.  
  169.             stack.push(neighbor);
  170.             break;
  171.         }
  172.     }
  173. }
  174.  
  175. if (!r) {
  176.     start = id(0, 0);
  177.     end = id(w - 1, l - 1);
  178. } else {
  179.     start = id(0, Math.floor(Math.random() * l));
  180.     end = id(w - 1, Math.floor(Math.random() * l));
  181. }
  182.  
  183. if (ee) {
  184.     noWallLeft[start] = true;
  185.     noWallLeft[end + 1] = true;
  186. }
  187.  
  188. /*for (y = -1; y < l; y++) {
  189.     line = "";
  190.     for (x = 0; x <= w; x++) {
  191.         cell = id(x, y);
  192.         a = y >= 0 ? (noWallLeft[cell] ? "_" : "|") : "_";
  193.         b = x < w ? (noWallAbove[id(x, y + 1)] ? "  " : "_") : "";
  194.         line += a + b;
  195.     }
  196.     context.print(line);
  197. }*/
  198.  
  199. for (y = 0; y <= l; y++) for (x = 0; x <= w; x++) {
  200.     cell = id(x, y);
  201.  
  202.     if (!noWallLeft[cell] && cell != id(x, l)) {
  203.         if (!ao) {
  204.             for (z = 0; z < h; z++) for (yi = 0; yi < s; yi++) for (xi = 1; xi <= wa; xi++) {
  205.                 if (!v) sess.setBlock(origin.add(x * (s + wa) - xi, z, y * (s + wa) + yi), block);
  206.                 else sess.setBlock(origin.add(x * (s + wa) - xi, y * (s + wa) + yi, -z), block);
  207.             }
  208.         }
  209.     } else if (e && cell != id(x, l)) {
  210.         for (z = 0; z < h; z++) for (yi = 0; yi < s; yi++) for (xi = 1; xi <= wa; xi++) {
  211.             if (!v) sess.setBlock(origin.add(x * (s + wa) - xi, z, y * (s + wa) + yi), BaseBlock(0));
  212.             else sess.setBlock(origin.add(x * (s + wa) - xi, y * (s + wa) + yi, -z), BaseBlock(0));
  213.         }
  214.     }
  215.  
  216.     if (!noWallAbove[cell] && cell != id(w, y)) {
  217.         if (!ao) {
  218.             for (z = 0; z < h; z++) for (yi = 1; yi <= wa; yi++) for (xi = 0; xi < s; xi++) {
  219.                 if (!v) sess.setBlock(origin.add(x * (s + wa) + xi, z, y * (s + wa) - yi), block);
  220.                 else sess.setBlock(origin.add(x * (s + wa) + xi, y * (s + wa) - yi, -z), block);
  221.             }
  222.         }
  223.     } else if (e && cell != id(w, y)) {
  224.         for (z = 0; z < h; z++) for (yi = 1; yi <= wa; yi++) for (xi = 0; xi < s; xi++) {
  225.             if (!v) sess.setBlock(origin.add(x * (s + wa) + xi, z, y * (s + wa) - yi), BaseBlock(0));
  226.             else sess.setBlock(origin.add(x * (s + wa) + xi, y * (s + wa) - yi, -z), BaseBlock(0));
  227.         }
  228.     }
  229.  
  230.     if (!ao) {
  231.         for (z = 0; z < h; z++) for (yi = 1; yi <= wa; yi++) for (xi = 1; xi <= wa; xi++) {
  232.             if (!v) sess.setBlock(origin.add(x * (s + wa) - xi, z, y * (s + wa) - yi), block);
  233.             else sess.setBlock(origin.add(x * (s + wa) - xi, y * (s + wa) - yi, -z), block);
  234.         }
  235.     }
  236.  
  237.     if (e && cell != id(x, l) && cell != id(w, y)) {
  238.         for (z = 0; z < h; z++) for (yi = 0; yi < s; yi++) for (xi = 0; xi < s; xi++) {
  239.             if (!v) sess.setBlock(origin.add(x * (s + wa) + xi, z, y * (s + wa) + yi), BaseBlock(0));
  240.             else sess.setBlock(origin.add(x * (s + wa) + xi, y * (s + wa) + yi, -z), BaseBlock(0));
  241.         }
  242.     }
  243. }
  244.  
  245. if (so) {
  246.     stack = [];
  247.     visited = [];
  248.     wrong = [];
  249.  
  250.     stack.push(start);
  251.  
  252.     while (cell != end) {
  253.         if (visited[stack[stack.length - 1]]) wrong[cell] = true;
  254.  
  255.         cell = stack.pop();
  256.         x = $x(cell);
  257.         y = $y(cell);
  258.         visited[cell] = true;
  259.         neighbors = [];
  260.  
  261.         if (noWallLeft[cell] && cell != start) neighbors.push(id(x - 1, y));
  262.         if (noWallLeft[id(x + 1, y)]) neighbors.push(id(x + 1, y));
  263.         if (noWallAbove[cell]) neighbors.push(id(x, y - 1));
  264.         if (noWallAbove[id(x, y + 1)]) neighbors.push(id(x, y + 1));
  265.  
  266.         shuffle(neighbors);
  267.  
  268.         while (neighbors.length > 0) {
  269.             neighbor = neighbors.pop();
  270.  
  271.             if (!visited[neighbor]) {
  272.                 stack.push(cell);
  273.                 stack.push(neighbor);
  274.                 break;
  275.             }
  276.         }
  277.     }
  278.  
  279.     for (y = 0; y <= l; y++) for (x = 0; x <= w; x++) {
  280.         cell = id(x, y);
  281.  
  282.         if (visited[cell] && !wrong[cell]) {
  283.             for (yi = 0; yi < s; yi++) for (xi = 0; xi < s; xi++) {
  284.                 if (!v) sess.setBlock(origin.add(x * (s + wa) + xi, -1, y * (s + wa) + yi), BaseBlock(35, 5));
  285.                 else sess.setBlock(origin.add(x * (s + wa) + xi, y * (s + wa) + yi, +1), BaseBlock(35, 5));
  286.             }
  287.         }
  288.  
  289.         if ((visited[cell] && !wrong[cell] && visited[id(x - 1, y)] && !wrong[id(x - 1, y)] && noWallLeft[cell]) || cell == start || id(x - 1, y) == end) {
  290.             for (xi = 1; xi <= wa; xi++) for (yi = 0; yi < s; yi++) {
  291.                 if (!v) sess.setBlock(origin.add(x * (s + wa) - xi, -1, y * (s + wa) + yi), BaseBlock(35, 5));
  292.                 else sess.setBlock(origin.add(x * (s + wa) - xi, y * (s + wa) + yi, +1), BaseBlock(35, 5));
  293.             }
  294.         }
  295.  
  296.         if (visited[cell] && !wrong[cell] && visited[id(x, y - 1)] && !wrong[id(x, y - 1)] && noWallAbove[cell]) {
  297.             for (xi = 0; xi < s; xi++) for (yi = 1; yi <= wa; yi++) {
  298.                 if (!v) sess.setBlock(origin.add(x * (s + wa) + xi, -1, y * (s + wa) - yi), BaseBlock(35, 5));
  299.                 else sess.setBlock(origin.add(x * (s + wa) + xi, y * (s + wa) - yi, +1), BaseBlock(35, 5));
  300.             }
  301.         }
  302.  
  303.         if (g) {
  304.             if (visited[cell] && !wrong[cell] && (!visited[id(x - 1, y)] || wrong[id(x - 1, y)]) && noWallLeft[cell] && cell != start) {
  305.                 for (z = 0; z < h; z++) for (xi = 1; xi <= wa; xi++) for (yi = 0; yi < s; yi++) {
  306.                     if (!v) sess.setBlock(origin.add(x * (s + wa) - xi, z, y * (s + wa) + yi), BaseBlock(20));
  307.                     else sess.setBlock(origin.add(x * (s + wa) - xi, y * (s + wa) + yi, -z), BaseBlock(20));
  308.                 }
  309.             }
  310.  
  311.             if ((!visited[cell] || wrong[cell]) && visited[id(x - 1, y)] && !wrong[id(x - 1, y)] && noWallLeft[cell] && id(x - 1, y) != end) {
  312.                 for (z = 0; z < h; z++) for (xi = 1; xi <= wa; xi++) for (yi = 0; yi < s; yi++) {
  313.                     if (!v) sess.setBlock(origin.add(x * (s + wa) - xi, z, y * (s + wa) + yi), BaseBlock(20));
  314.                     else sess.setBlock(origin.add(x * (s + wa) - xi, y * (s + wa) + yi, -z), BaseBlock(20));
  315.                 }
  316.             }
  317.  
  318.             if (visited[cell] && !wrong[cell] && (!visited[id(x, y - 1)] || wrong[id(x, y - 1)]) && noWallAbove[cell]) {
  319.                 for (z = 0; z < h; z++) for (xi = 0; xi < s; xi++) for (yi = 1; yi <= wa; yi++) {
  320.                     if (!v) sess.setBlock(origin.add(x * (s + wa) + xi, z, y * (s + wa) - yi), BaseBlock(20));
  321.                     else sess.setBlock(origin.add(x * (s + wa) + xi, y * (s + wa) - yi, -z), BaseBlock(20));
  322.                 }
  323.             }
  324.  
  325.             if ((!visited[cell] || wrong[cell]) && visited[id(x, y - 1)] && !wrong[id(x, y - 1)] && noWallAbove[cell]) {
  326.                 for (z = 0; z < h; z++) for (xi = 0; xi < s; xi++) for (yi = 1; yi <= wa; yi++) {
  327.                     if (!v) sess.setBlock(origin.add(x * (s + wa) + xi, z, y * (s + wa) - yi), BaseBlock(20));
  328.                     else sess.setBlock(origin.add(x * (s + wa) + xi, y * (s + wa) - yi, -z), BaseBlock(20));
  329.                 }
  330.             }
  331.         }
  332.  
  333.         if (re) {
  334.             if (wrong[cell]) {
  335.                 for (yi = 0; yi < s; yi++) for (xi = 0; xi < s; xi++) {
  336.                     if (!v) sess.setBlock(origin.add(x * (s + wa) + xi, -1, y * (s + wa) + yi), BaseBlock(35, 14));
  337.                     else sess.setBlock(origin.add(x * (s + wa) + xi, y * (s + wa) + yi, +1), BaseBlock(35, 14));
  338.                 }
  339.             }
  340.  
  341.             if ((wrong[cell] || wrong[id(x - 1, y)]) && noWallLeft[cell]) {
  342.                 for (xi = 1; xi <= wa; xi++) for (yi = 0; yi < s; yi++) {
  343.                     if (!v) sess.setBlock(origin.add(x * (s + wa) - xi, -1, y * (s + wa) + yi), BaseBlock(35, 14));
  344.                     else sess.setBlock(origin.add(x * (s + wa) - xi, y * (s + wa) + yi, +1), BaseBlock(35, 14));
  345.                 }
  346.             }
  347.  
  348.             if ((wrong[cell] || wrong[id(x, y - 1)]) && noWallAbove[cell]) {
  349.                 for (xi = 0; xi < s; xi++) for (yi = 1; yi <= wa; yi++) {
  350.                     if (!v) sess.setBlock(origin.add(x * (s + wa) + xi, -1, y * (s + wa) - yi), BaseBlock(35, 14));
  351.                     else sess.setBlock(origin.add(x * (s + wa) + xi, y * (s + wa) - yi, +1), BaseBlock(35, 14));
  352.                 }
  353.             }
  354.         }
  355.  
  356.         if (bl) {
  357.             if (!visited[cell] && y < l && x < w) {
  358.                 for (yi = 0; yi < s; yi++) for (xi = 0; xi < s; xi++) {
  359.                     if (!v) sess.setBlock(origin.add(x * (s + wa) + xi, -1, y * (s + wa) + yi), BaseBlock(35, 11));
  360.                     else sess.setBlock(origin.add(x * (s + wa) + xi, y * (s + wa) + yi, +1), BaseBlock(35, 11));
  361.                 }
  362.             }
  363.  
  364.             if ((!visited[cell] || !visited[id(x - 1, y)]) && noWallLeft[cell] && x > 0 && x < w) {
  365.                 for (xi = 1; xi <= wa; xi++) for (yi = 0; yi < s; yi++) {
  366.                     if (!v) sess.setBlock(origin.add(x * (s + wa) - xi, -1, y * (s + wa) + yi), BaseBlock(35, 11));
  367.                     else sess.setBlock(origin.add(x * (s + wa) - xi, y * (s + wa) + yi, +1), BaseBlock(35, 11));
  368.                 }
  369.             }
  370.  
  371.             if ((!visited[cell] || !visited[id(x, y - 1)]) && noWallAbove[cell]) {
  372.                 for (xi = 0; xi < s; xi++) for (yi = 1; yi <= wa; yi++) {
  373.                     if (!v) sess.setBlock(origin.add(x * (s + wa) + xi, -1, y * (s + wa) - yi), BaseBlock(35, 11));
  374.                     else sess.setBlock(origin.add(x * (s + wa) + xi, y * (s + wa) - yi, +1), BaseBlock(35, 11));
  375.                 }
  376.             }
  377.         }
  378.     }
  379. }
Advertisement
Add Comment
Please, Sign In to add comment