home · contact · privacy
Only allow cookable items to be cooked into cookies.
[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 reduce your weariness."'
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
79
80
81 class Task_WRITE(Task):
82     argtypes = 'string:char string'
83
84     def check(self):
85         if not self.thing.game.can_do_tile_with_pw(*self.thing.position,
86                                                    self.args[1]):
87             raise GameError('wrong password for tile')
88
89     def do(self):
90         big_yx = self.thing.position[0]
91         little_yx = self.thing.position[1]
92         self.thing.game.maps[big_yx][little_yx] = self.args[0]
93         self.thing.game.record_change((big_yx, little_yx), 'fov')
94
95
96
97 class Task_FLATTEN_SURROUNDINGS(Task):
98     argtypes = 'string'
99
100     def check(self):
101         pass
102
103     def do(self):
104         for yxyx in [self.thing.position] + \
105                 list(self.thing.game.map_geometry.get_neighbors_yxyx(
106                     self.thing.position).values()):
107             if not self.thing.game.can_do_tile_with_pw(*yxyx, self.args[0]):
108                 continue
109             self.thing.game.maps[yxyx[0]][yxyx[1]] = self.thing.game.get_flatland()
110             self.thing.game.record_change(yxyx, 'fov')
111
112
113
114 class Task_PICK_UP(Task):
115     argtypes = 'int:pos'
116
117     def check(self):
118         if self.thing.carrying:
119             raise PlayError('already carrying something')
120         to_pick_up = self.thing.game.get_thing(self.args[0])
121         neighbors = self.thing.game.map_geometry.\
122             get_neighbors_yxyx(self.thing.position).values()
123         reach = [self.thing.position] + list(neighbors)
124         if to_pick_up is None:
125             raise PlayError('no such thing exists')
126         elif to_pick_up == self.thing:
127             raise PlayError('cannot pick up oneself')
128         elif to_pick_up.type_ == 'Player':
129             raise PlayError('cannot pick up player')
130         elif to_pick_up.carried:
131             raise PlayError('thing already carried by a player')
132         elif to_pick_up.position not in reach:
133             raise PlayError('thing not in reach')
134         elif not to_pick_up.portable:
135             raise PlayError('thing not portable')
136
137     def do(self):
138         to_pick_up = self.thing.game.get_thing(self.args[0])
139         to_pick_up.position = self.thing.position[:]
140         self.thing.carrying = to_pick_up
141         to_pick_up.carried = True
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         if self.thing.carrying.type_ == 'Bottle' and self.thing.carrying.full:
154             for t in [t for t in self.thing.game.things
155                       if t.type_ == 'BottleDeposit'
156                       and t.position == target_position]:
157                 raise PlayError('cannot drop full bottle into bottle deposit')
158         for t in [t for t in self.thing.game.things
159                   if t.type_ == 'CookieSpawner'
160                   and t.position == target_position]:
161             if not self.thing.carrying.cookable:
162                 raise PlayError('cannot cook items of this type')
163             break
164
165     def do(self):
166         target_position = self._get_move_target()
167         dropped = self.thing.uncarry()
168         dropped.position = target_position
169         over_cookie_spawner = None
170         for t in [t for t in self.thing.game.things
171                   if t.type_ == 'CookieSpawner'
172                   and t.position == dropped.position]:
173             over_cookie_spawner = t
174             break
175         if over_cookie_spawner:
176             over_cookie_spawner.accept(dropped)
177             self.thing.game.remove_thing(dropped)
178         elif dropped.type_ == 'Bottle' and not dropped.full:
179             for t in [t for t in self.thing.game.things
180                       if t.type_ == 'BottleDeposit'
181                       and t.position == dropped.position]:
182                 t.accept()
183                 self.thing.game.remove_thing(dropped)
184                 break
185         elif dropped.type_ == 'Hat':
186             for t in [t for t in self.thing.game.things
187                       if t.type_ == 'HatRemixer'
188                       and t.position == dropped.position]:
189                 t.accept(dropped)
190                 break
191         self.thing.game.record_change(self.thing.position, 'other')
192
193
194
195 class Task_DOOR(Task):
196
197     def check(self):
198         action_radius = list(self.thing.game.map_geometry.
199                              get_neighbors_yxyx(self.thing.position).values())
200         reachable_doors = [t for t in self.thing.game.things if
201                            t.type_ == 'Door' and t.position in action_radius]
202         if len(reachable_doors) == 0:
203             raise PlayError('not standing next to a door to open/close')
204         for door in reachable_doors:
205             if not door.blocks_movement:
206                 return
207             if not door.locked:
208                 return
209             if self.thing.carrying and self.thing.carrying.type_ == 'DoorKey'\
210                and self.thing.carrying.door == door:
211                 return
212         raise PlayError('cannot open locked door without its key')
213
214     def do(self):
215         action_radius = list(self.thing.game.map_geometry.
216                              get_neighbors_yxyx(self.thing.position).values())
217         for t in [t for t in self.thing.game.things if
218                   t.type_ == 'Door' and t.position in action_radius]:
219             if t.blocks_movement:
220                 t.open()
221             else:
222                 t.close()
223                 if self.thing.carrying and\
224                    self.thing.carrying.type_ == 'DoorKey' and\
225                    self.thing.carrying.door == t:
226                     self.thing.send_msg('CHAT "You lock the door."')
227                     t.lock()
228             self.thing.game.record_change(t.position, 'other')
229             self.thing.game.record_change(t.position, 'fov')
230
231
232
233 class Task_INTOXICATE(Task):
234
235     def check(self):
236         if self.thing.carrying is None:
237             raise PlayError('carrying nothing to consume')
238         if self.thing.carrying.type_ not in {'Bottle', 'Cookie', 'Psychedelic'}:
239             raise PlayError('cannot consume this kind of thing')
240         if self.thing.carrying.type_ == 'Bottle' and\
241            not self.thing.carrying.full:
242             raise PlayError('bottle is empty')
243
244     def do(self):
245         if self.thing.carrying.type_ == 'Bottle':
246             self.thing.carrying.full = False
247             self.thing.carrying.empty()
248             self.thing.send_msg('CHAT "You are drunk now."')
249             self.thing.need_for_toilet += 1
250             self.thing.drunk = 10000
251             self.thing.invalidate('fov')
252             self.thing.game.record_change(self.thing.position, 'other')
253         elif self.thing.carrying.type_ == 'Psychedelic':
254             self.thing.tripping = 10000
255             self.thing.send_msg('CHAT "You start tripping."')
256             self.thing.send_msg('RANDOM_COLORS')
257             eaten = self.thing.uncarry()
258             self.thing.game.remove_thing(eaten)
259         elif self.thing.carrying.type_ == 'Cookie':
260             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))
261             self.thing.add_cookie_char(self.thing.carrying.thing_char)
262             eaten = self.thing.uncarry()
263             self.thing.game.remove_thing(eaten)
264
265
266
267 class Task_COMMAND(Task):
268     argtypes = 'string'
269
270     def check(self):
271         if self.thing.carrying is None:
272             raise PlayError('nothing to command')
273         if not self.thing.carrying.commandable:
274             raise PlayError('cannot command this item type')
275
276     def do(self):
277         reply_lines = self.thing.carrying.interpret(self.args[0])
278         for line in reply_lines:
279             self.thing.send_msg('REPLY ' + quote(line))
280
281
282
283 class Task_INSTALL(Task):
284     argtypes = 'string'
285
286     def _get_uninstallables(self):
287         return [t for t in self.thing.game.things
288                 if t != self.thing
289                 and hasattr(t, 'installable') and t.installable
290                 and (not t.portable)
291                 and t.position == self.thing.position]
292
293     def check(self):
294         if not self.thing.game.can_do_tile_with_pw(*self.thing.position,
295                                                    self.args[0]):
296             raise GameError('wrong password for tile')
297         if self.thing.carrying:
298             if not hasattr(self.thing.carrying, 'installable')\
299                or not self.thing.carrying.installable:
300                 raise PlayError('carried thing not installable')
301         elif len(self._get_uninstallables()) == 0:
302             raise PlayError('nothing to uninstall here')
303
304     def do(self):
305         if self.thing.carrying:
306             t = self.thing.uncarry()
307             t.install()
308             self.thing.send_msg('CHAT "You install the thing you carry."')
309         else:
310             self._get_uninstallables()[0].uninstall()
311             self.thing.send_msg('CHAT "You uninstall the thing here."')
312         self.thing.game.record_change(self.thing.position, 'other')
313
314
315
316 class Task_WEAR(Task):
317
318     def check(self):
319         if self.thing.name in self.thing.game.hats:
320             return
321         if not self.thing.carrying:
322             raise PlayError('carrying nothing to wear')
323         if self.thing.name in self.thing.game.hats:
324             raise PlayError('already wearing a hat')
325         if self.thing.carrying.type_ not in {'Hat', 'Bottle'}:
326             raise PlayError('can not wear the kind of thing you hold')
327
328     def do(self):
329         if self.thing.name in self.thing.game.hats:
330             t = self.thing.game.add_thing('Hat', self.thing.position)
331             t.design = self.thing.game.hats[self.thing.name]
332             del self.thing.game.hats[self.thing.name]
333             self.thing.send_msg('CHAT "You drop your hat."')
334             for remixer in [t for t in self.thing.game.things
335                             if t.type_ == 'HatRemixer'
336                             and t.position == self.thing.position]:
337                 remixer.accept(t)
338                 break
339         else:
340             if self.thing.carrying.type_ == 'Bottle':
341                 self.thing.send_msg('CHAT "Your attempt to wear a bottle on '
342                                     'your head fails."')
343                 self.thing.carrying.sound('BOTTLE', 'SMASH')
344             elif self.thing.carrying.type_ == 'Hat':
345                 self.thing.game.hats[self.thing.name] =\
346                     self.thing.carrying.design
347                 self.thing.send_msg('CHAT "You put on a hat."')
348             dropped = self.thing.uncarry()
349             self.thing.game.remove_thing(dropped)
350         self.thing.game.record_change(self.thing.position, 'other')
351
352
353
354 class Task_SPIN(Task):
355
356     def check(self):
357         if not self.thing.carrying:
358             raise PlayError('holding nothing to spin')
359         if not hasattr(self.thing.carrying, 'spinnable'):
360             raise PlayError('held object not spinnable')
361
362     def do(self):
363         self.thing.carrying.spin()
364         self.thing.send_msg('CHAT "You spin this object."')