home · contact · privacy
Disallow picking up thing already carried by other player.
[plomrogue2] / plomrogue / tasks.py
1 from plomrogue.errors import PlayError, GameError
2
3
4
5 class Task:
6     argtypes = ''
7     todo = 1
8
9     def __init__(self, thing, args=()):
10         self.thing = thing
11         self.args = args
12
13     def check(self):
14         pass
15
16
17
18 class Task_WAIT(Task):
19
20     def do(self):
21         pass
22
23
24
25 class Task_MOVE(Task):
26     argtypes = 'string:direction'
27
28     def get_move_target(self):
29         return self.thing.game.map_geometry.move_yxyx(self.thing.position,
30                                                       self.args[0])
31
32     def check(self):
33         test_yxyx = self.get_move_target()
34         if test_yxyx in [t.position for t in self.thing.game.things
35                          if t.blocking]:
36             raise PlayError('blocked by other thing')
37         elif self.thing.game.maps[test_yxyx[0]][test_yxyx[1]] != '.':
38             raise PlayError('blocked by impassable tile')
39
40     def do(self):
41         self.thing.game.record_fov_change(self.thing.position)
42         self.thing.position = self.get_move_target()
43         self.thing.game.record_fov_change(self.thing.position)
44         if self.thing.carrying:
45             self.thing.carrying.position = self.thing.position
46
47
48
49 class Task_WRITE(Task):
50     argtypes = 'string:char string'
51
52     def check(self):
53         if not self.thing.game.can_do_tile_with_pw(*self.thing.position,
54                                                    self.args[1]):
55             raise GameError('wrong password for tile')
56
57     def do(self):
58         big_yx = self.thing.position[0]
59         little_yx = self.thing.position[1]
60         self.thing.game.maps[big_yx][little_yx] = self.args[0]
61         self.thing.game.record_fov_change((big_yx, little_yx))
62
63
64
65 class Task_FLATTEN_SURROUNDINGS(Task):
66     argtypes = 'string'
67
68     def check(self):
69         pass
70
71     def do(self):
72         for yxyx in [self.thing.position] + \
73                 list(self.thing.game.map_geometry.get_neighbors_yxyx(
74                     self.thing.position).values()):
75             if not self.thing.game.can_do_tile_with_pw(*yxyx, self.args[0]):
76                 continue
77             self.thing.game.maps[yxyx[0]][yxyx[1]] = '.'
78             self.thing.game.record_fov_change(yxyx)
79
80
81
82 class Task_PICK_UP(Task):
83     argtypes = 'int:pos'
84
85     def check(self):
86         if self.thing.carrying:
87             raise PlayError('already carrying something')
88         to_pick_up = self.thing.game.get_thing(self.args[0])
89         neighbors = self.thing.game.map_geometry.\
90             get_neighbors_yxyx(self.thing.position).values()
91         reach = [self.thing.position] + list(neighbors)
92         if to_pick_up is None:
93             raise PlayError('no such thing exists')
94         elif to_pick_up == self.thing:
95             raise PlayError('cannot pick up oneself')
96         elif to_pick_up.type_ == 'Player':
97             raise PlayError('cannot pick up player')
98         elif to_pick_up.carried:
99             raise PlayError('thing already carried by a player')
100         elif to_pick_up.position not in reach:
101             raise PlayError('thing not in reach')
102         elif not to_pick_up.portable:
103             raise PlayError('thing not portable')
104
105     def do(self):
106         to_pick_up = self.thing.game.get_thing(self.args[0])
107         to_pick_up.position = self.thing.position[:]
108         self.thing.carrying = to_pick_up
109         to_pick_up.carried = True
110
111
112
113 class Task_DROP(Task):
114
115     def check(self):
116         if not self.thing.carrying:
117             raise PlayError('nothing to drop')
118         if self.thing.carrying.type_ == 'Bottle' and self.thing.carrying.full:
119             for t in [t for t in self.thing.game.things
120                       if t.type_ == 'BottleDeposit'
121                       and t.position == self.thing.position]:
122                 raise PlayError('cannot drop full bottle into bottle deposit')
123
124     def do(self):
125         dropped = self.thing.uncarry()
126         if dropped.type_ == 'Bottle' and not dropped.full:
127             for t in [t for t in self.thing.game.things
128                       if t.type_ == 'BottleDeposit'
129                       and t.position == self.thing.position]:
130                 t.accept()
131                 self.thing.game.remove_thing(dropped)
132                 break
133         elif dropped.type_ == 'Hat':
134             for t in [t for t in self.thing.game.things
135                       if t.type_ == 'HatRemixer'
136                       and t.position == self.thing.position]:
137                 t.accept(dropped)
138                 break
139
140
141
142 class Task_DOOR(Task):
143
144     def do(self):
145         action_radius = list(self.thing.game.map_geometry.
146                              get_neighbors_yxyx(self.thing.position).values())
147         for t in [t for t in self.thing.game.things if
148                   t.type_ == 'Door' and t.position in action_radius]:
149             if t.blocking:
150                 t.open()
151             else:
152                 t.close()
153             self.thing.game.record_fov_change(t.position)
154
155
156
157 class Task_INTOXICATE(Task):
158
159     def check(self):
160         if self.thing.carrying is None:
161             raise PlayError('carrying nothing to drink from')
162         if self.thing.carrying.type_ != 'Bottle':
163             raise PlayError('cannot drink from non-bottle')
164         if not self.thing.carrying.full:
165             raise PlayError('bottle is empty')
166
167     def do(self):
168         self.thing.carrying.full = False
169         self.thing.carrying.empty()
170         self.thing.send_msg('RANDOM_COLORS')
171         self.thing.send_msg('CHAT "You are drunk now."')
172         self.thing.drunk = 10000
173         self.thing.invalidate_map_view()
174
175
176
177 class Task_COMMAND(Task):
178     argtypes = 'string'
179
180     def check(self):
181         if self.thing.carrying is None:
182             raise PlayError('nothing to command')
183         if not self.thing.carrying.commandable:
184             raise PlayError('cannot command this item type')
185
186     def do(self):
187         from plomrogue.misc import quote
188         reply_lines = self.thing.carrying.interpret(self.args[0])
189         for line in reply_lines:
190             self.thing.send_msg('REPLY ' + quote(line))
191
192
193
194 class Task_INSTALL(Task):
195
196     def _get_uninstallables(self):
197         return [t for t in self.thing.game.things
198                 if t != self.thing
199                 and hasattr(t, 'installable') and t.installable
200                 and (not t.portable)
201                 and t.position == self.thing.position]
202
203     def check(self):
204         if self.thing.carrying:
205             if not hasattr(self.thing.carrying, 'installable')\
206                or not self.thing.carrying.installable:
207                 raise PlayError('carried thing not installable')
208         elif len(self._get_uninstallables()) == 0:
209             raise PlayError('nothing to uninstall here')
210
211     def do(self):
212         if self.thing.carrying:
213             t = self.thing.uncarry()
214             t.install()
215             self.thing.send_msg('CHAT "You install the thing you carry."')
216         else:
217             self._get_uninstallables()[0].uninstall()
218             self.thing.send_msg('CHAT "You uninstall the thing here."')
219
220
221
222 class Task_WEAR(Task):
223
224     def check(self):
225         if self.thing.name in self.thing.game.hats:
226             return
227         if not self.thing.carrying:
228             raise PlayError('carrying nothing to wear')
229         if self.thing.name in self.thing.game.hats:
230             raise PlayError('already wearing a hat')
231         if self.thing.carrying.type_ != 'Hat':
232             raise PlayError('can only wear a hat')
233
234     def do(self):
235         if self.thing.name in self.thing.game.hats:
236             t = self.thing.game.add_thing('Hat', self.thing.position)
237             t.design = self.thing.game.hats[self.thing.name]
238             del self.thing.game.hats[self.thing.name]
239             self.thing.send_msg('CHAT "You drop your hat."')
240             for remixer in [t for t in self.thing.game.things
241                             if t.type_ == 'HatRemixer'
242                             and t.position == self.thing.position]:
243                 remixer.accept(t)
244                 break
245         else:
246             from plomrogue.misc import quote
247             self.thing.game.hats[self.thing.name] = self.thing.carrying.design
248             self.thing.game.remove_thing(self.thing.carrying)
249             self.thing.carrying = None
250             self.thing.send_msg('CHAT "You put on a hat."')