|
1 | 1 | package com.nmerrill.kothcomm.game.maps.generators.mazes;
|
2 | 2 |
|
3 |
| -import com.nmerrill.kothcomm.game.maps.generators.Generator; |
4 |
| -import com.nmerrill.kothcomm.game.maps.graphmaps.GraphMapImpl; |
5 | 3 | import com.nmerrill.kothcomm.game.maps.Point2D;
|
| 4 | +import com.nmerrill.kothcomm.game.maps.generators.Generator; |
| 5 | +import com.nmerrill.kothcomm.game.maps.graphmaps.GraphGraphMap; |
| 6 | +import com.nmerrill.kothcomm.game.maps.graphmaps.bounds.point2D.SquareRegion; |
6 | 7 | import com.nmerrill.kothcomm.utils.iterables.Itertools;
|
| 8 | +import org.eclipse.collections.api.list.MutableList; |
| 9 | +import org.eclipse.collections.api.list.primitive.MutableBooleanList; |
7 | 10 | import org.eclipse.collections.api.list.primitive.MutableIntList;
|
| 11 | +import org.eclipse.collections.api.set.primitive.MutableIntSet; |
| 12 | +import org.eclipse.collections.impl.factory.primitive.BooleanLists; |
| 13 | +import org.eclipse.collections.impl.factory.primitive.IntSets; |
8 | 14 |
|
9 |
| -import java.util.List; |
10 | 15 | import java.util.Random;
|
11 |
| -import java.util.stream.IntStream; |
12 | 16 |
|
13 |
| -public class Ellers implements Generator<GraphMapImpl<Point2D, ?>> { |
| 17 | +public class Ellers implements Generator<GraphGraphMap<Point2D, ?>> { |
14 | 18 | private final Random random;
|
15 |
| - private final int minX, maxX, minY, maxY; |
16 |
| - public Ellers(Point2D p1, Point2D p2, Random random){ |
| 19 | + private final SquareRegion region; |
| 20 | + private MutableList<MutableIntSet> currentLine; |
| 21 | + private Point2D currentOffset; |
| 22 | + private GraphGraphMap<Point2D, ?> currentMap; |
| 23 | + |
| 24 | + public Ellers(SquareRegion region, Random random) { |
17 | 25 | this.random = random;
|
18 |
| - if (p1.getX() < p2.getX()){ |
19 |
| - minX = p1.getX(); |
20 |
| - maxX = p2.getX()+1; |
21 |
| - } else { |
22 |
| - minX = p2.getX(); |
23 |
| - maxX = p1.getX()+1; |
24 |
| - } |
25 |
| - if (p1.getY() < p2.getY()){ |
26 |
| - minY = p1.getY(); |
27 |
| - maxY = p2.getY()+1; |
28 |
| - } else { |
29 |
| - minY = p2.getY(); |
30 |
| - maxY = p1.getY()+1; |
31 |
| - } |
| 26 | + this.region = region; |
32 | 27 | }
|
33 |
| - public Ellers(Point2D p1, Point2D p2){ |
34 |
| - this(p1, p2, new Random()); |
| 28 | + |
| 29 | + public Ellers(SquareRegion region) { |
| 30 | + this(region, new Random()); |
35 | 31 | }
|
36 | 32 |
|
37 | 33 | @Override
|
38 |
| - public void generate(GraphMapImpl<Point2D, ?> map) { |
39 |
| - MutableIntList states = Itertools.range(maxX-minX); |
40 |
| - IntStream.range(minY, maxY).forEach(y -> { |
41 |
| - IntStream.range(minX, maxX).forEach(x->map.put(new Point2D(x, y), null)); |
42 |
| - connectNeighbors(states).forEach(x -> |
43 |
| - map.connect(new Point2D(minX+x, y), new Point2D(minX+x+1, y)) |
44 |
| - ); |
45 |
| - }); |
46 |
| - |
| 34 | + public void generate(GraphGraphMap<Point2D, ?> map) { |
| 35 | + int width = region.getWidth(); |
| 36 | + int height = region.getHeight(); |
| 37 | + currentLine = Itertools.range(0, width).collect(IntSets.mutable::of).toList(); |
| 38 | + currentMap = map; |
| 39 | + for (int y = 0; y < height; y++) { |
| 40 | + currentOffset = new Point2D(region.getLeft(), region.getBottom()+y); |
| 41 | + connectSets(); |
| 42 | + if (y == height -1){ |
| 43 | + break; |
| 44 | + } |
| 45 | + newLine(); |
| 46 | + } |
| 47 | + while (currentLine.toSet().size() > 1){ |
| 48 | + connectSets(); |
| 49 | + } |
47 | 50 | }
|
48 | 51 |
|
49 |
| - private List<Integer> nextLine(List<Integer> states){ |
50 |
| - return null; |
| 52 | + private void connectSets(){ |
| 53 | + for (int x = 0; x < region.getWidth() - 1; x++) { |
| 54 | + MutableIntSet currentSet = currentLine.get(x); |
| 55 | + MutableIntSet adjacentSet = currentLine.get(x + 1); |
| 56 | + if (currentSet.equals(adjacentSet)) { |
| 57 | + break; |
| 58 | + } |
| 59 | + if (random.nextBoolean()) { |
| 60 | + break; |
| 61 | + } |
| 62 | + Point2D currentPoint = currentOffset.moveX(x); |
| 63 | + currentMap.connect(currentPoint, currentPoint.moveX(1)); |
| 64 | + MutableIntSet smaller, larger; |
| 65 | + if (currentSet.size() < adjacentSet.size()) { |
| 66 | + smaller = currentSet; |
| 67 | + larger = adjacentSet; |
| 68 | + } else { |
| 69 | + smaller = adjacentSet; |
| 70 | + larger = currentSet; |
| 71 | + } |
| 72 | + larger.addAll(smaller); |
| 73 | + smaller.forEach(i -> currentLine.set(i, adjacentSet)); |
| 74 | + } |
51 | 75 | }
|
52 | 76 |
|
53 |
| - |
54 |
| - private MutableIntList connectNeighbors(MutableIntList states){ |
55 |
| - return Itertools.range(0, states.size()-1) |
56 |
| - .select(x -> states.get(x+1) != states.get(x)) |
57 |
| - .select(i -> random.nextBoolean()); |
| 77 | + private void newLine(){ |
| 78 | + currentLine.toSet().forEach(set -> { |
| 79 | + MutableIntList list = set.toList(); |
| 80 | + MutableBooleanList bits = randomBits(set.size()); |
| 81 | + for (int i = 0; i < set.size(); i++) { |
| 82 | + int x = list.get(i); |
| 83 | + if (bits.get(i)) { |
| 84 | + Point2D currentPoint = currentOffset.move(x, 0); |
| 85 | + currentMap.connect(currentPoint, currentPoint.moveY(1)); |
| 86 | + } else { |
| 87 | + set.remove(list.get(i)); |
| 88 | + currentLine.set(x, IntSets.mutable.of(x)); |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
58 | 92 | }
|
59 |
| - /* |
60 |
| - states = range(self.maze.width) |
61 |
| - for y in xrange(self.maze.height): |
62 |
| - states = self.generate_line(y, states) |
63 | 93 |
|
64 |
| - def generate_line(self, y, states): |
65 |
| - self.connect_neighbors(y, states) |
66 |
| - if y == self.maze.height-1: |
67 |
| - while not all(x == states[0] for x in states):x |
68 |
| - self.connect_neighbors(y, states) |
69 |
| - else: |
70 |
| - states = self.connect_lines(y, states) |
71 |
| - return states |
72 |
| -
|
73 |
| - def connect_neighbors(self, y, states): |
74 |
| - for x in xrange(self.maze.width-1): |
75 |
| - if states[x] != states[x+1]: |
76 |
| - if random.random() < self.weight: |
77 |
| - self.maze.get((x, y)).connect_to_cell(Directions(Directions.RIGHT)) |
78 |
| - to_replace = states[x+1] |
79 |
| - for index, value in enumerate(states): |
80 |
| - if value == to_replace: |
81 |
| - states[index] = states[x] |
| 94 | + private MutableBooleanList randomBits(int count) { |
| 95 | + MutableBooleanList bits = BooleanLists.mutable.empty(); |
| 96 | + if (count < 32) { |
| 97 | + int intBits = random.nextInt((1 << count) - 1); |
| 98 | + for (int i = 0; i < count; i++) { |
| 99 | + bits.add(((intBits >> i) & 1) == 0); |
| 100 | + } |
| 101 | + return bits; |
| 102 | + } |
| 103 | + while (true) { |
| 104 | + bits.clear(); |
| 105 | + for (int i = 0; i < count; i++) { |
| 106 | + bits.add(random.nextBoolean()); |
| 107 | + } |
| 108 | + if (bits.anySatisfy(i -> i)) { |
| 109 | + return bits; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
82 | 113 |
|
83 |
| - def connect_lines(self, y, states): |
84 |
| - new_states = range(max(states)+1, max(states)+1+len(states)) |
85 |
| - states_left = {state for state in states} |
86 |
| - states_to_pick = range(len(states)) |
87 |
| - random.shuffle(states_to_pick) |
88 |
| - for index in states_to_pick: |
89 |
| - if states[index] in states_left or random.random() >= self.weight: |
90 |
| - self.maze.get((index, y)).connect_to_cell(Directions(Directions.BOTTOM)) |
91 |
| - new_states[index] = states[index] |
92 |
| - if states[index] in states_left: |
93 |
| - states_left.remove(states[index]) |
94 |
| - return new_states |
95 |
| - */ |
96 | 114 | }
|
0 commit comments