-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.py
More file actions
187 lines (163 loc) · 7.38 KB
/
objects.py
File metadata and controls
187 lines (163 loc) · 7.38 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
class Object():
def __init__(self, id=None, rep=None, item_state={}, acts={}, updates={}):
self.id = id
self.rep = rep
self.item_state = item_state
self.acts = acts
self.updates = updates
def _handleInteract(self, act, state, io):
pass
def handleInteract(self, state, io, override_act=None):
act = None
if override_act is None:
io.write("* You can {0}.".format(list(self.acts.keys())))
act = io.read("? ", self.acts)
if 'act' in act:
act = act['act']
else:
io.write("* Not an option")
else:
act = override_act
self._handleInteract(act, state, io)
def update(self, state):
for update in updates:
if update['type'] == 'global':
self.item_state[updade['updateTo']] = update['update'][state[update['updateWith']]]
class TV(Object):
def __init__(self):
super().__init__(
id='TV',
rep='An old TV set.',
item_state={'curChannel': 'OFF',
'channels': ['OFF', '0', '1'],
'contents': {'OFF': 'The TV is OFF', '0': 'Nothing', '1': 'Nothing'}})
self.acts = {'Change channel': 'changeChannel', 'Inspect': 'inspectScreen'}
self.updates = {'changeContents':
{'type': 'global',
'updateWith': 'aiSentiment',
'updateTo': 'contents',
'update':
{
"Pos":
{
"OFF": "The TV is OFF.",
"0": "Nothing",
"1": "Nothing"
},
"Neu":
{
"OFF": "The TV is OFF.",
"0": "Nothing",
"1": "Nothing"
},
"Neg":
{
"OFF": "The TV is OFF.",
"0": "An ominous sound comes off the speaker.",
"1": "Nothing"
}
}
}
}
def _handleInteract(self, act, state, io):
if act == 'changeChannel':
channel = io.read("? Change to channel: ", {})['raw']
if channel in self.item_state['channels']:
self.item_state['curChannel'] = channel
io.write("* Changed channel to {0}".format(channel))
else:
io.write("* Channel not available.")
elif act == 'inspectScreen':
io.write("* " + self.item_state['contents'][self.item_state['curChannel']])
class Key(Object):
def __init__(self):
super().__init__(
id='Key',
rep='A small bronze key',
item_state={})
self.acts = {'Pick up': 'pick', 'Drop': 'drop'}
self.update = {}
def _handleInteract(self, act, state, io):
if self in state['curRoom'].items and act == 'pick':
io.write("* You picked up the {0}".format(self.id))
state['inventory'].append(self)
state['curRoom'].items.remove(self)
state['Known Items'].remove(self)
elif self in state['inventory'] and act == 'drop':
io.write("* You dropped up the {0}".format(self.id))
state['inventory'].remove(self)
state['curRoom'].items.append(self)
state['Known Items'].append(self)
class Path(Object):
def __init__(self, fromArea, toArea, item_state={}, id=None, rep=None):
# "{0} <-> {1}".format(fromArea.id, toArea.id)
Object.__init__(self,
id=id,
rep=rep,
item_state=item_state)
self.item_state["blocking"] = True
self.item_state["area1"] = fromArea
self.item_state["area2"] = toArea
self.acts = {'traverse': 'traverse'}
def _handleInnerInteract(self, act, state, io):
pass
def _handleInteract(self, act, state, io):
if act == 'traverse':
if not self.item_state["blocking"]:
fromArea = state['curRoom']
toArea = None
if fromArea == self.item_state["area1"]:
toArea = self.item_state["area2"]
elif fromArea == self.item_state["area2"]:
toArea = self.item_state["area1"]
if fromArea.id not in state['memory']:
state['memory'][fromArea.id] = {}
state['memory'][fromArea.id]['Known Items'] = state['Known Items']
state['memory'][fromArea.id]['Known Exits'] = state['Known Exits']
if toArea.id in state['memory']:
state['Known Items'] = state['memory'][toArea.id]['Known Items']
state['Known Exits'] = state['memory'][toArea.id]['Known Exits']
else:
state['Known Exits'] = [self]
state['Known Items'] = []
state['curRoom'] = toArea
print(toArea.id)
else:
io.write('* This path is blocked.')
else:
self._handleInnerInteract(act, state, io)
def getOtherArea(self, thisArea):
if thisArea == self.item_state["area1"]:
return self.item_state["area2"]
elif thisArea == self.item_state["area2"]:
return self.item_state["area1"]
class Passway(Path):
def __init__(self, fromArea, toArea, item_state={}):
Path.__init__(self, fromArea, toArea, item_state=item_state,
id="Passway between {0} and {1}".format(fromArea.id, toArea.id), rep="An open passway.")
self.item_state["blocking"] = False
class Door(Path):
def __init__(self, fromArea, toArea, keyDict, item_state={}):
Path.__init__(self, fromArea, toArea, item_state=item_state,
id="Door between {0} and {1}".format(fromArea.id, toArea.id), rep="A Door.")
self.item_state["blocking"] = True
self.acts.update({'use': 'use'})
self.keyDict = keyDict
def _handleInnerInteract(self, act, state, io):
if act == 'use':
if not state['inventory']:
io.write("* Your inventory is empty. Nothing can be used")
else:
io.write("* You can use {0}".format([s.id for s in state['inventory']]))
inDict = {}
for item in state['inventory']:
if item.id == self.keyDict['item']:
inDict[item.id] = 'valid'
else:
inDict[item.id] = 'invalid'
res = io.read("> ", inDict)
if res['act'] == 'valid':
io.write(self.keyDict['act_resp'])
self.item_state["blocking"] = False
else:
io.write("Nothing happened")