home · contact · privacy
Add basic food clock (but no consumables yet to re-set it).
[plomrogue] / src / client / draw_wins.c
index 5c35dd6cc2939e5f2176b1a6aec3e9cbb08b14c3..9857af522565dee13086b9deeaa3250c7065a826 100644 (file)
@@ -1,18 +1,22 @@
-/* src/client/draw_wins.c */
+/* src/client/draw_wins.c
+ *
+ * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
+ * or any later version. For details on its copyright, license, and warranties,
+ * see the file NOTICE in the root directory of the PlomRogue source package.
+ */
 
 #define _POSIX_C_SOURCE 200809L /* strdup() */
 #include "draw_wins.h"
-#include <ncurses.h> /* typedefs attr_t, chtype, define A_REVERSE */
+#include <ncurses.h> /* attr_t, chtype, init_pair(), A_REVERSE, COLOR_PAIR() */
 #include <stddef.h> /* NULL */
-#include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT16_MAX */
+#include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT16_MAX, UINT32_MAX */
 #include <stdio.h> /* sprintf() */
 #include <stdlib.h> /* free() */
-#include <string.h> /* memset(), strchr(), strdup/(), strlen() */
-#include "../common/rexit.h" /* exit_err() */
+#include <string.h> /* memset(), strcmp(), strchr(), strdup/(), strlen() */
+#include "../common/rexit.h" /* exit_err(), exit_trouble() */
 #include "../common/try_malloc.h" /* try_malloc() */
-#include "../common/yx_uint16.h" /* struct yx_uint16 */
 #include "keybindings.h" /* struct KeyBindingDB, get_keyname_to_keycode() */
-#include "windows.h" /* struct Win, get_win_by_id() */
+#include "windows.h" /* yx_uint16, Win */
 #include "world.h" /* global world */
 
 
