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