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