From: Christian Heller Date: Fri, 26 Apr 2019 02:19:12 +0000 (+0200) Subject: Fix mapping interaction between server and client. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/add_task?a=commitdiff_plain;h=be4473640666bbdb9e7c002945699ed54a2546ed;p=plomrogue2-experiments Fix mapping interaction between server and client. --- diff --git a/new/example_client.py b/new/example_client.py index 6816f0e..8f7af66 100755 --- a/new/example_client.py +++ b/new/example_client.py @@ -3,8 +3,7 @@ import curses import socket import threading from plomrogue.parser import ArgError, Parser -from plomrogue.commands import (cmd_MAP, cmd_THING_POS, cmd_PLAYER_ID, - cmd_THING_HEALTH) +from plomrogue.commands import cmd_PLAYER_ID, cmd_THING_HEALTH from plomrogue.game import Game, WorldBase from plomrogue.mapping import MapHex from plomrogue.io import PlomSocket @@ -63,9 +62,9 @@ class ClientMap(MapHex): else: for i in range(len(map_lines)): map_lines[i] = '0' + map_lines[i] - self.y_cut(map_lines, center[1][0], size[0]) + self.y_cut(map_lines, center[0], size[0]) map_width = self.size[1] * 2 + 1 - self.x_cut(map_lines, center[1][1] * 2, size[1], map_width) + self.x_cut(map_lines, center[1] * 2, size[1], map_width) return map_lines @@ -78,13 +77,15 @@ class World(WorldBase): on any update, even before we actually receive map data. """ super().__init__(*args, **kwargs) - self.maps = {(0,0): ClientMap()} + self.map_ = ClientMap() + self.offset = (0,0) self.player_inventory = [] self.player_id = 0 self.pickable_items = [] - def new_map(self, map_pos, size): - self.maps[map_pos] = ClientMap(size) + def new_map(self, offset, size): + self.map_ = ClientMap(size) + self.offset = offset @property def player(self): @@ -111,8 +112,13 @@ def cmd_TURN(game, n): cmd_TURN.argtypes = 'int:nonneg' +def cmd_VISIBLE_MAP(game, offset, size): + game.world.new_map(offset, size) +cmd_VISIBLE_MAP.argtypes = 'yx_tuple yx_tuple:pos' + + def cmd_VISIBLE_MAP_LINE(game, y, terrain_line): - game.world.maps[(0,0)].set_line(y, terrain_line) + game.world.map_.set_line(y, terrain_line) cmd_VISIBLE_MAP_LINE.argtypes = 'int:nonneg string' @@ -128,6 +134,12 @@ def cmd_THING_TYPE(game, i, type_): cmd_THING_TYPE.argtypes = 'int:nonneg string' +def cmd_THING_POS(game, i, yx): + t = game.world.get_thing(i) + t.position = yx +cmd_THING_POS.argtypes = 'int:nonneg yx_tuple:nonneg' + + def cmd_PLAYER_INVENTORY(game, ids): game.world.player_inventory[:] = ids # TODO: test whether valid IDs game.tui.to_update['inventory'] = True @@ -153,7 +165,7 @@ class Game: 'PLAYER_ID': cmd_PLAYER_ID, 'PLAYER_INVENTORY': cmd_PLAYER_INVENTORY, 'GAME_STATE_COMPLETE': cmd_GAME_STATE_COMPLETE, - 'MAP': cmd_MAP, + 'VISIBLE_MAP': cmd_VISIBLE_MAP, 'PICKABLE_ITEMS': cmd_PICKABLE_ITEMS, 'THING_TYPE': cmd_THING_TYPE, 'THING_HEALTH': cmd_THING_HEALTH, @@ -191,6 +203,8 @@ class Game: def log(self, msg): """Prefix msg plus newline to self.log_text.""" self.log_text = msg + '\n' + self.log_text + with open('log', 'w') as f: + f.write(self.log_text) self.tui.to_update['log'] = True def symbol_for_type(self, type_): @@ -321,9 +335,9 @@ class DescriptorWidget(TextLinesWidget): def get_text_lines(self): lines = [] - pos_i = self.tui.game.world.maps[(0,0)].\ - get_position_index(self.tui.examiner_position[1]) - terrain = self.tui.game.world.maps[(0,0)].terrain[pos_i] + pos_i = self.tui.game.world.map_.\ + get_position_index(self.tui.examiner_position) + terrain = self.tui.game.world.map_.terrain[pos_i] lines = [terrain] for t in self.tui.game.world.things_at_pos(self.tui.examiner_position): lines += [t.type_] @@ -386,12 +400,12 @@ class MapWidget(Widget): def draw(self): def annotated_terrain(): - terrain_as_list = list(self.tui.game.world.maps[(0,0)].terrain[:]) + terrain_as_list = list(self.tui.game.world.map_.terrain[:]) for t in self.tui.game.world.things: if t.id_ in self.tui.game.world.player_inventory: continue - pos_i = self.tui.game.world.maps[(0,0)].\ - get_position_index(t.position[1]) + pos_i = self.tui.game.world.map_.\ + get_position_index(t.position) symbol = self.tui.game.symbol_for_type(t.type_) if terrain_as_list[pos_i][0] in {'f', '@', 'm'}: old_symbol = terrain_as_list[pos_i][0] @@ -401,8 +415,8 @@ class MapWidget(Widget): else: terrain_as_list[pos_i] = symbol if self.tui.examiner_mode: - pos_i = self.tui.game.world.maps[(0,0)].\ - get_position_index(self.tui.examiner_position[1]) + pos_i = self.tui.game.world.map_.\ + get_position_index(self.tui.examiner_position) terrain_as_list[pos_i] = (terrain_as_list[pos_i][0], '?') return terrain_as_list @@ -438,7 +452,7 @@ class MapWidget(Widget): chars_with_attrs += [c] return chars_with_attrs - if self.tui.game.world.maps[(0,0)].terrain == '': + if self.tui.game.world.map_.terrain == '': lines = [] pad_y(lines) self.safe_write(''.join(lines)) @@ -448,7 +462,7 @@ class MapWidget(Widget): center = self.tui.game.world.player.position if self.tui.examiner_mode: center = self.tui.examiner_position - lines = self.tui.game.world.maps[(0,0)].\ + lines = self.tui.game.world.map_.\ format_to_view(annotated_terrain, center, self.size) pad_or_cut_x(lines) pad_y(lines) @@ -553,10 +567,9 @@ class TUI: def move_examiner(direction): start_pos = self.examiner_position - new_examine_pos = self.game.world.maps[(0,0)].\ - move(start_pos[0], direction) + new_examine_pos = self.game.world.map_.move(start_pos, direction) if new_examine_pos: - self.examiner_position[1] = new_examine_pos + self.examiner_position = new_examine_pos self.to_update['map'] = True def switch_to_pick_or_drop(target_widget): diff --git a/new/plomrogue/commands.py b/new/plomrogue/commands.py index fc3a8a0..1e0cad0 100644 --- a/new/plomrogue/commands.py +++ b/new/plomrogue/commands.py @@ -10,9 +10,9 @@ def cmd_GET_GAMESTATE(game, connection_id): """Send game state to caller.""" game.send_gamestate(connection_id) -def cmd_MAP(game, big_yx, small_yx): - """Create new map of size small_yx at pos big_yx and only '?' cells.""" - game.world.new_map(big_yx, small_yx) +def cmd_MAP(game, map_pos, size): + """Create new map of size at position map_pos, and only '?' cells.""" + game.world.new_map(map_pos, size) cmd_MAP.argtypes = 'yx_tuple yx_tuple:pos' def cmd_THING_TYPE(game, i, type_): @@ -39,7 +39,7 @@ cmd_THING_TYPE.argtypes = 'int:nonneg string:thingtype' def cmd_THING_POS(game, i, big_yx, small_yx): t = game.world.get_thing(i) t.position = (big_yx, small_yx) -cmd_THING_POS.argtypes = 'int:nonneg yx_tuple yx_tuple' +cmd_THING_POS.argtypes = 'int:nonneg yx_tuple yx_tuple:nonneg' def cmd_THING_INVENTORY(game, id_, ids): t = game.world.get_thing(id_) diff --git a/new/plomrogue/game.py b/new/plomrogue/game.py index eda54ae..f86383d 100755 --- a/new/plomrogue/game.py +++ b/new/plomrogue/game.py @@ -185,19 +185,22 @@ class Game: def send_gamestate(self, connection_id=None): """Send out game state data relevant to clients.""" + def send_thing(offset, thing): + offset_pos = (thing.position[1][0] - offset[0], + thing.position[1][1] - offset[1]) + self.io.send('THING_TYPE %s %s' % (thing.id_, thing.type_)) + self.io.send('THING_POS %s %s' % (thing.id_, + stringify_yx(offset_pos))) + self.io.send('TURN ' + str(self.world.turn)) visible_map = self.world.player.get_visible_map() - self.io.send('MAP ' + stringify_yx([0,0]) + ' ' + stringify_yx(visible_map.size)) + offset = self.world.player.get_surroundings_offset() + self.io.send('VISIBLE_MAP ' + stringify_yx(offset) + ' ' + stringify_yx(visible_map.size)) for y, line in visible_map.lines(): self.io.send('VISIBLE_MAP_LINE %5s %s' % (y, quote(line))) - visible_things, offset = self.world.player.get_visible_things() + visible_things = self.world.player.get_visible_things() for thing in visible_things: - offset_pos = (thing.position[1][0] - offset[0], - thing.position[1][1] - offset[1]) - self.io.send('THING_TYPE %s %s' % (thing.id_, thing.type_)) - self.io.send('THING_POS %s %s %s' % (thing.id_, - stringify_yx(thing.position[0]), - stringify_yx(offset_pos))) + send_thing(offset, thing) if hasattr(thing, 'health'): self.io.send('THING_HEALTH %s %s' % (thing.id_, thing.health)) @@ -208,10 +211,7 @@ class Game: self.io.send('PLAYER_INVENTORY ,') for id_ in self.world.player.inventory: thing = self.world.get_thing(id_) - self.io.send('THING_TYPE %s %s' % (thing.id_, thing.type_)) - self.io.send('THING_POS %s %s %s' % (thing.id_, - stringify_yx(thing.position[0]), - stringify_yx(thing.position[1]))) + send_thing(offset, thing) self.io.send('GAME_STATE_COMPLETE') def proceed(self): diff --git a/new/plomrogue/things.py b/new/plomrogue/things.py index b645841..df8c78a 100644 --- a/new/plomrogue/things.py +++ b/new/plomrogue/things.py @@ -80,7 +80,8 @@ class ThingAnimate(Thing): return target_direction def hunt_player(self): - visible_things, offset = self.get_visible_things() + visible_things = self.get_visible_things() + offset = self.get_surroundings_offset() target = None for t in visible_things: if t.type_ == 'human': @@ -111,7 +112,8 @@ class ThingAnimate(Thing): if t.type_ == 'food': self.set_task('PICKUP', (id_,)) return True - visible_things, offset = self.get_visible_things() + visible_things = self.get_visible_things() + offset = self.get_surroundings_offset() food_targets = [] for t in visible_things: if t.type_ == 'food': @@ -274,11 +276,11 @@ class ThingAnimate(Thing): continue if (not thing.in_inventory) and stencil[(pos_y, pos_x)] == '.': visible_things += [thing] - return visible_things, offset + return visible_things def get_pickable_items(self): pickable_ids = [] - visible_things, _ = self.get_visible_things() + visible_things = self.get_visible_things() for t in [t for t in visible_things if isinstance(t, ThingItem) and (t.position == self.position or