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