home · contact · privacy
Fix FOV floating point bugs by using fractions.Fraction for fractions.
[plomrogue2-experiments] / client-curses.py
index 6846fc5bf96b83a5a464d33ab3a1c5c0bb61145f..fa35ffde70578b43a695d90e9db472f657a215ed 100755 (executable)
@@ -13,13 +13,12 @@ class Map(game_common.Map):
         map_height = len(map_lines)
         if map_height > view_height and center_y > view_height / 2:
             if center_y > map_height - view_height / 2:
-                map_lines = map_lines[map_height - view_height:]
+                map_lines[:] = map_lines[map_height - view_height:]
             else:
-                start = center_y - int(view_height / 2)
+                start = center_y - int(view_height / 2) - 1
                 map_lines[:] = map_lines[start:start + view_height]
 
-    def x_cut(self, map_lines, center_x, view_width):
-        map_width = len(map_lines[0])
+    def x_cut(self, map_lines, center_x, view_width, map_width):
         if map_width > view_width and center_x > view_width / 2:
             if center_x > map_width - view_width / 2:
                 cut_start = map_width - view_width
@@ -45,7 +44,7 @@ class MapSquare(Map):
 
         map_lines = map_string_to_lines(map_string)
         self.y_cut(map_lines, center[0], size[0])
-        self.x_cut(map_lines, center[1], size[1])
+        self.x_cut(map_lines, center[1], size[1], self.size[1])
         return map_lines
 
 
@@ -54,7 +53,7 @@ class MapHex(Map):
     def format_to_view(self, map_string, center, size):
 
         def map_string_to_lines(map_string):
-            map_view_chars = [' ']
+            map_view_chars = ['+']
             x = 0
             y = 0
             for c in map_string:
@@ -65,12 +64,16 @@ class MapHex(Map):
                     x = 0
                     y += 1
                     if y % 2 == 0:
-                        map_view_chars += [' ']
+                        map_view_chars += ['+']
+            if y % 2 == 0:
+                map_view_chars = map_view_chars[:-1]
+            map_view_chars = map_view_chars[:-1]
             return ''.join(map_view_chars).split('\n')
 
         map_lines = map_string_to_lines(map_string)
         self.y_cut(map_lines, center[0], size[0])
-        self.x_cut(map_lines, center[1] * 2, size[1])
+        map_width = self.size[1] * 2 + 1
+        self.x_cut(map_lines, center[1] * 2, size[1], map_width)
         return map_lines
 
 
@@ -123,6 +126,7 @@ class Game(game_common.CommonCommandsMixin):
     def log(self, msg):
         """Prefix msg plus newline to self.log_text."""
         self.log_text = msg + '\n' + self.log_text
+        self.to_update['log'] = True
 
     def symbol_for_type(self, type_):
         symbol = '?'
@@ -135,7 +139,6 @@ class Game(game_common.CommonCommandsMixin):
     def cmd_LAST_PLAYER_TASK_RESULT(self, msg):
         if msg != "success":
             self.log(msg)
-            self.to_update['log'] = True
     cmd_LAST_PLAYER_TASK_RESULT.argtypes = 'string'
 
     def cmd_TURN_FINISHED(self, n):
@@ -359,6 +362,7 @@ class TUI:
         self.log = LogWidget(self, (4, 0), (None, 20), ['log'])
         self.map_ = MapWidget(self, (0, 21), (None, None), ['map'])
         widgets = (self.edit, self.turn, self.log, self.map_)
+        map_mode = False
         while True:
             for w in widgets:
                 w.ensure_freshness()
@@ -368,23 +372,49 @@ class TUI:
                 self.to_update[key] = False
             try:
                 key = self.stdscr.getkey()
-                if len(key) == 1 and key in ASCII_printable and \
-                        len(self.to_send) < len(self.edit):
-                    self.to_send += [key]
-                    self.to_update['edit'] = True
-                elif key == 'KEY_BACKSPACE':
-                    self.to_send[:] = self.to_send[:-1]
-                    self.to_update['edit'] = True
-                elif key == '\n':
-                    plom_socket_io.send(self.socket, ''.join(self.to_send))
-                    self.to_send[:] = []
-                    self.to_update['edit'] = True
-                elif key == 'KEY_RESIZE':
+                if key == 'KEY_RESIZE':
                     curses.endwin()
                     self.setup_screen(curses.initscr())
                     for w in widgets:
                         w.size = w.size_def
                         w.ensure_freshness(True)
+                elif key == '\t':  # Tabulator key.
+                    map_mode = False if map_mode else True
+                elif map_mode:
+                    if type(self.game.world.map_) == MapSquare:
+                        if key == 'a':
+                            plom_socket_io.send(self.socket, 'MOVE LEFT')
+                        elif key == 'd':
+                            plom_socket_io.send(self.socket, 'MOVE RIGHT')
+                        elif key == 'w':
+                            plom_socket_io.send(self.socket, 'MOVE UP')
+                        elif key == 's':
+                            plom_socket_io.send(self.socket, 'MOVE DOWN')
+                    elif type(self.game.world.map_) == MapHex:
+                        if key == 'w':
+                            plom_socket_io.send(self.socket, 'MOVE UPLEFT')
+                        elif key == 'e':
+                            plom_socket_io.send(self.socket, 'MOVE UPRIGHT')
+                        if key == 's':
+                            plom_socket_io.send(self.socket, 'MOVE LEFT')
+                        elif key == 'd':
+                            plom_socket_io.send(self.socket, 'MOVE RIGHT')
+                        if key == 'x':
+                            plom_socket_io.send(self.socket, 'MOVE DOWNLEFT')
+                        elif key == 'c':
+                            plom_socket_io.send(self.socket, 'MOVE DOWNRIGHT')
+                else:
+                    if len(key) == 1 and key in ASCII_printable and \
+                            len(self.to_send) < len(self.edit):
+                        self.to_send += [key]
+                        self.to_update['edit'] = True
+                    elif key == 'KEY_BACKSPACE':
+                        self.to_send[:] = self.to_send[:-1]
+                        self.to_update['edit'] = True
+                    elif key == '\n':  # Return key
+                        plom_socket_io.send(self.socket, ''.join(self.to_send))
+                        self.to_send[:] = []
+                        self.to_update['edit'] = True
             except curses.error:
                 pass
             if self.game.do_quit: