]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/dr_3.c
Converted from sgtty to termios api, no longer needs libcompat.
[bsdgames-darwin.git] / sail / dr_3.c
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, 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 /*static char sccsid[] = "from: @(#)dr_3.c 5.4 (Berkeley) 6/1/90";*/
36 static char rcsid[] = "$Id: dr_3.c,v 1.2 1993/08/01 18:51:50 mycroft Exp $";
37 #endif /* not lint */
38
39 #include "driver.h"
40
41 moveall() /* move all comp ships */
42 {
43 register struct ship *sp, *sq; /* r11, r10 */
44 register int n; /* r9 */
45 register int k, l; /* r8, r7 */
46 int row[NSHIP], col[NSHIP], dir[NSHIP], drift[NSHIP];
47 char moved[NSHIP];
48
49 /*
50 * first try to create moves for OUR ships
51 */
52 foreachship(sp) {
53 struct ship *closest;
54 int ma, ta;
55 char af;
56
57 if (sp->file->captain[0] || sp->file->dir == 0)
58 continue;
59 if (!sp->file->struck && windspeed && !snagged(sp)
60 && sp->specs->crew3) {
61 ta = maxturns(sp, &af);
62 ma = maxmove(sp, sp->file->dir, 0);
63 closest = closestenemy(sp, 0, 0);
64 if (closest == 0)
65 *sp->file->movebuf = '\0';
66 else
67 closeon(sp, closest, sp->file->movebuf,
68 ta, ma, af);
69 } else
70 *sp->file->movebuf = '\0';
71 }
72 /*
73 * Then execute the moves for ALL ships (dead ones too),
74 * checking for collisions and snags at each step.
75 * The old positions are saved in row[], col[], dir[].
76 * At the end, we compare and write out the changes.
77 */
78 n = 0;
79 foreachship(sp) {
80 if (snagged(sp))
81 (void) strcpy(sp->file->movebuf, "d");
82 else
83 if (*sp->file->movebuf != 'd')
84 (void) strcat(sp->file->movebuf, "d");
85 row[n] = sp->file->row;
86 col[n] = sp->file->col;
87 dir[n] = sp->file->dir;
88 drift[n] = sp->file->drift;
89 moved[n] = 0;
90 n++;
91 }
92 /*
93 * Now resolve collisions.
94 * This is the tough part.
95 */
96 for (k = 0; stillmoving(k); k++) {
97 /*
98 * Step once.
99 * And propagate the nulls at the end of sp->file->movebuf.
100 */
101 n = 0;
102 foreachship(sp) {
103 if (!sp->file->movebuf[k])
104 sp->file->movebuf[k+1] = '\0';
105 else if (sp->file->dir)
106 step(sp->file->movebuf[k], sp, &moved[n]);
107 n++;
108 }
109 /*
110 * The real stuff.
111 */
112 n = 0;
113 foreachship(sp) {
114 if (sp->file->dir == 0 || isolated(sp))
115 goto cont1;
116 l = 0;
117 foreachship(sq) {
118 char snap = 0;
119
120 if (sp == sq)
121 goto cont2;
122 if (sq->file->dir == 0)
123 goto cont2;
124 if (!push(sp, sq))
125 goto cont2;
126 if (snagged2(sp, sq) && range(sp, sq) > 1)
127 snap++;
128 if (!range(sp, sq) && !fouled2(sp, sq)) {
129 makesignal(sp,
130 "collision with %s (%c%c)", sq);
131 if (die() < 4) {
132 makesignal(sp,
133 "fouled with %s (%c%c)",
134 sq);
135 Write(W_FOUL, sp, 0, l, 0, 0, 0);
136 Write(W_FOUL, sq, 0, n, 0, 0, 0);
137 }
138 snap++;
139 }
140 if (snap) {
141 sp->file->movebuf[k + 1] = 0;
142 sq->file->movebuf[k + 1] = 0;
143 sq->file->row = sp->file->row - 1;
144 if (sp->file->dir == 1
145 || sp->file->dir == 5)
146 sq->file->col =
147 sp->file->col - 1;
148 else
149 sq->file->col = sp->file->col;
150 sq->file->dir = sp->file->dir;
151 }
152 cont2:
153 l++;
154 }
155 cont1:
156 n++;
157 }
158 }
159 /*
160 * Clear old moves. And write out new pos.
161 */
162 n = 0;
163 foreachship(sp) {
164 if (sp->file->dir != 0) {
165 *sp->file->movebuf = 0;
166 if (row[n] != sp->file->row)
167 Write(W_ROW, sp, 0, sp->file->row, 0, 0, 0);
168 if (col[n] != sp->file->col)
169 Write(W_COL, sp, 0, sp->file->col, 0, 0, 0);
170 if (dir[n] != sp->file->dir)
171 Write(W_DIR, sp, 0, sp->file->dir, 0, 0, 0);
172 if (drift[n] != sp->file->drift)
173 Write(W_DRIFT, sp, 0, sp->file->drift, 0, 0, 0);
174 }
175 n++;
176 }
177 }
178
179 stillmoving(k)
180 register int k;
181 {
182 register struct ship *sp;
183
184 foreachship(sp)
185 if (sp->file->movebuf[k])
186 return 1;
187 return 0;
188 }
189
190 isolated(ship)
191 register struct ship *ship;
192 {
193 register struct ship *sp;
194
195 foreachship(sp) {
196 if (ship != sp && range(ship, sp) <= 10)
197 return 0;
198 }
199 return 1;
200 }
201
202 push(from, to)
203 register struct ship *from, *to;
204 {
205 register int bs, sb;
206
207 sb = to->specs->guns;
208 bs = from->specs->guns;
209 if (sb > bs)
210 return 1;
211 if (sb < bs)
212 return 0;
213 return from < to;
214 }
215
216 step(com, sp, moved)
217 char com;
218 register struct ship *sp;
219 char *moved;
220 {
221 register int dist;
222
223 switch (com) {
224 case 'r':
225 if (++sp->file->dir == 9)
226 sp->file->dir = 1;
227 break;
228 case 'l':
229 if (--sp->file->dir == 0)
230 sp->file->dir = 8;
231 break;
232 case '0': case '1': case '2': case '3':
233 case '4': case '5': case '6': case '7':
234 if (sp->file->dir % 2 == 0)
235 dist = dtab[com - '0'];
236 else
237 dist = com - '0';
238 sp->file->row -= dr[sp->file->dir] * dist;
239 sp->file->col -= dc[sp->file->dir] * dist;
240 *moved = 1;
241 break;
242 case 'b':
243 break;
244 case 'd':
245 if (!*moved) {
246 if (windspeed != 0 && ++sp->file->drift > 2 &&
247 (sp->specs->class >= 3 && !snagged(sp)
248 || (turn & 1) == 0)) {
249 sp->file->row -= dr[winddir];
250 sp->file->col -= dc[winddir];
251 }
252 } else
253 sp->file->drift = 0;
254 break;
255 }
256 }
257
258 sendbp(from, to, sections, isdefense)
259 register struct ship *from, *to;
260 int sections;
261 char isdefense;
262 {
263 int n;
264 register struct BP *bp;
265
266 bp = isdefense ? from->file->DBP : from->file->OBP;
267 for (n = 0; n < NBP && bp[n].turnsent; n++)
268 ;
269 if (n < NBP && sections) {
270 Write(isdefense ? W_DBP : W_OBP, from, 0,
271 n, turn, to->file->index, sections);
272 if (isdefense)
273 makesignal(from, "repelling boarders",
274 (struct ship *)0);
275 else
276 makesignal(from, "boarding the %s (%c%c)", to);
277 }
278 }
279
280 toughmelee(ship, to, isdefense, count)
281 register struct ship *ship, *to;
282 int isdefense, count;
283 {
284 register struct BP *bp;
285 register obp = 0;
286 int n, OBP = 0, DBP = 0, dbp = 0;
287 int qual;
288
289 qual = ship->specs->qual;
290 bp = isdefense ? ship->file->DBP : ship->file->OBP;
291 for (n = 0; n < NBP; n++, bp++) {
292 if (bp->turnsent && (to == bp->toship || isdefense)) {
293 obp += bp->mensent / 100
294 ? ship->specs->crew1 * qual : 0;
295 obp += (bp->mensent % 100)/10
296 ? ship->specs->crew2 * qual : 0;
297 obp += bp->mensent % 10
298 ? ship->specs->crew3 * qual : 0;
299 }
300 }
301 if (count || isdefense)
302 return obp;
303 OBP = toughmelee(to, ship, 0, count + 1);
304 dbp = toughmelee(ship, to, 1, count + 1);
305 DBP = toughmelee(to, ship, 1, count + 1);
306 if (OBP > obp + 10 || OBP + DBP >= obp + dbp + 10)
307 return 1;
308 else
309 return 0;
310 }
311
312 reload()
313 {
314 register struct ship *sp;
315
316 foreachship(sp) {
317 sp->file->loadwith = 0;
318 }
319 }
320
321 checksails()
322 {
323 register struct ship *sp;
324 register int rig, full;
325 struct ship *close;
326
327 foreachship(sp) {
328 if (sp->file->captain[0] != 0)
329 continue;
330 rig = sp->specs->rig1;
331 if (windspeed == 6 || windspeed == 5 && sp->specs->class > 4)
332 rig = 0;
333 if (rig && sp->specs->crew3) {
334 close = closestenemy(sp, 0, 0);
335 if (close != 0) {
336 if (range(sp, close) > 9)
337 full = 1;
338 else
339 full = 0;
340 } else
341 full = 0;
342 } else
343 full = 0;
344 if ((sp->file->FS != 0) != full)
345 Write(W_FS, sp, 0, full, 0, 0, 0);
346 }
347 }