home · contact · privacy
Only re-process FOVs for players whose FOV radius is affected by change.
[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.position not in reach:
99             raise PlayError('thing not in reach')
100         elif not to_pick_up.portable:
101             raise PlayError('thing not portable')
102
103     def do(self):
104         to_pick_up = self.thing.game.get_thing(self.args[0])
105         to_pick_up.position = self.thing.position[:]
106         self.thing.carrying = to_pick_up
107
108
109
110 class Task_DROP(Task):
111
112     def check(self):
113         if not self.thing.carrying:
114             raise PlayError('nothing to drop')
115         if self.thing.carrying.type_ == 'Bottle' and self.thing.carrying.full:
116             for t in [t for t in self.thing.game.things
117                       if t.type_ == 'BottleDeposit'
118                       and t.position == self.thing.position]:
119                 raise PlayError('cannot drop full bottle into bottle deposit')
120
121     def do(self):
122         if self.thing.carrying.type_ == 'Bottle' and not 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 == self.thing.position]:
126                 t.accept()
127                 self.thing.game.remove_thing(self.thing.carrying)
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                 break
135         self.thing.carrying = None
136
137
138
139 class Task_DOOR(Task):
140
141     def do(self):
142         self.thing.carrying = None
143         action_radius = list(self.thing.game.map_geometry.
144                              get_neighbors_yxyx(self.thing.position).values())
145         for t in [t for t in self.thing.game.things if
146                   t.type_ == 'Door' and t.position in action_radius]:
147             if t.blocking:
148                 t.open()
149             else:
150                 t.close()
151             self.thing.game.record_fov_change(t.position)
152
153
154
155 class Task_INTOXICATE(Task):
156
157     def check(self):
158         if self.thing.carrying is None:
159             raise PlayError('carrying nothing to drink from')
160         if self.thing.carrying.type_ != 'Bottle':
161             raise PlayError('cannot drink from non-bottle')
162         if not self.thing.carrying.full:
163             raise PlayError('bottle is empty')
164
165     def do(self):
166         self.thing.carrying.full = False
167         self.thing.carrying.empty()
168         self.thing.send_msg('RANDOM_COLORS')
169         self.thing.send_msg('CHAT "You are drunk now."')
170         self.thing.drunk = 10000
171         self.thing.invalidate_map_view()
172
173
174
175 class Task_COMMAND(Task):
176     argtypes = 'string'
177
178     def check(self):
179         if self.thing.carrying is None:
180             raise PlayError('nothing to command')
181         if not self.thing.carrying.commandable:
182             raise PlayError('cannot command this item type')
183
184     def do(self):
185         from plomrogue.misc import quote
186         reply_lines = self.thing.carrying.interpret(self.args[0])
187         for line in reply_lines:
188             self.thing.send_msg('REPLY ' + quote(line))
189
190
191
192 class Task_INSTALL(Task):
193
194     def _get_uninstallables(self):
195         return [t for t in self.thing.game.things
196                 if t != self.thing
197                 and hasattr(t, 'installable') and t.installable
198                 and (not t.portable)
199                 and t.position == self.thing.position]
200
201     def check(self):
202         if self.thing.carrying:
203             if not hasattr(self.thing.carrying, 'installable')\
204                or not self.thing.carrying.installable:
205                 raise PlayError('carried thing not installable')
206         elif len(self._get_uninstallables()) == 0:
207             raise PlayError('nothing to uninstall here')
208
209     def do(self):
210         if self.thing.carrying:
211             self.thing.carrying.install()
212             self.thing.carrying = None
213             self.thing.send_msg('CHAT "You install the thing you carry."')
214         else:
215             self._get_uninstallables()[0].uninstall()
216             self.thing.send_msg('CHAT "You uninstall the thing here."')
217
218
219
220 class Task_WEAR(Task):
221
222     def check(self):
223         if self.thing.name in self.thing.game.hats:
224             return
225         if not self.thing.carrying:
226             raise PlayError('carrying nothing to wear')
227         if self.thing.name in self.thing.game.hats:
228             raise PlayError('already wearing a hat')
229         if self.thing.carrying.type_ != 'Hat':
230             raise PlayError('can only wear a hat')
231
232     def do(self):
233         if self.thing.name in self.thing.game.hats:
234             t = self.thing.game.add_thing('Hat', self.thing.position)
235             t.design = self.thing.game.hats[self.thing.name]
236             del self.thing.game.hats[self.thing.name]
237             self.thing.send_msg('CHAT "You drop your hat."')
238             for remixer in [t for t in self.thing.game.things
239                             if t.type_ == 'HatRemixer'
240                             and t.position == self.thing.position]:
241                 remixer.accept(t)
242                 break
243         else:
244             from plomrogue.misc import quote
245             self.thing.game.hats[self.thing.name] = self.thing.carrying.design
246             self.thing.game.remove_thing(self.thing.carrying)
247             self.thing.carrying = None
248             self.thing.send_msg('CHAT "You put on a hat."')