@@ -52,25 +56,27 @@ static void add_line_compact(struct Win * win, char * line, attr_t attri,
 static void add_text_with_linebreaks(struct Win * win, char * text);
 static void draw_text_from_bottom(struct Win * win, char * text);
 
-/* Return a properly formatted keybinding list line for "kb". */
-static char * get_kb_line(struct KeyBinding * kb);
-
 /* Draw from line "start" on config view for keybindings defined at "kb". */
 static void draw_keybinding_config(struct Win * win, struct KeyBindingDB * kbdb,
                                    uint16_t offset);
 
-/* Draw into window "w" from line "start" on a "title" followed by an empty
- * line followed by a list of all keybindings starting in "kbdb".
+/* String building helper functions to draw_winconf_geometry(). */
+static char * get_keyname_to_command(char * command_name);
+static char * winconf_geom_helper(struct Win * win, char axis, char * sep,
+                                  char * newlines, char * value_prefix);
+
+
+
+/* try_resize_winmap() only realloc's Win->winmap if this is set. Use this to
+ * skip inflationary malloc's by successive try_resize_winmap() calls when the
+ * memory needed can be pre-calculated.
  */
-static void draw_titled_keybinding_list(char * title, struct Win * win,
-                                        uint16_t * offset, uint8_t last,
-                                        struct KeyBindingDB * kbdb);
+static uint8_t do_realloc_winmap = 1;
 
 
 
 static void try_resize_winmap(struct Win * win, int new_size_y, int new_size_x)
 {
-    char * f_name = "try_resize_winmap()";
     if (win->winmap_size.y >= new_size_y && win->winmap_size.x >= new_size_x)
     {
         return;
@@ -83,23 +89,26 @@ static void try_resize_winmap(struct Win * win, int new_size_y, int new_size_x)
     {
         new_size_x = win->winmap_size.x;
     }
-    chtype * old_winmap = win->winmap;
-    uint32_t new_size = sizeof(chtype) * new_size_y * new_size_x;
-    win->winmap = try_malloc(new_size, f_name);
-    uint16_t y, x;
-    for (y = 0; y < new_size_y; y++)
+    if (do_realloc_winmap)
     {
-        for (x = 0; y < win->winmap_size.y && x < win->winmap_size.x; x++)
+        chtype * old_winmap = win->winmap;
+        uint32_t new_size = sizeof(chtype) * new_size_y * new_size_x;
+        win->winmap = try_malloc(new_size, __func__);
+        uint16_t y, x;
+        for (y = 0; y < new_size_y; y++)
         {
-            chtype ch = old_winmap[(y * win->winmap_size.x) + x];
-            win->winmap[(y * new_size_x) + x] = ch;
-        }
-        for (; x < new_size_x; x++)
-        {
-            win->winmap[(y * new_size_x) + x] = ' ';
+            for (x = 0; y < win->winmap_size.y && x < win->winmap_size.x; x++)
+            {
+                chtype ch = old_winmap[(y * win->winmap_size.x) + x];
+                win->winmap[(y * new_size_x) + x] = ch;
+            }
+            for (; x < new_size_x; x++)
+            {
+                win->winmap[(y * new_size_x) + x] = ' ';
+            }
         }
+        free(old_winmap);
     }
-    free(old_winmap);
     win->winmap_size.y = new_size_y;
     win->winmap_size.x = new_size_x;
 }
@@ -184,8 +193,9 @@ static void add_line_compact(struct Win * win, char * line, attr_t attri,
     uint16_t len_line = strlen(line);
     char * separator = last ? "" : " / ";
     uint32_t len_line_new = len_line + strlen(separator);
-    char line_new[len_line_new];
-    sprintf(line_new, "%s%s", line, separator);
+    char * line_new = try_malloc(len_line_new + 1, __func__);
+    int test = sprintf(line_new, "%s%s", line, separator);
+    exit_trouble(test < 0, __func__, "sprintf");
     uint16_t x = 0;
     uint16_t y;
     uint32_t z;
@@ -205,6 +215,7 @@ static void add_line_compact(struct Win * win, char * line, attr_t attri,
             try_resize_winmap(win, y + 1 + 1, win->winmap_size.x);
         }
     }
+    free(line_new);
     *offset = x;
 }
 
@@ -247,7 +258,7 @@ static void draw_text_from_bottom(struct Win * win, char * text)
         }
         while (new_y_start > win->winmap_size.y);
         if (2 == win->linebreak) /* add_text_with_linebreaks() will start not */
-        {                        /* not after, but within the last line then. */
+        {                        /* after, but within the last line then.     */
             add_line_wide(win, " ", 0);
         }
         add_text_with_linebreaks(win, text);
@@ -256,19 +267,6 @@ static void draw_text_from_bottom(struct Win * win, char * text)
 
 
 
-static char * get_kb_line(struct KeyBinding * kb)
-{
-    char * f_name = "get_kb_line()";
-    char * keyname = get_keyname_to_keycode(kb->keycode);
-    uint16_t size = strlen(keyname) + 3 + strlen(kb->command->dsc_long) + 1;
-    char * kb_line = try_malloc(size, f_name);
-    sprintf(kb_line, "%s - %s", keyname, kb->command->dsc_long);
-    free(keyname);
-    return kb_line;
-}
-
-
-
 static void draw_keybinding_config(struct Win * win, struct KeyBindingDB * kbdb,
                                    uint16_t offset)
 {
@@ -290,7 +288,13 @@ static void draw_keybinding_config(struct Win * win, struct KeyBindingDB * kbdb,
             }
             win->center.y = win->winmap_size.y;
         }
-        char * kb_line = get_kb_line(&kbdb->kbs[kb_n]);
+        struct KeyBinding kb = kbdb->kbs[kb_n];
+        char * keyname = get_keyname_to_keycode(kb.keycode);
+        uint16_t size = strlen(keyname) + 3 + strlen(kb.command->dsc_long) + 1;
+        char * kb_line = try_malloc(size, __func__);
+        int test = sprintf(kb_line, "%s - %s", keyname, kb.command->dsc_long);
+        exit_trouble(test < 0, __func__, "sprintf");
+        free(keyname);
         add_line(win, kb_line, attri, &offset, (kbdb->n_of_kbs == kb_n + 1));
         free(kb_line);
     }
