home · contact · privacy
c0fb98b3fa9138a5766c3b6be70d47b12ad28240
[plomrogue2-experiments] / game_common.py
1 from parser import ArgError
2
3
4 class MapManager:
5
6     def __init__(self, globs):
7         """With globs a globals() call, collect caller's Map classes."""
8         self.map_classes = []
9         for name in globs:
10             if name[:3] == 'Map':
11                 self.map_classes += [globs[name]]
12
13     def get_map_geometries(self):
14         geometries = []
15         for map_class in self.map_classes:
16             geometries += [map_class.__name__[3:]]
17         return geometries
18
19     def get_map_class(self, geometry):
20         for map_class in self.map_classes:
21             if map_class.__name__[3:] == geometry:
22                 return map_class
23
24
25 class Map:
26
27     def __init__(self, size=(0, 0)):
28         self.size = size
29         self.terrain = '?'*self.size_i
30
31     @property
32     def size_i(self):
33         return self.size[0] * self.size[1]
34
35     def set_line(self, y, line):
36         height_map = self.size[0]
37         width_map = self.size[1]
38         if y >= height_map:
39             raise ArgError('too large row number %s' % y)
40         width_line = len(line)
41         if width_line > width_map:
42             raise ArgError('too large map line width %s' % width_line)
43         self.terrain = self.terrain[:y * width_map] + line +\
44                        self.terrain[(y + 1) * width_map:]
45
46     def get_position_index(self, yx):
47         return yx[0] * self.size[1] + yx[1]
48
49
50 class World:
51
52     def __init__(self):
53         self.Thing = Thing  # child classes may use an extended Thing class here
54         self.turn = 0
55         self.things = []
56
57     def get_thing(self, id_, create_unfound=True):
58         for thing in self.things:
59             if id_ == thing.id_:
60                 return thing
61         if create_unfound:
62             t = self.Thing(self, id_)
63             self.things += [t]
64             return t
65         return None
66
67     def new_map(self, geometry, yx):
68         map_type = self.game.map_manager.get_map_class(geometry)
69         self.map_ = map_type(yx)
70
71
72 class Thing:
73
74     def __init__(self, world, id_):
75         self.world = world
76         self.id_ = id_
77         self.type_ = '?'
78         self.position = [0,0]
79
80
81 class CommonCommandsMixin:
82
83     def cmd_MAP(self, geometry, yx):
84         """Create new map of grid geometry, size yx and only '?' cells."""
85         self.world.new_map(geometry, yx)
86     cmd_MAP.argtypes = 'string:geometry yx_tuple:pos'
87
88     def cmd_THING_TYPE(self, i, type_):
89         t = self.world.get_thing(i)
90         t.type_ = type_
91     cmd_THING_TYPE.argtypes = 'int:nonneg string'
92
93     def cmd_THING_POS(self, i, yx):
94         t = self.world.get_thing(i)
95         t.position = list(yx)
96     cmd_THING_POS.argtypes = 'int:nonneg yx_tuple:nonneg'