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