forked from Ohohcakester/Any-Angle-Pathfinding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultGenerator.java
More file actions
196 lines (174 loc) · 8.2 KB
/
Copy pathDefaultGenerator.java
File metadata and controls
196 lines (174 loc) · 8.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main.graphgeneration;
import grid.GridAndGoals;
import grid.GridGraph;
import java.util.Random;
/**
* Generates a random "block map".
* A block map is made out of small 2x2 blocks and is guaranteed to have no squeezable corners.<br>
* Exception: the removed block at sx,sy and ex,ey.
*/
public class DefaultGenerator {
/**
* Generates a graph using the parameters specified in the arguments.
* @param _seed The random seed to be used
* @param _sizeX x-axis size
* @param _sizeY y-axis size
* @param _ratio chance of spawning a cluster of blocked tiles is 1 in unblockedRatio.
* @param _sx x-coordinate of start point
* @param _sy y-coordinate of start point
* @param _ex x-coordinate of goal point
* @param _ey y-coordinate of goal point
* @return the generated gridGraph.
*/
public static GridAndGoals generateSeededOld(int seed, int sizeX, int sizeY, int unblockedRatio, int sx, int sy, int ex, int ey) {
GridGraph gridGraph = generate(true, seed, sizeX, sizeY, unblockedRatio, sx, sy, ex, ey);
return new GridAndGoals(gridGraph, sx, sy, ex, ey);
}
public static GridAndGoals generateUnseededOld(int sizeX, int sizeY, int unblockedRatio, int sx, int sy, int ex, int ey) {
GridGraph gridGraph = generate(false, 0, sizeX, sizeY, unblockedRatio, sx, sy, ex, ey);
return new GridAndGoals(gridGraph, sx, sy, ex, ey);
}
/**
* Does not remove the block at 0,0
*/
public static GridAndGoals generateSeeded(int seed, int sizeX, int sizeY, int unblockedRatio, int sx, int sy, int ex, int ey) {
GridGraph gridGraph = generate(true, seed, sizeX, sizeY, unblockedRatio, -1,-1,-1,-1);
return new GridAndGoals(gridGraph, sx, sy, ex, ey);
}
/**
* Does not remove the block at 0,0
*/
public static GridAndGoals generateUnseeded(int sizeX, int sizeY, int unblockedRatio, int sx, int sy, int ex, int ey) {
GridGraph gridGraph = generate(false, 0, sizeX, sizeY, unblockedRatio, -1,-1,-1,-1);
return new GridAndGoals(gridGraph, sx, sy, ex, ey);
}
/**
* For backwards compatibility. Removes the block at 0,0.
*/
public static GridGraph generateSeededGraphOnlyOld(int seed, int sizeX, int sizeY, int unblockedRatio) {
return generate(true, seed, sizeX, sizeY, unblockedRatio, 0,0,0,0);
}
/**
* Does not remove the block at 0,0
*/
public static GridGraph generateSeededGraphOnly(int seed, int sizeX, int sizeY, int unblockedRatio) {
return generate(true, seed, sizeX, sizeY, unblockedRatio, -1,-1,-1,-1);
}
/**
* Removes the blocks at sx,sy and ex,ey.
*/
public static GridGraph generateSeededGraphOnly(int seed, int sizeX, int sizeY, int unblockedRatio, int sx, int sy, int ex, int ey) {
return generate(true, seed, sizeX, sizeY, unblockedRatio, sx, sy, ex, ey);
}
public static GridAndGoals generateSeededTrueRandomGraph(long seed, int sizeX, int sizeY, int frequency, int sx, int sy, int ex, int ey) {
return new GridAndGoals(generateSeededTrueRandomGraphOnly(seed, sizeX, sizeY, frequency), sx, sy, ex, ey);
}
public static GridGraph generateSeededTrueRandomGraphOnly(long seed, int sizeX, int sizeY, int frequency) {
GridGraph gridGraph = new GridGraph(sizeX, sizeY);
//System.out.println("True Random Graph with predefined seed = " + seed);
Random rand = new Random(seed);
generateRandomMap(rand, gridGraph, frequency);
return gridGraph;
}
/**
* Generates a graph using the static parameters defined at the beginning of the class.
*/
private static GridGraph generate(boolean seededRandom, int seed, int sizeX, int sizeY, int unblockedRatio, int sx, int sy, int ex, int ey) {
GridGraph gridGraph = new GridGraph(sizeX, sizeY);
Random rand = new Random();
if (!seededRandom) {
seed = rand.nextInt();
System.out.println("Starting random with random seed = " + seed);
} else {
System.out.println("Starting random with predefined seed = " + seed);
}
rand = new Random(seed);
generateRandomBlockMap(rand, gridGraph, unblockedRatio);
fillCorners(rand, gridGraph);
gridGraph.trySetBlocked(sx, sy, false);
gridGraph.trySetBlocked(ex, ey, false);
return gridGraph;
}
/**
* Generates a truly random map for the gridGraph.
* No longer used as this does not generate very good or realistic grids.
*/
private static void generateRandomMap(Random rand, GridGraph gridGraph, int frequency) {
for (int x = 0; x < gridGraph.sizeX; x++) {
for (int y = 0; y < gridGraph.sizeY; y++) {
gridGraph.setBlocked(x, y, rand.nextInt()%frequency == 0);
}
}
}
/**
* Post-processing step to remove situations like this:
* <pre>
* ___ ___
* | |||||
* |...X'''|
* |||||___|</pre>
* An unblocked path can pass through x from the top left to the bottom right.<br>
* We fix this by filling up the gaps with additional blocks.
*/
private static void fillCorners(Random rand, GridGraph gridGraph) {
boolean didSomething = true;;
while (didSomething) {
didSomething = false;
for (int x = 0; x < gridGraph.sizeX; x++) {
for (int y = 0; y < gridGraph.sizeY; y++) {
if (gridGraph.isBlocked(x, y)) {
if (gridGraph.isValidBlock(x+1, y+1) && gridGraph.isBlocked(x+1, y+1)) {
if (!gridGraph.isBlocked(x+1, y) && !gridGraph.isBlocked(x, y+1)) {
if (rand.nextBoolean())
gridGraph.setBlocked(x+1, y, true);
else
gridGraph.setBlocked(x, y+1, true);
didSomething = true;
}
}
if (gridGraph.isValidBlock(x-1, y+1) && gridGraph.isBlocked(x-1, y+1)) {
if (!gridGraph.isBlocked(x-1, y) && !gridGraph.isBlocked(x, y+1)) {
if (rand.nextBoolean())
gridGraph.setBlocked(x-1, y, true);
else
gridGraph.setBlocked(x, y+1, true);
didSomething = true;
}
}
}
}
}
}
}
/**
* Generates a random "block" map for the gridGraph.<br>
* This generates more realistic grids by spawning clusters of blocks
* instead of simply spawning random blocks.
*/
private static void generateRandomBlockMap(Random rand, GridGraph gridGraph, int frequency) {
for (int x = 0; x < gridGraph.sizeX; x++) {
for (int y = 0; y < gridGraph.sizeY; y++) {
if (rand.nextInt(frequency) == 0) {
switch(rand.nextInt(3)) {
case 0:
gridGraph.trySetBlocked(x, y, true);
gridGraph.trySetBlocked(x, y+1, true);
gridGraph.trySetBlocked(x+1, y, true);
gridGraph.trySetBlocked(x+1, y+1, true);
break;
case 1:
gridGraph.trySetBlocked(x, y-1, true);
gridGraph.trySetBlocked(x, y, true);
gridGraph.trySetBlocked(x, y+1, true);
break;
case 2:
gridGraph.trySetBlocked(x-1, y, true);
gridGraph.trySetBlocked(x, y, true);
gridGraph.trySetBlocked(x+1, y, true);
break;
}
}
}
}
}
}