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