home · contact · privacy
Update FOVs on dropping Things in a way that removes them.
[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.things.remove(self.thing.carrying)
127                 self.thing.game.changed_fovs = True
128                 break
129         elif self.thing.carrying.type_ == 'Hat':
130             for t in [t for t in self.thing.game.things
131                       if t.type_ == 'HatRemixer'
132                       and t.position == self.thing.position]:
133                 t.accept(self.thing.carrying)
134                 self.thing.game.changed_fovs = True
135                 break
136         self.thing.carrying = None
137
138
139
140 class Task_DOOR(Task):
141
142     def do(self):
143         self.thing.carrying = None
144         action_radius = list(self.thing.game.map_geometry.
145                              get_neighbors_yxyx(self.thing.position).values())
146         for t in [t for t in self.thing.game.things if
147                   t.type_ == 'Door' and t.position in action_radius]:
148             if t.blocking:
149                 t.open()
150             else:
151                 t.close()
152         self.thing.game.changed_fovs = True
153
154
155
156 class Task_INTOXICATE(Task):
157
158     def check(self):
159         if self.thing.carrying is None:
160             raise PlayError('carrying nothing to drink from')
161         if self.thing.carrying.type_ != 'Bottle':
162             raise PlayError('cannot drink from non-bottle')
163         if not self.thing.carrying.full:
164             raise PlayError('bottle is empty')
165
166     def do(self):
167         self.thing.carrying.full = False
168         self.thing.carrying.empty()
169         self.thing.send_msg('RANDOM_COLORS')
170         self.thing.send_msg('CHAT "You are drunk now."')
171         self.thing.drunk = 10000
172         self.thing.game.changed_fovs = True
173
174
175
176 class Task_COMMAND(Task):
177     argtypes = 'string'
178
179     def check(self):
180         if self.thing.carrying is None:
181             raise PlayError('nothing to command')
182         if not self.thing.carrying.commandable:
183             raise PlayError('cannot command this item type')
184
185     def do(self):
186         from plomrogue.misc import quote
187         reply_lines = self.thing.carrying.interpret(self.args[0])
188         for line in reply_lines:
189             self.thing.send_msg('REPLY ' + quote(line))
190
191
192
193 class Task_INSTALL(Task):
194
195     def _get_uninstallables(self):
196         return [t for t in self.thing.game.things
197                 if t != self.thing
198                 and hasattr(t, 'installable') and t.installable
199                 and (not t.portable)
200                 and t.position == self.thing.position]
201
202     def check(self):
203         if self.thing.carrying:
204             if not hasattr(self.thing.carrying, 'installable')\
205                or not self.thing.carrying.installable:
206                 raise PlayError('carried thing not installable')
207         elif len(self._get_uninstallables()) == 0:
208             raise PlayError('nothing to uninstall here')
209
210     def do(self):
211         if self.thing.carrying:
212             self.thing.carrying.install()
213             self.thing.carrying = None
214             self.thing.send_msg('CHAT "You install the thing you carry."')
215         else:
216             self._get_uninstallables()[0].uninstall()
217             self.thing.send_msg('CHAT "You uninstall the thing here."')
218
219
220
221 class Task_WEAR(Task):
222
223     def check(self):
224         if self.thing.name in self.thing.game.hats:
225             return
226         if not self.thing.carrying:
227             raise PlayError('carrying nothing to wear')
228         if self.thing.name in self.thing.game.hats:
229             raise PlayError('already wearing a hat')
230         if self.thing.carrying.type_ != 'Hat':
231             raise PlayError('can only wear a hat')
232
233     def do(self):
234         if self.thing.name in self.thing.game.hats:
235             t = self.thing.game.thing_types['Hat'](self.thing.game,
236                                                    position=self.thing.position)
237             self.thing.game.things += [t]
238             t.design = self.thing.game.hats[self.thing.name]
239             del self.thing.game.hats[self.thing.name]
240             self.thing.send_msg('CHAT "You drop your hat."')
241             for remixer in [t for t in self.thing.game.things
242                             if t.type_ == 'HatRemixer'
243                             and t.position == self.thing.position]:
244                 remixer.accept(t)
245                 break
246         else:
247             from plomrogue.misc import quote
248             self.thing.game.hats[self.thing.name] = self.thing.carrying.design
249             self.thing.game.things.remove(self.thing.carrying)
250             self.thing.carrying = None
251             self.thing.send_msg('CHAT "You put on a hat."')