Skip to content

Commit cb85412

Browse files
author
Dave
committed
Added E5 that can generate a simple maze map using DFS.
1 parent 32879db commit cb85412

11 files changed

Lines changed: 539 additions & 25 deletions

File tree

DukeMapGenLib/.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
66
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
77
<classpathentry kind="lib" path="libs/commons-io-2.4.jar"/>
8+
<classpathentry kind="lib" path="libs/commons-lang3-3.4.jar"/>
89
<classpathentry kind="output" path="bin"/>
910
</classpath>
424 KB
Binary file not shown.

DukeMapGenLib/src/trn/Map.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,31 @@ public Wall getWall(int i){
7777
return this.walls.get(i);
7878
}
7979

80+
/**
81+
* @return indexes of all walls in a sector
82+
*/
83+
public List<Integer> getSectorWallIndexes(int sectorIndex){
84+
85+
Sector sector = getSector(sectorIndex);
86+
87+
List<Integer> list = new ArrayList<Integer>(sector.getWallCount());
88+
89+
int safety = 10000;
90+
int index = sector.getFirstWall();
91+
while(safety-- > 0){
92+
list.add(index);
93+
94+
index = walls.get(index).getPoint2();
95+
96+
if(index == sector.getFirstWall()){
97+
break; //back to where we started
98+
}
99+
}
100+
101+
return list;
102+
103+
}
104+
80105
/**
81106
* WARNING: this does not automatically update any indexes (it does update wall count)
82107
*
@@ -132,6 +157,16 @@ public int addLoop(Wall ... wallsToAdd){
132157

133158
}
134159

160+
/**
161+
* adds the walls and the new sector object.
162+
*
163+
* @param wallsToAdd
164+
* @return
165+
*/
166+
public int createSectorFromLoop(Wall ... wallsToAdd){
167+
return addSector(new Sector(addLoop(wallsToAdd), wallsToAdd.length));
168+
}
169+
135170
/**
136171
* connects two sectors by creating the necessary link between the sectors' walls,
137172
* creating two-sided walls (which appear red in build);

DukeMapGenLib/src/trn/PlayerStart.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
public class PlayerStart {
88

99
/** facing "up" when looking at the map in build */
10-
public static final int NORTH = 1536;
10+
public static final int NORTH = 1536; //i think 2048 == 360 degrees, with 0 == east and 90 deg is south
11+
12+
public static final int SOUTH = 512; // 90 * (2048 / 360)
1113

1214
public static final int BYTE_COUNT = 14;
1315

DukeMapGenLib/src/trn/Sector.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ public short getFirstWall(){
125125
return this.firstWall;
126126
}
127127

