From: Christian Heller Date: Sat, 12 Jan 2019 21:21:44 +0000 (+0100) Subject: Redo map system, add map visibility infrastructure. X-Git-Url: https://plomlompom.com/repos/?p=plomrogue2-experiments;a=commitdiff_plain;h=827134a13175939231b85fbc159c013e0f024e78 Redo map system, add map visibility infrastructure. --- diff --git a/client.py b/client.py index 0a25e45..80cac96 100755 --- a/client.py +++ b/client.py @@ -34,6 +34,10 @@ class Game(Commander): 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: @@ -50,13 +54,13 @@ 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]]) diff --git a/game_common.py b/game_common.py index 0598dfa..689398d 100644 --- a/game_common.py +++ b/game_common.py @@ -1,31 +1,38 @@ 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: @@ -49,13 +56,9 @@ class Commander: 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_ diff --git a/server.py b/server.py index c398930..c5fbff4 100755 --- a/server.py +++ b/server.py @@ -159,11 +159,10 @@ class CommandHandler(game_common.Commander, server_.game.Commander): 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_, diff --git a/server_/game.py b/server_/game.py index af6c970..4f31dc1 100644 --- a/server_/game.py +++ b/server_/game.py @@ -19,11 +19,19 @@ def move_pos(direction, pos_yx): 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): @@ -72,11 +80,11 @@ class Task: 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: @@ -136,6 +144,9 @@ class Thing(game_common.Thing): 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(): @@ -166,3 +177,7 @@ 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'