home · contact · privacy
Double audible distance.
[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_yxyx(self.thing.position,
33                                                       self.args[0])
34
35     def check(self):
36         test_yxyx = self.get_move_target()
37         if test_yxyx in [t.position for t in self.thing.game.things
38                          if t.blocking]:
39             raise PlayError('blocked by other thing')
40         elif self.thing.game.maps[test_yxyx[0]][test_yxyx[1]] != '.':
41             raise PlayError('blocked by impassable tile')
42
43     def do(self):
44         self.thing.position = self.get_move_target()
45         if self.thing.carrying:
46             self.thing.carrying.position = self.thing.position
47
48
49
50 class Task_WRITE(Task):
51     todo = 1
52     argtypes = 'string:char string'
53
54     def check(self):
55         if not self.thing.game.can_do_tile_with_pw(*self.thing.position,
56                                                    self.args[1]):
57             raise GameError('wrong password for tile')
58
59     def do(self):
60         big_yx = self.thing.position[0]
61         little_yx = self.thing.position[1]
62         self.thing.game.maps[big_yx][little_yx] = 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 yxyx in[self.thing.position] + \
75             list(self.thing.game.map_geometry.get_neighbors_yxyx(self.thing.position).values()):
76             if not self.thing.game.can_do_tile_with_pw(*yxyx, self.args[0]):
77                 continue
78             self.thing.game.maps[yxyx[0]][yxyx[1]] = '.'
79
80
81
82 class Task_PICK_UP(Task):
83     todo = 1
84
85     def check(self):
86         if self.thing.carrying:
87             raise PlayError('already carrying something')
88         nothing_to_pick_up = True
89         for t in [t for t in self.thing.game.things
90                   if t != self.thing and t.position == self.thing.position
91                   and t.type_ != 'Player']:
92             nothing_to_pick_up = False
93             break
94         if nothing_to_pick_up:
95             raise PlayError('nothing to pick up')
96
97     def do(self):
98         to_pick_up = [t for t in self.thing.game.things
99                       if t != self.thing and t.position == self.thing.position][0]
100         self.thing.carrying = to_pick_up
101
102
103
104 class Task_DROP(Task):
105     todo = 1
106
107     def check(self):
108         if not self.thing.carrying:
109             raise PlayError('nothing to drop')
110
111     def do(self):
112         self.thing.carrying = None