@@ -298,30 +302,47 @@ static void draw_keybinding_config(struct Win * win, struct KeyBindingDB * kbdb,
 
 
 
-static void draw_titled_keybinding_list(char * title, struct Win * win,
-                                        uint16_t * offset, uint8_t last,
-                                        struct KeyBindingDB * kbdb)
+static char * get_keyname_to_command(char * command_name)
 {
-    uint8_t state = 0;
-    uint8_t kb_n = 0;
-    while (0 == state || kb_n < kbdb->n_of_kbs)
+    uint8_t i = 0;
+    for (i = 0; i < world.kb_wingeom.n_of_kbs; i++)
     {
-        if (0 == state)
+        if (!strcmp(world.kb_wingeom.kbs[i].command->dsc_short, command_name))
         {
-            add_line(win, title, 0, offset, 0);
-            add_line(win, " ", 0, offset, 0);
-            state = 1 + (0 == kbdb->n_of_kbs);
-            continue;
+            return get_keyname_to_keycode(world.kb_wingeom.kbs[i].keycode);
         }
-        char * kb_line = get_kb_line(&kbdb->kbs[kb_n]);
-        add_line(win, kb_line, 0, offset, (last * kbdb->n_of_kbs == kb_n + 1));
-        free(kb_line);
-        kb_n++;
     }
-    if (2 == state)
+    return "UNDEFINED";
+}
+
+
+
+static char * winconf_geom_helper(struct Win * win, char axis, char * sep,
+                                  char * newlines, char * value_prefix)
+{
+    char * p0 = 'v'==axis? "Height" : "Width";
+    char * p1 = " to save (grow/shrink/toggle positivity with ";
+    char * p2 = get_keyname_to_command('v'==axis? "grow_v" : "grow_h");
+    char * p4 = get_keyname_to_command('v'==axis? "shri_v" : "shri_v");
+    char * p6 = get_keyname_to_command('v'==axis? "to_height_t" : "to_width_t");
+    char p8[6 + 1];                            /* 6: int16_t value max strlen */
+    int test = sprintf(p8,"%d", 'v'==axis?win->target_height:win->target_width);
+    exit_trouble(test < 0, __func__, "sprintf");
+    char * p9 = " (";
+    char * p10 = "in_cells";
+    if (1 == ('v'==axis? win->target_height_type : win->target_width_type))
     {
-        add_line(win, "(none)", 0, offset, last);
+        p10 = "non-positive diff: cells to screen maximum";
     }
+    char * p11 = ")";
+    uint8_t size =   strlen(p0) + strlen(p1) + strlen(p2) + strlen(sep)
+                   + strlen(p4) + strlen(sep) + strlen(p6) +strlen(value_prefix)
+                   + strlen(p8) + strlen(p9) + strlen(p10) + strlen(p11)
+                   + strlen(newlines) + 1;
+    char * msg = try_malloc(size, __func__);
+    sprintf(msg, "%s%s%s%s%s%s%s%s%s%s%s%s%s", p0, p1, p2, sep, p4, sep, p6,
+            value_prefix, p8, p9, p10, p11, newlines);
+    return msg;
 }
 
 
@@ -332,6 +353,27 @@ extern void draw_win_log(struct Win * win)
     {
         return;
     }
+    uint32_t x, i, n_postbreak_lines;
+    for (i = 0, x = 0, n_postbreak_lines = 1; i < strlen(world.log); i++)
+    {
+        exit_err(i == UINT32_MAX, "Log too large.");
+        x++;
+        if (x > win->frame_size.x || '\n' == world.log[i])
+        {
+            n_postbreak_lines++;
+            x = 0;
+        }
+    }
+    if (n_postbreak_lines > win->frame_size.y)
+    {
+        uint32_t size = n_postbreak_lines * (win->frame_size.x + 1);
+        win->winmap = try_malloc(sizeof(chtype) * size, __func__);
+        for (i = 0; i < size; win->winmap[i] = ' ', i++);
+        do_realloc_winmap = 0;
+        draw_text_from_bottom(win, world.log);
+        do_realloc_winmap = 1;
+        return;
+    }
     draw_text_from_bottom(win, world.log);
 }
 
