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 $ */
4 /* $DragonFly: src/games/hack/hack.main.c,v 1.2 2003/06/17 04:25:24 dillon Exp $ */
13 #define gamename "quest"
15 #define gamename "hack"
18 extern char plname[PL_NSIZ], pl_character[PL_CSIZ];
19 extern struct permonst mons[CMNUM+2];
20 extern char genocided[60], fut_geno[];
24 char *occtxt; /* defined when occupation != NULL */
29 int hackpid; /* current pid */
30 int locknum; /* max num of players */
32 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 extern char *nomovemsg;
57 #ifdef CHDIR /* otherwise no chdir() */
59 * See if we must change directory to the playground.
60 * (Perhaps hack runs suid and playground is inaccessible
62 * The environment variable HACKDIR is overridden by a
63 * -d command line option (must be the first option given)
66 dir = getenv("HACKDIR");
67 if(argc > 1 && !strncmp(argv[1], "-d", 2)) {
71 if(*dir == '=' || *dir == ':') dir++;
72 if(!*dir && argc > 1) {
78 error("Flag -d must be followed by a directory name.");
83 * Who am i? Algorithm: 1. Use name as specified in HACKOPTIONS
84 * 2. Use $USER or $LOGNAME (if 1. fails)
85 * 3. Use getlogin() (if 2. fails)
86 * The resulting name is overridden by command line options.
87 * If everything fails, or if the resulting name is some generic
88 * account like "games", "play", "player", "hack" then eventually
90 * Note that we trust him here; it is possible to play under
91 * somebody else's name.
96 if(!*plname && (s = getenv("USER")))
97 (void) strncpy(plname, s, sizeof(plname)-1);
98 if(!*plname && (s = getenv("LOGNAME")))
99 (void) strncpy(plname, s, sizeof(plname)-1);
100 if(!*plname && (s = getlogin()))
101 (void) strncpy(plname, s, sizeof(plname)-1);
105 * Now we know the directory containing 'record' and
106 * may do a prscore().
108 if(argc > 1 && !strncmp(argv[1], "-s", 2)) {
117 * It seems he really wants to play.
118 * Remember tty modes, to be restored on exit.
126 u.uhp = 1; /* prevent RIP on early quits */
127 u.ux = FAR; /* prevent nscr() */
128 (void) signal(SIGHUP, hangup);
131 * Find the creation date of this game,
132 * so as to avoid restoring outdated savefiles.
137 * We cannot do chdir earlier, otherwise gethdate will fail.
146 while(argc > 1 && argv[1][0] == '-'){
152 /* if(!strcmp(getlogin(), WIZARD)) */
155 printf("Sorry.\n"); */
165 (void) strncpy(plname, argv[0]+2, sizeof(plname)-1);
169 (void) strncpy(plname, argv[0], sizeof(plname)-1);
171 printf("Player name expected after -u\n");
174 /* allow -T for Tourist, etc. */
175 (void) strncpy(pl_character, argv[0]+1,
176 sizeof(pl_character)-1);
178 /* printf("Unknown option: %s\n", *argv); */
183 locknum = atoi(argv[1]);
184 #ifdef MAX_NR_OF_PLAYERS
185 if(!locknum || locknum > MAX_NR_OF_PLAYERS)
186 locknum = MAX_NR_OF_PLAYERS;
189 if(!(catmore = getenv("HACKPAGER")) && !(catmore = getenv("PAGER")))
196 if(wizard) (void) strcpy(plname, "wizard"); else
198 if(!*plname || !strncmp(plname, "player", 4)
199 || !strncmp(plname, "games", 4))
201 plnamesuffix(); /* strip suffix from name; calls askname() */
202 /* again if suffix was whole name */
203 /* accepts any suffix */
208 * check for multiple games under the same name
209 * (if !locknum) or check max nr of players (otherwise)
211 (void) signal(SIGQUIT,SIG_IGN);
212 (void) signal(SIGINT,SIG_IGN);
214 (void) strcpy(lock,plname);
215 getlock(); /* sets lock if locknum != 0 */
219 (void) strcpy(lock,plname);
220 if(sfoo = getenv("MAGIC"))
223 case 'n': (void) srandom(*sfoo++);
227 if(sfoo = getenv("GENOCIDED")){
229 struct permonst *pm = mons;
230 char *gp = genocided;
232 while(pm < mons+CMNUM+2){
233 if(!index(sfoo, pm->mlet))
239 (void) strncpy(genocided, sfoo, sizeof(genocided)-1);
240 (void) strcpy(fut_geno, genocided);
245 (void) sprintf(SAVEF, "save/%d%s", getuid(), plname);
246 regularize(SAVEF+5); /* avoid . or / in name */
247 if((fd = open(SAVEF,0)) >= 0 &&
248 (uptodate(fd) || unlink(SAVEF) == 666)) {
249 (void) signal(SIGINT,done1);
250 pline("Restoring old save file...");
251 (void) fflush(stdout);
254 pline("Hello %s, welcome to %s!", plname, gamename);
258 fobj = fcobj = invent = 0;
259 fmon = fallen_down = 0;
266 (void) signal(SIGINT,done1);
274 { struct monst *mtmp;
275 if(mtmp = m_at(u.ux, u.uy)) mnexto(mtmp); /* riv05!a3 */
279 if(flags.nonews || !readnews())
280 /* after reading news we did docrt() already */
284 /* give welcome message before pickup messages */
285 pline("Hello %s, welcome to %s!", plname, gamename);
288 read_engr_at(u.ux,u.uy);
292 flags.moonphase = phase_of_the_moon();
293 if(flags.moonphase == FULL_MOON) {
294 pline("You are lucky! Full moon tonight.");
296 } else if(flags.moonphase == NEW_MOON) {
297 pline("Be careful! New moon tonight.");
303 if(flags.move) { /* actual time passed */
308 (!(Fast & ~INTRINSIC) && (!Fast || rn2(3)))) {
309 extern struct monst *makemon();
312 (void) makemon((struct permonst *)0, 0, 0);
317 if(flags.time) flags.botl = 1;
322 if(u.uhp*10 < u.uhpmax && moves-wailmsg > 50){
325 pline("You hear the wailing of the Banshee...");
327 pline("You hear the howling of the CwnAnnwn...");
329 if(u.uhp < u.uhpmax) {
331 if(Regeneration || !(moves%3)) {
333 u.uhp += rnd((int) u.ulevel-9);
337 } else if(Regeneration ||
338 (!(moves%(22-u.ulevel*2)))) {
343 if(Teleportation && !rn2(85)) tele();
344 if(Searching && multi >= 0) (void) dosearch();
351 pline(nomovemsg ? nomovemsg :
352 "You can move again.");
354 if(afternmv) (*afternmv)();
361 if(!flags.mv || Blind)
368 if(flags.botl || flags.botlx) bot();
372 if(multi >= 0 && occupation) {
375 else if ((*occupation)() == 0)
382 if(flags.run >= 4) finddir();
385 if(!multi) { /* lookaround may clear multi */
390 if(multi < COLNO && !--multi)
391 flags.mv = flags.run = 0;
397 } else if(multi == 0) {
403 if(multi && multi%7 == 0)
404 (void) fflush(stdout);
411 /* construct the string xlock.n */
415 while(*tf && *tf != '.') tf++;
416 (void) sprintf(tf, ".%d", foo);
420 * plname is filled either by an option (-u Player or -uPlayer) or
421 * explicitly (-w implies wizard) or by askname.
422 * It may still contain a suffix denoting pl_character.
426 printf("\nWho are you? ");
427 (void) fflush(stdout);
429 while((c = getchar()) != '\n'){
430 if(c == EOF) error("End of input\n");
431 /* some people get confused when their erase char is not ^H */
437 if(c < 'A' || (c > 'Z' && c < 'a') || c > 'z') c = '_';
438 if(ct < sizeof(plname)-1) plname[ct++] = c;
441 if(ct == 0) askname();
449 pline("Program in disorder - perhaps you'd better Quit.");
460 if(dir /* User specified directory? */
462 && strcmp(dir, HACKDIR) /* and not the default? */
475 if(dir && chdir(dir) < 0) {
477 error("Cannot chdir to %s.", dir);
480 /* warn the player if he cannot write the record file */
481 /* perhaps we should also test whether . is writable */
482 /* unfortunately the access systemcall is worthless */
488 if((fd = open(RECORD, 2)) < 0) {
489 printf("Warning: cannot write %s/%s", dir, RECORD);
500 pline("You stop %s.", occtxt);