[BACK]Return to io.c CVS log [TXT][DIR] Up to [contributed] / early-roguelike / rogue4

Annotation of early-roguelike/rogue4/io.c, Revision 1.1.1.1

1.1       rubenllo    1: /*
                      2:  * Various input/output functions
                      3:  *
                      4:  * @(#)io.c    4.13 (Berkeley) 2/25/82
                      5:  *
                      6:  * Rogue: Exploring the Dungeons of Doom
                      7:  * Copyright (C) 1980, 1981, 1982 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 <string.h>
                     16: #include "rogue.h"
                     17: #include <stdarg.h>
                     18:
                     19: void doadd(char *fmt, va_list ap);
                     20:
                     21: /*
                     22:  * msg:
                     23:  *     Display a message at the top of the screen.
                     24:  */
                     25: static char msgbuf[BUFSIZ];
                     26: static int newpos = 0;
                     27:
                     28: void
                     29: msg(char *fmt, ...)
                     30: {
                     31:     va_list ap;
                     32:     /*
                     33:      * if the string is "", just clear the line
                     34:      */
                     35:     if (*fmt == '\0')
                     36:     {
                     37:        move(0, 0);
                     38:        clrtoeol();
                     39:        mpos = 0;
                     40:        return;
                     41:     }
                     42:     /*
                     43:      * otherwise add to the message and flush it out
                     44:      */
                     45:     va_start(ap,fmt);
                     46:     doadd(fmt, ap);
                     47:     va_end(ap);
                     48:     endmsg();
                     49: }
                     50:
                     51: /*
                     52:  * addmsg:
                     53:  *     Add things to the current message
                     54:  */
                     55:
                     56: void
                     57: addmsg(char *fmt, ...)
                     58: {
                     59:     va_list ap;
                     60:
                     61:     va_start(ap, fmt);
                     62:     doadd(fmt, ap);
                     63:     va_end(ap);
                     64: }
                     65:
                     66: /*
                     67:  * endmsg:
                     68:  *     Display a new msg (giving him a chance to see the previous one
                     69:  *     if it is up there with the --More--)
                     70:  */
                     71:
                     72: void
                     73: endmsg(void)
                     74: {
                     75:     if (save_msg)
                     76:     {
                     77:        strncpy(huh, msgbuf, 80);
                     78:        huh[79] = 0;
                     79:     }
                     80:
                     81:     if (mpos)
                     82:     {
                     83:        look(FALSE);
                     84:        move(0, mpos);
                     85:        addstr("--More--");
                     86:        refresh();
                     87:        wait_for(' ');
                     88:     }
                     89:     /*
                     90:      * All messages should start with uppercase, except ones that
                     91:      * start with a pack addressing character
                     92:      */
                     93:     if (islower(msgbuf[0]) && msgbuf[1] != ')')
                     94:        msgbuf[0] = toupper(msgbuf[0]);
                     95:     mvaddstr(0, 0, msgbuf);
                     96:     clrtoeol();
                     97:     mpos = newpos;
                     98:     newpos = 0;
                     99:     refresh();
                    100: }
                    101:
                    102: /*
                    103:  * doadd:
                    104:  *     Perform an add onto the message buffer
                    105:  */
                    106:
                    107: void
                    108: doadd(char *fmt, va_list ap)
                    109: {
                    110:     vsprintf(&msgbuf[newpos], fmt, ap);
                    111:     newpos = strlen(msgbuf);
                    112: }
                    113:
                    114: /*
                    115:  * step_ok:
                    116:  *     Returns true if it is ok to step on ch
                    117:  */
                    118: bool
                    119: step_ok(char ch)
                    120: {
                    121:     switch (ch)
                    122:     {
                    123:        case ' ':
                    124:        case '|':
                    125:        case '-':
                    126:            return FALSE;
                    127:        default:
                    128:            return (!isalpha(ch));
                    129:     }
                    130: }
                    131:
                    132: /*
                    133:  * readchar:
                    134:  *     Flushes stdout so that screen is up to date and then returns
                    135:  *     getchar().
                    136:  */
                    137: int
                    138: readcharw(WINDOW *win)
                    139: {
                    140:     int ch;
                    141:
                    142:     ch = md_readchar(win);
                    143:
                    144:     if ((ch == 3) || (ch == 0))
                    145:     {
                    146:        quit(0);
                    147:        return(27);
                    148:     }
                    149:
                    150:     return(ch);
                    151: }
                    152:
                    153: int
                    154: readchar(void)
                    155: {
                    156:     return( readcharw(stdscr) );
                    157: }
                    158:
                    159: char *
                    160: unctrol(char ch)
                    161: {
                    162:     return( (char *) unctrl(ch) );
                    163: }
                    164:
                    165: /*
                    166:  * status:
                    167:  *     Display the important stats line.  Keep the cursor where it was.
                    168:  */
                    169: void
                    170: status(void)
                    171: {
                    172:     register int oy, ox, temp;
                    173:     static int hpwidth = 0, s_hungry;
                    174:     static int s_lvl, s_pur = -1, s_hp, s_ac = 0;
                    175:     static str_t s_str;
                    176:     static long s_exp = 0;
                    177:     static char *state_name[] =
                    178:     {
                    179:        "", "Hungry", "Weak", "Faint"
                    180:     };
                    181:
                    182:     /*
                    183:      * If nothing has changed since the last status, don't
                    184:      * bother.
                    185:      */
                    186:     if (s_hp == pstats.s_hpt && s_exp == pstats.s_exp && s_pur == purse
                    187:        && s_ac == (cur_armor != NULL ? cur_armor->o_ac : pstats.s_arm)
                    188:        && s_str == pstats.s_str && s_lvl == level && s_hungry == hungry_state)
                    189:            return;
                    190:
                    191:     getyx(stdscr, oy, ox);
                    192:     if (s_hp != max_hp)
                    193:     {
                    194:        temp = s_hp = max_hp;
                    195:        for (hpwidth = 0; temp; hpwidth++)
                    196:            temp /= 10;
                    197:     }
                    198:     move(LINES - 1, 0);
                    199:     printw("Level: %d  Gold: %-5d  Hp: %*d(%*d)  Str: %2d(%d)  Ac: %-2d  Exp: %d/%ld  %s",
                    200:            level, purse, hpwidth, pstats.s_hpt, hpwidth, max_hp, pstats.s_str,
                    201:            max_stats.s_str,
                    202:            cur_armor != NULL ? cur_armor->o_ac : pstats.s_arm, pstats.s_lvl,
                    203:            pstats.s_exp, state_name[hungry_state]);
                    204:
                    205:     clrtoeol();
                    206:     /*
                    207:      * Save old status
                    208:      */
                    209:     s_lvl = level;
                    210:     s_pur = purse;
                    211:     s_hp = pstats.s_hpt;
                    212:     s_str = pstats.s_str;
                    213:     s_exp = pstats.s_exp;
                    214:     s_ac = (cur_armor != NULL ? cur_armor->o_ac : pstats.s_arm);
                    215:     s_hungry = hungry_state;
                    216:     move(oy, ox);
                    217: }
                    218:
                    219: /*
                    220:  * wait_for
                    221:  *     Sit around until the guy types the right key
                    222:  */
                    223:
                    224:
                    225:
                    226: void
                    227: wait_for(char ch)
                    228: {
                    229:     w_wait_for(stdscr, ch);
                    230: }
                    231:
                    232: void
                    233: w_wait_for(WINDOW *win, char ch)
                    234: {
                    235:     register char c;
                    236:
                    237:     if (ch == '\n')
                    238:         while ((c = readcharw(win)) != '\n' && c != '\r')
                    239:            continue;
                    240:     else
                    241:         while (readcharw(win) != ch)
                    242:            continue;
                    243: }
                    244:
                    245: /*
                    246:  * show_win:
                    247:  *     Function used to display a window and wait before returning
                    248:  */
                    249: void
                    250: show_win(WINDOW *scr, char *message)
                    251: {
                    252:     mvwaddstr(scr, 0, 0, message);
                    253:     touchwin(scr);
                    254:     wmove(scr, hero.y, hero.x);
                    255:     wrefresh(scr);
                    256:     w_wait_for(scr,' ');
                    257:     clearok(curscr, TRUE);
                    258:     touchwin(stdscr);
                    259: }

CVSweb