This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathtest_world_map.py
309 lines (258 loc) · 11.5 KB
/
test_world_map.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
from __future__ import absolute_import
from string import ascii_uppercase
from unittest import TestCase
import math
from simulation.game_logic import SpawnLocationFinder
from simulation.interactables.interactable import _Interactable
from simulation.interactables.pickups import ALL_PICKUPS, HealthPickup
from simulation.interactables.score_location import ScoreLocation
from simulation.location import Location
from simulation.world_map import WorldMap, WorldMapStaticSpawnDecorator
from .dummy_avatar import DummyAvatar
from .maps import AvatarMap, MockCell, MockPickup
WORLD_MAP_SETTINGS = {
"TARGET_NUM_CELLS_PER_AVATAR": 0,
"TARGET_NUM_PICKUPS_PER_AVATAR": 0,
"TARGET_NUM_SCORE_LOCATIONS_PER_AVATAR": 0,
"SCORE_DESPAWN_CHANCE": 0,
"PICKUP_SPAWN_CHANCE": 0,
}
def int_ceil(num):
return int(math.ceil(num))
def int_floor(num):
return int(math.floor(num))
def generate_grid(columns=2, rows=2):
alphabet = iter(ascii_uppercase)
grid = {
Location(x, y): MockCell(Location(x, y), name=next(alphabet))
for x in range(-int_ceil(columns / 2.0) + 1, int_floor(columns / 2.0) + 1)
for y in range(-int_ceil(rows / 2.0) + 1, int_floor(rows / 2.0) + 1)
}
return grid
class TestWorldMap(TestCase):
def setUp(self):
self.settings = WORLD_MAP_SETTINGS
def _grid_from_list(self, in_list):
out = {}
for x, column in enumerate(in_list):
for y, cell in enumerate(column):
out[Location(x, y)] = cell
return out
def assertGridSize(self, world_map, expected_columns, expected_rows=None):
if expected_rows is None:
expected_rows = expected_columns
assert world_map.num_rows == expected_rows
assert world_map.num_cols == expected_columns
assert world_map.num_cells == expected_rows * expected_columns
assert len(list(world_map.all_cells())) == expected_rows * expected_columns
def test_get_all_cells(self):
world_map = WorldMap(generate_grid(), self.settings)
cell_list = list(world_map.all_cells())
assert len(cell_list) == 4
self.assertTrue(isinstance(cell_list[0], MockCell))
def test_get_all_interactables(self):
interactable_cell = MockCell()
interactable_cell.interactable = ScoreLocation(interactable_cell)
grid = self._grid_from_list(
[[interactable_cell, MockCell()], [MockCell(), MockCell()]]
)
world_map = WorldMap(grid, self.settings)
interactable_list = list(world_map.interactable_cells())
assert len(interactable_list) == 1
self.assertTrue(isinstance(interactable_list[0].interactable, _Interactable))
def test_get_all_score_locations(self):
score_cell = MockCell()
score_cell.interactable = ScoreLocation(score_cell)
grid = self._grid_from_list(
[[score_cell, MockCell()], [MockCell(), MockCell()]]
)
world_map = WorldMap(grid, self.settings)
score_list = list(world_map.score_cells())
assert len(score_list) == 1
self.assertTrue(isinstance(score_list[0].interactable, ScoreLocation))
def test_get_all_pickup_locations(self):
pickup_cell = MockCell()
pickup_cell.interactable = HealthPickup(pickup_cell)
grid = self._grid_from_list(
[[pickup_cell, MockCell()], [MockCell(), MockCell()]]
)
world_map = WorldMap(grid, self.settings)
pickup_list = list(world_map.pickup_cells())
assert len(pickup_list) == 1
self.assertTrue(isinstance(pickup_list[0].interactable, ALL_PICKUPS))
def test_grid_size(self):
world_map = WorldMap(generate_grid(1, 3), self.settings)
self.assertGridSize(world_map, 1, 3)
def test_generated_map(self):
world_map = WorldMap.generate_empty_map(2, 5, {})
self.assertGridSize(world_map, 5, 2)
def test_all_cells(self):
world_map = WorldMap(generate_grid(), self.settings)
cell_names = [c.name for c in world_map.all_cells()]
self.assertIn("A", cell_names)
self.assertIn("B", cell_names)
self.assertIn("C", cell_names)
self.assertIn("D", cell_names)
assert len(cell_names) == 4
def test_score_cells(self):
score_cell1 = MockCell()
score_cell1.interactable = ScoreLocation(score_cell1)
score_cell2 = MockCell()
score_cell2.interactable = ScoreLocation(score_cell2)
no_score_cell = MockCell()
grid = self._grid_from_list(
[[score_cell1, no_score_cell], [no_score_cell, score_cell2]]
)
world_map = WorldMap(grid, self.settings)
cells = list(world_map.score_cells())
self.assertIn(score_cell1, cells)
self.assertIn(score_cell2, cells)
assert len(cells) == 2, "Non-scoring cells present"
def test_potential_spawns(self):
spawnable1 = MockCell()
spawnable2 = MockCell()
score_cell = MockCell()
score_cell.interactable = ScoreLocation(score_cell)
unhabitable = MockCell(habitable=False)
filled = MockCell(avatar="avatar")
grid = self._grid_from_list(
[[spawnable1, score_cell, unhabitable], [unhabitable, spawnable2, filled]]
)
world_map = WorldMap(grid, self.settings)
spawn_location_finder = SpawnLocationFinder(world_map)
cells = list(spawn_location_finder.potential_spawn_locations())
self.assertIn(spawnable1, cells)
self.assertIn(spawnable2, cells)
self.assertNotIn(score_cell, cells, "Score cells should not be spawns")
self.assertNotIn(unhabitable, cells, "Unhabitable cells should not be spawns")
self.assertNotIn(filled, cells, "Cells with avatars should not be spawns")
assert len(cells) == 2
def test_interactable_cells(self):
pickup_cell1 = MockCell(interactable=MockPickup())
pickup_cell2 = MockCell(interactable=MockPickup())
no_pickup_cell = MockCell()
grid = self._grid_from_list(
[[pickup_cell1, no_pickup_cell], [no_pickup_cell, pickup_cell2]]
)
world_map = WorldMap(grid, self.settings)
cells = list(world_map.interactable_cells())
self.assertIn(pickup_cell1, cells)
self.assertIn(pickup_cell2, cells)
assert len(cells) == 2, "Non-pickup cells present"
def test_location_on_map(self):
world_map = WorldMap(generate_grid(), self.settings)
for x in (0, 1):
for y in (0, 1):
self.assertTrue(world_map.is_on_map(Location(x, y)))
self.assertFalse(world_map.is_on_map(Location(2, 2)))
self.assertFalse(world_map.is_on_map(Location(-1, 1)))
def test_x_off_map(self):
world_map = WorldMap(generate_grid(), self.settings)
for y in (0, 1):
self.assertFalse(world_map.is_on_map(Location(-1, y)))
self.assertFalse(world_map.is_on_map(Location(2, y)))
def test_y_off_map(self):
world_map = WorldMap(generate_grid(), self.settings)
for x in (0, 1):
self.assertFalse(world_map.is_on_map(Location(x, -1)))
self.assertFalse(world_map.is_on_map(Location(x, 2)))
def test_get_valid_cell(self):
world_map = WorldMap(generate_grid(), self.settings)
for x in (0, 1):
for y in (0, 1):
location = Location(x, y)
assert world_map.get_cell(location).location == location
def test_get_x_off_map(self):
world_map = WorldMap(generate_grid(), self.settings)
for y in (0, 1):
with self.assertRaises(ValueError):
world_map.get_cell(Location(-1, y))
with self.assertRaises(ValueError):
world_map.get_cell(Location(2, y))
def test_get_y_off_map(self):
world_map = WorldMap(generate_grid(), self.settings)
for x in (0, 1):
with self.assertRaises(ValueError):
world_map.get_cell(Location(x, -1))
with self.assertRaises(ValueError):
world_map.get_cell(Location(x, 2))
def test_random_spawn_location_successful(self):
cell = MockCell()
world_map = WorldMap({Location(0, 0): cell}, self.settings)
assert world_map.get_random_spawn_location() == cell.location
def test_random_spawn_location_with_no_candidates(self):
grid = generate_grid(1, 1)
world_map = WorldMap(grid, self.settings)
grid[Location(0, 0)].avatar = True
with self.assertRaises(IndexError):
world_map.get_random_spawn_location()
def test_can_move_to(self):
world_map = WorldMap(generate_grid(), self.settings)
target = Location(1, 1)
self.assertTrue(world_map.can_move_to(target))
def test_cannot_move_to_cell_off_grid(self):
world_map = WorldMap(generate_grid(), self.settings)
target = Location(4, 1)
self.assertFalse(world_map.can_move_to(target))
def test_cannot_move_to_uninhabitable_cell(self):
target = Location(0, 0)
cell = MockCell(target, habitable=False)
world_map = WorldMap({target: cell}, self.settings)
self.assertFalse(world_map.can_move_to(target))
def test_cannot_move_to_inhabited_cell(self):
target = Location(0, 0)
cell = MockCell(target, avatar=DummyAvatar(target, 0))
world_map = WorldMap({target: cell}, self.settings)
target = Location(0, 0)
self.assertFalse(world_map.can_move_to(target))
def test_empty_grid(self):
world_map = WorldMap({}, self.settings)
self.assertFalse(world_map.is_on_map(Location(0, 0)))
def test_iter(self):
grid = [
[
MockCell(Location(-1, -1), name="A"),
MockCell(Location(-1, 0), name="B"),
MockCell(Location(-1, 1), name="C"),
],
[
MockCell(Location(0, -1), name="D"),
MockCell(Location(0, 0), name="E"),
MockCell(Location(0, 1), name="F"),
],
[
MockCell(Location(1, -1), name="E"),
MockCell(Location(1, 0), name="G"),
MockCell(Location(1, 1), name="H"),
],
]
world_map = WorldMap(self._grid_from_list(grid), self.settings)
assert [list(column) for column in world_map] == grid
def test_attackable_avatar_returns_none(self):
world_map = WorldMap(generate_grid(), self.settings)
for x in (0, 1):
for y in (0, 1):
self.assertIsNone(world_map.attackable_avatar(Location(x, y)))
def test_attackable_avatar_returns_avatar(self):
avatar = DummyAvatar()
world_map = AvatarMap(avatar)
assert world_map.attackable_avatar(Location(0, 0)) == avatar
class TestWorldMapWithOriginCentre(TestWorldMap):
def _grid_from_list(self, in_list):
out = {}
min_x = -int_ceil(len(in_list) / 2.0) + 1
min_y = -int_ceil(len(in_list[0]) / 2.0) + 1
for i, column in enumerate(in_list):
x = i + min_x
for j, cell in enumerate(column):
y = j + min_y
out[Location(x, y)] = cell
return out
def test_retrieve_negative(self):
world_map = WorldMap(generate_grid(3, 3), self.settings)
self.assertTrue(world_map.is_on_map(Location(-1, -1)))
class TestStaticSpawnDecorator(TestCase):
def test_spawn_is_static(self):
decorated_map = WorldMapStaticSpawnDecorator(WorldMap({}, {}), Location(3, 7))
for _ in range(5):
assert decorated_map.get_random_spawn_location() == Location(3, 7)