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