Skip to content

Commit 507d9c0

Browse files
committed
added extra information to cell states
1 parent 4f366c7 commit 507d9c0

File tree

6 files changed

+101
-56
lines changed

6 files changed

+101
-56
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
org.gradle.jvmargs = -Xmx1G
22
org.gradle.parallel = true
33

4-
version = 9.1.0
4+
version = 9.1.1
55
maven_group = net.ludocrypt
66
archives_base_name = limlib

src/main/java/net/ludocrypt/limlib/api/world/maze/CombineMaze.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,29 @@ public void generateMaze() {
1616
for (Vec2i pos : maze.solvedMaze) {
1717
int x = pos.getX();
1818
int y = pos.getY();
19+
CellState reference = maze.cellState(x, y);
1920

20-
if (maze.cellState(x, y).isNorth()) {
21+
if (reference.isNorth()) {
2122
this.cellState(x, y).setNorth(true);
2223
}
2324

24-
if (maze.cellState(x, y).isEast()) {
25+
if (reference.isEast()) {
2526
this.cellState(x, y).setEast(true);
2627
}
2728

28-
if (maze.cellState(x, y).isSouth()) {
29+
if (reference.isSouth()) {
2930
this.cellState(x, y).setSouth(true);
3031
}
3132

32-
if (maze.cellState(x, y).isWest()) {
33+
if (reference.isWest()) {
3334
this.cellState(x, y).setWest(true);
3435
}
3536

3637
if (this.cellState(x, y).isNorth() || this.cellState(x, y).isEast() || this.cellState(x, y).isSouth() || this.cellState(x, y).isWest()) {
3738
this.solvedMaze.add(new Vec2i(x, y));
3839
}
3940

40-
if (this.cellState(x, y).isNorth() && this.cellState(x, y).isEast() && this.cellState(x, y).isSouth() && this.cellState(x, y).isWest()) {
41-
continue;
42-
}
43-
41+
this.cellState(x, y).appendAll(reference.getExtra());
4442
}
4543

4644
}

src/main/java/net/ludocrypt/limlib/api/world/maze/DepthFirstMazeSolver.java

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public class DepthFirstMazeSolver extends MazeComponent {
1515
private final MazeComponent mazeToSolve;
1616
private final Vec2i start;
1717
private final List<Vec2i> ends;
18-
1918
public final RandomGenerator random;
2019

2120
/**
@@ -38,12 +37,11 @@ public DepthFirstMazeSolver(MazeComponent mazeToSolve, Vec2i start, List<Vec2i>
3837
@Override
3938
public void generateMaze() {
4039
List<Stack<Vec2i>> paths = Lists.newArrayList();
41-
4240
this.ends.forEach((end) -> {
4341
Stack<Vec2i> stack = new Stack<Vec2i>();
4442
stack.push(new Vec2i(start.getX(), start.getY()));
45-
4643
Vec2i peek = stack.peek();
44+
4745
while (!peek.equals(end)) {
4846
List<Integer> neighbours = Lists.newArrayList();
4947

@@ -71,22 +69,22 @@ public void generateMaze() {
7169
int nextCellDir = neighbours.get(random.nextInt(neighbours.size()));
7270

7371
switch (nextCellDir) {
74-
case 0: // North
75-
this.cellState(stack.peek().getX() + 1, stack.peek().getY()).visited();
76-
stack.push(new Vec2i(stack.peek().getX() + 1, stack.peek().getY()));
77-
break;
78-
case 1: // East
79-
this.cellState(stack.peek().getX(), stack.peek().getY() + 1).visited();
80-
stack.push(new Vec2i(stack.peek().getX(), stack.peek().getY() + 1));
81-
break;
82-
case 2: // South
83-
this.cellState(stack.peek().getX() - 1, stack.peek().getY()).visited();
84-
stack.push(new Vec2i(stack.peek().getX() - 1, stack.peek().getY()));
85-
break;
86-
case 3: // West
87-
this.cellState(stack.peek().getX(), stack.peek().getY() - 1).visited();
88-
stack.push(new Vec2i(stack.peek().getX(), stack.peek().getY() - 1));
89-
break;
72+
case 0: // North
73+
this.cellState(stack.peek().getX() + 1, stack.peek().getY()).visited();
74+
stack.push(new Vec2i(stack.peek().getX() + 1, stack.peek().getY()));
75+
break;
76+
case 1: // East
77+
this.cellState(stack.peek().getX(), stack.peek().getY() + 1).visited();
78+
stack.push(new Vec2i(stack.peek().getX(), stack.peek().getY() + 1));
79+
break;
80+
case 2: // South
81+
this.cellState(stack.peek().getX() - 1, stack.peek().getY()).visited();
82+
stack.push(new Vec2i(stack.peek().getX() - 1, stack.peek().getY()));
83+
break;
84+
case 3: // West
85+
this.cellState(stack.peek().getX(), stack.peek().getY() - 1).visited();
86+
stack.push(new Vec2i(stack.peek().getX(), stack.peek().getY() - 1));
87+
break;
9088
}
9189

9290
} else {
@@ -97,58 +95,67 @@ public void generateMaze() {
9795
}
9896

9997
for (int x = 0; x < width; x++) {
98+
10099
for (int y = 0; y < height; y++) {
101100
this.maze[y * this.width + x].setVisited(false);
102101
}
102+
103103
}
104104

105105
paths.add(stack);
106106
});
107-
108107
paths.forEach((path) -> {
108+
109109
for (int i = 0; i < path.size(); i++) {
110110
Vec2i pos = path.get(i);
111111

112112
if (i + 1 != path.size()) {
113113
Vec2i nextPos = path.get(i + 1);
114114

115115
if (nextPos.equals(new Vec2i(pos.getX() + 1, pos.getY()))) { // North
116-
this.cellState(pos.getX(), pos.getY()).north();
117-
this.cellState(nextPos.getX(), nextPos.getY()).south();
116+
this.cellState(pos).north();
117+
this.cellState(nextPos).south();
118118
} else if (nextPos.equals(new Vec2i(pos.getX(), pos.getY() + 1))) { // East
119-
this.cellState(pos.getX(), pos.getY()).east();
120-
this.cellState(nextPos.getX(), nextPos.getY()).west();
119+
this.cellState(pos).east();
120+
this.cellState(nextPos).west();
121121
} else if (nextPos.equals(new Vec2i(pos.getX() - 1, pos.getY()))) { // South
122-
this.cellState(pos.getX(), pos.getY()).south();
123-
this.cellState(nextPos.getX(), nextPos.getY()).north();
122+
this.cellState(pos).south();
123+
this.cellState(nextPos).north();
124124
} else if (nextPos.equals(new Vec2i(pos.getX(), pos.getY() - 1))) { // West
125-
this.cellState(pos.getX(), pos.getY()).west();
126-
this.cellState(nextPos.getX(), nextPos.getY()).east();
125+
this.cellState(pos).west();
126+
this.cellState(nextPos).east();
127127
}
128128

129+
this.cellState(pos).appendAll(mazeToSolve.cellState(pos).getExtra());
130+
129131
if (!this.solvedMaze.contains(new Vec2i(pos.getX(), pos.getY()))) {
130132
this.solvedMaze.add(new Vec2i(pos.getX(), pos.getY()));
131133
}
134+
132135
}
133136

134137
if (this.ends.contains(pos) || pos.equals(this.start)) {
138+
135139
if (pos.getX() == 0) {
136-
this.cellState(pos.getX(), pos.getY()).south();
140+
this.cellState(pos).south();
137141
}
138142

139143
if (pos.getY() == 0) {
140-
this.cellState(pos.getX(), pos.getY()).west();
144+
this.cellState(pos).west();
141145
}
142146

143147
if (pos.getX() == width - 1) {
144-
this.cellState(pos.getX(), pos.getY()).north();
148+
this.cellState(pos).north();
145149
}
146150

147151
if (pos.getY() == height - 1) {
148-
this.cellState(pos.getX(), pos.getY()).east();
152+
this.cellState(pos).east();
149153
}
154+
150155
}
156+
151157
}
158+
152159
});
153160
}
154161

src/main/java/net/ludocrypt/limlib/api/world/maze/DilateMaze.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package net.ludocrypt.limlib.api.world.maze;
2-
32
/**
43
* Dilates or scales a maze to be dilation times bigger.
54
**/
@@ -16,50 +15,71 @@ public DilateMaze(MazeComponent mazeIn, int dilation) {
1615

1716
@Override
1817
public void generateMaze() {
18+
1919
for (int x = 0; x < mazeIn.width; x++) {
20+
2021
for (int y = 0; y < mazeIn.height; y++) {
22+
2123
for (int dx = 0; dx < dilation; dx++) {
24+
2225
for (int dy = 0; dy < dilation; dy++) {
2326
int mazeX = x * dilation + dx;
2427
int mazeY = y * dilation + dy;
2528
Vec2i position = new Vec2i(mazeX, mazeY);
29+
CellState reference = mazeIn.cellState(x, y);
2630

2731
if (dx % dilation == 0) {
32+
2833
if (dy % dilation == 0) {
29-
CellState copy = mazeIn.cellState(x, y).copy();
34+
CellState copy = reference.copy();
3035
copy.setPosition(position);
3136
this.maze[mazeY * this.width + mazeX] = copy;
3237
this.solvedMaze.add(position);
3338
} else {
39+
3440
if (mazeIn.cellState(x, y).isEast()) {
35-
CellState dilatedCell = new CellState();
36-
dilatedCell.east();
37-
dilatedCell.west();
38-
dilatedCell.setPosition(position);
39-
this.maze[mazeY * this.width + mazeX] = dilatedCell;
41+
CellState copy = new CellState();
42+
copy.east();
43+
copy.west();
44+
copy.setPosition(position);
45+
copy.appendAll(reference.getExtra());
46+
this.maze[mazeY * this.width + mazeX] = copy;
4047
this.solvedMaze.add(position);
4148
}
49+
4250
}
51+
4352
} else {
53+
4454
if (dy % dilation == 0) {
55+
4556
if (mazeIn.cellState(x, y).isNorth()) {
46-
CellState dilatedCell = new CellState();
47-
dilatedCell.north();
48-
dilatedCell.south();
49-
dilatedCell.setPosition(position);
50-
this.maze[mazeY * this.width + mazeX] = dilatedCell;
57+
CellState copy = new CellState();
58+
copy.north();
59+
copy.south();
60+
copy.setPosition(position);
61+
copy.appendAll(reference.getExtra());
62+
this.maze[mazeY * this.width + mazeX] = copy;
5163
this.solvedMaze.add(position);
5264
}
65+
5366
} else {
5467
CellState copy = new CellState();
5568
copy.setPosition(position);
69+
copy.appendAll(reference.getExtra());
5670
this.maze[mazeY * this.width + mazeX] = copy;
5771
}
72+
5873
}
74+
5975
}
76+
6077
}
78+
6179
}
80+
6281
}
82+
6383
}
6484

6585
public MazeComponent getMazeIn() {

src/main/java/net/ludocrypt/limlib/api/world/maze/MazeComponent.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package net.ludocrypt.limlib.api.world.maze;
22

33
import java.util.List;
4+
import java.util.Map;
45

56
import com.google.common.collect.Lists;
7+
import com.google.common.collect.Maps;
68

79
import net.minecraft.util.math.BlockPos;
810

@@ -13,23 +15,25 @@ public abstract class MazeComponent {
1315

1416
public final int width;
1517
public final int height;
16-
1718
public final CellState[] maze;
1819
public final List<Vec2i> solvedMaze = Lists.newArrayList();
19-
2020
public int visitedCells = 0;
2121

2222
public MazeComponent(int width, int height) {
2323
this.width = width;
2424
this.height = height;
2525
this.maze = new CellState[width * height];
26+
2627
for (int x = 0; x < width; x++) {
28+
2729
for (int y = 0; y < height; y++) {
2830
CellState state = new CellState();
2931
state.setPosition(new Vec2i(x, y));
3032
this.maze[y * this.width + x] = state;
3133
}
34+
3235
}
36+
3337
this.visitedCells = 1;
3438
}
3539

@@ -88,6 +92,7 @@ public static class CellState {
8892
private boolean south = false;
8993
private boolean west = false;
9094
private boolean visited = false;
95+
private Map<String, Object> extra = Maps.newHashMap();
9196

9297
public CellState copy() {
9398
CellState newState = new CellState();
@@ -96,6 +101,7 @@ public CellState copy() {
96101
newState.setEast(this.east);
97102
newState.setSouth(this.south);
98103
newState.setWest(this.west);
104+
newState.appendAll(this.extra);
99105
return newState;
100106
}
101107

@@ -143,6 +149,14 @@ public void setVisited(boolean visited) {
143149
this.visited = visited;
144150
}
145151

152+
public void append(String name, Object data) {
153+
this.extra.put(name, data);
154+
}
155+
156+
public void appendAll(Map<String, Object> data) {
157+
this.extra.putAll(data);
158+
}
159+
146160
public Vec2i getPosition() {
147161
return position;
148162
}
@@ -167,6 +181,10 @@ public boolean isVisited() {
167181
return visited;
168182
}
169183

184+
public Map<String, Object> getExtra() {
185+
return extra;
186+
}
187+
170188
}
171189

172190
public static class Vec2i {
@@ -193,9 +211,11 @@ public BlockPos toPos() {
193211

194212
@Override
195213
public boolean equals(Object obj) {
214+
196215
if (obj instanceof Vec2i pos) {
197216
return pos.x == this.x && pos.y == this.y;
198217
}
218+
199219
return super.equals(obj);
200220
}
201221

src/main/java/net/ludocrypt/limlib/api/world/maze/RectangularMazeGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* A rectangular maze generator class
1212
*/
13-
public abstract class RectangularMazeGenerator<M extends MazeComponent> {
13+
public class RectangularMazeGenerator<M extends MazeComponent> {
1414

1515
private final HashMap<BlockPos, M> mazes = new HashMap<BlockPos, M>(30);
1616
public final int width;

0 commit comments

Comments
 (0)