1 /* $NetBSD: pl_7.c,v 1.6 1995/04/22 10:37:17 cgd Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)pl_7.c 8.1 (Berkeley) 5/31/93"; 39 #else 40 static char rcsid[] = "$NetBSD: pl_7.c,v 1.6 1995/04/22 10:37:17 cgd Exp $"; 41 #endif 42 #endif /* not lint */ 43 44 #include <sys/ttydefaults.h> 45 #include "player.h" 46 47 48 /* 49 * Display interface 50 */ 51 52 static char sc_hasprompt; 53 static char *sc_prompt; 54 static char *sc_buf; 55 static int sc_line; 56 57 initscreen() 58 { 59 /* initscr() already done in SCREENTEST() */ 60 view_w = newwin(VIEW_Y, VIEW_X, VIEW_T, VIEW_L); 61 slot_w = newwin(SLOT_Y, SLOT_X, SLOT_T, SLOT_L); 62 scroll_w = newwin(SCROLL_Y, SCROLL_X, SCROLL_T, SCROLL_L); 63 stat_w = newwin(STAT_Y, STAT_X, STAT_T, STAT_L); 64 turn_w = newwin(TURN_Y, TURN_X, TURN_T, TURN_L); 65 done_curses++; 66 (void) leaveok(view_w, 1); 67 (void) leaveok(slot_w, 1); 68 (void) leaveok(stat_w, 1); 69 (void) leaveok(turn_w, 1); 70 noecho(); 71 crmode(); 72 } 73 74 cleanupscreen() 75 { 76 /* alarm already turned off */ 77 if (done_curses) { 78 (void) wmove(scroll_w, SCROLL_Y - 1, 0); 79 (void) wclrtoeol(scroll_w); 80 draw_screen(); 81 endwin(); 82 } 83 } 84 85 void 86 newturn() 87 { 88 repaired = loaded = fired = changed = 0; 89 movebuf[0] = '\0'; 90 91 (void) alarm(0); 92 if (mf->readyL & R_LOADING) 93 if (mf->readyL & R_DOUBLE) 94 mf->readyL = R_LOADING; 95 else 96 mf->readyL = R_LOADED; 97 if (mf->readyR & R_LOADING) 98 if (mf->readyR & R_DOUBLE) 99 mf->readyR = R_LOADING; 100 else 101 mf->readyR = R_LOADED; 102 if (!hasdriver) 103 Write(W_DDEAD, SHIP(0), 0, 0, 0, 0, 0); 104 105 if (sc_hasprompt) { 106 (void) wmove(scroll_w, sc_line, 0); 107 (void) wclrtoeol(scroll_w); 108 } 109 if (Sync() < 0) 110 leave(LEAVE_SYNC); 111 if (!hasdriver) 112 leave(LEAVE_DRIVER); 113 if (sc_hasprompt) 114 (void) wprintw(scroll_w, "%s%s", sc_prompt, sc_buf); 115 116 if (turn % 50 == 0) 117 Write(W_ALIVE, SHIP(0), 0, 0, 0, 0, 0); 118 if (mf->FS && (!mc->rig1 || windspeed == 6)) 119 Write(W_FS, ms, 0, 0, 0, 0, 0); 120 if (mf->FS == 1) 121 Write(W_FS, ms, 0, 2, 0, 0, 0); 122 123 if (mf->struck) 124 leave(LEAVE_QUIT); 125 if (mf->captured != 0) 126 leave(LEAVE_CAPTURED); 127 if (windspeed == 7) 128 leave(LEAVE_HURRICAN); 129 130 adjustview(); 131 draw_screen(); 132 133 (void) signal(SIGALRM, newturn); 134 (void) alarm(7); 135 } 136 137 /*VARARGS2*/ 138 Signal(fmt, ship, a, b, c, d) 139 char *fmt; 140 register struct ship *ship; 141 int a, b, c, d; 142 { 143 if (!done_curses) 144 return; 145 if (*fmt == '\7') 146 putchar(*fmt++); 147 if (ship == 0) 148 (void) wprintw(scroll_w, fmt, a, b, c, d); 149 else 150 (void) wprintw(scroll_w, fmt, ship->shipname, 151 colours(ship), sterncolour(ship), a, b, c, d); 152 Scroll(); 153 } 154 155 Scroll() 156 { 157 if (++sc_line >= SCROLL_Y) 158 sc_line = 0; 159 (void) wmove(scroll_w, sc_line, 0); 160 (void) wclrtoeol(scroll_w); 161 } 162 163 prompt(p, ship) 164 register char *p; 165 struct ship *ship; 166 { 167 static char buf[60]; 168 169 if (ship != 0) { 170 (void)sprintf(buf, p, ship->shipname, colours(ship), 171 sterncolour(ship)); 172 p = buf; 173 } 174 sc_prompt = p; 175 sc_buf = ""; 176 sc_hasprompt = 1; 177 (void) waddstr(scroll_w, p); 178 } 179 180 endprompt(flag) 181 char flag; 182 { 183 sc_hasprompt = 0; 184 if (flag) 185 Scroll(); 186 } 187 188 sgetch(p, ship, flag) 189 char *p; 190 struct ship *ship; 191 char flag; 192 { 193 register c; 194 195 prompt(p, ship); 196 blockalarm(); 197 (void) wrefresh(scroll_w); 198 unblockalarm(); 199 while ((c = wgetch(scroll_w)) == EOF) 200 ; 201 if (flag && c >= ' ' && c < 0x7f) 202 (void) waddch(scroll_w, c); 203 endprompt(flag); 204 return c; 205 } 206 207 sgetstr(pr, buf, n) 208 char *pr; 209 register char *buf; 210 register n; 211 { 212 register c; 213 register char *p = buf; 214 215 prompt(pr, (struct ship *)0); 216 sc_buf = buf; 217 for (;;) { 218 *p = 0; 219 blockalarm(); 220 (void) wrefresh(scroll_w); 221 unblockalarm(); 222 while ((c = wgetch(scroll_w)) == EOF) 223 ; 224 switch (c) { 225 case '\n': 226 case '\r': 227 endprompt(1); 228 return; 229 case '\b': 230 if (p > buf) { 231 (void) waddstr(scroll_w, "\b \b"); 232 p--; 233 } 234 break; 235 default: 236 if (c >= ' ' && c < 0x7f && p < buf + n - 1) { 237 *p++ = c; 238 (void) waddch(scroll_w, c); 239 } else 240 (void) putchar('\a'); 241 } 242 } 243 } 244 245 draw_screen() 246 { 247 draw_view(); 248 draw_turn(); 249 draw_stat(); 250 draw_slot(); 251 (void) wrefresh(scroll_w); /* move the cursor */ 252 } 253 254 draw_view() 255 { 256 register struct ship *sp; 257 258 (void) werase(view_w); 259 foreachship(sp) { 260 if (sp->file->dir 261 && sp->file->row > viewrow 262 && sp->file->row < viewrow + VIEW_Y 263 && sp->file->col > viewcol 264 && sp->file->col < viewcol + VIEW_X) { 265 (void) wmove(view_w, sp->file->row - viewrow, 266 sp->file->col - viewcol); 267 (void) waddch(view_w, colours(sp)); 268 (void) wmove(view_w, 269 sternrow(sp) - viewrow, 270 sterncol(sp) - viewcol); 271 (void) waddch(view_w, sterncolour(sp)); 272 } 273 } 274 (void) wrefresh(view_w); 275 } 276 277 draw_turn() 278 { 279 (void) wmove(turn_w, 0, 0); 280 (void) wprintw(turn_w, "%cTurn %d", dont_adjust?'*':'-', turn); 281 (void) wrefresh(turn_w); 282 } 283 284 draw_stat() 285 { 286 (void) wmove(stat_w, STAT_1, 0); 287 (void) wprintw(stat_w, "Points %3d\n", mf->points); 288 (void) wprintw(stat_w, "Fouls %2d\n", fouled(ms)); 289 (void) wprintw(stat_w, "Grapples %2d\n", grappled(ms)); 290 291 (void) wmove(stat_w, STAT_2, 0); 292 (void) wprintw(stat_w, " 0 %c(%c)\n", 293 maxmove(ms, winddir + 3, -1) + '0', 294 maxmove(ms, winddir + 3, 1) + '0'); 295 (void) waddstr(stat_w, " \\|/\n"); 296 (void) wprintw(stat_w, " -^-%c(%c)\n", 297 maxmove(ms, winddir + 2, -1) + '0', 298 maxmove(ms, winddir + 2, 1) + '0'); 299 (void) waddstr(stat_w, " /|\\\n"); 300 (void) wprintw(stat_w, " | %c(%c)\n", 301 maxmove(ms, winddir + 1, -1) + '0', 302 maxmove(ms, winddir + 1, 1) + '0'); 303 (void) wprintw(stat_w, " %c(%c)\n", 304 maxmove(ms, winddir, -1) + '0', 305 maxmove(ms, winddir, 1) + '0'); 306 307 (void) wmove(stat_w, STAT_3, 0); 308 (void) wprintw(stat_w, "Load %c%c %c%c\n", 309 loadname[mf->loadL], readyname(mf->readyL), 310 loadname[mf->loadR], readyname(mf->readyR)); 311 (void) wprintw(stat_w, "Hull %2d\n", mc->hull); 312 (void) wprintw(stat_w, "Crew %2d %2d %2d\n", 313 mc->crew1, mc->crew2, mc->crew3); 314 (void) wprintw(stat_w, "Guns %2d %2d\n", mc->gunL, mc->gunR); 315 (void) wprintw(stat_w, "Carr %2d %2d\n", mc->carL, mc->carR); 316 (void) wprintw(stat_w, "Rigg %d %d %d ", mc->rig1, mc->rig2, mc->rig3); 317 if (mc->rig4 < 0) 318 (void) waddch(stat_w, '-'); 319 else 320 (void) wprintw(stat_w, "%d", mc->rig4); 321 (void) wrefresh(stat_w); 322 } 323 324 draw_slot() 325 { 326 if (!boarding(ms, 0)) { 327 (void) mvwaddstr(slot_w, 0, 0, " "); 328 (void) mvwaddstr(slot_w, 1, 0, " "); 329 } else 330 (void) mvwaddstr(slot_w, 1, 0, "OBP"); 331 if (!boarding(ms, 1)) { 332 (void) mvwaddstr(slot_w, 2, 0, " "); 333 (void) mvwaddstr(slot_w, 3, 0, " "); 334 } else 335 (void) mvwaddstr(slot_w, 3, 0, "DBP"); 336 337 (void) wmove(slot_w, SLOT_Y-4, 0); 338 if (mf->RH) 339 (void) wprintw(slot_w, "%dRH", mf->RH); 340 else 341 (void) waddstr(slot_w, " "); 342 (void) wmove(slot_w, SLOT_Y-3, 0); 343 if (mf->RG) 344 (void) wprintw(slot_w, "%dRG", mf->RG); 345 else 346 (void) waddstr(slot_w, " "); 347 (void) wmove(slot_w, SLOT_Y-2, 0); 348 if (mf->RR) 349 (void) wprintw(slot_w, "%dRR", mf->RR); 350 else 351 (void) waddstr(slot_w, " "); 352 353 #define Y (SLOT_Y/2) 354 (void) wmove(slot_w, 7, 1); 355 (void) wprintw(slot_w,"%d", windspeed); 356 (void) mvwaddch(slot_w, Y, 0, ' '); 357 (void) mvwaddch(slot_w, Y, 2, ' '); 358 (void) mvwaddch(slot_w, Y-1, 0, ' '); 359 (void) mvwaddch(slot_w, Y-1, 1, ' '); 360 (void) mvwaddch(slot_w, Y-1, 2, ' '); 361 (void) mvwaddch(slot_w, Y+1, 0, ' '); 362 (void) mvwaddch(slot_w, Y+1, 1, ' '); 363 (void) mvwaddch(slot_w, Y+1, 2, ' '); 364 (void) wmove(slot_w, Y - dr[winddir], 1 - dc[winddir]); 365 switch (winddir) { 366 case 1: 367 case 5: 368 (void) waddch(slot_w, '|'); 369 break; 370 case 2: 371 case 6: 372 (void) waddch(slot_w, '/'); 373 break; 374 case 3: 375 case 7: 376 (void) waddch(slot_w, '-'); 377 break; 378 case 4: 379 case 8: 380 (void) waddch(slot_w, '\\'); 381 break; 382 } 383 (void) mvwaddch(slot_w, Y + dr[winddir], 1 + dc[winddir], '+'); 384 (void) wrefresh(slot_w); 385 } 386 387 draw_board() 388 { 389 register int n; 390 391 (void) clear(); 392 (void) werase(view_w); 393 (void) werase(slot_w); 394 (void) werase(scroll_w); 395 (void) werase(stat_w); 396 (void) werase(turn_w); 397 398 sc_line = 0; 399 400 (void) move(BOX_T, BOX_L); 401 for (n = 0; n < BOX_X; n++) 402 (void) addch('-'); 403 (void) move(BOX_B, BOX_L); 404 for (n = 0; n < BOX_X; n++) 405 (void) addch('-'); 406 for (n = BOX_T+1; n < BOX_B; n++) { 407 (void) mvaddch(n, BOX_L, '|'); 408 (void) mvaddch(n, BOX_R, '|'); 409 } 410 (void) mvaddch(BOX_T, BOX_L, '+'); 411 (void) mvaddch(BOX_T, BOX_R, '+'); 412 (void) mvaddch(BOX_B, BOX_L, '+'); 413 (void) mvaddch(BOX_B, BOX_R, '+'); 414 (void) refresh(); 415 416 #define WSaIM "Wooden Ships & Iron Men" 417 (void) wmove(view_w, 2, (VIEW_X - sizeof WSaIM - 1) / 2); 418 (void) waddstr(view_w, WSaIM); 419 (void) wmove(view_w, 4, (VIEW_X - strlen(cc->name)) / 2); 420 (void) waddstr(view_w, cc->name); 421 (void) wrefresh(view_w); 422 423 (void) move(LINE_T, LINE_L); 424 (void) printw("Class %d %s (%d guns) '%s' (%c%c)", 425 mc->class, 426 classname[mc->class], 427 mc->guns, 428 ms->shipname, 429 colours(ms), 430 sterncolour(ms)); 431 (void) refresh(); 432 } 433 434 centerview() 435 { 436 viewrow = mf->row - VIEW_Y / 2; 437 viewcol = mf->col - VIEW_X / 2; 438 } 439 440 upview() 441 { 442 viewrow -= VIEW_Y / 3; 443 } 444 445 downview() 446 { 447 viewrow += VIEW_Y / 3; 448 } 449 450 leftview() 451 { 452 viewcol -= VIEW_X / 5; 453 } 454 455 rightview() 456 { 457 viewcol += VIEW_X / 5; 458 } 459 460 adjustview() 461 { 462 if (dont_adjust) 463 return; 464 if (mf->row < viewrow + VIEW_Y/4) 465 viewrow = mf->row - (VIEW_Y - VIEW_Y/4); 466 else if (mf->row > viewrow + (VIEW_Y - VIEW_Y/4)) 467 viewrow = mf->row - VIEW_Y/4; 468 if (mf->col < viewcol + VIEW_X/8) 469 viewcol = mf->col - (VIEW_X - VIEW_X/8); 470 else if (mf->col > viewcol + (VIEW_X - VIEW_X/8)) 471 viewcol = mf->col - VIEW_X/8; 472 } 473