home · contact · privacy
In examine cursor mode, show map cell contents in left bar.
[plomrogue2-experiments] / new / example_client.py
index ee71241110e28d0121a83571c8ddf93ff4118416..0e2ceddec7375e2a27d1a1cf05b0bb5afa965144 100755 (executable)
@@ -5,14 +5,14 @@ import threading
 from plomrogue.parser import ArgError, Parser
 from plomrogue.commands import cmd_MAP, cmd_THING_POS, cmd_PLAYER_ID
 from plomrogue.game import Game, WorldBase
-from plomrogue.mapping import MapBase
+from plomrogue.mapping import MapHex
 from plomrogue.io import PlomSocket
 from plomrogue.things import ThingBase
 import types
 import queue
 
 
-class Map(MapBase):
+class ClientMap(MapHex):
 
     def y_cut(self, map_lines, center_y, view_height):
         map_height = len(map_lines)
@@ -33,14 +33,17 @@ class Map(MapBase):
                 cut_end = cut_start + view_width
             map_lines[:] = [line[cut_start:cut_end] for line in map_lines]
 
-    def format_to_view(self, map_string, center, size):
+    def format_to_view(self, map_cells, center, size):
 
-        def map_string_to_lines(map_string):
+        def map_cells_to_lines(map_cells):
             map_view_chars = ['0']
             x = 0
             y = 0
-            for c in map_string:
-                map_view_chars += [c, ' ']
+            for cell in map_cells:
+                if type(cell) == str:
+                    map_view_chars += [cell, ' ']
+                else:
+                    map_view_chars += [cell[0], cell[1]]
                 x += 1
                 if x == self.size[1]:
                     map_view_chars += ['\n']
@@ -53,7 +56,7 @@ class Map(MapBase):
             map_view_chars = map_view_chars[:-1]
             return ''.join(map_view_chars).split('\n')
 
-        map_lines = map_string_to_lines(map_string)
+        map_lines = map_cells_to_lines(map_cells)
         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)
