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.2 2003/06/17 04:25:24 dillon Exp $
11 /************************************************************************
13 / FUNCTION NAME: changestats()
15 / FUNCTION: examine/change statistics for a player
17 / AUTHOR: E. A. Estes, 12/4/85
20 / bool ingameflag - set if called while playing game (Wizard only)
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()
29 / GLOBAL INPUTS: LINES, *Login, Other, Wizard, Player, *stdscr, Databuf[],
32 / GLOBAL OUTPUTS: Echo
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.
43 / Players may alter their names and passwords, if the following
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)
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
55 *************************************************************************/
57 changestats(ingameflag)
60 static char flag[2] = /* for printing values of bools */
62 struct player *playerp;/* pointer to structure to alter */
63 char *prompt; /* pointer to prompt string */
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 */
77 /* get name of player to examine/alter */
79 mvaddstr(5, 0, "Which character do you want to look at ? ");
80 getstring(Databuf, SZ_DATABUF);
83 if (Databuf[0] == '\0')
92 /* use 'Player' structure */
94 else if (strcmp(Databuf, Player.p_name) == 0)
95 /* alter/examine current player */
101 /* use 'Other' structure */
104 /* find player on file */
105 if (loc < 0L && (loc = findname(Databuf, playerp)) < 0L)
106 /* didn't find player */
109 mvaddstr(11, 0, "Not found.");
114 today = localtime(&now)->tm_yday;
119 /* print player structure, and prompt for action */
121 mvprintw(0, 0,"A:Name %s\n", playerp->p_name);
124 printw("B:Password %s\n", playerp->p_password);
126 addstr("B:Password XXXXXXXX\n");
128 printw(" :Login %s\n", playerp->p_login);
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);
145 if (Wizard || playerp->p_specialtype != SC_VALAR)
146 mvaddstr(0, 40, descrstatus(playerp));
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);
154 mvprintw(5, 40, "S:X-coord %.0f\n", playerp->p_x);
155 mvprintw(6, 40, "T:Y-coord %.0f\n", playerp->p_y);
159 mvaddstr(5, 40, "S:X-coord ?\n");
160 mvaddstr(6, 40, "T:Y-coord ?\n");
163 mvprintw(7, 40, "U:Age %ld\n", playerp->p_age);
164 mvprintw(8, 40, "V:Degenerated %d\n", playerp->p_degenerated);
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);
175 temp = today - playerp->p_lastused;
179 mvprintw(16, 40, "3:Lastused %d (%d)\n", playerp->p_lastused, temp);
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]);
188 mvprintw(19, 8, "8:Ring %c",
189 flag[playerp->p_ring.ring_type != R_NONE]);
191 mvprintw(19, 8, "8:Ring %d 9:Duration %d",
192 playerp->p_ring.ring_type, playerp->p_ring.ring_duration);
196 && (ingameflag || strcmp(Login, playerp->p_login) != 0))
197 /* in game or not examining own character */
210 mvaddstr(20, 0, "!:Quit ?:Delete");
211 mvaddstr(21, 0, "What would you like to change ? ");
214 c = getanswer(" ", TRUE);
216 /* examining own player; allow to change name and password */
217 c = getanswer("!BA", FALSE);
221 case 'A': /* change name */
222 case 'B': /* change password */
224 /* prompt for password */
226 mvaddstr(23, 0, "Password ? ");
228 getstring(Databuf, 9);
230 if (strcmp(Databuf, playerp->p_password) != 0)
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);
245 /* get new password */
251 /* get two copies of new password until they match */
254 mvaddstr(23, 0, "New password ? ");
255 getstring(Databuf, SZ_PASSWORD);
256 if (Databuf[0] == '\0')
259 /* get second copy */
260 mvaddstr(23, 0, "One more time ? ");
261 getstring(playerp->p_password, SZ_PASSWORD);
263 while (strcmp(playerp->p_password, Databuf) != 0);
270 case 'C': /* change experience */
271 prompt = "experience";
272 dptr = &playerp->p_experience;
275 case 'D': /* change level */
277 dptr = &playerp->p_level;
280 case 'E': /* change strength */
282 dptr = &playerp->p_strength;
285 case 'F': /* change swords */
287 dptr = &playerp->p_sword;
290 case 'G': /* change energy */
292 dptr = &playerp->p_energy;
295 case 'H': /* change maximum energy */
296 prompt = "max energy";
297 dptr = &playerp->p_maxenergy;
300 case 'I': /* change shields */
302 dptr = &playerp->p_shield;
305 case 'J': /* change quickness */
306 prompt = "quickness";
307 dptr = &playerp->p_quickness;
310 case 'K': /* change quicksilver */
311 prompt = "quicksilver";
312 dptr = &playerp->p_quksilver;
315 case 'L': /* change magic */
316 prompt = "magic level";
317 dptr = &playerp->p_magiclvl;
320 case 'M': /* change mana */
322 dptr = &playerp->p_mana;
325 case 'N': /* change brains */
327 dptr = &playerp->p_brains;
330 case 'O': /* change poison */
332 dptr = &playerp->p_poison;
335 case 'P': /* change gold */
337 dptr = &playerp->p_gold;
340 case 'Q': /* change gems */
342 dptr = &playerp->p_gems;
345 case 'R': /* change sin */
347 dptr = &playerp->p_sin;
350 case 'S': /* change x coord */
352 dptr = &playerp->p_x;
355 case 'T': /* change y coord */
357 dptr = &playerp->p_y;
360 case 'U': /* change age */
361 mvprintw(23, 0, "age = %ld; age = ", playerp->p_age);
364 playerp->p_age = (long) dtemp;
367 case 'V': /* change degen */
368 mvprintw(23, 0, "degen = %d; degen = ", playerp->p_degenerated);
371 playerp->p_degenerated = (int) dtemp;
374 case 'W': /* change type */
376 sptr = &playerp->p_type;
379 case 'X': /* change special type */
380 prompt = "special type";
381 sptr = &playerp->p_specialtype;
384 case 'Y': /* change lives */
386 sptr = &playerp->p_lives;
389 case 'Z': /* change crowns */
391 sptr = &playerp->p_crowns;
394 case '0': /* change charms */
396 sptr = &playerp->p_charms;
399 case '1': /* change amulet */
401 sptr = &playerp->p_amulets;
404 case '2': /* change holy water */
405 prompt = "holy water";
406 sptr = &playerp->p_holywater;
409 case '3': /* change last-used */
410 prompt = "last-used";
411 sptr = &playerp->p_lastused;
414 case '4': /* change palantir */
416 bptr = &playerp->p_palantir;
419 case '5': /* change blessing */
421 bptr = &playerp->p_blessing;
424 case '6': /* change virgin */
426 bptr = &playerp->p_virgin;
429 case '7': /* change blindness */
430 prompt = "blindness";
431 bptr = &playerp->p_blindness;
434 case '8': /* change ring type */
435 prompt = "ring-type";
436 sptr = &playerp->p_ring.ring_type;
439 case '9': /* change ring duration */
440 prompt = "ring-duration";
441 sptr = &playerp->p_ring.ring_duration;
444 case '!': /* quit, update */
446 (!ingameflag || playerp != &Player))
447 /* turn off status if not modifying self */
449 playerp->p_status = S_OFF;
450 playerp->p_tampered = T_OFF;
453 writerecord(playerp, loc);
457 case '?': /* delete player */
458 if (ingameflag && playerp == &Player)
459 /* cannot delete self */
462 freerecord(playerp, loc);
470 mvprintw(23, 0, "%s = %f; %s = ", prompt, *dptr, prompt);
477 mvprintw(23, 0, "%s = %d; %s = ", prompt, *sptr, prompt);
480 *sptr = (short) dtemp;
484 mvprintw(23, 0, "%s = %c; %s = ", prompt, flag[*bptr], prompt);
485 c = getanswer("\nTF", TRUE);
494 /************************************************************************
496 / FUNCTION NAME: monstlist()
498 / FUNCTION: print a monster listing
500 / AUTHOR: E. A. Estes, 2/27/86
506 / MODULES CALLED: puts(), fread(), fseek(), printf()
508 / GLOBAL INPUTS: Curmonster, *Monstfp
510 / GLOBAL OUTPUTS: none
513 / Read monster file, and print a monster listing on standard output.
515 *************************************************************************/
519 int count = 0; /* count in file */
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);
530 /************************************************************************
532 / FUNCTION NAME: scorelist()
534 / FUNCTION: print player score board
536 / AUTHOR: E. A. Estes, 12/4/85
542 / MODULES CALLED: fread(), fopen(), printf(), fclose()
546 / GLOBAL OUTPUTS: none
549 / Read the scoreboard file and print the contents.
551 *************************************************************************/
555 struct scoreboard sbuf; /* for reading entries */
556 FILE *fp; /* to open the file */
558 if ((fp = fopen(_PATH_SCORE, "r")) != NULL)
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);
567 /************************************************************************
569 / FUNCTION NAME: activelist()
571 / FUNCTION: print list of active players to standard output
573 / AUTHOR: E. A. Estes, 3/7/86
579 / MODULES CALLED: descrstatus(), fread(), fseek(), printf(), descrtype()
581 / GLOBAL INPUTS: Other, *Playersfp
583 / GLOBAL OUTPUTS: none
586 / Read player file, and print list of active records to standard output.
588 *************************************************************************/
592 fseek(Playersfp, 0L, 0);
593 printf("Current characters on file are:\n\n");
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));
603 /************************************************************************
605 / FUNCTION NAME: purgeoldplayers()
607 / FUNCTION: purge inactive players from player file
609 / AUTHOR: E. A. Estes, 12/4/85
615 / MODULES CALLED: freerecord(), time(), fread(), fseek(), localtime()
617 / GLOBAL INPUTS: Other, *Playersfp
619 / GLOBAL OUTPUTS: none
622 / Delete characters which have not been used with the last
625 *************************************************************************/
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 */
635 today = localtime(<ime)->tm_yday;
639 fseek(Playersfp, loc, 0);
640 if (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) != 1)
643 daysold = today - Other.p_lastused;
647 if (daysold > N_DAYSOLD)
648 /* player hasn't been used in a while; delete */
649 freerecord(&Other, loc);
651 loc += SZ_PLAYERSTRUCT;
655 /************************************************************************
657 / FUNCTION NAME: enterscore()
659 / FUNCTION: enter player into scoreboard
661 / AUTHOR: E. A. Estes, 12/4/85
667 / MODULES CALLED: fread(), fseek(), fopen(), error(), strcmp(), fclose(),
668 / strcpy(), fwrite(), descrtype()
670 / GLOBAL INPUTS: Player
672 / GLOBAL OUTPUTS: none
675 / The scoreboard keeps track of the highest character on a
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.
681 *************************************************************************/
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 */
690 if ((fp = fopen(_PATH_SCORE, "r+")) != NULL)
692 while (fread((char *) &sbuf, SZ_SCORESTRUCT, 1, fp) == 1)
693 if (strcmp(Player.p_login, sbuf.sb_login) == 0)
699 loc += SZ_SCORESTRUCT;
708 * At this point, 'loc' will either indicate a point beyond
709 * the end of file, or the place where the previous entry
713 if ((!found) || Player.p_level > sbuf.sb_level)
714 /* put new entry in for this login */
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));
724 fwrite((char *) &sbuf, SZ_SCORESTRUCT, 1, fp);