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.3 2005/05/22 03:37:05 y0netan1 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[];
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 */
41 static void chdirx(const char *, boolean);
56 #ifdef CHDIR /* otherwise no chdir() */
58 * See if we must change directory to the playground.
59 * (Perhaps hack runs suid and playground is inaccessible
61 * The environment variable HACKDIR is overridden by a
62 * -d command line option (must be the first option given)
65 dir = getenv("HACKDIR");
66 if(argc > 1 && !strncmp(argv[1], "-d", 2)) {
70 if(*dir == '=' || *dir == ':') dir++;
71 if(!*dir && argc > 1) {
77 error("Flag -d must be followed by a directory name.");
82 * Who am i? Algorithm: 1. Use name as specified in HACKOPTIONS
83 * 2. Use $USER or $LOGNAME (if 1. fails)
84 * 3. Use getlogin() (if 2. fails)
85 * The resulting name is overridden by command line options.
86 * If everything fails, or if the resulting name is some generic
87 * account like "games", "play", "player", "hack" then eventually
89 * Note that we trust him here; it is possible to play under
90 * somebody else's name.
95 if(!*plname && (s = getenv("USER")))
96 (void) strncpy(plname, s, sizeof(plname)-1);
97 if(!*plname && (s = getenv("LOGNAME")))
98 (void) strncpy(plname, s, sizeof(plname)-1);
99 if(!*plname && (s = getlogin()))
100 (void) strncpy(plname, s, sizeof(plname)-1);
104 * Now we know the directory containing 'record' and
105 * may do a prscore().
107 if(argc > 1 && !strncmp(argv[1], "-s", 2)) {
116 * It seems he really wants to play.
117 * Remember tty modes, to be restored on exit.
125 u.uhp = 1; /* prevent RIP on early quits */
126 u.ux = FAR; /* prevent nscr() */
127 (void) signal(SIGHUP, hangup);
130 * Find the creation date of this game,
131 * so as to avoid restoring outdated savefiles.
136 * We cannot do chdir earlier, otherwise gethdate will fail.
145 while(argc > 1 && argv[1][0] == '-'){
151 /* if(!strcmp(getlogin(), WIZARD)) */
154 printf("Sorry.\n"); */
164 (void) strncpy(plname, argv[0]+2, sizeof(plname)-1);
168 (void) strncpy(plname, argv[0], sizeof(plname)-1);
170 printf("Player name expected after -u\n");
173 /* allow -T for Tourist, etc. */
174 (void) strncpy(pl_character, argv[0]+1,
175 sizeof(pl_character)-1);
177 /* printf("Unknown option: %s\n", *argv); */
182 locknum = atoi(argv[1]);
183 #ifdef MAX_NR_OF_PLAYERS
184 if(!locknum || locknum > MAX_NR_OF_PLAYERS)
185 locknum = MAX_NR_OF_PLAYERS;
188 if(!(catmore = getenv("HACKPAGER")) && !(catmore = getenv("PAGER")))
195 if(wizard) (void) strcpy(plname, "wizard"); else
197 if(!*plname || !strncmp(plname, "player", 4)
198 || !strncmp(plname, "games", 4))
200 plnamesuffix(); /* strip suffix from name; calls askname() */
201 /* again if suffix was whole name */
202 /* accepts any suffix */
207 * check for multiple games under the same name
208 * (if !locknum) or check max nr of players (otherwise)
210 (void) signal(SIGQUIT,SIG_IGN);
211 (void) signal(SIGINT,SIG_IGN);
213 (void) strcpy(lock,plname);
214 getlock(); /* sets lock if locknum != 0 */
218 (void) strcpy(lock,plname);
219 if(sfoo = getenv("MAGIC"))
222 case 'n': (void) srandom(*sfoo++);
226 if(sfoo = getenv("GENOCIDED")){
228 struct permonst *pm = mons;
229 char *gp = genocided;
231 while(pm < mons+CMNUM+2){
232 if(!index(sfoo, pm->mlet))
238 (void) strncpy(genocided, sfoo, sizeof(genocided)-1);
239 (void) strcpy(fut_geno, genocided);
244 (void) sprintf(SAVEF, "save/%d%s", getuid(), plname);
245 regularize(SAVEF+5); /* avoid . or / in name */
246 if((fd = open(SAVEF,0)) >= 0 &&
247 (uptodate(fd) || unlink(SAVEF) == 666)) {
248 (void) signal(SIGINT,done1);
249 pline("Restoring old save file...");
250 (void) fflush(stdout);
253 pline("Hello %s, welcome to %s!", plname, gamename);
257 fobj = fcobj = invent = 0;
258 fmon = fallen_down = 0;
265 (void) signal(SIGINT,done1);
273 { struct monst *mtmp;
274 if(mtmp = m_at(u.ux, u.uy)) mnexto(mtmp); /* riv05!a3 */
278 if(flags.nonews || !readnews())
279 /* after reading news we did docrt() already */
283 /* give welcome message before pickup messages */
284 pline("Hello %s, welcome to %s!", plname, gamename);
287 read_engr_at(u.ux,u.uy);
291 flags.moonphase = phase_of_the_moon();
292 if(flags.moonphase == FULL_MOON) {
293 pline("You are lucky! Full moon tonight.");
295 } else if(flags.moonphase == NEW_MOON) {
296 pline("Be careful! New moon tonight.");
302 if(flags.move) { /* actual time passed */
307 (!(Fast & ~INTRINSIC) && (!Fast || rn2(3)))) {
308 extern struct monst *makemon();
311 (void) makemon((struct permonst *)0, 0, 0);
316 if(flags.time) flags.botl = 1;
321 if(u.uhp*10 < u.uhpmax && moves-wailmsg > 50){
324 pline("You hear the wailing of the Banshee...");
326 pline("You hear the howling of the CwnAnnwn...");
328 if(u.uhp < u.uhpmax) {
330 if(Regeneration || !(moves%3)) {
332 u.uhp += rnd((int) u.ulevel-9);
336 } else if(Regeneration ||
337 (!(moves%(22-u.ulevel*2)))) {
342 if(Teleportation && !rn2(85)) tele();
343 if(Searching && multi >= 0) (void) dosearch();
350 pline(nomovemsg ? nomovemsg :
351 "You can move again.");
353 if(afternmv) (*afternmv)();
360 if(!flags.mv || Blind)
367 if(flags.botl || flags.botlx) bot();
371 if(multi >= 0 && occupation) {
374 else if ((*occupation)() == 0)
381 if(flags.run >= 4) finddir();
384 if(!multi) { /* lookaround may clear multi */
389 if(multi < COLNO && !--multi)
390 flags.mv = flags.run = 0;
396 } else if(multi == 0) {
402 if(multi && multi%7 == 0)
403 (void) fflush(stdout);
410 /* construct the string xlock.n */
414 while(*tf && *tf != '.') tf++;
415 (void) sprintf(tf, ".%d", foo);
419 * plname is filled either by an option (-u Player or -uPlayer) or
420 * explicitly (-w implies wizard) or by askname.
421 * It may still contain a suffix denoting pl_character.
425 printf("\nWho are you? ");
426 (void) fflush(stdout);
428 while((c = getchar()) != '\n'){
429 if(c == EOF) error("End of input\n");
430 /* some people get confused when their erase char is not ^H */
436 if(c < 'A' || (c > 'Z' && c < 'a') || c > 'z') c = '_';
437 if(ct < sizeof(plname)-1) plname[ct++] = c;
440 if(ct == 0) askname();
448 pline("Program in disorder - perhaps you'd better Quit.");
453 chdirx(const char *dir, boolean wr)
457 if(dir /* User specified directory? */
459 && strcmp(dir, HACKDIR) /* and not the default? */
472 if(dir && chdir(dir) < 0) {
474 error("Cannot chdir to %s.", dir);
477 /* warn the player if he cannot write the record file */
478 /* perhaps we should also test whether . is writable */
479 /* unfortunately the access systemcall is worthless */
485 if((fd = open(RECORD, 2)) < 0) {
486 printf("Warning: cannot write %s/%s", dir, RECORD);
497 pline("You stop %s.", occtxt);