Initial import from FreeBSD RELENG_4:
[dragonfly.git] / games / phantasia / main.c
1 /*
2  * Phantasia 3.3.2 -- Interterminal fantasy game
3  *
4  * Edward A. Estes
5  * AT&T, March 12, 1986
6  *
7  * $FreeBSD: src/games/phantasia/main.c,v 1.8 1999/11/16 02:57:34 billf Exp $
8  */
9
10 /* DISCLAIMER:
11  *
12  * This game is distributed for free as is.  It is not guaranteed to work
13  * in every conceivable environment.  It is not even guaranteed to work
14  * in ANY environment.
15  *
16  * This game is distributed without notice of copyright, therefore it
17  * may be used in any manner the recipient sees fit.  However, the
18  * author assumes no responsibility for maintaining or revising this
19  * game, in its original form, or any derivitives thereof.
20  *
21  * The author shall not be responsible for any loss, cost, or damage,
22  * including consequential damage, caused by reliance on this material.
23  *
24  * The author makes no warranties, express or implied, including warranties
25  * of merchantability or fitness for a particular purpose or use.
26  *
27  * AT&T is in no way connected with this game.
28  */
29
30 #include <sys/types.h>
31 #include <pwd.h>
32 #include <string.h>
33
34 /*
35  * The program allocates as much file space as it needs to store characters,
36  * so the possibility exists for the character file to grow without bound.
37  * The file is purged upon normal entry to try to avoid that problem.
38  * A similar problem exists for energy voids.  To alleviate the problem here,
39  * the void file is cleared with every new king, and a limit is placed
40  * on the size of the energy void file.
41  */
42
43 /*
44  * Put one line of text into the file 'motd' for announcements, etc.
45  */
46
47 /*
48  * The scoreboard file is updated when someone dies, and keeps track
49  * of the highest character to date for that login.
50  * Being purged from the character file does not cause the scoreboard
51  * to be updated.
52  */
53
54 /*
55  * All source files are set up for 'vi' with shiftwidth=4, tabstop=8.
56  */
57
58 /*\f*/
59
60 /*
61  * main.c       Main routines for Phantasia
62  */
63
64 #include "include.h"
65
66 /***************************************************************************
67 / FUNCTION NAME: main()
68 /
69 / FUNCTION: initialize state, and call main process
70 /
71 / AUTHOR: E. A. Estes, 12/4/85
72 /
73 / ARGUMENTS:
74 /       int     argc - argument count
75 /       char    **argv - argument vector
76 /
77 / RETURN VALUE: none
78 /
79 / MODULES CALLED: monstlist(), checkenemy(), activelist(),
80 /       throneroom(), checkbattle(), readmessage(), changestats(), writerecord(),
81 /       tradingpost(), adjuststats(), recallplayer(), displaystats(), checktampered(),
82 /       fabs(), rollnewplayer(), time(), exit(), sqrt(), floor(), wmove(),
83 /       signal(), strcat(), purgeoldplayers(), getuid(), isatty(), wclear(),
84 /       strcpy(), system(), altercoordinates(), cleanup(), waddstr(), procmain(),
85 /       playinit(), leavegame(), localtime(), getanswer(), neatstuff(), initialstate(),
86 /       scorelist(), titlelist()
87 /
88 / GLOBAL INPUTS: *Login, Throne, Wizard, Player, *stdscr, Changed, Databuf[],
89 /       Fileloc, Stattable[]
90 /
91 / GLOBAL OUTPUTS: Wizard, Player, Changed, Fileloc, Timeout, *Statptr
92 /
93 / DESCRIPTION:
94 /       Process arguments, initialize program, and loop forever processing
95 /       player input.
96 /
97 ****************************************************************************/
98
99 main(argc, argv)
100 int     argc;
101 char    **argv;
102 {
103 bool    noheader = FALSE;       /* set if don't want header */
104 bool    headeronly = FALSE;     /* set if only want header */
105 bool    examine = FALSE;        /* set if examine a character */
106 time_t  seconds;                /* for time of day */
107 double  dtemp;                  /* for temporary calculations */
108
109     initialstate();             /* init globals */
110
111     /* process arguments */
112     while (--argc && (*++argv)[0] == '-')
113         switch ((*argv)[1])
114             {
115             case 's':   /* short */
116                 noheader = TRUE;
117                 break;
118
119             case 'H':   /* Header */
120                 headeronly = TRUE;
121                 break;
122
123             case 'a':   /* all users */
124                 activelist();
125                 cleanup(TRUE);
126                 /*NOTREACHED*/
127
128             case 'p':   /* purge old players */
129                 purgeoldplayers();
130                 cleanup(TRUE);
131                 /*NOTREACHED*/
132
133             case 'S':   /* set 'Wizard' */
134                 Wizard = !getuid();
135                 break;
136
137             case 'x':   /* examine */
138                 examine = TRUE;
139                 break;
140
141             case 'm':   /* monsters */
142                 monstlist();
143                 cleanup(TRUE);
144                 /*NOTREACHED*/
145
146             case 'b':   /* scoreboard */
147                 scorelist();
148                 cleanup(TRUE);
149                 /*NOTREACHED*/
150                 }
151
152     if (!isatty(0))             /* don't let non-tty's play */
153         cleanup(TRUE);
154         /*NOTREACHED*/
155
156     playinit();                 /* set up to catch signals, init curses */
157
158     if (examine)
159         {
160         changestats(FALSE);
161         cleanup(TRUE);
162         /*NOTREACHED*/
163         }
164
165     if (!noheader)
166         {
167         titlelist();
168         purgeoldplayers();    /* clean up old characters */
169         }
170
171     if (headeronly)
172         cleanup(TRUE);
173         /*NOTREACHED*/
174
175     do
176         /* get the player structure filled */
177         {
178         Fileloc = -1L;
179
180         mvaddstr(22, 17, "Do you have a character to run [Q = Quit] ? ");
181
182         switch (getanswer("NYQ", FALSE))
183             {
184             case 'Y':
185                 Fileloc = recallplayer();
186                 break;
187
188             case 'Q':
189                 cleanup(TRUE);
190                 /*NOTREACHED*/
191
192             default:
193                 Fileloc = rollnewplayer();
194                 break;
195             }
196         clear();
197         }
198     while (Fileloc < 0L);
199
200     if (Player.p_level > 5.0)
201         /* low level players have long timeout */
202         Timeout = TRUE;
203
204     /* update some important player statistics */
205     strcpy(Player.p_login, Login);
206     time(&seconds);
207     Player.p_lastused = localtime(&seconds)->tm_yday;
208     Player.p_status = S_PLAYING;
209     writerecord(&Player, Fileloc);
210
211     Statptr = &Stattable[Player.p_type];        /* initialize pointer */
212
213     /* catch interrupts */
214 #ifdef  BSD41
215     sigset(SIGINT, interrupt);
216 #endif
217 #ifdef  BSD42
218     signal(SIGINT, interrupt);
219 #endif
220 #ifdef  SYS3
221     signal(SIGINT, interrupt);
222 #endif
223 #ifdef  SYS5
224     signal(SIGINT, interrupt);
225 #endif
226
227     altercoordinates(Player.p_x, Player.p_y, A_FORCED); /* set some flags */
228
229     clear();
230
231     for (;;)
232         /* loop forever, processing input */
233         {
234
235         adjuststats();          /* cleanup stats */
236
237         if (Throne && Player.p_crowns == 0 && Player.p_specialtype != SC_KING)
238             /* not allowed on throne -- move */
239             {
240             mvaddstr(5,0,"You're not allowed in the Lord's Chamber without a crown.\n");
241             altercoordinates(0.0, 0.0, A_NEAR);
242             }
243
244         checktampered();        /* check for energy voids, etc. */
245
246         if (Player.p_status != S_CLOAKED
247             /* not cloaked */
248             && (dtemp = fabs(Player.p_x)) == fabs(Player.p_y)
249             /* |x| = |y| */
250             && !Throne)
251             /* not on throne */
252             {
253             dtemp = sqrt(dtemp / 100.0);
254             if (floor(dtemp) == dtemp)
255                 /* |x| / 100 == n*n; at a trading post */
256                 {
257                 tradingpost();
258                 clear();
259                 }
260             }
261
262         checkbattle();          /* check for player to player battle */
263         neatstuff();            /* gurus, medics, etc. */
264
265         if (Player.p_status == S_CLOAKED)
266             /* costs 3 mana per turn to be cloaked */
267             if (Player.p_mana > 3.0)
268                 Player.p_mana -= 3.0;
269             else
270                 /* ran out of mana, uncloak */
271                 {
272                 Player.p_status = S_PLAYING;
273                 Changed = TRUE;
274                 }
275
276         if (Player.p_status != S_PLAYING && Player.p_status != S_CLOAKED)
277             /* change status back to S_PLAYING */
278             {
279             Player.p_status = S_PLAYING;
280             Changed = TRUE;
281             }
282
283         if (Changed)
284             /* update file only if important stuff has changed */
285             {
286             writerecord(&Player, Fileloc);
287             Changed = FALSE;
288             continue;
289             }
290
291         readmessage();                  /* read message, if any */
292
293         displaystats();                 /* print statistics */
294
295         move(6, 0);
296
297         if (Throne)
298             /* maybe make king, print prompt, etc. */
299             throneroom();
300
301         /* print status line */
302         addstr("1:Move  2:Players  3:Talk  4:Stats  5:Quit  ");
303         if (Player.p_level >= MEL_CLOAK && Player.p_magiclvl >= ML_CLOAK)
304             addstr("6:Cloak  ");
305         if (Player.p_level >= MEL_TELEPORT && Player.p_magiclvl >= ML_TELEPORT)
306             addstr("7:Teleport  ");
307         if (Player.p_specialtype >= SC_COUNCIL || Wizard)
308             addstr("8:Intervene  ");
309
310         procmain();                     /* process input */
311         }
312 }
313 /*\f*/
314 /************************************************************************
315 /
316 / FUNCTION NAME: initialstate()
317 /
318 / FUNCTION: initialize some important global variable
319 /
320 / AUTHOR: E. A. Estes, 12/4/85
321 /
322 / ARGUMENTS: none
323 /
324 / RETURN VALUE: none
325 /
326 / MODULES CALLED: time(), fopen(), srandom(), error(), getuid(), getlogin(),
327 /       getpwuid()
328 /
329 / GLOBAL INPUTS:
330 /
331 / GLOBAL OUTPUTS: *Energyvoidfp, Echo, Marsh, *Login, Users, Beyond,
332 /       Throne, Wizard, Changed, Okcount, Timeout, Windows, *Monstfp, *Messagefp,
333 /       *Playersfp
334 /
335 / DESCRIPTION:
336 /       Set global flags, and open files which remain open.
337 /
338 *************************************************************************/
339
340 initialstate()
341 {
342     Beyond = FALSE;
343     Marsh = FALSE;
344     Throne = FALSE;
345     Changed = FALSE;
346     Wizard = FALSE;
347     Timeout = FALSE;
348     Users = 0;
349     Windows = FALSE;
350     Echo = TRUE;
351
352     /* setup login name */
353     if ((Login = getlogin()) == NULL)
354         Login = getpwuid(getuid())->pw_name;
355
356     /* open some files */
357     if ((Playersfp = fopen(_PATH_PEOPLE, "r+")) == NULL)
358         error(_PATH_PEOPLE);
359         /*NOTREACHED*/
360
361     if ((Monstfp = fopen(_PATH_MONST, "r+")) == NULL)
362         error(_PATH_MONST);
363         /*NOTREACHED*/
364
365     if ((Messagefp = fopen(_PATH_MESS, "r")) == NULL)
366         error(_PATH_MESS);
367         /*NOTREACHED*/
368
369     if ((Energyvoidfp = fopen(_PATH_VOID, "r+")) == NULL)
370         error(_PATH_VOID);
371         /*NOTREACHED*/
372
373     srandomdev();
374 }
375 /*\f*/
376 /************************************************************************
377 /
378 / FUNCTION NAME: rollnewplayer()
379 /
380 / FUNCTION: roll up a new character
381 /
382 / AUTHOR: E. A. Estes, 12/4/85
383 /
384 / ARGUMENTS: none
385 /
386 / RETURN VALUE: none
387 /
388 / MODULES CALLED: initplayer(), allocrecord(), truncstring(), fabs(), wmove(),
389 /       wclear(), sscanf(), strcmp(), genchar(), waddstr(), findname(), mvprintw(),
390 /       getanswer(), getstring()
391 /
392 / GLOBAL INPUTS: Other, Wizard, Player, *stdscr, Databuf[]
393 /
394 / GLOBAL OUTPUTS: Echo
395 /
396 / DESCRIPTION:
397 /       Prompt player, and roll up new character.
398 /
399 *************************************************************************/
400
401 long
402 rollnewplayer()
403 {
404 int     chartype;       /* character type */
405 int     ch;             /* input */
406
407     initplayer(&Player);                /* initialize player structure */
408
409     clear();
410     mvaddstr(4, 21, "Which type of character do you want:");
411     mvaddstr(8, 4, "1:Magic User  2:Fighter  3:Elf  4:Dwarf  5:Halfling  6:Experimento  ");
412     if (Wizard) {
413         addstr("7:Super  ? ");
414         chartype = getanswer("1234567", FALSE);
415         }
416     else {
417         addstr("?  ");
418         chartype = getanswer("123456", FALSE);
419         }
420
421     do
422         {
423         genchar(chartype);              /* roll up a character */
424
425         /* print out results */
426         mvprintw(12, 14,
427             "Strength    :  %2.0f  Quickness:  %2.0f  Mana       :  %2.0f\n",
428             Player.p_strength, Player.p_quickness, Player.p_mana);
429         mvprintw(13, 14,
430             "Energy Level:  %2.0f  Brains   :  %2.0f  Magic Level:  %2.0f\n",
431             Player.p_energy, Player.p_brains, Player.p_magiclvl);
432
433         if (Player.p_type == C_EXPER || Player.p_type == C_SUPER)
434             break;
435
436         mvaddstr(14, 14, "Type '1' to keep >");
437         ch = getanswer(" ", TRUE);
438         }
439     while (ch != '1');
440
441     if (Player.p_type == C_EXPER || Player.p_type == C_SUPER)
442         /* get coordinates for experimento */
443         for (;;)
444             {
445             mvaddstr(16, 0, "Enter the X Y coordinates of your experimento ? ");
446             getstring(Databuf, SZ_DATABUF);
447             sscanf(Databuf, "%lf %lf", &Player.p_x, &Player.p_y);
448
449             if (fabs(Player.p_x) > D_EXPER || fabs(Player.p_y) > D_EXPER)
450                 mvaddstr(17, 0, "Invalid coordinates.  Try again.\n");
451             else
452                 break;
453             }
454
455     for (;;)
456         /* name the new character */
457         {
458         mvprintw(18, 0,
459             "Give your character a name [up to %d characters] ?  ", SZ_NAME - 1);
460         getstring(Player.p_name, SZ_NAME);
461         truncstring(Player.p_name);             /* remove trailing blanks */
462
463         if (Player.p_name[0] == '\0')
464             /* no null names */
465             mvaddstr(19, 0, "Invalid name.");
466         else if (findname(Player.p_name, &Other) >= 0L)
467             /* cannot have duplicate names */
468             mvaddstr(19, 0, "Name already in use.");
469         else
470             /* name is acceptable */
471             break;
472
473         addstr("  Pick another.\n");
474         }
475
476     /* get a password for character */
477     Echo = FALSE;
478
479     do
480         {
481         mvaddstr(20, 0, "Give your character a password [up to 8 characters] ? ");
482         getstring(Player.p_password, SZ_PASSWORD);
483         mvaddstr(21, 0, "One more time to verify ? ");
484         getstring(Databuf, SZ_PASSWORD);
485         }
486     while (strcmp(Player.p_password, Databuf) != 0);
487
488     Echo = TRUE;
489
490     return(allocrecord());
491 }
492 /*\f*/
493 /************************************************************************
494 /
495 / FUNCTION NAME: procmain()
496 /
497 / FUNCTION: process input from player
498 /
499 / AUTHOR: E. A. Estes, 12/4/85
500 /
501 / ARGUMENTS: none
502 /
503 / RETURN VALUE: none
504 /
505 / MODULES CALLED: dotampered(), changestats(), inputoption(), allstatslist(),
506 /       fopen(), wmove(), drandom(), sscanf(), fclose(), altercoordinates(),
507 /       waddstr(), fprintf(), distance(), userlist(), leavegame(), encounter(),
508 /       getstring(), wclrtobot()
509 /
510 / GLOBAL INPUTS: Circle, Illcmd[], Throne, Wizard, Player, *stdscr,
511 /       Databuf[], Illmove[]
512 /
513 / GLOBAL OUTPUTS: Player, Changed
514 /
515 / DESCRIPTION:
516 /       Process main menu options.
517 /
518 *************************************************************************/
519
520 procmain()
521 {
522 int     ch;                     /* input */
523 double  x;                      /* desired new x coordinate */
524 double  y;                      /* desired new y coordinate */
525 double  temp;                   /* for temporary calculations */
526 FILE    *fp;                    /* for opening files */
527 int     loop;           /* a loop counter */
528 bool    hasmoved = FALSE;       /* set if player has moved */
529
530     ch = inputoption();
531     mvaddstr(4, 0, "\n\n");             /* clear status area */
532
533     move(7, 0);
534     clrtobot();                 /* clear data on bottom area of screen */
535
536     if (Player.p_specialtype == SC_VALAR && (ch == '1' || ch == '7'))
537         /* valar cannot move */
538         ch = ' ';
539
540     switch (ch)
541         {
542         case 'K':               /* move up/north */
543         case 'N':
544             x = Player.p_x;
545             y = Player.p_y + MAXMOVE();
546             hasmoved = TRUE;
547             break;
548
549         case 'J':               /* move down/south */
550         case 'S':
551             x = Player.p_x;
552             y = Player.p_y - MAXMOVE();
553             hasmoved = TRUE;
554             break;
555
556         case 'L':               /* move right/east */
557         case 'E':
558             x = Player.p_x + MAXMOVE();
559             y = Player.p_y;
560             hasmoved = TRUE;
561             break;
562
563         case 'H':               /* move left/west */
564         case 'W':
565             x = Player.p_x - MAXMOVE();
566             y = Player.p_y;
567             hasmoved = TRUE;
568             break;
569
570         default:    /* rest */
571             Player.p_energy += (Player.p_maxenergy + Player.p_shield) / 15.0
572                 + Player.p_level / 3.0 + 2.0;
573             Player.p_energy =
574                 MIN(Player.p_energy, Player.p_maxenergy + Player.p_shield);
575
576             if (Player.p_status != S_CLOAKED)
577                 /* cannot find mana if cloaked */
578                 {
579                 Player.p_mana += (Circle + Player.p_level) / 4.0;
580
581                 if (drandom() < 0.2 && Player.p_status == S_PLAYING && !Throne)
582                     /* wandering monster */
583                     encounter(-1);
584                 }
585             break;
586
587         case 'X':               /* change/examine a character */
588             changestats(TRUE);
589             break;
590
591         case '1':               /* move */
592             for (loop = 3; loop; --loop)
593                 {
594                 mvaddstr(4, 0, "X Y Coordinates ? ");
595                 getstring(Databuf, SZ_DATABUF);
596
597                 if (sscanf(Databuf, "%lf %lf", &x, &y) != 2)
598                     mvaddstr(5, 0, "Try again\n");
599                 else if (distance(Player.p_x, x, Player.p_y, y) > MAXMOVE())
600                     ILLMOVE();
601                 else
602                     {
603                     hasmoved = TRUE;
604                     break;
605                     }
606                 }
607             break;
608
609         case '2':               /* players */
610             userlist(TRUE);
611             break;
612
613         case '3':               /* message */
614             mvaddstr(4, 0, "Message ? ");
615             getstring(Databuf, SZ_DATABUF);
616             /* we open the file for writing to erase any data which is already there */
617             fp = fopen(_PATH_MESS, "w");
618             if (Databuf[0] != '\0')
619                 fprintf(fp, "%s: %s", Player.p_name, Databuf);
620             fclose(fp);
621             break;
622
623         case '4':               /* stats */
624             allstatslist();
625             break;
626
627         case '5':               /* good-bye */
628             leavegame();
629             /*NOTREACHED*/
630
631         case '6':               /* cloak */
632             if (Player.p_level < MEL_CLOAK || Player.p_magiclvl < ML_CLOAK)
633                 ILLCMD();
634             else if (Player.p_status == S_CLOAKED)
635                 Player.p_status = S_PLAYING;
636             else if (Player.p_mana < MM_CLOAK)
637                 mvaddstr(5, 0, "No mana left.\n");
638             else
639                 {
640                 Changed = TRUE;
641                 Player.p_mana -= MM_CLOAK;
642                 Player.p_status = S_CLOAKED;
643                 }
644             break;
645
646         case '7':       /* teleport */
647             /*
648              * conditions for teleport
649              *  - 20 per (level plus magic level)
650              *  - OR council of the wise or valar or ex-valar
651              *  - OR transport from throne
652              * transports from throne cost no mana
653              */
654             if (Player.p_level < MEL_TELEPORT || Player.p_magiclvl < ML_TELEPORT)
655                 ILLCMD();
656             else
657                 for (loop = 3; loop; --loop)
658                     {
659                     mvaddstr(4, 0, "X Y Coordinates ? ");
660                     getstring(Databuf, SZ_DATABUF);
661
662                     if (sscanf(Databuf, "%lf %lf", &x, &y) == 2)
663                         {
664                         temp = distance(Player.p_x, x, Player.p_y, y);
665                         if (!Throne
666                             /* can transport anywhere from throne */
667                             && Player.p_specialtype <= SC_COUNCIL
668                             /* council, valar can transport anywhere */
669                             && temp > (Player.p_level + Player.p_magiclvl) * 20.0)
670                             /* can only move 20 per exp. level + mag. level */
671                             ILLMOVE();
672                         else
673                             {
674                             temp = (temp / 75.0 + 1.0) * 20.0;  /* mana used */
675
676                             if (!Throne && temp > Player.p_mana)
677                                 mvaddstr(5, 0, "Not enough power for that distance.\n");
678                             else
679                                 {
680                                 if (!Throne)
681                                     Player.p_mana -= temp;
682                                 hasmoved = TRUE;
683                                 break;
684                                 }
685                             }
686                         }
687                     }
688             break;
689
690         case 'C':
691         case '9':               /* monster */
692             if (Throne)
693                 /* no monsters while on throne */
694                 mvaddstr(5, 0, "No monsters in the chamber!\n");
695             else if (Player.p_specialtype != SC_VALAR)
696                 /* the valar cannot call monsters */
697                 {
698                 Player.p_sin += 1e-6;
699                 encounter(-1);
700                 }
701             break;
702
703         case '0':               /* decree */
704             if (Wizard || Player.p_specialtype == SC_KING && Throne)
705                 /* kings must be on throne to decree */
706                 dotampered();
707             else
708                 ILLCMD();
709             break;
710
711         case '8':               /* intervention */
712             if (Wizard || Player.p_specialtype >= SC_COUNCIL)
713                 dotampered();
714             else
715                 ILLCMD();
716             break;
717         }
718
719     if (hasmoved)
720         /* player has moved -- alter coordinates, and do random monster */
721         {
722         altercoordinates(x, y, A_SPECIFIC);
723
724         if (drandom() < 0.2 && Player.p_status == S_PLAYING && !Throne)
725             encounter(-1);
726         }
727 }
728 /*\f*/
729 /************************************************************************
730 /
731 / FUNCTION NAME: titlelist()
732 /
733 / FUNCTION: print title page
734 /
735 / AUTHOR: E. A. Estes, 12/4/85
736 /
737 / ARGUMENTS: none
738 /
739 / RETURN VALUE: none
740 /
741 / MODULES CALLED: fread(), fseek(), fopen(), fgets(), wmove(), strcpy(),
742 /       fclose(), strlen(), waddstr(), sprintf(), wrefresh()
743 /
744 / GLOBAL INPUTS: Lines, Other, *stdscr, Databuf[], *Playersfp
745 /
746 / GLOBAL OUTPUTS: Lines
747 /
748 / DESCRIPTION:
749 /       Print important information about game, players, etc.
750 /
751 *************************************************************************/
752
753 titlelist()
754 {
755 FILE    *fp;            /* used for opening various files */
756 bool    councilfound = FALSE;   /* set if we find a member of the council */
757 bool    kingfound = FALSE;      /* set if we find a king */
758 double  hiexp, nxtexp;          /* used for finding the two highest players */
759 double  hilvl, nxtlvl;          /* used for finding the two highest players */
760 char    hiname[21], nxtname[21];/* used for finding the two highest players */
761
762     mvaddstr(0, 14, "W e l c o m e   t o   P h a n t a s i a (vers. 3.3.2)!");
763
764     /* print message of the day */
765     if ((fp = fopen(_PATH_MOTD, "r")) != NULL
766         && fgets(Databuf, SZ_DATABUF, fp) != NULL)
767         {
768         mvaddstr(2, 40 - strlen(Databuf) / 2, Databuf);
769         fclose(fp);
770         }
771
772     /* search for king */
773     fseek(Playersfp, 0L, 0);
774     while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
775         if (Other.p_specialtype == SC_KING && Other.p_status != S_NOTUSED)
776             /* found the king */
777             {
778             sprintf(Databuf, "The present ruler is %s  Level:%.0f",
779                 Other.p_name, Other.p_level);
780             mvaddstr(4, 40 - strlen(Databuf) / 2, Databuf);
781             kingfound = TRUE;
782             break;
783             }
784
785     if (!kingfound)
786         mvaddstr(4, 24, "There is no ruler at this time.");
787
788     /* search for valar */
789     fseek(Playersfp, 0L, 0);
790     while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
791         if (Other.p_specialtype == SC_VALAR && Other.p_status != S_NOTUSED)
792             /* found the valar */
793             {
794             sprintf(Databuf, "The Valar is %s   Login:  %s", Other.p_name, Other.p_login);
795             mvaddstr(6, 40 - strlen(Databuf) / 2 , Databuf);
796             break;
797             }
798
799     /* search for council of the wise */
800     fseek(Playersfp, 0L, 0);
801     Lines = 10;
802     while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
803         if (Other.p_specialtype == SC_COUNCIL && Other.p_status != S_NOTUSED)
804             /* found a member of the council */
805             {
806             if (!councilfound)
807                 {
808                 mvaddstr(8, 30, "Council of the Wise:");
809                 councilfound = TRUE;
810                 }
811
812             /* This assumes a finite (<=5) number of C.O.W.: */
813             sprintf(Databuf, "%s   Login:  %s", Other.p_name, Other.p_login);
814             mvaddstr(Lines++, 40 - strlen(Databuf) / 2, Databuf);
815             }
816
817     /* search for the two highest players */
818     nxtname[0] = hiname[0] = '\0';
819     hiexp = 0.0;
820     nxtlvl = hilvl = 0;
821
822     fseek(Playersfp, 0L, 0);
823     while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
824         if (Other.p_experience > hiexp && Other.p_specialtype <= SC_KING && Other.p_status != S_NOTUSED)
825             /* highest found so far */
826             {
827             nxtexp = hiexp;
828             hiexp = Other.p_experience;
829             nxtlvl = hilvl;
830             hilvl = Other.p_level;
831             strcpy(nxtname, hiname);
832             strcpy(hiname, Other.p_name);
833             }
834         else if (Other.p_experience > nxtexp
835             && Other.p_specialtype <= SC_KING
836             && Other.p_status != S_NOTUSED)
837             /* next highest found so far */
838             {
839             nxtexp = Other.p_experience;
840             nxtlvl = Other.p_level;
841             strcpy(nxtname, Other.p_name);
842             }
843
844     mvaddstr(15, 28, "Highest characters are:");
845     sprintf(Databuf, "%s  Level:%.0f   and   %s  Level:%.0f",
846         hiname, hilvl, nxtname, nxtlvl);
847     mvaddstr(17, 40 - strlen(Databuf) / 2, Databuf);
848
849     /* print last to die */
850     if ((fp = fopen(_PATH_LASTDEAD,"r")) != NULL
851         && fgets(Databuf, SZ_DATABUF, fp) != NULL)
852         {
853         mvaddstr(19, 25, "The last character to die was:");
854         mvaddstr(20, 40 - strlen(Databuf) / 2,Databuf);
855         fclose(fp);
856         }
857
858     refresh();
859 }
860 /*\f*/
861 /************************************************************************
862 /
863 / FUNCTION NAME: recallplayer()
864 /
865 / FUNCTION: find a character on file
866 /
867 / AUTHOR: E. A. Estes, 12/4/85
868 /
869 / ARGUMENTS: none
870 /
871 / RETURN VALUE: none
872 /
873 / MODULES CALLED: writerecord(), truncstring(), more(), death(), wmove(),
874 /       wclear(), strcmp(), printw(), cleanup(), waddstr(), findname(), mvprintw(),
875 /       getanswer(), getstring()
876 /
877 / GLOBAL INPUTS: Player, *stdscr, Databuf[]
878 /
879 / GLOBAL OUTPUTS: Echo, Player
880 /
881 / DESCRIPTION:
882 /       Search for a character of a certain name, and check password.
883 /
884 *************************************************************************/
885
886 long
887 recallplayer()
888 {
889 long    loc = 0L;               /* location in player file */
890 int     loop;           /* loop counter */
891 int     ch;                     /* input */
892
893     clear();
894     mvprintw(10, 0, "What was your character's name ? ");
895     getstring(Databuf, SZ_NAME);
896     truncstring(Databuf);
897
898     if ((loc = findname(Databuf, &Player)) >= 0L)
899         /* found character */
900         {
901         Echo = FALSE;
902
903         for (loop = 0; loop < 2; ++loop)
904             {
905             /* prompt for password */
906             mvaddstr(11, 0, "Password ? ");
907             getstring(Databuf, SZ_PASSWORD);
908             if (strcmp(Databuf, Player.p_password) == 0)
909                 /* password good */
910                 {
911                 Echo = TRUE;
912
913                 if (Player.p_status != S_OFF)
914                     /* player did not exit normally last time */
915                     {
916                     clear();
917                     addstr("Your character did not exit normally last time.\n");
918                     addstr("If you think you have good cause to have your character saved,\n");
919                     printw("you may quit and mail your reason to 'root'.\n");
920                     addstr("Otherwise, continuing spells certain death.\n");
921                     addstr("Do you want to quit ? ");
922                     ch = getanswer("YN", FALSE);
923                     if (ch == 'Y')
924                         {
925                         Player.p_status = S_HUNGUP;
926                         writerecord(&Player, loc);
927                         cleanup(TRUE);
928                         /*NOTREACHED*/
929                         }
930                     death("Stupidity");
931                     /*NOTREACHED*/
932                     }
933                 return(loc);
934                 }
935             else
936                 mvaddstr(12, 0, "No good.\n");
937             }
938
939         Echo = TRUE;
940         }
941     else
942         mvaddstr(11, 0, "Not found.\n");
943
944     more(13);
945     return(-1L);
946 }
947 /*\f*/
948 /************************************************************************
949 /
950 / FUNCTION NAME: neatstuff()
951 /
952 / FUNCTION: do random stuff
953 /
954 / AUTHOR: E. A. Estes, 3/3/86
955 /
956 / ARGUMENTS: none
957 /
958 / RETURN VALUE: none
959 /
960 / MODULES CALLED: collecttaxes(), floor(), wmove(), drandom(), infloat(),
961 /       waddstr(), mvprintw(), getanswer()
962 /
963 / GLOBAL INPUTS: Player, *stdscr, *Statptr
964 /
965 / GLOBAL OUTPUTS: Player
966 /
967 / DESCRIPTION:
968 /       Handle gurus, medics, etc.
969 /
970 *************************************************************************/
971
972 neatstuff()
973 {
974 double  temp;   /* for temporary calculations */
975 int     ch;     /* input */
976
977     switch ((int) ROLL(0.0, 100.0))
978         {
979         case 1:
980         case 2:
981             if (Player.p_poison > 0.0)
982                 {
983                 mvaddstr(4, 0, "You've found a medic!  How much will you offer to be cured ? ");
984                 temp = floor(infloat());
985                 if (temp < 0.0 || temp > Player.p_gold)
986                     /* negative gold, or more than available */
987                     {
988                     mvaddstr(6, 0, "He was not amused, and made you worse.\n");
989                     Player.p_poison += 1.0;
990                     }
991                 else if (drandom() / 2.0 > (temp + 1.0) / MAX(Player.p_gold, 1))
992                     /* medic wants 1/2 of available gold */
993                     mvaddstr(5, 0, "Sorry, he wasn't interested.\n");
994                 else
995                     {
996                     mvaddstr(5, 0, "He accepted.");
997                     Player.p_poison = MAX(0.0, Player.p_poison - 1.0);
998                     Player.p_gold -= temp;
999                     }
1000                 }
1001             break;
1002
1003         case 3:
1004             mvaddstr(4, 0, "You've been caught raping and pillaging!\n");
1005             Player.p_experience += 4000.0;
1006             Player.p_sin += 0.5;
1007             break;
1008
1009         case 4:
1010             temp = ROLL(10.0, 75.0);
1011             mvprintw(4, 0, "You've found %.0f gold pieces, want them ? ", temp);
1012             ch = getanswer("NY", FALSE);
1013
1014             if (ch == 'Y')
1015                 collecttaxes(temp, 0.0);
1016             break;
1017
1018         case 5:
1019             if (Player.p_sin > 1.0)
1020                 {
1021                 mvaddstr(4, 0, "You've found a Holy Orb!\n");
1022                 Player.p_sin -= 0.25;
1023                 }
1024             break;
1025
1026         case 6:
1027             if (Player.p_poison < 1.0)
1028                 {
1029                 mvaddstr(4, 0, "You've been hit with a plague!\n");
1030                 Player.p_poison += 1.0;
1031                 }
1032             break;
1033
1034         case 7:
1035             mvaddstr(4, 0, "You've found some holy water.\n");
1036             ++Player.p_holywater;
1037             break;
1038
1039         case 8:
1040             mvaddstr(4, 0, "You've met a Guru. . .");
1041             if (drandom() * Player.p_sin > 1.0)
1042                 addstr("You disgusted him with your sins!\n");
1043             else if (Player.p_poison > 0.0)
1044                 {
1045                 addstr("He looked kindly upon you, and cured you.\n");
1046                 Player.p_poison = 0.0;
1047                 }
1048             else
1049                 {
1050                 addstr("He rewarded you for your virtue.\n");
1051                 Player.p_mana += 50.0;
1052                 Player.p_shield += 2.0;
1053                 }
1054             break;
1055
1056         case 9:
1057             mvaddstr(4, 0, "You've found an amulet.\n");
1058             ++Player.p_amulets;
1059             break;
1060
1061         case 10:
1062             if (Player.p_blindness)
1063                 {
1064                 mvaddstr(4, 0, "You've regained your sight!\n");
1065                 Player.p_blindness = FALSE;
1066                 }
1067             break;
1068
1069         default:        /* deal with poison */
1070             if (Player.p_poison > 0.0)
1071                 {
1072                 temp = Player.p_poison * Statptr->c_weakness
1073                     * Player.p_maxenergy / 600.0;
1074                 if (Player.p_energy > Player.p_maxenergy / 10.0
1075                     && temp + 5.0 < Player.p_energy)
1076                     Player.p_energy -= temp;
1077                 }
1078             break;
1079         }
1080 }
1081 /*\f*/
1082 /************************************************************************
1083 /
1084 / FUNCTION NAME: genchar()
1085 /
1086 / FUNCTION: generate a random character
1087 /
1088 / AUTHOR: E. A. Estes, 12/4/85
1089 /
1090 / ARGUMENTS:
1091 /       int type - ASCII value of character type to generate
1092 /
1093 / RETURN VALUE: none
1094 /
1095 / MODULES CALLED: floor(), drandom()
1096 /
1097 / GLOBAL INPUTS: Wizard, Player, Stattable[]
1098 /
1099 / GLOBAL OUTPUTS: Player
1100 /
1101 / DESCRIPTION:
1102 /       Use the lookup table for rolling stats.
1103 /
1104 *************************************************************************/
1105
1106 genchar(type)
1107 int     type;
1108 {
1109 int     subscript;              /* used for subscripting into Stattable */
1110 struct charstats        *statptr;/* for pointing into Stattable */
1111
1112     subscript = type - '1';
1113
1114     if (subscript < C_MAGIC || subscript > C_EXPER)
1115         if (subscript != C_SUPER || !Wizard)
1116             /* fighter is default */
1117             subscript = C_FIGHTER;
1118
1119     statptr = &Stattable[subscript];
1120
1121     Player.p_quickness =
1122         ROLL(statptr->c_quickness.base, statptr->c_quickness.interval);
1123     Player.p_strength =
1124         ROLL(statptr->c_strength.base, statptr->c_strength.interval);
1125     Player.p_mana =
1126         ROLL(statptr->c_mana.base, statptr->c_mana.interval);
1127     Player.p_maxenergy =
1128     Player.p_energy =
1129         ROLL(statptr->c_energy.base, statptr->c_energy.interval);
1130     Player.p_brains =
1131         ROLL(statptr->c_brains.base, statptr->c_brains.interval);
1132     Player.p_magiclvl =
1133         ROLL(statptr->c_magiclvl.base, statptr->c_magiclvl.interval);
1134
1135     Player.p_type = subscript;
1136
1137     if (Player.p_type == C_HALFLING)
1138         /* give halfling some experience */
1139         Player.p_experience = ROLL(600.0, 200.0);
1140 }
1141 /*\f*/
1142 /************************************************************************
1143 /
1144 / FUNCTION NAME: playinit()
1145 /
1146 / FUNCTION: initialize for playing game
1147 /
1148 / AUTHOR: E. A. Estes, 12/4/85
1149 /
1150 / ARGUMENTS: none
1151 /
1152 / RETURN VALUE: none
1153 /
1154 / MODULES CALLED: signal(), wclear(), noecho(), crmode(), initscr(),
1155 /       wrefresh()
1156 /
1157 / GLOBAL INPUTS: *stdscr, ill_sig()
1158 /
1159 / GLOBAL OUTPUTS: Windows
1160 /
1161 / DESCRIPTION:
1162 /       Catch a bunch of signals, and turn on curses stuff.
1163 /
1164 *************************************************************************/
1165
1166 playinit()
1167 {
1168     /* catch/ingnore signals */
1169
1170 #ifdef  BSD41
1171     sigignore(SIGQUIT);
1172     sigignore(SIGALRM);
1173     sigignore(SIGTERM);
1174     sigignore(SIGTSTP);
1175     sigignore(SIGTTIN);
1176     sigignore(SIGTTOU);
1177     sighold(SIGINT);
1178     sigset(SIGHUP, ill_sig);
1179     sigset(SIGTRAP, ill_sig);
1180     sigset(SIGIOT, ill_sig);
1181     sigset(SIGEMT, ill_sig);
1182     sigset(SIGFPE, ill_sig);
1183     sigset(SIGBUS, ill_sig);
1184     sigset(SIGSEGV, ill_sig);
1185     sigset(SIGSYS, ill_sig);
1186     sigset(SIGPIPE, ill_sig);
1187 #endif
1188 #ifdef  BSD42
1189     signal(SIGQUIT, ill_sig);
1190     signal(SIGALRM, SIG_IGN);
1191     signal(SIGTERM, SIG_IGN);
1192     signal(SIGTSTP, SIG_IGN);
1193     signal(SIGTTIN, SIG_IGN);
1194     signal(SIGTTOU, SIG_IGN);
1195     signal(SIGINT, ill_sig);
1196     signal(SIGHUP, SIG_DFL);
1197     signal(SIGTRAP, ill_sig);
1198     signal(SIGIOT, ill_sig);
1199     signal(SIGEMT, ill_sig);
1200     signal(SIGFPE, ill_sig);
1201     signal(SIGBUS, ill_sig);
1202     signal(SIGSEGV, ill_sig);
1203     signal(SIGSYS, ill_sig);
1204     signal(SIGPIPE, ill_sig);
1205 #endif
1206 #ifdef  SYS3
1207     signal(SIGINT, SIG_IGN);
1208     signal(SIGQUIT, SIG_IGN);
1209     signal(SIGTERM, SIG_IGN);
1210     signal(SIGALRM, SIG_IGN);
1211     signal(SIGHUP, ill_sig);
1212     signal(SIGTRAP, ill_sig);
1213     signal(SIGIOT, ill_sig);
1214     signal(SIGEMT, ill_sig);
1215     signal(SIGFPE, ill_sig);
1216     signal(SIGBUS, ill_sig);
1217     signal(SIGSEGV, ill_sig);
1218     signal(SIGSYS, ill_sig);
1219     signal(SIGPIPE, ill_sig);
1220 #endif
1221 #ifdef  SYS5
1222     signal(SIGINT, SIG_IGN);
1223     signal(SIGQUIT, SIG_IGN);
1224     signal(SIGTERM, SIG_IGN);
1225     signal(SIGALRM, SIG_IGN);
1226     signal(SIGHUP, ill_sig);
1227     signal(SIGTRAP, ill_sig);
1228     signal(SIGIOT, ill_sig);
1229     signal(SIGEMT, ill_sig);
1230     signal(SIGFPE, ill_sig);
1231     signal(SIGBUS, ill_sig);
1232     signal(SIGSEGV, ill_sig);
1233     signal(SIGSYS, ill_sig);
1234     signal(SIGPIPE, ill_sig);
1235 #endif
1236
1237     initscr();          /* turn on curses */
1238     noecho();           /* do not echo input */
1239     crmode();           /* do not process erase, kill */
1240     clear();
1241     refresh();
1242     Windows = TRUE;     /* mark the state */
1243 }
1244
1245 /*\f*/
1246 /************************************************************************
1247 /
1248 / FUNCTION NAME: cleanup()
1249 /
1250 / FUNCTION: close some files, and maybe exit
1251 /
1252 / AUTHOR: E. A. Estes, 12/4/85
1253 /
1254 / ARGUMENTS:
1255 /       bool doexit - exit flag
1256 /
1257 / RETURN VALUE: none
1258 /
1259 / MODULES CALLED: exit(), wmove(), fclose(), endwin(), nocrmode(), wrefresh()
1260 /
1261 / GLOBAL INPUTS: *Energyvoidfp, LINES, *stdscr, Windows, *Monstfp,
1262 /       *Messagefp, *Playersfp
1263 /
1264 / GLOBAL OUTPUTS: none
1265 /
1266 / DESCRIPTION:
1267 /       Close all open files.  If we are "in curses" terminate curses.
1268 /       If 'doexit' is set, exit, otherwise return.
1269 /
1270 *************************************************************************/
1271
1272 cleanup(doexit)
1273 bool    doexit;
1274 {
1275     if (Windows)
1276         {
1277         move(LINES - 2, 0);
1278         refresh();
1279         nocrmode();
1280         endwin();
1281         }
1282
1283     if (Playersfp) {
1284         fclose(Playersfp);
1285         Playersfp = NULL;
1286     }
1287     if (Monstfp) {
1288         fclose(Monstfp);
1289         Monstfp = NULL;
1290     }
1291     if (Messagefp) {
1292         fclose(Messagefp);
1293         Messagefp = NULL;
1294     }
1295     if (Energyvoidfp) {
1296         fclose(Energyvoidfp);
1297         Energyvoidfp = NULL;
1298     }
1299
1300     if (doexit)
1301         exit(0);
1302         /*NOTREACHED*/
1303 }