]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.main.c
2 * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
6 static char rcsid
[] = "$Id: hack.main.c,v 1.2 1993/08/02 17:17:18 mycroft Exp $";
14 #define gamename "quest"
16 #define gamename "hack"
19 extern char *getlogin(), *getenv();
20 extern char plname
[PL_NSIZ
], pl_character
[PL_CSIZ
];
21 extern struct permonst mons
[CMNUM
+2];
22 extern char genocided
[], fut_geno
[];
26 char *occtxt
; /* defined when occupation != NULL */
31 int hackpid
; /* current pid */
32 int locknum
; /* max num of players */
34 char *catmore
; /* default pager */
36 char SAVEF
[PL_NSIZ
+ 11] = "save/"; /* save/99999player */
37 char *hname
; /* name of the game (argv[0] of call) */
38 char obuf
[BUFSIZ
]; /* BUFSIZ is defined in stdio.h */
40 extern char *nomovemsg
;
59 #ifdef CHDIR /* otherwise no chdir() */
61 * See if we must change directory to the playground.
62 * (Perhaps hack runs suid and playground is inaccessible
64 * The environment variable HACKDIR is overridden by a
65 * -d command line option (must be the first option given)
68 dir
= getenv("HACKDIR");
69 if(argc
> 1 && !strncmp(argv
[1], "-d", 2)) {
73 if(*dir
== '=' || *dir
== ':') dir
++;
74 if(!*dir
&& argc
> 1) {
80 error("Flag -d must be followed by a directory name.");
85 * Who am i? Algorithm: 1. Use name as specified in HACKOPTIONS
86 * 2. Use $USER or $LOGNAME (if 1. fails)
87 * 3. Use getlogin() (if 2. fails)
88 * The resulting name is overridden by command line options.
89 * If everything fails, or if the resulting name is some generic
90 * account like "games", "play", "player", "hack" then eventually
92 * Note that we trust him here; it is possible to play under
93 * somebody else's name.
98 if(!*plname
&& (s
= getenv("USER")))
99 (void) strncpy(plname
, s
, sizeof(plname
)-1);
100 if(!*plname
&& (s
= getenv("LOGNAME")))
101 (void) strncpy(plname
, s
, sizeof(plname
)-1);
102 if(!*plname
&& (s
= getlogin()))
103 (void) strncpy(plname
, s
, sizeof(plname
)-1);
107 * Now we know the directory containing 'record' and
108 * may do a prscore().
110 if(argc
> 1 && !strncmp(argv
[1], "-s", 2)) {
119 * It seems he really wants to play.
120 * Remember tty modes, to be restored on exit.
127 u
.uhp
= 1; /* prevent RIP on early quits */
128 u
.ux
= FAR
; /* prevent nscr() */
129 (void) signal(SIGHUP
, hangup
);
132 * Find the creation date of this game,
133 * so as to avoid restoring outdated savefiles.
138 * We cannot do chdir earlier, otherwise gethdate will fail.
147 while(argc
> 1 && argv
[1][0] == '-'){
153 /* if(!strcmp(getlogin(), WIZARD)) */
156 printf("Sorry.\n"); */
166 (void) strncpy(plname
, argv
[0]+2, sizeof(plname
)-1);
170 (void) strncpy(plname
, argv
[0], sizeof(plname
)-1);
172 printf("Player name expected after -u\n");
175 /* allow -T for Tourist, etc. */
176 (void) strncpy(pl_character
, argv
[0]+1,
177 sizeof(pl_character
)-1);
179 /* printf("Unknown option: %s\n", *argv); */
184 locknum
= atoi(argv
[1]);
185 #ifdef MAX_NR_OF_PLAYERS
186 if(!locknum
|| locknum
> MAX_NR_OF_PLAYERS
)
187 locknum
= MAX_NR_OF_PLAYERS
;
190 if(!(catmore
= getenv("HACKPAGER")) && !(catmore
= getenv("PAGER")))
197 if(wizard
) (void) strcpy(plname
, "wizard"); else
199 if(!*plname
|| !strncmp(plname
, "player", 4)
200 || !strncmp(plname
, "games", 4))
202 plnamesuffix(); /* strip suffix from name; calls askname() */
203 /* again if suffix was whole name */
204 /* accepts any suffix */
209 * check for multiple games under the same name
210 * (if !locknum) or check max nr of players (otherwise)
212 (void) signal(SIGQUIT
,SIG_IGN
);
213 (void) signal(SIGINT
,SIG_IGN
);
215 (void) strcpy(lock
,plname
);
216 getlock(); /* sets lock if locknum != 0 */
220 (void) strcpy(lock
,plname
);
221 if(sfoo
= getenv("MAGIC"))
224 case 'n': (void) srandom(*sfoo
++);
228 if(sfoo
= getenv("GENOCIDED")){
230 register struct permonst
*pm
= mons
;
231 register char *gp
= genocided
;
233 while(pm
< mons
+CMNUM
+2){
234 if(!index(sfoo
, pm
->mlet
))
240 (void) strcpy(genocided
, sfoo
);
241 (void) strcpy(fut_geno
, genocided
);
246 (void) sprintf(SAVEF
, "save/%d%s", getuid(), plname
);
247 regularize(SAVEF
+5); /* avoid . or / in name */
248 if((fd
= open(SAVEF
,0)) >= 0 &&
249 (uptodate(fd
) || unlink(SAVEF
) == 666)) {
250 (void) signal(SIGINT
,done1
);
251 pline("Restoring old save file...");
252 (void) fflush(stdout
);
255 pline("Hello %s, welcome to %s!", plname
, gamename
);
259 fobj
= fcobj
= invent
= 0;
260 fmon
= fallen_down
= 0;
267 (void) signal(SIGINT
,done1
);
275 { register struct monst
*mtmp
;
276 if(mtmp
= m_at(u
.ux
, u
.uy
)) mnexto(mtmp
); /* riv05!a3 */
280 if(flags
.nonews
|| !readnews())
281 /* after reading news we did docrt() already */
285 /* give welcome message before pickup messages */
286 pline("Hello %s, welcome to %s!", plname
, gamename
);
289 read_engr_at(u
.ux
,u
.uy
);
293 flags
.moonphase
= phase_of_the_moon();
294 if(flags
.moonphase
== FULL_MOON
) {
295 pline("You are lucky! Full moon tonight.");
297 } else if(flags
.moonphase
== NEW_MOON
) {
298 pline("Be careful! New moon tonight.");
304 if(flags
.move
) { /* actual time passed */
309 (!(Fast
& ~INTRINSIC
) && (!Fast
|| rn2(3)))) {
310 extern struct monst
*makemon();
313 (void) makemon((struct permonst
*)0, 0, 0);
318 if(flags
.time
) flags
.botl
= 1;
323 if(u
.uhp
*10 < u
.uhpmax
&& moves
-wailmsg
> 50){
326 pline("You hear the wailing of the Banshee...");
328 pline("You hear the howling of the CwnAnnwn...");
330 if(u
.uhp
< u
.uhpmax
) {
332 if(Regeneration
|| !(moves%3
)) {
334 u
.uhp
+= rnd((int) u
.ulevel
-9);
338 } else if(Regeneration
||
339 (!(moves
%(22-u
.ulevel
*2)))) {
344 if(Teleportation
&& !rn2(85)) tele();
345 if(Searching
&& multi
>= 0) (void) dosearch();
352 pline(nomovemsg
? nomovemsg
:
353 "You can move again.");
355 if(afternmv
) (*afternmv
)();
362 if(!flags
.mv
|| Blind
)
369 if(flags
.botl
|| flags
.botlx
) bot();
373 if(multi
>= 0 && occupation
) {
376 else if ((*occupation
)() == 0)
383 if(flags
.run
>= 4) finddir();
386 if(!multi
) { /* lookaround may clear multi */
391 if(multi
< COLNO
&& !--multi
)
392 flags
.mv
= flags
.run
= 0;
398 } else if(multi
== 0) {
404 if(multi
&& multi%7
== 0)
405 (void) fflush(stdout
);
412 /* construct the string xlock.n */
416 while(*tf
&& *tf
!= '.') tf
++;
417 (void) sprintf(tf
, ".%d", foo
);
421 * plname is filled either by an option (-u Player or -uPlayer) or
422 * explicitly (-w implies wizard) or by askname.
423 * It may still contain a suffix denoting pl_character.
427 printf("\nWho are you? ");
428 (void) fflush(stdout
);
430 while((c
= getchar()) != '\n'){
431 if(c
== EOF
) error("End of input\n");
432 /* some people get confused when their erase char is not ^H */
438 if(c
< 'A' || (c
> 'Z' && c
< 'a') || c
> 'z') c
= '_';
439 if(ct
< sizeof(plname
)-1) plname
[ct
++] = c
;
442 if(ct
== 0) askname();
450 pline("Program in disorder - perhaps you'd better Quit.");
461 if(dir
/* User specified directory? */
463 && strcmp(dir
, HACKDIR
) /* and not the default? */
466 (void) setuid(getuid()); /* Ron Wessels */
467 (void) setgid(getgid());
476 if(dir
&& chdir(dir
) < 0) {
478 error("Cannot chdir to %s.", dir
);
481 /* warn the player if he cannot write the record file */
482 /* perhaps we should also test whether . is writable */
483 /* unfortunately the access systemcall is worthless */
489 if((fd
= open(RECORD
, 2)) < 0) {
490 printf("Warning: cannot write %s/%s", dir
, RECORD
);
501 pline("You stop %s.", occtxt
);