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