-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoor.py
133 lines (109 loc) · 4.36 KB
/
door.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
from outcomes import *
from attributes import Openable, Lockable
class Door(Openable, Lockable):
def __init__(self, name, long, parent, lock):
super().__init__(name, long, None, None, parent)
self.connected_rooms = []
self.lock_ = lock
self.add_child(lock)
self.attach(lock)
@property
def scope(self, modifier=None):
scope = super().scope
scope.update(self.connected_rooms)
return scope
def connect_rooms(self, *rooms):
if not self.connected_rooms:
self.connected_rooms = []
self.connected_rooms.extend(rooms)
for room in rooms:
room.add_door(self)
def get_connected_room_from(self, coming_room):
return next((room for room in self.connected_rooms if room != coming_room), None)
@property
def key(self):
return self.lock_.key
@property
def can_be_picked(self):
return self.lock_.can_be_picked
@property
def locked(self):
return self.lock_.locked
@property
def is_blocked(self):
if self.locked:
return BLOCKED_OBJECT_LOCKED
if not self.is_open:
return BLOCKED_OBJECT_CLOSED
return False
def open(self, opening_tool=None):
if self.locked and not opening_tool:
return BLOCKED_OBJECT_LOCKED
if self.locked and opening_tool:
# Generate a command to unlock the door first, before attempting to open
result = self.world.parse(f"unlock {self.long} with {opening_tool}", advance_time=False)
if result.outcome.outcome != UNLOCK_SUCCESS:
return NO_MESSAGE
self.is_open = True
return OPEN_SUCCESS
def lock(self, locking_tool):
if self.is_open:
return MUST_CLOSE_OBJECT
return self.lock_.lock(locking_tool)
def unlock(self, unlocking_tool):
return self.lock_.unlock(unlocking_tool)
class CellDoor(Door):
@property
def textual(self):
return "heavy barred iron door"
@property
def initial(self):
if self.locked:
return f"A {self} separates you from the prison dungeon."
if not self.locked and not self.is_open:
return f"A {self} separates you from the prison dungeon.\n" \
f"It's been unlocked."
if self.is_open:
return f"The {self} that separated you from the prison dungeon is open."
@property
def description(self):
if self.locked:
return f"It's a {self}, which separates you from the prison dungeon.\n" \
"You can see some things behind the iron bars, " \
"but you're unable to distinguish anything clearly in the darkness."
if not self.locked and not self.is_open:
return f"It's a {self}, which separates you from the prison dungeon.\n" \
"It has been unlocked."
if self.is_open:
return f"The {self} that separated you from the prison dungeon is now open."
class DungeonDoor(Door):
@property
def textual(self):
return "heavy wooden door"
@property
def initial(self):
if self.locked:
return f"A {self} separates you from the prison's courtyard."
if not self.locked and not self.is_open:
return f"A {self} separates you from the prison's courtyard.\n" \
f"It's been unlocked."
if self.is_open:
return f"The {self} that leads to the prison's courtyard is open."
@property
def description(self):
if self.locked:
return f"It's a {self}, which separates you from freedom.\n" \
"It has to lead to the prison's courtyard."
if not self.locked and not self.is_open:
return f"It's a {self}, which separates you from the prison's courtyard.\n" \
"It has been unlocked."
if self.is_open:
return f"The {self} that leads to the prison's courtyard is now open."
def open(self, opening_tool=None):
outcome = super().open(opening_tool)
if outcome == OPEN_SUCCESS:
self.message(f"The {self} opens and reveals your way to freedom.\n"
f"The prison's courtyard seems safe for the time.\n"
f"You may proceed.")
return NO_MESSAGE
return outcome