mirror of
https://github.com/cyberarm/i-mic-rts.git
synced 2025-12-15 15:52:34 +00:00
Updated pathfinder to consider cost of tile instead of only distance to goal
This commit is contained in:
@@ -53,7 +53,7 @@ class IMICRTS
|
|||||||
position.x, position.y = position.x.floor, position.y.floor
|
position.x, position.y = position.x.floor, position.y.floor
|
||||||
position /= @map.tile_size
|
position /= @map.tile_size
|
||||||
|
|
||||||
@current_node = create_node(position.x, position.y)
|
@current_node = create_node(position.x, position.y, nil)
|
||||||
unless @current_node
|
unless @current_node
|
||||||
puts "Failed to find path!" if Setting.enabled?(:debug_mode)
|
puts "Failed to find path!" if Setting.enabled?(:debug_mode)
|
||||||
return
|
return
|
||||||
@@ -129,19 +129,19 @@ class IMICRTS
|
|||||||
if @allow_diagonal
|
if @allow_diagonal
|
||||||
# LEFT-UP
|
# LEFT-UP
|
||||||
if node_above? && node_above_left?
|
if node_above? && node_above_left?
|
||||||
add_node create_node(@current_node.tile.grid_position.x - 1, @current_node.tile.grid_position.y - 1, @current_node)
|
add_node create_node(@current_node.tile.grid_position.x - 1, @current_node.tile.grid_position.y - 1, @current_node, 1.4)
|
||||||
end
|
end
|
||||||
# LEFT-DOWN
|
# LEFT-DOWN
|
||||||
if node_below? && node_below_left?
|
if node_below? && node_below_left?
|
||||||
add_node create_node(@current_node.tile.grid_position.x - 1, @current_node.tile.grid_position.y + 1, @current_node)
|
add_node create_node(@current_node.tile.grid_position.x - 1, @current_node.tile.grid_position.y + 1, @current_node, 1.4)
|
||||||
end
|
end
|
||||||
# RIGHT-UP
|
# RIGHT-UP
|
||||||
if node_above? && node_above_right?
|
if node_above? && node_above_right?
|
||||||
add_node create_node(@current_node.tile.grid_position.x + 1, @current_node.tile.grid_position.y - 1, @current_node)
|
add_node create_node(@current_node.tile.grid_position.x + 1, @current_node.tile.grid_position.y - 1, @current_node, 1.4)
|
||||||
end
|
end
|
||||||
# RIGHT-DOWN
|
# RIGHT-DOWN
|
||||||
if node_below? && node_below_right?
|
if node_below? && node_below_right?
|
||||||
add_node create_node(@current_node.tile.grid_position.x + 1, @current_node.tile.grid_position.y + 1, @current_node)
|
add_node create_node(@current_node.tile.grid_position.x + 1, @current_node.tile.grid_position.y + 1, @current_node, 1.4)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -160,7 +160,7 @@ class IMICRTS
|
|||||||
return node
|
return node
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_node(x, y, parent = nil)
|
def create_node(x, y, parent, cost = 1.0)
|
||||||
return unless tile = @map.tiles.dig(x, y)
|
return unless tile = @map.tiles.dig(x, y)
|
||||||
return unless tile.type == @travels_along
|
return unless tile.type == @travels_along
|
||||||
return if tile.entity
|
return if tile.entity
|
||||||
@@ -180,21 +180,19 @@ class IMICRTS
|
|||||||
def next_node
|
def next_node
|
||||||
fittest = nil
|
fittest = nil
|
||||||
fittest_distance = Float::INFINITY
|
fittest_distance = Float::INFINITY
|
||||||
|
fittest_cost = Float::INFINITY
|
||||||
|
|
||||||
distance = nil
|
distance = nil
|
||||||
@nodes.each do |node|
|
@nodes.each do |node|
|
||||||
next if node == @current_node
|
next if node == @current_node
|
||||||
|
|
||||||
distance = node.tile.grid_position.distance(@goal)
|
distance = node.tile.grid_position.distance(@goal)
|
||||||
|
cost = node.cost
|
||||||
|
|
||||||
if distance < fittest_distance
|
if distance < fittest_distance && cost < fittest_cost
|
||||||
if fittest && (node.distance + node.cost) < (fittest.distance + fittest.cost)
|
fittest = node
|
||||||
fittest = node
|
fittest_distance = distance
|
||||||
fittest_distance = distance
|
fittest_cost = cost
|
||||||
else
|
|
||||||
fittest = node
|
|
||||||
fittest_distance = distance
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user