hack.main.c revision 1.5 1 1.5 kleink /* $NetBSD: hack.main.c,v 1.5 2000/03/02 18:19:06 kleink Exp $ */
2 1.4 christos
3 1.2 mycroft /*
4 1.2 mycroft * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
5 1.2 mycroft */
6 1.2 mycroft
7 1.4 christos #include <sys/cdefs.h>
8 1.2 mycroft #ifndef lint
9 1.5 kleink __RCSID("$NetBSD: hack.main.c,v 1.5 2000/03/02 18:19:06 kleink Exp $");
10 1.4 christos #endif /* not lint */
11 1.1 cgd
12 1.1 cgd #include <signal.h>
13 1.4 christos #include <stdlib.h>
14 1.4 christos #include <unistd.h>
15 1.4 christos #include <fcntl.h>
16 1.1 cgd #include "hack.h"
17 1.4 christos #include "extern.h"
18 1.1 cgd
19 1.1 cgd #ifdef QUEST
20 1.1 cgd #define gamename "quest"
21 1.1 cgd #else
22 1.1 cgd #define gamename "hack"
23 1.1 cgd #endif
24 1.1 cgd
25 1.4 christos int (*afternmv) __P((void));
26 1.4 christos int (*occupation) __P((void));
27 1.4 christos char *occtxt; /* defined when occupation != NULL */
28 1.1 cgd
29 1.4 christos int hackpid; /* current pid */
30 1.4 christos int locknum; /* max num of players */
31 1.1 cgd #ifdef DEF_PAGER
32 1.4 christos char *catmore; /* default pager */
33 1.1 cgd #endif
34 1.4 christos char SAVEF[PL_NSIZ + 11] = "save/"; /* save/99999player */
35 1.4 christos char *hname; /* name of the game (argv[0] of call) */
36 1.4 christos char obuf[BUFSIZ]; /* BUFSIZ is defined in stdio.h */
37 1.4 christos
38 1.4 christos int main __P((int, char *[]));
39 1.4 christos static void chdirx __P((char *, boolean));
40 1.4 christos
41 1.4 christos int
42 1.4 christos main(argc, argv)
43 1.4 christos int argc;
44 1.4 christos char *argv[];
45 1.1 cgd {
46 1.4 christos int fd;
47 1.1 cgd #ifdef CHDIR
48 1.4 christos char *dir;
49 1.1 cgd #endif
50 1.1 cgd
51 1.1 cgd hname = argv[0];
52 1.1 cgd hackpid = getpid();
53 1.1 cgd
54 1.1 cgd #ifdef CHDIR /* otherwise no chdir() */
55 1.1 cgd /*
56 1.1 cgd * See if we must change directory to the playground.
57 1.1 cgd * (Perhaps hack runs suid and playground is inaccessible
58 1.1 cgd * for the player.)
59 1.1 cgd * The environment variable HACKDIR is overridden by a
60 1.1 cgd * -d command line option (must be the first option given)
61 1.1 cgd */
62 1.1 cgd
63 1.1 cgd dir = getenv("HACKDIR");
64 1.4 christos if (argc > 1 && !strncmp(argv[1], "-d", 2)) {
65 1.1 cgd argc--;
66 1.1 cgd argv++;
67 1.4 christos dir = argv[0] + 2;
68 1.4 christos if (*dir == '=' || *dir == ':')
69 1.4 christos dir++;
70 1.4 christos if (!*dir && argc > 1) {
71 1.1 cgd argc--;
72 1.1 cgd argv++;
73 1.1 cgd dir = argv[0];
74 1.1 cgd }
75 1.4 christos if (!*dir)
76 1.4 christos error("Flag -d must be followed by a directory name.");
77 1.1 cgd }
78 1.1 cgd #endif
79 1.1 cgd
80 1.1 cgd /*
81 1.1 cgd * Who am i? Algorithm: 1. Use name as specified in HACKOPTIONS
82 1.1 cgd * 2. Use $USER or $LOGNAME (if 1. fails)
83 1.1 cgd * 3. Use getlogin() (if 2. fails)
84 1.1 cgd * The resulting name is overridden by command line options.
85 1.1 cgd * If everything fails, or if the resulting name is some generic
86 1.1 cgd * account like "games", "play", "player", "hack" then eventually
87 1.1 cgd * we'll ask him.
88 1.1 cgd * Note that we trust him here; it is possible to play under
89 1.1 cgd * somebody else's name.
90 1.1 cgd */
91 1.4 christos {
92 1.4 christos char *s;
93 1.1 cgd
94 1.4 christos initoptions();
95 1.4 christos if (!*plname && (s = getenv("USER")))
96 1.4 christos (void) strncpy(plname, s, sizeof(plname) - 1);
97 1.4 christos if (!*plname && (s = getenv("LOGNAME")))
98 1.4 christos (void) strncpy(plname, s, sizeof(plname) - 1);
99 1.4 christos if (!*plname && (s = getlogin()))
100 1.4 christos (void) strncpy(plname, s, sizeof(plname) - 1);
101 1.1 cgd }
102 1.1 cgd
103 1.1 cgd /*
104 1.1 cgd * Now we know the directory containing 'record' and
105 1.1 cgd * may do a prscore().
106 1.1 cgd */
107 1.4 christos if (argc > 1 && !strncmp(argv[1], "-s", 2)) {
108 1.1 cgd #ifdef CHDIR
109 1.4 christos chdirx(dir, 0);
110 1.1 cgd #endif
111 1.1 cgd prscore(argc, argv);
112 1.1 cgd exit(0);
113 1.1 cgd }
114 1.1 cgd /*
115 1.1 cgd * It seems he really wants to play.
116 1.1 cgd * Remember tty modes, to be restored on exit.
117 1.1 cgd */
118 1.1 cgd gettty();
119 1.4 christos setbuf(stdout, obuf);
120 1.1 cgd setrandom();
121 1.1 cgd startup();
122 1.1 cgd cls();
123 1.4 christos u.uhp = 1; /* prevent RIP on early quits */
124 1.4 christos u.ux = FAR; /* prevent nscr() */
125 1.1 cgd (void) signal(SIGHUP, hangup);
126 1.1 cgd
127 1.1 cgd /*
128 1.1 cgd * Find the creation date of this game,
129 1.1 cgd * so as to avoid restoring outdated savefiles.
130 1.1 cgd */
131 1.1 cgd gethdate(hname);
132 1.1 cgd
133 1.1 cgd /*
134 1.1 cgd * We cannot do chdir earlier, otherwise gethdate will fail.
135 1.1 cgd */
136 1.1 cgd #ifdef CHDIR
137 1.4 christos chdirx(dir, 1);
138 1.1 cgd #endif
139 1.1 cgd
140 1.1 cgd /*
141 1.1 cgd * Process options.
142 1.1 cgd */
143 1.4 christos while (argc > 1 && argv[1][0] == '-') {
144 1.1 cgd argv++;
145 1.1 cgd argc--;
146 1.4 christos switch (argv[0][1]) {
147 1.1 cgd #ifdef WIZARD
148 1.1 cgd case 'D':
149 1.4 christos /* if(!strcmp(getlogin(), WIZARD)) */
150 1.4 christos wizard = TRUE;
151 1.4 christos /*
152 1.4 christos * else printf("Sorry.\n");
153 1.4 christos */
154 1.1 cgd break;
155 1.1 cgd #endif
156 1.1 cgd #ifdef NEWS
157 1.1 cgd case 'n':
158 1.1 cgd flags.nonews = TRUE;
159 1.1 cgd break;
160 1.1 cgd #endif
161 1.1 cgd case 'u':
162 1.4 christos if (argv[0][2])
163 1.4 christos (void) strncpy(plname, argv[0] + 2, sizeof(plname) - 1);
164 1.4 christos else if (argc > 1) {
165 1.4 christos argc--;
166 1.4 christos argv++;
167 1.4 christos (void) strncpy(plname, argv[0], sizeof(plname) - 1);
168 1.1 cgd } else
169 1.1 cgd printf("Player name expected after -u\n");
170 1.1 cgd break;
171 1.1 cgd default:
172 1.1 cgd /* allow -T for Tourist, etc. */
173 1.4 christos (void) strncpy(pl_character, argv[0] + 1,
174 1.4 christos sizeof(pl_character) - 1);
175 1.1 cgd
176 1.1 cgd /* printf("Unknown option: %s\n", *argv); */
177 1.1 cgd }
178 1.1 cgd }
179 1.1 cgd
180 1.4 christos if (argc > 1)
181 1.1 cgd locknum = atoi(argv[1]);
182 1.1 cgd #ifdef MAX_NR_OF_PLAYERS
183 1.4 christos if (!locknum || locknum > MAX_NR_OF_PLAYERS)
184 1.1 cgd locknum = MAX_NR_OF_PLAYERS;
185 1.1 cgd #endif
186 1.1 cgd #ifdef DEF_PAGER
187 1.5 kleink if (((catmore = getenv("HACKPAGER")) == NULL &&
188 1.5 kleink (catmore = getenv("PAGER")) == NULL) ||
189 1.5 kleink catmore[0] == '\0')
190 1.1 cgd catmore = DEF_PAGER;
191 1.1 cgd #endif
192 1.1 cgd #ifdef MAIL
193 1.1 cgd getmailstatus();
194 1.1 cgd #endif
195 1.1 cgd #ifdef WIZARD
196 1.4 christos if (wizard)
197 1.4 christos (void) strcpy(plname, "wizard");
198 1.4 christos else
199 1.1 cgd #endif
200 1.4 christos if (!*plname || !strncmp(plname, "player", 4)
201 1.1 cgd || !strncmp(plname, "games", 4))
202 1.1 cgd askname();
203 1.1 cgd plnamesuffix(); /* strip suffix from name; calls askname() */
204 1.4 christos /* again if suffix was whole name */
205 1.4 christos /* accepts any suffix */
206 1.1 cgd #ifdef WIZARD
207 1.4 christos if (!wizard) {
208 1.1 cgd #endif
209 1.1 cgd /*
210 1.1 cgd * check for multiple games under the same name
211 1.1 cgd * (if !locknum) or check max nr of players (otherwise)
212 1.1 cgd */
213 1.4 christos (void) signal(SIGQUIT, SIG_IGN);
214 1.4 christos (void) signal(SIGINT, SIG_IGN);
215 1.4 christos if (!locknum)
216 1.4 christos (void) strcpy(lock, plname);
217 1.1 cgd getlock(); /* sets lock if locknum != 0 */
218 1.1 cgd #ifdef WIZARD
219 1.1 cgd } else {
220 1.4 christos char *sfoo;
221 1.4 christos (void) strcpy(lock, plname);
222 1.4 christos if ((sfoo = getenv("MAGIC")) != NULL)
223 1.4 christos while (*sfoo) {
224 1.4 christos switch (*sfoo++) {
225 1.4 christos case 'n':
226 1.4 christos (void) srandom(*sfoo++);
227 1.1 cgd break;
228 1.1 cgd }
229 1.1 cgd }
230 1.4 christos if ((sfoo = getenv("GENOCIDED")) != NULL) {
231 1.4 christos if (*sfoo == '!') {
232 1.4 christos struct permonst *pm = mons;
233 1.4 christos char *gp = genocided;
234 1.1 cgd
235 1.4 christos while (pm < mons + CMNUM + 2) {
236 1.4 christos if (!strchr(sfoo, pm->mlet))
237 1.1 cgd *gp++ = pm->mlet;
238 1.1 cgd pm++;
239 1.1 cgd }
240 1.1 cgd *gp = 0;
241 1.1 cgd } else
242 1.1 cgd (void) strcpy(genocided, sfoo);
243 1.1 cgd (void) strcpy(fut_geno, genocided);
244 1.1 cgd }
245 1.1 cgd }
246 1.1 cgd #endif
247 1.1 cgd setftty();
248 1.1 cgd (void) sprintf(SAVEF, "save/%d%s", getuid(), plname);
249 1.4 christos regularize(SAVEF + 5); /* avoid . or / in name */
250 1.4 christos if ((fd = open(SAVEF, 0)) >= 0 &&
251 1.4 christos (uptodate(fd) || unlink(SAVEF) == 666)) {
252 1.4 christos (void) signal(SIGINT, done1);
253 1.1 cgd pline("Restoring old save file...");
254 1.1 cgd (void) fflush(stdout);
255 1.4 christos if (!dorecover(fd))
256 1.1 cgd goto not_recovered;
257 1.1 cgd pline("Hello %s, welcome to %s!", plname, gamename);
258 1.1 cgd flags.move = 0;
259 1.1 cgd } else {
260 1.1 cgd not_recovered:
261 1.1 cgd fobj = fcobj = invent = 0;
262 1.1 cgd fmon = fallen_down = 0;
263 1.1 cgd ftrap = 0;
264 1.1 cgd fgold = 0;
265 1.1 cgd flags.ident = 1;
266 1.1 cgd init_objects();
267 1.1 cgd u_init();
268 1.1 cgd
269 1.4 christos (void) signal(SIGINT, done1);
270 1.1 cgd mklev();
271 1.1 cgd u.ux = xupstair;
272 1.1 cgd u.uy = yupstair;
273 1.1 cgd (void) inshop();
274 1.1 cgd setsee();
275 1.1 cgd flags.botlx = 1;
276 1.1 cgd makedog();
277 1.4 christos {
278 1.4 christos struct monst *mtmp;
279 1.4 christos if ((mtmp = m_at(u.ux, u.uy)) != NULL)
280 1.4 christos mnexto(mtmp); /* riv05!a3 */
281 1.1 cgd }
282 1.1 cgd seemons();
283 1.1 cgd #ifdef NEWS
284 1.4 christos if (flags.nonews || !readnews())
285 1.1 cgd /* after reading news we did docrt() already */
286 1.1 cgd #endif
287 1.1 cgd docrt();
288 1.1 cgd
289 1.1 cgd /* give welcome message before pickup messages */
290 1.1 cgd pline("Hello %s, welcome to %s!", plname, gamename);
291 1.1 cgd
292 1.1 cgd pickup(1);
293 1.4 christos read_engr_at(u.ux, u.uy);
294 1.1 cgd flags.move = 1;
295 1.1 cgd }
296 1.1 cgd
297 1.1 cgd flags.moonphase = phase_of_the_moon();
298 1.4 christos if (flags.moonphase == FULL_MOON) {
299 1.1 cgd pline("You are lucky! Full moon tonight.");
300 1.1 cgd u.uluck++;
301 1.4 christos } else if (flags.moonphase == NEW_MOON) {
302 1.1 cgd pline("Be careful! New moon tonight.");
303 1.1 cgd }
304 1.1 cgd initrack();
305 1.1 cgd
306 1.4 christos for (;;) {
307 1.4 christos if (flags.move) { /* actual time passed */
308 1.1 cgd
309 1.1 cgd settrack();
310 1.1 cgd
311 1.4 christos if (moves % 2 == 0 ||
312 1.4 christos (!(Fast & ~INTRINSIC) && (!Fast || rn2(3)))) {
313 1.1 cgd movemon();
314 1.4 christos if (!rn2(70))
315 1.4 christos (void) makemon((struct permonst *) 0, 0, 0);
316 1.1 cgd }
317 1.4 christos if (Glib)
318 1.4 christos glibr();
319 1.1 cgd timeout();
320 1.1 cgd ++moves;
321 1.4 christos if (flags.time)
322 1.4 christos flags.botl = 1;
323 1.4 christos if (u.uhp < 1) {
324 1.1 cgd pline("You die...");
325 1.1 cgd done("died");
326 1.1 cgd }
327 1.4 christos if (u.uhp * 10 < u.uhpmax && moves - wailmsg > 50) {
328 1.4 christos wailmsg = moves;
329 1.4 christos if (u.uhp == 1)
330 1.4 christos pline("You hear the wailing of the Banshee...");
331 1.4 christos else
332 1.4 christos pline("You hear the howling of the CwnAnnwn...");
333 1.1 cgd }
334 1.4 christos if (u.uhp < u.uhpmax) {
335 1.4 christos if (u.ulevel > 9) {
336 1.4 christos if (Regeneration || !(moves % 3)) {
337 1.4 christos flags.botl = 1;
338 1.4 christos u.uhp += rnd((int) u.ulevel - 9);
339 1.4 christos if (u.uhp > u.uhpmax)
340 1.4 christos u.uhp = u.uhpmax;
341 1.1 cgd }
342 1.4 christos } else if (Regeneration ||
343 1.4 christos (!(moves % (22 - u.ulevel * 2)))) {
344 1.1 cgd flags.botl = 1;
345 1.1 cgd u.uhp++;
346 1.1 cgd }
347 1.1 cgd }
348 1.4 christos if (Teleportation && !rn2(85))
349 1.4 christos tele();
350 1.4 christos if (Searching && multi >= 0)
351 1.4 christos (void) dosearch();
352 1.1 cgd gethungry();
353 1.1 cgd invault();
354 1.1 cgd amulet();
355 1.1 cgd }
356 1.4 christos if (multi < 0) {
357 1.4 christos if (!++multi) {
358 1.1 cgd pline(nomovemsg ? nomovemsg :
359 1.4 christos "You can move again.");
360 1.1 cgd nomovemsg = 0;
361 1.4 christos if (afternmv)
362 1.4 christos (*afternmv) ();
363 1.1 cgd afternmv = 0;
364 1.1 cgd }
365 1.1 cgd }
366 1.1 cgd find_ac();
367 1.1 cgd #ifndef QUEST
368 1.4 christos if (!flags.mv || Blind)
369 1.1 cgd #endif
370 1.1 cgd {
371 1.1 cgd seeobjs();
372 1.1 cgd seemons();
373 1.1 cgd nscr();
374 1.1 cgd }
375 1.4 christos if (flags.botl || flags.botlx)
376 1.4 christos bot();
377 1.1 cgd
378 1.1 cgd flags.move = 1;
379 1.1 cgd
380 1.4 christos if (multi >= 0 && occupation) {
381 1.4 christos if (monster_nearby())
382 1.1 cgd stop_occupation();
383 1.4 christos else if ((*occupation) () == 0)
384 1.1 cgd occupation = 0;
385 1.1 cgd continue;
386 1.1 cgd }
387 1.4 christos if (multi > 0) {
388 1.1 cgd #ifdef QUEST
389 1.4 christos if (flags.run >= 4)
390 1.4 christos finddir();
391 1.1 cgd #endif
392 1.1 cgd lookaround();
393 1.4 christos if (!multi) { /* lookaround may clear multi */
394 1.1 cgd flags.move = 0;
395 1.1 cgd continue;
396 1.1 cgd }
397 1.4 christos if (flags.mv) {
398 1.4 christos if (multi < COLNO && !--multi)
399 1.1 cgd flags.mv = flags.run = 0;
400 1.1 cgd domove();
401 1.1 cgd } else {
402 1.1 cgd --multi;
403 1.1 cgd rhack(save_cm);
404 1.1 cgd }
405 1.4 christos } else if (multi == 0) {
406 1.1 cgd #ifdef MAIL
407 1.1 cgd ckmailstatus();
408 1.1 cgd #endif
409 1.1 cgd rhack((char *) 0);
410 1.1 cgd }
411 1.4 christos if (multi && multi % 7 == 0)
412 1.1 cgd (void) fflush(stdout);
413 1.1 cgd }
414 1.1 cgd }
415 1.1 cgd
416 1.4 christos void
417 1.1 cgd glo(foo)
418 1.4 christos int foo;
419 1.1 cgd {
420 1.1 cgd /* construct the string xlock.n */
421 1.4 christos char *tf;
422 1.1 cgd
423 1.1 cgd tf = lock;
424 1.4 christos while (*tf && *tf != '.')
425 1.4 christos tf++;
426 1.1 cgd (void) sprintf(tf, ".%d", foo);
427 1.1 cgd }
428 1.1 cgd
429 1.1 cgd /*
430 1.1 cgd * plname is filled either by an option (-u Player or -uPlayer) or
431 1.1 cgd * explicitly (-w implies wizard) or by askname.
432 1.1 cgd * It may still contain a suffix denoting pl_character.
433 1.1 cgd */
434 1.4 christos void
435 1.4 christos askname()
436 1.4 christos {
437 1.4 christos int c, ct;
438 1.1 cgd printf("\nWho are you? ");
439 1.1 cgd (void) fflush(stdout);
440 1.1 cgd ct = 0;
441 1.4 christos while ((c = getchar()) != '\n') {
442 1.4 christos if (c == EOF)
443 1.4 christos error("End of input\n");
444 1.1 cgd /* some people get confused when their erase char is not ^H */
445 1.4 christos if (c == '\010') {
446 1.4 christos if (ct)
447 1.4 christos ct--;
448 1.1 cgd continue;
449 1.1 cgd }
450 1.4 christos if (c != '-')
451 1.4 christos if (c < 'A' || (c > 'Z' && c < 'a') || c > 'z')
452 1.4 christos c = '_';
453 1.4 christos if (ct < sizeof(plname) - 1)
454 1.4 christos plname[ct++] = c;
455 1.1 cgd }
456 1.1 cgd plname[ct] = 0;
457 1.4 christos if (ct == 0)
458 1.4 christos askname();
459 1.1 cgd }
460 1.1 cgd
461 1.4 christos /* VARARGS1 */
462 1.4 christos void
463 1.4 christos #ifdef __STDC__
464 1.4 christos impossible(const char *s, ...)
465 1.4 christos #else
466 1.4 christos impossible(va_alist)
467 1.4 christos va_dcl
468 1.4 christos #endif
469 1.1 cgd {
470 1.4 christos va_list ap;
471 1.4 christos #ifndef __STDC__
472 1.4 christos const char *s;
473 1.4 christos
474 1.4 christos va_start(ap);
475 1.4 christos s = va_arg(ap, const char *);
476 1.4 christos #else
477 1.4 christos va_start(ap, s);
478 1.4 christos #endif
479 1.4 christos vpline(s, ap);
480 1.4 christos va_end(ap);
481 1.1 cgd pline("Program in disorder - perhaps you'd better Quit.");
482 1.1 cgd }
483 1.1 cgd
484 1.1 cgd #ifdef CHDIR
485 1.1 cgd static void
486 1.1 cgd chdirx(dir, wr)
487 1.4 christos char *dir;
488 1.4 christos boolean wr;
489 1.1 cgd {
490 1.1 cgd
491 1.1 cgd #ifdef SECURE
492 1.4 christos if (dir /* User specified directory? */
493 1.1 cgd #ifdef HACKDIR
494 1.4 christos && strcmp(dir, HACKDIR) /* and not the default? */
495 1.1 cgd #endif
496 1.1 cgd ) {
497 1.4 christos (void) setuid(getuid()); /* Ron Wessels */
498 1.1 cgd (void) setgid(getgid());
499 1.1 cgd }
500 1.1 cgd #endif
501 1.1 cgd
502 1.1 cgd #ifdef HACKDIR
503 1.4 christos if (dir == NULL)
504 1.1 cgd dir = HACKDIR;
505 1.1 cgd #endif
506 1.1 cgd
507 1.4 christos if (dir && chdir(dir) < 0) {
508 1.1 cgd perror(dir);
509 1.1 cgd error("Cannot chdir to %s.", dir);
510 1.1 cgd }
511 1.1 cgd /* warn the player if he cannot write the record file */
512 1.1 cgd /* perhaps we should also test whether . is writable */
513 1.1 cgd /* unfortunately the access systemcall is worthless */
514 1.4 christos if (wr) {
515 1.4 christos int fd;
516 1.1 cgd
517 1.4 christos if (dir == NULL)
518 1.4 christos dir = ".";
519 1.4 christos if ((fd = open(RECORD, 2)) < 0) {
520 1.4 christos printf("Warning: cannot write %s/%s", dir, RECORD);
521 1.4 christos getret();
522 1.4 christos } else
523 1.4 christos (void) close(fd);
524 1.1 cgd }
525 1.1 cgd }
526 1.1 cgd #endif
527 1.1 cgd
528 1.4 christos void
529 1.1 cgd stop_occupation()
530 1.1 cgd {
531 1.4 christos if (occupation) {
532 1.1 cgd pline("You stop %s.", occtxt);
533 1.1 cgd occupation = 0;
534 1.1 cgd }
535 1.1 cgd }
536