|
| 1 | +package io.github.coderodde.pathfinding.finders.jps; |
| 2 | + |
| 3 | +import io.github.coderodde.pathfinding.finders.JumpPointSearchFinder; |
| 4 | +import io.github.coderodde.pathfinding.model.GridModel; |
| 5 | +import io.github.coderodde.pathfinding.utils.Cell; |
| 6 | +import io.github.coderodde.pathfinding.utils.CellType; |
| 7 | + |
| 8 | +/** |
| 9 | + * This class implements the algorithm for doing jumps also diagonally but only |
| 10 | + * if there is no obstacle wall crossing on the way. |
| 11 | + * |
| 12 | + * @author Rodion "rodde" Efremov |
| 13 | + * @version 1.0.0 (Oct 21, 2025) |
| 14 | + * @since 1.0.0 (Oct 21, 2025) |
| 15 | + */ |
| 16 | +public final class DiagonalNonCrossingJumper |
| 17 | + implements JumpPointSearchFinder.Jumper { |
| 18 | + |
| 19 | + /** |
| 20 | + * This method implements jumping when diagonal moves crossing obstacle wall |
| 21 | + * corners are not allowed. |
| 22 | + * |
| 23 | + * @param x the {@code X}-coordinate of the current cell. |
| 24 | + * @param y the {@code Y}-coordinate of the current cell. |
| 25 | + * @param px the {@code X}-coordinate of the parent cell. |
| 26 | + * @param py the {@code Y}-coordinate of the parent cell. |
| 27 | + * @param model the grid model. |
| 28 | + * |
| 29 | + * @return the next cell. |
| 30 | + */ |
| 31 | + @Override |
| 32 | + public Cell jump(int x, |
| 33 | + int y, |
| 34 | + int px, |
| 35 | + int py, |
| 36 | + GridModel model) { |
| 37 | + |
| 38 | + int dx = x - px; |
| 39 | + int dy = y - py; |
| 40 | + |
| 41 | + if (!model.isWalkable(x, y)) { |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + if (!model.getCellType(x, y).equals(CellType.SOURCE) && |
| 46 | + !model.getCellType(x, y).equals(CellType.TARGET)) { |
| 47 | + model.setCellType(x, y, CellType.TRACED); |
| 48 | + } |
| 49 | + |
| 50 | + if (model.getCell(x, y).equals(model.getTargetGridCell())) { |
| 51 | + return model.getTargetGridCell(); |
| 52 | + } |
| 53 | + |
| 54 | + if (dx != 0 && dy != 0) { |
| 55 | + if (jump(x + dx, |
| 56 | + y, |
| 57 | + x, |
| 58 | + y, |
| 59 | + model) != null || |
| 60 | + jump(x, |
| 61 | + y + dy, |
| 62 | + x, |
| 63 | + y, |
| 64 | + model) != null) { |
| 65 | + |
| 66 | + return model.getCell(x, y); |
| 67 | + } |
| 68 | + } else { |
| 69 | + if (dx != 0) { |
| 70 | + if ((model.isWalkable(x, y - 1) && |
| 71 | + !model.isWalkable(x - dx, y - 1)) || |
| 72 | + (model.isWalkable(x, y + 1) && |
| 73 | + !model.isWalkable(x - dx, y + 1))) { |
| 74 | + |
| 75 | + return model.getCell(x, y); |
| 76 | + } |
| 77 | + } else if (dy != 0) { |
| 78 | + if ((model.isWalkable(x - 1, y) && |
| 79 | + !model.isWalkable(x - 1, y - dy)) || |
| 80 | + (model.isWalkable(x + 1, y) && |
| 81 | + !model.isWalkable(x + 1, y - dy))) { |
| 82 | + |
| 83 | + return model.getCell(x, y); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (model.isWalkable(x + dx, y) && model.isWalkable(x, y + dy)) { |
| 89 | + return jump(x + dx, |
| 90 | + y + dy, |
| 91 | + x, |
| 92 | + y, |
| 93 | + model); |
| 94 | + } |
| 95 | + |
| 96 | + return null; |
| 97 | + } |
| 98 | +} |
0 commit comments