Annotation of researchv10no/games/rogue/io.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.