home · contact · privacy
Add Thing spawner.
[plomrogue2] / plomrogue / tasks.py
1 from plomrogue.errors import PlayError, GameError
2
3
4
5 class Task:
6     argtypes = ''
7     todo = 3
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     todo = 1
20
21     def do(self):
22         pass
23
24
25
26 class Task_MOVE(Task):
27     todo = 1
28     argtypes = 'string:direction'
29
30     def get_move_target(self):
31         return self.thing.game.map_geometry.move_yxyx(self.thing.position,
32                                                       self.args[0])
33
34     def check(self):
35         test_yxyx = self.get_move_target()
36         if test_yxyx in [t.position for t in self.thing.game.things
37                          if t.blocking]:
38             raise PlayError('blocked by other thing')
39         elif self.thing.game.maps[test_yxyx[0]][test_yxyx[1]] != '.':
40             raise PlayError('blocked by impassable tile')
41
42     def do(self):
43         self.thing.position = self.get_move_target()
44         if self.thing.carrying:
45             self.thing.carrying.position = self.thing.position
46
47
48
49 class Task_WRITE(Task):
50     todo = 1
51     argtypes = 'string:char string'
52
53     def check(self):
54         if not self.thing.game.can_do_tile_with_pw(*self.thing.position,
55                                                    self.args[1]):
56             raise GameError('wrong password for tile')
57
58     def do(self):
59         big_yx = self.thing.position[0]
60         little_yx = self.thing.position[1]
61         self.thing.game.maps[big_yx][little_yx] = self.args[0]
62
63
64
65 class Task_FLATTEN_SURROUNDINGS(Task):
66     todo = 10
67     argtypes = 'string'
68
69     def check(self):
70         pass
71
72     def do(self):
73         for yxyx in [self.thing.position] + \
74                 list(self.thing.game.map_geometry.get_neighbors_yxyx(
75                     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.portable
91                   and t != self.thing and t.position == self.thing.position and
92                   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.portable
101                       and t != self.thing and t.position == self.thing.position][0]
102         self.thing.carrying = to_pick_up
103
104
105
106 class Task_DROP(Task):
107     todo = 1
108
109     def check(self):
110         if not self.thing.carrying:
111             raise PlayError('nothing to drop')
112
113     def do(self):
114         self.thing.carrying = None