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