message.c revision 1.5
1/* $NetBSD: message.c,v 1.5 1995/04/22 10:27:43 cgd Exp $ */ 2 3/* 4 * Copyright (c) 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Timothy C. Stoehr. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39#ifndef lint 40#if 0 41static char sccsid[] = "@(#)message.c 8.1 (Berkeley) 5/31/93"; 42#else 43static char rcsid[] = "$NetBSD: message.c,v 1.5 1995/04/22 10:27:43 cgd Exp $"; 44#endif 45#endif /* not lint */ 46 47/* 48 * message.c 49 * 50 * This source herein may be modified and/or distributed by anybody who 51 * so desires, with the following restrictions: 52 * 1.) No portion of this notice shall be removed. 53 * 2.) Credit shall not be taken for the creation of this source. 54 * 3.) This code is not to be traded, sold, or used for personal 55 * gain or profit. 56 * 57 */ 58 59#include <stdio.h> 60#include <termios.h> 61#include <signal.h> 62#include "rogue.h" 63 64char msgs[NMESSAGES][DCOLS] = {"", "", "", "", ""}; 65short msg_col = 0, imsg = -1; 66boolean msg_cleared = 1, rmsg = 0; 67char hunger_str[8] = ""; 68char *more = "-more-"; 69 70extern boolean cant_int, did_int, interrupted, save_is_interactive; 71extern short add_strength; 72extern short cur_level; 73 74message(msg, intrpt) 75char *msg; 76boolean intrpt; 77{ 78 cant_int = 1; 79 80 if (!save_is_interactive) { 81 return; 82 } 83 if (intrpt) { 84 interrupted = 1; 85 md_slurp(); 86 } 87 88 if (!msg_cleared) { 89 mvaddstr(MIN_ROW-1, msg_col, more); 90 refresh(); 91 wait_for_ack(); 92 check_message(); 93 } 94 if (!rmsg) { 95 imsg = (imsg + 1) % NMESSAGES; 96 (void) strcpy(msgs[imsg], msg); 97 } 98 mvaddstr(MIN_ROW-1, 0, msg); 99 addch(' '); 100 refresh(); 101 msg_cleared = 0; 102 msg_col = strlen(msg); 103 104 cant_int = 0; 105 106 if (did_int) { 107 did_int = 0; 108 onintr(); 109 } 110} 111 112remessage(c) 113short c; 114{ 115 if (imsg != -1) { 116 check_message(); 117 rmsg = 1; 118 while (c > imsg) { 119 c -= NMESSAGES; 120 } 121 message(msgs[((imsg - c) % NMESSAGES)], 0); 122 rmsg = 0; 123 move(rogue.row, rogue.col); 124 refresh(); 125 } 126} 127 128check_message() 129{ 130 if (msg_cleared) { 131 return; 132 } 133 move(MIN_ROW-1, 0); 134 clrtoeol(); 135 refresh(); 136 msg_cleared = 1; 137} 138 139get_input_line(prompt, insert, buf, if_cancelled, add_blank, do_echo) 140char *prompt, *buf, *insert; 141char *if_cancelled; 142boolean add_blank; 143boolean do_echo; 144{ 145 short ch; 146 short i = 0, n; 147 148 message(prompt, 0); 149 n = strlen(prompt); 150 151 if (insert[0]) { 152 mvaddstr(0, n + 1, insert); 153 (void) strcpy(buf, insert); 154 i = strlen(insert); 155 move(0, (n + i + 1)); 156 refresh(); 157 } 158 159 while (((ch = rgetchar()) != '\r') && (ch != '\n') && (ch != CANCEL)) { 160 if ((ch >= ' ') && (ch <= '~') && (i < MAX_TITLE_LENGTH-2)) { 161 if ((ch != ' ') || (i > 0)) { 162 buf[i++] = ch; 163 if (do_echo) { 164 addch(ch); 165 } 166 } 167 } 168 if ((ch == '\b') && (i > 0)) { 169 if (do_echo) { 170 mvaddch(0, i + n, ' '); 171 move(MIN_ROW-1, i+n); 172 } 173 i--; 174 } 175 refresh(); 176 } 177 check_message(); 178 if (add_blank) { 179 buf[i++] = ' '; 180 } else { 181 while ((i > 0) && (buf[i-1] == ' ')) { 182 i--; 183 } 184 } 185 186 buf[i] = 0; 187 188 if ((ch == CANCEL) || (i == 0) || ((i == 1) && add_blank)) { 189 if (if_cancelled) { 190 message(if_cancelled, 0); 191 } 192 return(0); 193 } 194 return(i); 195} 196 197rgetchar() 198{ 199 register ch; 200 201 for(;;) { 202 ch = getchar(); 203 204 switch(ch) { 205 case '\022': 206 wrefresh(curscr); 207 break; 208#ifdef UNIX_BSD4_2 209 case '\032': 210 printf(CL); 211 fflush(stdout); 212 tstp(); 213 break; 214#endif 215 case '&': 216 save_screen(); 217 break; 218 default: 219 return(ch); 220 } 221 } 222} 223/* 224Level: 99 Gold: 999999 Hp: 999(999) Str: 99(99) Arm: 99 Exp: 21/10000000 Hungry 2250 5 1 5 2 5 3 5 4 5 5 5 6 5 7 5 226*/ 227 228print_stats(stat_mask) 229register stat_mask; 230{ 231 char buf[16]; 232 boolean label; 233 int row = DROWS - 1; 234 235 label = (stat_mask & STAT_LABEL) ? 1 : 0; 236 237 if (stat_mask & STAT_LEVEL) { 238 if (label) { 239 mvaddstr(row, 0, "Level: "); 240 } 241 /* max level taken care of in make_level() */ 242 sprintf(buf, "%d", cur_level); 243 mvaddstr(row, 7, buf); 244 pad(buf, 2); 245 } 246 if (stat_mask & STAT_GOLD) { 247 if (label) { 248 mvaddstr(row, 10, "Gold: "); 249 } 250 if (rogue.gold > MAX_GOLD) { 251 rogue.gold = MAX_GOLD; 252 } 253 sprintf(buf, "%ld", rogue.gold); 254 mvaddstr(row, 16, buf); 255 pad(buf, 6); 256 } 257 if (stat_mask & STAT_HP) { 258 if (label) { 259 mvaddstr(row, 23, "Hp: "); 260 } 261 if (rogue.hp_max > MAX_HP) { 262 rogue.hp_current -= (rogue.hp_max - MAX_HP); 263 rogue.hp_max = MAX_HP; 264 } 265 sprintf(buf, "%d(%d)", rogue.hp_current, rogue.hp_max); 266 mvaddstr(row, 27, buf); 267 pad(buf, 8); 268 } 269 if (stat_mask & STAT_STRENGTH) { 270 if (label) { 271 mvaddstr(row, 36, "Str: "); 272 } 273 if (rogue.str_max > MAX_STRENGTH) { 274 rogue.str_current -= (rogue.str_max - MAX_STRENGTH); 275 rogue.str_max = MAX_STRENGTH; 276 } 277 sprintf(buf, "%d(%d)", (rogue.str_current + add_strength), 278 rogue.str_max); 279 mvaddstr(row, 41, buf); 280 pad(buf, 6); 281 } 282 if (stat_mask & STAT_ARMOR) { 283 if (label) { 284 mvaddstr(row, 48, "Arm: "); 285 } 286 if (rogue.armor && (rogue.armor->d_enchant > MAX_ARMOR)) { 287 rogue.armor->d_enchant = MAX_ARMOR; 288 } 289 sprintf(buf, "%d", get_armor_class(rogue.armor)); 290 mvaddstr(row, 53, buf); 291 pad(buf, 2); 292 } 293 if (stat_mask & STAT_EXP) { 294 if (label) { 295 mvaddstr(row, 56, "Exp: "); 296 } 297 if (rogue.exp_points > MAX_EXP) { 298 rogue.exp_points = MAX_EXP; 299 } 300 if (rogue.exp > MAX_EXP_LEVEL) { 301 rogue.exp = MAX_EXP_LEVEL; 302 } 303 sprintf(buf, "%d/%ld", rogue.exp, rogue.exp_points); 304 mvaddstr(row, 61, buf); 305 pad(buf, 11); 306 } 307 if (stat_mask & STAT_HUNGER) { 308 mvaddstr(row, 73, hunger_str); 309 clrtoeol(); 310 } 311 refresh(); 312} 313 314pad(s, n) 315char *s; 316short n; 317{ 318 short i; 319 320 for (i = strlen(s); i < n; i++) { 321 addch(' '); 322 } 323} 324 325save_screen() 326{ 327 FILE *fp; 328 short i, j; 329 char buf[DCOLS+2]; 330 boolean found_non_blank; 331 332 if ((fp = fopen("rogue.screen", "w")) != NULL) { 333 for (i = 0; i < DROWS; i++) { 334 found_non_blank = 0; 335 for (j = (DCOLS - 1); j >= 0; j--) { 336 buf[j] = mvinch(i, j); 337 if (!found_non_blank) { 338 if ((buf[j] != ' ') || (j == 0)) { 339 buf[j + ((j == 0) ? 0 : 1)] = 0; 340 found_non_blank = 1; 341 } 342 } 343 } 344 fputs(buf, fp); 345 putc('\n', fp); 346 } 347 fclose(fp); 348 } else { 349 sound_bell(); 350 } 351} 352 353sound_bell() 354{ 355 putchar(7); 356 fflush(stdout); 357} 358 359boolean 360is_digit(ch) 361short ch; 362{ 363 return((ch >= '0') && (ch <= '9')); 364} 365 366r_index(str, ch, last) 367char *str; 368int ch; 369boolean last; 370{ 371 int i = 0; 372 373 if (last) { 374 for (i = strlen(str) - 1; i >= 0; i--) { 375 if (str[i] == ch) { 376 return(i); 377 } 378 } 379 } else { 380 for (i = 0; str[i]; i++) { 381 if (str[i] == ch) { 382 return(i); 383 } 384 } 385 } 386 return(-1); 387} 388