1 from plomrogue.errors import PlayError, GameError
2 from plomrogue.misc import quote
10 def __init__(self, thing, args=()):
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,
25 class Task_WAIT(Task):
32 class Task_MOVE(Task):
33 argtypes = 'string:direction'
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')
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 for t in [t for t in self.thing.game.things
53 if t.type_ == 'Player' and not t == self.thing
54 and t.position == self.thing.position]:
55 self.thing.send_msg('CHAT %s' %
56 quote('You get awkwardly close to %s.' % t.name))
57 for c_id in self.thing.game.sessions:
58 if self.thing.game.sessions[c_id]['thing_id'] == t.id_:
59 t.send_msg('CHAT %s' %
60 quote('%s gets awkwardly close to you.' %
63 self.thing.game.record_change(self.thing.position, 'other')
65 self.thing.game.maps[self.thing.position[0]][self.thing.position[1]]
66 if terrain in self.thing.game.terrains:
67 terrain_type = self.thing.game.terrains[terrain]
68 if 'sittable' in terrain_type.tags:
69 self.thing.standing = False
70 self.thing.send_msg('CHAT "You sink into the %s.'
71 'Staying here will reduce your weariness."'
72 % terrain_type.description)
73 self.thing.invalidate('fov')
74 if self.thing.blocks_light:
75 self.thing.game.record_change(self.thing.position, 'fov')
76 if self.thing.carrying:
77 self.thing.carrying.position = self.thing.position
81 class Task_WRITE(Task):
82 argtypes = 'string:char string'
85 if not self.thing.game.can_do_tile_with_pw(*self.thing.position,
87 raise GameError('wrong password for tile')
90 big_yx = self.thing.position[0]
91 little_yx = self.thing.position[1]
92 self.thing.game.maps[big_yx][little_yx] = self.args[0]
93 self.thing.game.record_change((big_yx, little_yx), 'fov')
97 class Task_FLATTEN_SURROUNDINGS(Task):
104 for yxyx in [self.thing.position] + \
105 list(self.thing.game.map_geometry.get_neighbors_yxyx(
106 self.thing.position).values()):
107 if not self.thing.game.can_do_tile_with_pw(*yxyx, self.args[0]):
109 self.thing.game.maps[yxyx[0]][yxyx[1]] = self.thing.game.get_flatland()
110 self.thing.game.record_change(yxyx, 'fov')
114 class Task_PICK_UP(Task):
118 if self.thing.carrying:
119 raise PlayError('already carrying something')
120 to_pick_up = self.thing.game.get_thing(self.args[0])
121 neighbors = self.thing.game.map_geometry.\
122 get_neighbors_yxyx(self.thing.position).values()
123 reach = [self.thing.position] + list(neighbors)
124 if to_pick_up is None:
125 raise PlayError('no such thing exists')
126 elif to_pick_up == self.thing:
127 raise PlayError('cannot pick up oneself')
128 elif to_pick_up.type_ == 'Player':
129 raise PlayError('cannot pick up player')
130 elif to_pick_up.carried:
131 raise PlayError('thing already carried by a player')
132 elif to_pick_up.position not in reach:
133 raise PlayError('thing not in reach')
134 elif not to_pick_up.portable:
135 raise PlayError('thing not portable')
138 to_pick_up = self.thing.game.get_thing(self.args[0])
139 to_pick_up.position = self.thing.position[:]
140 self.thing.carrying = to_pick_up
141 to_pick_up.carried = True
142 self.thing.game.record_change(self.thing.position, 'other')
146 class Task_DROP(Task):
147 argtypes = 'string:direction+here'
150 if not self.thing.carrying:
151 raise PlayError('nothing to drop')
152 target_position = self._get_move_target()
153 if self.thing.carrying.type_ == 'Bottle' and self.thing.carrying.full:
154 for t in [t for t in self.thing.game.things
155 if t.type_ == 'BottleDeposit'
156 and t.position == target_position]:
157 raise PlayError('cannot drop full bottle into bottle deposit')
160 target_position = self._get_move_target()
161 dropped = self.thing.uncarry()
162 dropped.position = target_position
163 over_cookie_spawner = None
164 for t in [t for t in self.thing.game.things
165 if t.type_ == 'CookieSpawner'
166 and t.position == dropped.position]:
167 over_cookie_spawner = t
169 if over_cookie_spawner:
170 over_cookie_spawner.accept(dropped)
171 self.thing.game.remove_thing(dropped)
172 elif dropped.type_ == 'Bottle' and not dropped.full:
173 for t in [t for t in self.thing.game.things
174 if t.type_ == 'BottleDeposit'
175 and t.position == dropped.position]:
177 self.thing.game.remove_thing(dropped)
179 elif dropped.type_ == 'Hat':
180 for t in [t for t in self.thing.game.things
181 if t.type_ == 'HatRemixer'
182 and t.position == dropped.position]:
185 self.thing.game.record_change(self.thing.position, 'other')
189 class Task_DOOR(Task):
192 action_radius = list(self.thing.game.map_geometry.
193 get_neighbors_yxyx(self.thing.position).values())
194 reachable_doors = [t for t in self.thing.game.things if
195 t.type_ == 'Door' and t.position in action_radius]
196 if len(reachable_doors) == 0:
197 raise PlayError('not standing next to a door to open/close')
198 for door in reachable_doors:
199 if not door.blocks_movement:
203 if self.thing.carrying and self.thing.carrying.type_ == 'DoorKey'\
204 and self.thing.carrying.door == door:
206 raise PlayError('cannot open locked door without its key')
209 action_radius = list(self.thing.game.map_geometry.
210 get_neighbors_yxyx(self.thing.position).values())
211 for t in [t for t in self.thing.game.things if
212 t.type_ == 'Door' and t.position in action_radius]:
213 if t.blocks_movement:
217 if self.thing.carrying and\
218 self.thing.carrying.type_ == 'DoorKey' and\
219 self.thing.carrying.door == t:
220 self.thing.send_msg('CHAT "You lock the door."')
222 self.thing.game.record_change(t.position, 'other')
223 self.thing.game.record_change(t.position, 'fov')
227 class Task_INTOXICATE(Task):
230 if self.thing.carrying is None:
231 raise PlayError('carrying nothing to consume')
232 if self.thing.carrying.type_ not in {'Bottle', 'Cookie', 'Psychedelic'}:
233 raise PlayError('cannot consume this kind of thing')
234 if self.thing.carrying.type_ == 'Bottle' and\
235 not self.thing.carrying.full:
236 raise PlayError('bottle is empty')
239 if self.thing.carrying.type_ == 'Bottle':
240 self.thing.carrying.full = False
241 self.thing.carrying.empty()
242 self.thing.send_msg('CHAT "You are drunk now."')
243 self.thing.need_for_toilet += 1
244 self.thing.drunk = 10000
245 self.thing.invalidate('fov')
246 self.thing.game.record_change(self.thing.position, 'other')
247 elif self.thing.carrying.type_ == 'Psychedelic':
248 self.thing.tripping = 10000
249 self.thing.send_msg('CHAT "You start tripping."')
250 self.thing.send_msg('RANDOM_COLORS')
251 eaten = self.thing.uncarry()
252 self.thing.game.remove_thing(eaten)
253 elif self.thing.carrying.type_ == 'Cookie':
254 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))
255 self.thing.add_cookie_char(self.thing.carrying.thing_char)
256 eaten = self.thing.uncarry()
257 self.thing.game.remove_thing(eaten)
261 class Task_COMMAND(Task):
265 if self.thing.carrying is None:
266 raise PlayError('nothing to command')
267 if not self.thing.carrying.commandable:
268 raise PlayError('cannot command this item type')
271 reply_lines = self.thing.carrying.interpret(self.args[0])
272 for line in reply_lines:
273 self.thing.send_msg('REPLY ' + quote(line))
277 class Task_INSTALL(Task):
280 def _get_uninstallables(self):
281 return [t for t in self.thing.game.things
283 and hasattr(t, 'installable') and t.installable
285 and t.position == self.thing.position]
288 if not self.thing.game.can_do_tile_with_pw(*self.thing.position,
290 raise GameError('wrong password for tile')
291 if self.thing.carrying:
292 if not hasattr(self.thing.carrying, 'installable')\
293 or not self.thing.carrying.installable:
294 raise PlayError('carried thing not installable')
295 elif len(self._get_uninstallables()) == 0:
296 raise PlayError('nothing to uninstall here')
299 if self.thing.carrying:
300 t = self.thing.uncarry()
302 self.thing.send_msg('CHAT "You install the thing you carry."')
304 self._get_uninstallables()[0].uninstall()
305 self.thing.send_msg('CHAT "You uninstall the thing here."')
306 self.thing.game.record_change(self.thing.position, 'other')
310 class Task_WEAR(Task):
313 if self.thing.name in self.thing.game.hats:
315 if not self.thing.carrying:
316 raise PlayError('carrying nothing to wear')
317 if self.thing.name in self.thing.game.hats:
318 raise PlayError('already wearing a hat')
319 if self.thing.carrying.type_ not in {'Hat', 'Bottle'}:
320 raise PlayError('can not wear the kind of thing you hold')
323 if self.thing.name in self.thing.game.hats:
324 t = self.thing.game.add_thing('Hat', self.thing.position)
325 t.design = self.thing.game.hats[self.thing.name]
326 del self.thing.game.hats[self.thing.name]
327 self.thing.send_msg('CHAT "You drop your hat."')
328 for remixer in [t for t in self.thing.game.things
329 if t.type_ == 'HatRemixer'
330 and t.position == self.thing.position]:
334 if self.thing.carrying.type_ == 'Bottle':
335 self.thing.send_msg('CHAT "Your attempt to wear a bottle on '
337 self.thing.carrying.sound('BOTTLE', 'SMASH')
338 elif self.thing.carrying.type_ == 'Hat':
339 self.thing.game.hats[self.thing.name] =\
340 self.thing.carrying.design
341 self.thing.send_msg('CHAT "You put on a hat."')
342 dropped = self.thing.uncarry()
343 self.thing.game.remove_thing(dropped)
344 self.thing.game.record_change(self.thing.position, 'other')
348 class Task_SPIN(Task):
351 if not self.thing.carrying:
352 raise PlayError('holding nothing to spin')
353 if not hasattr(self.thing.carrying, 'spinnable'):
354 raise PlayError('held object not spinnable')
357 self.thing.carrying.spin()
358 self.thing.send_msg('CHAT "You spin this object."')