NetBSD/games/sail/pl_7.c

543 lines
11 KiB
C
Raw Normal View History

/* $NetBSD: pl_7.c,v 1.19 2001/01/04 03:51:24 jwise Exp $ */
1995-04-22 14:36:43 +04:00
1993-03-21 12:45:37 +03:00
/*
1995-04-22 14:36:43 +04:00
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
1995-04-22 14:36:43 +04:00
#if 0
static char sccsid[] = "@(#)pl_7.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: pl_7.c,v 1.19 2001/01/04 03:51:24 jwise Exp $");
1995-04-22 14:36:43 +04:00
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#include "player.h"
1993-03-21 12:45:37 +03:00
void initscreen(void);
void cleanupscreen(void);
void newturn(int);
void Signal(const char *, struct ship *, ...);
void Msg(const char *, ...);
static void Scroll(void);
void prompt(const char *, struct ship *);
static void endprompt(int);
int sgetch(const char *, struct ship *, int);
void sgetstr(const char *, char *, int);
void draw_screen(void);
void draw_view(void);
void draw_turn(void);
void draw_stat(void);
void draw_slot(void);
void draw_board(void);
void centerview(void);
void upview(void);
void downview(void);
void leftview(void);
void rightview(void);
static void adjustview(void);
1995-04-22 14:36:43 +04:00
1993-03-21 12:45:37 +03:00
/*
* Display interface
*/
static char sc_hasprompt;
static const char *sc_prompt;
static const char *sc_buf;
1993-03-21 12:45:37 +03:00
static int sc_line;
WINDOW *view_w;
WINDOW *slot_w;
WINDOW *scroll_w;
WINDOW *stat_w;
WINDOW *turn_w;
char done_curses;
char loaded, fired, changed, repaired;
char dont_adjust;
int viewrow, viewcol;
char movebuf[sizeof SHIP(0)->file->movebuf];
int player;
struct ship *ms; /* memorial structure, &cc->ship[player] */
struct File *mf; /* ms->file */
struct shipspecs *mc; /* ms->specs */
void
initscreen(void)
1993-03-21 12:45:37 +03:00
{
if (!SCREENTEST()) {
printf("Can't sail on this terminal.\n");
exit(1);
}
1993-03-21 12:45:37 +03:00
/* initscr() already done in SCREENTEST() */
view_w = newwin(VIEW_Y, VIEW_X, VIEW_T, VIEW_L);
slot_w = newwin(SLOT_Y, SLOT_X, SLOT_T, SLOT_L);
scroll_w = newwin(SCROLL_Y, SCROLL_X, SCROLL_T, SCROLL_L);
stat_w = newwin(STAT_Y, STAT_X, STAT_T, STAT_L);
turn_w = newwin(TURN_Y, TURN_X, TURN_T, TURN_L);
done_curses++;
leaveok(view_w, 1);
leaveok(slot_w, 1);
leaveok(stat_w, 1);
leaveok(turn_w, 1);
1993-03-21 12:45:37 +03:00
noecho();
crmode();
}
void
cleanupscreen(void)
1993-03-21 12:45:37 +03:00
{
/* alarm already turned off */
if (done_curses) {
wmove(scroll_w, SCROLL_Y - 1, 0);
wclrtoeol(scroll_w);
1993-03-21 12:45:37 +03:00
draw_screen();
endwin();
}
}
/*ARGSUSED*/
1993-03-21 12:45:37 +03:00
void
newturn(int n __attribute__((__unused__)))
1993-03-21 12:45:37 +03:00
{
repaired = loaded = fired = changed = 0;
movebuf[0] = '\0';
alarm(0);
1998-08-30 13:19:36 +04:00
if (mf->readyL & R_LOADING) {
1993-03-21 12:45:37 +03:00
if (mf->readyL & R_DOUBLE)
mf->readyL = R_LOADING;
else
mf->readyL = R_LOADED;
1998-08-30 13:19:36 +04:00
}
if (mf->readyR & R_LOADING) {
1993-03-21 12:45:37 +03:00
if (mf->readyR & R_DOUBLE)
mf->readyR = R_LOADING;
else
mf->readyR = R_LOADED;
1998-08-30 13:19:36 +04:00
}
1993-03-21 12:45:37 +03:00
if (!hasdriver)
Write(W_DDEAD, SHIP(0), 0, 0, 0, 0);
1993-03-21 12:45:37 +03:00
if (sc_hasprompt) {
wmove(scroll_w, sc_line, 0);
wclrtoeol(scroll_w);
1993-03-21 12:45:37 +03:00
}
if (Sync() < 0)
leave(LEAVE_SYNC);
if (!hasdriver)
leave(LEAVE_DRIVER);
if (sc_hasprompt)
wprintw(scroll_w, "%s%s", sc_prompt, sc_buf);
1993-03-21 12:45:37 +03:00
if (turn % 50 == 0)
Write(W_ALIVE, SHIP(0), 0, 0, 0, 0);
1993-03-21 12:45:37 +03:00
if (mf->FS && (!mc->rig1 || windspeed == 6))
Write(W_FS, ms, 0, 0, 0, 0);
1993-03-21 12:45:37 +03:00
if (mf->FS == 1)
Write(W_FS, ms, 2, 0, 0, 0);
1993-03-21 12:45:37 +03:00
if (mf->struck)
leave(LEAVE_QUIT);
if (mf->captured != 0)
leave(LEAVE_CAPTURED);
if (windspeed == 7)
leave(LEAVE_HURRICAN);
adjustview();
draw_screen();
signal(SIGALRM, newturn);
alarm(7);
1993-03-21 12:45:37 +03:00
}
/*VARARGS2*/
void
Signal(const char *fmt, struct ship *ship, ...)
1993-03-21 12:45:37 +03:00
{
va_list ap;
char format[BUFSIZ];
va_start(ap, ship);
1993-03-21 12:45:37 +03:00
if (!done_curses)
return;
if (*fmt == '\7')
putchar(*fmt++);
fmtship(format, sizeof(format), fmt, ship);
vwprintw(scroll_w, format, ap);
va_end(ap);
1993-03-21 12:45:37 +03:00
Scroll();
}
/*VARARGS2*/
void
Msg(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (!done_curses)
return;
if (*fmt == '\7')
putchar(*fmt++);
vwprintw(scroll_w, fmt, ap);
va_end(ap);
Scroll();
}
static void
Scroll(void)
1993-03-21 12:45:37 +03:00
{
if (++sc_line >= SCROLL_Y)
sc_line = 0;
wmove(scroll_w, sc_line, 0);
wclrtoeol(scroll_w);
1993-03-21 12:45:37 +03:00
}
void
prompt(const char *p, struct ship *ship)
1993-03-21 12:45:37 +03:00
{
static char buf[BUFSIZ];
1993-03-21 12:45:37 +03:00
fmtship(buf, sizeof(buf), p, ship);
sc_prompt = buf;
1993-03-21 12:45:37 +03:00
sc_buf = "";
sc_hasprompt = 1;
waddstr(scroll_w, buf);
1993-03-21 12:45:37 +03:00
}
static void
endprompt(int flag)
1993-03-21 12:45:37 +03:00
{
sc_hasprompt = 0;
if (flag)
Scroll();
}
int
sgetch(const char *p, struct ship *ship, int flag)
1993-03-21 12:45:37 +03:00
{
int c;
1993-03-21 12:45:37 +03:00
prompt(p, ship);
blockalarm();
wrefresh(scroll_w);
1993-03-21 12:45:37 +03:00
unblockalarm();
while ((c = wgetch(scroll_w)) == EOF)
;
if (flag && c >= ' ' && c < 0x7f)
waddch(scroll_w, c);
1993-03-21 12:45:37 +03:00
endprompt(flag);
return c;
}
void
sgetstr(const char *pr, char *buf, int n)
1993-03-21 12:45:37 +03:00
{
int c;
char *p = buf;
1993-03-21 12:45:37 +03:00
prompt(pr, (struct ship *)0);
sc_buf = buf;
for (;;) {
*p = 0;
blockalarm();
wrefresh(scroll_w);
1993-03-21 12:45:37 +03:00
unblockalarm();
while ((c = wgetch(scroll_w)) == EOF)
;
switch (c) {
case '\n':
case '\r':
endprompt(1);
return;
case '\b':
if (p > buf) {
waddstr(scroll_w, "\b \b");
1993-03-21 12:45:37 +03:00
p--;
}
break;
default:
if (c >= ' ' && c < 0x7f && p < buf + n - 1) {
*p++ = c;
waddch(scroll_w, c);
1993-03-21 12:45:37 +03:00
} else
putchar('\a');
1993-03-21 12:45:37 +03:00
}
}
}
void
draw_screen(void)
1993-03-21 12:45:37 +03:00
{
draw_view();
draw_turn();
draw_stat();
draw_slot();
wrefresh(scroll_w); /* move the cursor */
1993-03-21 12:45:37 +03:00
}
void
draw_view(void)
1993-03-21 12:45:37 +03:00
{
struct ship *sp;
1993-03-21 12:45:37 +03:00
werase(view_w);
1993-03-21 12:45:37 +03:00
foreachship(sp) {
if (sp->file->dir
&& sp->file->row > viewrow
&& sp->file->row < viewrow + VIEW_Y
&& sp->file->col > viewcol
&& sp->file->col < viewcol + VIEW_X) {
wmove(view_w, sp->file->row - viewrow,
1993-03-21 12:45:37 +03:00
sp->file->col - viewcol);
waddch(view_w, colours(sp));
wmove(view_w,
1993-03-21 12:45:37 +03:00
sternrow(sp) - viewrow,
sterncol(sp) - viewcol);
waddch(view_w, sterncolour(sp));
1993-03-21 12:45:37 +03:00
}
}
wrefresh(view_w);
1993-03-21 12:45:37 +03:00
}
void
draw_turn(void)
1993-03-21 12:45:37 +03:00
{
wmove(turn_w, 0, 0);
wprintw(turn_w, "%cTurn %d", dont_adjust?'*':'-', turn);
wrefresh(turn_w);
1993-03-21 12:45:37 +03:00
}
void
draw_stat(void)
1993-03-21 12:45:37 +03:00
{
wmove(stat_w, STAT_1, 0);
wprintw(stat_w, "Points %3d\n", mf->points);
wprintw(stat_w, "Fouls %2d\n", fouled(ms));
wprintw(stat_w, "Grapples %2d\n", grappled(ms));
1993-03-21 12:45:37 +03:00
wmove(stat_w, STAT_2, 0);
wprintw(stat_w, " 0 %c(%c)\n",
1993-03-21 12:45:37 +03:00
maxmove(ms, winddir + 3, -1) + '0',
maxmove(ms, winddir + 3, 1) + '0');
waddstr(stat_w, " \\|/\n");
wprintw(stat_w, " -^-%c(%c)\n",
1993-03-21 12:45:37 +03:00
maxmove(ms, winddir + 2, -1) + '0',
maxmove(ms, winddir + 2, 1) + '0');
waddstr(stat_w, " /|\\\n");
wprintw(stat_w, " | %c(%c)\n",
1993-03-21 12:45:37 +03:00
maxmove(ms, winddir + 1, -1) + '0',
maxmove(ms, winddir + 1, 1) + '0');
wprintw(stat_w, " %c(%c)\n",
1993-03-21 12:45:37 +03:00
maxmove(ms, winddir, -1) + '0',
maxmove(ms, winddir, 1) + '0');
wmove(stat_w, STAT_3, 0);
wprintw(stat_w, "Load %c%c %c%c\n",
1993-03-21 12:45:37 +03:00
loadname[mf->loadL], readyname(mf->readyL),
loadname[mf->loadR], readyname(mf->readyR));
wprintw(stat_w, "Hull %2d\n", mc->hull);
wprintw(stat_w, "Crew %2d %2d %2d\n",
1993-03-21 12:45:37 +03:00
mc->crew1, mc->crew2, mc->crew3);
wprintw(stat_w, "Guns %2d %2d\n", mc->gunL, mc->gunR);
wprintw(stat_w, "Carr %2d %2d\n", mc->carL, mc->carR);
wprintw(stat_w, "Rigg %d %d %d ", mc->rig1, mc->rig2, mc->rig3);
1993-03-21 12:45:37 +03:00
if (mc->rig4 < 0)
waddch(stat_w, '-');
1993-03-21 12:45:37 +03:00
else
wprintw(stat_w, "%d", mc->rig4);
wrefresh(stat_w);
1993-03-21 12:45:37 +03:00
}
void
draw_slot(void)
1993-03-21 12:45:37 +03:00
{
if (!boarding(ms, 0)) {
mvwaddstr(slot_w, 0, 0, " ");
mvwaddstr(slot_w, 1, 0, " ");
1993-03-21 12:45:37 +03:00
} else
mvwaddstr(slot_w, 1, 0, "OBP");
1993-03-21 12:45:37 +03:00
if (!boarding(ms, 1)) {
mvwaddstr(slot_w, 2, 0, " ");
mvwaddstr(slot_w, 3, 0, " ");
1993-03-21 12:45:37 +03:00
} else
mvwaddstr(slot_w, 3, 0, "DBP");
1993-03-21 12:45:37 +03:00
wmove(slot_w, SLOT_Y-4, 0);
1993-03-21 12:45:37 +03:00
if (mf->RH)
wprintw(slot_w, "%dRH", mf->RH);
1993-03-21 12:45:37 +03:00
else
waddstr(slot_w, " ");
wmove(slot_w, SLOT_Y-3, 0);
1993-03-21 12:45:37 +03:00
if (mf->RG)
wprintw(slot_w, "%dRG", mf->RG);
1993-03-21 12:45:37 +03:00
else
waddstr(slot_w, " ");
wmove(slot_w, SLOT_Y-2, 0);
1993-03-21 12:45:37 +03:00
if (mf->RR)
wprintw(slot_w, "%dRR", mf->RR);
1993-03-21 12:45:37 +03:00
else
waddstr(slot_w, " ");
1993-03-21 12:45:37 +03:00
#define Y (SLOT_Y/2)
wmove(slot_w, 7, 1);
wprintw(slot_w,"%d", windspeed);
mvwaddch(slot_w, Y, 0, ' ');
mvwaddch(slot_w, Y, 2, ' ');
mvwaddch(slot_w, Y-1, 0, ' ');
mvwaddch(slot_w, Y-1, 1, ' ');
mvwaddch(slot_w, Y-1, 2, ' ');
mvwaddch(slot_w, Y+1, 0, ' ');
mvwaddch(slot_w, Y+1, 1, ' ');
mvwaddch(slot_w, Y+1, 2, ' ');
wmove(slot_w, Y - dr[winddir], 1 - dc[winddir]);
1993-03-21 12:45:37 +03:00
switch (winddir) {
case 1:
case 5:
waddch(slot_w, '|');
1993-03-21 12:45:37 +03:00
break;
case 2:
case 6:
waddch(slot_w, '/');
1993-03-21 12:45:37 +03:00
break;
case 3:
case 7:
waddch(slot_w, '-');
1993-03-21 12:45:37 +03:00
break;
case 4:
case 8:
waddch(slot_w, '\\');
1993-03-21 12:45:37 +03:00
break;
}
mvwaddch(slot_w, Y + dr[winddir], 1 + dc[winddir], '+');
wrefresh(slot_w);
1993-03-21 12:45:37 +03:00
}
void
draw_board(void)
1993-03-21 12:45:37 +03:00
{
int n;
1993-03-21 12:45:37 +03:00
clear();
werase(view_w);
werase(slot_w);
werase(scroll_w);
werase(stat_w);
werase(turn_w);
1993-03-21 12:45:37 +03:00
sc_line = 0;
move(BOX_T, BOX_L);
1993-03-21 12:45:37 +03:00
for (n = 0; n < BOX_X; n++)
addch('-');
move(BOX_B, BOX_L);
1993-03-21 12:45:37 +03:00
for (n = 0; n < BOX_X; n++)
addch('-');
1993-03-21 12:45:37 +03:00
for (n = BOX_T+1; n < BOX_B; n++) {
mvaddch(n, BOX_L, '|');
mvaddch(n, BOX_R, '|');
1993-03-21 12:45:37 +03:00
}
mvaddch(BOX_T, BOX_L, '+');
mvaddch(BOX_T, BOX_R, '+');
mvaddch(BOX_B, BOX_L, '+');
mvaddch(BOX_B, BOX_R, '+');
refresh();
1993-03-21 12:45:37 +03:00
#define WSaIM "Wooden Ships & Iron Men"
wmove(view_w, 2, (VIEW_X - sizeof WSaIM - 1) / 2);
waddstr(view_w, WSaIM);
wmove(view_w, 4, (VIEW_X - strlen(cc->name)) / 2);
waddstr(view_w, cc->name);
wrefresh(view_w);
move(LINE_T, LINE_L);
printw("Class %d %s (%d guns) '%s' (%c%c)",
1993-03-21 12:45:37 +03:00
mc->class,
classname[mc->class],
mc->guns,
ms->shipname,
colours(ms),
sterncolour(ms));
refresh();
1993-03-21 12:45:37 +03:00
}
void
centerview(void)
1993-03-21 12:45:37 +03:00
{
viewrow = mf->row - VIEW_Y / 2;
viewcol = mf->col - VIEW_X / 2;
}
void
upview(void)
1993-03-21 12:45:37 +03:00
{
viewrow -= VIEW_Y / 3;
}
void
downview(void)
1993-03-21 12:45:37 +03:00
{
viewrow += VIEW_Y / 3;
}
void
leftview(void)
1993-03-21 12:45:37 +03:00
{
viewcol -= VIEW_X / 5;
}
void
rightview(void)
1993-03-21 12:45:37 +03:00
{
viewcol += VIEW_X / 5;
}
static void
adjustview(void)
1993-03-21 12:45:37 +03:00
{
if (dont_adjust)
return;
if (mf->row < viewrow + VIEW_Y/4)
viewrow = mf->row - (VIEW_Y - VIEW_Y/4);
else if (mf->row > viewrow + (VIEW_Y - VIEW_Y/4))
viewrow = mf->row - VIEW_Y/4;
if (mf->col < viewcol + VIEW_X/8)
viewcol = mf->col - (VIEW_X - VIEW_X/8);
else if (mf->col > viewcol + (VIEW_X - VIEW_X/8))
viewcol = mf->col - VIEW_X/8;
}