xref: /dragonfly/games/sail/pl_6.c (revision 279dd846)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)pl_6.c	8.1 (Berkeley) 5/31/93
30  * $FreeBSD: src/games/sail/pl_6.c,v 1.5 1999/11/30 03:49:37 billf Exp $
31  * $DragonFly: src/games/sail/pl_6.c,v 1.3 2006/09/03 17:33:13 pavalos Exp $
32  */
33 
34 #include "player.h"
35 
36 static bool turned(void);
37 
38 void
39 repair(void)
40 {
41 	char c;
42 	char *repairs;
43 	struct shipspecs *ptr = mc;
44 	int count;
45 
46 #define FIX(x, m) (m - ptr->x > count \
47 	? (ptr->x += count, count = 0) : (count -= m - ptr->x, ptr->x = m))
48 
49 	if (repaired || loaded || fired || changed || turned()) {
50 		Signal("No hands free to repair", NULL);
51 		return;
52 	}
53 	c = sgetch("Repair (hull, guns, rigging)? ", NULL, 1);
54 	switch (c) {
55 		case 'h':
56 			repairs = &mf->RH;
57 			break;
58 		case 'g':
59 			repairs = &mf->RG;
60 			break;
61 		case 'r':
62 			repairs = &mf->RR;
63 			break;
64 		default:
65 			Signal("Avast heaving!", NULL);
66 			return;
67 	}
68 	if (++*repairs >= 3) {
69 		count = 2;
70 		switch (c) {
71 		case 'h': {
72 			int max = ptr->guns/4;
73 			if (ptr->hull < max) {
74 				FIX(hull, max);
75 				Write(W_HULL, ms, ptr->hull, 0, 0, 0);
76 			}
77 			break;
78 			}
79 		case 'g':
80 			if (ptr->gunL < ptr->gunR) {
81 				int max = ptr->guns/5 - ptr->carL;
82 				if (ptr->gunL < max) {
83 					FIX(gunL, max);
84 					Write(W_GUNL, ms, ptr->gunL,
85 						ptr->carL, 0, 0);
86 				}
87 			} else {
88 				int max = ptr->guns/5 - ptr->carR;
89 				if (ptr->gunR < max) {
90 					FIX(gunR, max);
91 					Write(W_GUNR, ms, ptr->gunR,
92 						ptr->carR, 0, 0);
93 				}
94 			}
95 			break;
96 		case 'r':
97 #define X 2
98 			if (ptr->rig4 >= 0 && ptr->rig4 < X) {
99 				FIX(rig4, X);
100 				Write(W_RIG4, ms, ptr->rig4, 0, 0, 0);
101 			}
102 			if (count && ptr->rig3 < X) {
103 				FIX(rig3, X);
104 				Write(W_RIG3, ms, ptr->rig3, 0, 0, 0);
105 			}
106 			if (count && ptr->rig2 < X) {
107 				FIX(rig2, X);
108 				Write(W_RIG2, ms, ptr->rig2, 0, 0, 0);
109 			}
110 			if (count && ptr->rig1 < X) {
111 				FIX(rig1, X);
112 				Write(W_RIG1, ms, ptr->rig1, 0, 0, 0);
113 			}
114 			break;
115 		}
116 		if (count == 2) {
117 			Signal("Repairs completed.", NULL);
118 			*repairs = 2;
119 		} else {
120 			*repairs = 0;
121 			blockalarm();
122 			draw_stat();
123 			unblockalarm();
124 		}
125 	}
126 	blockalarm();
127 	draw_slot();
128 	unblockalarm();
129 	repaired = 1;
130 }
131 
132 static bool
133 turned(void)
134 {
135 	char *p;
136 
137 	for (p = movebuf; *p; p++)
138 		if (*p == 'r' || *p == 'l')
139 			return 1;
140 	return 0;
141 }
142 
143 void
144 loadplayer(void)
145 {
146 	char c;
147 	int loadL, loadR, ready, load;
148 
149 	if (!mc->crew3) {
150 		Signal("Out of crew", NULL);
151 		return;
152 	}
153 	loadL = mf->loadL;
154 	loadR = mf->loadR;
155 	if (!loadL && !loadR) {
156 		c = sgetch("Load which broadside (left or right)? ",
157 			NULL, 1);
158 		if (c == 'r')
159 			loadL = 1;
160 		else
161 			loadR = 1;
162 	}
163 	if ((!loadL && loadR) || (loadL && !loadR)) {
164 		c = sgetch("Reload with (round, double, chain, grape)? ",
165 			NULL, 1);
166 		switch (c) {
167 		case 'r':
168 			load = L_ROUND;
169 			ready = 0;
170 			break;
171 		case 'd':
172 			load = L_DOUBLE;
173 			ready = R_DOUBLE;
174 			break;
175 		case 'c':
176 			load = L_CHAIN;
177 			ready = 0;
178 			break;
179 		case 'g':
180 			load = L_GRAPE;
181 			ready = 0;
182 			break;
183 		default:
184 			Signal("Broadside not loaded.",
185 				NULL);
186 			return;
187 		}
188 		if (!loadR) {
189 			mf->loadR = load;
190 			mf->readyR = ready|R_LOADING;
191 		} else {
192 			mf->loadL = load;
193 			mf->readyL = ready|R_LOADING;
194 		}
195 		loaded = 1;
196 	}
197 }
198