Paging and swapping system fixes.
[dragonfly.git] / games / phantasia / gamesupport.c
blob43697db8a40804a3c65416e253ad9a967e176c26
1 /*
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 $
6 */
8 #include <string.h>
9 #include "include.h"
11 /* functions which we need to know about */
12 /* interplayer.c */
13 extern void userlist(bool);
14 /* io.c */
15 extern int getanswer(const char *, bool);
16 extern void getstring(char *, int);
17 extern double infloat(void);
18 extern void more(int);
19 /* main.c */
20 extern void cleanup(bool);
21 /* misc.c */
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);
31 void monstlist(void);
32 void scorelist(void);
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
45 / ARGUMENTS:
46 / bool ingameflag - set if called while playing game (Wizard only)
48 / RETURN VALUE: none
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[],
56 / Fileloc
58 / GLOBAL OUTPUTS: Echo
60 / DESCRIPTION:
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
70 / are true:
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
79 / already in use
81 *************************************************************************/
83 void
84 changestats(bool ingameflag)
86 static char flag[2] = /* for printing values of bools */
87 {'F', 'T'};
88 struct player *playerp;/* pointer to structure to alter */
89 const char *prompt; /* pointer to prompt string */
90 int c; /* input */
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 */
100 clear();
102 for (;;)
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);
111 else
112 break;
115 loc = -1L;
117 if (!ingameflag)
118 /* use 'Player' structure */
119 playerp = &Player;
120 else if (strcmp(Databuf, Player.p_name) == 0)
121 /* alter/examine current player */
123 playerp = &Player;
124 loc = Fileloc;
126 else
127 /* use 'Other' structure */
128 playerp = &Other;
130 /* find player on file */
131 if (loc < 0L && (loc = findname(Databuf, playerp)) < 0L)
132 /* didn't find player */
134 clear();
135 mvaddstr(11, 0, "Not found.");
136 return;
139 time(&now);
140 today = localtime(&now)->tm_yday;
142 clear();
144 for (;;)
145 /* print player structure, and prompt for action */
147 mvprintw(0, 0,"A:Name %s\n", playerp->p_name);
149 if (Wizard)
150 printw("B:Password %s\n", playerp->p_password);
151 else
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);
178 if (Wizard)
180 mvprintw(5, 40, "S:X-coord %.0f\n", playerp->p_x);
181 mvprintw(6, 40, "T:Y-coord %.0f\n", playerp->p_y);
183 else
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;
202 if (temp < 0)
203 /* last year */
204 temp += 365;
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]);
213 if (!Wizard)
214 mvprintw(19, 8, "8:Ring %c",
215 flag[playerp->p_ring.ring_type != R_NONE]);
216 else
217 mvprintw(19, 8, "8:Ring %d 9:Duration %d",
218 playerp->p_ring.ring_type, playerp->p_ring.ring_duration);
220 if (!Wizard
221 /* not wizard */
222 && (ingameflag || strcmp(Login, playerp->p_login) != 0))
223 /* in game or not examining own character */
225 if (ingameflag)
227 more(LINES - 1);
228 clear();
229 return;
231 else
232 cleanup(TRUE);
233 /*NOTREACHED*/
236 mvaddstr(20, 0, "!:Quit ?:Delete");
237 mvaddstr(21, 0, "What would you like to change ? ");
239 if (Wizard)
240 c = getanswer(" ", TRUE);
241 else
242 /* examining own player; allow to change name and password */
243 c = getanswer("!BA", FALSE);
245 switch (c)
247 case 'A': /* change name */
248 case 'B': /* change password */
249 if (!Wizard)
250 /* prompt for password */
252 mvaddstr(23, 0, "Password ? ");
253 Echo = FALSE;
254 getstring(Databuf, 9);
255 Echo = TRUE;
256 if (strcmp(Databuf, playerp->p_password) != 0)
257 continue;
260 if (c == 'A')
261 /* get new name */
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);
270 else
271 /* get new password */
273 if (!Wizard)
274 Echo = FALSE;
277 /* get two copies of new password until they match */
279 /* get first copy */
280 mvaddstr(23, 0, "New password ? ");
281 getstring(Databuf, SZ_PASSWORD);
282 if (Databuf[0] == '\0')
283 break;
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);
291 Echo = TRUE;
294 continue;
296 case 'C': /* change experience */
297 prompt = "experience";
298 dptr = &playerp->p_experience;
299 goto DALTER;
301 case 'D': /* change level */
302 prompt = "level";
303 dptr = &playerp->p_level;
304 goto DALTER;
306 case 'E': /* change strength */
307 prompt = "strength";
308 dptr = &playerp->p_strength;
309 goto DALTER;
311 case 'F': /* change swords */
312 prompt = "sword";
313 dptr = &playerp->p_sword;
314 goto DALTER;
316 case 'G': /* change energy */
317 prompt = "energy";
318 dptr = &playerp->p_energy;
319 goto DALTER;
321 case 'H': /* change maximum energy */
322 prompt = "max energy";
323 dptr = &playerp->p_maxenergy;
324 goto DALTER;
326 case 'I': /* change shields */
327 prompt = "shield";
328 dptr = &playerp->p_shield;
329 goto DALTER;
331 case 'J': /* change quickness */
332 prompt = "quickness";
333 dptr = &playerp->p_quickness;
334 goto DALTER;
336 case 'K': /* change quicksilver */
337 prompt = "quicksilver";
338 dptr = &playerp->p_quksilver;
339 goto DALTER;
341 case 'L': /* change magic */
342 prompt = "magic level";
343 dptr = &playerp->p_magiclvl;
344 goto DALTER;
346 case 'M': /* change mana */
347 prompt = "mana";
348 dptr = &playerp->p_mana;
349 goto DALTER;
351 case 'N': /* change brains */
352 prompt = "brains";
353 dptr = &playerp->p_brains;
354 goto DALTER;
356 case 'O': /* change poison */
357 prompt = "poison";
358 dptr = &playerp->p_poison;
359 goto DALTER;
361 case 'P': /* change gold */
362 prompt = "gold";
363 dptr = &playerp->p_gold;
364 goto DALTER;
366 case 'Q': /* change gems */
367 prompt = "gems";
368 dptr = &playerp->p_gems;
369 goto DALTER;
371 case 'R': /* change sin */
372 prompt = "sin";
373 dptr = &playerp->p_sin;
374 goto DALTER;
376 case 'S': /* change x coord */
377 prompt = "x";
378 dptr = &playerp->p_x;
379 goto DALTER;
381 case 'T': /* change y coord */
382 prompt = "y";
383 dptr = &playerp->p_y;
384 goto DALTER;
386 case 'U': /* change age */
387 mvprintw(23, 0, "age = %ld; age = ", playerp->p_age);
388 dtemp = infloat();
389 if (dtemp != 0.0)
390 playerp->p_age = (long) dtemp;
391 continue;
393 case 'V': /* change degen */
394 mvprintw(23, 0, "degen = %d; degen = ", playerp->p_degenerated);
395 dtemp = infloat();
396 if (dtemp != 0.0)
397 playerp->p_degenerated = (int) dtemp;
398 continue;
400 case 'W': /* change type */
401 prompt = "type";
402 sptr = &playerp->p_type;
403 goto SALTER;
405 case 'X': /* change special type */
406 prompt = "special type";
407 sptr = &playerp->p_specialtype;
408 goto SALTER;
410 case 'Y': /* change lives */
411 prompt = "lives";
412 sptr = &playerp->p_lives;
413 goto SALTER;
415 case 'Z': /* change crowns */
416 prompt = "crowns";
417 sptr = &playerp->p_crowns;
418 goto SALTER;
420 case '0': /* change charms */
421 prompt = "charm";
422 sptr = &playerp->p_charms;
423 goto SALTER;
425 case '1': /* change amulet */
426 prompt = "amulet";
427 sptr = &playerp->p_amulets;
428 goto SALTER;
430 case '2': /* change holy water */
431 prompt = "holy water";
432 sptr = &playerp->p_holywater;
433 goto SALTER;
435 case '3': /* change last-used */
436 prompt = "last-used";
437 sptr = &playerp->p_lastused;
438 goto SALTER;
440 case '4': /* change palantir */
441 prompt = "palantir";
442 bptr = &playerp->p_palantir;
443 goto BALTER;
445 case '5': /* change blessing */
446 prompt = "blessing";
447 bptr = &playerp->p_blessing;
448 goto BALTER;
450 case '6': /* change virgin */
451 prompt = "virgin";
452 bptr = &playerp->p_virgin;
453 goto BALTER;
455 case '7': /* change blindness */
456 prompt = "blindness";
457 bptr = &playerp->p_blindness;
458 goto BALTER;
460 case '8': /* change ring type */
461 prompt = "ring-type";
462 sptr = &playerp->p_ring.ring_type;
463 goto SALTER;
465 case '9': /* change ring duration */
466 prompt = "ring-duration";
467 sptr = &playerp->p_ring.ring_duration;
468 goto SALTER;
470 case '!': /* quit, update */
471 if (Wizard &&
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);
480 clear();
481 return;
483 case '?': /* delete player */
484 if (ingameflag && playerp == &Player)
485 /* cannot delete self */
486 continue;
488 freerecord(playerp, loc);
489 clear();
490 return;
492 default:
493 continue;
495 DALTER:
496 mvprintw(23, 0, "%s = %f; %s = ", prompt, *dptr, prompt);
497 dtemp = infloat();
498 if (dtemp != 0.0)
499 *dptr = dtemp;
500 continue;
502 SALTER:
503 mvprintw(23, 0, "%s = %d; %s = ", prompt, *sptr, prompt);
504 dtemp = infloat();
505 if (dtemp != 0.0)
506 *sptr = (short) dtemp;
507 continue;
509 BALTER:
510 mvprintw(23, 0, "%s = %c; %s = ", prompt, flag[*bptr], prompt);
511 c = getanswer("\nTF", TRUE);
512 if (c == 'T')
513 *bptr = TRUE;
514 else if (c == 'F')
515 *bptr = FALSE;
516 continue;
519 /*\f*/
520 /************************************************************************
522 / FUNCTION NAME: monstlist()
524 / FUNCTION: print a monster listing
526 / AUTHOR: E. A. Estes, 2/27/86
528 / ARGUMENTS: none
530 / RETURN VALUE: none
532 / MODULES CALLED: puts(), fread(), fseek(), printf()
534 / GLOBAL INPUTS: Curmonster, *Monstfp
536 / GLOBAL OUTPUTS: none
538 / DESCRIPTION:
539 / Read monster file, and print a monster listing on standard output.
541 *************************************************************************/
543 void
544 monstlist(void)
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);
556 /*\f*/
557 /************************************************************************
559 / FUNCTION NAME: scorelist()
561 / FUNCTION: print player score board
563 / AUTHOR: E. A. Estes, 12/4/85
565 / ARGUMENTS: none
567 / RETURN VALUE: none
569 / MODULES CALLED: fread(), fopen(), printf(), fclose()
571 / GLOBAL INPUTS:
573 / GLOBAL OUTPUTS: none
575 / DESCRIPTION:
576 / Read the scoreboard file and print the contents.
578 *************************************************************************/
580 void
581 scorelist(void)
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);
591 fclose(fp);
594 /*\f*/
595 /************************************************************************
597 / FUNCTION NAME: activelist()
599 / FUNCTION: print list of active players to standard output
601 / AUTHOR: E. A. Estes, 3/7/86
603 / ARGUMENTS: none
605 / RETURN VALUE: none
607 / MODULES CALLED: descrstatus(), fread(), fseek(), printf(), descrtype()
609 / GLOBAL INPUTS: Other, *Playersfp
611 / GLOBAL OUTPUTS: none
613 / DESCRIPTION:
614 / Read player file, and print list of active records to standard output.
616 *************************************************************************/
618 void
619 activelist(void)
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));
631 /*\f*/
632 /************************************************************************
634 / FUNCTION NAME: purgeoldplayers()
636 / FUNCTION: purge inactive players from player file
638 / AUTHOR: E. A. Estes, 12/4/85
640 / ARGUMENTS: none
642 / RETURN VALUE: none
644 / MODULES CALLED: freerecord(), time(), fread(), fseek(), localtime()
646 / GLOBAL INPUTS: Other, *Playersfp
648 / GLOBAL OUTPUTS: none
650 / DESCRIPTION:
651 / Delete characters which have not been used with the last
652 / three weeks.
654 *************************************************************************/
656 void
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 */
664 time(&ltime);
665 today = localtime(&ltime)->tm_yday;
667 for (;;)
669 fseek(Playersfp, loc, 0);
670 if (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) != 1)
671 break;
673 daysold = today - Other.p_lastused;
674 if (daysold < 0)
675 daysold += 365;
677 if (daysold > N_DAYSOLD)
678 /* player hasn't been used in a while; delete */
679 freerecord(&Other, loc);
681 loc += SZ_PLAYERSTRUCT;
684 /*\f*/
685 /************************************************************************
687 / FUNCTION NAME: enterscore()
689 / FUNCTION: enter player into scoreboard
691 / AUTHOR: E. A. Estes, 12/4/85
693 / ARGUMENTS: none
695 / RETURN VALUE: none
697 / MODULES CALLED: fread(), fseek(), fopen(), error(), strcmp(), fclose(),
698 / strcpy(), fwrite(), descrtype()
700 / GLOBAL INPUTS: Player
702 / GLOBAL OUTPUTS: none
704 / DESCRIPTION:
705 / The scoreboard keeps track of the highest character on a
706 / per-login basis.
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 *************************************************************************/
713 void
714 enterscore(void)
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)
726 found = TRUE;
727 break;
729 else
730 loc += SZ_SCORESTRUCT;
732 else
734 error(_PATH_SCORE);
735 /*NOTREACHED*/
739 * At this point, 'loc' will either indicate a point beyond
740 * the end of file, or the place where the previous entry
741 * was found.
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));
753 /* update entry */
754 fseek(fp, loc, 0);
755 fwrite((char *) &sbuf, SZ_SCORESTRUCT, 1, fp);
756 fclose(fp);