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