Mercurial > hg > early-roguelike
comparison rogue3/main.c @ 1:b4856d4d4c4e
Add -n option and system savedir functionality
| author | edwarj4 |
|---|---|
| date | Wed, 14 Oct 2009 01:32:13 +0000 |
| parents | 527e2150eaf0 |
| children | 78df7025783b |
comparison
equal
deleted
inserted
replaced
| 0:527e2150eaf0 | 1:b4856d4d4c4e |
|---|---|
| 36 | 36 |
| 37 md_init(MD_STRIP_CTRL_KEYPAD); | 37 md_init(MD_STRIP_CTRL_KEYPAD); |
| 38 | 38 |
| 39 open_score(); | 39 open_score(); |
| 40 | 40 |
| 41 /* | |
| 42 * Drop setuid/setgid after opening the scoreboard file. | |
| 43 */ | |
| 44 | |
| 45 md_normaluser(); | |
| 46 | |
| 47 /* | 41 /* |
| 48 * check for print-score option | 42 * check for print-score option |
| 49 */ | 43 */ |
| 50 if (argc == 2 && strcmp(argv[1], "-s") == 0) | 44 if (argc == 2 && strcmp(argv[1], "-s") == 0) |
| 51 { | 45 { |
| 62 wizard = TRUE; | 56 wizard = TRUE; |
| 63 argv++; | 57 argv++; |
| 64 argc--; | 58 argc--; |
| 65 } | 59 } |
| 66 | 60 |
| 67 /* | 61 /* Are we using the system savefile directory? */ |
| 68 * get home and options from environment | 62 #ifdef SAVEDIR |
| 69 */ | 63 if (argc >= 3 && !strcmp(argv[1], "-n")) |
| 70 strcpy(home, md_gethomedir()); | 64 { |
| 71 | 65 strncpy(whoami, argv[2], 79); |
| 72 if (strlen(home) > PATH_MAX - strlen("rogue.save") - 1) | 66 whoami[79] = '\0'; |
| 73 *home = 0; | 67 use_savedir = TRUE; |
| 74 | 68 /* look for savefile at SAVEDIR/UIDplayername.r3sav */ |
| 75 strcpy(file_name, home); | 69 if (snprintf(file_name, 80, "%s%d%.10s.r3sav", SAVEDIR, md_getuid(), whoami) >= 80) |
| 76 strcat(file_name, "rogue.save"); | 70 { |
| 77 | 71 /* this shouldn't happen */ |
| 72 strcpy(file_name, "rogue3.save"); | |
| 73 use_savedir = FALSE; | |
| 74 } | |
| 75 } | |
| 76 #endif | |
| 77 | |
| 78 if (use_savedir == FALSE) | |
| 79 { | |
| 80 md_normaluser(); | |
| 81 /* because we don't need to create a file in the common savedir, | |
| 82 * and the scorefile is already open */ | |
| 83 strcpy(home, md_gethomedir()); | |
| 84 | |
| 85 if (strlen(home) > PATH_MAX - strlen("rogue3.save") - 1) | |
| 86 *home = 0; | |
| 87 | |
| 88 strcpy(file_name, home); | |
| 89 strcat(file_name, "rogue3.save"); | |
| 90 } | |
| 91 | |
| 78 if ((env = getenv("ROGUEOPTS")) != NULL) | 92 if ((env = getenv("ROGUEOPTS")) != NULL) |
| 79 parse_opts(env); | 93 parse_opts(env); |
| 80 if (env == NULL || whoami[0] == '\0') | 94 if (!use_savedir && (env == NULL || whoami[0] == '\0')) |
| 81 strucpy(whoami, md_getusername(), strlen(md_getusername())); | 95 strucpy(whoami, md_getusername(), strlen(md_getusername())); |
| 82 if (env == NULL || fruit[0] == '\0') | 96 if (env == NULL || fruit[0] == '\0') |
| 83 strcpy(fruit, "slime-mold"); | 97 strcpy(fruit, "slime-mold"); |
| 84 | 98 |
| 85 if (too_much() && !wizard && !author()) | 99 if (too_much() && !wizard && !author()) |
| 87 printf("Sorry, %s, but the system is too loaded now.\n", whoami); | 101 printf("Sorry, %s, but the system is too loaded now.\n", whoami); |
| 88 printf("Try again later. Meanwhile, why not enjoy a%s %s?\n", | 102 printf("Try again later. Meanwhile, why not enjoy a%s %s?\n", |
| 89 vowelstr(fruit), fruit); | 103 vowelstr(fruit), fruit); |
| 90 exit(1); | 104 exit(1); |
| 91 } | 105 } |
| 92 | 106 |
| 93 if (argc == 2) | 107 /* now start the game */ |
| 108 if (use_savedir) | |
| 109 { | |
| 110 /* Try to restore from file_name which we just set up. */ | |
| 111 if (!restore(file_name, envp)) | |
| 112 exit(1); | |
| 113 /* If restore() returns true, the system savefile doesn't exist. | |
| 114 So we'll start a new game. */ | |
| 115 } | |
| 116 else if (argc == 2) | |
| 94 if (!restore(argv[1], envp)) /* Note: restore will never return */ | 117 if (!restore(argv[1], envp)) /* Note: restore will never return */ |
| 95 exit(1); | 118 exit(1); |
| 96 | 119 |
| 120 /* If we reach this point, either | |
| 121 * 1. A system savefile was specified and doesn't exist. | |
| 122 * 2. No savefile was specified. | |
| 123 * Either way, start a new game. | |
| 124 */ | |
| 125 | |
| 126 if (!use_savedir) | |
| 127 md_normaluser(); | |
| 128 | |
| 97 time(&now); | 129 time(&now); |
| 98 lowtime = (int) now; | 130 lowtime = (int) now; |
| 99 | 131 |
| 100 env = getenv("SEED"); | 132 env = getenv("SEED"); |
| 101 | 133 |
