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