home · contact · privacy
Make keybindings configurable.
[plomrogue2-experiments] / new2 / rogue_chat_curses.py
index 6fda713d2206ac3ed7d186586f0c0aaac542801f..bb3dac263e84670c8f9ef851f8b87844d6b9e4a6 100755 (executable)
@@ -48,19 +48,19 @@ def cmd_MAP(game, geometry, size, content):
     game.map_content = content
     if type(game.map_geometry) == MapGeometrySquare:
         game.tui.movement_keys = {
-            'w': 'UP',
-            'a': 'LEFT',
-            's': 'DOWN',
-            'd': 'RIGHT',
+            game.tui.keys['square_move_up']: 'UP',
+            game.tui.keys['square_move_left']: 'LEFT',
+            game.tui.keys['square_move_down']: 'DOWN',
+            game.tui.keys['square_move_right']: 'RIGHT',
         }
     elif type(game.map_geometry) == MapGeometryHex:
         game.tui.movement_keys = {
-            'w': 'UPLEFT',
-            'e': 'UPRIGHT',
-            'd': 'RIGHT',
-            'c': 'DOWNRIGHT',
-            'x': 'DOWNLEFT',
-            's': 'LEFT',
+            game.tui.keys['hex_move_upleft']: 'UPLEFT',
+            game.tui.keys['hex_move_upright']: 'UPRIGHT',
+            game.tui.keys['hex_move_right']: 'RIGHT',
+            game.tui.keys['hex_move_downright']: 'DOWNRIGHT',
+            game.tui.keys['hex_move_downleft']: 'DOWNLEFT',
+            game.tui.keys['hex_move_left']: 'LEFT',
         }
 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos string'
 
@@ -153,6 +153,8 @@ class TUI:
             self.is_intro = is_intro
 
     def __init__(self, host, port):
+        import os
+        import json
         self.host = host
         self.port = port
         self.mode_play = self.Mode('play')
@@ -173,6 +175,30 @@ class TUI:
         self.queue = queue.Queue()
         self.login_name = None
         self.switch_mode('waiting_for_server')
+        self.keys = {
+            'switch_to_chat': 'C',
+            'switch_to_play': 'P',
+            'switch_to_annotate': 'E',
+            'switch_to_portal': 'p',
+            'switch_to_study': '?',
+            'switch_to_edit': 'E',
+            'flatten': 'f',
+            'hex_move_upleft': 'w',
+            'hex_move_upright': 'e',
+            'hex_move_right': 'd',
+            'hex_move_downright': 'c',
+            'hex_move_downleft': 'x',
+            'hex_move_left': 's',
+            'square_move_up': 'w',
+            'square_move_left': 'a',
+            'square_move_down': 's',
+            'square_move_right': 'd',
+        }
+        if os.path.isfile('config.json'):
+            with open('config.json', 'r') as f:
+                keys_conf = json.loads(f.read())
+            for k in keys_conf:
+                self.keys[k] = keys_conf[k]
         curses.wrapper(self.loop)
 
     def flash(self):
@@ -219,21 +245,21 @@ class TUI:
     def help(self):
         self.log_msg("HELP:");
         self.log_msg("chat mode commands:");
-        self.log_msg("  :nick NAME - re-name yourself to NAME");
-        self.log_msg("  :msg USER TEXT - send TEXT to USER");
-        self.log_msg("  :help - show this help");
-        self.log_msg("  :p or :play - switch to play mode");
-        self.log_msg("  :? or :study - switch to study mode");
+        self.log_msg("  /nick NAME - re-name yourself to NAME");
+        self.log_msg("  /msg USER TEXT - send TEXT to USER");
+        self.log_msg("  /help - show this help");
+        self.log_msg("  /P or /play - switch to play mode");
+        self.log_msg("  /? or /study - switch to study mode");
         self.log_msg("commands common to study and play mode:");
-        self.log_msg("  w,a,s,d - move");
-        self.log_msg("  c - switch to chat mode");
+        self.log_msg("  %s - move" % ','.join(self.movement_keys));
+        self.log_msg("  %s - switch to chat mode" % self.keys['switch_to_chat']);
         self.log_msg("commands specific to play mode:");
