1 /* $NetBSD: pl_7.c,v 1.37 2009/03/15 22:19:23 dholland Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)pl_7.c 8.1 (Berkeley) 5/31/93";
37 __RCSID("$NetBSD: pl_7.c,v 1.37 2009/03/15 22:19:23 dholland Exp $");
55 * Use values above KEY_MAX for custom keycodes. (blymn@ says this is ok)
57 #define KEY_ESC(ch) (KEY_MAX+10+ch)
64 static void draw_view(void);
65 static void draw_turn(void);
66 static void draw_stat(void);
67 static void draw_slot(void);
68 static void draw_board(void);
70 static struct stringarray
*sc_lines
;
71 static unsigned sc_scrollup
;
72 static bool sc_hasprompt
;
73 static bool sc_hideprompt
;
74 static const char *sc_prompt
;
75 static const char *sc_buf
;
77 static WINDOW
*view_w
;
78 static WINDOW
*turn_w
;
79 static WINDOW
*stat_w
;
80 static WINDOW
*slot_w
;
81 static WINDOW
*scroll_w
;
87 int loaded
, fired
, changed
, repaired
;
89 static int viewrow
, viewcol
;
90 char movebuf
[sizeof SHIP(0)->file
->movebuf
];
92 struct ship
*ms
; /* memorial structure, &cc->ship[player] */
93 struct File
*mf
; /* ms->file */
94 struct shipspecs
*mc
; /* ms->specs */
96 ////////////////////////////////////////////////////////////
97 // overall initialization
101 define_esc_key(int ch
)
103 char seq
[3] = { '\x1b', ch
, 0 };
105 define_key(seq
, KEY_ESC(ch
));
113 sc_lines
= stringarray_create();
114 if (sc_lines
== NULL
) {
118 if (signal(SIGTSTP
, SIG_DFL
) == SIG_ERR
) {
119 err(1, "signal(SIGTSTP)");
122 if (initscr() == NULL
) {
123 errx(1, "Can't sail on this terminal.");
125 if (STAT_R
>= COLS
|| SCROLL_Y
<= 0) {
126 errx(1, "Window/terminal not large enough.");
129 view_w
= newwin(VIEW_Y
, VIEW_X
, VIEW_T
, VIEW_L
);
130 slot_w
= newwin(SLOT_Y
, SLOT_X
, SLOT_T
, SLOT_L
);
131 scroll_w
= newwin(SCROLL_Y
, SCROLL_X
, SCROLL_T
, SCROLL_L
);
132 stat_w
= newwin(STAT_Y
, STAT_X
, STAT_T
, STAT_L
);
133 turn_w
= newwin(TURN_Y
, TURN_X
, TURN_T
, TURN_L
);
135 if (view_w
== NULL
||
141 errx(1, "Curses initialization failed.");
154 for (ch
= 0; ch
< 127; ch
++) {
164 /* alarm already turned off */
166 wmove(scroll_w
, SCROLL_Y
- 1, 0);
173 ////////////////////////////////////////////////////////////
174 // scrolling message area
177 scrollarea_add(const char *text
)
186 if (stringarray_add(sc_lines
, copy
, NULL
)) {
193 * XXX this should use leave(), but that won't
194 * currently work right.
201 sync_close(!hasdriver
);
210 unsigned total_lines
;
211 unsigned visible_lines
;
212 unsigned index_of_top
;
219 /* XXX: SCROLL_Y and whatnot should be unsigned too */
220 visible_lines
= SCROLL_Y
- 1;
222 total_lines
= stringarray_num(sc_lines
);
223 if (total_lines
> visible_lines
) {
224 index_of_top
= total_lines
- visible_lines
;
228 if (index_of_top
< sc_scrollup
) {
231 index_of_top
-= sc_scrollup
;
234 for (y
= 0; y
< visible_lines
; y
++) {
235 index_of_y
= index_of_top
+ y
;
236 if (index_of_y
>= total_lines
) {
239 wmove(scroll_w
, y
, 0);
240 waddstr(scroll_w
, stringarray_get(sc_lines
, index_of_y
));
242 if (sc_hasprompt
&& !sc_hideprompt
) {
243 wmove(scroll_w
, SCROLL_Y
-1, 0);
244 waddstr(scroll_w
, sc_prompt
);
245 waddstr(scroll_w
, sc_buf
);
246 cursorx
= strlen(sc_prompt
) + strlen(sc_buf
);
247 wmove(scroll_w
, SCROLL_Y
-1, cursorx
);
250 wmove(scroll_w
, SCROLL_Y
-1, 0);
256 Signal(const char *fmt
, struct ship
*ship
, ...)
269 fmtship(format
, sizeof(format
), fmt
, ship
);
270 vsnprintf(buf
, sizeof(buf
), format
, ap
);
277 Msg(const char *fmt
, ...)
289 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
295 prompt(const char *p
, struct ship
*ship
)
297 static char buf
[BUFSIZ
];
299 fmtship(buf
, sizeof(buf
), p
, ship
);
310 sc_hasprompt
= false;
314 * Next two functions called from newturn() to poke display. Shouldn't
319 display_hide_prompt(void)
321 sc_hideprompt
= true;
327 display_reshow_prompt(void)
329 sc_hideprompt
= false;
336 sgetch(const char *p
, struct ship
*ship
, int flag
)
350 while ((c
= wgetch(scroll_w
)) == EOF
)
352 if (flag
&& c
>= ' ' && c
< 0x7f) {
365 sgetstr(const char *pr
, char *buf
, int n
)
370 prompt(pr
, (struct ship
*)0);
379 while ((c
= wgetch(scroll_w
)) == EOF
)
388 /*waddstr(scroll_w, "\b \b");*/
393 if (c
>= ' ' && c
< 0x7f && p
< buf
+ n
- 1) {
395 /*waddch(scroll_w, c);*/
402 ////////////////////////////////////////////////////////////
403 // drawing of other panes
406 display_force_full_redraw(void)
420 /* move the cursor */
434 && sp
->file
->row
> viewrow
435 && sp
->file
->row
< viewrow
+ VIEW_Y
436 && sp
->file
->col
> viewcol
437 && sp
->file
->col
< viewcol
+ VIEW_X
) {
438 wmove(view_w
, sp
->file
->row
- viewrow
,
439 sp
->file
->col
- viewcol
);
440 waddch(view_w
, colours(sp
));
442 sternrow(sp
) - viewrow
,
443 sterncol(sp
) - viewcol
);
444 waddch(view_w
, sterncolour(sp
));
454 wprintw(turn_w
, "%cTurn %d", dont_adjust
?'*':'-', turn
);
461 wmove(stat_w
, STAT_1
, 0);
462 wprintw(stat_w
, "Points %3d\n", mf
->points
);
463 wprintw(stat_w
, "Fouls %2d\n", fouled(ms
));
464 wprintw(stat_w
, "Grapples %2d\n", grappled(ms
));
466 wmove(stat_w
, STAT_2
, 0);
467 wprintw(stat_w
, " 0 %c(%c)\n",
468 maxmove(ms
, winddir
+ 3, -1) + '0',
469 maxmove(ms
, winddir
+ 3, 1) + '0');
470 waddstr(stat_w
, " \\|/\n");
471 wprintw(stat_w
, " -^-%c(%c)\n",
472 maxmove(ms
, winddir
+ 2, -1) + '0',
473 maxmove(ms
, winddir
+ 2, 1) + '0');
474 waddstr(stat_w
, " /|\\\n");
475 wprintw(stat_w
, " | %c(%c)\n",
476 maxmove(ms
, winddir
+ 1, -1) + '0',
477 maxmove(ms
, winddir
+ 1, 1) + '0');
478 wprintw(stat_w
, " %c(%c)\n",
479 maxmove(ms
, winddir
, -1) + '0',
480 maxmove(ms
, winddir
, 1) + '0');
482 wmove(stat_w
, STAT_3
, 0);
483 wprintw(stat_w
, "Load %c%c %c%c\n",
484 loadname
[mf
->loadL
], readyname(mf
->readyL
),
485 loadname
[mf
->loadR
], readyname(mf
->readyR
));
486 wprintw(stat_w
, "Hull %2d\n", mc
->hull
);
487 wprintw(stat_w
, "Crew %2d %2d %2d\n",
488 mc
->crew1
, mc
->crew2
, mc
->crew3
);
489 wprintw(stat_w
, "Guns %2d %2d\n", mc
->gunL
, mc
->gunR
);
490 wprintw(stat_w
, "Carr %2d %2d\n", mc
->carL
, mc
->carR
);
491 wprintw(stat_w
, "Rigg %d %d %d ", mc
->rig1
, mc
->rig2
, mc
->rig3
);
495 wprintw(stat_w
, "%d", mc
->rig4
);
504 if (!boarding(ms
, 0)) {
505 mvwaddstr(slot_w
, 0, 0, " ");
506 mvwaddstr(slot_w
, 1, 0, " ");
509 for (i
= 0; i
< 3; i
++) {
510 waddch(slot_w
, obp
[i
] ? '1'+i
: ' ');
512 mvwaddstr(slot_w
, 1, 0, "OBP");
514 if (!boarding(ms
, 1)) {
515 mvwaddstr(slot_w
, 2, 0, " ");
516 mvwaddstr(slot_w
, 3, 0, " ");
519 for (i
= 0; i
< 3; i
++) {
520 waddch(slot_w
, dbp
[i
] ? '1'+i
: ' ');
522 mvwaddstr(slot_w
, 3, 0, "DBP");
525 wmove(slot_w
, SLOT_Y
-4, 0);
527 wprintw(slot_w
, "%dRH", mf
->RH
);
529 waddstr(slot_w
, " ");
530 wmove(slot_w
, SLOT_Y
-3, 0);
532 wprintw(slot_w
, "%dRG", mf
->RG
);
534 waddstr(slot_w
, " ");
535 wmove(slot_w
, SLOT_Y
-2, 0);
537 wprintw(slot_w
, "%dRR", mf
->RR
);
539 waddstr(slot_w
, " ");
543 wprintw(slot_w
,"%d", windspeed
);
544 mvwaddch(slot_w
, Y
, 0, ' ');
545 mvwaddch(slot_w
, Y
, 2, ' ');
546 mvwaddch(slot_w
, Y
-1, 0, ' ');
547 mvwaddch(slot_w
, Y
-1, 1, ' ');
548 mvwaddch(slot_w
, Y
-1, 2, ' ');
549 mvwaddch(slot_w
, Y
+1, 0, ' ');
550 mvwaddch(slot_w
, Y
+1, 1, ' ');
551 mvwaddch(slot_w
, Y
+1, 2, ' ');
552 wmove(slot_w
, Y
- dr
[winddir
], 1 - dc
[winddir
]);
568 waddch(slot_w
, '\\');
571 mvwaddch(slot_w
, Y
+ dr
[winddir
], 1 + dc
[winddir
], '+');
588 for (n
= 0; n
< BOX_X
; n
++)
591 for (n
= 0; n
< BOX_X
; n
++)
593 for (n
= BOX_T
+1; n
< BOX_B
; n
++) {
594 mvaddch(n
, BOX_L
, '|');
595 mvaddch(n
, BOX_R
, '|');
597 mvaddch(BOX_T
, BOX_L
, '+');
598 mvaddch(BOX_T
, BOX_R
, '+');
599 mvaddch(BOX_B
, BOX_L
, '+');
600 mvaddch(BOX_B
, BOX_R
, '+');
604 #define WSaIM "Wooden Ships & Iron Men"
605 wmove(view_w
, 2, (VIEW_X
- sizeof WSaIM
- 1) / 2);
606 waddstr(view_w
, WSaIM
);
607 wmove(view_w
, 4, (VIEW_X
- strlen(cc
->name
)) / 2);
608 waddstr(view_w
, cc
->name
);
612 move(LINE_T
, LINE_L
);
613 printw("Class %d %s (%d guns) '%s' (%c%c)",
615 classname
[mc
->class],
624 display_set_obp(int which
, bool show
)
630 display_set_dbp(int which
, bool show
)
635 ////////////////////////////////////////////////////////////
636 // external actions on the display
639 display_scroll_pageup(void)
641 unsigned total_lines
, visible_lines
, limit
;
642 unsigned pagesize
= SCROLL_Y
- 2;
644 total_lines
= stringarray_num(sc_lines
);
645 visible_lines
= SCROLL_Y
- 1;
646 limit
= total_lines
- visible_lines
;
648 sc_scrollup
+= pagesize
;
649 if (sc_scrollup
> limit
) {
655 display_scroll_pagedown(void)
657 unsigned pagesize
= SCROLL_Y
- 2;
659 if (sc_scrollup
< pagesize
) {
662 sc_scrollup
-= pagesize
;
669 viewrow
= mf
->row
- VIEW_Y
/ 2;
670 viewcol
= mf
->col
- VIEW_X
/ 2;
676 viewrow
-= VIEW_Y
/ 3;
682 viewrow
+= VIEW_Y
/ 3;
688 viewcol
-= VIEW_X
/ 5;
694 viewcol
+= VIEW_X
/ 5;
697 /* Called from newturn()... rename? */
699 display_adjust_view(void)
703 if (mf
->row
< viewrow
+ VIEW_Y
/4)
704 viewrow
= mf
->row
- (VIEW_Y
- VIEW_Y
/4);
705 else if (mf
->row
> viewrow
+ (VIEW_Y
- VIEW_Y
/4))
706 viewrow
= mf
->row
- VIEW_Y
/4;
707 if (mf
->col
< viewcol
+ VIEW_X
/8)
708 viewcol
= mf
->col
- (VIEW_X
- VIEW_X
/8);
709 else if (mf
->col
> viewcol
+ (VIEW_X
- VIEW_X
/8))
710 viewcol
= mf
->col
- VIEW_X
/8;