@@ -69,13 +72,13 @@ class World(WorldBase):
         on any update, even before we actually receive map data.
         """
         super().__init__(*args, **kwargs)
-        self.map_ = Map()
+        self.map_ = ClientMap()
         self.player_inventory = []
         self.player_id = 0
         self.pickable_items = []
 
     def new_map(self, yx):
-        self.map_ = Map(yx)
+        self.map_ = ClientMap(yx)
 
     @property
     def player(self):
@@ -233,14 +236,10 @@ class Widget:
             if not type(part) == str:
                 part_string = part[0]
                 attr = part[1]
-            if len(part_string) > 0:
-                return [(char, attr) for char in part_string]
-            elif len(part_string) == 1:
-                return [part]
-            return []
+            return [(char, attr) for char in part_string]
 
         chars_with_attrs = []
-        if type(foo) == str or len(foo) == 2 and type(foo[1]) == int:
+        if type(foo) == str or (len(foo) == 2 and type(foo[1]) == int):
             chars_with_attrs += to_chars_with_attrs(foo)
         else:
             for part in foo:
@@ -283,13 +282,13 @@ class EditWidget(Widget):
         self.safe_write((''.join(self.tui.to_send), curses.color_pair(1)))
 
 
-class LogWidget(Widget):
+class TextLinesWidget(Widget):
 
     def draw(self):
+        lines = self.get_text_lines()
         line_width = self.size[1]
-        log_lines = self.tui.game.log_text.split('\n')
         to_join = []
-        for line in log_lines:
+        for line in lines:
             to_pad = line_width - (len(line) % line_width)
             if to_pad == line_width:
                 to_pad = 0
@@ -297,6 +296,26 @@ class LogWidget(Widget):
         self.safe_write((''.join(to_join), curses.color_pair(3)))
 
 
+class LogWidget(TextLinesWidget):
+
+    def get_text_lines(self):
+        return self.tui.game.log_text.split('\n')
+
+
+class DescriptorWidget(TextLinesWidget):
+
+    def get_text_lines(self):
+        lines = []
+        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:
+            if t.position == self.tui.examiner_position:
+                lines += [t.type_]
+        return lines
+
+
 class PopUpWidget(Widget):
 
     def draw(self):
@@ -348,17 +367,29 @@ class PickableItemsWidget(ItemsSelectorWidget):
 
 class MapWidget(Widget):
 
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.examine_mode = False
+
     def draw(self):
 
-        def terrain_with_objects():
+        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)
                 symbol = self.tui.game.symbol_for_type(t.type_)
-                if symbol in {'i'} and terrain_as_list[pos_i] in {'@', 'm'}:
-                    continue
-                terrain_as_list[pos_i] = symbol
-            return ''.join(terrain_as_list)
+                if terrain_as_list[pos_i][0] in {'i', '@', 'm'}:
+                    old_symbol = terrain_as_list[pos_i][0]
+                    if old_symbol in {'@', 'm'}:
+                        symbol = old_symbol
+                    terrain_as_list[pos_i] = (symbol, '+')
+                else:
+                    terrain_as_list[pos_i] = symbol
+            if self.examine_mode:
+                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
 
         def pad_or_cut_x(lines):
             line_width = self.size[1]
@@ -396,9 +427,11 @@ class MapWidget(Widget):
             self.safe_write(''.join(lines))
             return
 
-        terrain_with_objects = terrain_with_objects()
+        annotated_terrain = annotated_terrain()
         center = self.tui.game.world.player.position
-        lines = self.tui.game.world.map_.format_to_view(terrain_with_objects,
+        if self.examine_mode:
+            center = self.tui.examiner_position
+        lines = self.tui.game.world.map_.format_to_view(annotated_terrain,
                                                         center, self.size)
         pad_or_cut_x(lines)
         pad_y(lines)
@@ -431,6 +464,7 @@ class TUI:
         self.parser = Parser(self.game)
         self.to_update = {}
         self.item_pointer = 0
+        self.examiner_position = (0, 0)
         curses.wrapper(self.loop)
 
     def loop(self, stdscr):
@@ -453,6 +487,7 @@ class TUI:
                 self.item_pointer = len(selectables) - 1
             if key == 'c':
                 switch_widgets(widget, map_widget)
+                map_widget.examine_mode = False
             elif key == 'j':
                 self.item_pointer += 1
             elif key == 'k' and self.item_pointer > 0:
@@ -469,6 +504,14 @@ class TUI:
             trigger = widget.check_updates[0]
             self.to_update[trigger] = True
 
+        def move_examiner(direction):
+            start_pos = self.examiner_position
+            new_examine_pos = self.game.world.map_.move(start_pos, direction)
+            if new_examine_pos:
+                self.examiner_position = new_examine_pos
+            self.to_update['map'] = True
+            self.to_update['descriptor'] = True
+
         setup_screen(stdscr)
         curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_RED)
         curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_GREEN)
@@ -482,13 +525,16 @@ class TUI:
         turn_widget = TextLineWidget('TURN:', self, (2, 0), (1, 20))
         turn_widget.children += [TurnWidget(self, (2, 6), (1, 14), ['turn'])]
         log_widget = LogWidget(self, (4, 0), (None, 20), ['log'])
+        descriptor_widget = DescriptorWidget(self, (4, 0), (None, 20),
+                                             ['descriptor'], False)
         map_widget = MapWidget(self, (0, 21), (None, None), ['map'])
         inventory_widget = InventoryWidget(self, (0, 21), (None, None),
                                            ['inventory'], False)
         pickable_items_widget = PickableItemsWidget(self, (0, 21), (None, None),
                                                     ['pickable_items'], False)
-        top_widgets = [edit_widget, turn_widget, log_widget, map_widget,
-                       inventory_widget, pickable_items_widget]
+        top_widgets = [edit_widget, turn_widget, log_widget,
+                       descriptor_widget, map_widget, inventory_widget,
+                       pickable_items_widget]
         popup_widget = PopUpWidget(self, (0, 0), (1, 1), visible=False)
         self.popup_text = 'Hi bob'
         write_mode = True
@@ -532,36 +578,58 @@ class TUI:
                         self.socket.send(''.join(self.to_send))
                         self.to_send[:] = []
                         self.to_update['edit'] = True
+                elif key == 't':
+                    if not popup_widget.visible:
+                        self.to_update['popup'] = True
+                        popup_widget.visible = True
+                        popup_widget.reconfigure()
+                        draw_popup_if_visible = True
+                    else:
+                        popup_widget.visible = False
+                        for w in top_widgets:
+                            w.ensure_freshness(True)
                 elif map_widget.visible:
-                    if key == 'w':
+                    if key == '?':
+                        map_widget.examine_mode = not map_widget.examine_mode
+                        if map_widget.examine_mode:
+                            self.examiner_position = self.game.world.\
+                                                     player.position
+                            switch_widgets(log_widget, descriptor_widget)
+                        else:
+                            switch_widgets(descriptor_widget, log_widget)
+                        self.to_update['map'] = True
+                    elif key == 'p':
+                        self.socket.send('GET_PICKABLE_ITEMS')
+                        self.item_pointer = 0
+                        switch_widgets(map_widget, pickable_items_widget)
+                    elif key == 'i':
+                        self.item_pointer = 0
+                        switch_widgets(map_widget, inventory_widget)
+                    elif map_widget.examine_mode:
+                        if key == 'w':
+                            move_examiner('UPLEFT')
+                        elif key == 'e':
+                            move_examiner('UPRIGHT')
+                        elif key == 's':
+                            move_examiner('LEFT')
+                        elif key == 'd':
+                            move_examiner('RIGHT')
+                        elif key == 'x':
+                            move_examiner('DOWNLEFT')
+                        elif key == 'c':
+                            move_examiner('DOWNRIGHT')
+                    elif key == 'w':
                         self.socket.send('TASK:MOVE UPLEFT')
                     elif key == 'e':
                         self.socket.send('TASK:MOVE UPRIGHT')
-                    if key == 's':
+                    elif key == 's':
                         self.socket.send('TASK:MOVE LEFT')
                     elif key == 'd':
                         self.socket.send('TASK:MOVE RIGHT')
-                    if key == 'x':
+                    elif key == 'x':
                         self.socket.send('TASK:MOVE DOWNLEFT')
                     elif key == 'c':
                         self.socket.send('TASK:MOVE DOWNRIGHT')
-                    elif key == 't':
-                        if not popup_widget.visible:
-                            self.to_update['popup'] = True
-                            popup_widget.visible = True
-                            popup_widget.reconfigure()
-                            draw_popup_if_visible = True
-                        else:
-                            popup_widget.visible = False
-                            for w in top_widgets:
-                                w.ensure_freshness(True)
-                    elif key == 'p':
-                        self.socket.send('GET_PICKABLE_ITEMS')
-                        self.item_pointer = 0
-                        switch_widgets(map_widget, pickable_items_widget)
-                    elif key == 'i':
-                        self.item_pointer = 0
-                        switch_widgets(map_widget, inventory_widget)
                 elif pickable_items_widget.visible:
                     pick_or_drop_menu('p', pickable_items_widget,
                                       self.game.world.pickable_items,