xref: /dragonfly/games/phantasia/gamesupport.c (revision 70344474)
1 /*	$NetBSD: gamesupport.c,v 1.11 2009/08/31 08:27:16 dholland Exp $	*/
2 
3 /*
4  * gamesupport.c - auxiliary routines for support of Phantasia
5  */
6 
7 #include <string.h>
8 #include "include.h"
9 
10 /*
11  * FUNCTION: examine/change statistics for a player
12  *
13  * ARGUMENTS:
14  *	bool ingameflag - set if called while playing game (Wizard only)
15  *
16  * GLOBAL INPUTS: LINES, *Login, Other, Wizard, Player, *stdscr, Databuf[],
17  *	Fileloc
18  *
19  * GLOBAL OUTPUTS: Echo
20  *
21  * DESCRIPTION:
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.
29  *
30  *	Players may alter their names and passwords, if the following
31  *	are true:
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)
35  *
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
40  *	      already in use
41  */
42 
43 void
44 changestats(bool ingameflag)
45 {
46 	static char flag[2] =	/* for printing values of bools */
47 	{ 'F', 'T' };
48 	struct player *playerp;	/* pointer to structure to alter */
49 	const char *prompt;	/* pointer to prompt string */
50 	int c;			/* input */
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 */
59 
60 	clear();
61 
62 	for (;;) {
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);
66 		truncstring(Databuf);
67 
68 		if (Databuf[0] == '\0')
69 			userlist(ingameflag);
70 		else
71 			break;
72 	}
73 
74 	loc = -1L;
75 
76 	if (!ingameflag)
77 		/* use 'Player' structure */
78 		playerp = &Player;
79 	else if (strcmp(Databuf, Player.p_name) == 0) {
80 		/* alter/examine current player */
81 		playerp = &Player;
82 		loc = Fileloc;
83 	} else
84 		/* use 'Other' structure */
85 		playerp = &Other;
86 
87 	/* find player on file */
88 	if (loc < 0L && (loc = findname(Databuf, playerp)) < 0L) {
89 		/* didn't find player */
90 		clear();
91 		mvaddstr(11, 0, "Not found.");
92 		return;
93 	}
94 
95 	time(&now);
96 	today = localtime(&now)->tm_yday;
97 
98 	clear();
99 
100 	for (;;) {
101 		/* print player structure, and prompt for action */
102 		mvprintw(0, 0, "A:Name         %s\n", playerp->p_name);
103 
104 		if (Wizard)
105 			printw("B:Password     %s\n", playerp->p_password);
106 		else
107 			addstr("B:Password     XXXXXXXX\n");
108 
109 		printw(" :Login        %s\n", playerp->p_login);
110 
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);
125 
126 		if (Wizard || playerp->p_specialtype != SC_VALAR)
127 			mvaddstr(0, 40, descrstatus(playerp));
128 
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);
133 		if (Wizard) {
134 			mvprintw(5, 40, "S:X-coord      %.0f\n", playerp->p_x);
135 			mvprintw(6, 40, "T:Y-coord      %.0f\n", playerp->p_y);
136 		} else {
137 			mvaddstr(5, 40, "S:X-coord      ?\n");
138 			mvaddstr(6, 40, "T:Y-coord      ?\n");
139 		}
140 
141 		mvprintw(7, 40, "U:Age          %ld\n", playerp->p_age);
142 		mvprintw(8, 40, "V:Degenerated  %d\n", playerp->p_degenerated);
143 
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);
152 
153 		temp = today - playerp->p_lastused;
154 		if (temp < 0)
155 			/* last year */
156 			temp += 365;
157 		mvprintw(16, 40, "3:Lastused     %d  (%d)\n", playerp->p_lastused, temp);
158 
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]);
164 
165 		if (!Wizard)
166 			mvprintw(19, 8, "8:Ring    %c",
167 			    flag[playerp->p_ring.ring_type != R_NONE]);
168 		else
169 			mvprintw(19, 8, "8:Ring    %d  9:Duration %d",
170 			    playerp->p_ring.ring_type, playerp->p_ring.ring_duration);
171 
172 		if (!Wizard
173 		    && (ingameflag || strcmp(Login, playerp->p_login) != 0)) {
174 			/* in game or not examining own character */
175 			if (ingameflag) {
176 				more(LINES - 1);
177 				clear();
178 				return;
179 			} else
180 				cleanup(TRUE);
181 			/* NOTREACHED */
182 		}
183 
184 		mvaddstr(20, 0, "!:Quit       ?:Delete");
185 		mvaddstr(21, 0, "What would you like to change ? ");
186 
187 		if (Wizard)
188 			c = getanswer(" ", TRUE);
189 		else
190 			/* examining own player; allow to change name and password */
191 			c = getanswer("!BA", FALSE);
192 
193 		switch (c) {
194 		case 'A':	/* change name */
195 		case 'B':	/* change password */
196 			if (!Wizard) {
197 				/* prompt for password */
198 				mvaddstr(23, 0, "Password ? ");
199 				Echo = FALSE;
200 				getstring(Databuf, 9);
201 				Echo = TRUE;
202 				if (strcmp(Databuf, playerp->p_password) != 0)
203 					continue;
204 			}
205 			if (c == 'A') {
206 				/* get new name */
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);
213 			} else {
214 				/* get new password */
215 				if (!Wizard)
216 					Echo = FALSE;
217 
218 				do {
219 					/* get two copies of new password until they match */
220 					/* get first copy */
221 					mvaddstr(23, 0, "New password ? ");
222 					getstring(Databuf, SZ_PASSWORD);
223 					if (Databuf[0] == '\0')
224 						break;
225 
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);
230 
231 				Echo = TRUE;
232 			}
233 
234 			continue;
235 
236 		case 'C':	/* change experience */
237 			prompt = "experience";
238 			dptr = &playerp->p_experience;
239 			goto DALTER;
240 
241 		case 'D':	/* change level */
242 			prompt = "level";
243 			dptr = &playerp->p_level;
244 			goto DALTER;
245 
246 		case 'E':	/* change strength */
247 			prompt = "strength";
248 			dptr = &playerp->p_strength;
249 			goto DALTER;
250 
251 		case 'F':	/* change swords */
252 			prompt = "sword";
253 			dptr = &playerp->p_sword;
254 			goto DALTER;
255 
256 		case 'G':	/* change energy */
257 			prompt = "energy";
258 			dptr = &playerp->p_energy;
259 			goto DALTER;
260 
261 		case 'H':	/* change maximum energy */
262 			prompt = "max energy";
263 			dptr = &playerp->p_maxenergy;
264 			goto DALTER;
265 
266 		case 'I':	/* change shields */
267 			prompt = "shield";
268 			dptr = &playerp->p_shield;
269 			goto DALTER;
270 
271 		case 'J':	/* change quickness */
272 			prompt = "quickness";
273 			dptr = &playerp->p_quickness;
274 			goto DALTER;
275 
276 		case 'K':	/* change quicksilver */
277 			prompt = "quicksilver";
278 			dptr = &playerp->p_quksilver;
279 			goto DALTER;
280 
281 		case 'L':	/* change magic */
282 			prompt = "magic level";
283 			dptr = &playerp->p_magiclvl;
284 			goto DALTER;
285 
286 		case 'M':	/* change mana */
287 			prompt = "mana";
288 			dptr = &playerp->p_mana;
289 			goto DALTER;
290 
291 		case 'N':	/* change brains */
292 			prompt = "brains";
293 			dptr = &playerp->p_brains;
294 			goto DALTER;
295 
296 		case 'O':	/* change poison */
297 			prompt = "poison";
298 			dptr = &playerp->p_poison;
299 			goto DALTER;
300 
301 		case 'P':	/* change gold */
302 			prompt = "gold";
303 			dptr = &playerp->p_gold;
304 			goto DALTER;
305 
306 		case 'Q':	/* change gems */
307 			prompt = "gems";
308 			dptr = &playerp->p_gems;
309 			goto DALTER;
310 
311 		case 'R':	/* change sin */
312 			prompt = "sin";
313 			dptr = &playerp->p_sin;
314 			goto DALTER;
315 
316 		case 'S':	/* change x coord */
317 			prompt = "x";
318 			dptr = &playerp->p_x;
319 			goto DALTER;
320 
321 		case 'T':	/* change y coord */
322 			prompt = "y";
323 			dptr = &playerp->p_y;
324 			goto DALTER;
325 
326 		case 'U':	/* change age */
327 			mvprintw(23, 0, "age = %ld; age = ", playerp->p_age);
328 			dtemp = infloat();
329 			if (dtemp != 0.0)
330 				playerp->p_age = (long)dtemp;
331 			continue;
332 
333 		case 'V':	/* change degen */
334 			mvprintw(23, 0, "degen = %d; degen = ", playerp->p_degenerated);
335 			dtemp = infloat();
336 			if (dtemp != 0.0)
337 				playerp->p_degenerated = (int)dtemp;
338 			continue;
339 
340 		case 'W':	/* change type */
341 			prompt = "type";
342 			sptr = &playerp->p_type;
343 			goto SALTER;
344 
345 		case 'X':	/* change special type */
346 			prompt = "special type";
347 			sptr = &playerp->p_specialtype;
348 			goto SALTER;
349 
350 		case 'Y':	/* change lives */
351 			prompt = "lives";
352 			sptr = &playerp->p_lives;
353 			goto SALTER;
354 
355 		case 'Z':	/* change crowns */
356 			prompt = "crowns";
357 			sptr = &playerp->p_crowns;
358 			goto SALTER;
359 
360 		case '0':	/* change charms */
361 			prompt = "charm";
362 			sptr = &playerp->p_charms;
363 			goto SALTER;
364 
365 		case '1':	/* change amulet */
366 			prompt = "amulet";
367 			sptr = &playerp->p_amulets;
368 			goto SALTER;
369 
370 		case '2':	/* change holy water */
371 			prompt = "holy water";
372 			sptr = &playerp->p_holywater;
373 			goto SALTER;
374 
375 		case '3':	/* change last-used */
376 			prompt = "last-used";
377 			sptr = &playerp->p_lastused;
378 			goto SALTER;
379 
380 		case '4':	/* change palantir */
381 			prompt = "palantir";
382 			bptr = &playerp->p_palantir;
383 			goto BALTER;
384 
385 		case '5':	/* change blessing */
386 			prompt = "blessing";
387 			bptr = &playerp->p_blessing;
388 			goto BALTER;
389 
390 		case '6':	/* change virgin */
391 			prompt = "virgin";
392 			bptr = &playerp->p_virgin;
393 			goto BALTER;
394 
395 		case '7':	/* change blindness */
396 			prompt = "blindness";
397 			bptr = &playerp->p_blindness;
398 			goto BALTER;
399 
400 		case '8':	/* change ring type */
401 			prompt = "ring-type";
402 			sptr = &playerp->p_ring.ring_type;
403 			goto SALTER;
404 
405 		case '9':	/* change ring duration */
406 			prompt = "ring-duration";
407 			sptr = &playerp->p_ring.ring_duration;
408 			goto SALTER;
409 
410 		case '!':	/* quit, update */
411 			if (Wizard &&
412 			    (!ingameflag || playerp != &Player)) {
413 				/* turn off status if not modifying self */
414 				playerp->p_status = S_OFF;
415 				playerp->p_tampered = T_OFF;
416 			}
417 
418 			writerecord(playerp, loc);
419 			clear();
420 			return;
421 
422 		case '?':	/* delete player */
423 			if (ingameflag && playerp == &Player)
424 				/* cannot delete self */
425 				continue;
426 
427 			freerecord(playerp, loc);
428 			clear();
429 			return;
430 
431 		default:
432 			continue;
433 		}
434 DALTER:
435 		mvprintw(23, 0, "%s = %f; %s = ", prompt, *dptr, prompt);
436 		dtemp = infloat();
437 		if (dtemp != 0.0)
438 			*dptr = dtemp;
439 		continue;
440 
441 SALTER:
442 		mvprintw(23, 0, "%s = %d; %s = ", prompt, *sptr, prompt);
443 		dtemp = infloat();
444 		if (dtemp != 0.0)
445 			*sptr = (short)dtemp;
446 		continue;
447 
448 BALTER:
449 		mvprintw(23, 0, "%s = %c; %s = ", prompt, flag[*bptr],
450 		    prompt);
451 		c = getanswer("\nTF", TRUE);
452 		if (c == 'T')
453 			*bptr = TRUE;
454 		else if (c == 'F')
455 			*bptr = FALSE;
456 		continue;
457 	}
458 }
459 
460 /*
461  * FUNCTION: print a monster listing
462  *
463  * GLOBAL INPUTS: Curmonster, *Monstfp
464  *
465  * DESCRIPTION:
466  *	Read monster file, and print a monster listing on standard output.
467  */
468 
469 void
470 monstlist(void)
471 {
472 	int count = 0;	/* count in file */
473 
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);
481 }
482 
483 /*
484  * FUNCTION: print player score board
485  *
486  * DESCRIPTION:
487  *	Read the scoreboard file and print the contents.
488  */
489 
490 void
491 scorelist(void)
492 {
493 	struct scoreboard sbuf;	/* for reading entries */
494 	FILE *fp;		/* to open the file */
495 
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);
500 		fclose(fp);
501 	}
502 }
503 
504 /*
505  * FUNCTION: print list of active players to standard output
506  *
507  * GLOBAL INPUTS: Other, *Playersfp
508  *
509  * DESCRIPTION:
510  *	Read player file, and print list of active records to standard output.
511  */
512 
513 void
514 activelist(void)
515 {
516 	fseek(Playersfp, 0L, SEEK_SET);
517 	printf("Current characters on file are:\n\n");
518 
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));
524 }
525 
526 /*
527  * FUNCTION: purge inactive players from player file
528  *
529  * GLOBAL INPUTS: Other, *Playersfp
530  *
531  * DESCRIPTION:
532  *	Delete characters which have not been used with the last
533  *	three weeks.
534  */
535 
536 void
537 purgeoldplayers(void)
538 {
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 */
543 
544 	time(&ltime);
545 	today = localtime(&ltime)->tm_yday;
546 
547 	for (;;) {
548 		fseek(Playersfp, loc, SEEK_SET);
549 		if (fread((char *)&Other, SZ_PLAYERSTRUCT, 1, Playersfp) != 1)
550 			break;
551 
552 		daysold = today - Other.p_lastused;
553 		if (daysold < 0)
554 			daysold += 365;
555 
556 		if (daysold > N_DAYSOLD)
557 			/* player hasn't been used in a while; delete */
558 			freerecord(&Other, loc);
559 
560 		loc += SZ_PLAYERSTRUCT;
561 	}
562 }
563 
564 /*
565  * FUNCTION: enter player into scoreboard
566  *
567  * GLOBAL INPUTS: Player
568  *
569  * DESCRIPTION:
570  *	The scoreboard keeps track of the highest character on a
571  *	per-login basis.
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.
575  */
576 
577 void
578 enterscore(void)
579 {
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 */
584 
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) {
588 				found = TRUE;
589 				break;
590 			} else
591 				loc += SZ_SCORESTRUCT;
592 	} else {
593 		error(_PATH_SCORE);
594 		/* NOTREACHED */
595 	}
596 
597 	/*
598 	 * At this point, 'loc' will either indicate a point beyond
599 	 * the end of file, or the place where the previous entry
600 	 * was found.
601 	 */
602 
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));
609 	}
610 
611 	/* update entry */
612 	fseek(fp, loc, SEEK_SET);
613 	fwrite((char *)&sbuf, SZ_SCORESTRUCT, 1, fp);
614 	fclose(fp);
615 }
616