home · contact · privacy
Enforce sane create_unfound decisions.
[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 get_move_target(self):
42         return self.thing.game.map_geometry.move(self.thing.position,
43                                                  self.args[0],
44                                                  self.thing.game.map_size)
45
46     def check(self):
47         test_pos = self.get_move_target()
48         if self.thing.game.maps[test_pos[0]][test_pos[1]] != '.':
49             raise GameError('%s would move into illegal terrain' % self.thing.id_)
50         for t in self.thing.game.things_at_pos(test_pos):
51             if t.blocking:
52                 raise GameError('%s would move into other thing' % self.thing.id_)
53
54     def do(self):
55         self.thing.position = self.get_move_target()
56
57
58
59 class Task_PICKUP(Task):
60     argtypes = 'int:nonneg'
61
62     def check(self):
63         to_pick_up = self.thing.game.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.game.get_thing(self.args[0],
72                                                create_unfound=False)
73         self.thing.inventory += [self.args[0]]
74         to_pick_up.in_inventory = True
75         to_pick_up.position = self.thing.position
76
77
78
79 class TaskOnInventoryItem(Task):
80     argtypes = 'int:nonneg'
81
82     def _basic_inventory_item_check(self):
83         item = self.thing.game.get_thing(self.args[0], create_unfound=False)
84         if item is None:
85             raise GameError('no thing of ID %s' % self.args[0])
86         if item.id_ not in self.thing.inventory:
87             raise GameError('no thing of ID %s in inventory' % self.args[0])
88         return item
89
90     def _eliminate_from_inventory(self):
91         item = self.thing.game.get_thing(self.args[0], create_unfound=False)
92         del self.thing.inventory[self.thing.inventory.index(item.id_)]
93         item.in_inventory = False
94         return item
95
96
97
98 class Task_DROP(TaskOnInventoryItem):
99     argtypes = 'int:nonneg'
100
101     def check(self):
102         self._basic_inventory_item_check()
103
104     def do(self):
105         self._eliminate_from_inventory()
106
107
108
109 class Task_EAT(TaskOnInventoryItem):
110     argtypes = 'int:nonneg'
111
112     def check(self):
113         to_eat = self._basic_inventory_item_check()
114         if to_eat.type_ != 'food':
115             raise GameError('thing of ID %s s not food' % self.args[0])
116
117     def do(self):
118         to_eat = self._eliminate_from_inventory()
119         del self.thing.game.things[self.thing.game.things.index(to_eat)]
120         self.thing.health += 50