xref: /csrg-svn/games/rogue/message.c (revision 32689)
1*32689Sbostic /*
2*32689Sbostic  * message.c
3*32689Sbostic  *
4*32689Sbostic  * This source herein may be modified and/or distributed by anybody who
5*32689Sbostic  * so desires, with the following restrictions:
6*32689Sbostic  *    1.)  No portion of this notice shall be removed.
7*32689Sbostic  *    2.)  Credit shall not be taken for the creation of this source.
8*32689Sbostic  *    3.)  This code is not to be traded, sold, or used for personal
9*32689Sbostic  *         gain or profit.
10*32689Sbostic  *
11*32689Sbostic  */
12*32689Sbostic 
13*32689Sbostic #ifndef lint
14*32689Sbostic static char sccsid[] = "@(#)message.c	5.1 (Berkeley) 11/25/87";
15*32689Sbostic #endif /* not lint */
16*32689Sbostic 
17*32689Sbostic #include <stdio.h>
18*32689Sbostic #include "rogue.h"
19*32689Sbostic 
20*32689Sbostic char msgs[NMESSAGES][DCOLS] = {"", "", "", "", ""};
21*32689Sbostic short msg_col = 0, imsg = -1;
22*32689Sbostic boolean msg_cleared = 1, rmsg = 0;
23*32689Sbostic char hunger_str[8] = "";
24*32689Sbostic char *more = "-more-";
25*32689Sbostic 
26*32689Sbostic extern boolean cant_int, did_int, interrupted, save_is_interactive;
27*32689Sbostic extern short add_strength;
28*32689Sbostic extern short cur_level;
29*32689Sbostic 
30*32689Sbostic message(msg, intrpt)
31*32689Sbostic char *msg;
32*32689Sbostic boolean intrpt;
33*32689Sbostic {
34*32689Sbostic 	cant_int = 1;
35*32689Sbostic 
36*32689Sbostic 	if (!save_is_interactive) {
37*32689Sbostic 		return;
38*32689Sbostic 	}
39*32689Sbostic 	if (intrpt) {
40*32689Sbostic 		interrupted = 1;
41*32689Sbostic 		md_slurp();
42*32689Sbostic 	}
43*32689Sbostic 
44*32689Sbostic 	if (!msg_cleared) {
45*32689Sbostic 		mvaddstr(MIN_ROW-1, msg_col, more);
46*32689Sbostic 		refresh();
47*32689Sbostic 		wait_for_ack();
48*32689Sbostic 		check_message();
49*32689Sbostic 	}
50*32689Sbostic 	if (!rmsg) {
51*32689Sbostic 		imsg = (imsg + 1) % NMESSAGES;
52*32689Sbostic 		(void) strcpy(msgs[imsg], msg);
53*32689Sbostic 	}
54*32689Sbostic 	mvaddstr(MIN_ROW-1, 0, msg);
55*32689Sbostic 	addch(' ');
56*32689Sbostic 	refresh();
57*32689Sbostic 	msg_cleared = 0;
58*32689Sbostic 	msg_col = strlen(msg);
59*32689Sbostic 
60*32689Sbostic 	cant_int = 0;
61*32689Sbostic 
62*32689Sbostic 	if (did_int) {
63*32689Sbostic 		did_int = 0;
64*32689Sbostic 		onintr();
65*32689Sbostic 	}
66*32689Sbostic }
67*32689Sbostic 
68*32689Sbostic remessage(c)
69*32689Sbostic short c;
70*32689Sbostic {
71*32689Sbostic 	if (imsg != -1) {
72*32689Sbostic 		check_message();
73*32689Sbostic 		rmsg = 1;
74*32689Sbostic 		while (c > imsg) {
75*32689Sbostic 			c -= NMESSAGES;
76*32689Sbostic 		}
77*32689Sbostic 		message(msgs[((imsg - c) % NMESSAGES)], 0);
78*32689Sbostic 		rmsg = 0;
79*32689Sbostic 		move(rogue.row, rogue.col);
80*32689Sbostic 		refresh();
81*32689Sbostic 	}
82*32689Sbostic }
83*32689Sbostic 
84*32689Sbostic check_message()
85*32689Sbostic {
86*32689Sbostic 	if (msg_cleared) {
87*32689Sbostic 		return;
88*32689Sbostic 	}
89*32689Sbostic 	move(MIN_ROW-1, 0);
90*32689Sbostic 	clrtoeol();
91*32689Sbostic 	refresh();
92*32689Sbostic 	msg_cleared = 1;
93*32689Sbostic }
94*32689Sbostic 
95*32689Sbostic get_input_line(prompt, insert, buf, if_cancelled, add_blank, do_echo)
96*32689Sbostic char *prompt, *buf, *insert;
97*32689Sbostic char *if_cancelled;
98*32689Sbostic boolean add_blank;
99*32689Sbostic boolean do_echo;
100*32689Sbostic {
101*32689Sbostic 	short ch;
102*32689Sbostic 	short i = 0, n;
103*32689Sbostic 
104*32689Sbostic 	message(prompt, 0);
105*32689Sbostic 	n = strlen(prompt);
106*32689Sbostic 
107*32689Sbostic 	if (insert[0]) {
108*32689Sbostic 		mvaddstr(0, n + 1, insert);
109*32689Sbostic 		(void) strcpy(buf, insert);
110*32689Sbostic 		i = strlen(insert);
111*32689Sbostic 		move(0, (n + i + 1));
112*32689Sbostic 		refresh();
113*32689Sbostic 	}
114*32689Sbostic 
115*32689Sbostic 	while (((ch = rgetchar()) != '\r') && (ch != '\n') && (ch != CANCEL)) {
116*32689Sbostic 		if ((ch >= ' ') && (ch <= '~') && (i < MAX_TITLE_LENGTH-2)) {
117*32689Sbostic 			if ((ch != ' ') || (i > 0)) {
118*32689Sbostic 				buf[i++] = ch;
119*32689Sbostic 				if (do_echo) {
120*32689Sbostic 					addch(ch);
121*32689Sbostic 				}
122*32689Sbostic 			}
123*32689Sbostic 		}
124*32689Sbostic 		if ((ch == '\b') && (i > 0)) {
125*32689Sbostic 			if (do_echo) {
126*32689Sbostic 				mvaddch(0, i + n, ' ');
127*32689Sbostic 				move(MIN_ROW-1, i+n);
128*32689Sbostic 			}
129*32689Sbostic 			i--;
130*32689Sbostic 		}
131*32689Sbostic 		refresh();
132*32689Sbostic 	}
133*32689Sbostic 	check_message();
134*32689Sbostic 	if (add_blank) {
135*32689Sbostic 		buf[i++] = ' ';
136*32689Sbostic 	} else {
137*32689Sbostic 		while ((i > 0) && (buf[i-1] == ' ')) {
138*32689Sbostic 			i--;
139*32689Sbostic 		}
140*32689Sbostic 	}
141*32689Sbostic 
142*32689Sbostic 	buf[i] = 0;
143*32689Sbostic 
144*32689Sbostic 	if ((ch == CANCEL) || (i == 0) || ((i == 1) && add_blank)) {
145*32689Sbostic 		if (if_cancelled) {
146*32689Sbostic 			message(if_cancelled, 0);
147*32689Sbostic 		}
148*32689Sbostic 		return(0);
149*32689Sbostic 	}
150*32689Sbostic 	return(i);
151*32689Sbostic }
152*32689Sbostic 
153*32689Sbostic rgetchar()
154*32689Sbostic {
155*32689Sbostic 	register ch;
156*32689Sbostic 
157*32689Sbostic 	for(;;) {
158*32689Sbostic 		ch = getchar();
159*32689Sbostic 
160*32689Sbostic 		switch(ch) {
161*32689Sbostic 		case '\022':
162*32689Sbostic 			wrefresh(curscr);
163*32689Sbostic 			break;
164*32689Sbostic #ifdef UNIX_BSD4_2
165*32689Sbostic 		case '\032':
166*32689Sbostic 			printf(CL);
167*32689Sbostic 			fflush(stdout);
168*32689Sbostic 			tstp();
169*32689Sbostic 			break;
170*32689Sbostic #endif
171*32689Sbostic 		case '&':
172*32689Sbostic 			save_screen();
173*32689Sbostic 			break;
174*32689Sbostic 		default:
175*32689Sbostic 			return(ch);
176*32689Sbostic 		}
177*32689Sbostic 	}
178*32689Sbostic }
179*32689Sbostic /*
180*32689Sbostic Level: 99 Gold: 999999 Hp: 999(999) Str: 99(99) Arm: 99 Exp: 21/10000000 Hungry
181*32689Sbostic 0    5    1    5    2    5    3    5    4    5    5    5    6    5    7    5
182*32689Sbostic */
183*32689Sbostic 
184*32689Sbostic print_stats(stat_mask)
185*32689Sbostic register stat_mask;
186*32689Sbostic {
187*32689Sbostic 	char buf[16];
188*32689Sbostic 	boolean label;
189*32689Sbostic 	int row = DROWS - 1;
190*32689Sbostic 
191*32689Sbostic 	label = (stat_mask & STAT_LABEL) ? 1 : 0;
192*32689Sbostic 
193*32689Sbostic 	if (stat_mask & STAT_LEVEL) {
194*32689Sbostic 		if (label) {
195*32689Sbostic 			mvaddstr(row, 0, "Level: ");
196*32689Sbostic 		}
197*32689Sbostic 		/* max level taken care of in make_level() */
198*32689Sbostic 		sprintf(buf, "%d", cur_level);
199*32689Sbostic 		mvaddstr(row, 7, buf);
200*32689Sbostic 		pad(buf, 2);
201*32689Sbostic 	}
202*32689Sbostic 	if (stat_mask & STAT_GOLD) {
203*32689Sbostic 		if (label) {
204*32689Sbostic 			mvaddstr(row, 10, "Gold: ");
205*32689Sbostic 		}
206*32689Sbostic 		if (rogue.gold > MAX_GOLD) {
207*32689Sbostic 			rogue.gold = MAX_GOLD;
208*32689Sbostic 		}
209*32689Sbostic 		sprintf(buf, "%ld", rogue.gold);
210*32689Sbostic 		mvaddstr(row, 16, buf);
211*32689Sbostic 		pad(buf, 6);
212*32689Sbostic 	}
213*32689Sbostic 	if (stat_mask & STAT_HP) {
214*32689Sbostic 		if (label) {
215*32689Sbostic 			mvaddstr(row, 23, "Hp: ");
216*32689Sbostic 		}
217*32689Sbostic 		if (rogue.hp_max > MAX_HP) {
218*32689Sbostic 			rogue.hp_current -= (rogue.hp_max - MAX_HP);
219*32689Sbostic 			rogue.hp_max = MAX_HP;
220*32689Sbostic 		}
221*32689Sbostic 		sprintf(buf, "%d(%d)", rogue.hp_current, rogue.hp_max);
222*32689Sbostic 		mvaddstr(row, 27, buf);
223*32689Sbostic 		pad(buf, 8);
224*32689Sbostic 	}
225*32689Sbostic 	if (stat_mask & STAT_STRENGTH) {
226*32689Sbostic 		if (label) {
227*32689Sbostic 			mvaddstr(row, 36, "Str: ");
228*32689Sbostic 		}
229*32689Sbostic 		if (rogue.str_max > MAX_STRENGTH) {
230*32689Sbostic 			rogue.str_current -= (rogue.str_max - MAX_STRENGTH);
231*32689Sbostic 			rogue.str_max = MAX_STRENGTH;
232*32689Sbostic 		}
233*32689Sbostic 		sprintf(buf, "%d(%d)", (rogue.str_current + add_strength),
234*32689Sbostic 			rogue.str_max);
235*32689Sbostic 		mvaddstr(row, 41, buf);
236*32689Sbostic 		pad(buf, 6);
237*32689Sbostic 	}
238*32689Sbostic 	if (stat_mask & STAT_ARMOR) {
239*32689Sbostic 		if (label) {
240*32689Sbostic 			mvaddstr(row, 48, "Arm: ");
241*32689Sbostic 		}
242*32689Sbostic 		if (rogue.armor && (rogue.armor->d_enchant > MAX_ARMOR)) {
243*32689Sbostic 			rogue.armor->d_enchant = MAX_ARMOR;
244*32689Sbostic 		}
245*32689Sbostic 		sprintf(buf, "%d", get_armor_class(rogue.armor));
246*32689Sbostic 		mvaddstr(row, 53, buf);
247*32689Sbostic 		pad(buf, 2);
248*32689Sbostic 	}
249*32689Sbostic 	if (stat_mask & STAT_EXP) {
250*32689Sbostic 		if (label) {
251*32689Sbostic 			mvaddstr(row, 56, "Exp: ");
252*32689Sbostic 		}
253*32689Sbostic 		if (rogue.exp_points > MAX_EXP) {
254*32689Sbostic 			rogue.exp_points = MAX_EXP;
255*32689Sbostic 		}
256*32689Sbostic 		if (rogue.exp > MAX_EXP_LEVEL) {
257*32689Sbostic 			rogue.exp = MAX_EXP_LEVEL;
258*32689Sbostic 		}
259*32689Sbostic 		sprintf(buf, "%d/%ld", rogue.exp, rogue.exp_points);
260*32689Sbostic 		mvaddstr(row, 61, buf);
261*32689Sbostic 		pad(buf, 11);
262*32689Sbostic 	}
263*32689Sbostic 	if (stat_mask & STAT_HUNGER) {
264*32689Sbostic 		mvaddstr(row, 73, hunger_str);
265*32689Sbostic 		clrtoeol();
266*32689Sbostic 	}
267*32689Sbostic 	refresh();
268*32689Sbostic }
269*32689Sbostic 
270*32689Sbostic pad(s, n)
271*32689Sbostic char *s;
272*32689Sbostic short n;
273*32689Sbostic {
274*32689Sbostic 	short i;
275*32689Sbostic 
276*32689Sbostic 	for (i = strlen(s); i < n; i++) {
277*32689Sbostic 		addch(' ');
278*32689Sbostic 	}
279*32689Sbostic }
280*32689Sbostic 
281*32689Sbostic save_screen()
282*32689Sbostic {
283*32689Sbostic 	FILE *fp;
284*32689Sbostic 	short i, j;
285*32689Sbostic 	char buf[DCOLS+2];
286*32689Sbostic 	boolean found_non_blank;
287*32689Sbostic 
288*32689Sbostic 	if ((fp = fopen("rogue.screen", "w")) != NULL) {
289*32689Sbostic 		for (i = 0; i < DROWS; i++) {
290*32689Sbostic 			found_non_blank = 0;
291*32689Sbostic 			for (j = (DCOLS - 1); j >= 0; j--) {
292*32689Sbostic 				buf[j] = mvinch(i, j);
293*32689Sbostic 				if (!found_non_blank) {
294*32689Sbostic 					if ((buf[j] != ' ') || (j == 0)) {
295*32689Sbostic 						buf[j + ((j == 0) ? 0 : 1)] = 0;
296*32689Sbostic 						found_non_blank = 1;
297*32689Sbostic 					}
298*32689Sbostic 				}
299*32689Sbostic 			}
300*32689Sbostic 			fputs(buf, fp);
301*32689Sbostic 			putc('\n', fp);
302*32689Sbostic 		}
303*32689Sbostic 		fclose(fp);
304*32689Sbostic 	} else {
305*32689Sbostic 		sound_bell();
306*32689Sbostic 	}
307*32689Sbostic }
308*32689Sbostic 
309*32689Sbostic sound_bell()
310*32689Sbostic {
311*32689Sbostic 	putchar(7);
312*32689Sbostic 	fflush(stdout);
313*32689Sbostic }
314*32689Sbostic 
315*32689Sbostic boolean
316*32689Sbostic is_digit(ch)
317*32689Sbostic short ch;
318*32689Sbostic {
319*32689Sbostic 	return((ch >= '0') && (ch <= '9'));
320*32689Sbostic }
321*32689Sbostic 
322*32689Sbostic r_index(str, ch, last)
323*32689Sbostic char *str;
324*32689Sbostic int ch;
325*32689Sbostic boolean last;
326*32689Sbostic {
327*32689Sbostic 	int i = 0;
328*32689Sbostic 
329*32689Sbostic 	if (last) {
330*32689Sbostic 		for (i = strlen(str) - 1; i >= 0; i--) {
331*32689Sbostic 			if (str[i] == ch) {
332*32689Sbostic 				return(i);
333*32689Sbostic 			}
334*32689Sbostic 		}
335*32689Sbostic 	} else {
336*32689Sbostic 		for (i = 0; str[i]; i++) {
337*32689Sbostic 			if (str[i] == ch) {
338*32689Sbostic 				return(i);
339*32689Sbostic 			}
340*32689Sbostic 		}
341*32689Sbostic 	}
342*32689Sbostic 	return(-1);
343*32689Sbostic }
344