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