-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
199 lines (170 loc) · 7.5 KB
/
Copy pathtests.py
File metadata and controls
199 lines (170 loc) · 7.5 KB
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
class TestCastle():
def test_style_inheritance(self):
"""Tests that Castle game is an abstract subclass of GameStyle."""
import castle.castle_style as c
import engine.styles as s
assert issubclass(c.Castle, s.GameStyle)
assert issubclass(c.MainFloor, s.LevelStyle)
assert isinstance(c.Castle(), s.GameStyle)
assert isinstance(c.MainFloor(), s.LevelStyle)
def test_game_setup(self):
"""Tests that basic game setup results in predicted state."""
from engine import game
from engine import interface
from engine import place
from engine import styles
from castle import castle_style
from castle import human
# Game generation
t_game = game.Game("The Howling Manor", castle_style.Castle)
thisLevel = t_game.level_list[0]
# Character creation
adam = human.Human(location=thisLevel.start)
thisLevel.start.creatures.append(adam)
t_game.set_char(adam)
adam.team = "player"
i = interface.Interface(t_game)
assert i.state == "move"
# Check creatures
assert adam in thisLevel.start.creatures
# Check that Adam has been clothed
assert adam.subelements[0].equipment
# test that creatures exist
assert len(thisLevel.start.creatures) > 1
# Check organs
assert len(adam.subelements[0].limb_check("grasp")) == 2
assert len(adam.subelements[0].limb_check("amble")) == 2
# Check room
assert isinstance(adam.location, place.Place)
borders = [x for x in adam.location.borders.values() if isinstance(x, place.Place)]
assert len(borders) >= 1
# Check border between first two rooms exists and is on map
r1 = adam.location
r2 = borders[0]
xy1 = thisLevel.roomLocations[r1]
xy2 = thisLevel.roomLocations[r2]
dxy = (int((xy1[0] + xy2[0]) / 2), int((xy1[1] + xy2[1]) / 2))
assert isinstance(thisLevel.layout[dxy[0]][dxy[1]], styles.door)
def test_furniture_creation(self):
"""Tests that furniture is added to rooms properly."""
import castle.castle_style as cs
import castle.furniture as fur
parlor = cs.Parlor("test_parlor", None)
chairs = [x for x in parlor.elements if isinstance(x, fur.Chair)]
# Proper number of chairs
assert chairs[0].count[1] > len(chairs) >= chairs[0].count[0]
# Chairs get their own color schema
assert (chairs[0].color in fur.Chair.color) and (chairs[0].texture in fur.Chair.texture)
def test_room_connections(self):
"""Rooms in a level should have doors connecting them, with a path throughout the level."""
import castle.castle_style as cs
from castle import goblin
from engine import game
visited = []
def explore_depth(g, border):
"""We will send our goblin down each path until the end,
at which point it will backtrack."""
# Explore border
orig_loc = g.location
g.leave(border)
new_loc = g.location
# Goal is to visit every location in level.
if new_loc not in visited:
visited.append(new_loc)
# Save return info
return_path = [a[0] for a in new_loc.borders.items() if a[1] == orig_loc][0]
# Explore each path from new location, except the one we came in on.
for test_border in new_loc.borders.keys():
test_loc = new_loc.borders[test_border]
if (test_loc is not None) and (test_loc is not orig_loc):
# Go down each path until the end (recursion)
explore_depth(g, test_border)
# Backtrack once paths are explored
g.leave(return_path)
# Run test
t_game = game.Game("The Howling Manor", cs.Castle)
thisLevel = t_game.level_list[0]
g = goblin.Goblin(location=thisLevel.start)
test_borders = [b for b in g.location.borders.keys() if g.location.borders[b]]
# Check each path leaving from the first room.
visited.append(g.location)
for border in test_borders:
explore_depth(g, border)
# Collect all rooms so we can ensure each was visited (pigeonhole principle).
all_rooms = []
for level in t_game.level_list:
# print(level.show_map())
all_rooms.extend(level.get_rooms())
assert len(all_rooms) == len(visited)
def test_grasp(self):
"""NPC grasps and ungrasps an item."""
import castle.castle_style as cs
import castle.suits as su
from castle.goblin import ServantGoblin
room = cs.PlayerCell(level=None)
g = ServantGoblin(location=room)
s = su.Shiv(color="gray", texture="iron")
g.grasp(s)
assert g.subelements[0].subelements[1].subelements[0].grasped == s
g.ungrasp(s)
assert g.subelements[0].subelements[1].subelements[0].grasped is None
def test_return_from_depth(self):
from castle.goblin import Goblin
g = Goblin(None)
subelem_names = [x.name for x in g.subelements[0].return_from_depth(2)]
assert "right hand" in subelem_names
assert "finger" not in subelem_names
def test_covered(self):
"""Tests that items cover lower limbs as they're supposed to."""
import castle.castle_style as cs
import castle.suits as su
from castle.goblin import Goblin
Goblin.suits = [su.armorsuit]
room = cs.PlayerCell(level=None)
g = Goblin(location=room)
head = g.subelements[0].subelements[0]
assert head.equipment[0].descends == 2
# Helm should cover teeth as well
assert head.subelements[4].covered[0] == head.covered[0]
def test_auto_clothe(self):
"""Tests that the automatic reclothing function works correctly."""
import engine.place as pl
import assets.asset_collections as ac
import assets.suits as asu
from engine.styles import wall, pillar, floor
from assets.goblin import Goblin, ShallowGoblin
class ExperimentChest(pl.DisplayFurniture):
name = "treasure chest"
color = ["brown", "black", "gray"]
texture = ["wood"]
count = (1, 2)
vis_collections = [(ac.ironsuit_c, (1, 2))]
class ExperimentCell(pl.Place):
name = "Cell"
sprite = "C"
count = (1, 2)
colors = ["carved", "engraved", "etched", "scrolled"]
textures = ["granite", "obsidian", "marble"]
creature_classes = []
furniture_classes = [ExperimentChest]
subelement_classes = [wall, pillar, floor]
# Build
Goblin.suits = [asu.bronze_armorsuit]
ShallowGoblin.suits = [asu.steel_armorsuit]
lab = ExperimentCell(level=None)
g = Goblin(location=lab)
sg = ShallowGoblin(location=lab)
print(g.location)
assert g.subelements[0].subelements[1].equipment[0].armor == 1
g.auto_equip()
assert g.subelements[0].subelements[1].equipment[0].armor == 2
assert g.subelements[0].subelements[2].equipment[0].armor == 2
sg.die()
g.auto_equip()
assert g.subelements[0].subelements[1].equipment[0].armor == 3
assert g.subelements[0].subelements[2].equipment[0].armor == 3
class TestGeneral:
def test_dependencies(self):
import colorist
import dill
pass