xref: /netbsd/games/rogue/message.c (revision c4a72b64)
1 /*	$NetBSD: message.c,v 1.9 2002/10/01 14:18:57 mrg 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
42 static char sccsid[] = "@(#)message.c	8.1 (Berkeley) 5/31/93";
43 #else
44 __RCSID("$NetBSD: message.c,v 1.9 2002/10/01 14:18:57 mrg 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 
64 char msgs[NMESSAGES][DCOLS] = {"", "", "", "", ""};
65 short msg_col = 0, imsg = -1;
66 boolean msg_cleared = 1, rmsg = 0;
67 char hunger_str[HUNGER_STR_LEN] = "";
68 const char *more = "-more-";
69 
70 void
71 message(msg, intrpt)
72 	const 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 
109 void
110 remessage(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 
126 void
127 check_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 
138 int
139 get_input_line(prompt, insert, buf, if_cancelled, add_blank, do_echo)
140 	const char *prompt, *insert;
141 	char *buf;
142 	const char *if_cancelled;
143 	boolean add_blank;
144 	boolean do_echo;
145 {
146 	short ch;
147 	short i = 0, n;
148 
149 	message(prompt, 0);
150 	n = strlen(prompt);
151 
152 	if (insert[0]) {
153 		mvaddstr(0, n + 1, insert);
154 		(void) strcpy(buf, insert);
155 		i = strlen(insert);
156 		move(0, (n + i + 1));
157 		refresh();
158 	}
159 
160 	while (((ch = rgetchar()) != '\r') && (ch != '\n') && (ch != CANCEL)) {
161 		if ((ch >= ' ') && (ch <= '~') && (i < MAX_TITLE_LENGTH-2)) {
162 			if ((ch != ' ') || (i > 0)) {
163 				buf[i++] = ch;
164 				if (do_echo) {
165 					addch(ch);
166 				}
167 			}
168 		}
169 		if ((ch == '\b') && (i > 0)) {
170 			if (do_echo) {
171 				mvaddch(0, i + n, ' ');
172 				move(MIN_ROW-1, i+n);
173 			}
174 			i--;
175 		}
176 		refresh();
177 	}
178 	check_message();
179 	if (add_blank) {
180 		buf[i++] = ' ';
181 	} else {
182 		while ((i > 0) && (buf[i-1] == ' ')) {
183 			i--;
184 		}
185 	}
186 
187 	buf[i] = 0;
188 
189 	if ((ch == CANCEL) || (i == 0) || ((i == 1) && add_blank)) {
190 		if (if_cancelled) {
191 			message(if_cancelled, 0);
192 		}
193 		return(0);
194 	}
195 	return(i);
196 }
197 
198 int
199 rgetchar()
200 {
201 	int ch;
202 
203 	for(;;) {
204 		ch = getchar();
205 
206 		switch(ch) {
207 		case '\022':
208 			wrefresh(curscr);
209 			break;
210 #ifdef UNIX_BSD4_2
211 		case '\032':
212 			printf("%s", CL);
213 			fflush(stdout);
214 			tstp();
215 			break;
216 #endif
217 		case '&':
218 			save_screen();
219 			break;
220 		default:
221 			return(ch);
222 		}
223 	}
224 }
225 
226 /*
227 Level: 99 Gold: 999999 Hp: 999(999) Str: 99(99) Arm: 99 Exp: 21/10000000 Hungry
228 0    5    1    5    2    5    3    5    4    5    5    5    6    5    7    5
229 */
230 
231 void
232 print_stats(stat_mask)
233 	int stat_mask;
234 {
235 	char buf[16];
236 	boolean label;
237 	int row = DROWS - 1;
238 
239 	label = (stat_mask & STAT_LABEL) ? 1 : 0;
240 
241 	if (stat_mask & STAT_LEVEL) {
242 		if (label) {
243 			mvaddstr(row, 0, "Level: ");
244 		}
245 		/* max level taken care of in make_level() */
246 		sprintf(buf, "%d", cur_level);
247 		mvaddstr(row, 7, buf);
248 		pad(buf, 2);
249 	}
250 	if (stat_mask & STAT_GOLD) {
251 		if (label) {
252 			mvaddstr(row, 10, "Gold: ");
253 		}
254 		if (rogue.gold > MAX_GOLD) {
255 			rogue.gold = MAX_GOLD;
256 		}
257 		sprintf(buf, "%ld", rogue.gold);
258 		mvaddstr(row, 16, buf);
259 		pad(buf, 6);
260 	}
261 	if (stat_mask & STAT_HP) {
262 		if (label) {
263 			mvaddstr(row, 23, "Hp: ");
264 		}
265 		if (rogue.hp_max > MAX_HP) {
266 			rogue.hp_current -= (rogue.hp_max - MAX_HP);
267 			rogue.hp_max = MAX_HP;
268 		}
269 		sprintf(buf, "%d(%d)", rogue.hp_current, rogue.hp_max);
270 		mvaddstr(row, 27, buf);
271 		pad(buf, 8);
272 	}
273 	if (stat_mask & STAT_STRENGTH) {
274 		if (label) {
275 			mvaddstr(row, 36, "Str: ");
276 		}
277 		if (rogue.str_max > MAX_STRENGTH) {
278 			rogue.str_current -= (rogue.str_max - MAX_STRENGTH);
279 			rogue.str_max = MAX_STRENGTH;
280 		}
281 		sprintf(buf, "%d(%d)", (rogue.str_current + add_strength),
282 			rogue.str_max);
283 		mvaddstr(row, 41, buf);
284 		pad(buf, 6);
285 	}
286 	if (stat_mask & STAT_ARMOR) {
287 		if (label) {
288 			mvaddstr(row, 48, "Arm: ");
289 		}
290 		if (rogue.armor && (rogue.armor->d_enchant > MAX_ARMOR)) {
291 			rogue.armor->d_enchant = MAX_ARMOR;
292 		}
293 		sprintf(buf, "%d", get_armor_class(rogue.armor));
294 		mvaddstr(row, 53, buf);
295 		pad(buf, 2);
296 	}
297 	if (stat_mask & STAT_EXP) {
298 		if (label) {
299 			mvaddstr(row, 56, "Exp: ");
300 		}
301 		if (rogue.exp_points > MAX_EXP) {
302 			rogue.exp_points = MAX_EXP;
303 		}
304 		if (rogue.exp > MAX_EXP_LEVEL) {
305 			rogue.exp = MAX_EXP_LEVEL;
306 		}
307 		sprintf(buf, "%d/%ld", rogue.exp, rogue.exp_points);
308 		mvaddstr(row, 61, buf);
309 		pad(buf, 11);
310 	}
311 	if (stat_mask & STAT_HUNGER) {
312 		mvaddstr(row, 73, hunger_str);
313 		clrtoeol();
314 	}
315 	refresh();
316 }
317 
318 void
319 pad(s, n)
320 	const char *s;
321 	short n;
322 {
323 	short i;
324 
325 	for (i = strlen(s); i < n; i++) {
326 		addch(' ');
327 	}
328 }
329 
330 void
331 save_screen()
332 {
333 	FILE *fp;
334 	short i, j;
335 	char buf[DCOLS+2];
336 	boolean found_non_blank;
337 
338 	if ((fp = fopen("rogue.screen", "w")) != NULL) {
339 		for (i = 0; i < DROWS; i++) {
340 			found_non_blank = 0;
341 			for (j = (DCOLS - 1); j >= 0; j--) {
342 				buf[j] = mvinch(i, j);
343 				if (!found_non_blank) {
344 					if ((buf[j] != ' ') || (j == 0)) {
345 						buf[j + ((j == 0) ? 0 : 1)] = 0;
346 						found_non_blank = 1;
347 					}
348 				}
349 			}
350 			fputs(buf, fp);
351 			putc('\n', fp);
352 		}
353 		fclose(fp);
354 	} else {
355 		sound_bell();
356 	}
357 }
358 
359 void
360 sound_bell()
361 {
362 	putchar(7);
363 	fflush(stdout);
364 }
365 
366 boolean
367 is_digit(ch)
368 	short ch;
369 {
370 	return((ch >= '0') && (ch <= '9'));
371 }
372 
373 int
374 r_index(str, ch, last)
375 	const char *str;
376 	int ch;
377 	boolean last;
378 {
379 	int i = 0;
380 
381 	if (last) {
382 		for (i = strlen(str) - 1; i >= 0; i--) {
383 			if (str[i] == ch) {
384 				return(i);
385 			}
386 		}
387 	} else {
388 		for (i = 0; str[i]; i++) {
389 			if (str[i] == ch) {
390 				return(i);
391 			}
392 		}
393 	}
394 	return(-1);
395 }
396