home · contact · privacy
Added diagonal movement, with a 1.4 penalty.
[plomrogue] / src / client / misc.c
index 7022c6f25c751945dc9b0ea746e16212d49c8767..09268b53ede3da51043910582ee16cc539e988ba 100644 (file)
 /* src/client/misc.c */
 
 #include "misc.h"
-#include <ncurses.h> /* delwin(), endwin(), refresh() */
-#include <stdint.h> /* uint8_t, uint16_t */
-#include <string.h> /* memset(), strlen() */
-#include "cleanup.h" /* for set_cleanup_flag() */
-#include "keybindings.h" /* init_keybindings(), free_keybindings(),
-                          * save_keybindings()
+#include <ncurses.h> /* delwin() */
+#include <stddef.h> /* NULL */
+#include <stdint.h> /* uint8_t, uint32_t */
+#include <stdio.h> /* FILE, sprintf() */
+#include <stdlib.h> /* free(), exit() */
+#include <string.h> /* memcpy(), strlen() */
+#include <unistd.h> /* global optarg, getopt() */
+#include "../common/err_try_fgets.h" /* reset_err_try_fgets_counter() */
+#include "../common/readwrite.h" /* try_fopen(), try_fclose(), textfile_sizes(),
+                                  * try_fclose_unlink_rename(),
+                                  */
+#include "../common/rexit.h" /* exit_err() */
+#include "../common/try_malloc.h" /* try_malloc() */
+#include "cleanup.h" /* set_cleanup_flag() */
+#include "keybindings.h" /* free_keybindings(), read_keybindings_from_file(),
+                          * write_keybindings_to_file()
                           */
-#include "map_window.h" /* for map_center() */
-#include "wincontrol.h" /* struct WinConf, init_winconfs(), init_wins(),
-                         * sorted_wintoggle_and_activate(), get_win_by_id(),
-                         * get_winconf_by_win(), toggle_window()
-                         */
-#include "windows.h" /* struct Win, make_pad(), suspend_win(), free_win() */
+#include "map.h" /* map_center() */
+#include "windows.h" /* free_winDB(), make_v_screen_and_init_win_sizes(),
+                      * read_winconf_from_file(), write_winconf_of_id_to_file(),
+                      * toggle_window()
+                      */
 #include "world.h" /* global world */
 
 
 
-extern void save_interface_conf()
-{
-    save_keybindings("confclient/keybindings_global", &world.kb_global);
-    save_keybindings("confclient/keybindings_wingeom", &world.kb_wingeom);
-    save_keybindings("confclient/keybindings_winkeys", &world.kb_winkeys);
-    save_win_configs();
-}
-
-
-
-extern void load_interface_conf()
+extern void obey_argv(int argc, char * argv[])
 {
-    init_keybindings("confclient/keybindings_global",  &world.kb_global);
-    init_keybindings("confclient/keybindings_wingeom", &world.kb_wingeom);
-    init_keybindings("confclient/keybindings_winkeys", &world.kb_winkeys);
-    init_winconfs();
-    make_pad();
-    init_wins();
-    sorted_wintoggle_and_activate();
-    set_cleanup_flag(CLEANUP_INTERFACE);
+    int opt;
+    while (-1 != (opt = getopt(argc, argv, "i:")))
+    {
+        if      ('i' == opt)
+        {
+            world.path_interface = optarg;
+        }
+        else
+        {
+            exit(EXIT_FAILURE);
+        }
+    }
 }
 
 
 
-extern void unload_interface_conf()
+extern void save_interface_conf()
 {
-    free_keybindings(world.kb_global.kbs);
-    free_keybindings(world.kb_wingeom.kbs);
-    free_keybindings(world.kb_winkeys.kbs);
-    while (0 != world.wmeta.active)
+    char * f_name = "save_interface_conf()";
+    char * path = world.path_interface;
+    char path_tmp[strlen(path) + 4 + 1];
+    sprintf(path_tmp, "%s_tmp", path);
+    FILE * file = try_fopen(path_tmp, "w", f_name);
+    write_keybindings_to_file(file, &world.kb_global);
+    write_keybindings_to_file(file, &world.kb_wingeom);
+    write_keybindings_to_file(file, &world.kb_winkeys);
+    write_order_wins_visible_active(file);
+    uint8_t i;
+    for (i = 0; i < strlen(world.winDB.ids); i++)
     {
-        suspend_win(world.wmeta.active);
+        write_winconf_of_id_to_file(file, world.winDB.ids[i]);
     }
-    free_winconfs();
-    delwin(world.wmeta.pad);
+    try_fclose_unlink_rename(file, path_tmp, path, f_name);
 }
 
 
 
