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