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