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 activelist(void);
31 void changestats(bool);
32 void enterscore(void);
34 void purgeoldplayers(void);
38 * FUNCTION: examine/change statistics for a player
41 * bool ingameflag - set if called while playing game (Wizard only)
43 * GLOBAL INPUTS: LINES, *Login, Other, Wizard, Player, *stdscr, Databuf[],
46 * GLOBAL OUTPUTS: Echo
49 * Prompt for player name to examine/change.
50 * If the name is NULL, print a list of all players.
51 * If we are called from within the game, check for the
52 * desired name being the same as the current player's name.
53 * Only the 'Wizard' may alter players.
54 * Items are changed only if a non-zero value is specified.
55 * To change an item to 0, use 0.1; it will be truncated later.
57 * Players may alter their names and passwords, if the following
59 * - current login matches the character's logins
60 * - the password is known
61 * - the player is not in the middle of the game (ingameflag == FALSE)
63 * The last condition is imposed for two reasons:
64 * - the game could possibly get a bit hectic if a player were
65 * continually changing his/her name
66 * - another player structure would be necessary to check for names
71 changestats(bool ingameflag)
73 static char flag[2] = /* for printing values of bools */
75 struct player *playerp; /* pointer to structure to alter */
76 const char *prompt; /* pointer to prompt string */
78 int today; /* day of year of today */
79 int temp; /* temporary variable */
80 long loc; /* location in player file */
81 time_t now; /* time now */
82 double dtemp; /* temporary variable */
83 bool *bptr; /* pointer to bool item to change */
84 double *dptr; /* pointer to double item to change */
85 short *sptr; /* pointer to short item to change */
90 /* get name of player to examine/alter */
91 mvaddstr(5, 0, "Which character do you want to look at ? ");
92 getstring(Databuf, SZ_DATABUF);
95 if (Databuf[0] == '\0')
104 /* use 'Player' structure */
106 else if (strcmp(Databuf, Player.p_name) == 0) {
107 /* alter/examine current player */
111 /* use 'Other' structure */
114 /* find player on file */
115 if (loc < 0L && (loc = findname(Databuf, playerp)) < 0L) {
116 /* didn't find player */
118 mvaddstr(11, 0, "Not found.");
123 today = localtime(&now)->tm_yday;
128 /* print player structure, and prompt for action */
129 mvprintw(0, 0, "A:Name %s\n", playerp->p_name);
132 printw("B:Password %s\n", playerp->p_password);
134 addstr("B:Password XXXXXXXX\n");
136 printw(" :Login %s\n", playerp->p_login);
138 printw("C:Experience %.0f\n", playerp->p_experience);
139 printw("D:Level %.0f\n", playerp->p_level);
140 printw("E:Strength %.0f\n", playerp->p_strength);
141 printw("F:Sword %.0f\n", playerp->p_sword);
142 printw(" :Might %.0f\n", playerp->p_might);
143 printw("G:Energy %.0f\n", playerp->p_energy);
144 printw("H:Max-Energy %.0f\n", playerp->p_maxenergy);
145 printw("I:Shield %.0f\n", playerp->p_shield);
146 printw("J:Quickness %.0f\n", playerp->p_quickness);
147 printw("K:Quicksilver %.0f\n", playerp->p_quksilver);
148 printw(" :Speed %.0f\n", playerp->p_speed);
149 printw("L:Magic Level %.0f\n", playerp->p_magiclvl);
150 printw("M:Mana %.0f\n", playerp->p_mana);
151 printw("N:Brains %.0f\n", playerp->p_brains);
153 if (Wizard || playerp->p_specialtype != SC_VALAR)
154 mvaddstr(0, 40, descrstatus(playerp));
156 mvprintw(1, 40, "O:Poison %0.3f\n", playerp->p_poison);
157 mvprintw(2, 40, "P:Gold %.0f\n", playerp->p_gold);
158 mvprintw(3, 40, "Q:Gem %.0f\n", playerp->p_gems);
159 mvprintw(4, 40, "R:Sin %0.3f\n", playerp->p_sin);
161 mvprintw(5, 40, "S:X-coord %.0f\n", playerp->p_x);
162 mvprintw(6, 40, "T:Y-coord %.0f\n", playerp->p_y);
164 mvaddstr(5, 40, "S:X-coord ?\n");
165 mvaddstr(6, 40, "T:Y-coord ?\n");
168 mvprintw(7, 40, "U:Age %ld\n", playerp->p_age);
169 mvprintw(8, 40, "V:Degenerated %d\n", playerp->p_degenerated);
171 mvprintw(9, 40, "W:Type %d (%s)\n",
172 playerp->p_type, descrtype(playerp, FALSE) + 1);
173 mvprintw(10, 40, "X:Special Type %d\n", playerp->p_specialtype);
174 mvprintw(11, 40, "Y:Lives %d\n", playerp->p_lives);
175 mvprintw(12, 40, "Z:Crowns %d\n", playerp->p_crowns);
176 mvprintw(13, 40, "0:Charms %d\n", playerp->p_charms);
177 mvprintw(14, 40, "1:Amulets %d\n", playerp->p_amulets);
178 mvprintw(15, 40, "2:Holy Water %d\n", playerp->p_holywater);
180 temp = today - playerp->p_lastused;
184 mvprintw(16, 40, "3:Lastused %d (%d)\n", playerp->p_lastused, temp);
186 mvprintw(18, 8, "4:Palantir %c 5:Blessing %c 6:Virgin %c 7:Blind %c",
187 flag[playerp->p_palantir],
188 flag[playerp->p_blessing],
189 flag[playerp->p_virgin],
190 flag[playerp->p_blindness]);
193 mvprintw(19, 8, "8:Ring %c",
194 flag[playerp->p_ring.ring_type != R_NONE]);
196 mvprintw(19, 8, "8:Ring %d 9:Duration %d",
197 playerp->p_ring.ring_type, playerp->p_ring.ring_duration);
200 && (ingameflag || strcmp(Login, playerp->p_login) != 0)) {
201 /* in game or not examining own character */
211 mvaddstr(20, 0, "!:Quit ?:Delete");
212 mvaddstr(21, 0, "What would you like to change ? ");
215 c = getanswer(" ", TRUE);
217 /* examining own player; allow to change name and password */
218 c = getanswer("!BA", FALSE);
221 case 'A': /* change name */
222 case 'B': /* change password */
224 /* prompt for password */
225 mvaddstr(23, 0, "Password ? ");
227 getstring(Databuf, 9);
229 if (strcmp(Databuf, playerp->p_password) != 0)
234 mvaddstr(23, 0, "New name: ");
235 getstring(Databuf, SZ_NAME);
236 truncstring(Databuf);
237 if (Databuf[0] != '\0')
238 if (Wizard || findname(Databuf, &Other) < 0L)
239 strcpy(playerp->p_name, Databuf);
241 /* get new password */
246 /* get two copies of new password until they match */
248 mvaddstr(23, 0, "New password ? ");
249 getstring(Databuf, SZ_PASSWORD);
250 if (Databuf[0] == '\0')
253 /* get second copy */
254 mvaddstr(23, 0, "One more time ? ");
255 getstring(playerp->p_password, SZ_PASSWORD);
256 } while (strcmp(playerp->p_password, Databuf) != 0);
263 case 'C': /* change experience */
264 prompt = "experience";
265 dptr = &playerp->p_experience;
268 case 'D': /* change level */
270 dptr = &playerp->p_level;
273 case 'E': /* change strength */
275 dptr = &playerp->p_strength;
278 case 'F': /* change swords */
280 dptr = &playerp->p_sword;
283 case 'G': /* change energy */
285 dptr = &playerp->p_energy;
288 case 'H': /* change maximum energy */
289 prompt = "max energy";
290 dptr = &playerp->p_maxenergy;
293 case 'I': /* change shields */
295 dptr = &playerp->p_shield;
298 case 'J': /* change quickness */
299 prompt = "quickness";
300 dptr = &playerp->p_quickness;
303 case 'K': /* change quicksilver */
304 prompt = "quicksilver";
305 dptr = &playerp->p_quksilver;
308 case 'L': /* change magic */
309 prompt = "magic level";
310 dptr = &playerp->p_magiclvl;
313 case 'M': /* change mana */
315 dptr = &playerp->p_mana;
318 case 'N': /* change brains */
320 dptr = &playerp->p_brains;
323 case 'O': /* change poison */
325 dptr = &playerp->p_poison;
328 case 'P': /* change gold */
330 dptr = &playerp->p_gold;
333 case 'Q': /* change gems */
335 dptr = &playerp->p_gems;
338 case 'R': /* change sin */
340 dptr = &playerp->p_sin;
343 case 'S': /* change x coord */
345 dptr = &playerp->p_x;
348 case 'T': /* change y coord */
350 dptr = &playerp->p_y;
353 case 'U': /* change age */
354 mvprintw(23, 0, "age = %ld; age = ", playerp->p_age);
357 playerp->p_age = (long)dtemp;
360 case 'V': /* change degen */
361 mvprintw(23, 0, "degen = %d; degen = ", playerp->p_degenerated);
364 playerp->p_degenerated = (int)dtemp;
367 case 'W': /* change type */
369 sptr = &playerp->p_type;
372 case 'X': /* change special type */
373 prompt = "special type";
374 sptr = &playerp->p_specialtype;
377 case 'Y': /* change lives */
379 sptr = &playerp->p_lives;
382 case 'Z': /* change crowns */
384 sptr = &playerp->p_crowns;
387 case '0': /* change charms */
389 sptr = &playerp->p_charms;
392 case '1': /* change amulet */
394 sptr = &playerp->p_amulets;
397 case '2': /* change holy water */
398 prompt = "holy water";
399 sptr = &playerp->p_holywater;
402 case '3': /* change last-used */
403 prompt = "last-used";
404 sptr = &playerp->p_lastused;
407 case '4': /* change palantir */
409 bptr = &playerp->p_palantir;
412 case '5': /* change blessing */
414 bptr = &playerp->p_blessing;
417 case '6': /* change virgin */
419 bptr = &playerp->p_virgin;
422 case '7': /* change blindness */
423 prompt = "blindness";
424 bptr = &playerp->p_blindness;
427 case '8': /* change ring type */
428 prompt = "ring-type";
429 sptr = &playerp->p_ring.ring_type;
432 case '9': /* change ring duration */
433 prompt = "ring-duration";
434 sptr = &playerp->p_ring.ring_duration;
437 case '!': /* quit, update */
439 (!ingameflag || playerp != &Player)) {
440 /* turn off status if not modifying self */
441 playerp->p_status = S_OFF;
442 playerp->p_tampered = T_OFF;
445 writerecord(playerp, loc);
449 case '?': /* delete player */
450 if (ingameflag && playerp == &Player)
451 /* cannot delete self */
454 freerecord(playerp, loc);
462 mvprintw(23, 0, "%s = %f; %s = ", prompt, *dptr, prompt);
469 mvprintw(23, 0, "%s = %d; %s = ", prompt, *sptr, prompt);
472 *sptr = (short)dtemp;
476 mvprintw(23, 0, "%s = %c; %s = ", prompt, flag[*bptr],
478 c = getanswer("\nTF", TRUE);
488 * FUNCTION: print a monster listing
490 * GLOBAL INPUTS: Curmonster, *Monstfp
493 * Read monster file, and print a monster listing on standard output.
499 int count = 0; /* count in file */
501 puts(" #) Name Str Brain Quick Energy Exper Treas Type Flock%\n");
502 fseek(Monstfp, 0L, SEEK_SET);
503 while (fread((char *)&Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp) == 1)
504 printf("%2d) %-20.20s%4.0f %4.0f %2.0f %5.0f %5.0f %2d %2d %3.0f\n", count++,
505 Curmonster.m_name, Curmonster.m_strength, Curmonster.m_brains,
506 Curmonster.m_speed, Curmonster.m_energy, Curmonster.m_experience,
507 Curmonster.m_treasuretype, Curmonster.m_type, Curmonster.m_flock);
511 * FUNCTION: print player score board
514 * Read the scoreboard file and print the contents.
520 struct scoreboard sbuf; /* for reading entries */
521 FILE *fp; /* to open the file */
523 if ((fp = fopen(_PATH_SCORE, "r")) != NULL) {
524 while (fread((char *)&sbuf, SZ_SCORESTRUCT, 1, fp) == 1)
525 printf("%-20s (%-9s) Level: %6.0f Type: %s\n",
526 sbuf.sb_name, sbuf.sb_login, sbuf.sb_level, sbuf.sb_type);
532 * FUNCTION: print list of active players to standard output
534 * GLOBAL INPUTS: Other, *Playersfp
537 * Read player file, and print list of active records to standard output.
543 fseek(Playersfp, 0L, SEEK_SET);
544 printf("Current characters on file are:\n\n");
546 while (fread((char *)&Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
547 if (Other.p_status != S_NOTUSED)
548 printf("%-20s (%-9s) Level: %6.0f %s (%s)\n",
549 Other.p_name, Other.p_login, Other.p_level,
550 descrtype(&Other, FALSE), descrstatus(&Other));
554 * FUNCTION: purge inactive players from player file
556 * GLOBAL INPUTS: Other, *Playersfp
559 * Delete characters which have not been used with the last
564 purgeoldplayers(void)
566 int today; /* day of year for today */
567 int daysold; /* how many days since the character has been used */
568 time_t ltime; /* time in seconds */
569 long loc = 0L; /* location in file */
572 today = localtime(<ime)->tm_yday;
575 fseek(Playersfp, loc, SEEK_SET);
576 if (fread((char *)&Other, SZ_PLAYERSTRUCT, 1, Playersfp) != 1)
579 daysold = today - Other.p_lastused;
583 if (daysold > N_DAYSOLD)
584 /* player hasn't been used in a while; delete */
585 freerecord(&Other, loc);
587 loc += SZ_PLAYERSTRUCT;
592 * FUNCTION: enter player into scoreboard
594 * GLOBAL INPUTS: Player
597 * The scoreboard keeps track of the highest character on a
599 * Search the scoreboard for an entry for the current login,
600 * if an entry is found, and it is lower than the current player,
601 * replace it, otherwise create an entry.
607 struct scoreboard sbuf; /* buffer to read in scoreboard entries */
608 FILE *fp; /* to open scoreboard file */
609 long loc = 0L; /* location in scoreboard file */
610 bool found = FALSE; /* set if we found an entry for this login */
612 if ((fp = fopen(_PATH_SCORE, "r+")) != NULL) {
613 while (fread((char *)&sbuf, SZ_SCORESTRUCT, 1, fp) == 1)
614 if (strcmp(Player.p_login, sbuf.sb_login) == 0) {
618 loc += SZ_SCORESTRUCT;
625 * At this point, 'loc' will either indicate a point beyond
626 * the end of file, or the place where the previous entry
630 if ((!found) || Player.p_level > sbuf.sb_level) {
631 /* put new entry in for this login */
632 strcpy(sbuf.sb_login, Player.p_login);
633 strcpy(sbuf.sb_name, Player.p_name);
634 sbuf.sb_level = Player.p_level;
635 strcpy(sbuf.sb_type, descrtype(&Player, TRUE));
639 fseek(fp, loc, SEEK_SET);
640 fwrite((char *)&sbuf, SZ_SCORESTRUCT, 1, fp);