-        self.log_msg("  e - write following ASCII character");
-        self.log_msg("  f - flatten surroundings");
-        self.log_msg("  ? - switch to study mode");
+        self.log_msg("  %s - write following ASCII character" % self.keys['switch_to_edit']);
+        self.log_msg("  %s - flatten surroundings" % self.keys['flatten']);
+        self.log_msg("  %s - switch to study mode" % self.keys['switch_to_study']);
         self.log_msg("commands specific to study mode:");
-        self.log_msg("  e - annotate terrain");
-        self.log_msg("  p - switch to play mode");
+        self.log_msg("  %s - annotate terrain" % self.keys['switch_to_annotate']);
+        self.log_msg("  %s - switch to play mode" % self.keys['switch_to_play']);
 
     def loop(self, stdscr):
 
@@ -297,7 +323,7 @@ class TUI:
 
         def reset_screen_size():
             self.size = YX(*stdscr.getmaxyx())
-            self.size = self.size - YX(self.size.y % 2, 0)
+            self.size = self.size - YX(self.size.y % 4, 0)
             self.size = self.size - YX(0, self.size.x % 4)
             self.window_width = int(self.size.x / 2)
 
@@ -381,15 +407,14 @@ class TUI:
                     indent = 0 if indent else 1
             else:
                 for line in map_lines_split:
-                    map_lines += [''.join(line)]
+                    map_lines += [' '.join(line)]
             window_center = YX(int(self.size.y / 2),
                                int(self.window_width / 2))
             player = self.game.get_thing(self.game.player_id, False)
             center = player.position
             if self.mode.shows_info:
                 center = self.explorer
-            if type(self.game.map_geometry) == MapGeometryHex:
-                center = YX(center.y, center.x * 2)
+            center = YX(center.y, center.x * 2)
             offset = center - window_center
             if type(self.game.map_geometry) == MapGeometryHex and offset.y % 2:
                 offset += YX(0, 1)
@@ -453,22 +478,22 @@ class TUI:
                 self.send('LOGIN ' + quote(self.input_))
                 self.input_ = ""
             elif self.mode == self.mode_chat and key == '\n':
-                if self.input_[0] == ':':
-                    if self.input_ in {':p', ':play'}:
+                if self.input_[0] == '/':
+                    if self.input_ in {'/P', '/play'}:
                         self.switch_mode('play')
-                    elif self.input_ in {':?', ':study'}:
+                    elif self.input_ in {'/?', '/study'}:
                         self.switch_mode('study')
-                    if self.input_ == ':help':
+                    elif self.input_ == '/help':
                         self.help()
-                    if self.input_ == ':reconnect':
+                    elif self.input_ == '/reconnect':
                         reconnect()
-                    elif self.input_.startswith(':nick'):
+                    elif self.input_.startswith('/nick'):
                         tokens = self.input_.split(maxsplit=1)
                         if len(tokens) == 2:
                             self.send('LOGIN ' + quote(tokens[1]))
                         else:
                             self.log_msg('? need login name')
-                    elif self.input_.startswith(':msg'):
+                    elif self.input_.startswith('/msg'):
                         tokens = self.input_.split(maxsplit=2)
                         if len(tokens) == 3:
                             self.send('QUERY %s %s' % (quote(tokens[1]),
@@ -502,24 +527,24 @@ class TUI:
                     self.switch_mode('play')
                 self.input_ = ''
             elif self.mode == self.mode_study:
-                if key == 'C':
+                if key == self.keys['switch_to_chat']:
                     self.switch_mode('chat')
-                elif key == 'P':
+                elif key == self.keys['switch_to_play']:
                     self.switch_mode('play')
-                elif key == 'A':
+                elif key == self.keys['switch_to_annotate']:
                     self.switch_mode('annotate', keep_position=True)
-                elif key == 'p':
+                elif key == self.keys['switch_to_portal']:
                     self.switch_mode('portal', keep_position=True)
                 elif key in self.movement_keys:
                     move_explorer(self.movement_keys[key])
             elif self.mode == self.mode_play:
-                if key == 'C':
+                if key == self.keys['switch_to_chat']:
                     self.switch_mode('chat')
-                elif key == '?':
+                elif key == self.keys['switch_to_study']:
                     self.switch_mode('study')
-                if key == 'E':
+                if key == self.keys['switch_to_edit']:
                     self.switch_mode('edit')
-                elif key == 'f':
+                elif key == self.keys['flatten']:
                     self.send('TASK:FLATTEN_SURROUNDINGS')
                 elif key in self.movement_keys:
                     self.send('TASK:MOVE ' + self.movement_keys[key])