-extern void winch_called(int signal)
+extern void load_interface_conf()
 {
-    world.winch = 1;
+    char * f_name = "load_interface_conf()";
+
+    /* Read keybindings and WincConf DB from interface config file. */
+    reset_err_try_fgets_counter();
+    FILE * file = try_fopen(world.path_interface, "r", f_name);
+    uint32_t linemax = textfile_sizes(file, NULL);
+    char line[linemax + 1];
+    read_keybindings_from_file(line, linemax, file, &world.kb_global);
+    read_keybindings_from_file(line, linemax, file, &world.kb_wingeom);
+    read_keybindings_from_file(line, linemax, file, &world.kb_winkeys);
+    char active_tmp;
+    char * order_tmp;
+    read_order_wins_visible_active(line, linemax, file, &order_tmp, &active_tmp);
+    while (read_winconf_from_file(line, linemax, file));
+    try_fclose(file, f_name);
+
+    /* Check that windows of all legal IDs have been initalized. The validity of
+     * this test relies on read_winconf_from_file() failing on duplicates. Only
+     * on success initialize the windows as visible, to enable safe cleaning up.
+     */
+    char * err = "Failed to initialize all expected windows.";
+    exit_err(strlen(world.winDB.legal_ids) != strlen(world.winDB.ids), err);
+    world.winDB.active = active_tmp;
+    world.winDB.order = order_tmp;
+
+    /* Build windows as defined by read interface data and toggle them on. */
+    make_v_screen_and_init_win_sizes();
+    char tmp_active = world.winDB.active;
+    char tmp_order[strlen(world.winDB.order) + 1];
+    sprintf(tmp_order, "%s", world.winDB.order);
+    world.winDB.order[0] = '\0';
+    uint8_t i;
+    for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
+    world.winDB.active = tmp_active;
+
+    /* So that the interface config data and the window structs get freed. */
+    set_cleanup_flag(CLEANUP_INTERFACE);
 }
 
 
 
-extern void reset_windows()
+extern void unload_interface_conf()
 {
-    endwin();  /* "[S]tandard way" to recalibrate ncurses post SIGWINCH, says */
-    refresh(); /* <http://invisible-island.net/ncurses/ncurses-intro.html>.   */
-    struct Win * w_p = world.wmeta.chain_start;
-    char win_ids[strlen(world.winconf_db.winconf_ids) + 1];
-    memset(win_ids, '\0', strlen(world.winconf_db.winconf_ids) + 1);
-    uint8_t i = 0;
-    char active = '\0';
-    for (; NULL != w_p; w_p = w_p->next, i++)
-    {
-        struct WinConf * wc_p = get_winconf_by_win(w_p);
-        win_ids[i] = wc_p->id;
-        if (w_p == world.wmeta.active)
-        {
-            active = wc_p->id;
-        }
-    }
-    while (0 != world.wmeta.active)
-    {
-        w_p = world.wmeta.active;
-        suspend_win(w_p);
-        free_win(w_p);
-    }
-    delwin(world.wmeta.pad);
-    make_pad();
-    init_wins();
-    if (strlen(win_ids) < 1)
-    {
-        return;
-    }
-    for (i = 0; i < strlen(win_ids); i++)
+    free_keybindings(world.kb_global.kbs);
+    free_keybindings(world.kb_wingeom.kbs);
+    free_keybindings(world.kb_winkeys.kbs);
+    while ('\0' != world.winDB.active)
     {
-        toggle_window(win_ids[i]);
-        if (active == win_ids[i])
-        {
-            world.wmeta.active = get_win_by_id(win_ids[i]);
-        }
+        toggle_window(world.winDB.active);
     }
+    free_winDB();
+    delwin(world.winDB.v_screen);
 }
 
 
@@ -112,29 +129,7 @@ extern void reload_interface_conf()
     unload_interface_conf();
     load_interface_conf();
     map_center();
-}
-
-
-
-extern uint16_t center_offset(uint16_t position, uint16_t mapsize,
-                              uint16_t framesize)
-{
-    uint16_t offset = 0;
-    if (mapsize > framesize)
-    {
-        if (position > framesize / 2)
-        {
-            if (position < mapsize - (framesize / 2))
-            {
-                offset = position - (framesize / 2);
-            }
-            else
-            {
-                offset = mapsize - framesize;
-            }
-        }
-    }
-    return offset;
+    world.winDB.v_screen_offset = 0;
 }
 
 
@@ -157,3 +152,17 @@ extern void nav_inventory(char dir)
                                     + (world.player_inventory_select < n_elems);
 }
 
+
+
+extern void array_append(uint32_t old_n, size_t region_size, void * new_region,
+                        void ** ptr_old_array)
+{
+    char * f_name = "array_append()";
+    uint32_t old_size = old_n * region_size;
+    uint32_t new_size = old_size + region_size;
+    void * new_array = try_malloc(new_size, f_name);
+    memcpy(new_array, * ptr_old_array, old_size);
+    memcpy(new_array + (old_n * region_size), new_region, region_size);
+    free(* ptr_old_array);
+    * ptr_old_array = new_array;
+}