Class: Gingham::PathFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/gingham/path_finder.rb

Class Method Summary collapse

Class Method Details

.find_adjacent_cells(space, cell) ⇒ Object



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
# File 'lib/gingham/path_finder.rb', line 172

def self.find_adjacent_cells(space, cell)
  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.cells[x + 1][y][z]
    adjacent_list << target_cell unless target_cell && target_cell.occupied?
  end

  if x - 1 >= 0
    target_cell = space.cells[x - 1][y][z]
    adjacent_list << target_cell unless target_cell && target_cell.occupied?
  end

  if y + 1 < d
    target_cell = space.cells[x][y + 1][z]
    adjacent_list << target_cell unless target_cell && target_cell.occupied?
  end

  if y - 1 >= 0
    target_cell = space.cells[x][y - 1][z]
    adjacent_list << target_cell unless target_cell && target_cell.occupied?
  end

  adjacent_list
end

.find_adjacent_waypoints(space, wp) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/gingham/path_finder.rb', line 153

def self.find_adjacent_waypoints(space, wp)
  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)
  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, cost_limit = 999) ⇒ Object

Raises:

  • (ArgumentError)


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, cost_limit = 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)
    adjacent_waypoints.each do |wp|
      if wp.sum_cost < cost_limit
        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 = 10) ⇒ 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
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
# File 'lib/gingham/path_finder.rb', line 42

def find_skill_path(space, from, to, max_height = 10)
  path = [from]
  last_wp = path.last
  should_move_y = from.direction == Gingham::Direction::D8 || from.direction == Gingham::Direction::D2

  loop_limit = 0
  while last_wp.cell.x != to.cell.x || last_wp.cell.y != to.cell.y
    loop_limit += 1
    break if loop_limit > 30

    if should_move_y && last_wp.cell.y != to.cell.y
      if last_wp.cell.y < to.cell.y
        if last_wp.cell.y + 1 != space.depth
          height = space.height_at(last_wp.cell.x, last_wp.cell.y + 1)
          cell = space.cells[last_wp.cell.x][last_wp.cell.y + 1][height]
          break unless cell.passable?

          if last_wp.direction == 8
            break if cell.z > max_height
            wp = Gingham::Waypoint.new(cell, 8, last_wp)
            path << wp
            last_wp = wp
          else
            tmp = Gingham::Waypoint.new(last_wp.cell, 8, last_wp)
            path << tmp
            break if cell.z > max_height
            tmp = Gingham::Waypoint.new(cell, 8, tmp)
            path << tmp
            last_wp = tmp
          end
        end
      elsif last_wp.cell.y > to.cell.y
        if last_wp.cell.y - 1 >= 0
          height = space.height_at(last_wp.cell.x, last_wp.cell.y - 1)
          cell = space.cells[last_wp.cell.x][last_wp.cell.y - 1][height]
          break unless cell.passable?

          if last_wp.direction == 2
            break if cell.z > max_height
            wp = Gingham::Waypoint.new(cell, 2, last_wp)
            path << wp
            last_wp = wp
          else
            tmp = Gingham::Waypoint.new(last_wp.cell, 2, last_wp)
            path << tmp
            break if cell.z > max_height
            tmp = Gingham::Waypoint.new(cell, 2, tmp)
            path << tmp
            last_wp = tmp
          end
        end
      end
      should_move_y = false
      next
    else
      should_move_y = false
    end

    if !should_move_y && last_wp.cell.x != to.cell.x
      if last_wp.cell.x < to.cell.x
        if last_wp.cell.x + 1 != space.width
          height = space.height_at(last_wp.cell.x + 1, last_wp.cell.y)
          cell = space.cells[last_wp.cell.x + 1][last_wp.cell.y][height]
          break unless cell.passable?

          if last_wp.direction == 6
            break if cell.z > max_height
            wp = Gingham::Waypoint.new(cell, 6, last_wp)
            path << wp
            last_wp = wp
          else
            tmp = Gingham::Waypoint.new(last_wp.cell, 6, last_wp)
            path << tmp
            break if cell.z > max_height
            tmp = Gingham::Waypoint.new(cell, 6, tmp)
            path << tmp
            last_wp = tmp
          end
        end
      elsif last_wp.cell.x > to.cell.x
        if last_wp.cell.x - 1 >= 0
          height = space.height_at(last_wp.cell.x - 1, last_wp.cell.y)
          cell = space.cells[last_wp.cell.x - 1][last_wp.cell.y][height]
          break unless cell.passable?

          if last_wp.direction == 4
            break if cell.z > max_height
            wp = Gingham::Waypoint.new(cell, 4, last_wp)
            path << wp
            last_wp = wp
          else
            tmp = Gingham::Waypoint.new(last_wp.cell, 4, last_wp)
            path << tmp
            break if cell.z > max_height
            tmp = Gingham::Waypoint.new(cell, 4, tmp)
            path << tmp
            last_wp = tmp
          end
        end
      end
      should_move_y = true
      next
    else
      should_move_y = true
    end
  end

  path.compact
end