1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.main.c - version 1.0.3 */
11 #define gamename "quest"
13 #define gamename "hack"
16 extern char plname[PL_NSIZ], pl_character[PL_CSIZ];
17 extern struct permonst mons[CMNUM+2];
18 extern char genocided[60], fut_geno[];
22 char *occtxt; /* defined when occupation != NULL */
27 int hackpid; /* current pid */
28 int locknum; /* max num of players */
30 char *catmore; /* default pager */
32 char SAVEF[PL_NSIZ + 11] = "save/"; /* save/99999player */
33 char *hname; /* name of the game (argv[0] of call) */
34 char obuf[BUFSIZ]; /* BUFSIZ is defined in stdio.h */
36 extern char *nomovemsg;
55 #ifdef CHDIR /* otherwise no chdir() */
57 * See if we must change directory to the playground.
58 * (Perhaps hack runs suid and playground is inaccessible
60 * The environment variable HACKDIR is overridden by a
61 * -d command line option (must be the first option given)
64 dir = getenv("HACKDIR");
65 if(argc > 1 && !strncmp(argv[1], "-d", 2)) {
69 if(*dir == '=' || *dir == ':') dir++;
70 if(!*dir && argc > 1) {
76 error("Flag -d must be followed by a directory name.");
81 * Who am i? Algorithm: 1. Use name as specified in HACKOPTIONS
82 * 2. Use $USER or $LOGNAME (if 1. fails)
83 * 3. Use getlogin() (if 2. fails)
84 * The resulting name is overridden by command line options.
85 * If everything fails, or if the resulting name is some generic
86 * account like "games", "play", "player", "hack" then eventually
88 * Note that we trust him here; it is possible to play under
89 * somebody else's name.
94 if(!*plname && (s = getenv("USER")))
95 (void) strncpy(plname, s, sizeof(plname)-1);
96 if(!*plname && (s = getenv("LOGNAME")))
97 (void) strncpy(plname, s, sizeof(plname)-1);
98 if(!*plname && (s = getlogin()))
99 (void) strncpy(plname, s, sizeof(plname)-1);
103 * Now we know the directory containing 'record' and
104 * may do a prscore().
106 if(argc > 1 && !strncmp(argv[1], "-s", 2)) {
115 * It seems he really wants to play.
116 * Remember tty modes, to be restored on exit.
124 u.uhp = 1; /* prevent RIP on early quits */
125 u.ux = FAR; /* prevent nscr() */
126 (void) signal(SIGHUP, hangup);
129 * Find the creation date of this game,
130 * so as to avoid restoring outdated savefiles.
135 * We cannot do chdir earlier, otherwise gethdate will fail.
144 while(argc > 1 && argv[1][0] == '-'){
150 /* if(!strcmp(getlogin(), WIZARD)) */
153 printf("Sorry.\n"); */
163 (void) strncpy(plname, argv[0]+2, sizeof(plname)-1);
167 (void) strncpy(plname, argv[0], sizeof(plname)-1);
169 printf("Player name expected after -u\n");
172 /* allow -T for Tourist, etc. */
173 (void) strncpy(pl_character, argv[0]+1,
174 sizeof(pl_character)-1);
176 /* printf("Unknown option: %s\n", *argv); */
181 locknum = atoi(argv[1]);
182 #ifdef MAX_NR_OF_PLAYERS
183 if(!locknum || locknum > MAX_NR_OF_PLAYERS)
184 locknum = MAX_NR_OF_PLAYERS;
187 if(!(catmore = getenv("HACKPAGER")) && !(catmore = getenv("PAGER")))
194 if(wizard) (void) strcpy(plname, "wizard"); else
196 if(!*plname || !strncmp(plname, "player", 4)
197 || !strncmp(plname, "games", 4))
199 plnamesuffix(); /* strip suffix from name; calls askname() */
200 /* again if suffix was whole name */
201 /* accepts any suffix */
206 * check for multiple games under the same name
207 * (if !locknum) or check max nr of players (otherwise)
209 (void) signal(SIGQUIT,SIG_IGN);
210 (void) signal(SIGINT,SIG_IGN);
212 (void) strcpy(lock,plname);
213 getlock(); /* sets lock if locknum != 0 */
217 (void) strcpy(lock,plname);
218 if(sfoo = getenv("MAGIC"))
221 case 'n': (void) srandom(*sfoo++);
225 if(sfoo = getenv("GENOCIDED")){
227 register struct permonst *pm = mons;
228 register char *gp = genocided;
230 while(pm < mons+CMNUM+2){
231 if(!index(sfoo, pm->mlet))
237 (void) strncpy(genocided, sfoo, sizeof(genocided)-1);
238 (void) strcpy(fut_geno, genocided);
243 (void) sprintf(SAVEF, "save/%d%s", getuid(), plname);
244 regularize(SAVEF+5); /* avoid . or / in name */
245 if((fd = open(SAVEF,0)) >= 0 &&
246 (uptodate(fd) || unlink(SAVEF) == 666)) {
247 (void) signal(SIGINT,done1);
248 pline("Restoring old save file...");
249 (void) fflush(stdout);
252 pline("Hello %s, welcome to %s!", plname, gamename);
256 fobj = fcobj = invent = 0;
257 fmon = fallen_down = 0;
264 (void) signal(SIGINT,done1);
272 { register struct monst *mtmp;
273 if(mtmp = m_at(u.ux, u.uy)) mnexto(mtmp); /* riv05!a3 */
277 if(flags.nonews || !readnews())
278 /* after reading news we did docrt() already */
282 /* give welcome message before pickup messages */
283 pline("Hello %s, welcome to %s!", plname, gamename);
286 read_engr_at(u.ux,u.uy);
290 flags.moonphase = phase_of_the_moon();
291 if(flags.moonphase == FULL_MOON) {
292 pline("You are lucky! Full moon tonight.");
294 } else if(flags.moonphase == NEW_MOON) {
295 pline("Be careful! New moon tonight.");
301 if(flags.move) { /* actual time passed */
306 (!(Fast & ~INTRINSIC) && (!Fast || rn2(3)))) {
307 extern struct monst *makemon();
310 (void) makemon((struct permonst *)0, 0, 0);
315 if(flags.time) flags.botl = 1;
320 if(u.uhp*10 < u.uhpmax && moves-wailmsg > 50){
323 pline("You hear the wailing of the Banshee...");
325 pline("You hear the howling of the CwnAnnwn...");
327 if(u.uhp < u.uhpmax) {
329 if(Regeneration || !(moves%3)) {
331 u.uhp += rnd((int) u.ulevel-9);
335 } else if(Regeneration ||
336 (!(moves%(22-u.ulevel*2)))) {
341 if(Teleportation && !rn2(85)) tele();
342 if(Searching && multi >= 0) (void) dosearch();
349 pline(nomovemsg ? nomovemsg :
350 "You can move again.");
352 if(afternmv) (*afternmv)();
359 if(!flags.mv || Blind)
366 if(flags.botl || flags.botlx) bot();
370 if(multi >= 0 && occupation) {
373 else if ((*occupation)() == 0)
380 if(flags.run >= 4) finddir();
383 if(!multi) { /* lookaround may clear multi */
388 if(multi < COLNO && !--multi)
389 flags.mv = flags.run = 0;
395 } else if(multi == 0) {
401 if(multi && multi%7 == 0)
402 (void) fflush(stdout);
409 /* construct the string xlock.n */
413 while(*tf && *tf != '.') tf++;
414 (void) sprintf(tf, ".%d", foo);
418 * plname is filled either by an option (-u Player or -uPlayer) or
419 * explicitly (-w implies wizard) or by askname.
420 * It may still contain a suffix denoting pl_character.
424 printf("\nWho are you? ");
425 (void) fflush(stdout);
427 while((c = getchar()) != '\n'){
428 if(c == EOF) error("End of input\n");
429 /* some people get confused when their erase char is not ^H */
435 if(c < 'A' || (c > 'Z' && c < 'a') || c > 'z') c = '_';
436 if(ct < sizeof(plname)-1) plname[ct++] = c;
439 if(ct == 0) askname();
447 pline("Program in disorder - perhaps you'd better Quit.");
458 if(dir /* User specified directory? */
460 && strcmp(dir, HACKDIR) /* and not the default? */
473 if(dir && chdir(dir) < 0) {
475 error("Cannot chdir to %s.", dir);
478 /* warn the player if he cannot write the record file */
479 /* perhaps we should also test whether . is writable */
480 /* unfortunately the access systemcall is worthless */
486 if((fd = open(RECORD, 2)) < 0) {
487 printf("Warning: cannot write %s/%s", dir, RECORD);
498 pline("You stop %s.", occtxt);