home · contact · privacy
Add door keys and door locking.
[plomrogue2] / plomrogue / tasks.py
1 from plomrogue.errors import PlayError, GameError
2 from plomrogue.misc import quote
3
4
5
6 class Task:
7     argtypes = ''
8     todo = 1
9
10     def __init__(self, thing, args=()):
11         self.thing = thing
12         self.args = args
13
14     def _get_move_target(self):
15         if self.args[0] == 'HERE':
16             return self.thing.position
17         return self.thing.game.map_geometry.move_yxyx(self.thing.position,
18                                                       self.args[0])
19
20     def check(self):
21         pass
22
23
24
25 class Task_WAIT(Task):
26
27     def do(self):
28         pass
29
30
31
32 class Task_MOVE(Task):
33     argtypes = 'string:direction'
34
35     def check(self):
36         test_yxyx = self._get_move_target()
37         move_blockers = self.thing.game.get_movement_blockers()
38         if test_yxyx in [t.position for t in self.thing.game.things
39                          if t.blocks_movement]:
40             raise PlayError('blocked by other thing')
41         elif self.thing.game.maps[test_yxyx[0]][test_yxyx[1]] in move_blockers:
42             raise PlayError('blocked by impassable tile')
43
44     def do(self):
45         if self.thing.type_ == 'Player' and not self.thing.standing:
46             self.thing.standing = True
47             self.thing.send_msg('CHAT "You get up."')
48         self.thing.game.record_change(self.thing.position, 'other')
49         if self.thing.blocks_light:
50             self.thing.game.record_change(self.thing.position, 'fov')
51         self.thing.position = self._get_move_target()
52         for t in [t for t in self.thing.game.things
53                   if t.type_ == 'Player' and not t == self.thing
54                   and t.position == self.thing.position]:
55             self.thing.send_msg('CHAT %s' %
56                                 quote('You get awkwardly close to %s.' % t.name))
57             for c_id in self.thing.game.sessions:
58                 if self.thing.game.sessions[c_id]['thing_id'] == t.id_:
59                     t.send_msg('CHAT %s' %
60                                quote('%s gets awkwardly close to you.' %
61                                      self.thing.name))
62                     break
63         self.thing.game.record_change(self.thing.position, 'other')
64         terrain = \
65             self.thing.game.maps[self.thing.position[0]][self.thing.position[1]]
66         if terrain in self.thing.game.terrains:
67             terrain_type = self.thing.game.terrains[terrain]
68             if 'sittable' in terrain_type.tags:
69                 self.thing.standing = False
70                 self.thing.send_msg('CHAT "You sink into the %s."'
71                                     % terrain_type.description)
72         self.thing.invalidate('fov')
73         if self.thing.blocks_light:
74             self.thing.game.record_change(self.thing.position, 'fov')
75         if self.thing.carrying:
76             self.thing.carrying.position = self.thing.position
77
78
79
80 class Task_WRITE(Task):
81     argtypes = 'string:char string'
82
83     def check(self):
84         if not self.thing.game.can_do_tile_with_pw(*self.thing.position,
85                                                    self.args[1]):
86             raise GameError('wrong password for tile')
87
88     def do(self):
89         big_yx = self.thing.position[0]
90         little_yx = self.thing.position[1]
91         self.thing.game.maps[big_yx][little_yx] = self.args[0]
92         self.thing.game.record_change((big_yx, little_yx), 'fov')
93
94
95
96 class Task_FLATTEN_SURROUNDINGS(Task):
97     argtypes = 'string'
98
99     def check(self):
100         pass
101
102     def do(self):
103         for yxyx in [self.thing.position] + \
104                 list(self.thing.game.map_geometry.get_neighbors_yxyx(
105                     self.thing.position).values()):
106             if not self.thing.game.can_do_tile_with_pw(*yxyx, self.args[0]):
107                 continue
108             self.thing.game.maps[yxyx[0]][yxyx[1]] = self.thing.game.get_flatland()
109             self.thing.game.record_change(yxyx, 'fov')
110
111
112
113 class Task_PICK_UP(Task):
114     argtypes = 'int:pos'
115
116     def check(self):
117         if self.thing.carrying:
118             raise PlayError('already carrying something')
119         to_pick_up = self.thing.game.get_thing(self.args[0])
120         neighbors = self.thing.game.map_geometry.\
121             get_neighbors_yxyx(self.thing.position).values()
122         reach = [self.thing.position] + list(neighbors)
123         if to_pick_up is None:
124             raise PlayError('no such thing exists')
125         elif to_pick_up == self.thing:
126             raise PlayError('cannot pick up oneself')
127         elif to_pick_up.type_ == 'Player':
128             raise PlayError('cannot pick up player')
129         elif to_pick_up.carried:
130             raise PlayError('thing already carried by a player')
131         elif to_pick_up.position not in reach:
132             raise PlayError('thing not in reach')
133         elif not to_pick_up.portable:
134             raise PlayError('thing not portable')
135
136     def do(self):
137         to_pick_up = self.thing.game.get_thing(self.args[0])
138         to_pick_up.position = self.thing.position[:]
139         self.thing.carrying = to_pick_up
140         to_pick_up.carried = True
141         self.thing.game.record_change(self.thing.position, 'other')
142
143
144
145 class Task_DROP(Task):
146     argtypes = 'string:direction+here'
147
148     def check(self):
149         if not self.thing.carrying:
150             raise PlayError('nothing to drop')
151         target_position = self._get_move_target()
152         if self.thing.carrying.type_ == 'Bottle' and self.thing.carrying.full:
153             for t in [t for t in self.thing.game.things
154                       if t.type_ == 'BottleDeposit'
155                       and t.position == target_position]:
156                 raise PlayError('cannot drop full bottle into bottle deposit')
157
158     def do(self):
159         target_position = self._get_move_target()
160         dropped = self.thing.uncarry()
161         dropped.position = target_position
162         over_cookie_spawner = None
163         for t in [t for t in self.thing.game.things
164                   if t.type_ == 'CookieSpawner'
165                   and t.position == dropped.position]:
166             over_cookie_spawner = t
167             break
168         if over_cookie_spawner:
169             over_cookie_spawner.accept(dropped)
170             self.thing.game.remove_thing(dropped)
171         elif dropped.type_ == 'Bottle' and not dropped.full:
172             for t in [t for t in self.thing.game.things
173                       if t.type_ == 'BottleDeposit'
174                       and t.position == dropped.position]:
175                 t.accept()
176                 self.thing.game.remove_thing(dropped)
177                 break
178         elif dropped.type_ == 'Hat':
179             for t in [t for t in self.thing.game.things
180                       if t.type_ == 'HatRemixer'
181                       and t.position == dropped.position]:
182                 t.accept(dropped)
183                 break
184         self.thing.game.record_change(self.thing.position, 'other')
185
186
187
188 class Task_DOOR(Task):
189
190     def check(self):
191         action_radius = list(self.thing.game.map_geometry.
192                              get_neighbors_yxyx(self.thing.position).values())
193         reachable_doors = [t for t in self.thing.game.things if
194                            t.type_ == 'Door' and t.position in action_radius]
195         if len(reachable_doors) == 0:
196             raise PlayError('not standing next to a door to open/close')
197         for door in reachable_doors:
198             if not door.blocks_movement:
199                 return
200             if not door.locked:
201                 return
202             if self.thing.carrying and self.thing.carrying.type_ == 'DoorKey'\
203                and self.thing.carrying.door == door:
204                 return
205         raise PlayError('cannot open locked door without its key')
206
207     def do(self):
208         action_radius = list(self.thing.game.map_geometry.
209                              get_neighbors_yxyx(self.thing.position).values())
210         for t in [t for t in self.thing.game.things if
211                   t.type_ == 'Door' and t.position in action_radius]:
212             if t.blocks_movement:
213                 t.open()
214             else:
215                 t.close()
216                 if self.thing.carrying and\
217                    self.thing.carrying.type_ == 'DoorKey' and\
218                    self.thing.carrying.door == t:
219                     self.thing.send_msg('CHAT "You lock the door."')
220                     t.lock()
221             self.thing.game.record_change(t.position, 'other')
222             self.thing.game.record_change(t.position, 'fov')
223
224
225
226 class Task_INTOXICATE(Task):
227
228     def check(self):
229         if self.thing.carrying is None:
230             raise PlayError('carrying nothing to consume')
231         if self.thing.carrying.type_ not in {'Bottle', 'Cookie', 'Psychedelic'}:
232             raise PlayError('cannot consume this kind of thing')
233         if self.thing.carrying.type_ == 'Bottle' and\
234            not self.thing.carrying.full:
235             raise PlayError('bottle is empty')
236
237     def do(self):
238         if self.thing.carrying.type_ == 'Bottle':
239             self.thing.carrying.full = False
240             self.thing.carrying.empty()
241             self.thing.send_msg('CHAT "You are drunk now."')
242             self.thing.need_for_toilet += 10000
243             self.thing.drunk = 10000
244             self.thing.invalidate('fov')
245             self.thing.game.record_change(self.thing.position, 'other')
246         elif self.thing.carrying.type_ == 'Psychedelic':
247             self.thing.tripping = 10000
248             self.thing.send_msg('CHAT "You start tripping."')
249             self.thing.send_msg('RANDOM_COLORS')
250             eaten = self.thing.uncarry()
251             self.thing.game.remove_thing(eaten)
252         elif self.thing.carrying.type_ == 'Cookie':
253             self.thing.send_msg('CHAT ' + quote('You eat a cookie and gain the ability to draw the following character: "%s"' % self.thing.carrying.thing_char))
254             self.thing.add_cookie_char(self.thing.carrying.thing_char)
255             eaten = self.thing.uncarry()
256             self.thing.game.remove_thing(eaten)
257
258
259
260 class Task_COMMAND(Task):
261     argtypes = 'string'
262
263     def check(self):
264         if self.thing.carrying is None:
265             raise PlayError('nothing to command')
266         if not self.thing.carrying.commandable:
267             raise PlayError('cannot command this item type')
268
269     def do(self):
270         reply_lines = self.thing.carrying.interpret(self.args[0])
271         for line in reply_lines:
272             self.thing.send_msg('REPLY ' + quote(line))
273
274
275
276 class Task_INSTALL(Task):
277     argtypes = 'string'
278
279     def _get_uninstallables(self):
280         return [t for t in self.thing.game.things
281                 if t != self.thing
282                 and hasattr(t, 'installable') and t.installable
283                 and (not t.portable)
284                 and t.position == self.thing.position]
285
286     def check(self):
287         if not self.thing.game.can_do_tile_with_pw(*self.thing.position,
288                                                    self.args[0]):
289             raise GameError('wrong password for tile')
290         if self.thing.carrying:
291             if not hasattr(self.thing.carrying, 'installable')\
292                or not self.thing.carrying.installable:
293                 raise PlayError('carried thing not installable')
294         elif len(self._get_uninstallables()) == 0:
295             raise PlayError('nothing to uninstall here')
296
297     def do(self):
298         if self.thing.carrying:
299             t = self.thing.uncarry()
300             t.install()
301             self.thing.send_msg('CHAT "You install the thing you carry."')
302         else:
303             self._get_uninstallables()[0].uninstall()
304             self.thing.send_msg('CHAT "You uninstall the thing here."')
305         self.thing.game.record_change(self.thing.position, 'other')
306
307
308
309 class Task_WEAR(Task):
310
311     def check(self):
312         if self.thing.name in self.thing.game.hats:
313             return
314         if not self.thing.carrying:
315             raise PlayError('carrying nothing to wear')
316         if self.thing.name in self.thing.game.hats:
317             raise PlayError('already wearing a hat')
318         if self.thing.carrying.type_ not in {'Hat', 'Bottle'}:
319             raise PlayError('can not wear the kind of thing you hold')
320
321     def do(self):
322         if self.thing.name in self.thing.game.hats:
323             t = self.thing.game.add_thing('Hat', self.thing.position)
324             t.design = self.thing.game.hats[self.thing.name]
325             del self.thing.game.hats[self.thing.name]
326             self.thing.send_msg('CHAT "You drop your hat."')
327             for remixer in [t for t in self.thing.game.things
328                             if t.type_ == 'HatRemixer'
329                             and t.position == self.thing.position]:
330                 remixer.accept(t)
331                 break
332         else:
333             if self.thing.carrying.type_ == 'Bottle':
334                 self.thing.send_msg('CHAT "Your attempt to wear a bottle on '
335                                     'your head fails."')
336                 self.thing.carrying.sound('BOTTLE', 'SMASH')
337             elif self.thing.carrying.type_ == 'Hat':
338                 self.thing.game.hats[self.thing.name] =\
339                     self.thing.carrying.design
340                 self.thing.send_msg('CHAT "You put on a hat."')
341             dropped = self.thing.uncarry()
342             self.thing.game.remove_thing(dropped)
343         self.thing.game.record_change(self.thing.position, 'other')
344
345
346
347 class Task_SPIN(Task):
348
349     def check(self):
350         if not self.thing.carrying:
351             raise PlayError('holding nothing to spin')
352         if not hasattr(self.thing.carrying, 'spinnable'):
353             raise PlayError('held object not spinnable')
354
355     def do(self):
356         self.thing.carrying.spin()
357         self.thing.send_msg('CHAT "You spin this object."')