home · contact · privacy
ae559800f5f931769f6861cae5a652a8ffd6987b
[plomrogue2-experiments] / new / plomrogue / tasks.py
1 from plomrogue.errors import GameError
2 from plomrogue.misc import quote
3
4
5
6 class Task:
7     argtypes = ''
8
9     def __init__(self, thing, args=()):
10         self.thing = thing
11         self.args = args
12         self.todo = 3
13
14     def check(self):
15         pass
16
17     def get_args_string(self):
18         stringed_args = []
19         for arg in self.args:
20             if type(arg) == str:
21                 stringed_args += [quote(arg)]
22             elif type(arg) == int:
23                 stringed_args += [str(arg)]
24             else:
25                 raise GameError('stringifying arg type not implemented')
26         return ' '.join(stringed_args)
27
28
29
30 class Task_WAIT(Task):
31
32     def do(self):
33         return 'success'
34
35
36
37 class Task_MOVE(Task):
38     argtypes = 'string:direction'
39
40     def check(self):
41         test_pos = ((0,0),
42                     self.thing.world.maps[(0,0)].
43                     move(self.thing.position[1], self.args[0]))
44         if test_pos == ((0,0), None):
45             raise GameError('would move outside map bounds')
46         if self.thing.world.maps[test_pos[0]][test_pos[1]] != '.':
47             raise GameError('%s would move into illegal terrain' % self.thing.id_)
48         for t in self.thing.world.things_at_pos(test_pos):
49             if t.blocking:
50                 raise GameError('%s would move into other thing' % self.thing.id_)
51
52     def do(self):
53         self.thing.position = (0,0), self.thing.world.maps[(0,0)].\
54                                      move(self.thing.position[1], self.args[0])
55         for id_ in self.thing.inventory:
56             t = self.thing.world.get_thing(id_)
57             t.position = self.thing.position
58
59
60
61 class Task_PICKUP(Task):
62     argtypes = 'int:nonneg'
63
64     def check(self):
65         to_pick_up = self.thing.world.get_thing(self.args[0],
66                                                 create_unfound=False)
67         if to_pick_up is None or \
68            to_pick_up.id_ not in self.thing.get_pickable_items():
69             raise GameError('thing of ID %s not in reach to pick up'
70                             % self.args[0])
71
72     def do(self):
73         to_pick_up = self.thing.world.get_thing(self.args[0])
74         self.thing.inventory += [self.args[0]]
75         to_pick_up.in_inventory = True
76         to_pick_up.position = self.thing.position
77
78
79
80 class TaskOnInventoryItem(Task):
81     argtypes = 'int:nonneg'
82
83     def _basic_inventory_item_check(self):
84         item = self.thing.world.get_thing(self.args[0], create_unfound=False)
85         if item is None:
86             raise GameError('no thing of ID %s' % self.args[0])
87         if item.id_ not in self.thing.inventory:
88             raise GameError('no thing of ID %s in inventory' % self.args[0])
89         return item
90
91     def _eliminate_from_inventory(self):
92         item = self.thing.world.get_thing(self.args[0])
93         del self.thing.inventory[self.thing.inventory.index(item.id_)]
94         item.in_inventory = False
95         return item
96
97
98
99 class Task_DROP(TaskOnInventoryItem):
100     argtypes = 'int:nonneg'
101
102     def check(self):
103         self._basic_inventory_item_check()
104
105     def do(self):
106         self._eliminate_from_inventory()
107
108
109
110 class Task_EAT(TaskOnInventoryItem):
111     argtypes = 'int:nonneg'
112
113     def check(self):
114         to_eat = self._basic_inventory_item_check()
115         if to_eat.type_ != 'food':
116             raise GameError('thing of ID %s s not food' % self.args[0])
117
118     def do(self):
119         to_eat = self._eliminate_from_inventory()
120         del self.thing.world.things[self.thing.world.things.index(to_eat)]
121         self.thing.health += 50