Terminals have changed a bit since 1981.
[rogue-pphs.git] / io.c
1 /*
2 * Various input/output functions
3 *
4 * @(#)io.c 3.10 (Berkeley) 6/15/81
5 *
6 * Rogue: Exploring the Dungeons of Doom
7 * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
8 * All rights reserved.
9 *
10 * See the file LICENSE.TXT for full copyright and licensing information.
11 */
12
13 #include "curses.h"
14 #include <ctype.h>
15 #include <stdarg.h>
16 #include <string.h>
17 #include "rogue.h"
18
19 /*
20 * msg:
21 * Display a message at the top of the screen.
22 */
23
24 static char msgbuf[BUFSIZ];
25 static int newpos = 0;
26
27 /*VARARGS1*/
28 msg(char *fmt, ...)
29 {
30 va_list ap;
31 /*
32 * if the string is "", just clear the line
33 */
34 if (*fmt == '\0')
35 {
36 wmove(cw, 0, 0);
37 wclrtoeol(cw);
38 mpos = 0;
39 return;
40 }
41 /*
42 * otherwise add to the message and flush it out
43 */
44 va_start(ap, fmt);
45 doadd(fmt, ap);
46 va_end(ap);
47 endmsg();
48 }
49
50 /*
51 * add things to the current message
52 */
53 addmsg(char *fmt, ...)
54 {
55 va_list ap;
56
57 va_start(ap, fmt);
58 doadd(fmt, ap);
59 va_end(ap);
60 }
61
62 /*
63 * Display a new msg (giving him a chance to see the previous one if it
64 * is up there with the --More--)
65 */
66 endmsg()
67 {
68 strncpy(huh, msgbuf, 80);
69 huh[79] = 0;
70
71 if (mpos)
72 {
73 wmove(cw, 0, mpos);
74 waddstr(cw, "--More--");
75 draw(cw);
76 wait_for(cw,' ');
77 }
78 mvwaddstr(cw, 0, 0, msgbuf);
79 wclrtoeol(cw);
80 mpos = newpos;
81 newpos = 0;
82 draw(cw);
83 }
84
85 doadd(char *fmt, va_list ap)
86 {
87 vsprintf(&msgbuf[newpos], fmt, ap);
88 newpos = (int) strlen(msgbuf);
89 }
90
91 /*
92 * step_ok:
93 * returns true if it is ok to step on ch
94 */
95
96 step_ok(ch)
97 {
98 switch (ch)
99 {
100 case ' ':
101 case '|':
102 case '-':
103 case SECRETDOOR:
104 return FALSE;
105 default:
106 return (!isalpha(ch));
107 }
108 }
109
110 /*
111 * readchar:
112 * flushes stdout so that screen is up to date and then returns
113 * getchar.
114 */
115
116 readchar(win)
117 WINDOW *win;
118 {
119 int ch;
120
121 ch = md_readchar(win);
122
123 if ((ch == 3) || (ch == 0))
124 {
125 quit(0);
126 return(27);
127 }
128
129 return(ch);
130 }
131
132 /*
133 * status:
134 * Display the important stats line. Keep the cursor where it was.
135 */
136
137 status()
138 {
139 register int oy, ox, temp;
140 register char *pb;
141 static char buf[80];
142 static int hpwidth = 0, s_hungry = -1;
143 static int s_lvl = -1, s_pur, s_hp = -1, s_str, s_add, s_ac = 0;
144 static long s_exp = 0;
145
146 /*
147 * If nothing has changed since the last status, don't
148 * bother.
149 */
150 if (s_hp == pstats.s_hpt && s_exp == pstats.s_exp && s_pur == purse
151 && s_ac == (cur_armor != NULL ? cur_armor->o_ac : pstats.s_arm)
152 && s_str == pstats.s_str.st_str && s_add == pstats.s_str.st_add
153 && s_lvl == level && s_hungry == hungry_state)
154 return;
155
156 getyx(cw, oy, ox);
157 if (s_hp != max_hp)
158 {
159 temp = s_hp = max_hp;
160 for (hpwidth = 0; temp; hpwidth++)
161 temp /= 10;
162 }
163 sprintf(buf, "Level: %d Gold: %-5d Hp: %*d(%*d) Str: %-2d",
164 level, purse, hpwidth, pstats.s_hpt, hpwidth, max_hp,
165 pstats.s_str.st_str);
166 if (pstats.s_str.st_add != 0)
167 {
168 pb = &buf[strlen(buf)];
169 sprintf(pb, "/%d", pstats.s_str.st_add);
170 }
171 pb = &buf[strlen(buf)];
172 sprintf(pb, " Ac: %-2d Exp: %d/%ld",
173 cur_armor != NULL ? cur_armor->o_ac : pstats.s_arm, pstats.s_lvl,
174 pstats.s_exp);
175 /*
176 * Save old status
177 */
178 s_lvl = level;
179 s_pur = purse;
180 s_hp = pstats.s_hpt;
181 s_str = pstats.s_str.st_str;
182 s_add = pstats.s_str.st_add;
183 s_exp = pstats.s_exp;
184 s_ac = (cur_armor != NULL ? cur_armor->o_ac : pstats.s_arm);
185 mvwaddstr(cw, LINES - 1, 0, buf);
186 switch (hungry_state)
187 {
188 case 0: ;
189 when 1:
190 waddstr(cw, " Hungry");
191 when 2:
192 waddstr(cw, " Weak");
193 when 3:
194 waddstr(cw, " Fainting");
195 }
196 wclrtoeol(cw);
197 s_hungry = hungry_state;
198 wmove(cw, oy, ox);
199 }
200
201 /*
202 * wait_for
203 * Sit around until the guy types the right key
204 */
205
206 wait_for(win, ch)
207 WINDOW *win;
208 register char ch;
209 {
210 register char c;
211
212 if (ch == '\n')
213 while ((c = readchar(win)) != '\n' && c != '\r')
214 continue;
215 else
216 while (readchar(win) != ch)
217 continue;
218 }
219
220 /*
221 * show_win:
222 * function used to display a window and wait before returning
223 */
224
225 show_win(scr, message)
226 register WINDOW *scr;
227 char *message;
228 {
229 mvwaddstr(scr, 0, 0, message);
230 touchwin(scr);
231 wmove(scr, hero.y, hero.x);
232 draw(scr);
233 wait_for(scr,' ');
234 clearok(cw, TRUE);
235 touchwin(cw);
236 }
237
238 flush_type()
239 {
240 flushinp();
241 }