@@ -339,17 +381,43 @@ extern void draw_win_log(struct Win * win)
 
 extern void draw_win_map(struct Win * win)
 {
-    try_resize_winmap(win, world.map.size.y, world.map.size.x * 2);
-    uint16_t z = 0;
     uint16_t x, y;
-    for (y = 0; y < world.map.size.y; y++)
+    init_pair(1, COLOR_WHITE, COLOR_BLUE);
+    init_pair(2, COLOR_BLUE, COLOR_BLACK);
+    attr_t attr_mem = COLOR_PAIR(2);
+    attr_t attr_sha = COLOR_PAIR(1);
+    try_resize_winmap(win, world.map.length, world.map.length * 2 + 1);
+    for (y = 0; y < world.map.length; y++)
     {
-        for (x = 0; x < world.map.size.x; x++)
+        for (x = 0; x < world.map.length; x++)
         {
-            set_ch_on_yx(win, y, x * 2 + (y % 2), world.map.cells[z]);
-            z++;
+            attr_t a=' '==world.mem_map[y*world.map.length+x]?attr_sha:attr_mem;
+            char c = world.mem_map[y*world.map.length + x];
+            set_ch_on_yx(win, y, x * 2 + (y % 2),     c   | a);
+            set_ch_on_yx(win, y, x * 2 + (y % 2) + 1, ' ' | a);
+        }
+    }
+    for (y = 0; y < world.map.length; y++)
+    {
+        for (x = 0; x < world.map.length; x++)
+        {
+            if (' ' != world.map.cells[y*world.map.length + x])
+            {
+                char c = world.map.cells[y*world.map.length + x];
+                set_ch_on_yx(win, y, x * 2 + (y % 2),     c);
+                set_ch_on_yx(win, y, x * 2 + (y % 2) + 1, ' ');
+            }
         }
     }
+    if (world.look)
+    {
+        y = world.look_pos.y;
+        x = world.look_pos.x;
+        char c = world.map.cells[y * world.map.length + x];
+        c = ' ' == c ? world.mem_map[y * world.map.length + x] : c;
+        set_ch_on_yx(win, y, x * 2 + (y % 2),     c   | A_REVERSE);
+        set_ch_on_yx(win, y, x * 2 + (y % 2) + 1, ' ' | A_REVERSE);
+    }
 }
 
 
@@ -358,11 +426,16 @@ extern void draw_win_info(struct Win * win)
 {
     char * dsc_turn      = "Turn: ";
     char * dsc_hitpoints = "\nHitpoints: ";
-    uint16_t maxl = strlen(dsc_turn) + 5 + strlen(dsc_hitpoints) + 3;
-    char text[maxl + 1];
-    sprintf(text, "%s%d%s%d",
-            dsc_turn, world.turn, dsc_hitpoints, world.player_lifepoints);
+    char * dsc_satiation = "\nSatiation: ";
+    uint16_t maxl =   strlen(dsc_turn) + 5 + strlen(dsc_hitpoints)
+                    + strlen(dsc_satiation) + 6 + 3;
+    char * text = try_malloc(maxl + 1, __func__);
+    int test = sprintf(text, "%s%d%s%d%s%d", dsc_turn, world.turn, dsc_hitpoints,
+                       world.player_lifepoints, dsc_satiation,
+                       world.player_satiation);
+    exit_trouble(test < 0, __func__, "sprintf");
     add_text_with_linebreaks(win, text);
+    free(text);
 }
 
 
@@ -396,29 +469,15 @@ extern void draw_win_inventory(struct Win * win)
 
 
 
