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
== ':')
62 if (!*dir
&& argc
> 1) {
68 error("Flag -d must be followed by a directory name.");
73 * Who am i? Algorithm: 1. Use name as specified in HACKOPTIONS
74 * 2. Use $USER or $LOGNAME (if 1. fails)
75 * 3. Use getlogin() (if 2. fails)
76 * The resulting name is overridden by command line options.
77 * If everything fails, or if the resulting name is some generic
78 * account like "games", "play", "player", "hack" then eventually
80 * Note that we trust him here; it is possible to play under
81 * somebody else's name.
87 if (!*plname
&& (s
= getenv("USER")))
88 strncpy(plname
, s
, sizeof(plname
) - 1);
89 if (!*plname
&& (s
= getenv("LOGNAME")))
90 strncpy(plname
, s
, sizeof(plname
) - 1);
91 if (!*plname
&& (s
= getlogin()))
92 strncpy(plname
, s
, sizeof(plname
) - 1);
96 * Now we know the directory containing 'record' and
99 if (argc
> 1 && !strncmp(argv
[1], "-s", 2)) {
108 * It seems he really wants to play.
109 * Remember tty modes, to be restored on exit.
112 setbuf(stdout
, obuf
);
117 u
.uhp
= 1; /* prevent RIP on early quits */
118 u
.ux
= FAR
; /* prevent nscr() */
119 signal(SIGHUP
, hangup
);
122 * Find the creation date of this game,
123 * so as to avoid restoring outdated savefiles.
128 * We cannot do chdir earlier, otherwise gethdate will fail.
137 while (argc
> 1 && argv
[1][0] == '-') {
140 switch (argv
[0][1]) {
153 strncpy(plname
, argv
[0] + 2, sizeof(plname
) - 1);
157 strncpy(plname
, argv
[0], sizeof(plname
) - 1);
159 printf("Player name expected after -u\n");
162 /* allow -T for Tourist, etc. */
163 strncpy(pl_character
, argv
[0] + 1,
164 sizeof(pl_character
) - 1);
169 locknum
= atoi(argv
[1]);
170 #ifdef MAX_NR_OF_PLAYERS
171 if (!locknum
|| locknum
> MAX_NR_OF_PLAYERS
)
172 locknum
= MAX_NR_OF_PLAYERS
;
175 if (!(catmore
= getenv("HACKPAGER")) && !(catmore
= getenv("PAGER")))
183 strcpy(plname
, "wizard");
186 if (!*plname
|| !strncmp(plname
, "player", 4)
187 || !strncmp(plname
, "games", 4))
189 plnamesuffix(); /* strip suffix from name; calls askname() */
190 /* again if suffix was whole name */
191 /* accepts any suffix */
196 * check for multiple games under the same name
197 * (if !locknum) or check max nr of players (otherwise)
199 signal(SIGQUIT
, SIG_IGN
);
200 signal(SIGINT
, SIG_IGN
);
202 strcpy(lock
, plname
);
203 getlock(); /* sets lock if locknum != 0 */
207 strcpy(lock
, plname
);
208 if ((sfoo
= getenv("MAGIC")))
216 if ((sfoo
= getenv("GENOCIDED")) != NULL
) {
218 struct permonst
*pm
= mons
;
219 char *gp
= genocided
;
221 while (pm
< mons
+ CMNUM
+ 2) {
222 if (!strchr(sfoo
, pm
->mlet
))
228 strncpy(genocided
, sfoo
, sizeof(genocided
) - 1);
229 strcpy(fut_geno
, genocided
);
234 sprintf(SAVEF
, "save/%d%s", getuid(), plname
);
235 regularize(SAVEF
+ 5); /* avoid . or / in name */
236 if ((fd
= open(SAVEF
, O_RDONLY
)) >= 0 &&
237 (uptodate(fd
) || unlink(SAVEF
) == 666)) {
238 signal(SIGINT
, done1
);
239 pline("Restoring old save file...");
243 pline("Hello %s, welcome to %s!", plname
, gamename
);
247 fobj
= fcobj
= invent
= 0;
248 fmon
= fallen_down
= 0;
255 signal(SIGINT
, done1
);
265 if ((mtmp
= m_at(u
.ux
, u
.uy
)) != NULL
)
266 mnexto(mtmp
); /* riv05!a3 */
270 if (flags
.nonews
|| !readnews())
271 /* after reading news we did docrt() already */
275 /* give welcome message before pickup messages */
276 pline("Hello %s, welcome to %s!", plname
, gamename
);
279 read_engr_at(u
.ux
, u
.uy
);
283 flags
.moonphase
= phase_of_the_moon();
284 if (flags
.moonphase
== FULL_MOON
) {
285 pline("You are lucky! Full moon tonight.");
287 } else if (flags
.moonphase
== NEW_MOON
)
288 pline("Be careful! New moon tonight.");
293 if (flags
.move
) { /* actual time passed */
296 if (moves
% 2 == 0 ||
297 (!(Fast
& ~INTRINSIC
) && (!Fast
|| rn2(3)))) {
312 if (u
.uhp
* 10 < u
.uhpmax
&& moves
- wailmsg
> 50) {
315 pline("You hear the wailing of the Banshee...");
317 pline("You hear the howling of the CwnAnnwn...");
319 if (u
.uhp
< u
.uhpmax
) {
321 if (Regeneration
|| !(moves
% 3)) {
323 u
.uhp
+= rnd((int)u
.ulevel
- 9);
324 if (u
.uhp
> u
.uhpmax
)
327 } else if (Regeneration
||
328 (!(moves
% (22 - u
.ulevel
* 2)))) {
333 if (Teleportation
&& !rn2(85))
335 if (Searching
&& multi
>= 0)
343 pline(nomovemsg
? nomovemsg
:
344 "You can move again.");
353 if (!flags
.mv
|| Blind
)
360 if (flags
.botl
|| flags
.botlx
)
365 if (multi
>= 0 && occupation
) {
366 if (monster_nearby())
368 else if ((*occupation
)() == 0)
379 if (!multi
) { /* lookaround may clear multi */
384 if (multi
< COLNO
&& !--multi
)
385 flags
.mv
= flags
.run
= 0;
391 } else if (multi
== 0) {
397 if (multi
&& multi
% 7 == 0)
405 /* construct the string xlock.n */
409 while (*tf
&& *tf
!= '.')
411 (void)sprintf(tf
, ".%d", foo
);
415 * plname is filled either by an option (-u Player or -uPlayer) or
416 * explicitly (-w implies wizard) or by askname.
417 * It may still contain a suffix denoting pl_character.
424 printf("\nWho are you? ");
427 while ((c
= getchar()) != '\n') {
429 error("End of input\n");
430 /* some people get confused when their erase char is not ^H */
437 if (c
< 'A' || (c
> 'Z' && c
< 'a') || c
> 'z')
439 if (ct
< (int)sizeof(plname
) - 1)
449 impossible(const char *s
, ...)
456 pline("Program in disorder - perhaps you'd better Quit.");
461 chdirx(const char *dir
, bool wr
)
464 if (dir
/* User specified directory? */
466 && strcmp(dir
, HACKDIR
) /* and not the default? */
479 if (dir
&& chdir(dir
) < 0) {
481 error("Cannot chdir to %s.", dir
);
484 /* warn the player if he cannot write the record file */
485 /* perhaps we should also test whether . is writable */
486 /* unfortunately the access systemcall is worthless */
492 if ((fd
= open(RECORD
, O_RDWR
)) < 0) {
493 printf("Warning: cannot write %s/%s", dir
, RECORD
);
502 stop_occupation(void)
505 pline("You stop %s.", occtxt
);