Merge from vendor branch OPENSSL:
[dragonfly.git] / games / sail / pl_7.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
1de703da
MD
32 *
33 * @(#)pl_7.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/games/sail/pl_7.c,v 1.7 1999/11/30 03:49:37 billf Exp $
4be74d36 35 * $DragonFly: src/games/sail/pl_7.c,v 1.3 2006/09/03 17:33:13 pavalos Exp $
984263bc
MD
36 */
37
984263bc
MD
38#include <sys/ttydefaults.h>
39#include <string.h>
40#include "player.h"
41
42
43/*
44 * Display interface
45 */
46
47static char sc_hasprompt;
4be74d36
PA
48static const char *sc_prompt;
49static const char *sc_buf;
984263bc
MD
50static int sc_line;
51
4be74d36
PA
52static void Scroll(void);
53static void prompt(const char *, struct ship *);
54static void endprompt(char);
55static void adjustview(void);
56
57void
58initscreen(void)
984263bc
MD
59{
60 /* initscr() already done in SCREENTEST() */
61 view_w = newwin(VIEW_Y, VIEW_X, VIEW_T, VIEW_L);
62 slot_w = newwin(SLOT_Y, SLOT_X, SLOT_T, SLOT_L);
63 scroll_w = newwin(SCROLL_Y, SCROLL_X, SCROLL_T, SCROLL_L);
64 stat_w = newwin(STAT_Y, STAT_X, STAT_T, STAT_L);
65 turn_w = newwin(TURN_Y, TURN_X, TURN_T, TURN_L);
66 done_curses++;
4be74d36
PA
67 leaveok(view_w, 1);
68 leaveok(slot_w, 1);
69 leaveok(stat_w, 1);
70 leaveok(turn_w, 1);
984263bc
MD
71 noecho();
72 crmode();
73}
74
4be74d36
PA
75void
76cleanupscreen(void)
984263bc
MD
77{
78 /* alarm already turned off */
79 if (done_curses) {
4be74d36
PA
80 wmove(scroll_w, SCROLL_Y - 1, 0);
81 wclrtoeol(scroll_w);
984263bc
MD
82 draw_screen();
83 endwin();
84 }
85}
86
87void
4be74d36 88newturn(void)
984263bc
MD
89{
90 repaired = loaded = fired = changed = 0;
91 movebuf[0] = '\0';
92
4be74d36 93 alarm(0);
984263bc
MD
94 if (mf->readyL & R_LOADING) {
95 if (mf->readyL & R_DOUBLE)
96 mf->readyL = R_LOADING;
97 else
98 mf->readyL = R_LOADED;
99 }
100 if (mf->readyR & R_LOADING) {
101 if (mf->readyR & R_DOUBLE)
102 mf->readyR = R_LOADING;
103 else
104 mf->readyR = R_LOADED;
105 }
106 if (!hasdriver)
107 Write(W_DDEAD, SHIP(0), 0, 0, 0, 0, 0);
108
109 if (sc_hasprompt) {
4be74d36
PA
110 wmove(scroll_w, sc_line, 0);
111 wclrtoeol(scroll_w);
984263bc
MD
112 }
113 if (Sync() < 0)
114 leave(LEAVE_SYNC);
115 if (!hasdriver)
116 leave(LEAVE_DRIVER);
117 if (sc_hasprompt)
4be74d36 118 wprintw(scroll_w, "%s%s", sc_prompt, sc_buf);
984263bc
MD
119
120 if (turn % 50 == 0)
121 Write(W_ALIVE, SHIP(0), 0, 0, 0, 0, 0);
122 if (mf->FS && (!mc->rig1 || windspeed == 6))
123 Write(W_FS, ms, 0, 0, 0, 0, 0);
124 if (mf->FS == 1)
125 Write(W_FS, ms, 0, 2, 0, 0, 0);
126
127 if (mf->struck)
128 leave(LEAVE_QUIT);
129 if (mf->captured != 0)
130 leave(LEAVE_CAPTURED);
131 if (windspeed == 7)
132 leave(LEAVE_HURRICAN);
133
134 adjustview();
135 draw_screen();
136
4be74d36
PA
137 signal(SIGALRM, (sig_t)newturn);
138 alarm(7);
984263bc
MD
139}
140
141/*VARARGS2*/
4be74d36
PA
142void
143Signal(const char *fmt, struct ship *ship, ...)
984263bc 144{
4be74d36
PA
145 va_list ap;
146 char format[BUFSIZ];
147
984263bc
MD
148 if (!done_curses)
149 return;
4be74d36 150 va_start(ap, ship);
984263bc
MD
151 if (*fmt == '\7')
152 putchar(*fmt++);
153 if (ship == 0)
4be74d36
PA
154 vw_printw(scroll_w, fmt, ap);
155 else {
156 fmtship(format, sizeof(format), fmt, ship);
157 vw_printw(scroll_w, format, ap);
158 }
159 va_end(ap);
984263bc
MD
160 Scroll();
161}
162
4be74d36
PA
163static void
164Scroll(void)
984263bc
MD
165{
166 if (++sc_line >= SCROLL_Y)
167 sc_line = 0;
4be74d36
PA
168 wmove(scroll_w, sc_line, 0);
169 wclrtoeol(scroll_w);
984263bc
MD
170}
171
4be74d36
PA
172static void
173prompt(const char *p, struct ship *ship)
984263bc
MD
174{
175 static char buf[60];
176
177 if (ship != 0) {
4be74d36 178 printf(buf, p, ship->shipname, colours(ship),
984263bc
MD
179 sterncolour(ship));
180 p = buf;
181 }
182 sc_prompt = p;
183 sc_buf = "";
184 sc_hasprompt = 1;
4be74d36 185 waddstr(scroll_w, p);
984263bc
MD
186}
187
4be74d36
PA
188static void
189endprompt(char flag)
984263bc
MD
190{
191 sc_hasprompt = 0;
192 if (flag)
193 Scroll();
194}
195
4be74d36
PA
196int
197sgetch(const char *p, struct ship *ship, char flag)
984263bc
MD
198{
199 int c;
200
201 prompt(p, ship);
202 blockalarm();
4be74d36 203 wrefresh(scroll_w);
984263bc
MD
204 unblockalarm();
205 while ((c = wgetch(scroll_w)) == EOF)
206 ;
207 if (flag && c >= ' ' && c < 0x7f)
4be74d36 208 waddch(scroll_w, c);
984263bc
MD
209 endprompt(flag);
210 return c;
211}
212
4be74d36
PA
213void
214sgetstr(const char *pr, char *buf, int n)
984263bc
MD
215{
216 int c;
217 char *p = buf;
218
219 prompt(pr, (struct ship *)0);
220 sc_buf = buf;
221 for (;;) {
222 *p = 0;
223 blockalarm();
4be74d36 224 wrefresh(scroll_w);
984263bc
MD
225 unblockalarm();
226 while ((c = wgetch(scroll_w)) == EOF)
227 ;
228 switch (c) {
229 case '\n':
230 case '\r':
231 endprompt(1);
232 return;
233 case '\b':
234 if (p > buf) {
4be74d36 235 waddstr(scroll_w, "\b \b");
984263bc
MD
236 p--;
237 }
238 break;
239 default:
240 if (c >= ' ' && c < 0x7f && p < buf + n - 1) {
241 *p++ = c;
4be74d36 242 waddch(scroll_w, c);
984263bc 243 } else
4be74d36 244 putchar(CTRL('g'));
984263bc
MD
245 }
246 }
247}
248
4be74d36
PA
249void
250draw_screen(void)
984263bc
MD
251{
252 draw_view();
253 draw_turn();
254 draw_stat();
255 draw_slot();
4be74d36 256 wrefresh(scroll_w); /* move the cursor */
984263bc
MD
257}
258
4be74d36
PA
259void
260draw_view(void)
984263bc
MD
261{
262 struct ship *sp;
263
4be74d36 264 werase(view_w);
984263bc
MD
265 foreachship(sp) {
266 if (sp->file->dir
267 && sp->file->row > viewrow
268 && sp->file->row < viewrow + VIEW_Y
269 && sp->file->col > viewcol
270 && sp->file->col < viewcol + VIEW_X) {
4be74d36 271 wmove(view_w, sp->file->row - viewrow,
984263bc 272 sp->file->col - viewcol);
4be74d36
PA
273 waddch(view_w, colours(sp));
274 wmove(view_w,
984263bc
MD
275 sternrow(sp) - viewrow,
276 sterncol(sp) - viewcol);
4be74d36 277 waddch(view_w, sterncolour(sp));
984263bc
MD
278 }
279 }
4be74d36 280 wrefresh(view_w);
984263bc
MD
281}
282
4be74d36
PA
283void
284draw_turn(void)
984263bc 285{
4be74d36
PA
286 wmove(turn_w, 0, 0);
287 wprintw(turn_w, "%cTurn %d", dont_adjust?'*':'-', turn);
288 wrefresh(turn_w);
984263bc
MD
289}
290
4be74d36
PA
291void
292draw_stat(void)
984263bc 293{
4be74d36
PA
294 wmove(stat_w, STAT_1, 0);
295 wprintw(stat_w, "Points %3d\n", mf->points);
296 wprintw(stat_w, "Fouls %2d\n", fouled(ms));
297 wprintw(stat_w, "Grapples %2d\n", grappled(ms));
984263bc 298
4be74d36
PA
299 wmove(stat_w, STAT_2, 0);
300 wprintw(stat_w, " 0 %c(%c)\n",
984263bc
MD
301 maxmove(ms, winddir + 3, -1) + '0',
302 maxmove(ms, winddir + 3, 1) + '0');
4be74d36
PA
303 waddstr(stat_w, " \\|/\n");
304 wprintw(stat_w, " -^-%c(%c)\n",
984263bc
MD
305 maxmove(ms, winddir + 2, -1) + '0',
306 maxmove(ms, winddir + 2, 1) + '0');
4be74d36
PA
307 waddstr(stat_w, " /|\\\n");
308 wprintw(stat_w, " | %c(%c)\n",
984263bc
MD
309 maxmove(ms, winddir + 1, -1) + '0',
310 maxmove(ms, winddir + 1, 1) + '0');
4be74d36 311 wprintw(stat_w, " %c(%c)\n",
984263bc
MD
312 maxmove(ms, winddir, -1) + '0',
313 maxmove(ms, winddir, 1) + '0');
314
4be74d36
PA
315 wmove(stat_w, STAT_3, 0);
316 wprintw(stat_w, "Load %c%c %c%c\n",
984263bc
MD
317 loadname[mf->loadL], readyname(mf->readyL),
318 loadname[mf->loadR], readyname(mf->readyR));
4be74d36
PA
319 wprintw(stat_w, "Hull %2d\n", mc->hull);
320 wprintw(stat_w, "Crew %2d %2d %2d\n",
984263bc 321 mc->crew1, mc->crew2, mc->crew3);
4be74d36
PA
322 wprintw(stat_w, "Guns %2d %2d\n", mc->gunL, mc->gunR);
323 wprintw(stat_w, "Carr %2d %2d\n", mc->carL, mc->carR);
324 wprintw(stat_w, "Rigg %d %d %d ", mc->rig1, mc->rig2, mc->rig3);
984263bc 325 if (mc->rig4 < 0)
4be74d36 326 waddch(stat_w, '-');
984263bc 327 else
4be74d36
PA
328 wprintw(stat_w, "%d", mc->rig4);
329 wrefresh(stat_w);
984263bc
MD
330}
331
4be74d36
PA
332void
333draw_slot(void)
984263bc
MD
334{
335 if (!boarding(ms, 0)) {
4be74d36
PA
336 mvwaddstr(slot_w, 0, 0, " ");
337 mvwaddstr(slot_w, 1, 0, " ");
984263bc 338 } else
4be74d36 339 mvwaddstr(slot_w, 1, 0, "OBP");
984263bc 340 if (!boarding(ms, 1)) {
4be74d36
PA
341 mvwaddstr(slot_w, 2, 0, " ");
342 mvwaddstr(slot_w, 3, 0, " ");
984263bc 343 } else
4be74d36 344 mvwaddstr(slot_w, 3, 0, "DBP");
984263bc 345
4be74d36 346 wmove(slot_w, SLOT_Y-4, 0);
984263bc 347 if (mf->RH)
4be74d36 348 wprintw(slot_w, "%dRH", mf->RH);
984263bc 349 else
4be74d36
PA
350 waddstr(slot_w, " ");
351 wmove(slot_w, SLOT_Y-3, 0);
984263bc 352 if (mf->RG)
4be74d36 353 wprintw(slot_w, "%dRG", mf->RG);
984263bc 354 else
4be74d36
PA
355 waddstr(slot_w, " ");
356 wmove(slot_w, SLOT_Y-2, 0);
984263bc 357 if (mf->RR)
4be74d36 358 wprintw(slot_w, "%dRR", mf->RR);
984263bc 359 else
4be74d36 360 waddstr(slot_w, " ");
984263bc
MD
361
362#define Y (SLOT_Y/2)
4be74d36
PA
363 wmove(slot_w, 7, 1);
364 wprintw(slot_w,"%d", windspeed);
365 mvwaddch(slot_w, Y, 0, ' ');
366 mvwaddch(slot_w, Y, 2, ' ');
367 mvwaddch(slot_w, Y-1, 0, ' ');
368 mvwaddch(slot_w, Y-1, 1, ' ');
369 mvwaddch(slot_w, Y-1, 2, ' ');
370 mvwaddch(slot_w, Y+1, 0, ' ');
371 mvwaddch(slot_w, Y+1, 1, ' ');
372 mvwaddch(slot_w, Y+1, 2, ' ');
373 wmove(slot_w, Y - dr[winddir], 1 - dc[winddir]);
984263bc
MD
374 switch (winddir) {
375 case 1:
376 case 5:
4be74d36 377 waddch(slot_w, '|');
984263bc
MD
378 break;
379 case 2:
380 case 6:
4be74d36 381 waddch(slot_w, '/');
984263bc
MD
382 break;
383 case 3:
384 case 7:
4be74d36 385 waddch(slot_w, '-');
984263bc
MD
386 break;
387 case 4:
388 case 8:
4be74d36 389 waddch(slot_w, '\\');
984263bc
MD
390 break;
391 }
4be74d36
PA
392 mvwaddch(slot_w, Y + dr[winddir], 1 + dc[winddir], '+');
393 wrefresh(slot_w);
984263bc
MD
394}
395
4be74d36
PA
396void
397draw_board(void)
984263bc
MD
398{
399 int n;
400
4be74d36
PA
401 clear();
402 werase(view_w);
403 werase(slot_w);
404 werase(scroll_w);
405 werase(stat_w);
406 werase(turn_w);
984263bc
MD
407
408 sc_line = 0;
409
4be74d36 410 move(BOX_T, BOX_L);
984263bc 411 for (n = 0; n < BOX_X; n++)
4be74d36
PA
412 addch('-');
413 move(BOX_B, BOX_L);
984263bc 414 for (n = 0; n < BOX_X; n++)
4be74d36 415 addch('-');
984263bc 416 for (n = BOX_T+1; n < BOX_B; n++) {
4be74d36
PA
417 mvaddch(n, BOX_L, '|');
418 mvaddch(n, BOX_R, '|');
984263bc 419 }
4be74d36
PA
420 mvaddch(BOX_T, BOX_L, '+');
421 mvaddch(BOX_T, BOX_R, '+');
422 mvaddch(BOX_B, BOX_L, '+');
423 mvaddch(BOX_B, BOX_R, '+');
424 refresh();
984263bc
MD
425
426#define WSaIM "Wooden Ships & Iron Men"
4be74d36
PA
427 wmove(view_w, 2, (VIEW_X - sizeof WSaIM - 1) / 2);
428 waddstr(view_w, WSaIM);
429 wmove(view_w, 4, (VIEW_X - strlen(cc->name)) / 2);
430 waddstr(view_w, cc->name);
431 wrefresh(view_w);
432
433 move(LINE_T, LINE_L);
434 printw("Class %d %s (%d guns) '%s' (%c%c)",
984263bc
MD
435 mc->class,
436 classname[mc->class],
437 mc->guns,
438 ms->shipname,
439 colours(ms),
440 sterncolour(ms));
4be74d36 441 refresh();
984263bc
MD
442}
443
4be74d36
PA
444void
445centerview(void)
984263bc
MD
446{
447 viewrow = mf->row - VIEW_Y / 2;
448 viewcol = mf->col - VIEW_X / 2;
449}
450
4be74d36
PA
451void
452upview(void)
984263bc
MD
453{
454 viewrow -= VIEW_Y / 3;
455}
456
4be74d36
PA
457void
458downview(void)
984263bc
MD
459{
460 viewrow += VIEW_Y / 3;
461}
462
4be74d36
PA
463void
464leftview(void)
984263bc
MD
465{
466 viewcol -= VIEW_X / 5;
467}
468
4be74d36
PA
469void
470rightview(void)
984263bc
MD
471{
472 viewcol += VIEW_X / 5;
473}
474
4be74d36
PA
475static void
476adjustview(void)
984263bc
MD
477{
478 if (dont_adjust)
479 return;
480 if (mf->row < viewrow + VIEW_Y/4)
481 viewrow = mf->row - (VIEW_Y - VIEW_Y/4);
482 else if (mf->row > viewrow + (VIEW_Y - VIEW_Y/4))
483 viewrow = mf->row - VIEW_Y/4;
484 if (mf->col < viewcol + VIEW_X/8)
485 viewcol = mf->col - (VIEW_X - VIEW_X/8);
486 else if (mf->col > viewcol + (VIEW_X - VIEW_X/8))
487 viewcol = mf->col - VIEW_X/8;
488}