home · contact · privacy
Improve map handling between client and server, add map scrolling.
[plomrogue2-experiments] / client-curses.py
index a18d8af59a8435fa2a9b553a55a0874192ee1ecb..4981a331963e5eae773cb06f53892df99b74a782 100755 (executable)
@@ -9,7 +9,7 @@ import game_common
 
 class MapSquare(game_common.Map):
 
-    def list_terrain_to_lines(self, terrain_as_list):
+    def list_terrain_to_lines(self, terrain_as_list, center, size):
         terrain = ''.join(terrain_as_list)
         map_lines = []
         start_cut = 0
@@ -17,12 +17,27 @@ class MapSquare(game_common.Map):
             limit = start_cut + self.size[1]
             map_lines += [terrain[start_cut:limit]]
             start_cut = limit
+        if len(map_lines) > size[0] and center[0] > size[0] / 2:
+            diff = len(map_lines) - size[0]
+            if center[0] > len(map_lines) - size[0] / 2:
+                map_lines = map_lines[diff:]
+            else:
+                start = center[0] - int(size[0] / 2)
+                map_lines = map_lines[start:start + size[0]]
+        if self.size[1] > size[1] and center[1] > size[1] / 2:
+            if center[1] > self.size[1] - size[1] / 2:
+                cut_start = self.size[1] - size[1]
+                cut_end = None
+            else:
+                cut_start = center[1] - int(size[1] / 2)
+                cut_end = cut_start + size[1]
+            map_lines = [line[cut_start:cut_end] for line in map_lines]
         return map_lines
 
 
 class MapHex(game_common.Map):
 
-    def list_terrain_to_lines(self, terrain_as_list):
+    def list_terrain_to_lines(self, terrain_as_list, center, size):
         new_terrain_list = [' ']
         x = 0
         y = 0
@@ -35,7 +50,23 @@ class MapHex(game_common.Map):
                 y += 1
                 if y % 2 == 0:
                     new_terrain_list += [' ']
-        return ''.join(new_terrain_list).split('\n')
+        map_lines = ''.join(new_terrain_list).split('\n')
+        if len(map_lines) > size[0] and center[0] > size[0] / 2:
+            diff = len(map_lines) - size[0]
+            if center[0] > len(map_lines) - size[0] / 2:
+                map_lines = map_lines[diff:]
+            else:
+                start = center[0] - int(size[0] / 2)
+                map_lines = map_lines[start:start + size[0]]
+        if self.size[1]*2 > size[1] and center[1]*4 > size[1]:
+            if center[1]*2 > self.size[1]*2 - size[1] / 2:
+                cut_start = self.size[1] * 2 - size[1]
+                cut_end = None
+            else:
+                cut_start = center[1]*2 - int(size[1] / 2)
+                cut_end = cut_start + size[1]
+            map_lines = [line[cut_start:cut_end] for line in map_lines]
+        return map_lines
 
 
 map_manager = game_common.MapManager(globals())
@@ -52,11 +83,13 @@ class World(game_common.World):
         super().__init__(*args, **kwargs)
         self.game = game
         self.map_ = self.game.map_manager.get_map_class('Hex')()
+        self.player_position = (0, 0)
 
 
 class Game(game_common.CommonCommandsMixin):
 
-    def __init__(self):
+    def __init__(self, tui):
+        self.tui = tui
         self.map_manager = map_manager
         self.parser = Parser(self)
         self.world = World(self)
@@ -77,6 +110,7 @@ class Game(game_common.CommonCommandsMixin):
     def cmd_LAST_PLAYER_TASK_RESULT(self, msg):
         if msg != "success":
             self.log(msg)
+            self.tui.log.do_update = True
     cmd_LAST_PLAYER_TASK_RESULT.argtypes = 'string'
 
     def cmd_TURN_FINISHED(self, n):
@@ -94,6 +128,14 @@ class Game(game_common.CommonCommandsMixin):
         self.world.map_.set_line(y, terrain_line)
     cmd_VISIBLE_MAP_LINE.argtypes = 'int:nonneg string'
 
+    def cmd_PLAYER_POS(self, yx):
+        self.world.player_position = yx
+    cmd_PLAYER_POS.argtypes = 'yx_tuple:pos'
+
+    def cmd_GAME_STATE_COMPLETE(self):
+        self.tui.turn.do_update = True
+        self.tui.map_.do_update = True
+
 
 ASCII_printable = ' !"#$%&\'\(\)*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX'\
                   'YZ[\\]^_\`abcdefghijklmnopqrstuvwxyz{|}~'
@@ -114,7 +156,7 @@ class Widget:
         self.win = curses.newwin(1, 1, self.start[0], self.start[1])
         self.size_def = size  # store for re-calling .size on SIGWINCH
         self.size = size
