home · contact · privacy
Improve player error messages, and log them to clients.
[plomrogue2] / plomrogue / tasks.py
1 from plomrogue.errors import PlayError, GameError
2 from plomrogue.mapping import YX
3
4
5
6 class Task:
7     argtypes = ''
8     todo = 3
9
10     def __init__(self, thing, args=()):
11         self.thing = thing
12         self.args = args
13
14     def check(self):
15         pass
16
17
18
19 class Task_WAIT(Task):
20     todo = 1
21
22     def do(self):
23         pass
24
25
26
27 class Task_MOVE(Task):
28     todo = 1
29     argtypes = 'string:direction'
30
31     def get_move_target(self):
32         return self.thing.game.map_geometry.move(self.thing.position,
33                                                  self.args[0])
34
35     def check(self):
36         test_pos = self.get_move_target()
37         if test_pos is None:
38             raise PlayError('would move out of map')
39         elif test_pos in [t.position for t in self.thing.game.things
40                           if t.blocking]:
41             raise PlayError('blocked by other thing')
42         elif self.thing.game.map[test_pos] != '.':
43             raise PlayError('blocked by impassable tile')
44
45     def do(self):
46         self.thing.position = self.get_move_target()
47         if self.thing.carrying:
48             self.thing.carrying.position = self.thing.position
49
50
51
52 class Task_WRITE(Task):
53     todo = 1
54     argtypes = 'string:char string'
55
56     def check(self):
57         if not self.thing.game.can_do_tile_with_pw(self.thing.position,
58                                                    self.args[1]):
59             raise GameError('wrong password for tile')
60
61     def do(self):
62         self.thing.game.map[self.thing.position] = self.args[0]
63
64
65
66 class Task_FLATTEN_SURROUNDINGS(Task):
67     todo = 10
68     argtypes = 'string'
69
70     def check(self):
71         pass
72
73     def do(self):
74         for yx in[self.thing.position] + \
75             list(self.thing.game.map_geometry.get_neighbors(self.thing.position).values()):
76             if yx is not None:
77                 if not self.thing.game.can_do_tile_with_pw(yx, self.args[0]):
78                     continue
79                 self.thing.game.map[yx] = '.'
80
81
82
83 class Task_PICK_UP(Task):
84     todo = 1
85
86     def check(self):
87         if self.thing.carrying:
88             raise PlayError('already carrying something')
89         nothing_to_pick_up = True
90         for t in [t for t in self.thing.game.things
91                   if t != self.thing and t.position == self.thing.position
92                   and t.type_ != 'Player']:
93             nothing_to_pick_up = False
94             break
95         if nothing_to_pick_up:
96             raise PlayError('nothing to pick up')
97
98     def do(self):
99         to_pick_up = [t for t in self.thing.game.things
100                       if t != self.thing and t.position == self.thing.position][0]
101         self.thing.carrying = to_pick_up
102
103
104
105 class Task_DROP(Task):
106     todo = 1
107
108     def check(self):
109         if not self.thing.carrying:
110             raise PlayError('nothing to drop')
111
112     def do(self):
113         self.thing.carrying = None