diff srogue/main.c @ 37:34d7a614855e

srogue: add support for SAVEDIR
author elwin
date Thu, 25 Nov 2010 17:28:29 +0000
parents 2128c7dc8a40
children d7d45e980791
line wrap: on
line diff
--- a/srogue/main.c	Thu Nov 25 12:21:41 2010 +0000
+++ b/srogue/main.c	Thu Nov 25 17:28:29 2010 +0000
@@ -37,6 +37,9 @@
 
 #include "rogue.ext"
 
+#define SCOREFILE "/usr/local/games/roguelike/srogue.scr"
+#define SAVEDIR "/usr/local/games/roguelike/sroguesave/"
+
 struct termios terminal;
 
 main(argc, argv, envp)
@@ -71,12 +74,17 @@
 	playgid = getgid();
 
 	/* check for print-score option */
+#ifdef SCOREFILE
+	strncpy(scorefile, SCOREFILE, LINLEN);
+	scorefile[LINLEN - 1] = '\0';
+#else
 
 	strcpy(scorefile, homedir);
 
 	if (*scorefile)
 		strcat(scorefile,"/");
 	strcat(scorefile, "srogue.scr");
+#endif
 
 	if(argc >= 2 && strcmp(argv[1], "-s") == 0)
 	{
@@ -105,6 +113,20 @@
 	time(&now);
 	lowtime = (int) now;
 
+#ifdef SAVEDIR
+	if (argc >= 3 && !strcmp(argv[1], "-n")) {
+		strncpy(whoami, argv[2], LINLEN);
+		whoami[LINLEN - 1] = '\0';
+		use_savedir = TRUE;
+		if (snprintf(file_name, LINLEN, "%s%d-%.10s.srsav", SAVEDIR, 
+			playuid, whoami) >= LINLEN) {
+			/* Just in case it doesn't fit */
+			strcpy(file_name, "srogue.save");
+			use_savedir = FALSE;
+		}
+	}
+#endif
+
 	/* get home and options from environment */
 
 	if ((env = getenv("HOME")) != NULL)
@@ -120,13 +142,15 @@
         if ((strlen(home) > 0) && (home[strlen(home)-1] != '/'))
 		strcat(home, "/");
 
-	strcpy(file_name, home);
-	strcat(file_name, "srogue.sav");
+	if (!use_savedir) {
+		strcpy(file_name, home);
+		strcat(file_name, "srogue.sav");
+        }
 
 	if ((env = getenv("ROGUEOPTS")) != NULL)
 		parse_opts(env);
 
-	if (env == NULL || whoami[0] == '\0')
+	if (!use_savedir && (env == NULL || whoami[0] == '\0'))
 	{
 		if((pw = getpwuid(playuid)) == NULL)
 		{
@@ -140,10 +164,20 @@
 	if (env == NULL || fruit[0] == '\0')
 		strcpy(fruit, "juicy-fruit");
 
-	if (argc == 2)
+	if (use_savedir)
+	{
+		/* restore() won't return if the restore succeeded.  If
+		 * file_name doesn't exist, it will return TRUE. In that 
+		 * case, start a new game. */
+		if (!restore(file_name, envp))
+			exit(1);
+	}
+	else if (argc == 2)
 		if(!restore(argv[1], envp)) /* NOTE: NEVER RETURNS */
 			exit(1);
 
+	/* START NEW GAME */
+
 	dnum = (wizard && getenv("SEED") != NULL ?
 		atoi(getenv("SEED")) : lowtime + getpid());