Class: Gingham::PathFinder
- Inherits:
-
Object
- Object
- Gingham::PathFinder
- Defined in:
- lib/gingham/path_finder.rb
Class Method Summary collapse
- .find_adjacent_cells(space, cell, jump_power = 999) ⇒ Object
- .find_adjacent_waypoints(space, wp, jump_power = 999) ⇒ Object
- .find_move_path(space, from, to, move_power = 999, jump_power = 999) ⇒ Object
- .find_skill_path(space, from, to, max_height = 999) ⇒ Object
Class Method Details
.find_adjacent_cells(space, cell, jump_power = 999) ⇒ Object
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 |
# File 'lib/gingham/path_finder.rb', line 141 def self.find_adjacent_cells(space, cell, jump_power = 999) raise unless space && space.is_a?(Gingham::Space) raise unless cell && cell.is_a?(Gingham::Cell) adjacent_list = [] w, d, h = space.width, space.depth, space.height x, y, z = cell.x, cell.y, cell.z if x + 1 < w target_cell = space.ground_at(x + 1, y) if target_cell if !target_cell.occupied? || (z - target_cell.z).abs <= jump_power adjacent_list << target_cell end end end if x - 1 >= 0 target_cell = space.ground_at(x - 1, y) if target_cell if !target_cell.occupied? || (z - target_cell.z).abs <= jump_power adjacent_list << target_cell end end end if y + 1 < d target_cell = space.ground_at(x, y + 1) if target_cell if !target_cell.occupied? || (z - target_cell.z).abs <= jump_power adjacent_list << target_cell end end end if y - 1 >= 0 target_cell = space.ground_at(x, y - 1) if target_cell if !target_cell.occupied? || (z - target_cell.z).abs <= jump_power adjacent_list << target_cell end end end adjacent_list end |
.find_adjacent_waypoints(space, wp, jump_power = 999) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/gingham/path_finder.rb', line 122 def self.find_adjacent_waypoints(space, wp, jump_power = 999) raise unless space && space.is_a?(Gingham::Space) raise unless wp && wp.is_a?(Gingham::Waypoint) adjacent_list = [] adjacent_cells = Gingham::PathFinder.find_adjacent_cells(space, wp.cell, jump_power) adjacent_cells.each do |cell| move_direction = Gingham::Waypoint.detect_direction(wp, cell) parent = wp if move_direction != wp.direction turn_wp = Gingham::Waypoint.new(wp.cell, move_direction, wp) parent = turn_wp end move_to = Gingham::Waypoint.new(cell, move_direction, parent) adjacent_list << move_to end adjacent_list end |
.find_move_path(space, from, to, move_power = 999, jump_power = 999) ⇒ Object
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 |
# File 'lib/gingham/path_finder.rb', line 4 def find_move_path(space, from, to, move_power = 999, jump_power = 999) raise ArgumentError unless space && space.is_a?(Gingham::Space) raise ArgumentError unless from && from.is_a?(Gingham::Waypoint) && to && to.is_a?(Gingham::Waypoint) open_list = [from] return open_list if from.cell == to.cell close_list = [] loop_limit = 0 while open_list.size > 0 && loop_limit < 1000 do current_wp = open_list.first close_list << current_wp open_list = open_list.drop 1 adjacent_waypoints = Gingham::PathFinder.find_adjacent_waypoints(space, current_wp, jump_power) adjacent_waypoints.each do |wp| if wp.sum_cost < move_power open_list << wp unless close_list.include? wp end end loop_limit += 1 end shortest_chains = [from] end_points = close_list.select { |closed| closed.cell == to.cell } unless end_points.size.zero? shortest_cost = 999 end_points.each do |end_wp| if end_wp.sum_cost < shortest_cost shortest_cost = end_wp.sum_cost shortest_chains = end_wp.chains end end end shortest_chains end |
.find_skill_path(space, from, to, max_height = 999) ⇒ Object
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 |
# File 'lib/gingham/path_finder.rb', line 42 def find_skill_path(space, from, to, max_height = 999) path = [from] should_move_y = from.direction == Gingham::Direction::D8 || from.direction == Gingham::Direction::D2 loop_limit = 0 while path.last.cell.x != to.cell.x || path.last.cell.y != to.cell.y loop_limit += 1 break if loop_limit > 30 if should_move_y && path.last.cell.y != to.cell.y if path.last.cell.y < to.cell.y if path.last.cell.y + 1 != space.depth height = space.height_at(path.last.cell.x, path.last.cell.y + 1) cell = space.cells[path.last.cell.x][path.last.cell.y + 1][height] break unless cell.passable? if path.last.direction != 8 path << Gingham::Waypoint.new(path.last.cell, 8, path.last) end break if cell.z > max_height path << Gingham::Waypoint.new(cell, 8, path.last) end elsif path.last.cell.y > to.cell.y if path.last.cell.y - 1 >= 0 height = space.height_at(path.last.cell.x, path.last.cell.y - 1) cell = space.cells[path.last.cell.x][path.last.cell.y - 1][height] break unless cell.passable? if path.last.direction != 2 path << Gingham::Waypoint.new(path.last.cell, 2, path.last) end break if cell.z > max_height path << Gingham::Waypoint.new(cell, 2, path.last) end end should_move_y = false next else should_move_y = false end if !should_move_y && path.last.cell.x != to.cell.x if path.last.cell.x < to.cell.x if path.last.cell.x + 1 != space.width height = space.height_at(path.last.cell.x + 1, path.last.cell.y) cell = space.cells[path.last.cell.x + 1][path.last.cell.y][height] break unless cell.passable? if path.last.direction != 6 path << Gingham::Waypoint.new(path.last.cell, 6, path.last) end break if cell.z > max_height path << Gingham::Waypoint.new(cell, 6, path.last) end elsif path.last.cell.x > to.cell.x if path.last.cell.x - 1 >= 0 height = space.height_at(path.last.cell.x - 1, path.last.cell.y) cell = space.cells[path.last.cell.x - 1][path.last.cell.y][height] break unless cell.passable? if path.last.direction != 4 path << Gingham::Waypoint.new(path.last.cell, 4, path.last) end break if cell.z > max_height path << Gingham::Waypoint.new(cell, 4, path.last) end end end should_move_y = true end path.compact end |