xref: /netbsd-src/games/rogue/message.c (revision ae1bfcddc410612bc8c58b807e1830becb69a24c)
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";*/
39 static char rcsid[] = "$Id: message.c,v 1.4 1993/11/10 10:02:19 cgd 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 <termios.h>
56 #include <signal.h>
57 #include "rogue.h"
58 
59 char msgs[NMESSAGES][DCOLS] = {"", "", "", "", ""};
60 short msg_col = 0, imsg = -1;
61 boolean msg_cleared = 1, rmsg = 0;
62 char hunger_str[8] = "";
63 char *more = "-more-";
64 
65 extern boolean cant_int, did_int, interrupted, save_is_interactive;
66 extern short add_strength;
67 extern short cur_level;
68 
69 message(msg, intrpt)
70 char *msg;
71 boolean intrpt;
72 {
73 	cant_int = 1;
74 
75 	if (!save_is_interactive) {
76 		return;
77 	}
78 	if (intrpt) {
79 		interrupted = 1;
80 		md_slurp();
81 	}
82 
83 	if (!msg_cleared) {
84 		mvaddstr(MIN_ROW-1, msg_col, more);
85 		refresh();
86 		wait_for_ack();
87 		check_message();
88 	}
89 	if (!rmsg) {
90 		imsg = (imsg + 1) % NMESSAGES;
91 		(void) strcpy(msgs[imsg], msg);
92 	}
93 	mvaddstr(MIN_ROW-1, 0, msg);
94 	addch(' ');
95 	refresh();
96 	msg_cleared = 0;
97 	msg_col = strlen(msg);
98 
99 	cant_int = 0;
100 
101 	if (did_int) {
102 		did_int = 0;
103 		onintr();
104 	}
105 }
106 
107 remessage(c)
108 short c;
109 {
110 	if (imsg != -1) {
111 		check_message();
112 		rmsg = 1;
113 		while (c > imsg) {
114 			c -= NMESSAGES;
115 		}
116 		message(msgs[((imsg - c) % NMESSAGES)], 0);
117 		rmsg = 0;
118 		move(rogue.row, rogue.col);
119 		refresh();
120 	}
121 }
122 
123 check_message()
124 {
125 	if (msg_cleared) {
126 		return;
127 	}
128 	move(MIN_ROW-1, 0);
129 	clrtoeol();
130 	refresh();
131 	msg_cleared = 1;
132 }
133 
134 get_input_line(prompt, insert, buf, if_cancelled, add_blank, do_echo)
135 char *prompt, *buf, *insert;
136 char *if_cancelled;
137 boolean add_blank;
138 boolean do_echo;
139 {
140 	short ch;
141 	short i = 0, n;
142 
143 	message(prompt, 0);
144 	n = strlen(prompt);
145 
146 	if (insert[0]) {
147 		mvaddstr(0, n + 1, insert);
148 		(void) strcpy(buf, insert);
149 		i = strlen(insert);
150 		move(0, (n + i + 1));
151 		refresh();
152 	}
153 
154 	while (((ch = rgetchar()) != '\r') && (ch != '\n') && (ch != CANCEL)) {
155 		if ((ch >= ' ') && (ch <= '~') && (i < MAX_TITLE_LENGTH-2)) {
156 			if ((ch != ' ') || (i > 0)) {
157 				buf[i++] = ch;
158 				if (do_echo) {
159 					addch(ch);
160 				}
161 			}
162 		}
163 		if ((ch == '\b') && (i > 0)) {
164 			if (do_echo) {
165 				mvaddch(0, i + n, ' ');
166 				move(MIN_ROW-1, i+n);
167 			}
168 			i--;
169 		}
170 		refresh();
171 	}
172 	check_message();
173 	if (add_blank) {
174 		buf[i++] = ' ';
175 	} else {
176 		while ((i > 0) && (buf[i-1] == ' ')) {
177 			i--;
178 		}
179 	}
180 
181 	buf[i] = 0;
182 
183 	if ((ch == CANCEL) || (i == 0) || ((i == 1) && add_blank)) {
184 		if (if_cancelled) {
185 			message(if_cancelled, 0);
186 		}
187 		return(0);
188 	}
189 	return(i);
190 }
191 
192 rgetchar()
193 {
194 	register ch;
195 
196 	for(;;) {
197 		ch = getchar();
198 
199 		switch(ch) {
200 		case '\022':
201 			wrefresh(curscr);
202 			break;
203 #ifdef UNIX_BSD4_2
204 		case '\032':
205 			printf(CL);
206 			fflush(stdout);
207 			tstp();
208 			break;
209 #endif
210 		case '&':
211 			save_screen();
212 			break;
213 		default:
214 			return(ch);
215 		}
216 	}
217 }
218 /*
219 Level: 99 Gold: 999999 Hp: 999(999) Str: 99(99) Arm: 99 Exp: 21/10000000 Hungry
220 0    5    1    5    2    5    3    5    4    5    5    5    6    5    7    5
221 */
222 
223 print_stats(stat_mask)
224 register stat_mask;
225 {
226 	char buf[16];
227 	boolean label;
228 	int row = DROWS - 1;
229 
230 	label = (stat_mask & STAT_LABEL) ? 1 : 0;
231 
232 	if (stat_mask & STAT_LEVEL) {
233 		if (label) {
234 			mvaddstr(row, 0, "Level: ");
235 		}
236 		/* max level taken care of in make_level() */
237 		sprintf(buf, "%d", cur_level);
238 		mvaddstr(row, 7, buf);
239 		pad(buf, 2);
240 	}
241 	if (stat_mask & STAT_GOLD) {
242 		if (label) {
243 			mvaddstr(row, 10, "Gold: ");
244 		}
245 		if (rogue.gold > MAX_GOLD) {
246 			rogue.gold = MAX_GOLD;
247 		}
248 		sprintf(buf, "%ld", rogue.gold);
249 		mvaddstr(row, 16, buf);
250 		pad(buf, 6);
251 	}
252 	if (stat_mask & STAT_HP) {
253 		if (label) {
254 			mvaddstr(row, 23, "Hp: ");
255 		}
256 		if (rogue.hp_max > MAX_HP) {
257 			rogue.hp_current -= (rogue.hp_max - MAX_HP);
258 			rogue.hp_max = MAX_HP;
259 		}
260 		sprintf(buf, "%d(%d)", rogue.hp_current, rogue.hp_max);
261 		mvaddstr(row, 27, buf);
262 		pad(buf, 8);
263 	}
264 	if (stat_mask & STAT_STRENGTH) {
265 		if (label) {
266 			mvaddstr(row, 36, "Str: ");
267 		}
268 		if (rogue.str_max > MAX_STRENGTH) {
269 			rogue.str_current -= (rogue.str_max - MAX_STRENGTH);
270 			rogue.str_max = MAX_STRENGTH;
271 		}
272 		sprintf(buf, "%d(%d)", (rogue.str_current + add_strength),
273 			rogue.str_max);
274 		mvaddstr(row, 41, buf);
275 		pad(buf, 6);
276 	}
277 	if (stat_mask & STAT_ARMOR) {
278 		if (label) {
279 			mvaddstr(row, 48, "Arm: ");
280 		}
281 		if (rogue.armor && (rogue.armor->d_enchant > MAX_ARMOR)) {
282 			rogue.armor->d_enchant = MAX_ARMOR;
283 		}
284 		sprintf(buf, "%d", get_armor_class(rogue.armor));
285 		mvaddstr(row, 53, buf);
286 		pad(buf, 2);
287 	}
288 	if (stat_mask & STAT_EXP) {
289 		if (label) {
290 			mvaddstr(row, 56, "Exp: ");
291 		}
292 		if (rogue.exp_points > MAX_EXP) {
293 			rogue.exp_points = MAX_EXP;
294 		}
295 		if (rogue.exp > MAX_EXP_LEVEL) {
296 			rogue.exp = MAX_EXP_LEVEL;
297 		}
298 		sprintf(buf, "%d/%ld", rogue.exp, rogue.exp_points);
299 		mvaddstr(row, 61, buf);
300 		pad(buf, 11);
301 	}
302 	if (stat_mask & STAT_HUNGER) {
303 		mvaddstr(row, 73, hunger_str);
304 		clrtoeol();
305 	}
306 	refresh();
307 }
308 
309 pad(s, n)
310 char *s;
311 short n;
312 {
313 	short i;
314 
315 	for (i = strlen(s); i < n; i++) {
316 		addch(' ');
317 	}
318 }
319 
320 save_screen()
321 {
322 	FILE *fp;
323 	short i, j;
324 	char buf[DCOLS+2];
325 	boolean found_non_blank;
326 
327 	if ((fp = fopen("rogue.screen", "w")) != NULL) {
328 		for (i = 0; i < DROWS; i++) {
329 			found_non_blank = 0;
330 			for (j = (DCOLS - 1); j >= 0; j--) {
331 				buf[j] = mvinch(i, j);
332 				if (!found_non_blank) {
333 					if ((buf[j] != ' ') || (j == 0)) {
334 						buf[j + ((j == 0) ? 0 : 1)] = 0;
335 						found_non_blank = 1;
336 					}
337 				}
338 			}
339 			fputs(buf, fp);
340 			putc('\n', fp);
341 		}
342 		fclose(fp);
343 	} else {
344 		sound_bell();
345 	}
346 }
347 
348 sound_bell()
349 {
350 	putchar(7);
351 	fflush(stdout);
352 }
353 
354 boolean
355 is_digit(ch)
356 short ch;
357 {
358 	return((ch >= '0') && (ch <= '9'));
359 }
360 
361 r_index(str, ch, last)
362 char *str;
363 int ch;
364 boolean last;
365 {
366 	int i = 0;
367 
368 	if (last) {
369 		for (i = strlen(str) - 1; i >= 0; i--) {
370 			if (str[i] == ch) {
371 				return(i);
372 			}
373 		}
374 	} else {
375 		for (i = 0; str[i]; i++) {
376 			if (str[i] == ch) {
377 				return(i);
378 			}
379 		}
380 	}
381 	return(-1);
382 }
383