]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/pl_7.c
Move all curses bits to pl_7.c.
[bsdgames-darwin.git] / sail / pl_7.c
1 /* $NetBSD: pl_7.c,v 1.33 2009/03/15 00:35:42 dholland 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. 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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)pl_7.c 8.1 (Berkeley) 5/31/93";
36 #else
37 __RCSID("$NetBSD: pl_7.c,v 1.33 2009/03/15 00:35:42 dholland Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <curses.h>
42 #include <err.h>
43 #include <signal.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <string.h>
49 #include "extern.h"
50 #include "player.h"
51 #include "display.h"
52
53 static void Scroll(void);
54 static void endprompt(int);
55 static void adjustview(void);
56
57 /*
58 * Display interface
59 */
60
61 static char sc_hasprompt;
62 static const char *sc_prompt;
63 static const char *sc_buf;
64 static int sc_line;
65
66 static WINDOW *view_w;
67 static WINDOW *slot_w;
68 static WINDOW *scroll_w;
69 static WINDOW *stat_w;
70 static WINDOW *turn_w;
71
72 int done_curses;
73 int loaded, fired, changed, repaired;
74 int dont_adjust;
75 int viewrow, viewcol;
76 char movebuf[sizeof SHIP(0)->file->movebuf];
77 int player;
78 struct ship *ms; /* memorial structure, &cc->ship[player] */
79 struct File *mf; /* ms->file */
80 struct shipspecs *mc; /* ms->specs */
81
82 void
83 initscreen(void)
84 {
85 if (signal(SIGTSTP, SIG_DFL) == SIG_ERR) {
86 err(1, "signal(SIGTSTP)");
87 }
88
89 if (initscr() == NULL) {
90 errx(1, "Can't sail on this terminal.");
91 }
92 if (STAT_R >= COLS || SCROLL_Y <= 0) {
93 errx(1, "Window/terminal not large enough.");
94 }
95
96 view_w = newwin(VIEW_Y, VIEW_X, VIEW_T, VIEW_L);
97 slot_w = newwin(SLOT_Y, SLOT_X, SLOT_T, SLOT_L);
98 scroll_w = newwin(SCROLL_Y, SCROLL_X, SCROLL_T, SCROLL_L);
99 stat_w = newwin(STAT_Y, STAT_X, STAT_T, STAT_L);
100 turn_w = newwin(TURN_Y, TURN_X, TURN_T, TURN_L);
101
102 if (view_w == NULL ||
103 slot_w == NULL ||
104 scroll_w == NULL ||
105 stat_w == NULL ||
106 turn_w == NULL) {
107 endwin();
108 errx(1, "Curses initialization failed.");
109 }
110
111 leaveok(view_w, 1);
112 leaveok(slot_w, 1);
113 leaveok(stat_w, 1);
114 leaveok(turn_w, 1);
115 noecho();
116 cbreak();
117
118 done_curses++;
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 __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 send_ddead();
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 send_alive();
169 if (mf->FS && (!mc->rig1 || windspeed == 6))
170 send_fs(ms, 0);
171 if (mf->FS == 1)
172 send_fs(ms, 2);
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 if (!done_curses)
196 return;
197 va_start(ap, ship);
198 if (*fmt == '\a')
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 if (!done_curses)
213 return;
214 va_start(ap, fmt);
215 if (*fmt == '\a')
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 /* Called after show_[od]bp. Shouldn't really exist... XXX */
499 void
500 display_refresh_slot_w(void)
501 {
502 blockalarm();
503 wrefresh(slot_w);
504 unblockalarm();
505 }
506
507 void
508 display_show_obp(int which, bool show)
509 {
510 wmove(slot_w, 0, which);
511 waddch(slot_w, show ? '1' + which : ' ');
512 mvwaddstr(slot_w, 1, 0, "OBP");
513 }
514
515 void
516 display_show_dbp(int which, bool show)
517 {
518 wmove(slot_w, 2, which);
519 waddch(slot_w, show ? '1' + which : ' ');
520 mvwaddstr(slot_w, 3, 0, "DBP");
521 }
522
523 void
524 centerview(void)
525 {
526 viewrow = mf->row - VIEW_Y / 2;
527 viewcol = mf->col - VIEW_X / 2;
528 }
529
530 void
531 upview(void)
532 {
533 viewrow -= VIEW_Y / 3;
534 }
535
536 void
537 downview(void)
538 {
539 viewrow += VIEW_Y / 3;
540 }
541
542 void
543 leftview(void)
544 {
545 viewcol -= VIEW_X / 5;
546 }
547
548 void
549 rightview(void)
550 {
551 viewcol += VIEW_X / 5;
552 }
553
554 static void
555 adjustview(void)
556 {
557 if (dont_adjust)
558 return;
559 if (mf->row < viewrow + VIEW_Y/4)
560 viewrow = mf->row - (VIEW_Y - VIEW_Y/4);
561 else if (mf->row > viewrow + (VIEW_Y - VIEW_Y/4))
562 viewrow = mf->row - VIEW_Y/4;
563 if (mf->col < viewcol + VIEW_X/8)
564 viewcol = mf->col - (VIEW_X - VIEW_X/8);
565 else if (mf->col > viewcol + (VIEW_X - VIEW_X/8))
566 viewcol = mf->col - VIEW_X/8;
567 }