diff rogue4/main.c @ 13:63b9fd7d70ce

rogue4: add -n option and system savedir
author edwarj4
date Sat, 31 Oct 2009 01:51:52 +0000
parents 9535a08ddc39
children ea7372f5d314
line wrap: on
line diff
--- a/rogue4/main.c	Sat Oct 24 16:52:52 2009 +0000
+++ b/rogue4/main.c	Sat Oct 31 01:51:52 2009 +0000
@@ -71,16 +71,36 @@
 	}
 #endif
 
+#ifdef SAVEDIR
+    if (argc >= 3 && !strcmp(argv[1], "-n"))
+    {
+        strncpy(whoami, argv[2], MAXSTR - 1);
+        whoami[MAXSTR - 1] = '\0'; /* insurance */
+        use_savedir = TRUE;
+        /* look for savefile at SAVEDIR/UIDplayername.r4sav */
+        if (snprintf(file_name, MAXSTR, "%s%d%.10s.r4sav", SAVEDIR, md_getuid(),
+            whoami) >= MAXSTR)
+        {
+            /* Name is too long- this shouldn't happen */
+            strcpy(file_name, "rogue4.save");
+            use_savedir = FALSE;
+        }
+    }
+#endif
+
     /*
      * get home and options from environment
      */
     strncpy(home, md_gethomedir(), PATH_MAX);
-    strcpy(file_name, home);
-    strcat(file_name, "rogue52.sav");
+    if (!use_savedir)
+    {
+        strcpy(file_name, home);
+        strcat(file_name, "/rogue4.save");
+    }
 
     if ((env = getenv("ROGUEOPTS")) != NULL)
 	parse_opts(env);
-    if (env == NULL || whoami[0] == '\0')
+    if (!use_savedir && (env == NULL || whoami[0] == '\0'))
 	strucpy(whoami, md_getusername(md_getuid()), strlen(md_getusername(md_getuid())));
     if (env == NULL || fruit[0] == '\0')
 	strcpy(fruit, "slime-mold");
@@ -96,12 +116,27 @@
 	exit(0);
     }
     init_check();			/* check for legal startup */
-    if (argc == 2)
+
+    if (use_savedir)
+    {
+        /* Try to restore from file_name which we just set up. */
+        if (!restore(file_name, envp))
+            exit(1);
+        /* If restore() returns true, the system savefile doesn't exist.
+           So we'll start a new game. */
+    }
+    else if (argc == 2)
+    {
 	if (!restore(argv[1], envp))	/* Note: restore will never return */
 	{
 		endwin();
 	    exit(1);
 	}
+    }
+
+    if (!use_savedir)
+        md_normaluser();
+
     lowtime = (int) time(NULL);
 
 #ifdef WIZARD