changeset 261:ac42afd962e4

UltraRogue: restrict changing name and save file. When the -n option is in use, player name and save file location cannot be changed. The score file is also restricted.
author John "Elwin" Edwards
date Sun, 12 Feb 2017 20:16:57 -0500
parents b80e1bf4eaec
children c7c6c5a7d840
files urogue/options.c urogue/save.c
diffstat 2 files changed, 103 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/urogue/options.c	Thu Feb 09 20:13:25 2017 -0500
+++ b/urogue/options.c	Sun Feb 12 20:16:57 2017 -0500
@@ -23,6 +23,8 @@
 #define NUM_OPTS    (sizeof optlist / sizeof (OPTION))
 #define EQSTR(a, b, c)  (strncmp(a, b, c) == 0)
 
+int get_restr(opt_arg *opt, WINDOW *win);
+
 /* description of an option and what to do with it */
 static OPTION optlist[] =
 {
@@ -30,10 +32,10 @@
 {"inven","Style of inventories (inven): ", &inv_type, put_inv, get_inv},
 {"askme","Ask me about unidentified things (askme): ",&askme,put_bool,get_bool},
 {"doorstop","Stop running when adjacent (doorstop): ",&doorstop,put_bool,get_bool},
-{"name", "Name (name): ",               &whoami, put_str, get_str},
+{"name", "Name (name): ",               &whoami, put_str, get_restr},
 {"fruit", "Fruit (fruit): ",            &fruit, put_str, get_str},
-{"file", "Save file (file): ",          &file_name, put_str, get_str},
-{"score", "Score file (score): ",       &score_file, put_str, get_str},
+{"file", "Save file (file): ",          &file_name, put_str, get_restr},
+{"score", "Score file (score): ",       &score_file, put_str, get_restr},
 {"class", "Character class (class): ",&char_type, put_abil, get_abil}
 };
 
@@ -311,6 +313,7 @@
         /* Look it up and deal with it */
 
         for (op = optlist; op < &optlist[NUM_OPTS]; op++)
+	{
             if (EQSTR(str, op->o_name, len))
             {
                 if (op->o_putfunc == put_bool)
@@ -334,6 +337,13 @@
 
                     strncpy(start, str, sp - str);
 
+		    /* Some options can't be changed. */
+		    if (use_savedir && 
+				    (op->o_opt.str == whoami || 
+				     op->o_opt.str == file_name || 
+				     op->o_opt.str == score_file))
+			break;
+
                     /* Put the value into the option field */
 
                     if (op->o_putfunc != put_abil &&
@@ -394,6 +404,7 @@
                 *op->o_opt.iarg = FALSE;
                 break;
             }
+	}
 
         /* skip to start of next option name  */
 
@@ -517,3 +528,56 @@
 
     return(NORM);
 }
+
+/* 
+ * get_restr()
+ *
+ * Gets strings that cannot be changed when use_savedir is set.
+ * get_abil() can't be repurposed to do this without ugliness.
+ *
+ */
+int
+get_restr(opt_arg *opt, WINDOW *win)
+{
+    int oy, ox, ny, nx;
+    int keep_up;
+
+    keep_up = TRUE;
+    getyx(win, oy, ox);
+    put_str(opt, win);
+
+    if (!use_savedir)
+	return get_str(opt, win);
+
+    getyx(win, ny, nx);
+    while(keep_up)
+    {
+        wmove(win, oy, ox);
+        wrefresh(win);
+
+        switch(readcharw(win))
+        {
+            case '\n':
+            case '\r':
+                keep_up = FALSE;
+                break;
+
+            case '\033':
+            case '\007':
+                return(QUIT);
+
+            case '-':
+                return(MINUS);
+
+            default:
+                mvwaddstr(win, ny, nx + 5, "(no change allowed)");
+        }
+    }
+
+    wmove(win, ny, nx + 5);
+    wclrtoeol(win);
+    wmove(win, ny, nx);
+    waddch(win, '\n');
+
+    return(NORM);
+}
--- a/urogue/save.c	Thu Feb 09 20:13:25 2017 -0500
+++ b/urogue/save.c	Sun Feb 12 20:16:57 2017 -0500
@@ -25,6 +25,8 @@
 #include <errno.h>
 #include "rogue.h"
 
+int save_savedir_game(void);
+
 int
 save_game(void)
 {
@@ -32,6 +34,9 @@
     char    buf[2 * LINELEN];
     char    oldfile[2*LINELEN];
 
+    if (use_savedir)
+	return save_savedir_game();
+
     /* get file name */
 
     strcpy(oldfile,file_name);
@@ -79,6 +84,37 @@
     return(TRUE);
 }
 
+/*
+ * save_savedir_game()
+ * Simplified save function for when system savefiles are used.
+ */
+int
+save_savedir_game(void)
+{
+    FILE *savef;
+    char c;
+
+    mpos = 0;
+    msg("Save game? ");
+    c = readcharw(cw);
+    if (c == 'y' || c == 'Y')
+    {
+        if ((savef = fopen(file_name, "w")) == NULL)
+	{
+            msg(strerror(errno));
+            return(FALSE);
+	}
+        msg("");
+        save_file(savef);
+        return(TRUE);
+    }
+    else
+    {
+        msg("");
+        return(FALSE);
+    }
+}
+
 int
 restore(char *file)
 {