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