2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)dr_3.c 8.1 (Berkeley) 5/31/93";
38 static const char rcsid[] =
39 "$FreeBSD: src/games/sail/dr_3.c,v 1.6 1999/11/30 03:49:32 billf Exp $";
44 moveall() /* move all comp ships */
46 struct ship *sp, *sq; /* r11, r10 */
48 int k, l; /* r8, r7 */
49 int row[NSHIP], col[NSHIP], dir[NSHIP], drift[NSHIP];
53 * first try to create moves for OUR ships
60 if (sp->file->captain[0] || sp->file->dir == 0)
62 if (!sp->file->struck && windspeed && !snagged(sp)
63 && sp->specs->crew3) {
64 ta = maxturns(sp, &af);
65 ma = maxmove(sp, sp->file->dir, 0);
66 closest = closestenemy(sp, 0, 0);
68 *sp->file->movebuf = '\0';
70 closeon(sp, closest, sp->file->movebuf,
73 *sp->file->movebuf = '\0';
76 * Then execute the moves for ALL ships (dead ones too),
77 * checking for collisions and snags at each step.
78 * The old positions are saved in row[], col[], dir[].
79 * At the end, we compare and write out the changes.
84 (void) strcpy(sp->file->movebuf, "d");
86 if (*sp->file->movebuf != 'd')
87 (void) strcat(sp->file->movebuf, "d");
88 row[n] = sp->file->row;
89 col[n] = sp->file->col;
90 dir[n] = sp->file->dir;
91 drift[n] = sp->file->drift;
96 * Now resolve collisions.
97 * This is the tough part.
99 for (k = 0; stillmoving(k); k++) {
102 * And propagate the nulls at the end of sp->file->movebuf.
106 if (!sp->file->movebuf[k])
107 sp->file->movebuf[k+1] = '\0';
108 else if (sp->file->dir)
109 step(sp->file->movebuf[k], sp, &moved[n]);
117 if (sp->file->dir == 0 || isolated(sp))
125 if (sq->file->dir == 0)
129 if (snagged2(sp, sq) && range(sp, sq) > 1)
131 if (!range(sp, sq) && !fouled2(sp, sq)) {
133 "collision with %s (%c%c)", sq);
136 "fouled with %s (%c%c)",
138 Write(W_FOUL, sp, 0, l, 0, 0, 0);
139 Write(W_FOUL, sq, 0, n, 0, 0, 0);
144 sp->file->movebuf[k + 1] = 0;
145 sq->file->movebuf[k + 1] = 0;
146 sq->file->row = sp->file->row - 1;
147 if (sp->file->dir == 1
148 || sp->file->dir == 5)
152 sq->file->col = sp->file->col;
153 sq->file->dir = sp->file->dir;
163 * Clear old moves. And write out new pos.
167 if (sp->file->dir != 0) {
168 *sp->file->movebuf = 0;
169 if (row[n] != sp->file->row)
170 Write(W_ROW, sp, 0, sp->file->row, 0, 0, 0);
171 if (col[n] != sp->file->col)
172 Write(W_COL, sp, 0, sp->file->col, 0, 0, 0);
173 if (dir[n] != sp->file->dir)
174 Write(W_DIR, sp, 0, sp->file->dir, 0, 0, 0);
175 if (drift[n] != sp->file->drift)
176 Write(W_DRIFT, sp, 0, sp->file->drift, 0, 0, 0);
188 if (sp->file->movebuf[k])
199 if (ship != sp && range(ship, sp) <= 10)
206 struct ship *from, *to;
210 sb = to->specs->guns;
211 bs = from->specs->guns;
228 if (++sp->file->dir == 9)
232 if (--sp->file->dir == 0)
235 case '0': case '1': case '2': case '3':
236 case '4': case '5': case '6': case '7':
237 if (sp->file->dir % 2 == 0)
238 dist = dtab[com - '0'];
241 sp->file->row -= dr[sp->file->dir] * dist;
242 sp->file->col -= dc[sp->file->dir] * dist;
249 if (windspeed != 0 && ++sp->file->drift > 2 &&
250 (sp->specs->class >= 3 && !snagged(sp)
251 || (turn & 1) == 0)) {
252 sp->file->row -= dr[winddir];
253 sp->file->col -= dc[winddir];
261 sendbp(from, to, sections, isdefense)
262 struct ship *from, *to;
269 bp = isdefense ? from->file->DBP : from->file->OBP;
270 for (n = 0; n < NBP && bp[n].turnsent; n++)
272 if (n < NBP && sections) {
273 Write(isdefense ? W_DBP : W_OBP, from, 0,
274 n, turn, to->file->index, sections);
276 makesignal(from, "repelling boarders",
279 makesignal(from, "boarding the %s (%c%c)", to);
283 toughmelee(ship, to, isdefense, count)
284 struct ship *ship, *to;
285 int isdefense, count;
289 int n, OBP = 0, DBP = 0, dbp = 0;
292 qual = ship->specs->qual;
293 bp = isdefense ? ship->file->DBP : ship->file->OBP;
294 for (n = 0; n < NBP; n++, bp++) {
295 if (bp->turnsent && (to == bp->toship || isdefense)) {
296 obp += bp->mensent / 100
297 ? ship->specs->crew1 * qual : 0;
298 obp += (bp->mensent % 100)/10
299 ? ship->specs->crew2 * qual : 0;
300 obp += bp->mensent % 10
301 ? ship->specs->crew3 * qual : 0;
304 if (count || isdefense)
306 OBP = toughmelee(to, ship, 0, count + 1);
307 dbp = toughmelee(ship, to, 1, count + 1);
308 DBP = toughmelee(to, ship, 1, count + 1);
309 if (OBP > obp + 10 || OBP + DBP >= obp + dbp + 10)
320 sp->file->loadwith = 0;
331 if (sp->file->captain[0] != 0)
333 rig = sp->specs->rig1;
334 if (windspeed == 6 || windspeed == 5 && sp->specs->class > 4)
336 if (rig && sp->specs->crew3) {
337 close = closestenemy(sp, 0, 0);
339 if (range(sp, close) > 9)
347 if ((sp->file->FS != 0) != full)
348 Write(W_FS, sp, 0, full, 0, 0, 0);