-        self.update = True
+        self.do_update = True
 
     @property
     def size(self):
@@ -142,10 +184,7 @@ class Widget:
                 part_string = part[0]
                 attr = part[1]
             if len(part_string) > 0:
-                chars_with_attrs = []
-                for char in part_string:
-                   chars_with_attrs += [(char, attr)]
-                return chars_with_attrs
+                return [(char, attr) for char in part_string]
             elif len(part_string) == 1:
                 return [part]
             return []
@@ -205,7 +244,8 @@ class MapWidget(Widget):
             for t in self.tui.game.world.things:
                 pos_i = self.tui.game.world.map_.get_position_index(t.position)
                 terrain_as_list[pos_i] = self.tui.game.symbol_for_type(t.type_)
-            lines = self.tui.game.world.map_.list_terrain_to_lines(terrain_as_list)
+            center = self.tui.game.world.player_position
+            lines = self.tui.game.world.map_.list_terrain_to_lines(terrain_as_list, center, self.size)
             line_width = self.size[1]
             for line in lines:
                 if line_width > len(line):
@@ -227,7 +267,6 @@ class MapWidget(Widget):
                 text_as_list += [(c, curses.color_pair(3))]
             else:
                 text_as_list += [c]
-        #self.safe_write(''.join(to_join))
         self.safe_write(text_as_list)
 
 
@@ -241,14 +280,15 @@ class TUI:
 
     def __init__(self, server_output):
         self.server_output = server_output
-        self.game = Game()
+        self.game = Game(self)
         self.parser = Parser(self.game)
+        self.do_update = True
         curses.wrapper(self.loop)
 
     def setup_screen(self, stdscr):
         self.stdscr = stdscr
         self.stdscr.refresh()  # will be called by getkey else, clearing screen
-        self.stdscr.timeout(10)
+        self.stdscr.timeout(1)
         self.stdscr.addstr(0, 0, 'SEND:')
         self.stdscr.addstr(2, 0, 'TURN:')
 
@@ -259,36 +299,35 @@ class TUI:
         curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_BLUE)
         curses.curs_set(False)  # hide cursor
         self.to_send = []
-        edit_line = EditWidget(self, (0, 6), (1, 14))
-        turn_line = TurnWidget(self, (2, 6), (1, 14))
-        log_display = LogWidget(self, (4, 0), (None, 20))
-        map_view = MapWidget(self, (0, 21), (None, None))
-        map_view.update = True
-        widgets = [edit_line, turn_line, log_display, map_view]
-        do_update = True
+        self.edit = EditWidget(self, (0, 6), (1, 14))
+        self.turn = TurnWidget(self, (2, 6), (1, 14))
+        self.log = LogWidget(self, (4, 0), (None, 20))
+        self.map_ = MapWidget(self, (0, 21), (None, None))
+        widgets = (self.edit, self.turn, self.log, self.map_)
         while True:
-            if do_update:
-                for w in widgets:
+            for w in widgets:
+                if w.do_update:
                     w.draw_and_refresh()
-                do_update = False
+                    w.do_update = False
             try:
                 key = self.stdscr.getkey()
-                do_update = True
                 if len(key) == 1 and key in ASCII_printable and \
-                        len(self.to_send) < len(edit_line):
+                        len(self.to_send) < len(self.edit):
                     self.to_send += [key]
+                    self.edit.do_update = True
                 elif key == 'KEY_BACKSPACE':
                     self.to_send[:] = self.to_send[:-1]
+                    self.edit.do_update = True
                 elif key == '\n':
                     plom_socket_io.send(s, ''.join(self.to_send))
                     self.to_send[:] = []
+                    self.edit.do_update = True
                 elif key == 'KEY_RESIZE':
                     curses.endwin()
                     self.setup_screen(curses.initscr())
                     for w in widgets:
                         w.size = w.size_def
-                else:
-                    do_update = False
+                        w.do_update = True
             except curses.error:
                 pass
             if len(self.server_output) > 0:
@@ -296,7 +335,7 @@ class TUI:
                 if do_quit:
                     break
                 self.server_output[:] = []
-                do_update = True
+                self.do_update = True
 
     def handle_input(self, msg):
         if msg == 'BYE':
@@ -305,10 +344,12 @@ class TUI:
             command = self.parser.parse(msg)
             if command is None:
                 self.game.log('UNHANDLED INPUT: ' + msg)
+                self.log.do_update = True
             else:
                 command()
         except ArgError as e:
                 self.game.log('ARGUMENT ERROR: ' + msg + '\n' + str(e))
+                self.log.do_update = True
         return False