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