xref: /csrg-svn/games/rogue/message.c (revision 60842)
132689Sbostic /*
2*60842Sbostic  * Copyright (c) 1988, 1993
3*60842Sbostic  *	The Regents of the University of California.  All rights reserved.
436704Sbostic  *
536704Sbostic  * This code is derived from software contributed to Berkeley by
636704Sbostic  * Timothy C. Stoehr.
736704Sbostic  *
842596Sbostic  * %sccs.include.redist.c%
936704Sbostic  */
1036704Sbostic 
1136704Sbostic #ifndef lint
12*60842Sbostic static char sccsid[] = "@(#)message.c	8.1 (Berkeley) 05/31/93";
1336704Sbostic #endif /* not lint */
1436704Sbostic 
1536704Sbostic /*
1632689Sbostic  * message.c
1732689Sbostic  *
1832689Sbostic  * This source herein may be modified and/or distributed by anybody who
1932689Sbostic  * so desires, with the following restrictions:
2032689Sbostic  *    1.)  No portion of this notice shall be removed.
2132689Sbostic  *    2.)  Credit shall not be taken for the creation of this source.
2232689Sbostic  *    3.)  This code is not to be traded, sold, or used for personal
2332689Sbostic  *         gain or profit.
2432689Sbostic  *
2532689Sbostic  */
2632689Sbostic 
2732689Sbostic #include <stdio.h>
2832689Sbostic #include "rogue.h"
2932689Sbostic 
3032689Sbostic char msgs[NMESSAGES][DCOLS] = {"", "", "", "", ""};
3132689Sbostic short msg_col = 0, imsg = -1;
3232689Sbostic boolean msg_cleared = 1, rmsg = 0;
3332689Sbostic char hunger_str[8] = "";
3432689Sbostic char *more = "-more-";
3532689Sbostic 
3632689Sbostic extern boolean cant_int, did_int, interrupted, save_is_interactive;
3732689Sbostic extern short add_strength;
3832689Sbostic extern short cur_level;
3932689Sbostic 
message(msg,intrpt)4032689Sbostic message(msg, intrpt)
4132689Sbostic char *msg;
4232689Sbostic boolean intrpt;
4332689Sbostic {
4432689Sbostic 	cant_int = 1;
4532689Sbostic 
4632689Sbostic 	if (!save_is_interactive) {
4732689Sbostic 		return;
4832689Sbostic 	}
4932689Sbostic 	if (intrpt) {
5032689Sbostic 		interrupted = 1;
5132689Sbostic 		md_slurp();
5232689Sbostic 	}
5332689Sbostic 
5432689Sbostic 	if (!msg_cleared) {
5532689Sbostic 		mvaddstr(MIN_ROW-1, msg_col, more);
5632689Sbostic 		refresh();
5732689Sbostic 		wait_for_ack();
5832689Sbostic 		check_message();
5932689Sbostic 	}
6032689Sbostic 	if (!rmsg) {
6132689Sbostic 		imsg = (imsg + 1) % NMESSAGES;
6232689Sbostic 		(void) strcpy(msgs[imsg], msg);
6332689Sbostic 	}
6432689Sbostic 	mvaddstr(MIN_ROW-1, 0, msg);
6532689Sbostic 	addch(' ');
6632689Sbostic 	refresh();
6732689Sbostic 	msg_cleared = 0;
6832689Sbostic 	msg_col = strlen(msg);
6932689Sbostic 
7032689Sbostic 	cant_int = 0;
7132689Sbostic 
7232689Sbostic 	if (did_int) {
7332689Sbostic 		did_int = 0;
7432689Sbostic 		onintr();
7532689Sbostic 	}
7632689Sbostic }
7732689Sbostic 
remessage(c)7832689Sbostic remessage(c)
7932689Sbostic short c;
8032689Sbostic {
8132689Sbostic 	if (imsg != -1) {
8232689Sbostic 		check_message();
8332689Sbostic 		rmsg = 1;
8432689Sbostic 		while (c > imsg) {
8532689Sbostic 			c -= NMESSAGES;
8632689Sbostic 		}
8732689Sbostic 		message(msgs[((imsg - c) % NMESSAGES)], 0);
8832689Sbostic 		rmsg = 0;
8932689Sbostic 		move(rogue.row, rogue.col);
9032689Sbostic 		refresh();
9132689Sbostic 	}
9232689Sbostic }
9332689Sbostic 
check_message()9432689Sbostic check_message()
9532689Sbostic {
9632689Sbostic 	if (msg_cleared) {
9732689Sbostic 		return;
9832689Sbostic 	}
9932689Sbostic 	move(MIN_ROW-1, 0);
10032689Sbostic 	clrtoeol();
10132689Sbostic 	refresh();
10232689Sbostic 	msg_cleared = 1;
10332689Sbostic }
10432689Sbostic 
get_input_line(prompt,insert,buf,if_cancelled,add_blank,do_echo)10532689Sbostic get_input_line(prompt, insert, buf, if_cancelled, add_blank, do_echo)
10632689Sbostic char *prompt, *buf, *insert;
10732689Sbostic char *if_cancelled;
10832689Sbostic boolean add_blank;
10932689Sbostic boolean do_echo;
11032689Sbostic {
11132689Sbostic 	short ch;
11232689Sbostic 	short i = 0, n;
11332689Sbostic 
11432689Sbostic 	message(prompt, 0);
11532689Sbostic 	n = strlen(prompt);
11632689Sbostic 
11732689Sbostic 	if (insert[0]) {
11832689Sbostic 		mvaddstr(0, n + 1, insert);
11932689Sbostic 		(void) strcpy(buf, insert);
12032689Sbostic 		i = strlen(insert);
12132689Sbostic 		move(0, (n + i + 1));
12232689Sbostic 		refresh();
12332689Sbostic 	}
12432689Sbostic 
12532689Sbostic 	while (((ch = rgetchar()) != '\r') && (ch != '\n') && (ch != CANCEL)) {
12632689Sbostic 		if ((ch >= ' ') && (ch <= '~') && (i < MAX_TITLE_LENGTH-2)) {
12732689Sbostic 			if ((ch != ' ') || (i > 0)) {
12832689Sbostic 				buf[i++] = ch;
12932689Sbostic 				if (do_echo) {
13032689Sbostic 					addch(ch);
13132689Sbostic 				}
13232689Sbostic 			}
13332689Sbostic 		}
13432689Sbostic 		if ((ch == '\b') && (i > 0)) {
13532689Sbostic 			if (do_echo) {
13632689Sbostic 				mvaddch(0, i + n, ' ');
13732689Sbostic 				move(MIN_ROW-1, i+n);
13832689Sbostic 			}
13932689Sbostic 			i--;
14032689Sbostic 		}
14132689Sbostic 		refresh();
14232689Sbostic 	}
14332689Sbostic 	check_message();
14432689Sbostic 	if (add_blank) {
14532689Sbostic 		buf[i++] = ' ';
14632689Sbostic 	} else {
14732689Sbostic 		while ((i > 0) && (buf[i-1] == ' ')) {
14832689Sbostic 			i--;
14932689Sbostic 		}
15032689Sbostic 	}
15132689Sbostic 
15232689Sbostic 	buf[i] = 0;
15332689Sbostic 
15432689Sbostic 	if ((ch == CANCEL) || (i == 0) || ((i == 1) && add_blank)) {
15532689Sbostic 		if (if_cancelled) {
15632689Sbostic 			message(if_cancelled, 0);
15732689Sbostic 		}
15832689Sbostic 		return(0);
15932689Sbostic 	}
16032689Sbostic 	return(i);
16132689Sbostic }
16232689Sbostic 
rgetchar()16332689Sbostic rgetchar()
16432689Sbostic {
16532689Sbostic 	register ch;
16632689Sbostic 
16732689Sbostic 	for(;;) {
16832689Sbostic 		ch = getchar();
16932689Sbostic 
17032689Sbostic 		switch(ch) {
17132689Sbostic 		case '\022':
17232689Sbostic 			wrefresh(curscr);
17332689Sbostic 			break;
17432689Sbostic #ifdef UNIX_BSD4_2
17532689Sbostic 		case '\032':
17632689Sbostic 			printf(CL);
17732689Sbostic 			fflush(stdout);
17832689Sbostic 			tstp();
17932689Sbostic 			break;
18032689Sbostic #endif
18132689Sbostic 		case '&':
18232689Sbostic 			save_screen();
18332689Sbostic 			break;
18432689Sbostic 		default:
18532689Sbostic 			return(ch);
18632689Sbostic 		}
18732689Sbostic 	}
18832689Sbostic }
18932689Sbostic /*
19032689Sbostic Level: 99 Gold: 999999 Hp: 999(999) Str: 99(99) Arm: 99 Exp: 21/10000000 Hungry
19132689Sbostic 0    5    1    5    2    5    3    5    4    5    5    5    6    5    7    5
19232689Sbostic */
19332689Sbostic 
print_stats(stat_mask)19432689Sbostic print_stats(stat_mask)
19532689Sbostic register stat_mask;
19632689Sbostic {
19732689Sbostic 	char buf[16];
19832689Sbostic 	boolean label;
19932689Sbostic 	int row = DROWS - 1;
20032689Sbostic 
20132689Sbostic 	label = (stat_mask & STAT_LABEL) ? 1 : 0;
20232689Sbostic 
20332689Sbostic 	if (stat_mask & STAT_LEVEL) {
20432689Sbostic 		if (label) {
20532689Sbostic 			mvaddstr(row, 0, "Level: ");
20632689Sbostic 		}
20732689Sbostic 		/* max level taken care of in make_level() */
20832689Sbostic 		sprintf(buf, "%d", cur_level);
20932689Sbostic 		mvaddstr(row, 7, buf);
21032689Sbostic 		pad(buf, 2);
21132689Sbostic 	}
21232689Sbostic 	if (stat_mask & STAT_GOLD) {
21332689Sbostic 		if (label) {
21432689Sbostic 			mvaddstr(row, 10, "Gold: ");
21532689Sbostic 		}
21632689Sbostic 		if (rogue.gold > MAX_GOLD) {
21732689Sbostic 			rogue.gold = MAX_GOLD;
21832689Sbostic 		}
21932689Sbostic 		sprintf(buf, "%ld", rogue.gold);
22032689Sbostic 		mvaddstr(row, 16, buf);
22132689Sbostic 		pad(buf, 6);
22232689Sbostic 	}
22332689Sbostic 	if (stat_mask & STAT_HP) {
22432689Sbostic 		if (label) {
22532689Sbostic 			mvaddstr(row, 23, "Hp: ");
22632689Sbostic 		}
22732689Sbostic 		if (rogue.hp_max > MAX_HP) {
22832689Sbostic 			rogue.hp_current -= (rogue.hp_max - MAX_HP);
22932689Sbostic 			rogue.hp_max = MAX_HP;
23032689Sbostic 		}
23132689Sbostic 		sprintf(buf, "%d(%d)", rogue.hp_current, rogue.hp_max);
23232689Sbostic 		mvaddstr(row, 27, buf);
23332689Sbostic 		pad(buf, 8);
23432689Sbostic 	}
23532689Sbostic 	if (stat_mask & STAT_STRENGTH) {
23632689Sbostic 		if (label) {
23732689Sbostic 			mvaddstr(row, 36, "Str: ");
23832689Sbostic 		}
23932689Sbostic 		if (rogue.str_max > MAX_STRENGTH) {
24032689Sbostic 			rogue.str_current -= (rogue.str_max - MAX_STRENGTH);
24132689Sbostic 			rogue.str_max = MAX_STRENGTH;
24232689Sbostic 		}
24332689Sbostic 		sprintf(buf, "%d(%d)", (rogue.str_current + add_strength),
24432689Sbostic 			rogue.str_max);
24532689Sbostic 		mvaddstr(row, 41, buf);
24632689Sbostic 		pad(buf, 6);
24732689Sbostic 	}
24832689Sbostic 	if (stat_mask & STAT_ARMOR) {
24932689Sbostic 		if (label) {
25032689Sbostic 			mvaddstr(row, 48, "Arm: ");
25132689Sbostic 		}
25232689Sbostic 		if (rogue.armor && (rogue.armor->d_enchant > MAX_ARMOR)) {
25332689Sbostic 			rogue.armor->d_enchant = MAX_ARMOR;
25432689Sbostic 		}
25532689Sbostic 		sprintf(buf, "%d", get_armor_class(rogue.armor));
25632689Sbostic 		mvaddstr(row, 53, buf);
25732689Sbostic 		pad(buf, 2);
25832689Sbostic 	}
25932689Sbostic 	if (stat_mask & STAT_EXP) {
26032689Sbostic 		if (label) {
26132689Sbostic 			mvaddstr(row, 56, "Exp: ");
26232689Sbostic 		}
26332689Sbostic 		if (rogue.exp_points > MAX_EXP) {
26432689Sbostic 			rogue.exp_points = MAX_EXP;
26532689Sbostic 		}
26632689Sbostic 		if (rogue.exp > MAX_EXP_LEVEL) {
26732689Sbostic 			rogue.exp = MAX_EXP_LEVEL;
26832689Sbostic 		}
26932689Sbostic 		sprintf(buf, "%d/%ld", rogue.exp, rogue.exp_points);
27032689Sbostic 		mvaddstr(row, 61, buf);
27132689Sbostic 		pad(buf, 11);
27232689Sbostic 	}
27332689Sbostic 	if (stat_mask & STAT_HUNGER) {
27432689Sbostic 		mvaddstr(row, 73, hunger_str);
27532689Sbostic 		clrtoeol();
27632689Sbostic 	}
27732689Sbostic 	refresh();
27832689Sbostic }
27932689Sbostic 
pad(s,n)28032689Sbostic pad(s, n)
28132689Sbostic char *s;
28232689Sbostic short n;
28332689Sbostic {
28432689Sbostic 	short i;
28532689Sbostic 
28632689Sbostic 	for (i = strlen(s); i < n; i++) {
28732689Sbostic 		addch(' ');
28832689Sbostic 	}
28932689Sbostic }
29032689Sbostic 
save_screen()29132689Sbostic save_screen()
29232689Sbostic {
29332689Sbostic 	FILE *fp;
29432689Sbostic 	short i, j;
29532689Sbostic 	char buf[DCOLS+2];
29632689Sbostic 	boolean found_non_blank;
29732689Sbostic 
29832689Sbostic 	if ((fp = fopen("rogue.screen", "w")) != NULL) {
29932689Sbostic 		for (i = 0; i < DROWS; i++) {
30032689Sbostic 			found_non_blank = 0;
30132689Sbostic 			for (j = (DCOLS - 1); j >= 0; j--) {
30232689Sbostic 				buf[j] = mvinch(i, j);
30332689Sbostic 				if (!found_non_blank) {
30432689Sbostic 					if ((buf[j] != ' ') || (j == 0)) {
30532689Sbostic 						buf[j + ((j == 0) ? 0 : 1)] = 0;
30632689Sbostic 						found_non_blank = 1;
30732689Sbostic 					}
30832689Sbostic 				}
30932689Sbostic 			}
31032689Sbostic 			fputs(buf, fp);
31132689Sbostic 			putc('\n', fp);
31232689Sbostic 		}
31332689Sbostic 		fclose(fp);
31432689Sbostic 	} else {
31532689Sbostic 		sound_bell();
31632689Sbostic 	}
31732689Sbostic }
31832689Sbostic 
sound_bell()31932689Sbostic sound_bell()
32032689Sbostic {
32132689Sbostic 	putchar(7);
32232689Sbostic 	fflush(stdout);
32332689Sbostic }
32432689Sbostic 
32532689Sbostic boolean
is_digit(ch)32632689Sbostic is_digit(ch)
32732689Sbostic short ch;
32832689Sbostic {
32932689Sbostic 	return((ch >= '0') && (ch <= '9'));
33032689Sbostic }
33132689Sbostic 
r_index(str,ch,last)33232689Sbostic r_index(str, ch, last)
33332689Sbostic char *str;
33432689Sbostic int ch;
33532689Sbostic boolean last;
33632689Sbostic {
33732689Sbostic 	int i = 0;
33832689Sbostic 
33932689Sbostic 	if (last) {
34032689Sbostic 		for (i = strlen(str) - 1; i >= 0; i--) {
34132689Sbostic 			if (str[i] == ch) {
34232689Sbostic 				return(i);
34332689Sbostic 			}
34432689Sbostic 		}
34532689Sbostic 	} else {
34632689Sbostic 		for (i = 0; str[i]; i++) {
34732689Sbostic 			if (str[i] == ch) {
34832689Sbostic 				return(i);
34932689Sbostic 			}
35032689Sbostic 		}
35132689Sbostic 	}
35232689Sbostic 	return(-1);
35332689Sbostic }
354