comparison 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
comparison
equal deleted inserted replaced
36:2128c7dc8a40 37:34d7a614855e
35 #include <sys/time.h> 35 #include <sys/time.h>
36 #endif 36 #endif
37 37
38 #include "rogue.ext" 38 #include "rogue.ext"
39 39
40 #define SCOREFILE "/usr/local/games/roguelike/srogue.scr"
41 #define SAVEDIR "/usr/local/games/roguelike/sroguesave/"
42
40 struct termios terminal; 43 struct termios terminal;
41 44
42 main(argc, argv, envp) 45 main(argc, argv, envp)
43 char **argv; 46 char **argv;
44 char **envp; 47 char **envp;
69 exit(1); 72 exit(1);
70 } 73 }
71 playgid = getgid(); 74 playgid = getgid();
72 75
73 /* check for print-score option */ 76 /* check for print-score option */
77 #ifdef SCOREFILE
78 strncpy(scorefile, SCOREFILE, LINLEN);
79 scorefile[LINLEN - 1] = '\0';
80 #else
74 81
75 strcpy(scorefile, homedir); 82 strcpy(scorefile, homedir);
76 83
77 if (*scorefile) 84 if (*scorefile)
78 strcat(scorefile,"/"); 85 strcat(scorefile,"/");
79 strcat(scorefile, "srogue.scr"); 86 strcat(scorefile, "srogue.scr");
87 #endif
80 88
81 if(argc >= 2 && strcmp(argv[1], "-s") == 0) 89 if(argc >= 2 && strcmp(argv[1], "-s") == 0)
82 { 90 {
83 showtop(0); 91 showtop(0);
84 exit(0); 92 exit(0);
103 } 111 }
104 } 112 }
105 time(&now); 113 time(&now);
106 lowtime = (int) now; 114 lowtime = (int) now;
107 115
116 #ifdef SAVEDIR
117 if (argc >= 3 && !strcmp(argv[1], "-n")) {
118 strncpy(whoami, argv[2], LINLEN);
119 whoami[LINLEN - 1] = '\0';
120 use_savedir = TRUE;
121 if (snprintf(file_name, LINLEN, "%s%d-%.10s.srsav", SAVEDIR,
122 playuid, whoami) >= LINLEN) {
123 /* Just in case it doesn't fit */
124 strcpy(file_name, "srogue.save");
125 use_savedir = FALSE;
126 }
127 }
128 #endif
129
108 /* get home and options from environment */ 130 /* get home and options from environment */
109 131
110 if ((env = getenv("HOME")) != NULL) 132 if ((env = getenv("HOME")) != NULL)
111 strcpy(home, env); 133 strcpy(home, env);
112 else if ((pw = getpwuid(playuid)) != NULL) 134 else if ((pw = getpwuid(playuid)) != NULL)
118 home[0] = '\0'; 140 home[0] = '\0';
119 141
120 if ((strlen(home) > 0) && (home[strlen(home)-1] != '/')) 142 if ((strlen(home) > 0) && (home[strlen(home)-1] != '/'))
121 strcat(home, "/"); 143 strcat(home, "/");
122 144
123 strcpy(file_name, home); 145 if (!use_savedir) {
124 strcat(file_name, "srogue.sav"); 146 strcpy(file_name, home);
147 strcat(file_name, "srogue.sav");
148 }
125 149
126 if ((env = getenv("ROGUEOPTS")) != NULL) 150 if ((env = getenv("ROGUEOPTS")) != NULL)
127 parse_opts(env); 151 parse_opts(env);
128 152
129 if (env == NULL || whoami[0] == '\0') 153 if (!use_savedir && (env == NULL || whoami[0] == '\0'))
130 { 154 {
131 if((pw = getpwuid(playuid)) == NULL) 155 if((pw = getpwuid(playuid)) == NULL)
132 { 156 {
133 printf("Say, who are you?\n"); 157 printf("Say, who are you?\n");
134 exit(1); 158 exit(1);
138 } 162 }
139 163
140 if (env == NULL || fruit[0] == '\0') 164 if (env == NULL || fruit[0] == '\0')
141 strcpy(fruit, "juicy-fruit"); 165 strcpy(fruit, "juicy-fruit");
142 166
143 if (argc == 2) 167 if (use_savedir)
168 {
169 /* restore() won't return if the restore succeeded. If
170 * file_name doesn't exist, it will return TRUE. In that
171 * case, start a new game. */
172 if (!restore(file_name, envp))
173 exit(1);
174 }
175 else if (argc == 2)
144 if(!restore(argv[1], envp)) /* NOTE: NEVER RETURNS */ 176 if(!restore(argv[1], envp)) /* NOTE: NEVER RETURNS */
145 exit(1); 177 exit(1);
178
179 /* START NEW GAME */
146 180
147 dnum = (wizard && getenv("SEED") != NULL ? 181 dnum = (wizard && getenv("SEED") != NULL ?
148 atoi(getenv("SEED")) : lowtime + getpid()); 182 atoi(getenv("SEED")) : lowtime + getpid());
149 183
150 if(wizard) 184 if(wizard)