128+
public short getWallCount(){
129+
return this.wallCount;
130+
}
131+
128132
public void toBytes(OutputStream output) throws IOException {
129133
ByteUtil.writeInt16LE(output, firstWall);
130134
ByteUtil.writeInt16LE(output, wallCount);

DukeMapGenLib/src/trn/Wall.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ public int getY(){
9797
return y;
9898
}
9999

100+
public boolean sameXY(Wall rh){
101+
return rh != null && x == rh.x && y == rh.y;
102+
}
103+
100104
/**
101105
* sets the next wall in the loop. calling this 'setPoint2' because the field 'nextWall' is already
102106
* take up by another field, which refers to the wall on the other side of this wall
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package trn.duke;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.apache.commons.lang3.tuple.ImmutablePair;
7+
import org.apache.commons.lang3.tuple.Pair;
8+
9+
import trn.Map;
10+
import trn.Wall;
11+
12+
public class Util {
13+
14+
/**
15+
*
16+
* @param map
17+
* @param sector0
18+
* @param sector1
19+
* @return wall indexes in each sector that match (by x,y coords) a wall in the other sector
20+
*/
21+
public static Pair<List<Integer>, List<Integer>> filterOverlappingPoints(trn.Map map, int sector0, int sector1){
22+
23+
List<Integer> sector0walls = map.getSectorWallIndexes(sector0);
24+
List<Integer> sector1walls = map.getSectorWallIndexes(sector1);
25+
26+
List<Integer> sector0wallsOut = new ArrayList<Integer>(sector0walls.size());
27+
List<Integer> sector1wallsOut = new ArrayList<Integer>(sector1walls.size());
28+
29+
30+
for(int w0i : sector0walls){
31+
for(int w1i : sector1walls){
32+
33+
if(map.getWall(w0i).sameXY(map.getWall(w1i))){
34+
sector0wallsOut.add(w0i);
35+
sector1wallsOut.add(w1i);
36+
}
37+
}
38+
}
39+
40+
return new ImmutablePair<List<Integer>, List<Integer>>(sector0wallsOut, sector1wallsOut);
41+
}
42+
43+
44+
/**
45+
* orders the walls so that wall 0 points to wall 1
46+
* @param map
47+
* @param walls
48+
*/
49+
public static void orderWalls(Map map, Integer ... walls){
50+
if(walls[0] == null || walls[1] == null) throw new RuntimeException();
51+
52+
53+
Wall w0 = map.getWall(walls[0]);
54+
Wall w1 = map.getWall(walls[1]);
55+
56+
if(w0.getPoint2() == walls[1]){
57+
//ok
58+
}else if(w1.getPoint2() == walls[0]){
59+
Integer tmp = walls[0];
60+
walls[0] = walls[1];
61+
walls[1] = tmp;
62+
}else{
63+
throw new RuntimeException("walls are not adjacent");
64+
}
65+
}
66+
67+
/**
68+
* orders the walls so that wall 0 points to wall 1 -- list versino
69+
* @param map
70+
* @param walls
71+
*/
72+
public static void orderWalls(Map map, List<Integer> walls){
73+
if(walls == null || walls.size() != 2) throw new IllegalArgumentException();
74+
75+
76+
Wall w0 = map.getWall(walls.get(0));
77+
Wall w1 = map.getWall(walls.get(1));
78+
79+
if(w0.getPoint2() == walls.get(1)){
80+
//ok
81+
}else if(w1.getPoint2() == walls.get(0)){
82+
Integer tmp = walls.get(0);
83+
walls.set(0, walls.get(1));
84+
walls.set(1, tmp);
85+
}else{
86+
throw new RuntimeException("walls are not adjacent");
87+
}
88+
}
89+
90+
/* so....this is completely useless since we are swapping on a fucking copy...
91+
public static void orderWalls(Map map, List<Integer> walls){
92+
orderWalls(map, walls.toArray(new Integer[]{}));
93+
}*/
94+
}

DukeMapGenLib/src/trn/duke/experiments/E4CreateLadderMaze.java

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import trn.PlayerStart;
88
import trn.Sector;
99
import trn.Wall;
10+
import trn.duke.Util;
1011

1112
/**
1213
* Map generator that starts by creating a square room, and then
@@ -228,7 +229,7 @@ static Integer[] getEastPoints(Map map, int startWallIndex){
228229

229230
}
230231

231-
orderWalls(map, walls);
232+
Util.orderWalls(map, walls);
232233
return walls;
233234

234235
}
@@ -274,33 +275,12 @@ static Integer[] getNorthPoints(Map map, int startWallIndex){
274275

275276
}
276277

277-
orderWalls(map, walls);
278+
Util.orderWalls(map, walls);
278279
return walls;
279280

280281
}
281282

282-
/**
283-
* orders the walls so that wall 0 points to wall 1
284-
* @param map
285-
* @param walls
286-
*/
287-
static void orderWalls(Map map, Integer[] walls){
288-
if(walls[0] == null || walls[1] == null) throw new RuntimeException();
289-
290-
291-
Wall w0 = map.getWall(walls[0]);
292-
Wall w1 = map.getWall(walls[1]);
293-
294-
if(w0.getPoint2() == walls[1]){
295-
//ok
296-
}else if(w1.getPoint2() == walls[0]){
297-
Integer tmp = walls[0];
298-
walls[0] = walls[1];
299-
walls[1] = tmp;
300-
}else{
301-
throw new RuntimeException("walls are not adjacent");
302-
}
303-
}
283+
304284

305285

306286
}

0 commit comments

Comments
 (0)