home · contact · privacy
Fix FOV floating point bugs by using fractions.Fraction for fractions.
[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_):
58         for thing in self.things:
59             if id_ == thing.id_:
60                 return thing
61         t = self.Thing(self, id_)
62         self.things += [t]
63         return t
64
65     def new_map(self, geometry, yx):
66         map_type = self.game.map_manager.get_map_class(geometry)
67         self.map_ = map_type(yx)
68
69
70 class Thing:
71
72     def __init__(self, world, id_):
73         self.world = world
74         self.id_ = id_
75         self.type_ = '?'
76         self.position = [0,0]
77
78
79 class CommonCommandsMixin:
80
81     def cmd_MAP(self, geometry, yx):
82         """Create new map of grid geometry, size yx and only '?' cells."""
83         legal_grids = self.map_manager.get_map_geometries()
84         if geometry not in legal_grids:
85             raise ArgError('First map argument must be one of: ' +
86                            ', '.join(legal_grids))
87         self.world.new_map(geometry, yx)
88     cmd_MAP.argtypes = 'string yx_tuple:pos'
89
90     def cmd_THING_TYPE(self, i, type_):
91         t = self.world.get_thing(i)
92         t.type_ = type_
93     cmd_THING_TYPE.argtypes = 'int:nonneg string'
94
95     def cmd_THING_POS(self, i, yx):
96         t = self.world.get_thing(i)
97         t.position = list(yx)
98     cmd_THING_POS.argtypes = 'int:nonneg yx_tuple:nonneg'