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