xref: /dragonfly/games/rogue/message.c (revision 333227be)
1 /*
2  * Copyright (c) 1988, 1993
3  *	The Regents of the University of California.  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  * @(#)message.c	8.1 (Berkeley) 5/31/93
37  * $FreeBSD: src/games/rogue/message.c,v 1.7.2.1 2000/07/20 10:35:07 kris Exp $
38  * $DragonFly: src/games/rogue/message.c,v 1.3 2003/08/26 23:52:50 drhodus Exp $
39  */
40 
41 /*
42  * message.c
43  *
44  * This source herein may be modified and/or distributed by anybody who
45  * so desires, with the following restrictions:
46  *    1.)  No portion of this notice shall be removed.
47  *    2.)  Credit shall not be taken for the creation of this source.
48  *    3.)  This code is not to be traded, sold, or used for personal
49  *         gain or profit.
50  *
51  */
52 
53 #include <stdio.h>
54 #include "rogue.h"
55 
56 char msgs[NMESSAGES][DCOLS] = {"", "", "", "", ""};
57 short msg_col = 0, imsg = -1;
58 boolean msg_cleared = 1, rmsg = 0;
59 char hunger_str[HUNGER_STR_LEN] = "";
60 const char *more = "-more-";
61 
62 extern boolean cant_int, did_int, interrupted, save_is_interactive, flush;
63 extern short add_strength;
64 extern short cur_level;
65 
66 message(msg, intrpt)
67 const char *msg;
68 boolean intrpt;
69 {
70 	cant_int = 1;
71 
72 	if (!save_is_interactive) {
73 		return;
74 	}
75 	if (intrpt) {
76 		interrupted = 1;
77 		if (flush)
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 
105 remessage(c)
106 short 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 
121 check_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 
132 get_input_line(prompt, insert, buf, if_cancelled, add_blank, do_echo)
133 const char *prompt, *insert;
134 char *buf;
135 const char *if_cancelled;
136 boolean add_blank;
137 boolean do_echo;
138 {
139 	short ch;
140 	short i = 0, n;
141 
142 	message(prompt, 0);
143 	n = strlen(prompt);
144 
145 	if (insert[0]) {
146 		mvaddstr(0, n + 1, insert);
147 		(void) strcpy(buf, insert);
148 		i = strlen(insert);
149 		move(0, (n + i + 1));
150 		refresh();
151 	}
152 
153 	while (((ch = rgetchar()) != '\r') && (ch != '\n') && (ch != CANCEL)) {
154 		if ((ch >= ' ') && (ch <= '~') && (i < MAX_TITLE_LENGTH-2)) {
155 			if ((ch != ' ') || (i > 0)) {
156 				buf[i++] = ch;
157 				if (do_echo) {
158 					addch(ch);
159 				}
160 			}
161 		}
162 		if ((ch == '\b') && (i > 0)) {
163 			if (do_echo) {
164 				mvaddch(0, i + n, ' ');
165 				move(MIN_ROW-1, i+n);
166 			}
167 			i--;
168 		}
169 		refresh();
170 	}
171 	check_message();
172 	if (add_blank) {
173 		buf[i++] = ' ';
174 	} else {
175 		while ((i > 0) && (buf[i-1] == ' ')) {
176 			i--;
177 		}
178 	}
179 
180 	buf[i] = 0;
181 
182 	if ((ch == CANCEL) || (i == 0) || ((i == 1) && add_blank)) {
183 		if (if_cancelled) {
184 			message(if_cancelled, 0);
185 		}
186 		return(0);
187 	}
188 	return(i);
189 }
190 
191 rgetchar()
192 {
193 	int ch;
194 
195 	for(;;) {
196 		ch = getchar();
197 
198 		switch(ch) {
199 		case '\022':
200 			wrefresh(curscr);
201 			break;
202 #ifdef UNIX_BSD4_2
203 		case '\032':
204 			printf("%s", CL);
205 			fflush(stdout);
206 			tstp();
207 			break;
208 #endif
209 		case '&':
210 			save_screen();
211 			break;
212 		default:
213 			return(ch);
214 		}
215 	}
216 }
217 /*
218 Level: 99 Gold: 999999 Hp: 999(999) Str: 99(99) Arm: 99 Exp: 21/10000000 Hungry
219 0    5    1    5    2    5    3    5    4    5    5    5    6    5    7    5
220 */
221 
222 print_stats(stat_mask)
223 int stat_mask;
224 {
225 	char buf[16];
226 	boolean label;
227 	int row = DROWS - 1;
228 
229 	label = (stat_mask & STAT_LABEL) ? 1 : 0;
230 
231 	if (stat_mask & STAT_LEVEL) {
232 		if (label) {
233 			mvaddstr(row, 0, "Level: ");
234 		}
235 		/* max level taken care of in make_level() */
236 		sprintf(buf, "%d", cur_level);
237 		mvaddstr(row, 7, buf);
238 		pad(buf, 2);
239 	}
240 	if (stat_mask & STAT_GOLD) {
241 		if (label) {
242 			mvaddstr(row, 10, "Gold: ");
243 		}
244 		if (rogue.gold > MAX_GOLD) {
245 			rogue.gold = MAX_GOLD;
246 		}
247 		sprintf(buf, "%ld", rogue.gold);
248 		mvaddstr(row, 16, buf);
249 		pad(buf, 6);
250 	}
251 	if (stat_mask & STAT_HP) {
252 		if (label) {
253 			mvaddstr(row, 23, "Hp: ");
254 		}
255 		if (rogue.hp_max > MAX_HP) {
256 			rogue.hp_current -= (rogue.hp_max - MAX_HP);
257 			rogue.hp_max = MAX_HP;
258 		}
259 		sprintf(buf, "%d(%d)", rogue.hp_current, rogue.hp_max);
260 		mvaddstr(row, 27, buf);
261 		pad(buf, 8);
262 	}
263 	if (stat_mask & STAT_STRENGTH) {
264 		if (label) {
265 			mvaddstr(row, 36, "Str: ");
266 		}
267 		if (rogue.str_max > MAX_STRENGTH) {
268 			rogue.str_current -= (rogue.str_max - MAX_STRENGTH);
269 			rogue.str_max = MAX_STRENGTH;
270 		}
271 		sprintf(buf, "%d(%d)", (rogue.str_current + add_strength),
272 			rogue.str_max);
273 		mvaddstr(row, 41, buf);
274 		pad(buf, 6);
275 	}
276 	if (stat_mask & STAT_ARMOR) {
277 		if (label) {
278 			mvaddstr(row, 48, "Arm: ");
279 		}
280 		if (rogue.armor && (rogue.armor->d_enchant > MAX_ARMOR)) {
281 			rogue.armor->d_enchant = MAX_ARMOR;
282 		}
283 		sprintf(buf, "%d", get_armor_class(rogue.armor));
284 		mvaddstr(row, 53, buf);
285 		pad(buf, 2);
286 	}
287 	if (stat_mask & STAT_EXP) {
288 		if (label) {
289 			mvaddstr(row, 56, "Exp: ");
290 		}
291 		if (rogue.exp_points > MAX_EXP) {
292 			rogue.exp_points = MAX_EXP;
293 		}
294 		if (rogue.exp > MAX_EXP_LEVEL) {
295 			rogue.exp = MAX_EXP_LEVEL;
296 		}
297 		sprintf(buf, "%d/%ld", rogue.exp, rogue.exp_points);
298 		mvaddstr(row, 61, buf);
299 		pad(buf, 11);
300 	}
301 	if (stat_mask & STAT_HUNGER) {
302 		mvaddstr(row, 73, hunger_str);
303 		clrtoeol();
304 	}
305 	refresh();
306 }
307 
308 pad(s, n)
309 const char *s;
310 short n;
311 {
312 	short i;
313 
314 	for (i = strlen(s); i < n; i++) {
315 		addch(' ');
316 	}
317 }
318 
319 save_screen()
320 {
321 	FILE *fp;
322 	short i, j;
323 	char buf[DCOLS+2];
324 	boolean found_non_blank;
325 
326 	if ((fp = fopen("rogue.screen", "w")) != NULL) {
327 		for (i = 0; i < DROWS; i++) {
328 			found_non_blank = 0;
329 			for (j = (DCOLS - 1); j >= 0; j--) {
330 				buf[j] = mvinch(i, j);
331 				if (!found_non_blank) {
332 					if ((buf[j] != ' ') || (j == 0)) {
333 						buf[j + ((j == 0) ? 0 : 1)] = 0;
334 						found_non_blank = 1;
335 					}
336 				}
337 			}
338 			fputs(buf, fp);
339 			putc('\n', fp);
340 		}
341 		fclose(fp);
342 	} else {
343 		sound_bell();
344 	}
345 }
346 
347 sound_bell()
348 {
349 	putchar(7);
350 	fflush(stdout);
351 }
352 
353 boolean
354 is_digit(ch)
355 short ch;
356 {
357 	return((ch >= '0') && (ch <= '9'));
358 }
359 
360 r_index(str, ch, last)
361 const char *str;
362 int ch;
363 boolean last;
364 {
365 	int i = 0;
366 
367 	if (last) {
368 		for (i = strlen(str) - 1; i >= 0; i--) {
369 			if (str[i] == ch) {
370 				return(i);
371 			}
372 		}
373 	} else {
374 		for (i = 0; str[i]; i++) {
375 			if (str[i] == ch) {
376 				return(i);
377 			}
378 		}
379 	}
380 	return(-1);
381 }
382