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