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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use direction::*;
use grid_2d::*;
use metadata::*;
use search::*;
use error::*;
use grid::*;
use path;

fn octile_distance(a: Coord, b: Coord) -> f64 {
    let dx = (a.x - b.x).abs();
    let dy = (a.y - b.y).abs();
    let (cardinal, ordinal) = if dx > dy { (dx, dx) } else { (dy, dx) };

    const SQRT_2_MIN_1: f64 = ::std::f64::consts::SQRT_2 - 1.0;
    cardinal as f64 + ordinal as f64 * SQRT_2_MIN_1
}

fn has_forced_neighbour_cardinal<G>(grid: &G, coord: Coord, direction: CardinalDirection) -> bool
where
    G: SolidGrid,
{
    if grid.is_solid_or_outside(coord + direction.left90().coord()) {
        return !grid.is_solid_or_outside(coord + direction.left45().coord());
    }

    if grid.is_solid_or_outside(coord + direction.right90().coord()) {
        return !grid.is_solid_or_outside(coord + direction.right45().coord());
    }

    false
}

fn has_forced_neighbour_ordinal<G>(grid: &G, coord: Coord, direction: OrdinalDirection) -> bool
where
    G: SolidGrid,
{
    if grid.is_solid_or_outside(coord + direction.left135().coord()) {
        return !grid.is_solid_or_outside(coord + direction.left90().coord());
    }

    if grid.is_solid_or_outside(coord + direction.right135().coord()) {
        return !grid.is_solid_or_outside(coord + direction.right90().coord());
    }

    false
}

fn jump_cardinal<G>(
    grid: &G,
    coord: Coord,
    direction: CardinalDirection,
    goal: Coord,
) -> Option<(Coord, f64)>
where
    G: SolidGrid,
{
    let neighbour_coord = coord + direction.coord();

    if grid.is_solid_or_outside(neighbour_coord) {
        return None;
    }

    const COST: f64 = 1.0;

    if neighbour_coord == goal {
        return Some((neighbour_coord, COST));
    }

    if has_forced_neighbour_cardinal(grid, neighbour_coord, direction) {
        return Some((neighbour_coord, COST));
    }

    jump_cardinal(grid, neighbour_coord, direction, goal).map(|(coord, cost)| (coord, cost + COST))
}

fn jump_ordinal<G>(
    grid: &G,
    coord: Coord,
    direction: OrdinalDirection,
    goal: Coord,
) -> Option<(Coord, f64)>
where
    G: SolidGrid,
{
    let neighbour_coord = coord + direction.coord();

    if grid.is_solid_or_outside(neighbour_coord) {
        return None;
    }

    const COST: f64 = ::std::f64::consts::SQRT_2;

    if neighbour_coord == goal {
        return Some((neighbour_coord, COST));
    }

    if has_forced_neighbour_ordinal(grid, neighbour_coord, direction) {
        return Some((neighbour_coord, COST));
    }

    let (card0, card1) = direction.to_cardinals();

    if jump_cardinal(grid, neighbour_coord, card0, goal).is_some()
        || jump_cardinal(grid, neighbour_coord, card1, goal).is_some()
    {
        return Some((neighbour_coord, COST));
    }

    jump_ordinal(grid, neighbour_coord, direction, goal).map(|(coord, cost)| (coord, cost + COST))
}

impl SearchContext<f64> {
    fn expand_cardinal<G>(
        &mut self,
        grid: &G,
        current_coord: Coord,
        current_cost: f64,
        direction: CardinalDirection,
        goal: Coord,
    ) where
        G: SolidGrid,
    {
        if let Some((successor_coord, successor_cost)) =
            jump_cardinal(grid, current_coord, direction, goal)
        {
            self.see_successor(
                current_cost + successor_cost,
                successor_coord,
                direction.direction(),
                octile_distance,
                goal,
            );
        }
    }

