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