xref: /original-bsd/games/sail/pl_6.c (revision d0e3910b)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)pl_6.c	5.2 (Berkeley) 03/09/88";
15 #endif /* not lint */
16 
17 #include "player.h"
18 
19 repair()
20 {
21 	char c;
22 	register char *repairs;
23 	register struct shipspecs *ptr = mc;
24 	register int count;
25 
26 #define FIX(x, m) (m - ptr->x > count \
27 	? (ptr->x += count, count = 0) : (count -= m - ptr->x, ptr->x = m))
28 
29 	if (repaired || loaded || fired || changed || turned()) {
30 		Signal("No hands free to repair", (struct ship *)0);
31 		return;
32 	}
33 	c = sgetch("Repair (hull, guns, rigging)? ", (struct ship *)0, 1);
34 	switch (c) {
35 		case 'h':
36 			repairs = &mf->RH;
37 			break;
38 		case 'g':
39 			repairs = &mf->RG;
40 			break;
41 		case 'r':
42 			repairs = &mf->RR;
43 			break;
44 		default:
45 			Signal("Avast heaving!", (struct ship *)0);
46 			return;
47 	}
48 	if (++*repairs >= 3) {
49 		count = 2;
50 		switch (c) {
51 		case 'h': {
52 			int max = ptr->guns/4;
53 			if (ptr->hull < max) {
54 				FIX(hull, max);
55 				Write(W_HULL, ms, 0, ptr->hull, 0, 0, 0);
56 			}
57 			break;
58 			}
59 		case 'g':
60 			if (ptr->gunL < ptr->gunR) {
61 				int max = ptr->guns/5 - ptr->carL;
62 				if (ptr->gunL < max) {
63 					FIX(gunL, max);
64 					Write(W_GUNL, ms, 0, ptr->gunL,
65 						ptr->carL, 0, 0);
66 				}
67 			} else {
68 				int max = ptr->guns/5 - ptr->carR;
69 				if (ptr->gunR < max) {
70 					FIX(gunR, max);
71 					Write(W_GUNR, ms, 0, ptr->gunR,
72 						ptr->carR, 0, 0);
73 				}
74 			}
75 			break;
76 		case 'r':
77 #define X 2
78 			if (ptr->rig4 >= 0 && ptr->rig4 < X) {
79 				FIX(rig4, X);
80 				Write(W_RIG4, ms, 0, ptr->rig4, 0, 0, 0);
81 			}
82 			if (count && ptr->rig3 < X) {
83 				FIX(rig3, X);
84 				Write(W_RIG3, ms, 0, ptr->rig3, 0, 0, 0);
85 			}
86 			if (count && ptr->rig2 < X) {
87 				FIX(rig2, X);
88 				Write(W_RIG2, ms, 0, ptr->rig2, 0, 0, 0);
89 			}
90 			if (count && ptr->rig1 < X) {
91 				FIX(rig1, X);
92 				Write(W_RIG1, ms, 0, ptr->rig1, 0, 0, 0);
93 			}
94 			break;
95 		}
96 		if (count == 2) {
97 			Signal("Repairs completed.", (struct ship *)0);
98 			*repairs = 2;
99 		} else {
100 			*repairs = 0;
101 			blockalarm();
102 			draw_stat();
103 			unblockalarm();
104 		}
105 	}
106 	blockalarm();
107 	draw_slot();
108 	unblockalarm();
109 	repaired = 1;
110 }
111 
112 turned()
113 {
114 	register char *p;
115 
116 	for (p = movebuf; *p; p++)
117 		if (*p == 'r' || *p == 'l')
118 			return 1;
119 	return 0;
120 }
121 
122 loadplayer()
123 {
124 	char c;
125 	register loadL, loadR, ready, load;
126 
127 	if (!mc->crew3) {
128 		Signal("Out of crew", (struct ship *)0);
129 		return;
130 	}
131 	loadL = mf->loadL;
132 	loadR = mf->loadR;
133 	if (!loadL && !loadR) {
134 		c = sgetch("Load which broadside (left or right)? ",
135 			(struct ship *)0, 1);
136 		if (c == 'r')
137 			loadL = 1;
138 		else
139 			loadR = 1;
140 	}
141 	if (!loadL && loadR || loadL && !loadR) {
142 		c = sgetch("Reload with (round, double, chain, grape)? ",
143 			(struct ship *)0, 1);
144 		switch (c) {
145 		case 'r':
146 			load = L_ROUND;
147 			ready = 0;
148 			break;
149 		case 'd':
150 			load = L_DOUBLE;
151 			ready = R_DOUBLE;
152 			break;
153 		case 'c':
154 			load = L_CHAIN;
155 			ready = 0;
156 			break;
157 		case 'g':
158 			load = L_GRAPE;
159 			ready = 0;
160 			break;
161 		default:
162 			Signal("Broadside not loaded.",
163 				(struct ship *)0);
164 			return;
165 		}
166 		if (!loadR) {
167 			mf->loadR = load;
168 			mf->readyR = ready|R_LOADING;
169 		} else {
170 			mf->loadL = load;
171 			mf->readyL = ready|R_LOADING;
172 		}
173 		loaded = 1;
174 	}
175 }
176