Sha256: 94ee88ef8f23715b52f14b8ccf6042332451d56ca64e3d99289aa1aeadd064af

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 KB

Contents

# @gamefic.script standard/pathfinder
#   Pathfinders provide the shortest route between two locations. The
#   destination needs to be accessible from the origin through portals. Note
#   that Pathfinders do not take into account portals that characters should
#   not be able to traverse, such as locked doors.

class Pathfinder
  # @return [Room]
  attr_reader :origin
  # @return [Room]
  attr_reader :destination
  
  def initialize origin, destination
    @origin = origin
    @destination = destination
    @path = nil
    @paths = [[@origin]]
    @visited = []
    if @origin == @destination
      @path = []
    else
      while @path.nil? and @paths.length > 0
        embark
      end
    end
  end

  # @return [Array<Room>]
  def path
    # @path is nil if the path is invalid, but #path should return an empty
    # array instead.
    @path || []
  end

  # @return [Boolean]
  def valid?
    path.length > 0 or origin == destination
  end

  private

  def embark
    new_paths = []
    @paths.each { |path|
      last = path.last
      portals = last.children.that_are(Portal)
      portals.each { |portal|
        new_path = path.clone
        if !@visited.include?(portal.destination)
          new_path.push portal.destination
          @visited.push portal.destination
          if portal.destination == @destination
            @path = new_path
            @path.shift
            break
          end
          new_paths.push new_path
        end
      }
      path.push nil
    }
    @paths += new_paths
    @paths.delete_if{|path| path.last.nil?}
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
gamefic-standard-2.1.0 lib/gamefic-standard/pathfinder.rb
gamefic-standard-2.0.0 lib/gamefic-standard/pathfinder.rb
gamefic-sdk-1.7.0 scripts/standard/pathfinder.plot.rb