message.c revision 1.2
1/*
2 * Copyright (c) 1988 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Timothy C. Stoehr.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38/*static char sccsid[] = "from: @(#)message.c	5.3 (Berkeley) 6/1/90";*/
39static char rcsid[] = "$Id: message.c,v 1.2 1993/08/01 18:52:24 mycroft Exp $";
40#endif /* not lint */
41
42/*
43 * message.c
44 *
45 * This source herein may be modified and/or distributed by anybody who
46 * so desires, with the following restrictions:
47 *    1.)  No portion of this notice shall be removed.
48 *    2.)  Credit shall not be taken for the creation of this source.
49 *    3.)  This code is not to be traded, sold, or used for personal
50 *         gain or profit.
51 *
52 */
53
54#include <stdio.h>
55#include "rogue.h"
56
57char msgs[NMESSAGES][DCOLS] = {"", "", "", "", ""};
58short msg_col = 0, imsg = -1;
59boolean msg_cleared = 1, rmsg = 0;
60char hunger_str[8] = "";
61char *more = "-more-";
62
63extern boolean cant_int, did_int, interrupted, save_is_interactive;
64extern short add_strength;
65extern short cur_level;
66
67message(msg, intrpt)
68char *msg;
69boolean intrpt;
70{
71	cant_int = 1;
72
73	if (!save_is_interactive) {
74		return;
75	}
76	if (intrpt) {
77		interrupted = 1;
78		md_slurp();
79	}
80
81	if (!msg_cleared) {
82		mvaddstr(MIN_ROW-1, msg_col, more);
83		refresh();
84		wait_for_ack();
85		check_message();
86	}
87	if (!rmsg) {
88		imsg = (imsg + 1) % NMESSAGES;
89		(void) strcpy(msgs[imsg], msg);
90	}
91	mvaddstr(MIN_ROW-1, 0, msg);
92	addch(' ');
93	refresh();
94	msg_cleared = 0;
95	msg_col = strlen(msg);
96
97	cant_int = 0;
98
99	if (did_int) {
100		did_int = 0;
101		onintr();
102	}
103}
104
105remessage(c)
106short c;
107{
108	if (imsg != -1) {
109		check_message();
110		rmsg = 1;
111		while (c > imsg) {
112			c -= NMESSAGES;
113		}
114		message(msgs[((imsg - c) % NMESSAGES)], 0);
115		rmsg = 0;
116		move(rogue.row, rogue.col);
117		refresh();
118	}
119}
120
121check_message()
122{
123	if (msg_cleared) {
124		return;
125	}
126	move(MIN_ROW-1, 0);
127	clrtoeol();
128	refresh();
129	msg_cleared = 1;
130}
131
132get_input_line(prompt, insert, buf, if_cancelled, add_blank, do_echo)
133char *prompt, *buf, *insert;
134char *if_cancelled;
135boolean add_blank;
136boolean do_echo;
137{
138	short ch;
139	short i = 0, n;
140
141	message(prompt, 0);
142	n = strlen(prompt);
143
144	if (insert[0]) {
145		mvaddstr(0, n + 1, insert);
146		(void) strcpy(buf, insert);
147		i = strlen(insert);
148		move(0, (n + i + 1));
149		refresh();
150	}
151
152	while (((ch = rgetchar()) != '\r') && (ch != '\n') && (ch != CANCEL)) {
153		if ((ch >= ' ') && (ch <= '~') && (i < MAX_TITLE_LENGTH-2)) {
154			if ((ch != ' ') || (i > 0)) {
155				buf[i++] = ch;
156				if (do_echo) {
157					addch(ch);
158				}
159			}
160		}
161		if ((ch == '\b') && (i > 0)) {
162			if (do_echo) {
163				mvaddch(0, i + n, ' ');
164				move(MIN_ROW-1, i+n);
165			}
166			i--;
167		}
168		refresh();
169	}
170	check_message();
171	if (add_blank) {
172		buf[i++] = ' ';
173	} else {
174		while ((i > 0) && (buf[i-1] == ' ')) {
175			i--;
176		}
177	}
178
179	buf[i] = 0;
180
181	if ((ch == CANCEL) || (i == 0) || ((i == 1) && add_blank)) {
182		if (if_cancelled) {
183			message(if_cancelled, 0);
184		}
185		return(0);
186	}
187	return(i);
188}
189
190rgetchar()
191{
192	register ch;
193
194	for(;;) {
195		ch = getchar();
196
197		switch(ch) {
198		case '\022':
199			wrefresh(curscr);
200			break;
201#ifdef UNIX_BSD4_2
202		case '\032':
203			printf(CL);
204			fflush(stdout);
205			tstp();
206			break;
207#endif
208		case '&':
209			save_screen();
210			break;
211		default:
212			return(ch);
213		}
214	}
215}
216/*
217Level: 99 Gold: 999999 Hp: 999(999) Str: 99(99) Arm: 99 Exp: 21/10000000 Hungry
2180    5    1    5    2    5    3    5    4    5    5    5    6    5    7    5
219*/
220
221print_stats(stat_mask)
222register stat_mask;
223{
224	char buf[16];
225	boolean label;
226	int row = DROWS - 1;
227
228	label = (stat_mask & STAT_LABEL) ? 1 : 0;
229
230	if (stat_mask & STAT_LEVEL) {
231		if (label) {
232			mvaddstr(row, 0, "Level: ");
233		}
234		/* max level taken care of in make_level() */
235		sprintf(buf, "%d", cur_level);
236		mvaddstr(row, 7, buf);
237		pad(buf, 2);
238	}
239	if (stat_mask & STAT_GOLD) {
240		if (label) {
241			mvaddstr(row, 10, "Gold: ");
242		}
243		if (rogue.gold > MAX_GOLD) {
244			rogue.gold = MAX_GOLD;
245		}
246		sprintf(buf, "%ld", rogue.gold);
247		mvaddstr(row, 16, buf);
248		pad(buf, 6);
249	}
250	if (stat_mask & STAT_HP) {
251		if (label) {
252			mvaddstr(row, 23, "Hp: ");
253		}
254		if (rogue.hp_max > MAX_HP) {
255			rogue.hp_current -= (rogue.hp_max - MAX_HP);
256			rogue.hp_max = MAX_HP;
257		}
258		sprintf(buf, "%d(%d)", rogue.hp_current, rogue.hp_max);
259		mvaddstr(row, 27, buf);
260		pad(buf, 8);
261	}
262	if (stat_mask & STAT_STRENGTH) {
263		if (label) {
264			mvaddstr(row, 36, "Str: ");
265		}
266		if (rogue.str_max > MAX_STRENGTH) {
267			rogue.str_current -= (rogue.str_max - MAX_STRENGTH);
268			rogue.str_max = MAX_STRENGTH;
269		}
270		sprintf(buf, "%d(%d)", (rogue.str_current + add_strength),
271			rogue.str_max);
272		mvaddstr(row, 41, buf);
273		pad(buf, 6);
274	}
275	if (stat_mask & STAT_ARMOR) {
276		if (label) {
277			mvaddstr(row, 48, "Arm: ");
278		}
279		if (rogue.armor && (rogue.armor->d_enchant > MAX_ARMOR)) {
280			rogue.armor->d_enchant = MAX_ARMOR;
281		}
282		sprintf(buf, "%d", get_armor_class(rogue.armor));
283		mvaddstr(row, 53, buf);
284		pad(buf, 2);
285	}
286	if (stat_mask & STAT_EXP) {
287		if (label) {
288			mvaddstr(row, 56, "Exp: ");
289		}
290		if (rogue.exp_points > MAX_EXP) {
291			rogue.exp_points = MAX_EXP;
292		}
293		if (rogue.exp > MAX_EXP_LEVEL) {
294			rogue.exp = MAX_EXP_LEVEL;
295		}
296		sprintf(buf, "%d/%ld", rogue.exp, rogue.exp_points);
297		mvaddstr(row, 61, buf);
298		pad(buf, 11);
299	}
300	if (stat_mask & STAT_HUNGER) {
301		mvaddstr(row, 73, hunger_str);
302		clrtoeol();
303	}
304	refresh();
305}
306
307pad(s, n)
308char *s;
309short n;
310{
311	short i;
312
313	for (i = strlen(s); i < n; i++) {
314		addch(' ');
315	}
316}
317
318save_screen()
319{
320	FILE *fp;
321	short i, j;
322	char buf[DCOLS+2];
323	boolean found_non_blank;
324
325	if ((fp = fopen("rogue.screen", "w")) != NULL) {
326		for (i = 0; i < DROWS; i++) {
327			found_non_blank = 0;
328			for (j = (DCOLS - 1); j >= 0; j--) {
329				buf[j] = mvinch(i, j);
330				if (!found_non_blank) {
331					if ((buf[j] != ' ') || (j == 0)) {
332						buf[j + ((j == 0) ? 0 : 1)] = 0;
333						found_non_blank = 1;
334					}
335				}
336			}
337			fputs(buf, fp);
338			putc('\n', fp);
339		}
340		fclose(fp);
341	} else {
342		sound_bell();
343	}
344}
345
346sound_bell()
347{
348	putchar(7);
349	fflush(stdout);
350}
351
352boolean
353is_digit(ch)
354short ch;
355{
356	return((ch >= '0') && (ch <= '9'));
357}
358
359r_index(str, ch, last)
360char *str;
361int ch;
362boolean last;
363{
364	int i = 0;
365
366	if (last) {
367		for (i = strlen(str) - 1; i >= 0; i--) {
368			if (str[i] == ch) {
369				return(i);
370			}
371		}
372	} else {
373		for (i = 0; str[i]; i++) {
374			if (str[i] == ch) {
375				return(i);
376			}
377		}
378	}
379	return(-1);
380}
381