mazegen.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. export const mazegen = (function() {
  2. function RouletteSelect(src) {
  3. const roll = Math.random() * src.length;
  4. let sum = 0;
  5. for (let i = 0; i < src.length; i++) {
  6. sum += 1.0;
  7. if (roll < sum) {
  8. const res = src[i];
  9. src = src.splice(i, 1);
  10. return res;
  11. }
  12. }
  13. }
  14. function _Key(x, y) {
  15. return x + '.' + y;
  16. }
  17. return {
  18. MazeGenerator: class {
  19. constructor(nodes) {
  20. this._nodes = nodes;
  21. this._visited = {};
  22. }
  23. *GenerateIteratively(nodeKey) {
  24. this._visited[nodeKey] = true;
  25. const node = this._nodes[nodeKey];
  26. const neighbours = [...node.potentialEdges];
  27. while (neighbours.length > 0) {
  28. const ki = RouletteSelect(neighbours);
  29. if (!(ki in this._visited)) {
  30. const adjNode = this._nodes[ki];
  31. node.edges.push(ki);
  32. adjNode.edges.push(nodeKey);
  33. yield* this.GenerateIteratively(ki);
  34. }
  35. }
  36. }
  37. Randomize() {
  38. for (let k in this._nodes) {
  39. const n = this._nodes[k];
  40. if (n.potentialEdges < 3) {
  41. continue;
  42. }
  43. const neighbours = [...n.potentialEdges];
  44. while (n.edges.length < 3) {
  45. const ki = RouletteSelect(neighbours);
  46. if (!(ki in n.edges)) {
  47. const adjNode = this._nodes[ki];
  48. n.edges.push(ki);
  49. adjNode.edges.push(k);
  50. }
  51. }
  52. }
  53. }
  54. GenerateMaze(start) {
  55. this._ProcessNode(start);
  56. }
  57. _ProcessNode(nodeKey) {
  58. this._visited[nodeKey] = true;
  59. const node = this._nodes[nodeKey];
  60. const neighbours = [...node.potentialEdges];
  61. while (neighbours.length > 0) {
  62. const ki = RouletteSelect(neighbours);
  63. if (!(ki in this._visited)) {
  64. const adjNode = this._nodes[ki];
  65. node.edges.push(ki);
  66. adjNode.edges.push(nodeKey);
  67. this._ProcessNode(ki);
  68. }
  69. }
  70. }
  71. }
  72. };
  73. })();