1 from parser import ArgError
6 def __init__(self, globs):
7 """With globs a globals() call, collect caller's Map classes."""
11 self.map_classes += [globs[name]]
13 def get_map_geometries(self):
15 for map_class in self.map_classes:
16 geometries += [map_class.__name__[3:]]
19 def get_map_class(self, geometry):
20 for map_class in self.map_classes:
21 if map_class.__name__[3:] == geometry:
27 def __init__(self, size=(0, 0)):
29 self.terrain = '?'*self.size_i
33 return self.size[0] * self.size[1]
35 def set_line(self, y, line):
36 height_map = self.size[0]
37 width_map = self.size[1]
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:]
46 def get_position_index(self, yx):
47 return yx[0] * self.size[1] + yx[1]
53 self.Thing = Thing # child classes may use an extended Thing class here
57 def get_thing(self, id_, create_unfound=True):
58 for thing in self.things:
62 t = self.Thing(self, id_)
67 def new_map(self, geometry, yx):
68 map_type = self.game.map_manager.get_map_class(geometry)
69 self.map_ = map_type(yx)
74 def __init__(self, world, id_):
81 class CommonCommandsMixin:
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'
88 def cmd_THING_TYPE(self, i, type_):
89 t = self.world.get_thing(i)
91 cmd_THING_TYPE.argtypes = 'int:nonneg string'
93 def cmd_THING_POS(self, i, yx):
94 t = self.world.get_thing(i)
96 cmd_THING_POS.argtypes = 'int:nonneg yx_tuple:nonneg'