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