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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)dr_3.c 8.1 (Berkeley) 5/31/93
30 * $FreeBSD: src/games/sail/dr_3.c,v 1.6 1999/11/30 03:49:32 billf Exp $
35 static bool stillmoving(int);
36 static bool isolated(struct ship *);
37 static bool push(struct ship *, struct ship *);
38 static void step(char, struct ship *, char *);
40 /* move all comp ships */
47 int row[NSHIP], col[NSHIP], dir[NSHIP], drift[NSHIP];
51 * first try to create moves for OUR ships
58 if (sp->file->captain[0] || sp->file->dir == 0)
60 if (!sp->file->struck && windspeed && !snagged(sp)
61 && sp->specs->crew3) {
62 ta = maxturns(sp, &af);
63 ma = maxmove(sp, sp->file->dir, 0);
64 closest = closestenemy(sp, 0, 0);
66 *sp->file->movebuf = '\0';
68 closeon(sp, closest, sp->file->movebuf,
71 *sp->file->movebuf = '\0';
74 * Then execute the moves for ALL ships (dead ones too),
75 * checking for collisions and snags at each step.
76 * The old positions are saved in row[], col[], dir[].
77 * At the end, we compare and write out the changes.
82 strcpy(sp->file->movebuf, "d");
84 if (*sp->file->movebuf != 'd')
85 strcat(sp->file->movebuf, "d");
86 row[n] = sp->file->row;
87 col[n] = sp->file->col;
88 dir[n] = sp->file->dir;
89 drift[n] = sp->file->drift;
94 * Now resolve collisions.
95 * This is the tough part.
97 for (k = 0; stillmoving(k); k++) {
100 * And propagate the nulls at the end of sp->file->movebuf.
104 if (!sp->file->movebuf[k])
105 sp->file->movebuf[k+1] = '\0';
106 else if (sp->file->dir)
107 step(sp->file->movebuf[k], sp, &moved[n]);
115 if (sp->file->dir == 0 || isolated(sp))
123 if (sq->file->dir == 0)
127 if (snagged2(sp, sq) && range(sp, sq) > 1)
129 if (!range(sp, sq) && !fouled2(sp, sq)) {
131 "collision with %s (%c%c)", sq);
134 "fouled with %s (%c%c)",
136 Write(W_FOUL, sp, l, 0, 0, 0);
137 Write(W_FOUL, sq, n, 0, 0, 0);
142 sp->file->movebuf[k + 1] = 0;
143 sq->file->movebuf[k + 1] = 0;
144 sq->file->row = sp->file->row - 1;
145 if (sp->file->dir == 1
146 || sp->file->dir == 5)
150 sq->file->col = sp->file->col;
151 sq->file->dir = sp->file->dir;
161 * Clear old moves. And write out new pos.
165 if (sp->file->dir != 0) {
166 *sp->file->movebuf = 0;
167 if (row[n] != sp->file->row)
168 Write(W_ROW, sp, sp->file->row, 0, 0, 0);
169 if (col[n] != sp->file->col)
170 Write(W_COL, sp, sp->file->col, 0, 0, 0);
171 if (dir[n] != sp->file->dir)
172 Write(W_DIR, sp, sp->file->dir, 0, 0, 0);
173 if (drift[n] != sp->file->drift)
174 Write(W_DRIFT, sp, sp->file->drift, 0, 0, 0);
186 if (sp->file->movebuf[k])
192 isolated(struct ship *ship)
197 if (ship != sp && range(ship, sp) <= 10)
204 push(struct ship *from, struct ship *to)
208 sb = to->specs->guns;
209 bs = from->specs->guns;
218 step(char com, struct ship *sp, char *moved)
224 if (++sp->file->dir == 9)
228 if (--sp->file->dir == 0)
231 case '0': case '1': case '2': case '3':
232 case '4': case '5': case '6': case '7':
233 if (sp->file->dir % 2 == 0)
234 dist = dtab[com - '0'];
237 sp->file->row -= dr[sp->file->dir] * dist;
238 sp->file->col -= dc[sp->file->dir] * dist;
245 if (windspeed != 0 && ++sp->file->drift > 2 &&
246 ((sp->specs->class >= 3 && !snagged(sp))
247 || (turn & 1) == 0)) {
248 sp->file->row -= dr[winddir];
249 sp->file->col -= dc[winddir];
259 sendbp(struct ship *from, struct ship *to, int sections, char isdefense)
264 bp = isdefense ? from->file->DBP : from->file->OBP;
265 for (n = 0; n < NBP && bp[n].turnsent; n++)
267 if (n < NBP && sections) {
268 Write(isdefense ? W_DBP : W_OBP, from,
269 n, turn, to->file->index, sections);
271 makesignal(from, "repelling boarders", NULL);
273 makesignal(from, "boarding the %s (%c%c)", to);
278 toughmelee(struct ship *ship, struct ship *to, int isdefense, int count)
282 int n, OBP = 0, DBP = 0, dbp = 0;
285 qual = ship->specs->qual;
286 bp = isdefense ? ship->file->DBP : ship->file->OBP;
287 for (n = 0; n < NBP; n++, bp++) {
288 if (bp->turnsent && (to == bp->toship || isdefense)) {
289 obp += bp->mensent / 100
290 ? ship->specs->crew1 * qual : 0;
291 obp += (bp->mensent % 100)/10
292 ? ship->specs->crew2 * qual : 0;
293 obp += bp->mensent % 10
294 ? ship->specs->crew3 * qual : 0;
297 if (count || isdefense)
299 OBP = toughmelee(to, ship, 0, count + 1);
300 dbp = toughmelee(ship, to, 1, count + 1);
301 DBP = toughmelee(to, ship, 1, count + 1);
302 if (OBP > obp + 10 || OBP + DBP >= obp + dbp + 10)
314 sp->file->loadwith = 0;
323 struct ship *closest;
326 if (sp->file->captain[0] != 0)
328 rig = sp->specs->rig1;
329 if (windspeed == 6 || (windspeed == 5 && sp->specs->class > 4))
331 if (rig && sp->specs->crew3) {
332 closest = closestenemy(sp, 0, 0);
333 if (closest != NULL) {
334 if (range(sp, closest) > 9)
342 if ((sp->file->FS != 0) != full)
343 Write(W_FS, sp, full, 0, 0, 0);