astar 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in astar.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Richard Patching
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Astar
2
+
3
+ Pathfinding library using the astar algorithm
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'astar'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install astar
18
+
19
+ ## Usage
20
+
21
+ Make sure you have an object that responds to methods x,y and walkable_neighbours
22
+
23
+ class Tile
24
+ attr_reader :x, :y
25
+
26
+ #returns the surrounding tiles that are walkable
27
+ def walkable_neighbours
28
+ end
29
+ end
30
+
31
+ Pass in your start and destination tiles
32
+
33
+ Astar::FindPath.from(tile1).to(tile3)
34
+ => [tile1, tile2, tile3]
35
+
36
+ The specs also have an example of how to use the lib.
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |spec|
5
+ spec.pattern = 'spec/**/*_spec.rb'
6
+ end
7
+
8
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
9
+ spec.pattern = 'spec/**/*_spec.rb'
10
+ spec.rcov = true
11
+ spec.rcov_opts = %q[--exclude "spec"]
12
+ end
13
+
14
+ task :default => :spec
data/astar.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'astar/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "astar"
8
+ gem.version = Astar::VERSION
9
+ gem.authors = ["Richard Patching"]
10
+ gem.email = ["richard@justaddpixels.com"]
11
+ gem.description = %q{Pathfinding library using the a* algorithm}
12
+ gem.summary = %q{Pathfinding library}
13
+ gem.homepage = "http://github.com/patchfx/astar"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency 'rspec', '~> 2.10.0'
21
+ gem.add_development_dependency 'rake', '~> 0.9.2.2'
22
+ end
data/lib/astar/node.rb ADDED
@@ -0,0 +1,38 @@
1
+ module Astar
2
+ class Node
3
+ attr_reader :tile, :parent
4
+ def initialize(tile, destination, parent=nil)
5
+ @tile = tile
6
+ @parent = parent
7
+ @destination = destination
8
+ end
9
+
10
+ def walkable_neighbours
11
+ @tile.walkable_neighbours
12
+ end
13
+
14
+ def x
15
+ @tile.x
16
+ end
17
+
18
+ def y
19
+ @tile.y
20
+ end
21
+
22
+ def score
23
+ relative_to_parent + manhatten_distance
24
+ end
25
+
26
+ def manhatten_distance
27
+ (@destination.x - @tile.x) + (@destination.y - @tile.y)
28
+ end
29
+
30
+ def relative_to_parent
31
+ return 10 if (@tile.x > @parent.x && @tile.y == @parent.y)
32
+ return 10 if (@tile.x < @parent.x && @tile.y == @parent.y)
33
+ return 10 if (@tile.y > @parent.y && @tile.x == @parent.x)
34
+ return 10 if (@tile.y < @parent.y && @tile.x == @parent.x)
35
+ 14
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module Astar
2
+ VERSION = "0.0.1"
3
+ end
data/lib/astar.rb ADDED
@@ -0,0 +1,48 @@
1
+ require "astar/node"
2
+ require "astar/version"
3
+
4
+ module Astar
5
+ class FindPath
6
+ def self.from(node)
7
+ new(node)
8
+ end
9
+
10
+ def to(node)
11
+ @to_node = Node.new(node,nil)
12
+ @open_list << Node.new(@from_node, @to_node)
13
+ calculate_route
14
+ end
15
+
16
+ def calculate_route
17
+ until destination_reached? || @open_list.empty?
18
+ current_node = @open_list.pop
19
+ next if @closed_list.include?(current_node)
20
+ add_neighbours_to_list(current_node, current_node.walkable_neighbours)
21
+ calculate_fastest
22
+ @closed_list << current_node
23
+ end
24
+ @closed_list
25
+ end
26
+
27
+ def add_neighbours_to_list(current_node, neighbours)
28
+ nodes = neighbours.map {|n| Node.new(n, @to_node, current_node) }
29
+ @open_list << nodes
30
+ @open_list.flatten!
31
+ end
32
+
33
+ def calculate_fastest
34
+ @open_list.sort_by! {|node| node.score }.reverse
35
+ end
36
+
37
+ def destination_reached?
38
+ @closed_list.any? { |node| node.x == @to_node.x && node.y == @to_node.y }
39
+ end
40
+
41
+ private
42
+ def initialize(node)
43
+ @from_node = Node.new(node, nil)
44
+ @open_list = []
45
+ @closed_list = []
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ module Astar
4
+ describe FindPath do
5
+ let(:node) { double(:node, x: 0, y: 0) }
6
+ let(:node2) { double(:node2, x: 1, y: 0) }
7
+ let(:node3) { double(:node3, x: 0, y: 1) }
8
+ let(:node4) { double(:node4, x: 1, y: 1) }
9
+
10
+ before do
11
+ node.stub(:walkable_neighbours => [node2,node3,node4])
12
+ node2.stub(:walkable_neighbours => [node,node3,node4])
13
+ node3.stub(:walkable_neighbours => [node,node2,node4])
14
+ node4.stub(:walkable_neighbours => [node2,node3,node])
15
+ end
16
+
17
+ it 'finds the fastest route between 2 nodes' do
18
+ path = FindPath.from(node).to(node4)
19
+ coords = path.collect { |node| [node.x, node.y] }
20
+ coords.should == [[0, 0], [1, 1]]
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'rubygems'
5
+ require 'bundler/setup'
6
+ require 'astar'
7
+ require 'rspec'
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: astar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Richard Patching
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.10.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.10.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.9.2.2
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.9.2.2
46
+ description: Pathfinding library using the a* algorithm
47
+ email:
48
+ - richard@justaddpixels.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - astar.gemspec
59
+ - lib/astar.rb
60
+ - lib/astar/node.rb
61
+ - lib/astar/version.rb
62
+ - spec/lib/astar_spec.rb
63
+ - spec/spec_helper.rb
64
+ homepage: http://github.com/patchfx/astar
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.23
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Pathfinding library
88
+ test_files:
89
+ - spec/lib/astar_spec.rb
90
+ - spec/spec_helper.rb
91
+ has_rdoc: