home · contact · privacy
Fix mapping interaction between server and client.
[plomrogue2-experiments] / new / example_client.py
index 45f5be0861239a68d9287ff547a323af5d83a20a..8f7af6643f2d63425e79767f50a65dc706f579d3 100755 (executable)
@@ -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
@@ -58,6 +57,11 @@ class ClientMap(MapHex):
             return ''.join(map_view_chars).split('\n')
 
         map_lines = map_cells_to_lines(map_cells)
+        if len(map_lines) % 2 == 0:
+            map_lines = map_lines[1:]
+        else:
+            for i in range(len(map_lines)):
+                map_lines[i] = '0' + map_lines[i]
         self.y_cut(map_lines, center[0], size[0])
         map_width = self.size[1] * 2 + 1
         self.x_cut(map_lines, center[1] * 2, size[1], map_width)
@@ -74,12 +78,14 @@ class World(WorldBase):
         """
         super().__init__(*args, **kwargs)
         self.map_ = ClientMap()
+        self.offset = (0,0)
         self.player_inventory = []
         self.player_id = 0
         self.pickable_items = []
 
-    def new_map(self, yx):
-        self.map_ = ClientMap(yx)
+    def new_map(self, offset, size):
+        self.map_ = ClientMap(size)
+        self.offset = offset
 
     @property
     def player(self):
@@ -106,6 +112,11 @@ 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.map_.set_line(y, terrain_line)
 cmd_VISIBLE_MAP_LINE.argtypes = 'int:nonneg string'
@@ -123,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
@@ -148,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,
@@ -186,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_):
@@ -320,9 +339,8 @@ class DescriptorWidget(TextLinesWidget):
                 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:
-            if t.position == self.tui.examiner_position:
-                lines += [t.type_]
+        for t in self.tui.game.world.things_at_pos(self.tui.examiner_position):
+            lines += [t.type_]
         return lines
 
 
@@ -384,7 +402,10 @@ class MapWidget(Widget):
         def annotated_terrain():
             terrain_as_list = list(self.tui.game.world.map_.terrain[:])
             for t in self.tui.game.world.things:
-                pos_i = self.tui.game.world.map_.get_position_index(t.position)
+                if t.id_ in self.tui.game.world.player_inventory:
+                    continue
+                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]
@@ -441,8 +462,8 @@ 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.map_.format_to_view(annotated_terrain,
-                                                        center, self.size)
+        lines = self.tui.game.world.map_.\
+                format_to_view(annotated_terrain, center, self.size)
         pad_or_cut_x(lines)
         pad_y(lines)
         self.safe_write(lines_to_colored_chars(lines))
@@ -482,7 +503,7 @@ class TUI:
         self.parser = Parser(self.game)
         self.to_update = {}
         self.item_pointer = 0
-        self.examiner_position = (0, 0)
+        self.examiner_position = ((0,0), (0, 0))
         self.examiner_mode = False
         self.popup_text = 'Hi bob'
         self.to_send = []