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