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