home · contact · privacy
Start pee-o-meter right on drinking, grow value exponentially over time.
[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.need_for_toilet *= 2
199             self.thing.need_for_toilet += 1
200             self.thing.drunk = 10000
201             self.thing.invalidate('fov')
202             self.thing.game.record_change(self.thing.position, 'other')
203         elif self.thing.carrying.type_ == 'Psychedelic':
204             self.thing.tripping = 10000
205             self.thing.send_msg('CHAT "You start tripping."')
206             self.thing.send_msg('RANDOM_COLORS')
207             eaten = self.thing.uncarry()
208             self.thing.game.remove_thing(eaten)
209         elif self.thing.carrying.type_ == 'Cookie':
210             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))
211             self.thing.add_cookie_char(self.thing.carrying.thing_char)
212             eaten = self.thing.uncarry()
213             self.thing.game.remove_thing(eaten)
214
215
216
217 class Task_COMMAND(Task):
218     argtypes = 'string'
219
220     def check(self):
221         if self.thing.carrying is None:
222             raise PlayError('nothing to command')
223         if not self.thing.carrying.commandable:
224             raise PlayError('cannot command this item type')
225
226     def do(self):
227         reply_lines = self.thing.carrying.interpret(self.args[0])
228         for line in reply_lines:
229             self.thing.send_msg('REPLY ' + quote(line))
230
231
232
233 class Task_INSTALL(Task):
234     argtypes = 'string'
235
236     def _get_uninstallables(self):
237         return [t for t in self.thing.game.things
238                 if t != self.thing
239                 and hasattr(t, 'installable') and t.installable
240                 and (not t.portable)
241                 and t.position == self.thing.position]
242
243     def check(self):
244         if not self.thing.game.can_do_tile_with_pw(*self.thing.position,
245                                                    self.args[0]):
246             raise GameError('wrong password for tile')
247         if self.thing.carrying:
248             if not hasattr(self.thing.carrying, 'installable')\
249                or not self.thing.carrying.installable:
250                 raise PlayError('carried thing not installable')
251         elif len(self._get_uninstallables()) == 0:
252             raise PlayError('nothing to uninstall here')
253
254     def do(self):
255         if self.thing.carrying:
256             t = self.thing.uncarry()
257             t.install()
258             self.thing.send_msg('CHAT "You install the thing you carry."')
259         else:
260             self._get_uninstallables()[0].uninstall()
261             self.thing.send_msg('CHAT "You uninstall the thing here."')
262         self.thing.game.record_change(self.thing.position, 'other')
263
264
265
266 class Task_WEAR(Task):
267
268     def check(self):
269         if self.thing.name in self.thing.game.hats:
270             return
271         if not self.thing.carrying:
272             raise PlayError('carrying nothing to wear')
273         if self.thing.name in self.thing.game.hats:
274             raise PlayError('already wearing a hat')
275         if self.thing.carrying.type_ not in {'Hat', 'Bottle'}:
276             raise PlayError('can not wear the kind of thing you hold')
277
278     def do(self):
279         if self.thing.name in self.thing.game.hats:
280             t = self.thing.game.add_thing('Hat', self.thing.position)
281             t.design = self.thing.game.hats[self.thing.name]
282             del self.thing.game.hats[self.thing.name]
283             self.thing.send_msg('CHAT "You drop your hat."')
284             for remixer in [t for t in self.thing.game.things
285                             if t.type_ == 'HatRemixer'
286                             and t.position == self.thing.position]:
287                 remixer.accept(t)
288                 break
289         else:
290             if self.thing.carrying.type_ == 'Bottle':
291                 self.thing.send_msg('CHAT "Your attempt to wear a bottle on '
292                                     'your head fails."')
293                 self.thing.carrying.sound('BOTTLE', 'SMASH')
294             elif self.thing.carrying.type_ == 'Hat':
295                 self.thing.game.hats[self.thing.name] =\
296                     self.thing.carrying.design
297                 self.thing.send_msg('CHAT "You put on a hat."')
298             dropped = self.thing.uncarry()
299             self.thing.game.remove_thing(dropped)
300         self.thing.game.record_change(self.thing.position, 'other')
301
302
303
304 class Task_SPIN(Task):
305
306     def check(self):
307         if not self.thing.carrying:
308             raise PlayError('holding nothing to spin')
309         if not hasattr(self.thing.carrying, 'spinnable'):
310             raise PlayError('held object not spinnable')
311
312     def do(self):
313         self.thing.carrying.spin()
314         self.thing.send_msg('CHAT "You spin this object."')