polaris 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ here = File.dirname(__FILE__)
2
+ polaris_root = File.expand_path(File.join(here, '..', 'lib'))
3
+
4
+ $: << polaris_root
@@ -0,0 +1,12 @@
1
+ require File.join(File.dirname(__FILE__),'helper')
2
+ require 'line_of_site'
3
+ require 'two_d_grid_map'
4
+
5
+ describe 'A new LineOfSite' do
6
+ before do
7
+ @map = TwoDGridMap.new 10, 20
8
+ @los = LineOfSite.new @map
9
+ end
10
+
11
+ it 'should be sane'
12
+ end
@@ -0,0 +1,193 @@
1
+ require File.join(File.dirname(__FILE__),'helper')
2
+ require 'polaris'
3
+ require 'two_d_grid_map'
4
+
5
+ describe 'A new polaris' do
6
+ before do
7
+ @map = TwoDGridMap.new 10, 20
8
+ @pather = Polaris.new @map
9
+ end
10
+
11
+ it 'should return an empty path if destination is not valid' do
12
+ from = TwoDGridLocation.new @map.w-1, @map.h-1
13
+ to = TwoDGridLocation.new @map.w, @map.h
14
+ @pather.guide(from,to).should be_nil
15
+
16
+ to = TwoDGridLocation.new(-1, -1)
17
+ @pather.guide(from,to).should be_nil
18
+ end
19
+
20
+ it 'should return an empty path if start is not valid' do
21
+ from = TwoDGridLocation.new @map.w, @map.h
22
+ to = TwoDGridLocation.new @map.w-1, @map.h-1
23
+ @pather.guide(from,to).should be_nil
24
+
25
+ from = TwoDGridLocation.new -1, -1
26
+ @pather.guide(from,to).should be_nil
27
+ end
28
+
29
+ it 'should return the path of "to" for accessible neighbor' do
30
+ from = TwoDGridLocation.new 0, 0
31
+ to = TwoDGridLocation.new 1, 0
32
+
33
+ path = @pather.guide(from,to)
34
+ path.should_not be_nil
35
+ path.size.should equal(1)
36
+
37
+ path.first.cost_to.should equal(TwoDGridMap::TRAVEL_COST_STRAIGHT)
38
+ path.first.dist_from.should equal(0)
39
+ path.first.location.x.should equal(to.x)
40
+ path.first.location.y.should equal(to.y)
41
+ end
42
+
43
+ it 'should return the right horizontal path of length 2' do
44
+ from = TwoDGridLocation.new 0, 0
45
+ to = TwoDGridLocation.new 2, 0
46
+
47
+ path = @pather.guide(from,to)
48
+
49
+ path.should_not be_nil
50
+ path.size.should equal(2)
51
+
52
+ path.first.location.x.should equal(1)
53
+ path.first.location.y.should equal(0)
54
+ path.last.location.should == to
55
+ end
56
+
57
+ it 'should return the left horizontal path of length 2' do
58
+ from = TwoDGridLocation.new 2, 0
59
+ to = TwoDGridLocation.new 0, 0
60
+
61
+ path = @pather.guide(from,to)
62
+
63
+ path.should_not be_nil
64
+ path.size.should equal(2)
65
+
66
+ path.first.location.x.should equal(1)
67
+ path.first.location.y.should equal(0)
68
+ path.last.location.should == to
69
+ end
70
+
71
+
72
+ it 'should return the left up diag path of length 2' do
73
+ from = TwoDGridLocation.new 2, 2
74
+ to = TwoDGridLocation.new 0, 0
75
+
76
+ path = @pather.guide(from,to)
77
+
78
+ path.should_not be_nil
79
+ path.size.should equal(2)
80
+
81
+ path.first.location.x.should equal(1)
82
+ path.first.location.y.should equal(1)
83
+ path.last.location.should == to
84
+ end
85
+
86
+ it 'should return the right up diag path of length 2' do
87
+ from = TwoDGridLocation.new 2, 2
88
+ to = TwoDGridLocation.new 4, 0
89
+
90
+ path = @pather.guide(from,to)
91
+
92
+ path.should_not be_nil
93
+ path.size.should equal(2)
94
+
95
+ path.first.location.x.should equal(3)
96
+ path.first.location.y.should equal(1)
97
+ path.last.location.should == to
98
+ end
99
+
100
+ it 'should return the right down diag path of length 2' do
101
+ from = TwoDGridLocation.new 2, 2
102
+ to = TwoDGridLocation.new 4, 4
103
+
104
+ path = @pather.guide(from,to)
105
+
106
+ path.should_not be_nil
107
+ path.size.should equal(2)
108
+
109
+ path.first.location.x.should equal(3)
110
+ path.first.location.y.should equal(3)
111
+ path.last.location.should == to
112
+ end
113
+
114
+ it 'should return the left down diag path of length 2' do
115
+ from = TwoDGridLocation.new 2, 2
116
+ to = TwoDGridLocation.new 0, 4
117
+
118
+ path = @pather.guide(from,to)
119
+
120
+ path.should_not be_nil
121
+ path.size.should equal(2)
122
+
123
+ path.first.location.x.should equal(1)
124
+ path.first.location.y.should equal(3)
125
+ path.last.location.should == to
126
+ end
127
+
128
+ it 'should return go around an obstacle' do
129
+ from = TwoDGridLocation.new 0, 0
130
+ to = TwoDGridLocation.new 2, 0
131
+
132
+ in_the_way = TwoDGridLocation.new 1, 0
133
+ @map.place in_the_way, "ME"
134
+
135
+ path = @pather.guide(from,to)
136
+
137
+ path.should_not be_nil
138
+ path.size.should equal(2)
139
+
140
+ path.first.location.x.should equal(1)
141
+ path.first.location.y.should equal(1)
142
+ path.last.location.should == to
143
+ end
144
+
145
+ it 'should return shortest path of horizontal and diag length 5' do
146
+ from = TwoDGridLocation.new 0, 0
147
+ to = TwoDGridLocation.new 5, 4
148
+
149
+ path = @pather.guide(from,to)
150
+
151
+ path.should_not be_nil
152
+ path.size.should equal(5)
153
+
154
+ # make sure that all elements of the path are neighbors
155
+ prev_el = PathElement.new from, nil
156
+ path.each do |path_el|
157
+ @map.neighbors(prev_el.location).include?(path_el.location).should be_true
158
+ prev_el = path_el
159
+ end
160
+
161
+ path.last.location.should == to
162
+ end
163
+
164
+ it 'should return nil when the shortest path is longer than the max step passed in' do
165
+ from = TwoDGridLocation.new 0, 0
166
+ to = TwoDGridLocation.new 5, 4
167
+
168
+ path = @pather.guide(from,to,nil,4)
169
+ path.should == nil
170
+ end
171
+
172
+ it 'should return nil when the path does not exist for unit type' do
173
+ from = TwoDGridLocation.new 0, 0
174
+ to = TwoDGridLocation.new 2, 0
175
+
176
+ path = @pather.guide(from,to,:blocked)
177
+ path.should == nil
178
+ end
179
+
180
+ it 'should return nil when the path does not exist' do
181
+ from = TwoDGridLocation.new 0, 0
182
+ to = TwoDGridLocation.new 2, 0
183
+
184
+ # put up a wall
185
+ @map.h.times do |i|
186
+ @map.place TwoDGridLocation.new(1, i), "ME"
187
+ end
188
+
189
+ path = @pather.guide(from,to)
190
+ path.should == nil
191
+ @pather.nodes_considered.should == @map.h
192
+ end
193
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: polaris
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Shawn Anderson
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-02-27 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: jeweler
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :development
43
+ version_requirements: *id002
44
+ - !ruby/object:Gem::Dependency
45
+ name: algorithms
46
+ prerelease: false
47
+ requirement: &id003 !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ type: :runtime
55
+ version_requirements: *id003
56
+ description: A* pathfinding in Ruby, using C datastructures to speed things up.
57
+ email: shawn42@gmail.com
58
+ executables: []
59
+
60
+ extensions: []
61
+
62
+ extra_rdoc_files: []
63
+
64
+ files: []
65
+
66
+ has_rdoc: true
67
+ homepage: http://github.com/shawn42/polaris
68
+ licenses: []
69
+
70
+ post_install_message:
71
+ rdoc_options:
72
+ - --charset=UTF-8
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ requirements: []
90
+
91
+ rubyforge_project: polaris
92
+ rubygems_version: 1.3.6
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: A* pathfinding in ruby.
96
+ test_files:
97
+ - spec/helper.rb
98
+ - spec/line_of_site_spec.rb
99
+ - spec/polaris_spec.rb