self.world.things = []
cmd_NEW_TURN.argtypes = 'int:nonneg'
+ def cmd_VISIBLE_MAP_LINE(self, y, terrain_line):
+ self.world.map_.set_line(y, terrain_line)
+ cmd_VISIBLE_MAP_LINE.argtypes = 'int:nonneg string'
+
class WidgetManager:
self.top = urwid.Filler(widget_pile, valign='top')
def draw_map(self):
- """Draw map view from .game.terrain_map, .game.things."""
+ """Draw map view from .game.map_.terrain, .game.things."""
map_lines = []
- map_size = len(self.game.world.terrain_map)
+ map_size = len(self.game.world.map_.terrain)
start_cut = 0
while start_cut < map_size:
- limit = start_cut + self.game.world.map_size[1]
- map_lines += [self.game.world.terrain_map[start_cut:limit]]
+ limit = start_cut + self.game.world.map_.size[1]
+ map_lines += [self.game.world.map_.terrain[start_cut:limit]]
start_cut = limit
for t in self.game.world.things:
line_as_list = list(map_lines[t.position[0]])
from parser import ArgError
-class World:
+class Map:
- def __init__(self):
- self.turn = 0
- self.map_size = (0, 0)
- self.terrain_map = ''
- self.things = []
- self.Thing = Thing # child classes may use an extended Thing class here
+ def __init__(self, size=(0, 0), terrain=''):
+ self.size = size
+ self.terrain = terrain
- def set_map_size(self, yx):
- y, x = yx
- self.map_size = (y, x)
- self.terrain_map = ''
- for y in range(self.map_size[0]):
- self.terrain_map += '?' * self.map_size[1]
-
- def set_map_line(self, y, line):
- width_map = self.map_size[1]
- if y >= self.map_size[0]:
+ def set_line(self, y, line):
+ height_map = self.size[0]
+ width_map = self.size[1]
+ if y >= height_map:
raise ArgError('too large row number %s' % y)
width_line = len(line)
if width_line > width_map:
raise ArgError('too large map line width %s' % width_line)
- self.terrain_map = self.terrain_map[:y * width_map] + line + \
- self.terrain_map[(y + 1) * width_map:]
+ self.terrain = self.terrain[:y * width_map] + line +\
+ self.terrain[(y + 1) * width_map:]
+
+ def set_size(self, yx):
+ y, x = yx
+ self.size = (y, x)
+ self.terrain = ''
+ for y in range(self.size[0]):
+ self.terrain += '?' * self.size[1]
+
+
+class World:
+
+ def __init__(self):
+ self.turn = 0
+ self.map_ = Map()
+ self.things = []
+ self.Thing = Thing # child classes may use an extended Thing class here
def get_thing(self, id_):
for thing in self.things:
def cmd_MAP_SIZE(self, yx):
"""Set self.map_size to yx, redraw self.terrain_map as '?' cells."""
- self.world.set_map_size(yx)
+ self.world.map_.set_size(yx)
cmd_MAP_SIZE.argtypes = 'yx_tuple:nonneg'
- def cmd_TERRAIN_LINE(self, y, terrain_line):
- self.world.set_map_line(y, terrain_line)
- cmd_TERRAIN_LINE.argtypes = 'int:nonneg string'
-
def cmd_THING_TYPE(self, i, type_):
t = self.world.get_thing(i)
t.type_ = type_
return ''.join(quoted)
self.send('NEW_TURN ' + str(self.world.turn))
- self.send('MAP_SIZE ' + stringify_yx(self.world.map_size))
- for y in range(self.world.map_size[0]):
- width = self.world.map_size[1]
- terrain_line = self.world.terrain_map[y * width:(y + 1) * width]
- self.send('TERRAIN_LINE %5s %s' % (y, quoted(terrain_line)))
+ self.send('MAP_SIZE ' + stringify_yx(self.world.map_.size))
+ visible_map = self.world.get_player().get_visible_map()
+ for y in range(self.world.map_.size[0]):
+ self.send('VISIBLE_MAP_LINE %5s %s' % (y, visible_map.get_line(y)))
for thing in self.world.things:
self.send('THING_TYPE %s %s' % (thing.id_, thing.type_))
self.send('THING_POS %s %s' % (thing.id_,
pos_yx[1] -= 1
+class Map(game_common.Map):
+
+ def get_line(self, y):
+ width = self.size[1]
+ return self.terrain[y * width:(y + 1) * width]
+
+
class World(game_common.World):
def __init__(self):
super().__init__()
self.Thing = Thing # use local Thing class instead of game_common's
+ self.map_ = Map() # use extended child class
self.player_id = 0
def proceed_to_next_player_turn(self):
test_pos = self.thing.position[:]
move_pos(direction, test_pos)
if test_pos[0] < 0 or test_pos[1] < 0 or \
- test_pos[0] >= self.thing.world.map_size[0] or \
- test_pos[1] >= self.thing.world.map_size[1]:
+ test_pos[0] >= self.thing.world.map_.size[0] or \
+ test_pos[1] >= self.thing.world.map_.size[1]:
raise GameError('would move outside map bounds')
- pos_i = test_pos[0] * self.thing.world.map_size[1] + test_pos[1]
- map_tile = self.thing.world.terrain_map[pos_i]
+ pos_i = test_pos[0] * self.thing.world.map_.size[1] + test_pos[1]
+ map_tile = self.thing.world.map_.terrain[pos_i]
if map_tile != '.':
raise GameError('would move into illegal terrain')
for t in self.thing.world.things:
if is_AI and self.task is None:
self.decide_task()
+ def get_visible_map(self):
+ return Map(self.world.map_.size, self.world.map_.terrain)
+
class Commander():
"""Send msg to all clients."""
self.send(msg)
cmd_ALL.argtypes = 'string'
+
+ def cmd_TERRAIN_LINE(self, y, terrain_line):
+ self.world.map_.set_line(y, terrain_line)
+ cmd_TERRAIN_LINE.argtypes = 'int:nonneg string'