-extern void draw_win_available_keybindings(struct Win * win)
+extern void draw_win_terrain_stack(struct Win * win)
 {
-    char * title = "Active window's keys:";
-    struct Win * win_active = get_win_by_id(world.winDB.active);
-    struct KeyBindingDB * kbdb = &win_active->kb;
-    if      (1 == win_active->view)
-    {
-        kbdb = &world.kb_wingeom;
-    }
-    else if (2 == win_active->view)
-    {
-        kbdb = &world.kb_winkeys;
-    }
-    uint16_t offset = 0;
-    draw_titled_keybinding_list(title, win, &offset, 0, kbdb);
-    add_line(win, " ", 0, &offset, 0);
-    title = "Global keys:";
-    draw_titled_keybinding_list(title, win, &offset, 1, &world.kb_global);
+    char * wait_response = "(polling)";
+    char * text = world.things_here ? world.things_here : wait_response;
+    add_text_with_linebreaks(win, text);
 }
 
 
 
-
 extern void draw_win_keybindings_global(struct Win * win)
 {
     win->center.y = world.kb_global.select;
@@ -456,28 +515,33 @@ extern void draw_winconf_keybindings(struct Win * win)
 
 extern void draw_winconf_geometry(struct Win * win)
 {
-    char * title = "Window's geometry:\n\n";
-    char * h_title = "Height to save: ";
-    char h_value[6 + 1];                       /* 6: int16_t value max strlen */
-    sprintf(h_value, "%d", win->target_height);
-    char * h_plus = " (width in cells)\n\n";
-    char * h_minus = " (negative diff: cells to screen width)\n\n";
-    char * h_type = (1 == win->target_height_type) ? h_minus : h_plus;
-    char * w_title = "Width to save: ";
-    char w_value[6 + 1];
-    sprintf(w_value, "%d", win->target_width);
-    char * w_plus = "(height in cells)\n\n";
-    char * w_minus = " (negative diff: cells to screen height)\n\n";
-    char * w_type = (1 == win->target_width_type)  ? w_minus : w_plus;
-    char * breaks_title = "Linebreak type: ";
-    char * breaks_type = (1 == win->linebreak) ? "wide" : "long";
-    breaks_type        = (2 == win->linebreak) ? "compact" : breaks_type;
-    uint16_t text_size =   strlen(title)
-                         + strlen(h_title) + strlen(h_value) + strlen(h_type)
-                         + strlen(w_title) + strlen(w_value) + strlen(w_type)
-                         + strlen(breaks_title) + strlen(breaks_type);
-    char text[text_size + 1];
-    sprintf(text, "%s%s%s%s%s%s%s%s%s", title, h_title, h_value, h_type,
-            w_title, w_value, w_type, breaks_title, breaks_type);
+    char * sep = "/";
+    char * newlines = "\n\n";
+    char * value_prefix = "): ";
+    char * title = "Window's geometry:\n\nShift up/down with ";
+    char * key_shift_b = get_keyname_to_command("shift_b");
+    char * key_shift_f = get_keyname_to_command("shift_f");
+    char * height = winconf_geom_helper(win, 'v', sep, newlines, value_prefix);
+    char * width = winconf_geom_helper(win, 'h', sep, newlines, value_prefix);
+    char * breaks_title = "Linebreak type (toggle with ";
+    char * key_to_break = get_keyname_to_command("to_break");
+    char * breaks_type = "long";
+    if (win->linebreak)
+    {
+        breaks_type = (1 == win->linebreak) ? "wide" : "compact";
+    }
+    uint16_t text_size =   strlen(title) + strlen(key_shift_b) + strlen(sep)
+                         + strlen(key_shift_f) + strlen(newlines)
+                         + strlen(height) + strlen(width)
+                         + strlen(breaks_title) + strlen(key_to_break)
+                         + strlen(value_prefix) + strlen(breaks_type);
+    char * text = try_malloc(text_size + 1, __func__);
+    int test = sprintf(text, "%s%s%s%s%s%s%s%s%s%s%s", title, key_shift_b, sep,
+                       key_shift_f, newlines, height, width, breaks_title,
+                       key_to_break, value_prefix, breaks_type);
+    free(height);
+    free(width);
+    exit_trouble(test < 0, __func__, "sprintf");
     add_text_with_linebreaks(win, text);
+    free(text);
 }