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