home · contact · privacy
Fix cookie message.
[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 replenish your energy."'
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         if dropped.type_ == 'Crate':
183             for item in dropped.content:
184                 item.position = target_position
185         targets = [t for t in self.thing.game.things
186                    if t.position == dropped.position and not t == dropped]
187         for target in targets:
188             if target.type_ == 'CookieSpawner':
189                 target.accept(dropped)
190                 self.thing.game.remove_thing(dropped)
191                 break
192             elif target.type_ == 'BottleDeposit':
193                 target.accept()
194                 self.thing.game.remove_thing(dropped)
195                 break
196             elif target.type_ == 'Crate':
197                 target.accept(dropped)
198                 self.thing.send_msg('CHAT "You put the item into the crate."')
199                 break
200             elif target.type_ == 'HatRemixer':
201                 t.accept(dropped)
202                 break
203         self.thing.game.record_change(self.thing.position, 'other')
204
205
206
207 class Task_DOOR(Task):
208
209     def check(self):
210         action_radius = list(self.thing.game.map_geometry.
211                              get_neighbors_yxyx(self.thing.position).values())
212         reachable_doors = [t for t in self.thing.game.things if
213                            t.type_ == 'Door' and t.position in action_radius]
214         if len(reachable_doors) == 0:
215             raise PlayError('not standing next to a door to open/close')
216         for door in reachable_doors:
217             if not door.blocks_movement:
218                 return
219             if not door.locked:
220                 return
221             if self.thing.carrying and self.thing.carrying.type_ == 'DoorKey'\
222                and self.thing.carrying.door == door:
223                 return
224         raise PlayError('cannot open locked door without its key')
225
226     def do(self):
227         action_radius = list(self.thing.game.map_geometry.
228                              get_neighbors_yxyx(self.thing.position).values())
229         for t in [t for t in self.thing.game.things if
230                   t.type_ == 'Door' and t.position in action_radius]:
231             if t.blocks_movement:
232                 t.open()
233             else:
234                 t.close()
235                 if self.thing.carrying and\
236                    self.thing.carrying.type_ == 'DoorKey' and\
237                    self.thing.carrying.door == t:
238                     self.thing.send_msg('CHAT "You lock the door."')
239                     t.lock()
240             self.thing.game.record_change(t.position, 'other')
241             self.thing.game.record_change(t.position, 'fov')
242
243
244
245 class Task_INTOXICATE(Task):
246
247     def check(self):
248         if self.thing.carrying is None:
249             raise PlayError('carrying nothing to consume')
250         if not self.thing.carrying.consumable:
251             raise PlayError('cannot consume this kind of thing')
252         if self.thing.carrying.type_ == 'Bottle' and\
253            not self.thing.carrying.full:
254             raise PlayError('bottle is empty')
255
256     def do(self):
257         if self.thing.carrying.type_ == 'Bottle':
258             self.thing.carrying.full = False
259             self.thing.carrying.empty()
260             self.thing.send_msg('CHAT "You are drunk now."')
261             self.thing.need_for_toilet += 1
262             self.thing.drunk += 10000
263             self.thing.invalidate('fov')
264             self.thing.game.record_change(self.thing.position, 'other')
265         elif self.thing.carrying.type_ == 'Psychedelic':
266             self.thing.tripping += 10000
267             self.thing.send_msg('CHAT "You start tripping."')
268             self.thing.send_msg('RANDOM_COLORS')
269             eaten = self.thing.uncarry()
270             self.thing.game.remove_thing(eaten)
271         elif self.thing.carrying.type_ == 'Cookie':
272             self.thing.send_msg('CHAT ' + quote('You eat a cookie that grants the ability to draw the following character: "%s"' % self.thing.carrying.thing_char))
273             self.thing.add_cookie_char(self.thing.carrying.thing_char)
274             eaten = self.thing.uncarry()
275             self.thing.game.remove_thing(eaten)
276         elif self.thing.carrying.type_ == 'Stimulant':
277             self.thing.send_msg('CHAT "You feel a flash of energy."')
278             self.thing.energy += 50
279             eaten = self.thing.uncarry()
280             self.thing.game.remove_thing(eaten)
281
282
283
284 class Task_COMMAND(Task):
285     argtypes = 'string'
286
287     def check(self):
288         if self.thing.carrying is None:
289             raise PlayError('nothing to command')
290         if not self.thing.carrying.commandable:
291             raise PlayError('cannot command this item type')
292
293     def do(self):
294         reply_lines = self.thing.carrying.interpret(self.args[0])
295         for line in reply_lines:
296             self.thing.send_msg('REPLY ' + quote(line))
297
298
299
300 class Task_INSTALL(Task):
301     argtypes = 'string'
302
303     def _get_uninstallables(self):
304         return [t for t in self.thing.game.things
305                 if t != self.thing
306                 and hasattr(t, 'installable') and t.installable
307                 and (not t.portable)
308                 and t.position == self.thing.position]
309
310     def check(self):
311         if not self.thing.game.can_do_tile_with_pw(*self.thing.position,
312                                                    self.args[0]):
313             raise GameError('wrong password for tile')
314         if self.thing.carrying:
315             if not hasattr(self.thing.carrying, 'installable')\
316                or not self.thing.carrying.installable:
317                 raise PlayError('carried thing not installable')
318         elif len(self._get_uninstallables()) == 0:
319             raise PlayError('nothing to uninstall here')
320
321     def do(self):
322         if self.thing.carrying:
323             t = self.thing.uncarry()
324             t.install()
325             self.thing.send_msg('CHAT "You install the thing you carry."')
326         else:
327             self._get_uninstallables()[0].uninstall()
328             self.thing.send_msg('CHAT "You uninstall the thing here."')
329         self.thing.game.record_change(self.thing.position, 'other')
330
331
332
333 class Task_WEAR(Task):
334
335     def check(self):
336         if self.thing.name in self.thing.game.hats:
337             return
338         if not self.thing.carrying:
339             raise PlayError('carrying nothing to wear')
340         if self.thing.name in self.thing.game.hats:
341             raise PlayError('already wearing a hat')
342         if self.thing.carrying.type_ not in {'Hat', 'Bottle'}:
343             raise PlayError('can not wear the kind of thing you hold')
344
345     def do(self):
346         if self.thing.name in self.thing.game.hats:
347             t = self.thing.game.add_thing('Hat', self.thing.position)
348             t.design = self.thing.game.hats[self.thing.name]
349             del self.thing.game.hats[self.thing.name]
350             self.thing.send_msg('CHAT "You drop your hat."')
351             for remixer in [t for t in self.thing.game.things
352                             if t.type_ == 'HatRemixer'
353                             and t.position == self.thing.position]:
354                 remixer.accept(t)
355                 break
356         else:
357             if self.thing.carrying.type_ == 'Bottle':
358                 self.thing.send_msg('CHAT "Your attempt to wear a bottle on '
359                                     'your head fails."')
360                 self.thing.carrying.sound('BOTTLE', 'SMASH')
361             elif self.thing.carrying.type_ == 'Hat':
362                 self.thing.game.hats[self.thing.name] =\
363                     self.thing.carrying.design
364                 self.thing.send_msg('CHAT "You put on a hat."')
365             dropped = self.thing.uncarry()
366             self.thing.game.remove_thing(dropped)
367         self.thing.game.record_change(self.thing.position, 'other')
368
369
370
371 class Task_SPIN(Task):
372
373     def check(self):
374         if not self.thing.carrying:
375             raise PlayError('holding nothing to spin')
376         if not hasattr(self.thing.carrying, 'spinnable'):
377             raise PlayError('held object not spinnable')
378
379     def do(self):
380         self.thing.carrying.spin()
381         self.thing.send_msg('CHAT "You spin this object."')
382
383
384
385 class Task_DANCE(Task):
386
387     def do(self):
388         self.thing.send_msg('CHAT "You dance."')
389         self.thing.dancing += 10
390         self.thing.game.changed = True