1 from parser import ArgError
6 def __init__(self, size=(0, 0)):
8 self.terrain = '?'*self.size_i
12 return self.size[0] * self.size[1]
14 def set_line(self, y, line):
15 height_map = self.size[0]
16 width_map = self.size[1]
18 raise ArgError('too large row number %s' % y)
19 width_line = len(line)
20 if width_line > width_map:
21 raise ArgError('too large map line width %s' % width_line)
22 self.terrain = self.terrain[:y * width_map] + line +\
23 self.terrain[(y + 1) * width_map:]
25 def get_position_index(self, yx):
26 return yx[0] * self.size[1] + yx[1]
32 self.Thing = Thing # child classes may use an extended Thing class here
36 def get_thing(self, id_):
37 for thing in self.things:
40 t = self.Thing(self, id_)
44 def new_map(self, geometry, yx):
45 map_type = self.get_map_class(geometry)
46 self.map_ = map_type(yx)
51 def __init__(self, world, id_):
58 class CommonCommandsMixin:
60 def cmd_MAP(self, geometry, yx):
61 """Create new map of grid geometry, size yx and only '?' cells."""
62 legal_grids = {'Hex', 'Square'}
63 if geometry not in legal_grids:
64 raise ArgError('First map argument must be one of: ' +
65 ', '.join(legal_grids))
66 self.world.new_map(geometry, yx)
67 cmd_MAP.argtypes = 'string yx_tuple:nonneg'
69 def cmd_THING_TYPE(self, i, type_):
70 t = self.world.get_thing(i)
72 cmd_THING_TYPE.argtypes = 'int:nonneg string'
74 def cmd_THING_POS(self, i, yx):
75 t = self.world.get_thing(i)
77 cmd_THING_POS.argtypes = 'int:nonneg yx_tuple:nonneg'