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