53 final Deque<Position> path =
new LinkedList<>();
55 final int distance = source.
getPosition().getChebyshevDistance(target);
56 if (distance >
Region.SIZE) {
57 return new Path(path);
65 return new Path(path);
69 int[][] via =
new int[104][104];
70 int[][] cost =
new int[104][104];
75 int destX = target.
getX() - regionX;
76 int destY = target.
getY() - regionY;
77 List<Integer> tileQueueX =
new ArrayList<>(9000);
78 List<Integer> tileQueueY =
new ArrayList<>(9000);
85 boolean foundPath =
false;
86 int pathLength = 4096;
88 while (tail != tileQueueX.size() && tileQueueX.size() < pathLength) {
90 curX = tileQueueX.get(tail);
91 curY = tileQueueY.get(tail);
93 int curAbsX = regionX + curX;
94 int curAbsY = regionY + curY;
96 if (curX == destX && curY == destY) {
102 if (targetWidth > 0 && targetLength > 0 &&
Region.reachable(targ, targetWidth, targetLength, position, source.
width(), source.
length())) {
107 int thisCost = cost[curX][curY] + 1 + 1;
108 tail = (tail + 1) % pathLength;
110 if (curY > 0 && via[curX][curY - 1] == 0
112 tileQueueX.add(curX);
113 tileQueueY.add(curY - 1);
114 via[curX][curY - 1] = 1;
115 cost[curX][curY - 1] = thisCost;
118 if (curX > 0 && via[curX - 1][curY] == 0
120 tileQueueX.add(curX - 1);
121 tileQueueY.add(curY);
122 via[curX - 1][curY] = 2;
123 cost[curX - 1][curY] = thisCost;
126 if (curY < 104 - 1 && via[curX][curY + 1] == 0
128 tileQueueX.add(curX);
129 tileQueueY.add(curY + 1);
130 via[curX][curY + 1] = 4;
131 cost[curX][curY + 1] = thisCost;
134 if (curX < 104 - 1 && via[curX + 1][curY] == 0
136 tileQueueX.add(curX + 1);
137 tileQueueY.add(curY);
138 via[curX + 1][curY] = 8;
139 cost[curX + 1][curY] = thisCost;
142 if (curX > 0 && curY > 0 && via[curX - 1][curY - 1] == 0
146 tileQueueX.add(curX - 1);
147 tileQueueY.add(curY - 1);
148 via[curX - 1][curY - 1] = 3;
149 cost[curX - 1][curY - 1] = thisCost;
152 if (curX > 0 && curY < 104 - 1 && via[curX - 1][curY + 1] == 0
156 tileQueueX.add(curX - 1);
157 tileQueueY.add(curY + 1);
158 via[curX - 1][curY + 1] = 6;
159 cost[curX - 1][curY + 1] = thisCost;
162 if (curX < 104 - 1 && curY > 0 && via[curX + 1][curY - 1] == 0
166 tileQueueX.add(curX + 1);
167 tileQueueY.add(curY - 1);
168 via[curX + 1][curY - 1] = 9;
169 cost[curX + 1][curY - 1] = thisCost;
172 if (curX < 104 - 1 && curY < 104 - 1 && via[curX + 1][curY + 1] == 0
176 tileQueueX.add(curX + 1);
177 tileQueueY.add(curY + 1);
178 via[curX + 1][curY + 1] = 12;
179 cost[curX + 1][curY + 1] = thisCost;
184 int hippo_max = 1_000;
185 int thisCost = 100 + 1;
188 for (
int x = destX - init_x; x <= destX + init_x; x++) {
189 for (
int y = destY - init_x; y <= destY + init_x; y++) {
190 if (x >= 0 && y >= 0 && x < 104 && y < 104 && cost[x][y] < 100 && cost[x][y] != 0) {
194 }
else if (x > destX + source.
width() - 1) {
195 dx = x - (destX + source.
width() - 1);
200 }
else if (y > destY + source.
length() - 1) {
201 dy = y - (destY + source.
length() - 1);
203 int hippo = dx * dx + dy * dy;
204 if (hippo < hippo_max || hippo == hippo_max && cost[x][y] < thisCost && cost[x][y] != 0) {
206 thisCost = cost[x][y];
214 if (hippo_max == 1000) {
215 return new Path(
null);
220 tileQueueX.set(tail, curX);
221 tileQueueY.set(tail++, curY);
224 for (
int nextVia = vurVia = via[curX][curY]; curX != source.
getPosition().getLocalX() || curY != source.
getPosition().
getLocalY(); nextVia = via[curX][curY]) {
225 if (nextVia != vurVia) {
227 tileQueueX.set(tail, curX);
228 tileQueueY.set(tail++, curY);
230 if ((nextVia & 2) != 0) {
232 }
else if ((nextVia & 8) != 0) {
235 if ((nextVia & 1) != 0) {
237 }
else if ((nextVia & 4) != 0) {
243 int pathX = (regionX) + tileQueueX.get(tail);
244 int pathY = (regionY) + tileQueueY.get(tail);
248 for (
int i = 1; i < s; i++) {
250 pathX = (regionX) + tileQueueX.get(tail);
251 pathY = (regionY) + tileQueueY.get(tail);
255 return new Path(path);