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')
158 for t in [t for t in self.thing.game.things
159 if t.type_ == 'CookieSpawner'
160 and t.position == target_position]:
161 if not self.thing.carrying.cookable:
162 raise PlayError('cannot cook items of this type')
166 target_position = self._get_move_target()
167 dropped = self.thing.uncarry()
168 dropped.position = target_position
169 over_cookie_spawner = None
170 for t in [t for t in self.thing.game.things
171 if t.type_ == 'CookieSpawner'
172 and t.position == dropped.position]:
173 over_cookie_spawner = t
175 if over_cookie_spawner:
176 over_cookie_spawner.accept(dropped)
177 self.thing.game.remove_thing(dropped)
178 elif dropped.type_ == 'Bottle' and not dropped.full:
179 for t in [t for t in self.thing.game.things
180 if t.type_ == 'BottleDeposit'
181 and t.position == dropped.position]:
183 self.thing.game.remove_thing(dropped)
185 elif dropped.type_ == 'Hat':
186 for t in [t for t in self.thing.game.things
187 if t.type_ == 'HatRemixer'
188 and t.position == dropped.position]:
191 self.thing.game.record_change(self.thing.position, 'other')
195 class Task_DOOR(Task):
198 action_radius = list(self.thing.game.map_geometry.
199 get_neighbors_yxyx(self.thing.position).values())
200 reachable_doors = [t for t in self.thing.game.things if
201 t.type_ == 'Door' and t.position in action_radius]
202 if len(reachable_doors) == 0:
203 raise PlayError('not standing next to a door to open/close')
204 for door in reachable_doors:
205 if not door.blocks_movement:
209 if self.thing.carrying and self.thing.carrying.type_ == 'DoorKey'\
210 and self.thing.carrying.door == door:
212 raise PlayError('cannot open locked door without its key')
215 action_radius = list(self.thing.game.map_geometry.
216 get_neighbors_yxyx(self.thing.position).values())
217 for t in [t for t in self.thing.game.things if
218 t.type_ == 'Door' and t.position in action_radius]:
219 if t.blocks_movement:
223 if self.thing.carrying and\
224 self.thing.carrying.type_ == 'DoorKey' and\
225 self.thing.carrying.door == t:
226 self.thing.send_msg('CHAT "You lock the door."')
228 self.thing.game.record_change(t.position, 'other')
229 self.thing.game.record_change(t.position, 'fov')
233 class Task_INTOXICATE(Task):
236 if self.thing.carrying is None:
237 raise PlayError('carrying nothing to consume')
238 if self.thing.carrying.type_ not in {'Bottle', 'Cookie', 'Psychedelic'}:
239 raise PlayError('cannot consume this kind of thing')
240 if self.thing.carrying.type_ == 'Bottle' and\
241 not self.thing.carrying.full:
242 raise PlayError('bottle is empty')
245 if self.thing.carrying.type_ == 'Bottle':
246 self.thing.carrying.full = False
247 self.thing.carrying.empty()
248 self.thing.send_msg('CHAT "You are drunk now."')
249 self.thing.need_for_toilet += 1
250 self.thing.drunk = 10000
251 self.thing.invalidate('fov')
252 self.thing.game.record_change(self.thing.position, 'other')
253 elif self.thing.carrying.type_ == 'Psychedelic':
254 self.thing.tripping = 10000
255 self.thing.send_msg('CHAT "You start tripping."')
256 self.thing.send_msg('RANDOM_COLORS')
257 eaten = self.thing.uncarry()
258 self.thing.game.remove_thing(eaten)
259 elif self.thing.carrying.type_ == 'Cookie':
260 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))
261 self.thing.add_cookie_char(self.thing.carrying.thing_char)
262 eaten = self.thing.uncarry()
263 self.thing.game.remove_thing(eaten)
267 class Task_COMMAND(Task):
271 if self.thing.carrying is None:
272 raise PlayError('nothing to command')
273 if not self.thing.carrying.commandable:
274 raise PlayError('cannot command this item type')
277 reply_lines = self.thing.carrying.interpret(self.args[0])
278 for line in reply_lines:
279 self.thing.send_msg('REPLY ' + quote(line))
283 class Task_INSTALL(Task):
286 def _get_uninstallables(self):
287 return [t for t in self.thing.game.things
289 and hasattr(t, 'installable') and t.installable
291 and t.position == self.thing.position]
294 if not self.thing.game.can_do_tile_with_pw(*self.thing.position,
296 raise GameError('wrong password for tile')
297 if self.thing.carrying:
298 if not hasattr(self.thing.carrying, 'installable')\
299 or not self.thing.carrying.installable:
300 raise PlayError('carried thing not installable')
301 elif len(self._get_uninstallables()) == 0:
302 raise PlayError('nothing to uninstall here')
305 if self.thing.carrying:
306 t = self.thing.uncarry()
308 self.thing.send_msg('CHAT "You install the thing you carry."')
310 self._get_uninstallables()[0].uninstall()
311 self.thing.send_msg('CHAT "You uninstall the thing here."')
312 self.thing.game.record_change(self.thing.position, 'other')
316 class Task_WEAR(Task):
319 if self.thing.name in self.thing.game.hats:
321 if not self.thing.carrying:
322 raise PlayError('carrying nothing to wear')
323 if self.thing.name in self.thing.game.hats:
324 raise PlayError('already wearing a hat')
325 if self.thing.carrying.type_ not in {'Hat', 'Bottle'}:
326 raise PlayError('can not wear the kind of thing you hold')
329 if self.thing.name in self.thing.game.hats:
330 t = self.thing.game.add_thing('Hat', self.thing.position)
331 t.design = self.thing.game.hats[self.thing.name]
332 del self.thing.game.hats[self.thing.name]
333 self.thing.send_msg('CHAT "You drop your hat."')
334 for remixer in [t for t in self.thing.game.things
335 if t.type_ == 'HatRemixer'
336 and t.position == self.thing.position]:
340 if self.thing.carrying.type_ == 'Bottle':
341 self.thing.send_msg('CHAT "Your attempt to wear a bottle on '
343 self.thing.carrying.sound('BOTTLE', 'SMASH')
344 elif self.thing.carrying.type_ == 'Hat':
345 self.thing.game.hats[self.thing.name] =\
346 self.thing.carrying.design
347 self.thing.send_msg('CHAT "You put on a hat."')
348 dropped = self.thing.uncarry()
349 self.thing.game.remove_thing(dropped)
350 self.thing.game.record_change(self.thing.position, 'other')
354 class Task_SPIN(Task):
357 if not self.thing.carrying:
358 raise PlayError('holding nothing to spin')
359 if not hasattr(self.thing.carrying, 'spinnable'):
360 raise PlayError('held object not spinnable')
363 self.thing.carrying.spin()
364 self.thing.send_msg('CHAT "You spin this object."')