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