home · contact · privacy
Don't generate objects at illegal positions. Plus, refactor.
[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             else:
23                 raise GameError('stringifying arg type not implemented')
24         return ' '.join(stringed_args)
25
26
27
28 class Task_WAIT(Task):
29
30     def do(self):
31         return 'success'
32
33
34
35 class Task_MOVE(Task):
36     argtypes = 'string:direction'
37
38     def check(self):
39         test_pos = self.thing.world.map_.move(self.thing.position, self.args[0])
40         if test_pos is None:
41             raise GameError('would move outside map bounds')
42         if self.thing.world.map_[test_pos] != '.':
43             raise GameError('%s would move into illegal terrain' % self.thing.id_)
44         for t in self.thing.world.things_at_pos(test_pos):
45             if t.blocking:
46                 raise GameError('%s would move into other thing' % self.thing.id_)
47
48     def do(self):
49         self.thing.position = self.thing.world.map_.move(self.thing.position,
50                                                          self.args[0])
51         for id_ in self.thing.inventory:
52             t = self.thing.world.get_thing(id_)
53             t.position = self.thing.position
54
55
56
57 class Task_PICKUP(Task):
58     argtypes = 'int:nonneg'
59
60     def check(self):
61         to_pick_up = self.thing.world.get_thing(self.args[0],
62                                                 create_unfound=False)
63         if to_pick_up is None or \
64            to_pick_up.id_ not in self.thing.get_pickable_items():
65             raise GameError('thing of ID %s not in reach to pick up'
66                             % self.args[0])
67
68     def do(self):
69         to_pick_up = self.thing.world.get_thing(self.args[0])
70         self.thing.inventory += [self.args[0]]
71         to_pick_up.in_inventory = True
72         to_pick_up.position = self.thing.position
73
74
75
76 class TaskOnInventoryItem(Task):
77     argtypes = 'int:nonneg'
78
79     def _basic_inventory_item_check(self):
80         item = self.thing.world.get_thing(self.args[0], create_unfound=False)
81         if item is None:
82             raise GameError('no thing of ID %s' % self.args[0])
83         if item.id_ not in self.thing.inventory:
84             raise GameError('no thing of ID %s in inventory' % self.args[0])
85         return item
86
87     def _eliminate_from_inventory(self):
88         item = self.thing.world.get_thing(self.args[0])
89         del self.thing.inventory[self.thing.inventory.index(item.id_)]
90         item.in_inventory = False
91         return item
92
93
94
95 class Task_DROP(TaskOnInventoryItem):
96     argtypes = 'int:nonneg'
97
98     def check(self):
99         self._basic_inventory_item_check()
100
101     def do(self):
102         self._eliminate_from_inventory()
103
104
105
106 class Task_EAT(TaskOnInventoryItem):
107     argtypes = 'int:nonneg'
108
109     def check(self):
110         to_eat = self._basic_inventory_item_check()
111         if to_eat.type_ != 'food':
112             raise GameError('thing of ID %s s not food' % self.args[0])
113
114     def do(self):
115         to_eat = self._eliminate_from_inventory()
116         del self.thing.world.things[self.thing.world.things.index(to_eat)]
117         self.thing.health += 50