home · contact · privacy
Refactored similar array append activities into array_append().
[plomrogue] / src / client / io.c
index 32843896143ae3202e17bc4b5074c8bcaf6b9072..481495d81fdb295ea0f00d622c9681a9bbbdfd22 100644 (file)
@@ -5,6 +5,7 @@
 #include <fcntl.h> /* open() */
 #include <limits.h> /* PIPE_BUF */
 #include <ncurses.h> /* halfdelay(), getch() */
+#include <stddef.h> /* NULL */
 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
 #include <stdio.h> /* FILE, sprintf(), fseek() */
 #include <string.h> /* strcmp(), strlen(), memcpy() */
 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), try_fgets(),
                                   * try_fgetc()
                                   */
-#include "control.h" /* meta_control(), player_control(), wingeom_control(),
-                      * winkeyb_control()
-                      */
-#include "map_window.h" /* for map_center() */
-#include "wincontrol.h" /* WinConf struct, get_winconf_by_win() */
-#include "windows.h" /* draw_all_wins() */
+#include "control.h" /* try_key() */
+#include "map_window.h" /* map_center() */
+#include "misc.h" /* reset_windows() */
+#include "windows.h" /* reset_windows_on_winch(), draw_all_wins() */
 #include "world.h" /* world global */
 
 
@@ -68,8 +67,8 @@ static FILE * changed_server_out_file(char * path);
  * a hard-coded serialization format. Returns 1 on success and 0 if the out file
  * wasn't read for supposedly not having changed since a last read_world() call.
  *
- * Note that the first successful read_world() triggers map_center(), so that on
- * start the client focuses the map window on the player.
+ * 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.
  */
 static uint8_t read_world();
 
@@ -80,7 +79,7 @@ static void read_inventory(char * read_buf, uint32_t linemax, FILE * file)
     char * f_name = "read_inventory()";
     char * delimiter = "%\n";
     free(world.player_inventory);
-    world.player_inventory = NULL;
+    world.player_inventory = NULL;          /* Avoids illegal strlen() below. */
     while (1)
     {
         try_fgets(read_buf, linemax + 1, file, f_name);
@@ -197,12 +196,12 @@ static uint8_t read_world()
     uint32_t linemax = textfile_sizes(file, NULL);
     char * read_buf = try_malloc(linemax + 1, f_name);
     world.turn = read_value_from_line(read_buf, linemax, file);
-    world.score = read_value_from_line(read_buf, linemax, file);
+    world.player_score = read_value_from_line(read_buf, linemax, file);
     world.player_lifepoints = read_value_from_line(read_buf, linemax, file);
     read_inventory(read_buf, linemax, file);
     world.player_pos.y = read_value_from_line(read_buf, linemax, file);
     world.player_pos.x = read_value_from_line(read_buf, linemax, file);
-    if (first_read)
+    if (1 == world.turn || first_read)
     {
         map_center();
         first_read = 0;
@@ -260,6 +259,12 @@ extern char * io_loop()
     uint8_t change_in_client = 0;
     while (1)
     {
+        if (world.winch)
+        {
+            reset_windows_on_winch();
+            world.winch = 0;
+            change_in_client++;
+        }
         if (read_world() || change_in_client)
         {
             draw_all_wins();
@@ -268,21 +273,11 @@ extern char * io_loop()
         int key = getch();
         if (ERR != key)
         {
-            change_in_client = meta_control(key);
+            change_in_client = try_key((uint16_t) key);
             if (2 == change_in_client)
             {
                 break;
             }
-            if (!change_in_client)
-            {
-                change_in_client = player_control(key);
-            }
-            if (!change_in_client)
-            {
-                struct WinConf * wc = get_winconf_by_win(world.wmeta.active);
-                change_in_client =    (1 == wc->view && wingeom_control(key))
-                                   || (2 == wc->view && winkeyb_control(key));
-            }
         }
     }
     try_send("QUIT");