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_):
58 for thing in self.things:
61 t = self.Thing(self, id_)
65 def new_map(self, geometry, yx):
66 map_type = self.game.map_manager.get_map_class(geometry)
67 self.map_ = map_type(yx)
72 def __init__(self, world, id_):
79 class CommonCommandsMixin:
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:nonneg'
90 def cmd_THING_TYPE(self, i, type_):
91 t = self.world.get_thing(i)
93 cmd_THING_TYPE.argtypes = 'int:nonneg string'
95 def cmd_THING_POS(self, i, yx):
96 t = self.world.get_thing(i)
98 cmd_THING_POS.argtypes = 'int:nonneg yx_tuple:nonneg'