Home | History | Annotate | Line # | Download | only in sail
pl_7.c revision 1.9
      1 /*	$NetBSD: pl_7.c,v 1.9 1998/08/30 09:19:40 veego 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 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)pl_7.c	8.1 (Berkeley) 5/31/93";
     40 #else
     41 __RCSID("$NetBSD: pl_7.c,v 1.9 1998/08/30 09:19:40 veego Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/ttydefaults.h>
     46 #include "player.h"
     47 #ifdef __STDC__
     48 #include <stdarg.h>
     49 #else
     50 #include <varargs.h>
     51 #endif
     52 #include <unistd.h>
     53 
     54 
     55 /*
     56  * Display interface
     57  */
     58 
     59 static char sc_hasprompt;
     60 static char *sc_prompt;
     61 static char *sc_buf;
     62 static int sc_line;
     63 
     64 void
     65 initscreen()
     66 {
     67 	/* initscr() already done in SCREENTEST() */
     68 	view_w = newwin(VIEW_Y, VIEW_X, VIEW_T, VIEW_L);
     69 	slot_w = newwin(SLOT_Y, SLOT_X, SLOT_T, SLOT_L);
     70 	scroll_w = newwin(SCROLL_Y, SCROLL_X, SCROLL_T, SCROLL_L);
     71 	stat_w = newwin(STAT_Y, STAT_X, STAT_T, STAT_L);
     72 	turn_w = newwin(TURN_Y, TURN_X, TURN_T, TURN_L);
     73 	done_curses++;
     74 	(void) leaveok(view_w, 1);
     75 	(void) leaveok(slot_w, 1);
     76 	(void) leaveok(stat_w, 1);
     77 	(void) leaveok(turn_w, 1);
     78 	noecho();
     79 	crmode();
     80 }
     81 
     82 void
     83 cleanupscreen()
     84 {
     85 	/* alarm already turned off */
     86 	if (done_curses) {
     87 		(void) wmove(scroll_w, SCROLL_Y - 1, 0);
     88 		(void) wclrtoeol(scroll_w);
     89 		draw_screen();
     90 		endwin();
     91 	}
     92 }
     93 
     94 /*ARGSUSED*/
     95 void
     96 newturn(n)
     97 	int n;
     98 {
     99 	repaired = loaded = fired = changed = 0;
    100 	movebuf[0] = '\0';
    101 
    102 	(void) alarm(0);
    103 	if (mf->readyL & R_LOADING) {
    104 		if (mf->readyL & R_DOUBLE)
    105 			mf->readyL = R_LOADING;
    106 		else
    107 			mf->readyL = R_LOADED;
    108 	}
    109 	if (mf->readyR & R_LOADING) {
    110 		if (mf->readyR & R_DOUBLE)
    111 			mf->readyR = R_LOADING;
    112 		else
    113 			mf->readyR = R_LOADED;
    114 	}
    115 	if (!hasdriver)
    116 		Write(W_DDEAD, SHIP(0), 0, 0, 0, 0, 0);
    117 
    118 	if (sc_hasprompt) {
    119 		(void) wmove(scroll_w, sc_line, 0);
    120 		(void) wclrtoeol(scroll_w);
    121 	}
    122 	if (Sync() < 0)
    123 		leave(LEAVE_SYNC);
    124 	if (!hasdriver)
    125 		leave(LEAVE_DRIVER);
    126 	if (sc_hasprompt)
    127 		(void) wprintw(scroll_w, "%s%s", sc_prompt, sc_buf);
    128 
    129 	if (turn % 50 == 0)
    130 		Write(W_ALIVE, SHIP(0), 0, 0, 0, 0, 0);
    131 	if (mf->FS && (!mc->rig1 || windspeed == 6))
    132 		Write(W_FS, ms, 0, 0, 0, 0, 0);
    133 	if (mf->FS == 1)
    134 		Write(W_FS, ms, 0, 2, 0, 0, 0);
    135 
    136 	if (mf->struck)
    137 		leave(LEAVE_QUIT);
    138 	if (mf->captured != 0)
    139 		leave(LEAVE_CAPTURED);
    140 	if (windspeed == 7)
    141 		leave(LEAVE_HURRICAN);
    142 
    143 	adjustview();
    144 	draw_screen();
    145 
    146 	(void) signal(SIGALRM, newturn);
    147 	(void) alarm(7);
    148 }
    149 
    150 /*VARARGS2*/
    151 void
    152 #ifdef __STDC__
    153 Signal(const char *fmt, struct ship *ship, ...)
    154 #else
    155 Signal(va_alist)
    156 	va_dcl
    157 #endif
    158 {
    159 	va_list ap;
    160 	char format[BUFSIZ];
    161 #ifndef __STDC__
    162 	const char *fmt;
    163 	struct ship *ship;
    164 
    165 	va_start(ap);
    166 	fmt = va_arg(ap, const char *);
    167 	ship = va_arg(ap, struct ship *);
    168 #else
    169 	va_start(ap, ship);
    170 #endif
    171 	if (!done_curses)
    172 		return;
    173 	if (*fmt == '\7')
    174 		putchar(*fmt++);
    175 	fmtship(format, sizeof(format), fmt, ship);
    176 	(void) vwprintw(scroll_w, format, ap);
    177 	va_end(ap);
    178 	Scroll();
    179 }
    180 
    181 /*VARARGS2*/
    182 void
    183 #ifdef __STDC__
    184 Msg(const char *fmt, ...)
    185 #else
    186 Msg(va_alist)
    187 	va_dcl
    188 #endif
    189 {
    190 	va_list ap;
    191 #ifndef __STDC__
    192 	const char *fmt;
    193 
    194 	va_start(ap);
    195 	fmt = va_arg(ap, const char *);
    196 #else
    197 	va_start(ap, fmt);
    198 #endif
    199 
    200 	if (!done_curses)
    201 		return;
    202 	if (*fmt == '\7')
    203 		putchar(*fmt++);
    204 	(void) vwprintw(scroll_w, fmt, ap);
    205 	va_end(ap);
    206 	Scroll();
    207 }
    208 
    209 void
    210 Scroll()
    211 {
    212 	if (++sc_line >= SCROLL_Y)
    213 		sc_line = 0;
    214 	(void) wmove(scroll_w, sc_line, 0);
    215 	(void) wclrtoeol(scroll_w);
    216 }
    217 
    218 void
    219 prompt(p, ship)
    220 char *p;
    221 struct ship *ship;
    222 {
    223 	static char buf[BUFSIZ];
    224 
    225 	fmtship(buf, sizeof(buf), p, ship);
    226 	sc_prompt = buf;
    227 	sc_buf = "";
    228 	sc_hasprompt = 1;
    229 	(void) waddstr(scroll_w, buf);
    230 }
    231 
    232 void
    233 endprompt(flag)
    234 char flag;
    235 {
    236 	sc_hasprompt = 0;
    237 	if (flag)
    238 		Scroll();
    239 }
    240 
    241 int
    242 sgetch(p, ship, flag)
    243 char *p;
    244 struct ship *ship;
    245 char flag;
    246 {
    247 	int c;
    248 	prompt(p, ship);
    249 	blockalarm();
    250 	(void) wrefresh(scroll_w);
    251 	unblockalarm();
    252 	while ((c = wgetch(scroll_w)) == EOF)
    253 		;
    254 	if (flag && c >= ' ' && c < 0x7f)
    255 		(void) waddch(scroll_w, c);
    256 	endprompt(flag);
    257 	return c;
    258 }
    259 
    260 void
    261 sgetstr(pr, buf, n)
    262 char *pr;
    263 char *buf;
    264 int n;
    265 {
    266 	int c;
    267 	char *p = buf;
    268 
    269 	prompt(pr, (struct ship *)0);
    270 	sc_buf = buf;
    271 	for (;;) {
    272 		*p = 0;
    273 		blockalarm();
    274 		(void) wrefresh(scroll_w);
    275 		unblockalarm();
    276 		while ((c = wgetch(scroll_w)) == EOF)
    277 			;
    278 		switch (c) {
    279 		case '\n':
    280 		case '\r':
    281 			endprompt(1);
    282 			return;
    283 		case '\b':
    284 			if (p > buf) {
    285 				(void) waddstr(scroll_w, "\b \b");
    286 				p--;
    287 			}
    288 			break;
    289 		default:
    290 			if (c >= ' ' && c < 0x7f && p < buf + n - 1) {
    291 				*p++ = c;
    292 				(void) waddch(scroll_w, c);
    293 			} else
    294 				(void) putchar('\a');
    295 		}
    296 	}
    297 }
    298 
    299 void
    300 draw_screen()
    301 {
    302 	draw_view();
    303 	draw_turn();
    304 	draw_stat();
    305 	draw_slot();
    306 	(void) wrefresh(scroll_w);		/* move the cursor */
    307 }
    308 
    309 void
    310 draw_view()
    311 {
    312 	struct ship *sp;
    313 
    314 	(void) werase(view_w);
    315 	foreachship(sp) {
    316 		if (sp->file->dir
    317 		    && sp->file->row > viewrow
    318 		    && sp->file->row < viewrow + VIEW_Y
    319 		    && sp->file->col > viewcol
    320 		    && sp->file->col < viewcol + VIEW_X) {
    321 			(void) wmove(view_w, sp->file->row - viewrow,
    322 				sp->file->col - viewcol);
    323 			(void) waddch(view_w, colours(sp));
    324 			(void) wmove(view_w,
    325 				sternrow(sp) - viewrow,
    326 				sterncol(sp) - viewcol);
    327 			(void) waddch(view_w, sterncolour(sp));
    328 		}
    329 	}
    330 	(void) wrefresh(view_w);
    331 }
    332 
    333 void
    334 draw_turn()
    335 {
    336 	(void) wmove(turn_w, 0, 0);
    337 	(void) wprintw(turn_w, "%cTurn %d", dont_adjust?'*':'-', turn);
    338 	(void) wrefresh(turn_w);
    339 }
    340 
    341 void
    342 draw_stat()
    343 {
    344 	(void) wmove(stat_w, STAT_1, 0);
    345 	(void) wprintw(stat_w, "Points  %3d\n", mf->points);
    346 	(void) wprintw(stat_w, "Fouls    %2d\n", fouled(ms));
    347 	(void) wprintw(stat_w, "Grapples %2d\n", grappled(ms));
    348 
    349 	(void) wmove(stat_w, STAT_2, 0);
    350 	(void) wprintw(stat_w, "    0 %c(%c)\n",
    351 		maxmove(ms, winddir + 3, -1) + '0',
    352 		maxmove(ms, winddir + 3, 1) + '0');
    353 	(void) waddstr(stat_w, "   \\|/\n");
    354 	(void) wprintw(stat_w, "   -^-%c(%c)\n",
    355 		maxmove(ms, winddir + 2, -1) + '0',
    356 		maxmove(ms, winddir + 2, 1) + '0');
    357 	(void) waddstr(stat_w, "   /|\\\n");
    358 	(void) wprintw(stat_w, "    | %c(%c)\n",
    359 		maxmove(ms, winddir + 1, -1) + '0',
    360 		maxmove(ms, winddir + 1, 1) + '0');
    361 	(void) wprintw(stat_w, "   %c(%c)\n",
    362 		maxmove(ms, winddir, -1) + '0',
    363 		maxmove(ms, winddir, 1) + '0');
    364 
    365 	(void) wmove(stat_w, STAT_3, 0);
    366 	(void) wprintw(stat_w, "Load  %c%c %c%c\n",
    367 		loadname[mf->loadL], readyname(mf->readyL),
    368 		loadname[mf->loadR], readyname(mf->readyR));
    369 	(void) wprintw(stat_w, "Hull %2d\n", mc->hull);
    370 	(void) wprintw(stat_w, "Crew %2d %2d %2d\n",
    371 		mc->crew1, mc->crew2, mc->crew3);
    372 	(void) wprintw(stat_w, "Guns %2d %2d\n", mc->gunL, mc->gunR);
    373 	(void) wprintw(stat_w, "Carr %2d %2d\n", mc->carL, mc->carR);
    374 	(void) wprintw(stat_w, "Rigg %d %d %d ", mc->rig1, mc->rig2, mc->rig3);
    375 	if (mc->rig4 < 0)
    376 		(void) waddch(stat_w, '-');
    377 	else
    378 		(void) wprintw(stat_w, "%d", mc->rig4);
    379 	(void) wrefresh(stat_w);
    380 }
    381 
    382 void
    383 draw_slot()
    384 {
    385 	if (!boarding(ms, 0)) {
    386 		(void) mvwaddstr(slot_w, 0, 0, "   ");
    387 		(void) mvwaddstr(slot_w, 1, 0, "   ");
    388 	} else
    389 		(void) mvwaddstr(slot_w, 1, 0, "OBP");
    390 	if (!boarding(ms, 1)) {
    391 		(void) mvwaddstr(slot_w, 2, 0, "   ");
    392 		(void) mvwaddstr(slot_w, 3, 0, "   ");
    393 	} else
    394 		(void) mvwaddstr(slot_w, 3, 0, "DBP");
    395 
    396 	(void) wmove(slot_w, SLOT_Y-4, 0);
    397 	if (mf->RH)
    398 		(void) wprintw(slot_w, "%dRH", mf->RH);
    399 	else
    400 		(void) waddstr(slot_w, "   ");
    401 	(void) wmove(slot_w, SLOT_Y-3, 0);
    402 	if (mf->RG)
    403 		(void) wprintw(slot_w, "%dRG", mf->RG);
    404 	else
    405 		(void) waddstr(slot_w, "   ");
    406 	(void) wmove(slot_w, SLOT_Y-2, 0);
    407 	if (mf->RR)
    408 		(void) wprintw(slot_w, "%dRR", mf->RR);
    409 	else
    410 		(void) waddstr(slot_w, "   ");
    411 
    412 #define Y	(SLOT_Y/2)
    413 	(void) wmove(slot_w, 7, 1);
    414 	(void) wprintw(slot_w,"%d", windspeed);
    415 	(void) mvwaddch(slot_w, Y, 0, ' ');
    416 	(void) mvwaddch(slot_w, Y, 2, ' ');
    417 	(void) mvwaddch(slot_w, Y-1, 0, ' ');
    418 	(void) mvwaddch(slot_w, Y-1, 1, ' ');
    419 	(void) mvwaddch(slot_w, Y-1, 2, ' ');
    420 	(void) mvwaddch(slot_w, Y+1, 0, ' ');
    421 	(void) mvwaddch(slot_w, Y+1, 1, ' ');
    422 	(void) mvwaddch(slot_w, Y+1, 2, ' ');
    423 	(void) wmove(slot_w, Y - dr[winddir], 1 - dc[winddir]);
    424 	switch (winddir) {
    425 	case 1:
    426 	case 5:
    427 		(void) waddch(slot_w, '|');
    428 		break;
    429 	case 2:
    430 	case 6:
    431 		(void) waddch(slot_w, '/');
    432 		break;
    433 	case 3:
    434 	case 7:
    435 		(void) waddch(slot_w, '-');
    436 		break;
    437 	case 4:
    438 	case 8:
    439 		(void) waddch(slot_w, '\\');
    440 		break;
    441 	}
    442 	(void) mvwaddch(slot_w, Y + dr[winddir], 1 + dc[winddir], '+');
    443 	(void) wrefresh(slot_w);
    444 }
    445 
    446 void
    447 draw_board()
    448 {
    449 	int n;
    450 
    451 	(void) clear();
    452 	(void) werase(view_w);
    453 	(void) werase(slot_w);
    454 	(void) werase(scroll_w);
    455 	(void) werase(stat_w);
    456 	(void) werase(turn_w);
    457 
    458 	sc_line = 0;
    459 
    460 	(void) move(BOX_T, BOX_L);
    461 	for (n = 0; n < BOX_X; n++)
    462 		(void) addch('-');
    463 	(void) move(BOX_B, BOX_L);
    464 	for (n = 0; n < BOX_X; n++)
    465 		(void) addch('-');
    466 	for (n = BOX_T+1; n < BOX_B; n++) {
    467 		(void) mvaddch(n, BOX_L, '|');
    468 		(void) mvaddch(n, BOX_R, '|');
    469 	}
    470 	(void) mvaddch(BOX_T, BOX_L, '+');
    471 	(void) mvaddch(BOX_T, BOX_R, '+');
    472 	(void) mvaddch(BOX_B, BOX_L, '+');
    473 	(void) mvaddch(BOX_B, BOX_R, '+');
    474 	(void) refresh();
    475 
    476 #define WSaIM "Wooden Ships & Iron Men"
    477 	(void) wmove(view_w, 2, (VIEW_X - sizeof WSaIM - 1) / 2);
    478 	(void) waddstr(view_w, WSaIM);
    479 	(void) wmove(view_w, 4, (VIEW_X - strlen(cc->name)) / 2);
    480 	(void) waddstr(view_w, cc->name);
    481 	(void) wrefresh(view_w);
    482 
    483 	(void) move(LINE_T, LINE_L);
    484 	(void) printw("Class %d %s (%d guns) '%s' (%c%c)",
    485 		mc->class,
    486 		classname[mc->class],
    487 		mc->guns,
    488 		ms->shipname,
    489 		colours(ms),
    490 		sterncolour(ms));
    491 	(void) refresh();
    492 }
    493 
    494 void
    495 centerview()
    496 {
    497 	viewrow = mf->row - VIEW_Y / 2;
    498 	viewcol = mf->col - VIEW_X / 2;
    499 }
    500 
    501 void
    502 upview()
    503 {
    504 	viewrow -= VIEW_Y / 3;
    505 }
    506 
    507 void
    508 downview()
    509 {
    510 	viewrow += VIEW_Y / 3;
    511 }
    512 
    513 void
    514 leftview()
    515 {
    516 	viewcol -= VIEW_X / 5;
    517 }
    518 
    519 void
    520 rightview()
    521 {
    522 	viewcol += VIEW_X / 5;
    523 }
    524 
    525 void
    526 adjustview()
    527 {
    528 	if (dont_adjust)
    529 		return;
    530 	if (mf->row < viewrow + VIEW_Y/4)
    531 		viewrow = mf->row - (VIEW_Y - VIEW_Y/4);
    532 	else if (mf->row > viewrow + (VIEW_Y - VIEW_Y/4))
    533 		viewrow = mf->row - VIEW_Y/4;
    534 	if (mf->col < viewcol + VIEW_X/8)
    535 		viewcol = mf->col - (VIEW_X - VIEW_X/8);
    536 	else if (mf->col > viewcol + (VIEW_X - VIEW_X/8))
    537 		viewcol = mf->col - VIEW_X/8;
    538 }
    539