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