home · contact · privacy
Make fullness/emptiness of bottles visible via .thing_char.
[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
80     def check(self):
81         if self.thing.carrying:
82             raise PlayError('already carrying something')
83         nothing_to_pick_up = True
84         for t in [t for t in self.thing.game.things
85                   if t.portable
86                   and t != self.thing and t.position == self.thing.position and
87                   t.type_ != 'Player']:
88             nothing_to_pick_up = False
89             break
90         if nothing_to_pick_up:
91             raise PlayError('nothing to pick up')
92
93     def do(self):
94         to_pick_up = [t for t in self.thing.game.things
95                       if t.portable
96                       and t != self.thing and t.position == self.thing.position][0]
97         self.thing.carrying = to_pick_up
98
99
100
101 class Task_DROP(Task):
102
103     def check(self):
104         if not self.thing.carrying:
105             raise PlayError('nothing to drop')
106         if self.thing.carrying.type_ == 'Bottle' and self.thing.carrying.full:
107             for t in [t for t in self.thing.game.things
108                       if t.type_ == 'BottleDeposit'
109                       and t.position == self.thing.position]:
110                 raise PlayError('cannot drop full bottle into bottle deposit')
111
112     def do(self):
113         if self.thing.carrying.type_ == 'Bottle' and not self.thing.carrying.full:
114             for t in [t for t in self.thing.game.things
115                       if t.type_ == 'BottleDeposit'
116                       and t.position == self.thing.position]:
117                 t.accept()
118                 self.thing.game.things.remove(self.thing.carrying)
119                 break
120         self.thing.carrying = None
121
122
123
124 class Task_DOOR(Task):
125
126     def do(self):
127         self.thing.carrying = None
128         action_radius = list(self.thing.game.map_geometry.
129                              get_neighbors_yxyx(self.thing.position).values())
130         for t in [t for t in self.thing.game.things if
131                   t.type_ == 'Door' and t.position in action_radius]:
132             if t.blocking:
133                 t.open()
134             else:
135                 t.close()
136
137
138
139 class Task_INTOXICATE(Task):
140
141     def check(self):
142         if self.thing.carrying is None:
143             raise PlayError('carrying nothing to drink from')
144         if self.thing.carrying.type_ != 'Bottle':
145             raise PlayError('cannot drink from non-bottle')
146         if not self.thing.carrying.full:
147             raise PlayError('bottle is empty')
148
149     def do(self):
150         self.thing.carrying.full = False
151         self.thing.carrying.empty()
152         for c_id in self.thing.game.sessions:
153             if self.thing.game.sessions[c_id]['thing_id'] == self.thing.id_:
154                 self.thing.game.io.send('RANDOM_COLORS', c_id)
155                 self.thing.game.io.send('CHAT "You are drunk now."', c_id)
156                 break
157         self.thing.drunk = 10000
158
159
160 class Task_COMMAND(Task):
161     argtypes = 'string'
162
163     def check(self):
164         if self.thing.carrying is None:
165             raise PlayError('nothing to command')
166         if not self.thing.carrying.commandable:
167             raise PlayError('cannot command this item type')
168
169     def do(self):
170         from plomrogue.misc import quote
171         reply = self.thing.carrying.interpret(self.args[0])
172         for c_id in self.thing.game.sessions:
173             if self.thing.game.sessions[c_id]['thing_id'] == self.thing.id_:
174                 self.thing.game.io.send('REPLY ' + quote(reply), c_id)