Annotation of early-roguelike/urogue/io.c, Revision 1.1.1.1
1.1 rubenllo 1: /*
2: io.c - Various input/output functions
3:
4: UltraRogue: The Ultimate Adventure in the Dungeons of Doom
5: Copyright (C) 1985, 1986, 1992, 1993, 1995 Herb Chong
6: All rights reserved.
7:
8: Based on "Advanced Rogue"
9: Copyright (C) 1984, 1985 Michael Morgan, Ken Dalka
10: All rights reserved.
11:
12: Based on "Rogue: Exploring the Dungeons of Doom"
13: Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
14: All rights reserved.
15:
16: See the file LICENSE.TXT for full copyright and licensing information.
17: */
18:
19: #include <string.h>
20: #include <ctype.h>
21: #include <stdarg.h>
22: #include "rogue.h"
23:
24: char prbuf[2 * LINELEN]; /* Buffer for sprintfs */
25: static char mbuf[2*LINELEN]; /* Current message buffer */
26: static int newpos = 0; /* index in mbuf to end of msg */
27:
28: int mpos = 0; /* 0 = Overwrite existing message */
29: /* >0 = print --More-- at this pos */
30: /* and wait for key */
31:
32: int line_cnt = 0;
33: int newpage = FALSE;
34:
35: /*
36: msg()
37: Display a message at the top of the screen.
38: */
39:
40: void
41: msg(const char *fmt, ...)
42: {
43: va_list ap;
44:
45: /* if the string is "", just clear the line */
46:
47: if (*fmt == '\0')
48: {
49: wmove(cw, 0, 0);
50: wclrtoeol(cw);
51: mpos = 0;
52: return;
53: }
54:
55: /* otherwise add to the message and flush it out */
56:
57: va_start(ap, fmt);
58: doadd(fmt, ap);
59: va_end(ap);
60:
61: endmsg();
62: }
63:
64: void
65: vmsg(const char *fmt, va_list ap)
66: {
67: /* if the string is "", just clear the line */
68:
69: if (*fmt == '\0')
70: {
71: wmove(cw, 0, 0);
72: wclrtoeol(cw);
73: mpos = 0;
74: return;
75: }
76:
77: /* otherwise add to the message and flush it out */
78:
79: doadd(fmt, ap);
80: endmsg();
81: }
82:
83:
84: /*
85: addmsg()
86: add things to the current message
87: */
88:
89: void
90: addmsg(const char *fmt, ...)
91: {
92: va_list ap;
93:
94: va_start(ap, fmt);
95: doadd(fmt,ap);
96: va_end(ap);
97: }
98:
99: /*
100: endmsg()
101: Display a new msg (giving him a chance to see the previous one
102: if it is up there with the --More--)
103: */
104:
105: void
106: endmsg(void)
107: {
108: strcpy(msgbuf[msg_index], mbuf);
109:
110: msg_index = ++msg_index % 10;
111:
112: if (mpos)
113: {
114: wmove(cw, 0, mpos);
115: wprintw(cw, (char *) morestr);
116: wrefresh(cw);
117: wait_for(' ');
118: }
119:
120: mvwprintw(cw, 0, 0, mbuf);
121: wclrtoeol(cw);
122:
123: mpos = newpos;
124: newpos = 0;
125:
126: wrefresh(cw);
127: }
128:
129: void
130: doadd(const char *fmt, va_list ap)
131: {
132: vsprintf(&mbuf[newpos], fmt, ap);
133: newpos = (int) strlen(mbuf);
134: }
135:
136: /*
137: status()
138: Display the important stats line. Keep the cursor where it was.
139: */
140:
141: void
142: status(int display)
143: {
144: static char buf[1024]; /* Temporary buffer */
145: struct stats *stat_ptr, *max_ptr;
146: int oy, ox;
147:
148: stat_ptr = &pstats;
149: max_ptr = &max_stats;
150:
151: getyx(cw, oy, ox);
152: sprintf(buf,
153: "Int:%d(%d) Str:%d(%d) Wis:%d(%d) Dex:%d(%d) Con:%d(%d) Carry:%d(%d) %d",
154: stat_ptr->s_intel, max_ptr->s_intel,
155: stat_ptr->s_str, max_ptr->s_str,
156: stat_ptr->s_wisdom, max_ptr->s_wisdom,
157: stat_ptr->s_dext, max_ptr->s_dext,
158: stat_ptr->s_const, max_ptr->s_const,
159: stat_ptr->s_pack / 10, stat_ptr->s_carry / 10, foodlev );
160:
161: mvwaddstr(cw, LINES - 2, 0, buf);
162: wclrtoeol(cw);
163:
164: sprintf(buf, "Lvl:%d Au:%ld Hpt:%3ld(%3ld) Pow:%d(%d) Ac:%d Exp:%d+%ld %s",
165: level,
166: purse,
167: stat_ptr->s_hpt, max_ptr->s_hpt,
168: stat_ptr->s_power, max_ptr->s_power,
169: (cur_armor != NULL ? (cur_armor->o_ac - 10 + stat_ptr->s_arm)
170: : stat_ptr->s_arm) - ring_value(R_PROTECT),
171: stat_ptr->s_lvl,
172: stat_ptr->s_exp,
173: cnames[player.t_ctype][min(stat_ptr->s_lvl - 1, 14)]);
174:
175: mvwaddstr(cw, LINES - 1, 0, buf);
176:
177: switch(hungry_state)
178: {
179: case F_OK: break;
180: case F_HUNGRY: waddstr(cw, " Hungry");
181: break;
182: case F_WEAK: waddstr(cw, " Weak");
183: break;
184: case F_FAINT: waddstr(cw, " Fainting");
185: break;
186: }
187:
188: wclrtoeol(cw);
189: wmove(cw, oy, ox);
190:
191: if (display)
192: wrefresh(cw);
193: }
194:
195: /*
196: * readchar:
197: * Flushes stdout so that screen is up to date and then returns
198: * getchar().
199: */
200:
201: char
202: readcharw(WINDOW *win)
203: {
204: char ch;
205:
206: ch = (char) md_readchar(win);
207:
208: if ((ch == 3) || (ch == 0))
209: {
210: quit();
211: return(27);
212: }
213:
214: return(ch);
215: }
216:
217: char
218: readchar()
219: {
220: return( readcharw(cw) );
221: }
222:
223: /*
224: wait_for()
225: Sit around until the guy types the right key
226: */
227:
228: void
229: w_wait_for(WINDOW *w, int ch)
230: {
231: char c;
232:
233: if (ch == '\n')
234: while ((c = readcharw(w)) != '\n' && c != '\r')
235: continue;
236: else
237: while (readcharw(w) != ch)
238: continue;
239: }
240:
241: void
242: wait_for(int ch)
243: {
244: w_wait_for(cw, ch);
245: }
246:
247: /*
248: show_win()
249: function used to display a window and wait before returning
250: */
251:
252: void
253: show_win(WINDOW *scr, char *message)
254: {
255: mvwaddstr(scr, 0, 0, message);
256: touchwin(scr);
257: wmove(scr, hero.y, hero.x);
258: wrefresh(scr);
259: wait_for(' ');
260: clearok(cw, TRUE);
261: touchwin(cw);
262: }
263:
264: /*
265: restscr()
266: Restores the screen to the terminal
267: */
268:
269: void
270: restscr(WINDOW *scr)
271: {
272: clearok(scr, TRUE);
273: touchwin(scr);
274: }
275:
276: /*
277: add_line()
278: Add a line to the list of discoveries
279: */
280:
281: void
282: add_line(const char *fmt, ...)
283: {
284: WINDOW *tw;
285: va_list ap;
286:
287: va_start(ap, fmt);
288:
289: if (line_cnt == 0)
290: {
291: wclear(hw);
292:
293: if (inv_type == INV_SLOW)
294: mpos = 0;
295: }
296:
297: if (inv_type == INV_SLOW)
298: {
299: if ( (fmt != NULL) && (*fmt != '\0') )
300: vmsg(fmt, ap);
301: line_cnt++;
302: }
303: else
304: {
305: if ( (line_cnt >= LINES - 2) || (fmt == NULL)) /* end 'o page */
306: {
307: if (fmt == NULL && !newpage && inv_type == INV_OVER)
308: {
309: tw = newwin(line_cnt + 2, COLS, 0, 0);
310: overwrite(hw, tw);
311: wstandout(tw);
312: mvwaddstr(tw, line_cnt, 0, spacemsg);
313: wstandend(tw);
314: touchwin(tw);
315: wrefresh(tw);
316: wait_for(' ');
317: delwin(tw);
318: touchwin(cw);
319: }
320: else
321: {
322: wstandout(hw);
323: mvwaddstr(hw, LINES - 1, 0, spacemsg);
324: wstandend(hw);
325: wrefresh(hw);
326: w_wait_for(hw, ' ');
327: touchwin(cw);
328: wclear(hw);
329: }
330: newpage = TRUE;
331: line_cnt = 0;
332: }
333:
334: /* draw line */
335: if (fmt != NULL && !(line_cnt == 0 && *fmt == '\0'))
336: {
337: static char tmpbuf[1024];
338:
339: vsprintf(tmpbuf, fmt, ap);
340: mvwprintw(hw, line_cnt++, 0, tmpbuf);
341: }
342: }
343: }
344:
345: /*
346: end_line()
347: End the list of lines
348: */
349:
350: void
351: end_line(void)
352: {
353: if (inv_type != INV_SLOW)
354: add_line(NULL);
355:
356: line_cnt = 0;
357: newpage = FALSE;
358: }
359:
360: /*
361: hearmsg()
362: Call msg() only if you are not deaf
363: */
364:
365: void
366: hearmsg(const char *fmt, ...)
367: {
368: va_list ap;
369:
370: va_start(ap, fmt);
371:
372: if (off(player, ISDEAF))
373: vmsg(fmt, ap);
374: else if (wizard)
375: {
376: msg("Couldn't hear: ");
377: vmsg(fmt, ap);
378: }
379:
380: va_end(ap);
381: }
382:
383: /*
384: seemsg()
385: Call msg() only if you are not blind
386: */
387:
388: void
389: seemsg(const char *fmt, ...)
390: {
391: va_list ap;
392:
393: va_start(ap, fmt);
394:
395: if (off(player, ISBLIND))
396: vmsg(fmt, ap);
397: else if (wizard)
398: {
399: msg("Couldn't see: ");
400: vmsg(fmt, ap);
401: }
402:
403: va_end(ap);
404: }
405:
406: int
407: get_string(char *buffer, WINDOW *win)
408: {
409: char *sp, c;
410: int oy, ox;
411: char buf[2 * LINELEN];
412:
413: wrefresh(win);
414: getyx(win, oy, ox);
415:
416: /* loop reading in the string, and put it in a temporary buffer */
417:
418: for (sp = buf; (c = readcharw(win)) != '\n' &&
419: c != '\r' &&
420: c != '\033' &&
421: c != '\007' &&
422: sp < &buf[LINELEN - 1];
423: wclrtoeol(win), wrefresh(win))
424: {
425: if ((c == '\b') || (c == 0x7f))
426: {
427: if (sp > buf)
428: {
429: size_t i;
430:
431: sp--;
432:
433: for (i = strlen(unctrl(*sp)); i; i--)
434: waddch(win, '\b');
435: }
436: continue;
437: }
438: else if (c == '\0')
439: {
440: sp = buf;
441: wmove(win, oy, ox);
442: continue;
443: }
444: else if (sp == buf && c == '-' && win == hw)
445: break;
446:
447: *sp++ = c;
448: waddstr(win, unctrl(c));
449: }
450:
451: *sp = '\0';
452:
453: if (sp > buf) /* only change option if something has been typed */
454: strncpy(buffer, buf, strlen(buf)+1);
455:
456: wmove(win, oy, ox);
457: waddstr(win, buffer);
458: waddch(win, '\n');
459: wrefresh(win);
460:
461: if (win == cw)
462: mpos += (int)(sp - buf);
463:
464: if (c == '-')
465: return(MINUS);
466: else if (c == '\033' || c == '\007')
467: return(QUIT);
468: else
469: return(NORM);
470: }
471:
CVSweb