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
78 if self.thing.carrying.type_ == 'Crate':
79 for t in self.thing.carrying.content:
80 t.position = self.thing.position
84 class Task_WRITE(Task):
85 argtypes = 'string:char string'
88 if not self.thing.game.can_do_tile_with_pw(*self.thing.position,
90 raise GameError('wrong password for tile')
93 big_yx = self.thing.position[0]
94 little_yx = self.thing.position[1]
95 self.thing.game.maps[big_yx][little_yx] = self.args[0]
96 self.thing.game.record_change((big_yx, little_yx), 'fov')
100 class Task_FLATTEN_SURROUNDINGS(Task):
107 for yxyx in [self.thing.position] + \
108 list(self.thing.game.map_geometry.get_neighbors_yxyx(
109 self.thing.position).values()):
110 if not self.thing.game.can_do_tile_with_pw(*yxyx, self.args[0]):
112 self.thing.game.maps[yxyx[0]][yxyx[1]] = self.thing.game.get_flatland()
113 self.thing.game.record_change(yxyx, 'fov')
117 class Task_PICK_UP(Task):
121 if self.thing.carrying:
122 raise PlayError('already carrying something')
123 to_pick_up = self.thing.game.get_thing(self.args[0])
124 neighbors = self.thing.game.map_geometry.\
125 get_neighbors_yxyx(self.thing.position).values()
126 reach = [self.thing.position] + list(neighbors)
127 if to_pick_up is None:
128 raise PlayError('no such thing exists')
129 elif to_pick_up == self.thing:
130 raise PlayError('cannot pick up oneself')
131 elif to_pick_up.type_ == 'Player':
132 raise PlayError('cannot pick up player')
133 elif to_pick_up.carried:
134 raise PlayError('thing already carried by a player')
135 elif to_pick_up.position not in reach:
136 raise PlayError('thing not in reach')
137 elif not to_pick_up.portable:
138 raise PlayError('thing not portable')
141 to_pick_up = self.thing.game.get_thing(self.args[0])
142 to_pick_up.position = self.thing.position[:]
143 self.thing.carrying = to_pick_up
144 to_pick_up.carried = True
145 for t in [t for t in self.thing.game.things
146 if t.type_ == 'Crate' and to_pick_up in t.content]:
147 t.remove_from_crate(to_pick_up)
148 self.thing.send_msg('CHAT "You take the item out of the crate."')
150 self.thing.game.record_change(self.thing.position, 'other')
154 class Task_DROP(Task):
155 argtypes = 'string:direction+here'
158 if not self.thing.carrying:
159 raise PlayError('nothing to drop')
160 target_position = self._get_move_target()
161 targets = [t for t in self.thing.game.things
162 if t.position == target_position
163 and not t == self.thing.carrying]
164 for target in targets:
165 if target.type_ == 'CookieSpawner' and\
166 not self.thing.carrying.cookable:
167 raise PlayError('cannot cook items of this type')
168 elif target.type_ == 'BottleDeposit':
169 if not self.thing.carrying.type_ == 'Bottle':
170 raise PlayError('cannot only put bottle into bottle deposit')
171 if self.thing.carrying.full:
172 raise PlayError('cannot drop full '
173 'bottle into bottle deposit')
174 elif target.type_ == 'Crate' and\
175 self.thing.carrying.type_ == 'Crate':
176 raise PlayError('cannot put crate into crate')
179 target_position = self._get_move_target()
180 dropped = self.thing.uncarry()
181 dropped.position = target_position
182 if dropped.type_ == 'Crate':
183 for item in dropped.content:
184 item.position = target_position
185 targets = [t for t in self.thing.game.things
186 if t.position == dropped.position and not t == dropped]
187 for target in targets:
188 if target.type_ == 'CookieSpawner':
189 target.accept(dropped)
190 self.thing.game.remove_thing(dropped)
192 elif target.type_ == 'BottleDeposit':
194 self.thing.game.remove_thing(dropped)
196 elif target.type_ == 'Crate':
197 target.accept(dropped)
198 self.thing.send_msg('CHAT "You put the item into the crate."')
200 elif target.type_ == 'HatRemixer':
203 self.thing.game.record_change(self.thing.position, 'other')
207 class Task_DOOR(Task):
210 action_radius = list(self.thing.game.map_geometry.
211 get_neighbors_yxyx(self.thing.position).values())
212 reachable_doors = [t for t in self.thing.game.things if
213 t.type_ == 'Door' and t.position in action_radius]
214 if len(reachable_doors) == 0:
215 raise PlayError('not standing next to a door to open/close')
216 for door in reachable_doors:
217 if not door.blocks_movement:
221 if self.thing.carrying and self.thing.carrying.type_ == 'DoorKey'\
222 and self.thing.carrying.door == door:
224 raise PlayError('cannot open locked door without its key')
227 action_radius = list(self.thing.game.map_geometry.
228 get_neighbors_yxyx(self.thing.position).values())
229 for t in [t for t in self.thing.game.things if
230 t.type_ == 'Door' and t.position in action_radius]:
231 if t.blocks_movement:
235 if self.thing.carrying and\
236 self.thing.carrying.type_ == 'DoorKey' and\
237 self.thing.carrying.door == t:
238 self.thing.send_msg('CHAT "You lock the door."')
240 self.thing.game.record_change(t.position, 'other')
241 self.thing.game.record_change(t.position, 'fov')
245 class Task_INTOXICATE(Task):
248 if self.thing.carrying is None:
249 raise PlayError('carrying nothing to consume')
250 if self.thing.carrying.type_ not in {'Bottle', 'Cookie', 'Psychedelic'}:
251 raise PlayError('cannot consume this kind of thing')
252 if self.thing.carrying.type_ == 'Bottle' and\
253 not self.thing.carrying.full:
254 raise PlayError('bottle is empty')
257 if self.thing.carrying.type_ == 'Bottle':
258 self.thing.carrying.full = False
259 self.thing.carrying.empty()
260 self.thing.send_msg('CHAT "You are drunk now."')
261 self.thing.need_for_toilet += 1
262 self.thing.drunk = 10000
263 self.thing.invalidate('fov')
264 self.thing.game.record_change(self.thing.position, 'other')
265 elif self.thing.carrying.type_ == 'Psychedelic':
266 self.thing.tripping = 10000
267 self.thing.send_msg('CHAT "You start tripping."')
268 self.thing.send_msg('RANDOM_COLORS')
269 eaten = self.thing.uncarry()
270 self.thing.game.remove_thing(eaten)
271 elif self.thing.carrying.type_ == 'Cookie':
272 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))
273 self.thing.add_cookie_char(self.thing.carrying.thing_char)
274 eaten = self.thing.uncarry()
275 self.thing.game.remove_thing(eaten)
279 class Task_COMMAND(Task):
283 if self.thing.carrying is None:
284 raise PlayError('nothing to command')
285 if not self.thing.carrying.commandable:
286 raise PlayError('cannot command this item type')
289 reply_lines = self.thing.carrying.interpret(self.args[0])
290 for line in reply_lines:
291 self.thing.send_msg('REPLY ' + quote(line))
295 class Task_INSTALL(Task):
298 def _get_uninstallables(self):
299 return [t for t in self.thing.game.things
301 and hasattr(t, 'installable') and t.installable
303 and t.position == self.thing.position]
306 if not self.thing.game.can_do_tile_with_pw(*self.thing.position,
308 raise GameError('wrong password for tile')
309 if self.thing.carrying:
310 if not hasattr(self.thing.carrying, 'installable')\
311 or not self.thing.carrying.installable:
312 raise PlayError('carried thing not installable')
313 elif len(self._get_uninstallables()) == 0:
314 raise PlayError('nothing to uninstall here')
317 if self.thing.carrying:
318 t = self.thing.uncarry()
320 self.thing.send_msg('CHAT "You install the thing you carry."')
322 self._get_uninstallables()[0].uninstall()
323 self.thing.send_msg('CHAT "You uninstall the thing here."')
324 self.thing.game.record_change(self.thing.position, 'other')
328 class Task_WEAR(Task):
331 if self.thing.name in self.thing.game.hats:
333 if not self.thing.carrying:
334 raise PlayError('carrying nothing to wear')
335 if self.thing.name in self.thing.game.hats:
336 raise PlayError('already wearing a hat')
337 if self.thing.carrying.type_ not in {'Hat', 'Bottle'}:
338 raise PlayError('can not wear the kind of thing you hold')
341 if self.thing.name in self.thing.game.hats:
342 t = self.thing.game.add_thing('Hat', self.thing.position)
343 t.design = self.thing.game.hats[self.thing.name]
344 del self.thing.game.hats[self.thing.name]
345 self.thing.send_msg('CHAT "You drop your hat."')
346 for remixer in [t for t in self.thing.game.things
347 if t.type_ == 'HatRemixer'
348 and t.position == self.thing.position]:
352 if self.thing.carrying.type_ == 'Bottle':
353 self.thing.send_msg('CHAT "Your attempt to wear a bottle on '
355 self.thing.carrying.sound('BOTTLE', 'SMASH')
356 elif self.thing.carrying.type_ == 'Hat':
357 self.thing.game.hats[self.thing.name] =\
358 self.thing.carrying.design
359 self.thing.send_msg('CHAT "You put on a hat."')
360 dropped = self.thing.uncarry()
361 self.thing.game.remove_thing(dropped)
362 self.thing.game.record_change(self.thing.position, 'other')
366 class Task_SPIN(Task):
369 if not self.thing.carrying:
370 raise PlayError('holding nothing to spin')
371 if not hasattr(self.thing.carrying, 'spinnable'):
372 raise PlayError('held object not spinnable')
375 self.thing.carrying.spin()
376 self.thing.send_msg('CHAT "You spin this object."')