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