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