1 /* aNetHack 0.0.1 bemain.c $ANH-Date: 1447844549 2015/11/18 11:02:29 $ $ANH-Branch: master $:$ANH-Revision: 1.18 $ */
2 /* Copyright (c) Dean Luick, 1996. */
3 /* aNetHack may be freely redistributed. See license for details. */
9 static void whoami(void);
10 static void process_options(int argc
, char **argv
);
11 static void chdirx(const char *dir
);
12 static void getlock(void);
16 int nhmain(int argc
, char **argv
);
22 MAIN(int argc
, char **argv
)
26 boolean resuming
= FALSE
; /* assume new game */
30 dir
= nh_getenv("ANETHACKDIR");
32 dir
= nh_getenv("HACKDIR");
34 choose_windows(DEFAULT_WINDOW_SYS
);
38 init_nhwindows(&argc
, argv
);
42 * It seems you really want to play.
44 u
.uhp
= 1; /* prevent RIP on early quits */
45 process_options(argc
, argv
); /* command line options */
47 set_playmode(); /* sets plname to "wizard" for wizard mode */
48 /* strip role,race,&c suffix; calls askname() if plname[] is empty
49 or holds a generic user name like "player" or "games" */
51 /* unlike Unix where the game might be invoked with a script
52 which forces a particular character name for each player
53 using a shared account, we always allow player to rename
54 the character during role/race/&c selection */
55 iflags
.renameallowed
= TRUE
;
59 dlb_init(); /* must be before newgame() */
62 * Initialize the vision system. This must be before mklev() on a
63 * new game or before a level restore on a saved game.
67 display_gamewindows();
70 * First, try to find and restore a save file for specified character.
71 * We'll return here if new game player_selection() renames the hero.
74 if ((fd
= restore_saved_game()) >= 0) {
77 display_file(NEWS
, FALSE
);
78 iflags
.news
= FALSE
; /* in case dorecover() fails */
81 pline("Restoring save file...");
82 mark_synch(); /* flush output */
84 resuming
= TRUE
; /* not starting new game */
86 You("are in non-scoring discovery mode.");
87 if (discover
|| wizard
) {
88 if (yn("Do you want to keep the save file?") == 'n')
89 (void) delete_savefile();
91 nh_compress(fqname(SAVEF
, SAVEPREFIX
, 0));
98 /* new game: start by choosing role, race, etc;
99 player might change the hero's name while doing that,
100 in which case we try to restore under the new name
101 and skip selection this time if that didn't succeed */
102 if (!iflags
.renameinprogress
) {
104 if (iflags
.renameinprogress
) {
105 /* player has renamed the hero while selecting role;
106 discard current lock file and create another for
107 the new character name */
108 delete_levelfile(0); /* remove empty lock file */
110 goto attempt_restore
;
115 You("are in non-scoring discovery mode.");
126 * Who am i? Algorithm: 1. Use name as specified in ANETHACKOPTIONS
127 * 2. Use $USER or $LOGNAME (if 1. fails)
128 * The resulting name is overridden by command line options.
129 * If everything fails, or if the resulting name is some generic
130 * account like "games", "play", "player", "hack" then eventually
137 if (s
= nh_getenv("USER")) {
138 (void) strncpy(plname
, s
, sizeof(plname
) - 1);
141 if (s
= nh_getenv("LOGNAME")) {
142 (void) strncpy(plname
, s
, sizeof(plname
) - 1);
147 /* normalize file name - we don't like .'s, /'s, spaces */
153 while ((lp
= strchr(s
, '.')) || (lp
= strchr(s
, '/'))
154 || (lp
= strchr(s
, ' ')))
159 process_options(int argc
, char **argv
)
163 while (argc
> 1 && argv
[1][0] == '-') {
166 switch (argv
[0][1]) {
168 wizard
= TRUE
, discover
= FALSE
;
171 discover
= TRUE
, wizard
= FALSE
;
180 (void) strncpy(plname
, argv
[0] + 2, sizeof(plname
) - 1);
184 (void) strncpy(plname
, argv
[0], sizeof(plname
) - 1);
186 raw_print("Player name expected after -u");
188 case 'p': /* profession (role) */
190 if ((i
= str2role(&argv
[0][2])) >= 0)
192 } else if (argc
> 1) {
195 if ((i
= str2role(argv
[0])) >= 0)
201 if ((i
= str2race(&argv
[0][2])) >= 0)
203 } else if (argc
> 1) {
206 if ((i
= str2race(argv
[0])) >= 0)
214 raw_printf("Unknown option: %s", *argv
);
221 chdirx(const char *dir
)
227 error("Cannot chdir to %s.", dir
);
229 /* Warn the player if we can't write the record file */
230 /* perhaps we should also test whether . is writable */
231 check_recordfile(dir
);
239 Sprintf(lock
, "%d%s", getuid(), plname
);
241 set_levelfile_name(lock
, 0);
242 fd
= creat(lock
, FCMASK
);
244 error("cannot creat lock file.");
246 if (write(fd
, (genericptr_t
) &hackpid
, sizeof(hackpid
))
247 != sizeof(hackpid
)) {
248 error("cannot write lock");
250 if (close(fd
) == -1) {
251 error("cannot close lock");
256 /* validate wizard mode if player has requested access to it */
258 authorize_wizard_mode()
260 /* other ports validate user name or character name here */
266 * If we are not using the Be GUI, then just exit -- we don't need to
269 void anethack_exit(int status
);
272 anethack_exit(int status
)
276 #endif /* !__begui__ */