1 /* $NetBSD: gamesupport.c,v 1.11 2009/08/31 08:27:16 dholland Exp $ */
4 * gamesupport.c - auxiliary routines for support of Phantasia
11 * FUNCTION: examine/change statistics for a player
14 * bool ingameflag - set if called while playing game (Wizard only)
16 * GLOBAL INPUTS: LINES, *Login, Other, Wizard, Player, *stdscr, Databuf[],
19 * GLOBAL OUTPUTS: Echo
22 * Prompt for player name to examine/change.
23 * If the name is NULL, print a list of all players.
24 * If we are called from within the game, check for the
25 * desired name being the same as the current player's name.
26 * Only the 'Wizard' may alter players.
27 * Items are changed only if a non-zero value is specified.
28 * To change an item to 0, use 0.1; it will be truncated later.
30 * Players may alter their names and passwords, if the following
32 * - current login matches the character's logins
33 * - the password is known
34 * - the player is not in the middle of the game (ingameflag == FALSE)
36 * The last condition is imposed for two reasons:
37 * - the game could possibly get a bit hectic if a player were
38 * continually changing his/her name
39 * - another player structure would be necessary to check for names
44 changestats(bool ingameflag
)
46 static char flag
[2] = /* for printing values of bools */
48 struct player
*playerp
; /* pointer to structure to alter */
49 const char *prompt
; /* pointer to prompt string */
51 int today
; /* day of year of today */
52 int temp
; /* temporary variable */
53 long loc
; /* location in player file */
54 time_t now
; /* time now */
55 double dtemp
; /* temporary variable */
56 bool *bptr
; /* pointer to bool item to change */
57 double *dptr
; /* pointer to double item to change */
58 short *sptr
; /* pointer to short item to change */
63 /* get name of player to examine/alter */
64 mvaddstr(5, 0, "Which character do you want to look at ? ");
65 getstring(Databuf
, SZ_DATABUF
);
68 if (Databuf
[0] == '\0')
77 /* use 'Player' structure */
79 else if (strcmp(Databuf
, Player
.p_name
) == 0) {
80 /* alter/examine current player */
84 /* use 'Other' structure */
87 /* find player on file */
88 if (loc
< 0L && (loc
= findname(Databuf
, playerp
)) < 0L) {
89 /* didn't find player */
91 mvaddstr(11, 0, "Not found.");
96 today
= localtime(&now
)->tm_yday
;
101 /* print player structure, and prompt for action */
102 mvprintw(0, 0, "A:Name %s\n", playerp
->p_name
);
105 printw("B:Password %s\n", playerp
->p_password
);
107 addstr("B:Password XXXXXXXX\n");
109 printw(" :Login %s\n", playerp
->p_login
);
111 printw("C:Experience %.0f\n", playerp
->p_experience
);
112 printw("D:Level %.0f\n", playerp
->p_level
);
113 printw("E:Strength %.0f\n", playerp
->p_strength
);
114 printw("F:Sword %.0f\n", playerp
->p_sword
);
115 printw(" :Might %.0f\n", playerp
->p_might
);
116 printw("G:Energy %.0f\n", playerp
->p_energy
);
117 printw("H:Max-Energy %.0f\n", playerp
->p_maxenergy
);
118 printw("I:Shield %.0f\n", playerp
->p_shield
);
119 printw("J:Quickness %.0f\n", playerp
->p_quickness
);
120 printw("K:Quicksilver %.0f\n", playerp
->p_quksilver
);
121 printw(" :Speed %.0f\n", playerp
->p_speed
);
122 printw("L:Magic Level %.0f\n", playerp
->p_magiclvl
);
123 printw("M:Mana %.0f\n", playerp
->p_mana
);
124 printw("N:Brains %.0f\n", playerp
->p_brains
);
126 if (Wizard
|| playerp
->p_specialtype
!= SC_VALAR
)
127 mvaddstr(0, 40, descrstatus(playerp
));
129 mvprintw(1, 40, "O:Poison %0.3f\n", playerp
->p_poison
);
130 mvprintw(2, 40, "P:Gold %.0f\n", playerp
->p_gold
);
131 mvprintw(3, 40, "Q:Gem %.0f\n", playerp
->p_gems
);
132 mvprintw(4, 40, "R:Sin %0.3f\n", playerp
->p_sin
);
134 mvprintw(5, 40, "S:X-coord %.0f\n", playerp
->p_x
);
135 mvprintw(6, 40, "T:Y-coord %.0f\n", playerp
->p_y
);
137 mvaddstr(5, 40, "S:X-coord ?\n");
138 mvaddstr(6, 40, "T:Y-coord ?\n");
141 mvprintw(7, 40, "U:Age %ld\n", playerp
->p_age
);
142 mvprintw(8, 40, "V:Degenerated %d\n", playerp
->p_degenerated
);
144 mvprintw(9, 40, "W:Type %d (%s)\n",
145 playerp
->p_type
, descrtype(playerp
, FALSE
) + 1);
146 mvprintw(10, 40, "X:Special Type %d\n", playerp
->p_specialtype
);
147 mvprintw(11, 40, "Y:Lives %d\n", playerp
->p_lives
);
148 mvprintw(12, 40, "Z:Crowns %d\n", playerp
->p_crowns
);
149 mvprintw(13, 40, "0:Charms %d\n", playerp
->p_charms
);
150 mvprintw(14, 40, "1:Amulets %d\n", playerp
->p_amulets
);
151 mvprintw(15, 40, "2:Holy Water %d\n", playerp
->p_holywater
);
153 temp
= today
- playerp
->p_lastused
;
157 mvprintw(16, 40, "3:Lastused %d (%d)\n", playerp
->p_lastused
, temp
);
159 mvprintw(18, 8, "4:Palantir %c 5:Blessing %c 6:Virgin %c 7:Blind %c",
160 flag
[playerp
->p_palantir
],
161 flag
[playerp
->p_blessing
],
162 flag
[playerp
->p_virgin
],
163 flag
[playerp
->p_blindness
]);
166 mvprintw(19, 8, "8:Ring %c",
167 flag
[playerp
->p_ring
.ring_type
!= R_NONE
]);
169 mvprintw(19, 8, "8:Ring %d 9:Duration %d",
170 playerp
->p_ring
.ring_type
, playerp
->p_ring
.ring_duration
);
173 && (ingameflag
|| strcmp(Login
, playerp
->p_login
) != 0)) {
174 /* in game or not examining own character */
184 mvaddstr(20, 0, "!:Quit ?:Delete");
185 mvaddstr(21, 0, "What would you like to change ? ");
188 c
= getanswer(" ", TRUE
);
190 /* examining own player; allow to change name and password */
191 c
= getanswer("!BA", FALSE
);
194 case 'A': /* change name */
195 case 'B': /* change password */
197 /* prompt for password */
198 mvaddstr(23, 0, "Password ? ");
200 getstring(Databuf
, 9);
202 if (strcmp(Databuf
, playerp
->p_password
) != 0)
207 mvaddstr(23, 0, "New name: ");
208 getstring(Databuf
, SZ_NAME
);
209 truncstring(Databuf
);
210 if (Databuf
[0] != '\0')
211 if (Wizard
|| findname(Databuf
, &Other
) < 0L)
212 strcpy(playerp
->p_name
, Databuf
);
214 /* get new password */
219 /* get two copies of new password until they match */
221 mvaddstr(23, 0, "New password ? ");
222 getstring(Databuf
, SZ_PASSWORD
);
223 if (Databuf
[0] == '\0')
226 /* get second copy */
227 mvaddstr(23, 0, "One more time ? ");
228 getstring(playerp
->p_password
, SZ_PASSWORD
);
229 } while (strcmp(playerp
->p_password
, Databuf
) != 0);
236 case 'C': /* change experience */
237 prompt
= "experience";
238 dptr
= &playerp
->p_experience
;
241 case 'D': /* change level */
243 dptr
= &playerp
->p_level
;
246 case 'E': /* change strength */
248 dptr
= &playerp
->p_strength
;
251 case 'F': /* change swords */
253 dptr
= &playerp
->p_sword
;
256 case 'G': /* change energy */
258 dptr
= &playerp
->p_energy
;
261 case 'H': /* change maximum energy */
262 prompt
= "max energy";
263 dptr
= &playerp
->p_maxenergy
;
266 case 'I': /* change shields */
268 dptr
= &playerp
->p_shield
;
271 case 'J': /* change quickness */
272 prompt
= "quickness";
273 dptr
= &playerp
->p_quickness
;
276 case 'K': /* change quicksilver */
277 prompt
= "quicksilver";
278 dptr
= &playerp
->p_quksilver
;
281 case 'L': /* change magic */
282 prompt
= "magic level";
283 dptr
= &playerp
->p_magiclvl
;
286 case 'M': /* change mana */
288 dptr
= &playerp
->p_mana
;
291 case 'N': /* change brains */
293 dptr
= &playerp
->p_brains
;
296 case 'O': /* change poison */
298 dptr
= &playerp
->p_poison
;
301 case 'P': /* change gold */
303 dptr
= &playerp
->p_gold
;
306 case 'Q': /* change gems */
308 dptr
= &playerp
->p_gems
;
311 case 'R': /* change sin */
313 dptr
= &playerp
->p_sin
;
316 case 'S': /* change x coord */
318 dptr
= &playerp
->p_x
;
321 case 'T': /* change y coord */
323 dptr
= &playerp
->p_y
;
326 case 'U': /* change age */
327 mvprintw(23, 0, "age = %ld; age = ", playerp
->p_age
);
330 playerp
->p_age
= (long)dtemp
;
333 case 'V': /* change degen */
334 mvprintw(23, 0, "degen = %d; degen = ", playerp
->p_degenerated
);
337 playerp
->p_degenerated
= (int)dtemp
;
340 case 'W': /* change type */
342 sptr
= &playerp
->p_type
;
345 case 'X': /* change special type */
346 prompt
= "special type";
347 sptr
= &playerp
->p_specialtype
;
350 case 'Y': /* change lives */
352 sptr
= &playerp
->p_lives
;
355 case 'Z': /* change crowns */
357 sptr
= &playerp
->p_crowns
;
360 case '0': /* change charms */
362 sptr
= &playerp
->p_charms
;
365 case '1': /* change amulet */
367 sptr
= &playerp
->p_amulets
;
370 case '2': /* change holy water */
371 prompt
= "holy water";
372 sptr
= &playerp
->p_holywater
;
375 case '3': /* change last-used */
376 prompt
= "last-used";
377 sptr
= &playerp
->p_lastused
;
380 case '4': /* change palantir */
382 bptr
= &playerp
->p_palantir
;
385 case '5': /* change blessing */
387 bptr
= &playerp
->p_blessing
;
390 case '6': /* change virgin */
392 bptr
= &playerp
->p_virgin
;
395 case '7': /* change blindness */
396 prompt
= "blindness";
397 bptr
= &playerp
->p_blindness
;
400 case '8': /* change ring type */
401 prompt
= "ring-type";
402 sptr
= &playerp
->p_ring
.ring_type
;
405 case '9': /* change ring duration */
406 prompt
= "ring-duration";
407 sptr
= &playerp
->p_ring
.ring_duration
;
410 case '!': /* quit, update */
412 (!ingameflag
|| playerp
!= &Player
)) {
413 /* turn off status if not modifying self */
414 playerp
->p_status
= S_OFF
;
415 playerp
->p_tampered
= T_OFF
;
418 writerecord(playerp
, loc
);
422 case '?': /* delete player */
423 if (ingameflag
&& playerp
== &Player
)
424 /* cannot delete self */
427 freerecord(playerp
, loc
);
435 mvprintw(23, 0, "%s = %f; %s = ", prompt
, *dptr
, prompt
);
442 mvprintw(23, 0, "%s = %d; %s = ", prompt
, *sptr
, prompt
);
445 *sptr
= (short)dtemp
;
449 mvprintw(23, 0, "%s = %c; %s = ", prompt
, flag
[*bptr
],
451 c
= getanswer("\nTF", TRUE
);
461 * FUNCTION: print a monster listing
463 * GLOBAL INPUTS: Curmonster, *Monstfp
466 * Read monster file, and print a monster listing on standard output.
472 int count
= 0; /* count in file */
474 puts(" #) Name Str Brain Quick Energy Exper Treas Type Flock%\n");
475 fseek(Monstfp
, 0L, SEEK_SET
);
476 while (fread((char *)&Curmonster
, SZ_MONSTERSTRUCT
, 1, Monstfp
) == 1)
477 printf("%2d) %-20.20s%4.0f %4.0f %2.0f %5.0f %5.0f %2d %2d %3.0f\n", count
++,
478 Curmonster
.m_name
, Curmonster
.m_strength
, Curmonster
.m_brains
,
479 Curmonster
.m_speed
, Curmonster
.m_energy
, Curmonster
.m_experience
,
480 Curmonster
.m_treasuretype
, Curmonster
.m_type
, Curmonster
.m_flock
);
484 * FUNCTION: print player score board
487 * Read the scoreboard file and print the contents.
493 struct scoreboard sbuf
; /* for reading entries */
494 FILE *fp
; /* to open the file */
496 if ((fp
= fopen(_PATH_SCORE
, "r")) != NULL
) {
497 while (fread((char *)&sbuf
, SZ_SCORESTRUCT
, 1, fp
) == 1)
498 printf("%-20s (%-9s) Level: %6.0f Type: %s\n",
499 sbuf
.sb_name
, sbuf
.sb_login
, sbuf
.sb_level
, sbuf
.sb_type
);
505 * FUNCTION: print list of active players to standard output
507 * GLOBAL INPUTS: Other, *Playersfp
510 * Read player file, and print list of active records to standard output.
516 fseek(Playersfp
, 0L, SEEK_SET
);
517 printf("Current characters on file are:\n\n");
519 while (fread((char *)&Other
, SZ_PLAYERSTRUCT
, 1, Playersfp
) == 1)
520 if (Other
.p_status
!= S_NOTUSED
)
521 printf("%-20s (%-9s) Level: %6.0f %s (%s)\n",
522 Other
.p_name
, Other
.p_login
, Other
.p_level
,
523 descrtype(&Other
, FALSE
), descrstatus(&Other
));
527 * FUNCTION: purge inactive players from player file
529 * GLOBAL INPUTS: Other, *Playersfp
532 * Delete characters which have not been used with the last
537 purgeoldplayers(void)
539 int today
; /* day of year for today */
540 int daysold
; /* how many days since the character has been used */
541 time_t ltime
; /* time in seconds */
542 long loc
= 0L; /* location in file */
545 today
= localtime(<ime
)->tm_yday
;
548 fseek(Playersfp
, loc
, SEEK_SET
);
549 if (fread((char *)&Other
, SZ_PLAYERSTRUCT
, 1, Playersfp
) != 1)
552 daysold
= today
- Other
.p_lastused
;
556 if (daysold
> N_DAYSOLD
)
557 /* player hasn't been used in a while; delete */
558 freerecord(&Other
, loc
);
560 loc
+= SZ_PLAYERSTRUCT
;
565 * FUNCTION: enter player into scoreboard
567 * GLOBAL INPUTS: Player
570 * The scoreboard keeps track of the highest character on a
572 * Search the scoreboard for an entry for the current login,
573 * if an entry is found, and it is lower than the current player,
574 * replace it, otherwise create an entry.
580 struct scoreboard sbuf
; /* buffer to read in scoreboard entries */
581 FILE *fp
; /* to open scoreboard file */
582 long loc
= 0L; /* location in scoreboard file */
583 bool found
= FALSE
; /* set if we found an entry for this login */
585 if ((fp
= fopen(_PATH_SCORE
, "r+")) != NULL
) {
586 while (fread((char *)&sbuf
, SZ_SCORESTRUCT
, 1, fp
) == 1)
587 if (strcmp(Player
.p_login
, sbuf
.sb_login
) == 0) {
591 loc
+= SZ_SCORESTRUCT
;
598 * At this point, 'loc' will either indicate a point beyond
599 * the end of file, or the place where the previous entry
603 if ((!found
) || Player
.p_level
> sbuf
.sb_level
) {
604 /* put new entry in for this login */
605 strcpy(sbuf
.sb_login
, Player
.p_login
);
606 strcpy(sbuf
.sb_name
, Player
.p_name
);
607 sbuf
.sb_level
= Player
.p_level
;
608 strcpy(sbuf
.sb_type
, descrtype(&Player
, TRUE
));
612 fseek(fp
, loc
, SEEK_SET
);
613 fwrite((char *)&sbuf
, SZ_SCORESTRUCT
, 1, fp
);