From 7e43b1fff89706212291384ddaa762fa30b02cb4 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Tue, 8 Jul 2014 04:38:02 +0200
Subject: [PATCH] Client: Add auto-center of map on player each new turn (can
 be toggled).

---
 confclient/commands              |  3 +++
 confclient/interface_conf        |  1 +
 confclient/single_wins/info      |  1 +
 confclient/single_wins/inventory |  1 +
 confclient/single_wins/log       |  1 +
 confclient/single_wins/map       |  1 +
 src/client/control.c             |  3 ++-
 src/client/io.c                  | 12 +++++++++---
 src/client/io.h                  |  3 ++-
 src/client/main.c                |  1 +
 src/client/map.c                 |  7 +++++++
 src/client/map.h                 |  3 ++-
 src/client/world.h               |  1 +
 13 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/confclient/commands b/confclient/commands
index 7cfa785..e4efa8a 100644
--- a/confclient/commands
+++ b/confclient/commands
@@ -179,3 +179,6 @@ DESCRIPTION 'window geometry keys'
 
 COMMAND to_wk_keywin
 DESCRIPTION 'window keybinding keys'
+
+COMMAND to_autofocus
+DESCRIPTION 'toggle auto map center'
diff --git a/confclient/interface_conf b/confclient/interface_conf
index 156aa4e..8a4a36f 100644
--- a/confclient/interface_conf
+++ b/confclient/interface_conf
@@ -100,6 +100,7 @@ KEY 258 map_d
 KEY 260 map_l
 KEY 261 map_r
 KEY 46 map_c
+KEY 70 to_autofocus
 
 WINDOW 0
 NAME 'Global keys'
diff --git a/confclient/single_wins/info b/confclient/single_wins/info
index 45c9457..80343ef 100644
--- a/confclient/single_wins/info
+++ b/confclient/single_wins/info
@@ -26,6 +26,7 @@ KEY 119 move_w
 KEY 46 map_c
 KEY 68 drop
 KEY 117 use
+KEY 70 to_autofocus
 
 KEYBINDINGS 'wingeom'
 KEY 258 shift_f
diff --git a/confclient/single_wins/inventory b/confclient/single_wins/inventory
index 8e774fb..c9e8930 100644
--- a/confclient/single_wins/inventory
+++ b/confclient/single_wins/inventory
@@ -26,6 +26,7 @@ KEY 119 move_w
 KEY 46 map_c
 KEY 68 drop
 KEY 117 use
+KEY 70 to_autofocus
 
 KEYBINDINGS 'wingeom'
 KEY 258 shift_f
diff --git a/confclient/single_wins/log b/confclient/single_wins/log
index 52429b9..9944687 100644
--- a/confclient/single_wins/log
+++ b/confclient/single_wins/log
@@ -26,6 +26,7 @@ KEY 119 move_w
 KEY 46 map_c
 KEY 68 drop
 KEY 117 use
+KEY 70 to_autofocus
 
 KEYBINDINGS 'wingeom'
 KEY 258 shift_f
diff --git a/confclient/single_wins/map b/confclient/single_wins/map
index bc63955..d3a48bb 100644
--- a/confclient/single_wins/map
+++ b/confclient/single_wins/map
@@ -26,6 +26,7 @@ KEY 119 move_w
 KEY 46 map_c
 KEY 68 drop
 KEY 117 use
+KEY 70 to_autofocus
 
 KEYBINDINGS 'wingeom'
 KEY 258 shift_f
diff --git a/src/client/control.c b/src/client/control.c
index d5d8d9e..43041ea 100644
--- a/src/client/control.c
+++ b/src/client/control.c
@@ -10,7 +10,7 @@
 #include "keybindings.h" /* get_command_to_keycode(), get_keycode_to_command(),
                           * mod_selected_keyb(), move_keyb_selection()
                           */
-#include "map.h" /* for map_scroll(), map_center() */
+#include "map.h" /* for map_scroll(), map_center(), toggle_autofocus() */
 #include "wincontrol.h" /* shift_active_win(), resize_active_win(),
                          * toggle_win_size_type(), toggle_window(),
                          * cycle_active_win(), scroll_v_screen(),
