home · contact · privacy
68ffb09d203055f4e2bd32126d1c609d103517be
[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         self.thing.try_to_sit()
65         self.thing.invalidate('fov')
66         if self.thing.blocks_light:
67             self.thing.game.record_change(self.thing.position, 'fov')
68         if self.thing.carrying:
69             self.thing.carrying.position = self.thing.position
70             if self.thing.carrying.type_ == 'Crate':
71                 for t in self.thing.carrying.content:
72                     t.position = self.thing.position
73
74
75
76 class Task_WRITE(Task):
77     argtypes = 'string:char string'
78
79     def check(self):
80         if not self.thing.game.can_do_tile_with_pw(*self.thing.position,
81                                                    self.args[1]):
82             raise GameError('wrong password for tile')
83
84     def do(self):
85         big_yx = self.thing.position[0]
86         little_yx = self.thing.position[1]
87         self.thing.game.maps[big_yx][little_yx] = self.args[0]
88         self.thing.game.record_change((big_yx, little_yx), 'fov')
89
90
91
92 class Task_FLATTEN_SURROUNDINGS(Task):
93     argtypes = 'string'
94
95     def check(self):
96         pass
97
98     def do(self):
99         for yxyx in [self.thing.position] + \
100                 list(self.thing.game.map_geometry.get_neighbors_yxyx(
101                     self.thing.position).values()):
102             if not self.thing.game.can_do_tile_with_pw(*yxyx, self.args[0]):
103                 continue
104             self.thing.game.maps[yxyx[0]][yxyx[1]] = self.thing.game.get_flatland()
105             self.thing.game.record_change(yxyx, 'fov')
106
107
108
109 class Task_PICK_UP(Task):
110     argtypes = 'int:pos'
111
112     def check(self):
113         if self.thing.carrying:
114             raise PlayError('already carrying something')
115         to_pick_up = self.thing.game.get_thing(self.args[0])
116         neighbors = self.thing.game.map_geometry.\
117             get_neighbors_yxyx(self.thing.position).values()
118         reach = [self.thing.position] + list(neighbors)
119         if to_pick_up is None:
120             raise PlayError('no such thing exists')
121         elif to_pick_up == self.thing:
122             raise PlayError('cannot pick up oneself')
123         elif to_pick_up.type_ == 'Player':
124             raise PlayError('cannot pick up player')
125         elif to_pick_up.carried:
126             raise PlayError('thing already carried by a player')
127         elif to_pick_up.position not in reach:
128             raise PlayError('thing not in reach')
129         elif not to_pick_up.portable:
130             raise PlayError('thing not portable')
131
132     def do(self):
133         to_pick_up = self.thing.game.get_thing(self.args[0])
134         to_pick_up.position = self.thing.position[:]
135         self.thing.carrying = to_pick_up
136         to_pick_up.carried = True
137         for t in [t for t in self.thing.game.things
138                   if t.type_ == 'Crate' and to_pick_up in t.content]:
139             t.remove_from_crate(to_pick_up)
140             self.thing.send_msg('CHAT "You take the item out of the crate."')
141             break
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         targets = [t for t in self.thing.game.things
154                    if t.position == target_position
155                    and not t == self.thing.carrying]
156         for target in targets:
157             if target.type_ == 'CookieSpawner' and\
158                not self.thing.carrying.cookable:
159                 raise PlayError('cannot cook items of this type')
160             elif target.type_ == 'BottleDeposit':
161                 if not self.thing.carrying.type_ == 'Bottle':
162                     raise PlayError('cannot only put bottle into bottle deposit')
163                 if self.thing.carrying.full:
164                     raise PlayError('cannot drop full '
165                                     'bottle into bottle deposit')
166             elif target.type_ == 'Crate' and\
167                  self.thing.carrying.type_ == 'Crate':
168                 raise PlayError('cannot put crate into crate')
169
170     def do(self):
171         target_position = self._get_move_target()
172         dropped = self.thing.uncarry()
173         dropped.position = target_position
174         if dropped.type_ == 'Crate':
175             for item in dropped.content:
176                 item.position = target_position
177         targets = [t for t in self.thing.game.things
178                    if t.position == dropped.position and not t == dropped]
179         for target in targets:
180             if target.type_ == 'CookieSpawner':
181                 target.accept(dropped)
182                 self.thing.game.remove_thing(dropped)
183                 break
184             elif target.type_ == 'BottleDeposit':
185                 target.accept()
186                 self.thing.game.remove_thing(dropped)
187                 break
188             elif target.type_ == 'Crate':
189                 target.accept(dropped)
190                 self.thing.send_msg('CHAT "You put the item into the crate."')
191                 break
192             elif target.type_ == 'HatRemixer':
193                 t.accept(dropped)
194                 break
195         self.thing.game.record_change(self.thing.position, 'other')
196
197
198
199 class Task_DOOR(Task):
200
201     def check(self):
202         action_radius = list(self.thing.game.map_geometry.
203                              get_neighbors_yxyx(self.thing.position).values())
204         reachable_doors = [t for t in self.thing.game.things if
205                            t.type_ == 'Door' and t.position in action_radius]
206         if len(reachable_doors) == 0:
207             raise PlayError('not standing next to a door to open/close')
208         for door in reachable_doors:
209             if not door.blocks_movement:
210                 return
211             if not door.locked:
212                 return
213             if self.thing.carrying and self.thing.carrying.type_ == 'DoorKey'\
214                and self.thing.carrying.door == door:
215                 return
216         raise PlayError('cannot open locked door without its key')
217
218     def do(self):
219         action_radius = list(self.thing.game.map_geometry.
220                              get_neighbors_yxyx(self.thing.position).values())
221         for t in [t for t in self.thing.game.things if
222                   t.type_ == 'Door' and t.position in action_radius]:
223             if t.blocks_movement:
224                 t.open()
225             else:
226                 t.close()
227                 if self.thing.carrying and\
228                    self.thing.carrying.type_ == 'DoorKey' and\
229                    self.thing.carrying.door == t:
230                     self.thing.send_msg('CHAT "You lock the door."')
231                     t.lock()
232             self.thing.game.record_change(t.position, 'other')
233             self.thing.game.record_change(t.position, 'fov')
234
235
236
237 class Task_INTOXICATE(Task):
238
239     def check(self):
240         if self.thing.carrying is None:
241             raise PlayError('carrying nothing to consume')
242         if not self.thing.carrying.consumable:
243             raise PlayError('cannot consume this kind of thing')
244         if self.thing.carrying.type_ == 'Bottle' and\
245            not self.thing.carrying.full:
246             raise PlayError('bottle is empty')
247
248     def do(self):
249         if self.thing.carrying.type_ == 'Bottle':
250             self.thing.carrying.full = False
251             self.thing.carrying.empty()
252             self.thing.send_msg('CHAT "You are drunk now."')
253             self.thing.need_for_toilet += 1
254             self.thing.drunk += 100000
255             self.thing.invalidate('fov')
256             self.thing.game.record_change(self.thing.position, 'other')
257         elif self.thing.carrying.type_ == 'Psychedelic':
258             self.thing.tripping += 100000
259             self.thing.send_msg('CHAT "You start tripping."')
260             self.thing.send_msg('RANDOM_COLORS')
261             eaten = self.thing.uncarry()
262             self.thing.game.remove_thing(eaten)
263         elif self.thing.carrying.type_ == 'Cookie':
264             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))
265             self.thing.add_cookie_char(self.thing.carrying.thing_char)
266             eaten = self.thing.uncarry()
267             self.thing.game.remove_thing(eaten)
268         elif self.thing.carrying.type_ == 'Stimulant':
269             self.thing.send_msg('CHAT "You feel a flash of energy."')
270             self.thing.energy += 50
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."')
374
375
376
377 class Task_DANCE(Task):
378
379     def do(self):
380         self.thing.send_msg('CHAT "You dance."')
381         self.thing.dancing += 10
382         self.thing.game.changed = True