home · contact · privacy
8af4bb957f683227decdcd2f2066a79a766004e5
[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.blocking]:
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.blocking:
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         if self.thing.blocking:
51             self.thing.game.record_change(self.thing.position, 'fov')
52         if self.thing.carrying:
53             self.thing.carrying.position = self.thing.position
54
55
56
57 class Task_WRITE(Task):
58     argtypes = 'string:char string'
59
60     def check(self):
61         if not self.thing.game.can_do_tile_with_pw(*self.thing.position,
62                                                    self.args[1]):
63             raise GameError('wrong password for tile')
64
65     def do(self):
66         big_yx = self.thing.position[0]
67         little_yx = self.thing.position[1]
68         self.thing.game.maps[big_yx][little_yx] = self.args[0]
69         self.thing.game.record_change((big_yx, little_yx), 'fov')
70
71
72
73 class Task_FLATTEN_SURROUNDINGS(Task):
74     argtypes = 'string'
75
76     def check(self):
77         pass
78
79     def do(self):
80         for yxyx in [self.thing.position] + \
81                 list(self.thing.game.map_geometry.get_neighbors_yxyx(
82                     self.thing.position).values()):
83             if not self.thing.game.can_do_tile_with_pw(*yxyx, self.args[0]):
84                 continue
85             self.thing.game.maps[yxyx[0]][yxyx[1]] = self.thing.game.get_flatland()
86             self.thing.game.record_change(yxyx, 'fov')
87
88
89
90 class Task_PICK_UP(Task):
91     argtypes = 'int:pos'
92
93     def check(self):
94         if self.thing.carrying:
95             raise PlayError('already carrying something')
96         to_pick_up = self.thing.game.get_thing(self.args[0])
97         neighbors = self.thing.game.map_geometry.\
98             get_neighbors_yxyx(self.thing.position).values()
99         reach = [self.thing.position] + list(neighbors)
100         if to_pick_up is None:
101             raise PlayError('no such thing exists')
102         elif to_pick_up == self.thing:
103             raise PlayError('cannot pick up oneself')
104         elif to_pick_up.type_ == 'Player':
105             raise PlayError('cannot pick up player')
106         elif to_pick_up.carried:
107             raise PlayError('thing already carried by a player')
108         elif to_pick_up.position not in reach:
109             raise PlayError('thing not in reach')
110         elif not to_pick_up.portable:
111             raise PlayError('thing not portable')
112
113     def do(self):
114         to_pick_up = self.thing.game.get_thing(self.args[0])
115         to_pick_up.position = self.thing.position[:]
116         self.thing.carrying = to_pick_up
117         to_pick_up.carried = True
118         self.thing.game.record_change(self.thing.position, 'other')
119
120
121
122 class Task_DROP(Task):
123     argtypes = 'string:direction+here'
124
125     def check(self):
126         if not self.thing.carrying:
127             raise PlayError('nothing to drop')
128         target_position = self._get_move_target()
129         if self.thing.carrying.type_ == 'Bottle' and self.thing.carrying.full:
130             for t in [t for t in self.thing.game.things
131                       if t.type_ == 'BottleDeposit'
132                       and t.position == target_position]:
133                 raise PlayError('cannot drop full bottle into bottle deposit')
134
135     def do(self):
136         target_position = self._get_move_target()
137         dropped = self.thing.uncarry()
138         dropped.position = target_position
139         over_cookie_spawner = None
140         for t in [t for t in self.thing.game.things
141                   if t.type_ == 'CookieSpawner'
142                   and t.position == dropped.position]:
143             over_cookie_spawner = t
144             break
145         if over_cookie_spawner:
146             over_cookie_spawner.accept(dropped)
147             self.thing.game.remove_thing(dropped)
148         elif dropped.type_ == 'Bottle' and not dropped.full:
149             for t in [t for t in self.thing.game.things
150                       if t.type_ == 'BottleDeposit'
151                       and t.position == dropped.position]:
152                 t.accept()
153                 self.thing.game.remove_thing(dropped)
154                 break
155         elif dropped.type_ == 'Hat':
156             for t in [t for t in self.thing.game.things
157                       if t.type_ == 'HatRemixer'
158                       and t.position == dropped.position]:
159                 t.accept(dropped)
160                 break
161         self.thing.game.record_change(self.thing.position, 'other')
162
163
164
165 class Task_DOOR(Task):
166
167     def do(self):
168         action_radius = list(self.thing.game.map_geometry.
169                              get_neighbors_yxyx(self.thing.position).values())
170         for t in [t for t in self.thing.game.things if
171                   t.type_ == 'Door' and t.position in action_radius]:
172             if t.blocking:
173                 t.open()
174             else:
175                 t.close()
176             self.thing.game.record_change(t.position, 'other')
177             self.thing.game.record_change(t.position, 'fov')
178
179
180
181 class Task_INTOXICATE(Task):
182
183     def check(self):
184         if self.thing.carrying is None:
185             raise PlayError('carrying nothing to drink from')
186         if self.thing.carrying.type_ not in {'Bottle', 'Cookie'}:
187             raise PlayError('cannot consume this kind of thing')
188         if self.thing.carrying.type_ == 'Bottle' and\
189            not self.thing.carrying.full:
190             raise PlayError('bottle is empty')
191
192     def do(self):
193         if self.thing.carrying.type_ == 'Bottle':
194             self.thing.carrying.full = False
195             self.thing.carrying.empty()
196             self.thing.send_msg('RANDOM_COLORS')
197             self.thing.send_msg('CHAT "You are drunk now."')
198             self.thing.drunk = 10000
199             self.thing.invalidate('fov')
200             self.thing.game.record_change(self.thing.position, 'other')
201         elif self.thing.carrying.type_ == 'Cookie':
202             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))
203             self.thing.add_cookie_char(self.thing.carrying.thing_char)
204             eaten = self.thing.uncarry()
205             self.thing.game.remove_thing(eaten)
206
207
208
209 class Task_COMMAND(Task):
210     argtypes = 'string'
211
212     def check(self):
213         if self.thing.carrying is None:
214             raise PlayError('nothing to command')
215         if not self.thing.carrying.commandable:
216             raise PlayError('cannot command this item type')
217
218     def do(self):
219         reply_lines = self.thing.carrying.interpret(self.args[0])
220         for line in reply_lines:
221             self.thing.send_msg('REPLY ' + quote(line))
222
223
224
225 class Task_INSTALL(Task):
226     argtypes = 'string'
227
228     def _get_uninstallables(self):
229         return [t for t in self.thing.game.things
230                 if t != self.thing
231                 and hasattr(t, 'installable') and t.installable
232                 and (not t.portable)
233                 and t.position == self.thing.position]
234
235     def check(self):
236         if not self.thing.game.can_do_tile_with_pw(*self.thing.position,
237                                                    self.args[0]):
238             raise GameError('wrong password for tile')
239         if self.thing.carrying:
240             if not hasattr(self.thing.carrying, 'installable')\
241                or not self.thing.carrying.installable:
242                 raise PlayError('carried thing not installable')
243         elif len(self._get_uninstallables()) == 0:
244             raise PlayError('nothing to uninstall here')
245
246     def do(self):
247         if self.thing.carrying:
248             t = self.thing.uncarry()
249             t.install()
250             self.thing.send_msg('CHAT "You install the thing you carry."')
251         else:
252             self._get_uninstallables()[0].uninstall()
253             self.thing.send_msg('CHAT "You uninstall the thing here."')
254         self.thing.game.record_change(self.thing.position, 'other')
255
256
257
258 class Task_WEAR(Task):
259
260     def check(self):
261         if self.thing.name in self.thing.game.hats:
262             return
263         if not self.thing.carrying:
264             raise PlayError('carrying nothing to wear')
265         if self.thing.name in self.thing.game.hats:
266             raise PlayError('already wearing a hat')
267         if self.thing.carrying.type_ not in {'Hat', 'Bottle'}:
268             raise PlayError('can not wear the kind of thing you hold')
269
270     def do(self):
271         if self.thing.name in self.thing.game.hats:
272             t = self.thing.game.add_thing('Hat', self.thing.position)
273             t.design = self.thing.game.hats[self.thing.name]
274             del self.thing.game.hats[self.thing.name]
275             self.thing.send_msg('CHAT "You drop your hat."')
276             for remixer in [t for t in self.thing.game.things
277                             if t.type_ == 'HatRemixer'
278                             and t.position == self.thing.position]:
279                 remixer.accept(t)
280                 break
281         else:
282             if self.thing.carrying.type_ == 'Bottle':
283                 self.thing.send_msg('CHAT "Your attempt to wear a bottle on '
284                                     'your head fails."')
285                 self.thing.carrying.sound('BOTTLE', 'SMASH')
286             elif self.thing.carrying.type_ == 'Hat':
287                 self.thing.game.hats[self.thing.name] =\
288                     self.thing.carrying.design
289                 self.thing.send_msg('CHAT "You put on a hat."')
290             dropped = self.thing.uncarry()
291             self.thing.game.remove_thing(dropped)
292         self.thing.game.record_change(self.thing.position, 'other')
293
294
295
296 class Task_SPIN(Task):
297
298     def check(self):
299         if not self.thing.carrying:
300             raise PlayError('holding nothing to spin')
301         if not hasattr(self.thing.carrying, 'spinnable'):
302             raise PlayError('held object not spinnable')
303
304     def do(self):
305         self.thing.carrying.spin()
306         self.thing.send_msg('CHAT "You spin this object."')