home · contact · privacy
95945055e0f7e15923bc34780a985c2b3a5ea556
[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                                     'Staying here will reduce your weariness."'
72                                     % terrain_type.description)
73         self.thing.invalidate('fov')
74         if self.thing.blocks_light:
75             self.thing.game.record_change(self.thing.position, 'fov')
76         if self.thing.carrying:
77             self.thing.carrying.position = self.thing.position
78             if self.thing.carrying.type_ == 'Crate':
79                 for t in self.thing.carrying.content:
80                     t.position = self.thing.position
81
82
83
84 class Task_WRITE(Task):
85     argtypes = 'string:char string'
86
87     def check(self):
88         if not self.thing.game.can_do_tile_with_pw(*self.thing.position,
89                                                    self.args[1]):
90             raise GameError('wrong password for tile')
91
92     def do(self):
93         big_yx = self.thing.position[0]
94         little_yx = self.thing.position[1]
95         self.thing.game.maps[big_yx][little_yx] = self.args[0]
96         self.thing.game.record_change((big_yx, little_yx), 'fov')
97
98
99
100 class Task_FLATTEN_SURROUNDINGS(Task):
101     argtypes = 'string'
102
103     def check(self):
104         pass
105
106     def do(self):
107         for yxyx in [self.thing.position] + \
108                 list(self.thing.game.map_geometry.get_neighbors_yxyx(
109                     self.thing.position).values()):
110             if not self.thing.game.can_do_tile_with_pw(*yxyx, self.args[0]):
111                 continue
112             self.thing.game.maps[yxyx[0]][yxyx[1]] = self.thing.game.get_flatland()
113             self.thing.game.record_change(yxyx, 'fov')
114
115
116
117 class Task_PICK_UP(Task):
118     argtypes = 'int:pos'
119
120     def check(self):
121         if self.thing.carrying:
122             raise PlayError('already carrying something')
123         to_pick_up = self.thing.game.get_thing(self.args[0])
124         neighbors = self.thing.game.map_geometry.\
125             get_neighbors_yxyx(self.thing.position).values()
126         reach = [self.thing.position] + list(neighbors)
127         if to_pick_up is None:
128             raise PlayError('no such thing exists')
129         elif to_pick_up == self.thing:
130             raise PlayError('cannot pick up oneself')
131         elif to_pick_up.type_ == 'Player':
132             raise PlayError('cannot pick up player')
133         elif to_pick_up.carried:
134             raise PlayError('thing already carried by a player')
135         elif to_pick_up.position not in reach:
136             raise PlayError('thing not in reach')
137         elif not to_pick_up.portable:
138             raise PlayError('thing not portable')
139
140     def do(self):
141         to_pick_up = self.thing.game.get_thing(self.args[0])
142         to_pick_up.position = self.thing.position[:]
143         self.thing.carrying = to_pick_up
144         to_pick_up.carried = True
145         for t in [t for t in self.thing.game.things
146                   if t.type_ == 'Crate' and to_pick_up in t.content]:
147             t.remove_from_crate(to_pick_up)
148             self.thing.send_msg('CHAT "You take the item out of the crate."')
149             break
150         self.thing.game.record_change(self.thing.position, 'other')
151
152
153
154 class Task_DROP(Task):
155     argtypes = 'string:direction+here'
156
157     def check(self):
158         if not self.thing.carrying:
159             raise PlayError('nothing to drop')
160         target_position = self._get_move_target()
161         targets = [t for t in self.thing.game.things
162                    if t.position == target_position
163                    and not t == self.thing.carrying]
164         for target in targets:
165             if target.type_ == 'CookieSpawner' and\
166                not self.thing.carrying.cookable:
167                 raise PlayError('cannot cook items of this type')
168             elif target.type_ == 'BottleDeposit':
169                 if not self.thing.carrying.type_ == 'Bottle':
170                     raise PlayError('cannot only put bottle into bottle deposit')
171                 if self.thing.carrying.full:
172                     raise PlayError('cannot drop full '
173                                     'bottle into bottle deposit')
174             elif target.type_ == 'Crate' and\
175                  self.thing.carrying.type_ == 'Crate':
176                 raise PlayError('cannot put crate into crate')
177
178     def do(self):
179         target_position = self._get_move_target()
180         dropped = self.thing.uncarry()
181         dropped.position = target_position
182         targets = [t for t in self.thing.game.things
183                    if t.position == dropped.position and not t == dropped]
184         for target in targets:
185             if target.type_ == 'CookieSpawner':
186                 target.accept(dropped)
187                 self.thing.game.remove_thing(dropped)
188                 break
189             elif target.type_ == 'BottleDeposit':
190                 target.accept()
191                 self.thing.game.remove_thing(dropped)
192                 break
193             elif target.type_ == 'Crate':
194                 target.accept(dropped)
195                 self.thing.send_msg('CHAT "You put the item into the crate."')
196                 break
197             elif target.type_ == 'HatRemixer':
198                 t.accept(dropped)
199                 break
200         self.thing.game.record_change(self.thing.position, 'other')
201
202
203
204 class Task_DOOR(Task):
205
206     def check(self):
207         action_radius = list(self.thing.game.map_geometry.
208                              get_neighbors_yxyx(self.thing.position).values())
209         reachable_doors = [t for t in self.thing.game.things if
210                            t.type_ == 'Door' and t.position in action_radius]
211         if len(reachable_doors) == 0:
212             raise PlayError('not standing next to a door to open/close')
213         for door in reachable_doors:
214             if not door.blocks_movement:
215                 return
216             if not door.locked:
217                 return
218             if self.thing.carrying and self.thing.carrying.type_ == 'DoorKey'\
219                and self.thing.carrying.door == door:
220                 return
221         raise PlayError('cannot open locked door without its key')
222
223     def do(self):
224         action_radius = list(self.thing.game.map_geometry.
225                              get_neighbors_yxyx(self.thing.position).values())
226         for t in [t for t in self.thing.game.things if
227                   t.type_ == 'Door' and t.position in action_radius]:
228             if t.blocks_movement:
229                 t.open()
230             else:
231                 t.close()
232                 if self.thing.carrying and\
233                    self.thing.carrying.type_ == 'DoorKey' and\
234                    self.thing.carrying.door == t:
235                     self.thing.send_msg('CHAT "You lock the door."')
236                     t.lock()
237             self.thing.game.record_change(t.position, 'other')
238             self.thing.game.record_change(t.position, 'fov')
239
240
241
242 class Task_INTOXICATE(Task):
243
244     def check(self):
245         if self.thing.carrying is None:
246             raise PlayError('carrying nothing to consume')
247         if self.thing.carrying.type_ not in {'Bottle', 'Cookie', 'Psychedelic'}:
248             raise PlayError('cannot consume this kind of thing')
249         if self.thing.carrying.type_ == 'Bottle' and\
250            not self.thing.carrying.full:
251             raise PlayError('bottle is empty')
252
253     def do(self):
254         if self.thing.carrying.type_ == 'Bottle':
255             self.thing.carrying.full = False
256             self.thing.carrying.empty()
257             self.thing.send_msg('CHAT "You are drunk now."')
258             self.thing.need_for_toilet += 1
259             self.thing.drunk = 10000
260             self.thing.invalidate('fov')
261             self.thing.game.record_change(self.thing.position, 'other')
262         elif self.thing.carrying.type_ == 'Psychedelic':
263             self.thing.tripping = 10000
264             self.thing.send_msg('CHAT "You start tripping."')
265             self.thing.send_msg('RANDOM_COLORS')
266             eaten = self.thing.uncarry()
267             self.thing.game.remove_thing(eaten)
268         elif self.thing.carrying.type_ == 'Cookie':
269             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))
270             self.thing.add_cookie_char(self.thing.carrying.thing_char)
271             eaten = self.thing.uncarry()
272             self.thing.game.remove_thing(eaten)
273
274
275
276 class Task_COMMAND(Task):
277     argtypes = 'string'
278
279     def check(self):
280         if self.thing.carrying is None:
281             raise PlayError('nothing to command')
282         if not self.thing.carrying.commandable:
283             raise PlayError('cannot command this item type')
284
285     def do(self):
286         reply_lines = self.thing.carrying.interpret(self.args[0])
287         for line in reply_lines:
288             self.thing.send_msg('REPLY ' + quote(line))
289
290
291
292 class Task_INSTALL(Task):
293     argtypes = 'string'
294
295     def _get_uninstallables(self):
296         return [t for t in self.thing.game.things
297                 if t != self.thing
298                 and hasattr(t, 'installable') and t.installable
299                 and (not t.portable)
300                 and t.position == self.thing.position]
301
302     def check(self):
303         if not self.thing.game.can_do_tile_with_pw(*self.thing.position,
304                                                    self.args[0]):
305             raise GameError('wrong password for tile')
306         if self.thing.carrying:
307             if not hasattr(self.thing.carrying, 'installable')\
308                or not self.thing.carrying.installable:
309                 raise PlayError('carried thing not installable')
310         elif len(self._get_uninstallables()) == 0:
311             raise PlayError('nothing to uninstall here')
312
313     def do(self):
314         if self.thing.carrying:
315             t = self.thing.uncarry()
316             t.install()
317             self.thing.send_msg('CHAT "You install the thing you carry."')
318         else:
319             self._get_uninstallables()[0].uninstall()
320             self.thing.send_msg('CHAT "You uninstall the thing here."')
321         self.thing.game.record_change(self.thing.position, 'other')
322
323
324
325 class Task_WEAR(Task):
326
327     def check(self):
328         if self.thing.name in self.thing.game.hats:
329             return
330         if not self.thing.carrying:
331             raise PlayError('carrying nothing to wear')
332         if self.thing.name in self.thing.game.hats:
333             raise PlayError('already wearing a hat')
334         if self.thing.carrying.type_ not in {'Hat', 'Bottle'}:
335             raise PlayError('can not wear the kind of thing you hold')
336
337     def do(self):
338         if self.thing.name in self.thing.game.hats:
339             t = self.thing.game.add_thing('Hat', self.thing.position)
340             t.design = self.thing.game.hats[self.thing.name]
341             del self.thing.game.hats[self.thing.name]
342             self.thing.send_msg('CHAT "You drop your hat."')
343             for remixer in [t for t in self.thing.game.things
344                             if t.type_ == 'HatRemixer'
345                             and t.position == self.thing.position]:
346                 remixer.accept(t)
347                 break
348         else:
349             if self.thing.carrying.type_ == 'Bottle':
350                 self.thing.send_msg('CHAT "Your attempt to wear a bottle on '
351                                     'your head fails."')
352                 self.thing.carrying.sound('BOTTLE', 'SMASH')
353             elif self.thing.carrying.type_ == 'Hat':
354                 self.thing.game.hats[self.thing.name] =\
355                     self.thing.carrying.design
356                 self.thing.send_msg('CHAT "You put on a hat."')
357             dropped = self.thing.uncarry()
358             self.thing.game.remove_thing(dropped)
359         self.thing.game.record_change(self.thing.position, 'other')
360
361
362
363 class Task_SPIN(Task):
364
365     def check(self):
366         if not self.thing.carrying:
367             raise PlayError('holding nothing to spin')
368         if not hasattr(self.thing.carrying, 'spinnable'):
369             raise PlayError('held object not spinnable')
370
371     def do(self):
372         self.thing.carrying.spin()
373         self.thing.send_msg('CHAT "You spin this object."')