rdgc-dm 0.1.0

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,190 @@
1
+ # coding: UTF-8
2
+ require File.expand_path(File.dirname(__FILE__) + '../../../spec_helper')
3
+
4
+ include RDGC::Map
5
+
6
+ describe RDGC::Map::Room, "is Area, filled with TileType::ROOM" do
7
+
8
+ before(:all) do
9
+ @top = 8
10
+ @bottom = 20
11
+ @left = 21
12
+ @right = 40
13
+
14
+ @block = Area.create(@top, @bottom, @left, @right)
15
+ @notblock = Area.create(1, 1, 1, 1)
16
+ end
17
+
18
+ it "room_size provides random value for room's height/width" do
19
+ min = 1
20
+ max = 50
21
+ (min..max).each do |val|
22
+ size = Room.room_size(val)
23
+ if size <= 0
24
+ (val-3).should < Util::Config.min_room_size
25
+ else
26
+ size.should >= Util::Config.min_room_size
27
+ size.should <= val-3
28
+ end
29
+ end
30
+ end
31
+
32
+ describe "for create_from_block" do
33
+
34
+ it "not enough size block can't create room" do
35
+ Room.create_from_block(@notblock).should be_nil
36
+ end
37
+
38
+ it "filled with TileType::ROOM" do
39
+ each_create_room do |r|
40
+ r.each_tile do |x, y, t|
41
+ t.should == TileType::ROOM
42
+ end
43
+ end
44
+ end
45
+
46
+ it "room height/width <= block height/width - 3" do
47
+ each_create_room do |r|
48
+ r.height.should <= @block.height - 3
49
+ r.width.should <= @block.width - 3
50
+ end
51
+ end
52
+
53
+ it "room's all coordinates in block area" do
54
+ each_create_room do |r|
55
+ r.each do |x, y|
56
+ @block.has_xy?(x, y).should be_true
57
+ end
58
+ end
59
+ end
60
+
61
+ end
62
+
63
+ describe "for create_from_block with min and max value" do
64
+
65
+ it "room create accept min and max value" do
66
+ min = 5
67
+ max = 10
68
+ opt = {:min => min, :max => max}
69
+
70
+ each_create_room(opt) do |r|
71
+ r.height.should <= max
72
+ r.height.should >= min
73
+ r.width.should <= max
74
+ r.width.should >= min
75
+ end
76
+ end
77
+
78
+ it "only max" do
79
+ max = 10
80
+ min = Util::Config.min_room_size
81
+ opt = {:max => max}
82
+
83
+ each_create_room(opt) do |r|
84
+ r.height.should <= max
85
+ r.height.should >= min
86
+ r.width.should <= max
87
+ r.width.should >= min
88
+ end
89
+ end
90
+
91
+ it "only min" do
92
+ min = 5
93
+ opt = {:min => min}
94
+
95
+ each_create_room(opt) do |r|
96
+ r.height.should <= @block.height - 3
97
+ r.height.should >= min
98
+ r.width.should <= @block.width - 3
99
+ r.width.should >= min
100
+ end
101
+ end
102
+
103
+ it "min = max if min > max" do
104
+ min = 10
105
+ max = 5
106
+ opt = {:min => min, :max => max}
107
+
108
+ each_create_room(opt) do |r|
109
+ r.height.should == max
110
+ r.width.should == max
111
+ end
112
+ end
113
+
114
+ it "min value can't under MIN_ROOM_SIZE, and adjust MIN_ROOM_SIZE" do
115
+ min = 1
116
+ opt = {:min => min}
117
+
118
+ each_create_room(opt) do |r|
119
+ valid_room?(r).should be_true
120
+ end
121
+ end
122
+
123
+ it "room size adjust limit size, if min value over base size" do
124
+ min = 200
125
+ opt = {:min => min}
126
+
127
+ each_create_room(opt) do |r|
128
+ r.height.should == @block.height - 3
129
+ r.width.should == @block.width - 3
130
+ end
131
+ end
132
+
133
+ it "max value can't over base size, and adjust base size" do
134
+ max = 200
135
+ opt = {:max => max}
136
+
137
+ each_create_room(opt) do |r|
138
+ valid_room?(r).should be_true
139
+ end
140
+ end
141
+
142
+ it "room size adjust MIN_ROOM_SIZE, if max value under MIN_ROOM_SIZE" do
143
+ max = 1
144
+ opt = {:max => max}
145
+
146
+ each_create_room(opt) do |r|
147
+ r.height.should == Util::Config.min_room_size
148
+ r.width.should == Util::Config.min_room_size
149
+ end
150
+ end
151
+
152
+ end
153
+
154
+ describe "Config affect min_room_size" do
155
+
156
+ it "min_room_size change if Config change" do
157
+ Util::Config.set(:min_room_size => 5).should be_true
158
+
159
+ min = 1
160
+ max = 50
161
+ (min..max).each do |val|
162
+ size = Room.room_size(val)
163
+ if size <= 0
164
+ (val-3).should < 5
165
+ else
166
+ size.should >= 5
167
+ size.should <= val-3
168
+ end
169
+ end
170
+
171
+ Util::Config.reset!.should be_true
172
+ end
173
+
174
+ end
175
+
176
+ def each_create_room(opt = nil)
177
+ 10.times do
178
+ yield(Room.create_from_block(@block, opt))
179
+ end
180
+ end
181
+
182
+ def valid_room?(r)
183
+ return false unless r.height <= @block.height - 3
184
+ return false unless r.height >= Util::Config.min_room_size
185
+ return false unless r.width <= @block.width - 3
186
+ return false unless r.width >= Util::Config.min_room_size
187
+ true
188
+ end
189
+
190
+ end
@@ -0,0 +1,273 @@
1
+ # coding: UTF-8
2
+ require File.expand_path(File.dirname(__FILE__) + '../../../spec_helper')
3
+
4
+ include RDGC::Map
5
+
6
+ describe RDGC::Map::Block, 'has room and roads' do
7
+
8
+ before do
9
+ @block = Block.create(1, 20, 11, 30)
10
+ end
11
+
12
+ it "set/remove room" do
13
+ room = Room.create(1, 7, 15, 20)
14
+
15
+ @block.room = room
16
+ @block.has_room?.should be_true
17
+ @block.room.should equal(room)
18
+
19
+ @block.remove_room
20
+ @block.has_room?.should be_false
21
+ @block.room.should be_nil
22
+ end
23
+
24
+ it "add/remove road" do
25
+ road1 = Road.create(2, 2, 11, 14)
26
+ road2 = Road.create(1, 6, 20, 20)
27
+
28
+ @block.add_road(road1)
29
+ @block.has_road?.should be_true
30
+ @block.roads.size.should == 1
31
+ @block.roads[0].should equal(road1)
32
+
33
+ @block.add_road(road2)
34
+ @block.has_road?.should be_true
35
+ @block.roads.size.should == 2
36
+ @block.roads[0].should equal(road1)
37
+ @block.roads[1].should equal(road2)
38
+
39
+ @block.remove_roads
40
+ @block.has_road?.should be_false
41
+ @block.roads.should be_empty
42
+ end
43
+
44
+ it "#fill_room will set tile at room coordinates" do
45
+ room = Room.create(1, 7, 15, 20)
46
+
47
+ @block.room = room
48
+ @block.has_room?.should be_true
49
+ @block.room.should equal(room)
50
+
51
+ @block.fill_room
52
+
53
+ room.each_tile do |x, y, t|
54
+ @block.has_xy?(x, y).should be_true
55
+ @block.tile(x, y).should equal(TileType::ROOM)
56
+ end
57
+ end
58
+
59
+ it "#fill_roads will set tile at road coordinates" do
60
+ road1 = Road.create(2, 2, 11, 14)
61
+ road2 = Road.create(1, 6, 20, 20)
62
+
63
+ @block.add_road(road1)
64
+ @block.add_road(road2)
65
+ @block.has_road?.should be_true
66
+ @block.roads.size.should == 2
67
+
68
+ @block.fill_roads
69
+
70
+ [road1, road2].each do |road|
71
+ road.each_tile do |x, y, t|
72
+ @block.has_xy?(x, y).should be_true
73
+ @block.tile(x, y).should equal(TileType::ROAD)
74
+ end
75
+ end
76
+ end
77
+
78
+ it "#fill will fill room/road, and fill at wall other coordinates" do
79
+ room = Room.create(1, 7, 15, 20)
80
+ road = Road.create(2, 2, 11, 14)
81
+
82
+ @block.room = room
83
+ @block.add_road(road)
84
+
85
+ @block.fill
86
+
87
+ @block.each_tile do |x, y, t|
88
+ case
89
+ when room.has_xy?(x, y)
90
+ t.should equal(TileType::ROOM)
91
+ when road.has_xy?(x, y)
92
+ t.should equal(TileType::ROAD)
93
+ else
94
+ t.should equal(TileType::WALL)
95
+ end
96
+ end
97
+ end
98
+
99
+ it "#create_room create and set room from self block" do
100
+ @block.room.should be_nil
101
+
102
+ 10.times do
103
+ room = @block.create_room
104
+ room.should be_an_instance_of(Room)
105
+
106
+ room.width.should <= @block.width - 3
107
+ room.height.should <= @block.height - 3
108
+
109
+ room.each do |x, y|
110
+ @block.has_xy?(x, y).should be_true
111
+ end
112
+ end
113
+ end
114
+
115
+ it "#create_room accept option value" do
116
+ min = 5
117
+ max = 8
118
+
119
+ @block.room.should be_nil
120
+ 10.times do
121
+ room = @block.create_room(:min => min, :max => max)
122
+ room.should be_an_instance_of(Room)
123
+
124
+ room.width.should >= min
125
+ room.width.should <= max
126
+ room.height.should >= min
127
+ room.height.should <= max
128
+ end
129
+ end
130
+
131
+ it "cross_point is cross point of road, instead of room" do
132
+ @block.cross_point.should be_nil
133
+ @block.has_cross_point?.should be_false
134
+
135
+ 10.times do
136
+ x, y = @block.create_cross_point
137
+
138
+ @block.has_cross_point?.should be_true
139
+ @block.has_xy?(x, y).should be_true
140
+ x.should >= @block.left + 1
141
+ x.should <= @block.right - 2
142
+ y.should >= @block.top + 1
143
+ y.should <= @block.bottom - 2
144
+
145
+ _x, _y = @block.cross_point
146
+ _x.should == x
147
+ _y.should == y
148
+ end
149
+
150
+ @block.remove_cross_point
151
+
152
+ @block.cross_point.should be_nil
153
+ @block.has_cross_point?.should be_false
154
+ end
155
+
156
+ it "#remove_all will clear room/road/cross_point and #empty? is true" do
157
+ room = Room.create(1, 7, 15, 20)
158
+ road = Road.create(2, 2, 11, 14)
159
+
160
+ @block.room = room
161
+ @block.add_road(road)
162
+ @block.create_cross_point
163
+
164
+ @block.has_room?.should be_true
165
+ @block.has_road?.should be_true
166
+ @block.has_cross_point?.should be_true
167
+ @block.empty?.should be_false
168
+
169
+ @block.remove_all
170
+
171
+ @block.has_room?.should be_false
172
+ @block.has_road?.should be_false
173
+ @block.has_cross_point?.should be_false
174
+ @block.empty?.should be_true
175
+ end
176
+
177
+ it "#cling_to_top? / #cling_direction_to judge self and target block adjoin with top" do
178
+ # 基準(元の左上)
179
+ b = Block.create(-10, 0, 0, 10)
180
+ @block.cling_to_top?(b).should be_false
181
+
182
+ # 右端が範囲内
183
+ b = Block.create(-10, 0, 1, 11)
184
+ @block.cling_to_top?(b).should be_true
185
+ @block.cling_direction_to(b).should == :top
186
+
187
+ # 左端が範囲内
188
+ b = Block.create(-10, 0, 30, 40)
189
+ @block.cling_to_top?(b).should be_true
190
+ @block.cling_direction_to(b).should == :top
191
+
192
+ # 左端が範囲外
193
+ b = Block.create(-10, 0, 31, 41)
194
+ @block.cling_to_top?(b).should be_false
195
+
196
+ # 重なるとダメ
197
+ b = Block.create(-9, 1, 15, 25)
198
+ @block.cling_to_top?(b).should be_false
199
+ end
200
+
201
+ it "#cling_to_bottom? / #cling_direction_to judge self and target block adjoin with bottom" do
202
+ # 基準(元の左下)
203
+ b = Block.create(21, 31, 0, 10)
204
+ @block.cling_to_bottom?(b).should be_false
205
+
206
+ # 右端が範囲内
207
+ b = Block.create(21, 31, 1, 11)
208
+ @block.cling_to_bottom?(b).should be_true
209
+ @block.cling_direction_to(b).should == :bottom
210
+
211
+ # 左端が範囲内
212
+ b = Block.create(21, 31, 30, 40)
213
+ @block.cling_to_bottom?(b).should be_true
214
+ @block.cling_direction_to(b).should == :bottom
215
+
216
+ # 左端が範囲外
217
+ b = Block.create(21, 31, 31, 41)
218
+ @block.cling_to_bottom?(b).should be_false
219
+
220
+ # 重なるとダメ
221
+ b = Block.create(20, 30, 15, 25)
222
+ @block.cling_to_bottom?(b).should be_false
223
+ end
224
+
225
+ it "#cling_to_left? / #cling_direction_to judge self and target block adjoin with left" do
226
+ # 基準(元の左上)
227
+ b = Block.create(-10, 0, 0, 10)
228
+ @block.cling_to_left?(b).should be_false
229
+
230
+ # 下端が範囲内
231
+ b = Block.create(-9, 1, 0, 10)
232
+ @block.cling_to_left?(b).should be_true
233
+ @block.cling_direction_to(b).should == :left
234
+
235
+ # 上端が範囲内
236
+ b = Block.create(20, 30, 0, 10)
237
+ @block.cling_to_left?(b).should be_true
238
+ @block.cling_direction_to(b).should == :left
239
+
240
+ # 上端が範囲外
241
+ b = Block.create(21, 31, 0, 10)
242
+ @block.cling_to_left?(b).should be_false
243
+
244
+ # 重なるとダメ
245
+ b = Block.create(15, 30, 1, 11)
246
+ @block.cling_to_left?(b).should be_false
247
+ end
248
+
249
+ it "#cling_to_right? / #cling_direction_to judge self and target block adjoin with right" do
250
+ # 基準(元の右上)
251
+ b = Block.create(-10, 0, 31, 41)
252
+ @block.cling_to_right?(b).should be_false
253
+
254
+ # 下端が範囲内
255
+ b = Block.create(-9, 1, 31, 41)
256
+ @block.cling_to_right?(b).should be_true
257
+ @block.cling_direction_to(b).should == :right
258
+
259
+ # 上端が範囲内
260
+ b = Block.create(20, 30, 31, 41)
261
+ @block.cling_to_right?(b).should be_true
262
+ @block.cling_direction_to(b).should == :right
263
+
264
+ # 上端が範囲外
265
+ b = Block.create(21, 31, 31, 41)
266
+ @block.cling_to_right?(b).should be_false
267
+
268
+ # 重なるとダメ
269
+ b = Block.create(15, 30, 30, 40)
270
+ @block.cling_to_right?(b).should be_false
271
+ end
272
+
273
+ end
@@ -0,0 +1,132 @@
1
+ # coding: UTF-8
2
+ require File.expand_path(File.dirname(__FILE__) + '../../../spec_helper')
3
+
4
+ include RDGC::Map
5
+
6
+ describe RDGC::Map::Board, 'has rooms and roads from Blocks' do
7
+
8
+ it "#set_coordinates will set self coordinates from blocks coordinates" do
9
+ b = Board.create(1, 1, 1, 1)
10
+ b.should be_an_instance_of(Board)
11
+ b.width.should == 1
12
+ b.height.should == 1
13
+
14
+ b.blocks << Block.create(0, 20, 10, 30)
15
+ b.blocks << Block.create(10, 30, 20, 40)
16
+
17
+ b.set_coordinates
18
+
19
+ b.top.should == 0
20
+ b.bottom.should == 30
21
+ b.left.should == 10
22
+ b.right.should == 40
23
+ end
24
+
25
+ it "#fill will fill room/road, and fill at wall other coordinates" do
26
+ b = Board.create(0, 20, 0, 20)
27
+
28
+ room = Room.create(5, 15, 5, 15)
29
+ road1 = Road.create(0, 4, 10, 10)
30
+ road2 = Road.create(16, 20, 12, 12)
31
+
32
+ b.rooms << room
33
+ b.roads << road1
34
+ b.roads << road2
35
+
36
+ b.fill
37
+
38
+ b.each_tile do |x, y, t|
39
+ case t
40
+ when TileType::WALL
41
+ room.has_xy?(x, y).should be_false
42
+ road1.has_xy?(x, y).should be_false
43
+ road2.has_xy?(x, y).should be_false
44
+
45
+ b.movable?(x, y).should be_false
46
+ b.room?(x, y).should be_false
47
+ b.road?(x, y).should be_false
48
+ when TileType::ROOM
49
+ room.has_xy?(x, y).should be_true
50
+ road1.has_xy?(x, y).should be_false
51
+ road2.has_xy?(x, y).should be_false
52
+
53
+ b.movable?(x, y).should be_true
54
+ b.room?(x, y).should be_true
55
+ b.road?(x, y).should be_false
56
+ when TileType::ROAD
57
+ room.has_xy?(x, y).should be_false
58
+ [road1, road2].map{|r| r.has_xy?(x, y)}.any?.should be_true
59
+
60
+ b.movable?(x, y).should be_true
61
+ b.room?(x, y).should be_false
62
+ b.road?(x, y).should be_true
63
+ end
64
+ end
65
+ end
66
+
67
+ it "#init will initialize self from blocks, and create_from_blocks will provides init Board" do
68
+ block1 = Block.create(0, 20, 0, 20)
69
+ room1 = Room.create(5, 15, 5, 15)
70
+ road1 = Road.create(0, 4, 10, 10)
71
+ road2 = Road.create(16, 20, 12, 12)
72
+ block1.room = room1
73
+ block1.add_road(road1)
74
+ block1.add_road(road2)
75
+
76
+ block2 = Block.create(21, 40, 0, 20)
77
+ room2 = Room.create(25, 35, 25, 35)
78
+ road3 = Road.create(21, 24, 12, 12)
79
+ block2.room = room2
80
+ block2.add_road(road3)
81
+
82
+ b1 = Board.create(1, 1, 1, 1)
83
+ b1.init([block1, block2])
84
+
85
+ b2 = Board.create_from_blocks([block1, block2])
86
+ b2.should be_an_instance_of(Board)
87
+
88
+ [b1, b2].each do |b|
89
+ b.blocks.should be_include(block1)
90
+ b.blocks.should be_include(block2)
91
+ b.rooms.should be_include(room1)
92
+ b.rooms.should be_include(room2)
93
+ b.roads.should be_include(road1)
94
+ b.roads.should be_include(road2)
95
+ b.roads.should be_include(road3)
96
+
97
+ # set_coordinates
98
+ b.top.should == 0
99
+ b.bottom.should == 40
100
+ b.left.should == 0
101
+ b.right.should == 20
102
+
103
+ # fill
104
+ b.each_tile do |x, y, t|
105
+ case t
106
+ when TileType::WALL
107
+ [room1, room2].map{|r| r.has_xy?(x, y)}.any?.should be_false
108
+ [road1, road2, road3].map{|r| r.has_xy?(x, y)}.any?.should be_false
109
+
110
+ b.movable?(x, y).should be_false
111
+ b.room?(x, y).should be_false
112
+ b.road?(x, y).should be_false
113
+ when TileType::ROOM
114
+ [room1, room2].map{|r| r.has_xy?(x, y)}.any?.should be_true
115
+ [road1, road2, road3].map{|r| r.has_xy?(x, y)}.any?.should be_false
116
+
117
+ b.movable?(x, y).should be_true
118
+ b.room?(x, y).should be_true
119
+ b.road?(x, y).should be_false
120
+ when TileType::ROAD
121
+ [room1, room2].map{|r| r.has_xy?(x, y)}.any?.should be_false
122
+ [road1, road2, road3].map{|r| r.has_xy?(x, y)}.any?.should be_true
123
+
124
+ b.movable?(x, y).should be_true
125
+ b.room?(x, y).should be_false
126
+ b.road?(x, y).should be_true
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ end
@@ -0,0 +1,44 @@
1
+ # coding: UTF-8
2
+ require File.expand_path(File.dirname(__FILE__) + '../../../spec_helper')
3
+
4
+ include RDGC::Util
5
+
6
+ describe RDGC::Util::Config do
7
+
8
+ it "default value" do
9
+ Util::Config.min_room_size.should == 4
10
+ Util::Config.min_block_size.should == 7
11
+ Util::Config.act_max_count.should == 200
12
+ end
13
+
14
+ it "value change only once, and #reset! will force change default value" do
15
+ val1 = {
16
+ :min_room_size => 5,
17
+ :act_max_count => 100
18
+ }
19
+
20
+ Util::Config.set(val1).should be_true
21
+ Util::Config.min_room_size.should == 5
22
+ Util::Config.act_max_count.should == 100
23
+
24
+ val2 = {
25
+ :min_room_size => 10,
26
+ :act_max_count => 80
27
+ }
28
+
29
+ Util::Config.set(val2).should be_false
30
+ Util::Config.min_room_size.should == 5
31
+ Util::Config.act_max_count.should == 100
32
+
33
+ Util::Config.reset!.should be_true
34
+ Util::Config.min_room_size.should == 4
35
+ Util::Config.act_max_count.should == 200
36
+
37
+ Util::Config.set(val2).should be_true
38
+ Util::Config.min_room_size.should == 10
39
+ Util::Config.act_max_count.should == 80
40
+
41
+ Util::Config.reset!.should be_true
42
+ end
43
+
44
+ end