Skip to content

Commit 08929d4

Browse files
author
Nathan Merrill
committed
Fixed Ellers maze not generating correctly. Renamed Bounds to Regions, and GraphMap to GraphGraphMap
1 parent 8042580 commit 08929d4

File tree

15 files changed

+268
-191
lines changed

15 files changed

+268
-191
lines changed

src/main/java/com/nmerrill/kothcomm/communication/serialization/GridMapSerializer.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22

33

44
import com.nmerrill.kothcomm.game.maps.MapPoint;
5-
import com.nmerrill.kothcomm.game.maps.graphmaps.GraphMapImpl;
5+
import com.nmerrill.kothcomm.game.maps.graphmaps.GraphGraphMap;
66

77
import java.util.function.Function;
88
import java.util.function.Supplier;
99

10-
public final class GridMapSerializer<U extends MapPoint, T> implements Serializer<GraphMapImpl<U, T>> {
10+
public final class GridMapSerializer<U extends MapPoint, T> implements Serializer<GraphGraphMap<U, T>> {
1111

1212
private final Function<Character, T> reverse;
1313
private final Function<T, Character> mapping;
14-
private final Supplier<GraphMapImpl<U, T>> supplier;
15-
public GridMapSerializer(Function<T, Character> mapping, Function<Character, T> reverse, Supplier<GraphMapImpl<U,T>> supplier){
14+
private final Supplier<GraphGraphMap<U, T>> supplier;
15+
public GridMapSerializer(Function<T, Character> mapping, Function<Character, T> reverse, Supplier<GraphGraphMap<U,T>> supplier){
1616
this.mapping = mapping;
1717
this.reverse = reverse;
1818
this.supplier = supplier;
1919
}
2020

2121
@Override
22-
public GraphMapImpl<U, T> deserialize(String representation) {
23-
GraphMapImpl<U, T> map = supplier.get();
22+
public GraphGraphMap<U, T> deserialize(String representation) {
23+
GraphGraphMap<U, T> map = supplier.get();
2424
int index = 0;
2525
for (U location: map.locations()){
2626
char character = representation.charAt(index);
@@ -35,7 +35,7 @@ public GraphMapImpl<U, T> deserialize(String representation) {
3535
}
3636

3737
@Override
38-
public String serialize(GraphMapImpl<U, T> map) {
38+
public String serialize(GraphGraphMap<U, T> map) {
3939
StringBuilder serialization = new StringBuilder();
4040
for (U location: map.locations()){
4141
T item = map.get(location);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.nmerrill.kothcomm.game.maps.generators;
2+
3+
import com.nmerrill.kothcomm.game.maps.MapPoint;
4+
import com.nmerrill.kothcomm.game.maps.graphmaps.GraphGraphMap;
5+
import com.nmerrill.kothcomm.game.maps.graphmaps.neighborhoods.Neighborhood;
6+
import com.nmerrill.kothcomm.game.maps.graphmaps.bounds.Region;
7+
8+
public class FillRegion<U extends MapPoint> implements Generator<GraphGraphMap<U, ?>>{
9+
private final Neighborhood<U> neighborhood;
10+
private final Region<U> region;
11+
public FillRegion(Neighborhood<U> neighborhood, Region<U> region){
12+
this.neighborhood = neighborhood;
13+
this.region = region;
14+
}
15+
16+
@Override
17+
public void generate(GraphGraphMap<U, ?> map) {
18+
addPoint(region.startingPoint(), map);
19+
}
20+
21+
public void addPoint(U point, GraphGraphMap<U, ?> map){
22+
if (map.contains(point)){
23+
return;
24+
}
25+
map.put(point, null);
26+
neighborhood.getAdjacencies(point).select(region::inBounds).forEachWith(this::addPoint, map);
27+
}
28+
}

src/main/java/com/nmerrill/kothcomm/game/maps/generators/Populate.java

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 92 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,114 @@
11
package com.nmerrill.kothcomm.game.maps.generators.mazes;
22

3-
import com.nmerrill.kothcomm.game.maps.generators.Generator;
4-
import com.nmerrill.kothcomm.game.maps.graphmaps.GraphMapImpl;
53
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;
67
import com.nmerrill.kothcomm.utils.iterables.Itertools;
8+
import org.eclipse.collections.api.list.MutableList;
9+
import org.eclipse.collections.api.list.primitive.MutableBooleanList;
710
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;
814

9-
import java.util.List;
1015
import java.util.Random;
11-
import java.util.stream.IntStream;
1216

13-
public class Ellers implements Generator<GraphMapImpl<Point2D, ?>> {
17+
public class Ellers implements Generator<GraphGraphMap<Point2D, ?>> {
1418
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) {
1725
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;
3227
}
33-
public Ellers(Point2D p1, Point2D p2){
34-
this(p1, p2, new Random());
28+
29+
public Ellers(SquareRegion region) {
30+
this(region, new Random());
3531
}
3632

3733
@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+
}
4750
}
4851

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+
}
5175
}
5276

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+
});
5892
}
59-
/*
60-
states = range(self.maze.width)
61-
for y in xrange(self.maze.height):
62-
states = self.generate_line(y, states)
6393

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+
}
82113

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-
*/
96114
}

src/main/java/com/nmerrill/kothcomm/game/maps/generators/mazes/GrowingTree.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.nmerrill.kothcomm.game.maps.generators.Generator;
44
import com.nmerrill.kothcomm.game.maps.MapPoint;
5-
import com.nmerrill.kothcomm.game.maps.graphmaps.GraphMapImpl;
5+
import com.nmerrill.kothcomm.game.maps.graphmaps.GraphGraphMap;
66
import com.nmerrill.kothcomm.utils.iterables.Itertools;
77
import org.eclipse.collections.api.list.MutableList;
88
import org.eclipse.collections.api.set.MutableSet;
@@ -14,7 +14,7 @@
1414
import java.util.Random;
1515
import java.util.function.ToIntFunction;
1616

17-
public class GrowingTree<U extends MapPoint> implements Generator<GraphMapImpl<U, ?>> {
17+
public class GrowingTree<U extends MapPoint> implements Generator<GraphGraphMap<U, ?>> {
1818

1919
@SuppressWarnings({"SameReturnValue", "UnusedParameters"})
2020
public static int selectOldest(MutableList<? extends MapPoint> points){
@@ -48,7 +48,7 @@ public GrowingTree(){
4848
}
4949

5050
@Override
51-
public void generate(GraphMapImpl<U, ?> map) {
51+
public void generate(GraphGraphMap<U, ?> map) {
5252
MutableList<U> locations = map.locations().toList();
5353
U start = locations.get(random.nextInt(locations.size()));
5454
MutableList<U> borders = map.getNeighbors(start).toList();

src/main/java/com/nmerrill/kothcomm/game/maps/graphmaps/GraphMapImpl.java renamed to src/main/java/com/nmerrill/kothcomm/game/maps/graphmaps/GraphGraphMap.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.nmerrill.kothcomm.game.maps.graphmaps;
22

33
import com.nmerrill.kothcomm.game.maps.MapPoint;
4-
import com.nmerrill.kothcomm.game.maps.graphmaps.bounds.Bounds;
4+
import com.nmerrill.kothcomm.game.maps.graphmaps.bounds.Region;
55
import org.eclipse.collections.api.list.MutableList;
66
import org.eclipse.collections.api.map.MutableMap;
77
import org.eclipse.collections.api.multimap.MutableMultimap;
@@ -12,21 +12,21 @@
1212
import java.util.Objects;
1313

1414

15-
public class GraphMapImpl<U extends MapPoint, T> implements GraphMap<U, T>{
15+
public class GraphGraphMap<U extends MapPoint, T> implements GraphMap<U, T>{
1616

1717
private final MutableMultimap<U, U> connections;
1818
private final MutableMap<U, T> items;
1919

20-
public GraphMapImpl(){
20+
public GraphGraphMap(){
2121
connections = Multimaps.mutable.set.empty();
2222
items = Maps.mutable.empty();
2323
}
2424

2525
@Override
26-
public GraphMapImpl<U, T> subMap(Bounds<U> bounds) {
27-
GraphMapImpl<U, T> map = new GraphMapImpl<>();
28-
map.connections.putAll(connections.selectKeysValues((i, j) -> bounds.inBounds(i) && bounds.inBounds(j)));
29-
map.items.putAll(items.select((i, j) -> bounds.inBounds(i)));
26+
public GraphGraphMap<U, T> subMap(Region<U> region) {
27+
GraphGraphMap<U, T> map = new GraphGraphMap<>();
28+
map.connections.putAll(connections.selectKeysValues((i, j) -> region.inBounds(i) && region.inBounds(j)));
29+
map.items.putAll(items.select((i, j) -> region.inBounds(i)));
3030
return map;
3131
}
3232

src/main/java/com/nmerrill/kothcomm/game/maps/graphmaps/GraphMap.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.nmerrill.kothcomm.game.maps.GameMap;
44
import com.nmerrill.kothcomm.game.maps.MapPoint;
5-
import com.nmerrill.kothcomm.game.maps.graphmaps.bounds.Bounds;
5+
import com.nmerrill.kothcomm.game.maps.graphmaps.bounds.Region;
66
import com.nmerrill.kothcomm.utils.iterables.Itertools;
77
import org.eclipse.collections.api.block.function.Function;
88
import org.eclipse.collections.api.list.ImmutableList;
@@ -72,8 +72,8 @@ default U randomFilledLocation(){
7272
return randomFilledLocation(new Random());
7373
}
7474

75-
default GraphMap<U, T> subMap(Bounds<U> bounds){
76-
return new BoundedGraphMap<>(this, bounds);
75+
default GraphMap<U, T> subMap(Region<U> region){
76+
return new GraphMapView<>(this, region);
7777
}
7878

7979
MutableList<T> items();

0 commit comments

Comments
 (0)