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