Method: Core::Game::Map#astar

Defined in:
lib/game/map/map.rb

#astar(node_start, node_goal) ⇒ Object



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
# File 'lib/game/map/map.rb', line 171

def astar(node_start, node_goal)
  iterations = 0
  open = AStar::PriorityQueue.new()
  closed = AStar::PriorityQueue.new()
  node_start.calc_h(node_goal)
  open.push(node_start)
  costmap = generate_costmap
  while !open.empty? do
    iterations += 1 #keep track of how many times this itersates
    node_current = open.find_best
    if node_current == node_goal
      #puts("Iterations: #{iterations}")
      #show_path(node_current)
      return node_current 
    end       
    generate_successor_nodes(node_current, costmap) { |node_successor|
      #now doing for each successor node of node_current
      node_successor.calc_g(node_current)
      #skip to next node_successor if better one already on open or closed list
      if open_successor=open.find(node_successor) then 
        if open_successor<=node_successor then next end  #need to account for nil result
      end
      if closed_successor=closed.find(node_successor) then
        if closed_successor<=node_successor then next end 
      end
      #still here, then there's no better node yet, so remove any copies of this node on open/closed lists
      open.remove(node_successor)
      closed.remove(node_successor)
      # set the parent node of node_successor to node_current
      node_successor.parent=node_current
      # set h to be the estimated distance to node_goal using the heuristic
      node_successor.calc_h(node_goal)
      # so now we know this is the best copy of the node so far, so put it onto the open list
      open.push(node_successor)
    }
    #now we've gone through all the successors, so the current node can be closed
    closed.push(node_current)
  end
end