1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.main.c - version 1.0.3 */
3 /* $FreeBSD: src/games/hack/hack.main.c,v 1.9 1999/11/16 10:26:36 marcel Exp $ */
9 #define gamename "quest"
11 #define gamename "hack"
14 void (*afternmv
)(void);
15 bool (*occupation
)(void);
19 int hackpid
; /* current pid */
20 int locknum
; /* max num of players */
22 char *catmore
; /* default pager */
24 char SAVEF
[PL_NSIZ
+ 11] = "save/"; /* save/99999player */
25 char *hname
; /* name of the game (argv[0] of call) */
26 char obuf
[BUFSIZ
]; /* BUFSIZ is defined in stdio.h */
31 static void chdirx(const char *, bool);
35 main(int argc
, char *argv
[])
45 #ifdef CHDIR /* otherwise no chdir() */
47 * See if we must change directory to the playground.
48 * (Perhaps hack runs suid and playground is inaccessible
50 * The environment variable HACKDIR is overridden by a
51 * -d command line option (must be the first option given)
54 dir
= getenv("HACKDIR");
55 if (argc
> 1 && !strncmp(argv
[1], "-d", 2)) {
59 if (*dir
== '=' || *dir
== ':')
61 if (!*dir
&& argc
> 1) {
67 error("Flag -d must be followed by a directory name.");
72 * Who am i? Algorithm: 1. Use name as specified in HACKOPTIONS
73 * 2. Use $USER or $LOGNAME (if 1. fails)
74 * 3. Use getlogin() (if 2. fails)
75 * The resulting name is overridden by command line options.
76 * If everything fails, or if the resulting name is some generic
77 * account like "games", "play", "player", "hack" then eventually
79 * Note that we trust him here; it is possible to play under
80 * somebody else's name.
86 if (!*plname
&& (s
= getenv("USER")))
87 strncpy(plname
, s
, sizeof(plname
) - 1);
88 if (!*plname
&& (s
= getenv("LOGNAME")))
89 strncpy(plname
, s
, sizeof(plname
) - 1);
90 if (!*plname
&& (s
= getlogin()))
91 strncpy(plname
, s
, sizeof(plname
) - 1);
95 * Now we know the directory containing 'record' and
98 if (argc
> 1 && !strncmp(argv
[1], "-s", 2)) {
107 * It seems he really wants to play.
108 * Remember tty modes, to be restored on exit.
111 setbuf(stdout
, obuf
);
116 u
.uhp
= 1; /* prevent RIP on early quits */
117 u
.ux
= FAR
; /* prevent nscr() */
118 signal(SIGHUP
, hangup
);
121 * Find the creation date of this game,
122 * so as to avoid restoring outdated savefiles.
127 * We cannot do chdir earlier, otherwise gethdate will fail.
136 while (argc
> 1 && argv
[1][0] == '-') {
139 switch (argv
[0][1]) {
152 strncpy(plname
, argv
[0] + 2, sizeof(plname
) - 1);
156 strncpy(plname
, argv
[0], sizeof(plname
) - 1);
158 printf("Player name expected after -u\n");
161 /* allow -T for Tourist, etc. */
162 strncpy(pl_character
, argv
[0] + 1,
163 sizeof(pl_character
) - 1);
168 locknum
= atoi(argv
[1]);
169 #ifdef MAX_NR_OF_PLAYERS
170 if (!locknum
|| locknum
> MAX_NR_OF_PLAYERS
)
171 locknum
= MAX_NR_OF_PLAYERS
;
174 if (!(catmore
= getenv("HACKPAGER")) && !(catmore
= getenv("PAGER")))
182 strcpy(plname
, "wizard");
185 if (!*plname
|| !strncmp(plname
, "player", 4)
186 || !strncmp(plname
, "games", 4))
188 plnamesuffix(); /* strip suffix from name; calls askname() */
189 /* again if suffix was whole name */
190 /* accepts any suffix */
195 * check for multiple games under the same name
196 * (if !locknum) or check max nr of players (otherwise)
198 signal(SIGQUIT
, SIG_IGN
);
199 signal(SIGINT
, SIG_IGN
);
201 strcpy(lock
, plname
);
202 getlock(); /* sets lock if locknum != 0 */
206 strcpy(lock
, plname
);
207 if ((sfoo
= getenv("MAGIC")))
215 if ((sfoo
= getenv("GENOCIDED")) != NULL
) {
217 struct permonst
*pm
= mons
;
218 char *gp
= genocided
;
220 while (pm
< mons
+ CMNUM
+ 2) {
221 if (!strchr(sfoo
, pm
->mlet
))
227 strncpy(genocided
, sfoo
, sizeof(genocided
) - 1);
228 strcpy(fut_geno
, genocided
);
233 sprintf(SAVEF
, "save/%d%s", getuid(), plname
);
234 regularize(SAVEF
+ 5); /* avoid . or / in name */
235 if ((fd
= open(SAVEF
, O_RDONLY
)) >= 0 &&
236 (uptodate(fd
) || unlink(SAVEF
) == 666)) {
237 signal(SIGINT
, done1
);
238 pline("Restoring old save file...");
242 pline("Hello %s, welcome to %s!", plname
, gamename
);
246 fobj
= fcobj
= invent
= 0;
247 fmon
= fallen_down
= 0;
254 signal(SIGINT
, done1
);
264 if ((mtmp
= m_at(u
.ux
, u
.uy
)) != NULL
)
265 mnexto(mtmp
); /* riv05!a3 */
269 if (flags
.nonews
|| !readnews())
270 /* after reading news we did docrt() already */
274 /* give welcome message before pickup messages */
275 pline("Hello %s, welcome to %s!", plname
, gamename
);
278 read_engr_at(u
.ux
, u
.uy
);
282 flags
.moonphase
= phase_of_the_moon();
283 if (flags
.moonphase
== FULL_MOON
) {
284 pline("You are lucky! Full moon tonight.");
286 } else if (flags
.moonphase
== NEW_MOON
)
287 pline("Be careful! New moon tonight.");
292 if (flags
.move
) { /* actual time passed */
295 if (moves
% 2 == 0 ||
296 (!(Fast
& ~INTRINSIC
) && (!Fast
|| rn2(3)))) {
311 if (u
.uhp
* 10 < u
.uhpmax
&& moves
- wailmsg
> 50) {
314 pline("You hear the wailing of the Banshee...");
316 pline("You hear the howling of the CwnAnnwn...");
318 if (u
.uhp
< u
.uhpmax
) {
320 if (Regeneration
|| !(moves
% 3)) {
322 u
.uhp
+= rnd((int)u
.ulevel
- 9);
323 if (u
.uhp
> u
.uhpmax
)
326 } else if (Regeneration
||
327 (!(moves
% (22 - u
.ulevel
* 2)))) {
332 if (Teleportation
&& !rn2(85))
334 if (Searching
&& multi
>= 0)
342 pline("%s", nomovemsg
? nomovemsg
:
343 "You can move again.");
352 if (!flags
.mv
|| Blind
)
359 if (flags
.botl
|| flags
.botlx
)
364 if (multi
>= 0 && occupation
) {
365 if (monster_nearby())
367 else if ((*occupation
)() == 0)
378 if (!multi
) { /* lookaround may clear multi */
383 if (multi
< COLNO
&& !--multi
)
384 flags
.mv
= flags
.run
= 0;
390 } else if (multi
== 0) {
396 if (multi
&& multi
% 7 == 0)
404 /* construct the string xlock.n */
408 while (*tf
&& *tf
!= '.')
410 (void)sprintf(tf
, ".%d", foo
);
414 * plname is filled either by an option (-u Player or -uPlayer) or
415 * explicitly (-w implies wizard) or by askname.
416 * It may still contain a suffix denoting pl_character.
423 printf("\nWho are you? ");
426 while ((c
= getchar()) != '\n') {
428 error("End of input\n");
429 /* some people get confused when their erase char is not ^H */
436 if (c
< 'A' || (c
> 'Z' && c
< 'a') || c
> 'z')
438 if (ct
< (int)sizeof(plname
) - 1)
448 impossible(const char *s
, ...)
455 pline("Program in disorder - perhaps you'd better Quit.");
460 chdirx(const char *dir
, bool wr
)
463 if (dir
/* User specified directory? */
465 && strcmp(dir
, HACKDIR
) /* and not the default? */
478 if (dir
&& chdir(dir
) < 0) {
480 error("Cannot chdir to %s.", dir
);
483 /* warn the player if he cannot write the record file */
484 /* perhaps we should also test whether . is writable */
485 /* unfortunately the access systemcall is worthless */
491 if ((fd
= open(RECORD
, O_RDWR
)) < 0) {
492 printf("Warning: cannot write %s/%s", dir
, RECORD
);
501 stop_occupation(void)
504 pline("You stop %s.", occtxt
);