2 * gamesupport.c - auxiliary routines for support of Phantasia
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.3 2005/05/31 00:06:26 swildner Exp $
11 /* functions which we need to know about */
13 extern void userlist(bool);
15 extern int getanswer(const char *, bool);
16 extern void getstring(char *, int);
17 extern double infloat(void);
18 extern void more(int);
20 extern void cleanup(bool);
22 extern const char *descrstatus(struct player *);
23 extern const char *descrtype(struct player *, bool);
24 extern void error(const char *);
25 extern long findname(char *, struct player *);
26 extern void freerecord(struct player *, long);
27 extern void truncstring(char *);
28 extern void writerecord(struct player *, long);
30 void changestats(bool);
33 void activelist(void);
34 void purgeoldplayers(void);
35 void enterscore(void);
37 /************************************************************************
39 / FUNCTION NAME: changestats()
41 / FUNCTION: examine/change statistics for a player
43 / AUTHOR: E. A. Estes, 12/4/85
46 / bool ingameflag - set if called while playing game (Wizard only)
50 / MODULES CALLED: freerecord(), writerecord(), descrstatus(), truncstring(),
51 / time(), more(), wmove(), wclear(), strcmp(), printw(), strcpy(),
52 / infloat(), waddstr(), cleanup(), findname(), userlist(), mvprintw(),
53 / localtime(), getanswer(), descrtype(), getstring()
55 / GLOBAL INPUTS: LINES, *Login, Other, Wizard, Player, *stdscr, Databuf[],
58 / GLOBAL OUTPUTS: Echo
61 / Prompt for player name to examine/change.
62 / If the name is NULL, print a list of all players.
63 / If we are called from within the game, check for the
64 / desired name being the same as the current player's name.
65 / Only the 'Wizard' may alter players.
66 / Items are changed only if a non-zero value is specified.
67 / To change an item to 0, use 0.1; it will be truncated later.
69 / Players may alter their names and passwords, if the following
71 / - current login matches the character's logins
72 / - the password is known
73 / - the player is not in the middle of the game (ingameflag == FALSE)
75 / The last condition is imposed for two reasons:
76 / - the game could possibly get a bit hectic if a player were
77 / continually changing his/her name
78 / - another player structure would be necessary to check for names
81 *************************************************************************/
84 changestats(bool ingameflag)
86 static char flag[2] = /* for printing values of bools */
88 struct player *playerp;/* pointer to structure to alter */
89 const char *prompt; /* pointer to prompt string */
91 int today; /* day of year of today */
92 int temp; /* temporary variable */
93 long loc; /* location in player file */
94 time_t now; /* time now */
95 double dtemp; /* temporary variable */
96 bool *bptr; /* pointer to bool item to change */
97 double *dptr; /* pointer to double item to change */
98 short *sptr; /* pointer to short item to change */
103 /* get name of player to examine/alter */
105 mvaddstr(5, 0, "Which character do you want to look at ? ");
106 getstring(Databuf, SZ_DATABUF);
107 truncstring(Databuf);
109 if (Databuf[0] == '\0')
110 userlist(ingameflag);
118 /* use 'Player' structure */
120 else if (strcmp(Databuf, Player.p_name) == 0)
121 /* alter/examine current player */
127 /* use 'Other' structure */
130 /* find player on file */
131 if (loc < 0L && (loc = findname(Databuf, playerp)) < 0L)
132 /* didn't find player */
135 mvaddstr(11, 0, "Not found.");
140 today = localtime(&now)->tm_yday;
145 /* print player structure, and prompt for action */
147 mvprintw(0, 0,"A:Name %s\n", playerp->p_name);
150 printw("B:Password %s\n", playerp->p_password);
152 addstr("B:Password XXXXXXXX\n");
154 printw(" :Login %s\n", playerp->p_login);
156 printw("C:Experience %.0f\n", playerp->p_experience);
157 printw("D:Level %.0f\n", playerp->p_level);
158 printw("E:Strength %.0f\n", playerp->p_strength);
159 printw("F:Sword %.0f\n", playerp->p_sword);
160 printw(" :Might %.0f\n", playerp->p_might);
161 printw("G:Energy %.0f\n", playerp->p_energy);
162 printw("H:Max-Energy %.0f\n", playerp->p_maxenergy);
163 printw("I:Shield %.0f\n", playerp->p_shield);
164 printw("J:Quickness %.0f\n", playerp->p_quickness);
165 printw("K:Quicksilver %.0f\n", playerp->p_quksilver);
166 printw(" :Speed %.0f\n", playerp->p_speed);
167 printw("L:Magic Level %.0f\n", playerp->p_magiclvl);
168 printw("M:Mana %.0f\n", playerp->p_mana);
169 printw("N:Brains %.0f\n", playerp->p_brains);
171 if (Wizard || playerp->p_specialtype != SC_VALAR)
172 mvaddstr(0, 40, descrstatus(playerp));
174 mvprintw(1, 40, "O:Poison %0.3f\n", playerp->p_poison);
175 mvprintw(2, 40, "P:Gold %.0f\n", playerp->p_gold);
176 mvprintw(3, 40, "Q:Gem %.0f\n", playerp->p_gems);
177 mvprintw(4, 40, "R:Sin %0.3f\n", playerp->p_sin);
180 mvprintw(5, 40, "S:X-coord %.0f\n", playerp->p_x);
181 mvprintw(6, 40, "T:Y-coord %.0f\n", playerp->p_y);
185 mvaddstr(5, 40, "S:X-coord ?\n");
186 mvaddstr(6, 40, "T:Y-coord ?\n");
189 mvprintw(7, 40, "U:Age %ld\n", playerp->p_age);
190 mvprintw(8, 40, "V:Degenerated %d\n", playerp->p_degenerated);
192 mvprintw(9, 40, "W:Type %d (%s)\n",
193 playerp->p_type, descrtype(playerp, FALSE) + 1);
194 mvprintw(10, 40, "X:Special Type %d\n", playerp->p_specialtype);
195 mvprintw(11, 40, "Y:Lives %d\n", playerp->p_lives);
196 mvprintw(12, 40, "Z:Crowns %d\n", playerp->p_crowns);
197 mvprintw(13, 40, "0:Charms %d\n", playerp->p_charms);
198 mvprintw(14, 40, "1:Amulets %d\n", playerp->p_amulets);
199 mvprintw(15, 40, "2:Holy Water %d\n", playerp->p_holywater);
201 temp = today - playerp->p_lastused;
205 mvprintw(16, 40, "3:Lastused %d (%d)\n", playerp->p_lastused, temp);
207 mvprintw(18, 8, "4:Palantir %c 5:Blessing %c 6:Virgin %c 7:Blind %c",
208 flag[playerp->p_palantir],
209 flag[playerp->p_blessing],
210 flag[playerp->p_virgin],
211 flag[playerp->p_blindness]);
214 mvprintw(19, 8, "8:Ring %c",
215 flag[playerp->p_ring.ring_type != R_NONE]);
217 mvprintw(19, 8, "8:Ring %d 9:Duration %d",
218 playerp->p_ring.ring_type, playerp->p_ring.ring_duration);
222 && (ingameflag || strcmp(Login, playerp->p_login) != 0))
223 /* in game or not examining own character */
236 mvaddstr(20, 0, "!:Quit ?:Delete");
237 mvaddstr(21, 0, "What would you like to change ? ");
240 c = getanswer(" ", TRUE);
242 /* examining own player; allow to change name and password */
243 c = getanswer("!BA", FALSE);
247 case 'A': /* change name */
248 case 'B': /* change password */
250 /* prompt for password */
252 mvaddstr(23, 0, "Password ? ");
254 getstring(Databuf, 9);
256 if (strcmp(Databuf, playerp->p_password) != 0)
263 mvaddstr(23, 0, "New name: ");
264 getstring(Databuf, SZ_NAME);
265 truncstring(Databuf);
266 if (Databuf[0] != '\0')
267 if (Wizard || findname(Databuf, &Other) < 0L)
268 strcpy(playerp->p_name, Databuf);
271 /* get new password */
277 /* get two copies of new password until they match */
280 mvaddstr(23, 0, "New password ? ");
281 getstring(Databuf, SZ_PASSWORD);
282 if (Databuf[0] == '\0')
285 /* get second copy */
286 mvaddstr(23, 0, "One more time ? ");
287 getstring(playerp->p_password, SZ_PASSWORD);
289 while (strcmp(playerp->p_password, Databuf) != 0);
296 case 'C': /* change experience */
297 prompt = "experience";
298 dptr = &playerp->p_experience;
301 case 'D': /* change level */
303 dptr = &playerp->p_level;
306 case 'E': /* change strength */
308 dptr = &playerp->p_strength;
311 case 'F': /* change swords */
313 dptr = &playerp->p_sword;
316 case 'G': /* change energy */
318 dptr = &playerp->p_energy;
321 case 'H': /* change maximum energy */
322 prompt = "max energy";
323 dptr = &playerp->p_maxenergy;
326 case 'I': /* change shields */
328 dptr = &playerp->p_shield;
331 case 'J': /* change quickness */
332 prompt = "quickness";
333 dptr = &playerp->p_quickness;
336 case 'K': /* change quicksilver */
337 prompt = "quicksilver";
338 dptr = &playerp->p_quksilver;
341 case 'L': /* change magic */
342 prompt = "magic level";
343 dptr = &playerp->p_magiclvl;
346 case 'M': /* change mana */
348 dptr = &playerp->p_mana;
351 case 'N': /* change brains */
353 dptr = &playerp->p_brains;
356 case 'O': /* change poison */
358 dptr = &playerp->p_poison;
361 case 'P': /* change gold */
363 dptr = &playerp->p_gold;
366 case 'Q': /* change gems */
368 dptr = &playerp->p_gems;
371 case 'R': /* change sin */
373 dptr = &playerp->p_sin;
376 case 'S': /* change x coord */
378 dptr = &playerp->p_x;
381 case 'T': /* change y coord */
383 dptr = &playerp->p_y;
386 case 'U': /* change age */
387 mvprintw(23, 0, "age = %ld; age = ", playerp->p_age);
390 playerp->p_age = (long) dtemp;
393 case 'V': /* change degen */
394 mvprintw(23, 0, "degen = %d; degen = ", playerp->p_degenerated);
397 playerp->p_degenerated = (int) dtemp;
400 case 'W': /* change type */
402 sptr = &playerp->p_type;
405 case 'X': /* change special type */
406 prompt = "special type";
407 sptr = &playerp->p_specialtype;
410 case 'Y': /* change lives */
412 sptr = &playerp->p_lives;
415 case 'Z': /* change crowns */
417 sptr = &playerp->p_crowns;
420 case '0': /* change charms */
422 sptr = &playerp->p_charms;
425 case '1': /* change amulet */
427 sptr = &playerp->p_amulets;
430 case '2': /* change holy water */
431 prompt = "holy water";
432 sptr = &playerp->p_holywater;
435 case '3': /* change last-used */
436 prompt = "last-used";
437 sptr = &playerp->p_lastused;
440 case '4': /* change palantir */
442 bptr = &playerp->p_palantir;
445 case '5': /* change blessing */
447 bptr = &playerp->p_blessing;
450 case '6': /* change virgin */
452 bptr = &playerp->p_virgin;
455 case '7': /* change blindness */
456 prompt = "blindness";
457 bptr = &playerp->p_blindness;
460 case '8': /* change ring type */
461 prompt = "ring-type";
462 sptr = &playerp->p_ring.ring_type;
465 case '9': /* change ring duration */
466 prompt = "ring-duration";
467 sptr = &playerp->p_ring.ring_duration;
470 case '!': /* quit, update */
472 (!ingameflag || playerp != &Player))
473 /* turn off status if not modifying self */
475 playerp->p_status = S_OFF;
476 playerp->p_tampered = T_OFF;
479 writerecord(playerp, loc);
483 case '?': /* delete player */
484 if (ingameflag && playerp == &Player)
485 /* cannot delete self */
488 freerecord(playerp, loc);
496 mvprintw(23, 0, "%s = %f; %s = ", prompt, *dptr, prompt);
503 mvprintw(23, 0, "%s = %d; %s = ", prompt, *sptr, prompt);
506 *sptr = (short) dtemp;
510 mvprintw(23, 0, "%s = %c; %s = ", prompt, flag[*bptr], prompt);
511 c = getanswer("\nTF", TRUE);
520 /************************************************************************
522 / FUNCTION NAME: monstlist()
524 / FUNCTION: print a monster listing
526 / AUTHOR: E. A. Estes, 2/27/86
532 / MODULES CALLED: puts(), fread(), fseek(), printf()
534 / GLOBAL INPUTS: Curmonster, *Monstfp
536 / GLOBAL OUTPUTS: none
539 / Read monster file, and print a monster listing on standard output.
541 *************************************************************************/
546 int count = 0; /* count in file */
548 puts(" #) Name Str Brain Quick Energy Exper Treas Type Flock%\n");
549 fseek(Monstfp, 0L, 0);
550 while (fread((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp) == 1)
551 printf("%2d) %-20.20s%4.0f %4.0f %2.0f %5.0f %5.0f %2d %2d %3.0f\n", count++,
552 Curmonster.m_name, Curmonster.m_strength, Curmonster.m_brains,
553 Curmonster.m_speed, Curmonster.m_energy, Curmonster.m_experience,
554 Curmonster.m_treasuretype, Curmonster.m_type, Curmonster.m_flock);
557 /************************************************************************
559 / FUNCTION NAME: scorelist()
561 / FUNCTION: print player score board
563 / AUTHOR: E. A. Estes, 12/4/85
569 / MODULES CALLED: fread(), fopen(), printf(), fclose()
573 / GLOBAL OUTPUTS: none
576 / Read the scoreboard file and print the contents.
578 *************************************************************************/
583 struct scoreboard sbuf; /* for reading entries */
584 FILE *fp; /* to open the file */
586 if ((fp = fopen(_PATH_SCORE, "r")) != NULL)
588 while (fread((char *) &sbuf, SZ_SCORESTRUCT, 1, fp) == 1)
589 printf("%-20s (%-9s) Level: %6.0f Type: %s\n",
590 sbuf.sb_name, sbuf.sb_login, sbuf.sb_level, sbuf.sb_type);
595 /************************************************************************
597 / FUNCTION NAME: activelist()
599 / FUNCTION: print list of active players to standard output
601 / AUTHOR: E. A. Estes, 3/7/86
607 / MODULES CALLED: descrstatus(), fread(), fseek(), printf(), descrtype()
609 / GLOBAL INPUTS: Other, *Playersfp
611 / GLOBAL OUTPUTS: none
614 / Read player file, and print list of active records to standard output.
616 *************************************************************************/
621 fseek(Playersfp, 0L, 0);
622 printf("Current characters on file are:\n\n");
624 while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
625 if (Other.p_status != S_NOTUSED)
626 printf("%-20s (%-9s) Level: %6.0f %s (%s)\n",
627 Other.p_name, Other.p_login, Other.p_level,
628 descrtype(&Other, FALSE), descrstatus(&Other));
632 /************************************************************************
634 / FUNCTION NAME: purgeoldplayers()
636 / FUNCTION: purge inactive players from player file
638 / AUTHOR: E. A. Estes, 12/4/85
644 / MODULES CALLED: freerecord(), time(), fread(), fseek(), localtime()
646 / GLOBAL INPUTS: Other, *Playersfp
648 / GLOBAL OUTPUTS: none
651 / Delete characters which have not been used with the last
654 *************************************************************************/
657 purgeoldplayers(void)
659 int today; /* day of year for today */
660 int daysold; /* how many days since the character has been used */
661 time_t ltime; /* time in seconds */
662 long loc = 0L; /* location in file */
665 today = localtime(<ime)->tm_yday;
669 fseek(Playersfp, loc, 0);
670 if (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) != 1)
673 daysold = today - Other.p_lastused;
677 if (daysold > N_DAYSOLD)
678 /* player hasn't been used in a while; delete */
679 freerecord(&Other, loc);
681 loc += SZ_PLAYERSTRUCT;
685 /************************************************************************
687 / FUNCTION NAME: enterscore()
689 / FUNCTION: enter player into scoreboard
691 / AUTHOR: E. A. Estes, 12/4/85
697 / MODULES CALLED: fread(), fseek(), fopen(), error(), strcmp(), fclose(),
698 / strcpy(), fwrite(), descrtype()
700 / GLOBAL INPUTS: Player
702 / GLOBAL OUTPUTS: none
705 / The scoreboard keeps track of the highest character on a
707 / Search the scoreboard for an entry for the current login,
708 / if an entry is found, and it is lower than the current player,
709 / replace it, otherwise create an entry.
711 *************************************************************************/
716 struct scoreboard sbuf; /* buffer to read in scoreboard entries */
717 FILE *fp; /* to open scoreboard file */
718 long loc = 0L; /* location in scoreboard file */
719 bool found = FALSE; /* set if we found an entry for this login */
721 if ((fp = fopen(_PATH_SCORE, "r+")) != NULL)
723 while (fread((char *) &sbuf, SZ_SCORESTRUCT, 1, fp) == 1)
724 if (strcmp(Player.p_login, sbuf.sb_login) == 0)
730 loc += SZ_SCORESTRUCT;
739 * At this point, 'loc' will either indicate a point beyond
740 * the end of file, or the place where the previous entry
744 if ((!found) || Player.p_level > sbuf.sb_level)
745 /* put new entry in for this login */
747 strcpy(sbuf.sb_login, Player.p_login);
748 strcpy(sbuf.sb_name, Player.p_name);
749 sbuf.sb_level = Player.p_level;
750 strcpy(sbuf.sb_type, descrtype(&Player, TRUE));
755 fwrite((char *) &sbuf, SZ_SCORESTRUCT, 1, fp);