dr_3.c revision 1.4 1 /* $NetBSD: dr_3.c,v 1.4 1997/10/13 19:43:32 christos Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)dr_3.c 8.1 (Berkeley) 5/31/93";
40 #else
41 __RCSID("$NetBSD: dr_3.c,v 1.4 1997/10/13 19:43:32 christos Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include "driver.h"
46 #include <stdlib.h>
47
48 void
49 moveall() /* move all comp ships */
50 {
51 struct ship *sp, *sq;
52 int n;
53 int k, l;
54 int row[NSHIP], col[NSHIP], dir[NSHIP], drift[NSHIP];
55 char moved[NSHIP];
56
57 /*
58 * first try to create moves for OUR ships
59 */
60 foreachship(sp) {
61 struct ship *closest;
62 int ma, ta;
63 char af;
64
65 if (sp->file->captain[0] || sp->file->dir == 0)
66 continue;
67 if (!sp->file->struck && windspeed && !snagged(sp)
68 && sp->specs->crew3) {
69 ta = maxturns(sp, &af);
70 ma = maxmove(sp, sp->file->dir, 0);
71 closest = closestenemy(sp, 0, 0);
72 if (closest == 0)
73 *sp->file->movebuf = '\0';
74 else
75 closeon(sp, closest, sp->file->movebuf,
76 ta, ma, af);
77 } else
78 *sp->file->movebuf = '\0';
79 }
80 /*
81 * Then execute the moves for ALL ships (dead ones too),
82 * checking for collisions and snags at each step.
83 * The old positions are saved in row[], col[], dir[].
84 * At the end, we compare and write out the changes.
85 */
86 n = 0;
87 foreachship(sp) {
88 if (snagged(sp))
89 (void) strcpy(sp->file->movebuf, "d");
90 else
91 if (*sp->file->movebuf != 'd')
92 (void) strcat(sp->file->movebuf, "d");
93 row[n] = sp->file->row;
94 col[n] = sp->file->col;
95 dir[n] = sp->file->dir;
96 drift[n] = sp->file->drift;
97 moved[n] = 0;
98 n++;
99 }
100 /*
101 * Now resolve collisions.
102 * This is the tough part.
103 */
104 for (k = 0; stillmoving(k); k++) {
105 /*
106 * Step once.
107 * And propagate the nulls at the end of sp->file->movebuf.
108 */
109 n = 0;
110 foreachship(sp) {
111 if (!sp->file->movebuf[k])
112 sp->file->movebuf[k+1] = '\0';
113 else if (sp->file->dir)
114 step(sp->file->movebuf[k], sp, &moved[n]);
115 n++;
116 }
117 /*
118 * The real stuff.
119 */
120 n = 0;
121 foreachship(sp) {
122 if (sp->file->dir == 0 || isolated(sp))
123 goto cont1;
124 l = 0;
125 foreachship(sq) {
126 char snap = 0;
127
128 if (sp == sq)
129 goto cont2;
130 if (sq->file->dir == 0)
131 goto cont2;
132 if (!push(sp, sq))
133 goto cont2;
134 if (snagged2(sp, sq) && range(sp, sq) > 1)
135 snap++;
136 if (!range(sp, sq) && !fouled2(sp, sq)) {
137 makesignal(sp,
138 "collision with %s (%c%c)", sq);
139 if (die() < 4) {
140 makesignal(sp,
141 "fouled with %s (%c%c)",
142 sq);
143 Write(W_FOUL, sp, 0, l, 0, 0, 0);
144 Write(W_FOUL, sq, 0, n, 0, 0, 0);
145 }
146 snap++;
147 }
148 if (snap) {
149 sp->file->movebuf[k + 1] = 0;
150 sq->file->movebuf[k + 1] = 0;
151 sq->file->row = sp->file->row - 1;
152 if (sp->file->dir == 1
153 || sp->file->dir == 5)
154 sq->file->col =
155 sp->file->col - 1;
156 else
157 sq->file->col = sp->file->col;
158 sq->file->dir = sp->file->dir;
159 }
160 cont2:
161 l++;
162 }
163 cont1:
164 n++;
165 }
166 }
167 /*
168 * Clear old moves. And write out new pos.
169 */
170 n = 0;
171 foreachship(sp) {
172 if (sp->file->dir != 0) {
173 *sp->file->movebuf = 0;
174 if (row[n] != sp->file->row)
175 Write(W_ROW, sp, 0, sp->file->row, 0, 0, 0);
176 if (col[n] != sp->file->col)
177 Write(W_COL, sp, 0, sp->file->col, 0, 0, 0);
178 if (dir[n] != sp->file->dir)
179 Write(W_DIR, sp, 0, sp->file->dir, 0, 0, 0);
180 if (drift[n] != sp->file->drift)
181 Write(W_DRIFT, sp, 0, sp->file->drift, 0, 0, 0);
182 }
183 n++;
184 }
185 }
186
187 int
188 stillmoving(k)
189 int k;
190 {
191 struct ship *sp;
192
193 foreachship(sp)
194 if (sp->file->movebuf[k])
195 return 1;
196 return 0;
197 }
198
199 int
200 isolated(ship)
201 struct ship *ship;
202 {
203 struct ship *sp;
204
205 foreachship(sp) {
206 if (ship != sp && range(ship, sp) <= 10)
207 return 0;
208 }
209 return 1;
210 }
211
212 int
213 push(from, to)
214 struct ship *from, *to;
215 {
216 int bs, sb;
217
218 sb = to->specs->guns;
219 bs = from->specs->guns;
220 if (sb > bs)
221 return 1;
222 if (sb < bs)
223 return 0;
224 return from < to;
225 }
226
227 void
228 step(com, sp, moved)
229 char com;
230 struct ship *sp;
231 char *moved;
232 {
233 int dist;
234
235 switch (com) {
236 case 'r':
237 if (++sp->file->dir == 9)
238 sp->file->dir = 1;
239 break;
240 case 'l':
241 if (--sp->file->dir == 0)
242 sp->file->dir = 8;
243 break;
244 case '0': case '1': case '2': case '3':
245 case '4': case '5': case '6': case '7':
246 if (sp->file->dir % 2 == 0)
247 dist = dtab[com - '0'];
248 else
249 dist = com - '0';
250 sp->file->row -= dr[sp->file->dir] * dist;
251 sp->file->col -= dc[sp->file->dir] * dist;
252 *moved = 1;
253 break;
254 case 'b':
255 break;
256 case 'd':
257 if (!*moved) {
258 if (windspeed != 0 && ++sp->file->drift > 2 &&
259 ((sp->specs->class >= 3 && !snagged(sp))
260 || (turn & 1) == 0)) {
261 sp->file->row -= dr[winddir];
262 sp->file->col -= dc[winddir];
263 }
264 } else
265 sp->file->drift = 0;
266 break;
267 }
268 }
269
270 void
271 sendbp(from, to, sections, isdefense)
272 struct ship *from, *to;
273 int sections;
274 char isdefense;
275 {
276 int n;
277 struct BP *bp;
278
279 bp = isdefense ? from->file->DBP : from->file->OBP;
280 for (n = 0; n < NBP && bp[n].turnsent; n++)
281 ;
282 if (n < NBP && sections) {
283 Write(isdefense ? W_DBP : W_OBP, from, 0,
284 n, turn, to->file->index, sections);
285 if (isdefense)
286 makesignal(from, "repelling boarders",
287 (struct ship *)0);
288 else
289 makesignal(from, "boarding the %s (%c%c)", to);
290 }
291 }
292
293 int
294 toughmelee(ship, to, isdefense, count)
295 struct ship *ship, *to;
296 int isdefense, count;
297 {
298 struct BP *bp;
299 int obp = 0;
300 int n, OBP = 0, DBP = 0, dbp = 0;
301 int qual;
302
303 qual = ship->specs->qual;
304 bp = isdefense ? ship->file->DBP : ship->file->OBP;
305 for (n = 0; n < NBP; n++, bp++) {
306 if (bp->turnsent && (to == bp->toship || isdefense)) {
307 obp += bp->mensent / 100
308 ? ship->specs->crew1 * qual : 0;
309 obp += (bp->mensent % 100)/10
310 ? ship->specs->crew2 * qual : 0;
311 obp += bp->mensent % 10
312 ? ship->specs->crew3 * qual : 0;
313 }
314 }
315 if (count || isdefense)
316 return obp;
317 OBP = toughmelee(to, ship, 0, count + 1);
318 dbp = toughmelee(ship, to, 1, count + 1);
319 DBP = toughmelee(to, ship, 1, count + 1);
320 if (OBP > obp + 10 || OBP + DBP >= obp + dbp + 10)
321 return 1;
322 else
323 return 0;
324 }
325
326 void
327 reload()
328 {
329 struct ship *sp;
330
331 foreachship(sp) {
332 sp->file->loadwith = 0;
333 }
334 }
335
336 void
337 checksails()
338 {
339 struct ship *sp;
340 int rig, full;
341 struct ship *close;
342
343 foreachship(sp) {
344 if (sp->file->captain[0] != 0)
345 continue;
346 rig = sp->specs->rig1;
347 if (windspeed == 6 || (windspeed == 5 && sp->specs->class > 4))
348 rig = 0;
349 if (rig && sp->specs->crew3) {
350 close = closestenemy(sp, 0, 0);
351 if (close != 0) {
352 if (range(sp, close) > 9)
353 full = 1;
354 else
355 full = 0;
356 } else
357 full = 0;
358 } else
359 full = 0;
360 if ((sp->file->FS != 0) != full)
361 Write(W_FS, sp, 0, full, 0, 0, 0);
362 }
363 }
364