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