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