zyps 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,310 @@
1
+ # Copyright 2007 Jay McGavren, jay@mcgavren.com.
2
+ #
3
+ # This file is part of Zyps.
4
+ #
5
+ # Zyps is free software; you can redistribute it and/or modify
6
+ # it under the terms of the GNU Lesser General Public License as published by
7
+ # the Free Software Foundation; either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+
19
+ require 'zyps'
20
+ require 'test/unit'
21
+
22
+
23
+ class TestCreature < Test::Unit::TestCase
24
+
25
+
26
+ def test_default_initialization
27
+ creature = Creature.new
28
+ assert_equal(0, creature.location.x)
29
+ assert_equal(0, creature.location.y)
30
+ assert_equal(1, creature.color.red)
31
+ assert_equal(1, creature.color.green)
32
+ assert_equal(1, creature.color.blue)
33
+ assert_equal(0, creature.vector.speed)
34
+ assert_equal(0, creature.vector.pitch)
35
+ assert_equal(nil, creature.name)
36
+ assert_in_delta(0, creature.age, 0.1)
37
+ assert_equal([], creature.tags)
38
+ assert_equal([], creature.behaviors)
39
+ end
40
+
41
+
42
+ def test_explicit_initialization
43
+ behavior = Behavior.new
44
+ creature = Creature.new(
45
+ "name",
46
+ Location.new(10, -3),
47
+ Color.new(0.5, 0.6, 0.7),
48
+ Vector.new(1.5, 225),
49
+ 2.001, #Age.
50
+ ["predator", "blue team"],
51
+ [behavior]
52
+ )
53
+ assert_equal("name", creature.name)
54
+ assert_equal(10, creature.location.x)
55
+ assert_equal(-3, creature.location.y)
56
+ assert_equal(0.5, creature.color.red)
57
+ assert_equal(0.6, creature.color.green)
58
+ assert_equal(0.7, creature.color.blue)
59
+ assert_equal(1.5, creature.vector.speed)
60
+ assert_equal(225, creature.vector.pitch)
61
+ assert_in_delta(2.001, creature.age, 0.01)
62
+ assert(creature.tags.include?("predator"))
63
+ assert(creature.tags.include?("blue team"))
64
+ assert(creature.behaviors.include?(behavior))
65
+ end
66
+
67
+
68
+ end
69
+
70
+
71
+
72
+ class TestEnvironment < Test::Unit::TestCase
73
+
74
+ def setup
75
+
76
+ #Interactions will be logged here.
77
+ @interactions = []
78
+
79
+ #Create creatures.
80
+ creature1 = Creature.new('1')
81
+ creature2 = Creature.new('2')
82
+ creature_behavior = Behavior.new
83
+ creature_behavior.actions << lambda {|creature, target| @interactions << "#{creature.name} targeting #{target.name}"}
84
+ creature1.behaviors << creature_behavior
85
+ creature2.behaviors << creature_behavior
86
+
87
+ #Create an environment and add the creatures.
88
+ @environment = Environment.new([creature1, creature2])
89
+
90
+ end
91
+
92
+
93
+ def test_interactions
94
+
95
+ #Have environment elements interact.
96
+ @environment.interact
97
+
98
+ #Look for expected interactions (each should only occur once).
99
+ assert(@interactions.find_all{|i| i == "2 targeting 1"}.length == 1)
100
+ assert(@interactions.find_all{|i| i == "1 targeting 2"}.length == 1)
101
+ #TODO: Ensure creatures don't target selves.
102
+
103
+ end
104
+
105
+
106
+ def test_environmental_factors
107
+
108
+ #Create an environmental factor.
109
+ behavior = Behavior.new
110
+ behavior.actions << lambda {|factor, target| @interactions << "Environment targeting #{target.name}"}
111
+ @environment.environmental_factors << EnvironmentalFactor.new([behavior])
112
+
113
+ #Have environment elements interact.
114
+ @environment.interact
115
+
116
+ #Look for expected interactions (each should only occur once).
117
+ assert(@interactions.find_all{|i| i == "Environment targeting 1"}.length == 1)
118
+ assert(@interactions.find_all{|i| i == "Environment targeting 2"}.length == 1)
119
+
120
+ end
121
+
122
+
123
+ def test_conditions
124
+
125
+ #Change behaviors to only occur if the target's name is '2'.
126
+ @environment.objects.each do |creature|
127
+ behavior = Behavior.new
128
+ behavior.conditions << lambda do |creature, target|
129
+ return true if creature.name == '1' and target.name == '2'
130
+ end
131
+ behavior.actions << lambda do |creature, target|
132
+ @interactions << "#{creature.name} is targeting #{target.name}"
133
+ end
134
+ creature.behaviors << behavior
135
+ end
136
+
137
+ #Have environment elements interact.
138
+ @environment.interact
139
+
140
+ #Creature '1' should not have been acted on.
141
+ assert(@interactions.find_all{|i| i == "2 is targeting 1"}.length == 0)
142
+ #Creature '2' *should* have been acted on.
143
+ assert(@interactions.find_all{|i| i == "1 is targeting 2"}.length == 1)
144
+
145
+ end
146
+
147
+
148
+ end
149
+
150
+
151
+
152
+ class TestVector < Test::Unit::TestCase
153
+
154
+
155
+ def test_initialize
156
+
157
+ vector = Vector.new
158
+ assert_equal(0, vector.speed)
159
+ assert_equal(0, vector.pitch)
160
+ assert_equal(0, vector.x)
161
+ assert_equal(0, vector.y)
162
+
163
+ end
164
+
165
+
166
+ def test_angles
167
+
168
+ vector = Vector.new(4, 150)
169
+ assert_in_delta(-3.464, vector.x, 0.001)
170
+ assert_in_delta(2, vector.y, 0.001)
171
+
172
+ vector = Vector.new(5, 53.13)
173
+ assert_in_delta(3, vector.x, 0.001)
174
+ assert_in_delta(4, vector.y, 0.001)
175
+
176
+ vector = Vector.new(5, 233.13)
177
+ assert_in_delta(-3, vector.x, 0.001)
178
+ assert_in_delta(-4, vector.y, 0.001)
179
+
180
+ vector = Vector.new(5, 306.87)
181
+ assert_in_delta(3, vector.x, 0.001)
182
+ assert_in_delta(-4, vector.y, 0.001)
183
+
184
+ #Angles over 360 should 'wrap around' to 0.
185
+ vector = Vector.new(5, 413.13) #360 + 53.13
186
+ assert_in_delta(3, vector.x, 0.001)
187
+ assert_in_delta(4, vector.y, 0.001)
188
+
189
+ #Negative angle should be converted to positive equivalent.
190
+ vector = Vector.new(5, -53.13) #360 - 53.13 = 306.87
191
+ assert_in_delta(3, vector.x, 0.001)
192
+ assert_in_delta(-4, vector.y, 0.001)
193
+
194
+ end
195
+
196
+
197
+ def test_components
198
+
199
+ vector = Vector.new(1.4142, 45)
200
+ assert_in_delta(1, vector.x, 0.001)
201
+ assert_in_delta(1, vector.y, 0.001)
202
+
203
+ vector = Vector.new(1.4142, 135)
204
+ assert_in_delta(-1, vector.x, 0.001)
205
+ assert_in_delta(1, vector.y, 0.001)
206
+
207
+ vector = Vector.new(1.4142, 225)
208
+ assert_in_delta(-1, vector.x, 0.001)
209
+ assert_in_delta(-1, vector.y, 0.001)
210
+
211
+ vector = Vector.new(1.4142, 315)
212
+ assert_in_delta(1, vector.x, 0.001)
213
+ assert_in_delta(-1, vector.y, 0.001)
214
+
215
+ end
216
+
217
+
218
+ def test_addition
219
+
220
+ vector = Vector.new(1, 45) + Vector.new(1, 45) #Same angle.
221
+ #Speed should be sum of added vectors' speeds.
222
+ assert_in_delta(2, vector.speed, 0.001)
223
+ #Angle should remain the same.
224
+ assert_in_delta(45, vector.pitch, 0.001)
225
+
226
+ #Vectors of opposite angles should cancel out.
227
+ vector = Vector.new(2, 0) + Vector.new(1, 180)
228
+ assert_in_delta(1, vector.speed, 0.001)
229
+ assert_in_delta(0, vector.pitch, 0.001)
230
+ vector = Vector.new(2, 45) + Vector.new(1, 225)
231
+ assert_in_delta(1, vector.speed, 0.001)
232
+ assert_in_delta(45, vector.pitch, 0.001)
233
+ vector = Vector.new(2, 135) + Vector.new(1, 315)
234
+ assert_in_delta(1, vector.speed, 0.001)
235
+ assert_in_delta(135, vector.pitch, 0.001)
236
+ vector = Vector.new(2, 225) + Vector.new(1, 45)
237
+ assert_in_delta(1, vector.speed, 0.001)
238
+ assert_in_delta(225, vector.pitch, 0.001)
239
+ vector = Vector.new(2, 315) + Vector.new(1, 135)
240
+ assert_in_delta(1, vector.speed, 0.001)
241
+ assert_in_delta(315, vector.pitch, 0.001)
242
+
243
+ end
244
+
245
+
246
+ end
247
+
248
+
249
+
250
+ class TestClock < Test::Unit::TestCase
251
+
252
+
253
+ def test_elapsed_time
254
+
255
+ #Create a clock, wait a moment, then see how much time has elapsed since its creation.
256
+ clock = Clock.new
257
+ sleep 0.1
258
+ assert_in_delta(0.1, clock.elapsed_time, 0.02)
259
+
260
+ end
261
+
262
+
263
+ end
264
+
265
+
266
+
267
+ class TestUtility < Test::Unit::TestCase
268
+
269
+
270
+ def test_to_radians
271
+
272
+ assert_in_delta(0, Utility.to_radians(0), 0.01)
273
+ assert_in_delta(Math::PI, Utility.to_radians(180), 0.01)
274
+ assert_in_delta(Math::PI * 2, Utility.to_radians(359), 0.1)
275
+
276
+ end
277
+
278
+
279
+ def test_to_degrees
280
+
281
+ assert_in_delta(0, Utility.to_degrees(0), 0.01)
282
+ assert_in_delta(180, Utility.to_degrees(Math::PI), 0.01)
283
+ assert_in_delta(359, Utility.to_degrees(Math::PI * 2 - 0.0001), 1)
284
+
285
+ end
286
+
287
+
288
+ def test_find_angle
289
+ origin = Location.new(0, 0)
290
+ assert_in_delta(0, Utility.find_angle(origin, Location.new(1,0)), 0.001)
291
+ assert_in_delta(90, Utility.find_angle(origin, Location.new(0,1)), 0.001)
292
+ assert_in_delta(45, Utility.find_angle(origin, Location.new(1,1)), 0.001)
293
+ assert_in_delta(135, Utility.find_angle(origin, Location.new(-1,1)), 0.001)
294
+ assert_in_delta(225, Utility.find_angle(origin, Location.new(-1,-1)), 0.001)
295
+ assert_in_delta(315, Utility.find_angle(origin, Location.new(1,-1)), 0.001)
296
+ end
297
+
298
+
299
+ def test_find_distance
300
+ origin = Location.new(0, 0)
301
+ assert_in_delta(1, Utility.find_distance(origin, Location.new(1,0)), 0.001)
302
+ assert_in_delta(1, Utility.find_distance(origin, Location.new(0,1)), 0.001)
303
+ assert_in_delta(1.4142, Utility.find_distance(origin, Location.new(1,1)), 0.001)
304
+ assert_in_delta(1.4142, Utility.find_distance(origin, Location.new(-1,1)), 0.001)
305
+ assert_in_delta(1.4142, Utility.find_distance(origin, Location.new(-1,-1)), 0.001)
306
+ assert_in_delta(1.4142, Utility.find_distance(origin, Location.new(1,-1)), 0.001)
307
+ end
308
+
309
+
310
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: zyps
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.1
7
+ date: 2007-07-25 00:00:00 -07:00
8
+ summary: A simulation/game with autonomous creatures
9
+ require_paths:
10
+ - lib
11
+ email: jay@mcgavren.com
12
+ homepage: http://jay.mcgavren.com/zyps/
13
+ rubyforge_project: zyps
14
+ description:
15
+ autorequire: zyps
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Jay McGavren
31
+ files:
32
+ - COPYING.LESSER.txt
33
+ - COPYING.txt
34
+ - README.txt
35
+ - bin/zyps
36
+ - bin/zyps_demo
37
+ - lib/zyps
38
+ - lib/zyps/views
39
+ - lib/zyps/views/trails.rb
40
+ - lib/zyps.rb
41
+ - test/test_zyps.rb
42
+ test_files:
43
+ - test/test_zyps.rb
44
+ rdoc_options:
45
+ - --title
46
+ - Zyps - A simulation/game with autonomous creatures
47
+ - --main
48
+ - README.txt
49
+ extra_rdoc_files:
50
+ - README.txt
51
+ - COPYING.LESSER.txt
52
+ - COPYING.txt
53
+ executables:
54
+ - zyps
55
+ - zyps_demo
56
+ extensions: []
57
+
58
+ requirements:
59
+ - - Ruby-GNOME2
60
+ dependencies: []
61
+