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