@@ -107,6 +107,7 @@ static uint8_t try_2args(struct Command * command, char * match,
 static uint8_t try_client_commands(struct Command * command)
 {
     return (   try_0args(command, "map_c", map_center)
+            || try_0args(command, "to_autofocus", toggle_autofocus)
             || try_1args(command, "map_u", map_scroll, '8')
             || try_1args(command, "map_d", map_scroll, '2')
             || try_1args(command, "map_r", map_scroll, '6')
diff --git a/src/client/io.c b/src/client/io.c
index e304723..31387f2 100644
--- a/src/client/io.c
+++ b/src/client/io.c
@@ -68,8 +68,8 @@ static FILE * changed_worldstate_file(char * path);
  * out file wasn't read for supposedly not having changed since a last
  * read_world() call.
  *
- * map_center() is triggered by the first successful read_world() or on turn 1,
- * so the client focuses the map window on the player on client and world start.
+ * map_center() is triggered by either, the first successful read_world() (thus
+ * on client start), or on turn 1 (thus on world start).
  */
 static uint8_t read_world();
 
@@ -281,6 +281,7 @@ extern char * io_loop()
     world.halfdelay = 1;            /* Ensures read_world() is only called 10 */
     halfdelay(world.halfdelay);     /* times a second during user inactivity. */
     uint8_t change_in_client = 0;
+    uint16_t last_focused_turn = world.turn;
     time_t last_server_answer_time = time(0);
     while (1)
     {
@@ -292,8 +293,13 @@ extern char * io_loop()
             world.winch = 0;
             change_in_client++;
         }
-        if (read_world() || change_in_client)
+        if (change_in_client || read_world())
         {
+            if (world.turn != last_focused_turn && world.focus_each_turn)
+            {
+                last_focused_turn = world.turn;
+                map_center();
+            }
             draw_all_wins();
         }
         change_in_client = 0;
diff --git a/src/client/io.h b/src/client/io.h
index 4db04cf..072a217 100644
--- a/src/client/io.h
+++ b/src/client/io.h
@@ -25,7 +25,8 @@ extern void send(char * msg);
  * loop ends regularly (due to the user sending a quit command), return an
  * appropriate quit message to write to stdout when the client winds down. Call
  * reset_windows() on receiving a SIGWINCH. Abort on assumed server death if the
- * server's out file does not get updated, even on PING requests.
+ * server's out file does not get updated, even on PING requests. Re-focus map
+ * view on player if world.focus_each_turn is set.
  */
 extern char * io_loop();
 
diff --git a/src/client/main.c b/src/client/main.c
index 1821e57..f2051a4 100644
--- a/src/client/main.c
+++ b/src/client/main.c
@@ -52,6 +52,7 @@ int main(int argc, char * argv[])
     keypad(world.winDB.t_screen, TRUE);
     init_command_db();      /* The command DB needs to be initialized before  */
     load_interface_conf();  /* the interface, whose keybindings depend on it. */
+    world.focus_each_turn = 1;
 
     /* Set handler for terminal window resizing. */
     struct sigaction act;
diff --git a/src/client/map.c b/src/client/map.c
index 7c2dcc3..2a1d66f 100644
--- a/src/client/map.c
+++ b/src/client/map.c
@@ -45,3 +45,10 @@ extern void map_center()
     win_map->center.y = world.player_pos.y;
     win_map->center.x = world.player_pos.x * 2;
 }
+
+
+
+extern void toggle_autofocus()
+{
+    world.focus_each_turn = world.focus_each_turn ? 0 : 1;
+}
diff --git a/src/client/map.h b/src/client/map.h
index 6cef27e..f8c6c9f 100644
--- a/src/client/map.h
+++ b/src/client/map.h
@@ -14,6 +14,7 @@ extern void map_scroll(char d);
 /* Center map window on player (even if it is non-visible). */
 extern void map_center();
 
-
+/* Toggle world.focus_each_turn (auto-centering of map on player each turn). */
+extern void toggle_autofocus();
 
 #endif
diff --git a/src/client/world.h b/src/client/world.h
index 8d490c8..5d50173 100644
--- a/src/client/world.h
+++ b/src/client/world.h
@@ -38,6 +38,7 @@ struct World
     uint8_t player_inventory_select; /* index of selected item in inventory */
     uint8_t player_lifepoints; /* how alive the player is */
     uint8_t winch; /* if set, SIGWINCH was registered; trigger reset_windows()*/
+    uint8_t focus_each_turn; /* if !0, re-focus map on player each new turn */
 };
 
 
-- 
2.30.2