Merge from vendor branch OPENSSL:
[dragonfly.git] / games / phantasia / gamesupport.c
1 /*
2  * gamesupport.c - auxiliary routines for support of Phantasia
3  *
4  * $FreeBSD: src/games/phantasia/gamesupport.c,v 1.6 1999/11/16 02:57:33 billf Exp $
5  * $DragonFly: src/games/phantasia/gamesupport.c,v 1.2 2003/06/17 04:25:24 dillon Exp $
6  */
7
8 #include <string.h>
9 #include "include.h"
10
11 /************************************************************************
12 /
13 / FUNCTION NAME: changestats()
14 /
15 / FUNCTION: examine/change statistics for a player
16 /
17 / AUTHOR: E. A. Estes, 12/4/85
18 /
19 / ARGUMENTS:
20 /       bool ingameflag - set if called while playing game (Wizard only)
21 /
22 / RETURN VALUE: none
23 /
24 / MODULES CALLED: freerecord(), writerecord(), descrstatus(), truncstring(),
25 /       time(), more(), wmove(), wclear(), strcmp(), printw(), strcpy(),
26 /       infloat(), waddstr(), cleanup(), findname(), userlist(), mvprintw(),
27 /       localtime(), getanswer(), descrtype(), getstring()
28 /
29 / GLOBAL INPUTS: LINES, *Login, Other, Wizard, Player, *stdscr, Databuf[],
30 /       Fileloc
31 /
32 / GLOBAL OUTPUTS: Echo
33 /
34 / DESCRIPTION:
35 /       Prompt for player name to examine/change.
36 /       If the name is NULL, print a list of all players.
37 /       If we are called from within the game, check for the
38 /       desired name being the same as the current player's name.
39 /       Only the 'Wizard' may alter players.
40 /       Items are changed only if a non-zero value is specified.
41 /       To change an item to 0, use 0.1; it will be truncated later.
42 /
43 /       Players may alter their names and passwords, if the following
44 /       are true:
45 /           - current login matches the character's logins
46 /           - the password is known
47 /           - the player is not in the middle of the game (ingameflag == FALSE)
48 /
49 /       The last condition is imposed for two reasons:
50 /           - the game could possibly get a bit hectic if a player were
51 /             continually changing his/her name
52 /           - another player structure would be necessary to check for names
53 /             already in use
54 /
55 *************************************************************************/
56
57 changestats(ingameflag)
58 bool    ingameflag;
59 {
60 static char     flag[2] = /* for printing values of bools */
61         {'F', 'T'};
62 struct player   *playerp;/* pointer to structure to alter */
63 char    *prompt;        /* pointer to prompt string */
64 int     c;                      /* input */
65 int     today;                  /* day of year of today */
66 int     temp;                   /* temporary variable */
67 long    loc;                    /* location in player file */
68 time_t  now;                    /* time now */
69 double  dtemp;                  /* temporary variable */
70 bool    *bptr;                  /* pointer to bool item to change */
71 double  *dptr;                  /* pointer to double item to change */
72 short   *sptr;                  /* pointer to short item to change */
73
74     clear();
75
76     for (;;)
77         /* get name of player to examine/alter */
78         {
79         mvaddstr(5, 0, "Which character do you want to look at ? ");
80         getstring(Databuf, SZ_DATABUF);
81         truncstring(Databuf);
82
83         if (Databuf[0] == '\0')
84             userlist(ingameflag);
85         else
86             break;
87         }
88
89     loc = -1L;
90
91     if (!ingameflag)
92         /* use 'Player' structure */
93         playerp = &Player;
94     else if (strcmp(Databuf, Player.p_name) == 0)
95         /* alter/examine current player */
96         {
97         playerp = &Player;
98         loc = Fileloc;
99         }
100     else
101         /* use 'Other' structure */
102         playerp = &Other;
103
104     /* find player on file */
105     if (loc < 0L && (loc = findname(Databuf, playerp)) < 0L)
106         /* didn't find player */
107         {
108         clear();
109         mvaddstr(11, 0, "Not found.");
110         return;
111         }
112
113     time(&now);
114     today = localtime(&now)->tm_yday;
115
116     clear();
117
118     for (;;)
119         /* print player structure, and prompt for action */
120         {
121         mvprintw(0, 0,"A:Name         %s\n", playerp->p_name);
122
123         if (Wizard)
124             printw("B:Password     %s\n", playerp->p_password);
125         else
126             addstr("B:Password     XXXXXXXX\n");
127
128         printw(" :Login        %s\n", playerp->p_login);
129
130         printw("C:Experience   %.0f\n", playerp->p_experience);
131         printw("D:Level        %.0f\n", playerp->p_level);
132         printw("E:Strength     %.0f\n", playerp->p_strength);
133         printw("F:Sword        %.0f\n", playerp->p_sword);
134         printw(" :Might        %.0f\n", playerp->p_might);
135         printw("G:Energy       %.0f\n", playerp->p_energy);
136         printw("H:Max-Energy   %.0f\n", playerp->p_maxenergy);
137         printw("I:Shield       %.0f\n", playerp->p_shield);
138         printw("J:Quickness    %.0f\n", playerp->p_quickness);
139         printw("K:Quicksilver  %.0f\n", playerp->p_quksilver);
140         printw(" :Speed        %.0f\n", playerp->p_speed);
141         printw("L:Magic Level  %.0f\n", playerp->p_magiclvl);
142         printw("M:Mana         %.0f\n", playerp->p_mana);
143         printw("N:Brains       %.0f\n", playerp->p_brains);
144
145         if (Wizard || playerp->p_specialtype != SC_VALAR)
146             mvaddstr(0, 40, descrstatus(playerp));
147
148         mvprintw(1, 40, "O:Poison       %0.3f\n", playerp->p_poison);
149         mvprintw(2, 40, "P:Gold         %.0f\n", playerp->p_gold);
150         mvprintw(3, 40, "Q:Gem          %.0f\n", playerp->p_gems);
151         mvprintw(4, 40, "R:Sin          %0.3f\n", playerp->p_sin);
152         if (Wizard)
153             {
154             mvprintw(5, 40, "S:X-coord      %.0f\n", playerp->p_x);
155             mvprintw(6, 40, "T:Y-coord      %.0f\n", playerp->p_y);
156             }
157         else
158             {
159             mvaddstr(5, 40, "S:X-coord      ?\n");
160             mvaddstr(6, 40, "T:Y-coord      ?\n");
161             }
162
163         mvprintw(7, 40, "U:Age          %ld\n", playerp->p_age);
164         mvprintw(8, 40, "V:Degenerated  %d\n", playerp->p_degenerated);
165
166         mvprintw(9, 40, "W:Type         %d (%s)\n",
167             playerp->p_type, descrtype(playerp, FALSE) + 1);
168         mvprintw(10, 40, "X:Special Type %d\n", playerp->p_specialtype);
169         mvprintw(11, 40, "Y:Lives        %d\n", playerp->p_lives);
170         mvprintw(12, 40, "Z:Crowns       %d\n", playerp->p_crowns);
171         mvprintw(13, 40, "0:Charms       %d\n", playerp->p_charms);
172         mvprintw(14, 40, "1:Amulets      %d\n", playerp->p_amulets);
173         mvprintw(15, 40, "2:Holy Water   %d\n", playerp->p_holywater);
174
175         temp = today - playerp->p_lastused;
176         if (temp < 0)
177             /* last year */
178             temp += 365;
179         mvprintw(16, 40, "3:Lastused     %d  (%d)\n", playerp->p_lastused,  temp);
180
181         mvprintw(18, 8, "4:Palantir %c  5:Blessing %c  6:Virgin %c  7:Blind %c",
182             flag[playerp->p_palantir],
183             flag[playerp->p_blessing],
184             flag[playerp->p_virgin],
185             flag[playerp->p_blindness]);
186
187         if (!Wizard)
188             mvprintw(19, 8, "8:Ring    %c",
189                 flag[playerp->p_ring.ring_type != R_NONE]);
190         else
191             mvprintw(19, 8, "8:Ring    %d  9:Duration %d",
192                 playerp->p_ring.ring_type, playerp->p_ring.ring_duration);
193
194         if (!Wizard
195             /* not wizard */
196             && (ingameflag || strcmp(Login, playerp->p_login) != 0))
197             /* in game or not examining own character */
198             {
199             if (ingameflag)
200                 {
201                 more(LINES - 1);
202                 clear();
203                 return;
204                 }
205             else
206                 cleanup(TRUE);
207                 /*NOTREACHED*/
208             }
209
210         mvaddstr(20, 0, "!:Quit       ?:Delete");
211         mvaddstr(21, 0, "What would you like to change ? ");
212
213         if (Wizard)
214             c = getanswer(" ", TRUE);
215         else
216             /* examining own player; allow to change name and password */
217             c = getanswer("!BA", FALSE);
218
219         switch (c)
220             {
221             case 'A':   /* change name */
222             case 'B':   /* change password */
223                 if (!Wizard)
224                     /* prompt for password */
225                     {
226                     mvaddstr(23, 0, "Password ? ");
227                     Echo = FALSE;
228                     getstring(Databuf, 9);
229                     Echo = TRUE;
230                     if (strcmp(Databuf, playerp->p_password) != 0)
231                         continue;
232                     }
233
234                 if (c == 'A')
235                     /* get new name */
236                     {
237                     mvaddstr(23, 0, "New name: ");
238                     getstring(Databuf, SZ_NAME);
239                     truncstring(Databuf);
240                     if (Databuf[0] != '\0')
241                         if (Wizard || findname(Databuf, &Other) < 0L)
242                             strcpy(playerp->p_name, Databuf);
243                     }
244                 else
245                     /* get new password */
246                     {
247                     if (!Wizard)
248                         Echo = FALSE;
249
250                     do
251                         /* get two copies of new password until they match */
252                         {
253                         /* get first copy */
254                         mvaddstr(23, 0, "New password ? ");
255                         getstring(Databuf, SZ_PASSWORD);
256                         if (Databuf[0] == '\0')
257                             break;
258
259                         /* get second copy */
260                         mvaddstr(23, 0, "One more time ? ");
261                         getstring(playerp->p_password, SZ_PASSWORD);
262                         }
263                     while (strcmp(playerp->p_password, Databuf) != 0);
264
265                     Echo = TRUE;
266                     }
267
268                 continue;
269
270             case 'C':   /* change experience */
271                 prompt = "experience";
272                 dptr = &playerp->p_experience;
273                 goto DALTER;
274
275             case 'D':   /* change level */
276                 prompt = "level";
277                 dptr = &playerp->p_level;
278                 goto DALTER;
279
280             case 'E':   /* change strength */
281                 prompt = "strength";
282                 dptr = &playerp->p_strength;
283                 goto DALTER;
284
285             case 'F':   /* change swords */
286                 prompt = "sword";
287                 dptr = &playerp->p_sword;
288                 goto DALTER;
289
290             case 'G':   /* change energy */
291                 prompt = "energy";
292                 dptr = &playerp->p_energy;
293                 goto DALTER;
294
295             case 'H':   /* change maximum energy */
296                 prompt = "max energy";
297                 dptr = &playerp->p_maxenergy;
298                 goto DALTER;
299
300             case 'I':   /* change shields */
301                 prompt = "shield";
302                 dptr = &playerp->p_shield;
303                 goto DALTER;
304
305             case 'J':   /* change quickness */
306                 prompt = "quickness";
307                 dptr = &playerp->p_quickness;
308                 goto DALTER;
309
310             case 'K':   /* change quicksilver */
311                 prompt = "quicksilver";
312                 dptr = &playerp->p_quksilver;
313                 goto DALTER;
314
315             case 'L':   /* change magic */
316                 prompt = "magic level";
317                 dptr = &playerp->p_magiclvl;
318                 goto DALTER;
319
320             case 'M':   /* change mana */
321                 prompt = "mana";
322                 dptr = &playerp->p_mana;
323                 goto DALTER;
324
325             case 'N':   /* change brains */
326                 prompt = "brains";
327                 dptr = &playerp->p_brains;
328                 goto DALTER;
329
330             case 'O':   /* change poison */
331                 prompt = "poison";
332                 dptr = &playerp->p_poison;
333                 goto DALTER;
334
335             case 'P':   /* change gold */
336                 prompt = "gold";
337                 dptr = &playerp->p_gold;
338                 goto DALTER;
339
340             case 'Q':   /* change gems */
341                 prompt = "gems";
342                 dptr = &playerp->p_gems;
343                 goto DALTER;
344
345             case 'R':   /* change sin */
346                 prompt = "sin";
347                 dptr = &playerp->p_sin;
348                 goto DALTER;
349
350             case 'S':   /* change x coord */
351                 prompt = "x";
352                 dptr = &playerp->p_x;
353                 goto DALTER;
354
355             case 'T':   /* change y coord */
356                 prompt = "y";
357                 dptr = &playerp->p_y;
358                 goto DALTER;
359
360             case 'U':   /* change age */
361                 mvprintw(23, 0, "age = %ld; age = ", playerp->p_age);
362                 dtemp = infloat();
363                 if (dtemp != 0.0)
364                     playerp->p_age = (long) dtemp;
365                 continue;
366
367             case 'V':   /* change degen */
368                 mvprintw(23, 0, "degen = %d; degen = ", playerp->p_degenerated);
369                 dtemp = infloat();
370                 if (dtemp != 0.0)
371                     playerp->p_degenerated = (int) dtemp;
372                 continue;
373
374             case 'W':   /* change type */
375                 prompt = "type";
376                 sptr = &playerp->p_type;
377                 goto SALTER;
378
379             case 'X':   /* change special type */
380                 prompt = "special type";
381                 sptr = &playerp->p_specialtype;
382                 goto SALTER;
383
384             case 'Y':   /* change lives */
385                 prompt = "lives";
386                 sptr = &playerp->p_lives;
387                 goto SALTER;
388
389             case 'Z':   /* change crowns */
390                 prompt = "crowns";
391                 sptr = &playerp->p_crowns;
392                 goto SALTER;
393
394             case '0':   /* change charms */
395                 prompt = "charm";
396                 sptr = &playerp->p_charms;
397                 goto SALTER;
398
399             case '1':   /* change amulet */
400                 prompt = "amulet";
401                 sptr = &playerp->p_amulets;
402                 goto SALTER;
403
404             case '2':   /* change holy water */
405                 prompt = "holy water";
406                 sptr = &playerp->p_holywater;
407                 goto SALTER;
408
409             case '3':   /* change last-used */
410                 prompt = "last-used";
411                 sptr = &playerp->p_lastused;
412                 goto SALTER;
413
414             case '4':   /* change palantir */
415                 prompt = "palantir";
416                 bptr = &playerp->p_palantir;
417                 goto BALTER;
418
419             case '5':   /* change blessing */
420                 prompt = "blessing";
421                 bptr = &playerp->p_blessing;
422                 goto BALTER;
423
424             case '6':   /* change virgin */
425                 prompt = "virgin";
426                 bptr = &playerp->p_virgin;
427                 goto BALTER;
428
429             case '7':   /* change blindness */
430                 prompt = "blindness";
431                 bptr = &playerp->p_blindness;
432                 goto BALTER;
433
434             case '8':   /* change ring type */
435                 prompt = "ring-type";
436                 sptr = &playerp->p_ring.ring_type;
437                 goto SALTER;
438
439             case '9':   /* change ring duration */
440                 prompt = "ring-duration";
441                 sptr = &playerp->p_ring.ring_duration;
442                 goto SALTER;
443
444             case '!':   /* quit, update */
445                 if (Wizard &&
446                     (!ingameflag || playerp != &Player))
447                     /* turn off status if not modifying self */
448                     {
449                     playerp->p_status = S_OFF;
450                     playerp->p_tampered = T_OFF;
451                     }
452
453                 writerecord(playerp, loc);
454                 clear();
455                 return;
456
457             case '?':   /* delete player */
458                 if (ingameflag && playerp == &Player)
459                     /* cannot delete self */
460                     continue;
461
462                 freerecord(playerp, loc);
463                 clear();
464                 return;
465
466             default:
467                 continue;
468             }
469 DALTER:
470         mvprintw(23, 0, "%s = %f; %s = ", prompt, *dptr, prompt);
471         dtemp = infloat();
472         if (dtemp != 0.0)
473             *dptr = dtemp;
474         continue;
475
476 SALTER:
477         mvprintw(23, 0, "%s = %d; %s = ", prompt, *sptr, prompt);
478         dtemp = infloat();
479         if (dtemp != 0.0)
480             *sptr = (short) dtemp;
481         continue;
482
483 BALTER:
484         mvprintw(23, 0, "%s = %c; %s = ", prompt, flag[*bptr], prompt);
485         c = getanswer("\nTF", TRUE);
486         if (c == 'T')
487             *bptr = TRUE;
488         else if (c == 'F')
489             *bptr = FALSE;
490         continue;
491         }
492 }
493 /*\f*/
494 /************************************************************************
495 /
496 / FUNCTION NAME: monstlist()
497 /
498 / FUNCTION: print a monster listing
499 /
500 / AUTHOR: E. A. Estes, 2/27/86
501 /
502 / ARGUMENTS: none
503 /
504 / RETURN VALUE: none
505 /
506 / MODULES CALLED: puts(), fread(), fseek(), printf()
507 /
508 / GLOBAL INPUTS: Curmonster, *Monstfp
509 /
510 / GLOBAL OUTPUTS: none
511 /
512 / DESCRIPTION:
513 /       Read monster file, and print a monster listing on standard output.
514 /
515 *************************************************************************/
516
517 monstlist()
518 {
519 int     count = 0;              /* count in file */
520
521     puts(" #)  Name                 Str  Brain  Quick  Energy  Exper  Treas  Type  Flock%\n");
522     fseek(Monstfp, 0L, 0);
523     while (fread((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp) == 1)
524         printf("%2d)  %-20.20s%4.0f   %4.0f     %2.0f   %5.0f  %5.0f     %2d    %2d     %3.0f\n", count++,
525             Curmonster.m_name, Curmonster.m_strength, Curmonster.m_brains,
526             Curmonster.m_speed, Curmonster.m_energy, Curmonster.m_experience,
527             Curmonster.m_treasuretype, Curmonster.m_type, Curmonster.m_flock);
528 }
529 /*\f*/
530 /************************************************************************
531 /
532 / FUNCTION NAME: scorelist()
533 /
534 / FUNCTION: print player score board
535 /
536 / AUTHOR: E. A. Estes, 12/4/85
537 /
538 / ARGUMENTS: none
539 /
540 / RETURN VALUE: none
541 /
542 / MODULES CALLED: fread(), fopen(), printf(), fclose()
543 /
544 / GLOBAL INPUTS:
545 /
546 / GLOBAL OUTPUTS: none
547 /
548 / DESCRIPTION:
549 /       Read the scoreboard file and print the contents.
550 /
551 *************************************************************************/
552
553 scorelist()
554 {
555 struct  scoreboard      sbuf;   /* for reading entries */
556 FILE    *fp;            /* to open the file */
557
558     if ((fp = fopen(_PATH_SCORE, "r")) != NULL)
559         {
560         while (fread((char *) &sbuf, SZ_SCORESTRUCT, 1, fp) == 1)
561             printf("%-20s   (%-9s)  Level: %6.0f  Type: %s\n",
562                 sbuf.sb_name, sbuf.sb_login, sbuf.sb_level, sbuf.sb_type);
563         fclose(fp);
564         }
565 }
566 /*\f*/
567 /************************************************************************
568 /
569 / FUNCTION NAME: activelist()
570 /
571 / FUNCTION: print list of active players to standard output
572 /
573 / AUTHOR: E. A. Estes, 3/7/86
574 /
575 / ARGUMENTS: none
576 /
577 / RETURN VALUE: none
578 /
579 / MODULES CALLED: descrstatus(), fread(), fseek(), printf(), descrtype()
580 /
581 / GLOBAL INPUTS: Other, *Playersfp
582 /
583 / GLOBAL OUTPUTS: none
584 /
585 / DESCRIPTION:
586 /       Read player file, and print list of active records to standard output.
587 /
588 *************************************************************************/
589
590 activelist()
591 {
592     fseek(Playersfp, 0L, 0);
593     printf("Current characters on file are:\n\n");
594
595     while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
596         if (Other.p_status != S_NOTUSED)
597             printf("%-20s   (%-9s)  Level: %6.0f  %s  (%s)\n",
598                 Other.p_name, Other.p_login, Other.p_level,
599                 descrtype(&Other, FALSE), descrstatus(&Other));
600
601 }
602 /*\f*/
603 /************************************************************************
604 /
605 / FUNCTION NAME: purgeoldplayers()
606 /
607 / FUNCTION: purge inactive players from player file
608 /
609 / AUTHOR: E. A. Estes, 12/4/85
610 /
611 / ARGUMENTS: none
612 /
613 / RETURN VALUE: none
614 /
615 / MODULES CALLED: freerecord(), time(), fread(), fseek(), localtime()
616 /
617 / GLOBAL INPUTS: Other, *Playersfp
618 /
619 / GLOBAL OUTPUTS: none
620 /
621 / DESCRIPTION:
622 /       Delete characters which have not been used with the last
623 /       three weeks.
624 /
625 *************************************************************************/
626
627 purgeoldplayers()
628 {
629 int     today;          /* day of year for today */
630 int     daysold;        /* how many days since the character has been used */
631 time_t  ltime;          /* time in seconds */
632 long    loc = 0L;       /* location in file */
633
634     time(&ltime);
635     today = localtime(&ltime)->tm_yday;
636
637     for (;;)
638         {
639         fseek(Playersfp, loc, 0);
640         if (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) != 1)
641             break;
642
643         daysold = today - Other.p_lastused;
644         if (daysold < 0)
645             daysold += 365;
646
647         if (daysold > N_DAYSOLD)
648             /* player hasn't been used in a while; delete */
649             freerecord(&Other, loc);
650
651         loc += SZ_PLAYERSTRUCT;
652         }
653 }
654 /*\f*/
655 /************************************************************************
656 /
657 / FUNCTION NAME: enterscore()
658 /
659 / FUNCTION: enter player into scoreboard
660 /
661 / AUTHOR: E. A. Estes, 12/4/85
662 /
663 / ARGUMENTS: none
664 /
665 / RETURN VALUE: none
666 /
667 / MODULES CALLED: fread(), fseek(), fopen(), error(), strcmp(), fclose(),
668 /       strcpy(), fwrite(), descrtype()
669 /
670 / GLOBAL INPUTS: Player
671 /
672 / GLOBAL OUTPUTS: none
673 /
674 / DESCRIPTION:
675 /       The scoreboard keeps track of the highest character on a
676 /       per-login basis.
677 /       Search the scoreboard for an entry for the current login,
678 /       if an entry is found, and it is lower than the current player,
679 /       replace it, otherwise create an entry.
680 /
681 *************************************************************************/
682
683 enterscore()
684 {
685 struct  scoreboard sbuf;                /* buffer to read in scoreboard entries */
686 FILE    *fp;                            /* to open scoreboard file */
687 long    loc = 0L;                       /* location in scoreboard file */
688 bool    found = FALSE;                  /* set if we found an entry for this login */
689
690     if ((fp = fopen(_PATH_SCORE, "r+")) != NULL)
691         {
692         while (fread((char *) &sbuf, SZ_SCORESTRUCT, 1, fp) == 1)
693             if (strcmp(Player.p_login, sbuf.sb_login) == 0)
694                 {
695                 found = TRUE;
696                 break;
697                 }
698             else
699                 loc += SZ_SCORESTRUCT;
700         }
701     else
702         {
703         error(_PATH_SCORE);
704         /*NOTREACHED*/
705         }
706
707     /*
708      * At this point, 'loc' will either indicate a point beyond
709      * the end of file, or the place where the previous entry
710      * was found.
711      */
712
713     if ((!found) || Player.p_level > sbuf.sb_level)
714         /* put new entry in for this login */
715         {
716         strcpy(sbuf.sb_login, Player.p_login);
717         strcpy(sbuf.sb_name, Player.p_name);
718         sbuf.sb_level = Player.p_level;
719         strcpy(sbuf.sb_type, descrtype(&Player, TRUE));
720         }
721
722     /* update entry */
723     fseek(fp, loc, 0);
724     fwrite((char *) &sbuf, SZ_SCORESTRUCT, 1, fp);
725     fclose(fp);
726 }