    fn expand_ordinal<G>(
        &mut self,
        grid: &G,
        current_coord: Coord,
        current_cost: f64,
        direction: OrdinalDirection,
        goal: Coord,
    ) where
        G: SolidGrid,
    {
        if let Some((successor_coord, successor_cost)) =
            jump_ordinal(grid, current_coord, direction, goal)
        {
            self.see_successor(
                current_cost + successor_cost,
                successor_coord,
                direction.direction(),
                octile_distance,
                goal,
            );
        }
    }

    fn expand_general<G>(
        &mut self,
        grid: &G,
        current_coord: Coord,
        current_cost: f64,
        direction: Direction,
        goal: Coord,
    ) where
        G: SolidGrid,
    {
        match direction.typ() {
            DirectionType::Cardinal(direction) => {
                self.expand_cardinal(grid, current_coord, current_cost, direction, goal)
            }
            DirectionType::Ordinal(direction) => {
                self.expand_ordinal(grid, current_coord, current_cost, direction, goal)
            }
        }
    }

    pub fn jump_point_search_octile_distance_heuristic<G>(
        &mut self,
        grid: &G,
        start: Coord,
        goal: Coord,
        path: &mut Vec<Direction>,
    ) -> Result<SearchMetadata, Error>
    where
        G: SolidGrid,
    {
        let initial_entry = match self.init(start, goal, grid, path) {
            Ok(initial_entry) => initial_entry,
            Err(result) => return result,
        };

        let goal_index = self.node_grid
            .coord_to_index(goal)
            .expect("SearchContext too small for grid");

        for direction in Directions {
            self.expand_general(grid, start, initial_entry.cost, direction, goal);
        }

        let mut num_nodes_visited = 0;

        while let Some(current_entry) = self.priority_queue.pop() {
            num_nodes_visited += 1;

            if current_entry.node_index == goal_index {
                path::make_path_jump_points(&self.node_grid, goal, self.seq, path);
                return Ok(SearchMetadata { num_nodes_visited });
            }

            let (current_coord, current_cost, direction) = {
                let node = &mut self.node_grid[current_entry.node_index];
                if node.visited == self.seq {
                    continue;
                }
                node.visited = self.seq;
                let direction = node.from_parent.expect("Open set node without direction");
                (node.coord, node.cost, direction)
            };

            match direction.typ() {
                DirectionType::Cardinal(direction) => {
                    self.expand_cardinal(grid, current_coord, current_cost, direction, goal);
                    let left = direction.left90();
                    if grid.is_solid_or_outside(current_coord + left.coord()) {
                        self.expand_ordinal(
                            grid,
                            current_coord,
                            current_cost,
                            direction.left45(),
                            goal,
                        );
                    }
                    let right = direction.right90();
                    if grid.is_solid_or_outside(current_coord + right.coord()) {
                        self.expand_ordinal(
                            grid,
                            current_coord,
                            current_cost,
                            direction.right45(),
                            goal,
                        );
                    }
                }
                DirectionType::Ordinal(direction) => {
                    self.expand_ordinal(grid, current_coord, current_cost, direction, goal);
                    let (left, right) = direction.to_cardinals();
                    self.expand_cardinal(grid, current_coord, current_cost, left, goal);
                    self.expand_cardinal(grid, current_coord, current_cost, right, goal);

                    let (check_right, check_left) = direction.opposite().to_cardinals();

                    if grid.is_solid_or_outside(current_coord + check_left.coord()) {
                        self.expand_ordinal(
                            grid,
                            current_coord,
                            current_cost,
                            direction.left90(),
                            goal,
                        );
                    }
                    if grid.is_solid_or_outside(current_coord + check_right.coord()) {
                        self.expand_ordinal(
                            grid,
                            current_coord,
                            current_cost,
                            direction.right90(),
                            goal,
                        );
                    }
                }
            }
        }

        Err(Error::NoPath)
    }
}