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