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 $ */
10 #define gamename "quest"
12 #define gamename "hack"
15 void (*afternmv
)(void);
16 bool (*occupation
)(void);
20 int hackpid
; /* current pid */
21 int locknum
; /* max num of players */
23 char *catmore
; /* default pager */
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 */
32 static void chdirx(const char *, bool);
36 main(int argc
, char *argv
[])
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
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)) {
60 if(*dir
== '=' || *dir
== ':') dir
++;
61 if(!*dir
&& argc
> 1) {
67 error("Flag -d must be followed by a directory name.");
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
79 * Note that we trust him here; it is possible to play under
80 * somebody else's name.
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
97 if(argc
> 1 && !strncmp(argv
[1], "-s", 2)) {
106 * It seems he really wants to play.
107 * Remember tty modes, to be restored on exit.
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.
126 * We cannot do chdir earlier, otherwise gethdate will fail.
135 while(argc
> 1 && argv
[1][0] == '-'){
151 strncpy(plname
, argv
[0]+2, sizeof(plname
)-1);
155 strncpy(plname
, argv
[0], sizeof(plname
)-1);
157 printf("Player name expected after -u\n");
160 /* allow -T for Tourist, etc. */
161 strncpy(pl_character
, argv
[0]+1,
162 sizeof(pl_character
)-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
;
173 if(!(catmore
= getenv("HACKPAGER")) && !(catmore
= getenv("PAGER")))
180 if(wizard
) strcpy(plname
, "wizard"); else
182 if(!*plname
|| !strncmp(plname
, "player", 4)
183 || !strncmp(plname
, "games", 4))
185 plnamesuffix(); /* strip suffix from name; calls askname() */
186 /* again if suffix was whole name */
187 /* accepts any suffix */
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
);
199 getlock(); /* sets lock if locknum != 0 */
204 if((sfoo
= getenv("MAGIC")))
207 case 'n': srandom(*sfoo
++);
211 if((sfoo
= getenv("GENOCIDED"))){
213 struct permonst
*pm
= mons
;
214 char *gp
= genocided
;
216 while(pm
< mons
+CMNUM
+2){
217 if(!index(sfoo
, pm
->mlet
))
223 strncpy(genocided
, sfoo
, sizeof(genocided
)-1);
224 strcpy(fut_geno
, genocided
);
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...");
238 pline("Hello %s, welcome to %s!", plname
, gamename
);
242 fobj
= fcobj
= invent
= 0;
243 fmon
= fallen_down
= 0;
250 signal(SIGINT
,done1
);
258 { struct monst
*mtmp
;
259 if((mtmp
= m_at(u
.ux
, u
.uy
))) mnexto(mtmp
); /* riv05!a3 */
263 if(flags
.nonews
|| !readnews())
264 /* after reading news we did docrt() already */
268 /* give welcome message before pickup messages */
269 pline("Hello %s, welcome to %s!", plname
, gamename
);
272 read_engr_at(u
.ux
,u
.uy
);
276 flags
.moonphase
= phase_of_the_moon();
277 if(flags
.moonphase
== FULL_MOON
) {
278 pline("You are lucky! Full moon tonight.");
280 } else if(flags
.moonphase
== NEW_MOON
) {
281 pline("Be careful! New moon tonight.");
287 if(flags
.move
) { /* actual time passed */
292 (!(Fast
& ~INTRINSIC
) && (!Fast
|| rn2(3)))) {
295 makemon((struct permonst
*)0, 0, 0);
300 if(flags
.time
) flags
.botl
= 1;
305 if(u
.uhp
*10 < u
.uhpmax
&& moves
-wailmsg
> 50){
308 pline("You hear the wailing of the Banshee...");
310 pline("You hear the howling of the CwnAnnwn...");
312 if(u
.uhp
< u
.uhpmax
) {
314 if(Regeneration
|| !(moves
%3)) {
316 u
.uhp
+= rnd((int) u
.ulevel
-9);
320 } else if(Regeneration
||
321 (!(moves
%(22-u
.ulevel
*2)))) {
326 if(Teleportation
&& !rn2(85)) tele();
327 if(Searching
&& multi
>= 0) dosearch();
334 pline(nomovemsg
? nomovemsg
:
335 "You can move again.");
337 if(afternmv
) (*afternmv
)();
344 if(!flags
.mv
|| Blind
)
351 if(flags
.botl
|| flags
.botlx
) bot();
355 if(multi
>= 0 && occupation
) {
358 else if ((*occupation
)() == 0)
365 if(flags
.run
>= 4) finddir();
368 if(!multi
) { /* lookaround may clear multi */
373 if(multi
< COLNO
&& !--multi
)
374 flags
.mv
= flags
.run
= 0;
380 } else if(multi
== 0) {
386 if(multi
&& multi
%7 == 0)
394 /* construct the string xlock.n */
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.
411 printf("\nWho are you? ");
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 */
422 if(c
< 'A' || (c
> 'Z' && c
< 'a') || c
> 'z') c
= '_';
423 if(ct
< (int)sizeof(plname
)-1) plname
[ct
++] = c
;
426 if(ct
== 0) askname();
431 impossible(const char *s
, ...)
437 pline("Program in disorder - perhaps you'd better Quit.");
442 chdirx(const char *dir
, bool wr
)
446 if(dir
/* User specified directory? */
448 && strcmp(dir
, HACKDIR
) /* and not the default? */
461 if(dir
&& chdir(dir
) < 0) {
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 */
474 if((fd
= open(RECORD
, 2)) < 0) {
475 printf("Warning: cannot write %s/%s", dir
, RECORD
);
484 stop_occupation(void)
487 pline("You stop %s.", occtxt
);