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