home · contact · privacy
Add inventory / item pickup/drop server-side.
[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 self.thing.world.map_[test_pos] != '.':
41             raise GameError('%s would move into illegal terrain' % self.thing.id_)
42         for t in self.thing.world.things:
43             if t.blocking and t.position == test_pos:
44                 raise GameError('%s would move into other thing' % self.thing.id_)
45
46     def do(self):
47         self.thing.position = self.thing.world.map_.move(self.thing.position,
48                                                          self.args[0])
49         for id_ in self.thing.inventory:
50             t = self.thing.world.get_thing(id_)
51             t.position[:] = self.thing.position
52
53
54
55 class Task_PICKUP(Task):
56     argtypes = 'int:nonneg'
57
58     def check(self):
59         to_pick_up = self.thing.world.get_thing(self.args[0],
60                                                 create_unfound=False)
61         if to_pick_up is None:
62             raise GameError('no thing of ID %s to pick up' % self.args[0])
63         if not (self.thing.position == to_pick_up.position or
64                 tuple(to_pick_up.position) in
65                 self.thing.world.map_.get_neighbors(self.thing.position)):
66             raise GameError('thing of ID %s not in reach to pick up'
67                             % self.args[0])
68
69     def do(self):
70         to_pick_up = self.thing.world.get_thing(self.args[0])
71         self.thing.inventory += [self.args[0]]
72         to_pick_up.in_inventory = True
73
74
75
76 class Task_DROP(Task):
77     argtypes = 'int:nonneg'
78
79     def check(self):
80         to_pick_up = self.thing.world.get_thing(self.args[0],
81                                                 create_unfound=False)
82         if to_pick_up is None:
83             raise GameError('no thing of ID %s to drop' % self.args[0])
84         if to_pick_up.id_ not in self.thing.inventory:
85             raise GameError('no thing of ID %s to drop in inventory'
86                             % self.args[0])
87
88     def do(self):
89         to_drop = self.thing.world.get_thing(self.args[0])
90         del self.thing.inventory[self.thing.inventory.index(to_drop.id_)]
91         to_drop.in_inventory = False