]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.main.c
1 /* $NetBSD: hack.main.c,v 1.7 2002/05/26 00:12:12 wiz Exp $ */
4 * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
9 __RCSID("$NetBSD: hack.main.c,v 1.7 2002/05/26 00:12:12 wiz Exp $");
20 #define gamename "quest"
22 #define gamename "hack"
25 int (*afternmv
) __P((void));
26 int (*occupation
) __P((void));
27 const char *occtxt
; /* defined when occupation != NULL */
29 int hackpid
; /* current pid */
30 int locknum
; /* max num of players */
32 const char *catmore
; /* default pager */
34 char SAVEF
[PL_NSIZ
+ 11] = "save/"; /* save/99999player */
35 char *hname
; /* name of the game (argv[0] of call) */
36 char obuf
[BUFSIZ
]; /* BUFSIZ is defined in stdio.h */
38 int main
__P((int, char *[]));
39 static void chdirx
__P((const char *, boolean
));
51 /* Check for dirty tricks with closed fds 0, 1, 2 */
52 fd
= open("/dev/null", O_RDONLY
);
60 #ifdef CHDIR /* otherwise no chdir() */
62 * See if we must change directory to the playground.
63 * (Perhaps hack runs suid and playground is inaccessible
65 * The environment variable HACKDIR is overridden by a
66 * -d command line option (must be the first option given)
69 dir
= getenv("HACKDIR");
70 if (argc
> 1 && !strncmp(argv
[1], "-d", 2)) {
74 if (*dir
== '=' || *dir
== ':')
76 if (!*dir
&& argc
> 1) {
82 error("Flag -d must be followed by a directory name.");
87 * Who am i? Algorithm: 1. Use name as specified in HACKOPTIONS
88 * 2. Use $USER or $LOGNAME (if 1. fails)
89 * 3. Use getlogin() (if 2. fails)
90 * The resulting name is overridden by command line options.
91 * If everything fails, or if the resulting name is some generic
92 * account like "games", "play", "player", "hack" then eventually
94 * Note that we trust him here; it is possible to play under
95 * somebody else's name.
101 if (!*plname
&& (s
= getenv("USER")))
102 (void) strncpy(plname
, s
, sizeof(plname
) - 1);
103 if (!*plname
&& (s
= getenv("LOGNAME")))
104 (void) strncpy(plname
, s
, sizeof(plname
) - 1);
105 if (!*plname
&& (s
= getlogin()))
106 (void) strncpy(plname
, s
, sizeof(plname
) - 1);
110 * Now we know the directory containing 'record' and
111 * may do a prscore().
113 if (argc
> 1 && !strncmp(argv
[1], "-s", 2)) {
121 * It seems he really wants to play.
122 * Remember tty modes, to be restored on exit.
125 setbuf(stdout
, obuf
);
129 u
.uhp
= 1; /* prevent RIP on early quits */
130 u
.ux
= FAR
; /* prevent nscr() */
131 (void) signal(SIGHUP
, hangup
);
134 * Find the creation date of this game,
135 * so as to avoid restoring outdated savefiles.
140 * We cannot do chdir earlier, otherwise gethdate will fail.
149 while (argc
> 1 && argv
[1][0] == '-') {
152 switch (argv
[0][1]) {
155 /* if(!strcmp(getlogin(), WIZARD)) */
158 * else printf("Sorry.\n");
169 (void) strncpy(plname
, argv
[0] + 2, sizeof(plname
) - 1);
173 (void) strncpy(plname
, argv
[0], sizeof(plname
) - 1);
175 printf("Player name expected after -u\n");
178 /* allow -T for Tourist, etc. */
179 (void) strncpy(pl_character
, argv
[0] + 1,
180 sizeof(pl_character
) - 1);
182 /* printf("Unknown option: %s\n", *argv); */
187 locknum
= atoi(argv
[1]);
188 #ifdef MAX_NR_OF_PLAYERS
189 if (!locknum
|| locknum
> MAX_NR_OF_PLAYERS
)
190 locknum
= MAX_NR_OF_PLAYERS
;
193 if (((catmore
= getenv("HACKPAGER")) == NULL
&&
194 (catmore
= getenv("PAGER")) == NULL
) ||
203 (void) strcpy(plname
, "wizard");
206 if (!*plname
|| !strncmp(plname
, "player", 4)
207 || !strncmp(plname
, "games", 4))
209 plnamesuffix(); /* strip suffix from name; calls askname() */
210 /* again if suffix was whole name */
211 /* accepts any suffix */
216 * check for multiple games under the same name
217 * (if !locknum) or check max nr of players (otherwise)
219 (void) signal(SIGQUIT
, SIG_IGN
);
220 (void) signal(SIGINT
, SIG_IGN
);
222 (void) strcpy(lock
, plname
);
223 getlock(); /* sets lock if locknum != 0 */
227 (void) strcpy(lock
, plname
);
228 if ((sfoo
= getenv("MAGIC")) != NULL
)
232 (void) srandom(*sfoo
++);
236 if ((sfoo
= getenv("GENOCIDED")) != NULL
) {
238 const struct permonst
*pm
= mons
;
239 char *gp
= genocided
;
241 while (pm
< mons
+ CMNUM
+ 2) {
242 if (!strchr(sfoo
, pm
->mlet
))
248 (void) strcpy(genocided
, sfoo
);
249 (void) strcpy(fut_geno
, genocided
);
254 (void) sprintf(SAVEF
, "save/%d%s", getuid(), plname
);
255 regularize(SAVEF
+ 5); /* avoid . or / in name */
256 if ((fd
= open(SAVEF
, O_RDONLY
)) >= 0 &&
257 (uptodate(fd
) || unlink(SAVEF
) == 666)) {
258 (void) signal(SIGINT
, done1
);
259 pline("Restoring old save file...");
260 (void) fflush(stdout
);
263 pline("Hello %s, welcome to %s!", plname
, gamename
);
267 fobj
= fcobj
= invent
= 0;
268 fmon
= fallen_down
= 0;
275 (void) signal(SIGINT
, done1
);
285 if ((mtmp
= m_at(u
.ux
, u
.uy
)) != NULL
)
286 mnexto(mtmp
); /* riv05!a3 */
290 if (flags
.nonews
|| !readnews())
291 /* after reading news we did docrt() already */
295 /* give welcome message before pickup messages */
296 pline("Hello %s, welcome to %s!", plname
, gamename
);
299 read_engr_at(u
.ux
, u
.uy
);
303 flags
.moonphase
= phase_of_the_moon();
304 if (flags
.moonphase
== FULL_MOON
) {
305 pline("You are lucky! Full moon tonight.");
307 } else if (flags
.moonphase
== NEW_MOON
) {
308 pline("Be careful! New moon tonight.");
313 if (flags
.move
) { /* actual time passed */
317 if (moves
% 2 == 0 ||
318 (!(Fast
& ~INTRINSIC
) && (!Fast
|| rn2(3)))) {
321 (void) makemon((struct permonst
*) 0, 0, 0);
333 if (u
.uhp
* 10 < u
.uhpmax
&& moves
- wailmsg
> 50) {
336 pline("You hear the wailing of the Banshee...");
338 pline("You hear the howling of the CwnAnnwn...");
340 if (u
.uhp
< u
.uhpmax
) {
342 if (Regeneration
|| !(moves
% 3)) {
344 u
.uhp
+= rnd((int) u
.ulevel
- 9);
345 if (u
.uhp
> u
.uhpmax
)
348 } else if (Regeneration
||
349 (!(moves
% (22 - u
.ulevel
* 2)))) {
354 if (Teleportation
&& !rn2(85))
356 if (Searching
&& multi
>= 0)
364 pline(nomovemsg
? nomovemsg
:
365 "You can move again.");
374 if (!flags
.mv
|| Blind
)
381 if (flags
.botl
|| flags
.botlx
)
386 if (multi
>= 0 && occupation
) {
387 if (monster_nearby())
389 else if ((*occupation
) () == 0)
399 if (!multi
) { /* lookaround may clear multi */
404 if (multi
< COLNO
&& !--multi
)
405 flags
.mv
= flags
.run
= 0;
411 } else if (multi
== 0) {
417 if (multi
&& multi
% 7 == 0)
418 (void) fflush(stdout
);
426 /* construct the string xlock.n */
430 while (*tf
&& *tf
!= '.')
432 (void) sprintf(tf
, ".%d", foo
);
436 * plname is filled either by an option (-u Player or -uPlayer) or
437 * explicitly (-w implies wizard) or by askname.
438 * It may still contain a suffix denoting pl_character.
444 printf("\nWho are you? ");
445 (void) fflush(stdout
);
447 while ((c
= getchar()) != '\n') {
449 error("End of input\n");
450 /* some people get confused when their erase char is not ^H */
457 if (c
< 'A' || (c
> 'Z' && c
< 'a') || c
> 'z')
459 if (ct
< sizeof(plname
) - 1)
469 impossible(const char *s
, ...)
476 pline("Program in disorder - perhaps you'd better Quit.");
487 if (dir
/* User specified directory? */
489 && strcmp(dir
, HACKDIR
) /* and not the default? */
492 (void) setuid(getuid()); /* Ron Wessels */
493 (void) setgid(getgid());
502 if (dir
&& chdir(dir
) < 0) {
504 error("Cannot chdir to %s.", dir
);
506 /* warn the player if he cannot write the record file */
507 /* perhaps we should also test whether . is writable */
508 /* unfortunately the access systemcall is worthless */
514 if ((fd
= open(RECORD
, O_RDWR
)) < 0) {
515 printf("Warning: cannot write %s/%s", dir
, RECORD
);
527 pline("You stop %s.", occtxt
);