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
33 * @(#)dr_3.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/games/sail/dr_3.c,v 1.6 1999/11/30 03:49:32 billf Exp $
35 * $DragonFly: src/games/sail/dr_3.c,v 1.2 2003/06/17 04:25:25 dillon Exp $
40 moveall() /* move all comp ships */
42 struct ship
*sp
, *sq
; /* r11, r10 */
44 int k
, l
; /* r8, r7 */
45 int row
[NSHIP
], col
[NSHIP
], dir
[NSHIP
], drift
[NSHIP
];
49 * first try to create moves for OUR ships
56 if (sp
->file
->captain
[0] || sp
->file
->dir
== 0)
58 if (!sp
->file
->struck
&& windspeed
&& !snagged(sp
)
59 && sp
->specs
->crew3
) {
60 ta
= maxturns(sp
, &af
);
61 ma
= maxmove(sp
, sp
->file
->dir
, 0);
62 closest
= closestenemy(sp
, 0, 0);
64 *sp
->file
->movebuf
= '\0';
66 closeon(sp
, closest
, sp
->file
->movebuf
,
69 *sp
->file
->movebuf
= '\0';
72 * Then execute the moves for ALL ships (dead ones too),
73 * checking for collisions and snags at each step.
74 * The old positions are saved in row[], col[], dir[].
75 * At the end, we compare and write out the changes.
80 (void) strcpy(sp
->file
->movebuf
, "d");
82 if (*sp
->file
->movebuf
!= 'd')
83 (void) strcat(sp
->file
->movebuf
, "d");
84 row
[n
] = sp
->file
->row
;
85 col
[n
] = sp
->file
->col
;
86 dir
[n
] = sp
->file
->dir
;
87 drift
[n
] = sp
->file
->drift
;
92 * Now resolve collisions.
93 * This is the tough part.
95 for (k
= 0; stillmoving(k
); k
++) {
98 * And propagate the nulls at the end of sp->file->movebuf.
102 if (!sp
->file
->movebuf
[k
])
103 sp
->file
->movebuf
[k
+1] = '\0';
104 else if (sp
->file
->dir
)
105 step(sp
->file
->movebuf
[k
], sp
, &moved
[n
]);
113 if (sp
->file
->dir
== 0 || isolated(sp
))
121 if (sq
->file
->dir
== 0)
125 if (snagged2(sp
, sq
) && range(sp
, sq
) > 1)
127 if (!range(sp
, sq
) && !fouled2(sp
, sq
)) {
129 "collision with %s (%c%c)", sq
);
132 "fouled with %s (%c%c)",
134 Write(W_FOUL
, sp
, 0, l
, 0, 0, 0);
135 Write(W_FOUL
, sq
, 0, n
, 0, 0, 0);
140 sp
->file
->movebuf
[k
+ 1] = 0;
141 sq
->file
->movebuf
[k
+ 1] = 0;
142 sq
->file
->row
= sp
->file
->row
- 1;
143 if (sp
->file
->dir
== 1
144 || sp
->file
->dir
== 5)
148 sq
->file
->col
= sp
->file
->col
;
149 sq
->file
->dir
= sp
->file
->dir
;
159 * Clear old moves. And write out new pos.
163 if (sp
->file
->dir
!= 0) {
164 *sp
->file
->movebuf
= 0;
165 if (row
[n
] != sp
->file
->row
)
166 Write(W_ROW
, sp
, 0, sp
->file
->row
, 0, 0, 0);
167 if (col
[n
] != sp
->file
->col
)
168 Write(W_COL
, sp
, 0, sp
->file
->col
, 0, 0, 0);
169 if (dir
[n
] != sp
->file
->dir
)
170 Write(W_DIR
, sp
, 0, sp
->file
->dir
, 0, 0, 0);
171 if (drift
[n
] != sp
->file
->drift
)
172 Write(W_DRIFT
, sp
, 0, sp
->file
->drift
, 0, 0, 0);
184 if (sp
->file
->movebuf
[k
])
195 if (ship
!= sp
&& range(ship
, sp
) <= 10)
202 struct ship
*from
, *to
;
206 sb
= to
->specs
->guns
;
207 bs
= from
->specs
->guns
;
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
];
257 sendbp(from
, to
, sections
, isdefense
)
258 struct ship
*from
, *to
;
265 bp
= isdefense
? from
->file
->DBP
: from
->file
->OBP
;
266 for (n
= 0; n
< NBP
&& bp
[n
].turnsent
; n
++)
268 if (n
< NBP
&& sections
) {
269 Write(isdefense
? W_DBP
: W_OBP
, from
, 0,
270 n
, turn
, to
->file
->index
, sections
);
272 makesignal(from
, "repelling boarders",
275 makesignal(from
, "boarding the %s (%c%c)", to
);
279 toughmelee(ship
, to
, isdefense
, count
)
280 struct ship
*ship
, *to
;
281 int isdefense
, count
;
285 int n
, OBP
= 0, DBP
= 0, dbp
= 0;
288 qual
= ship
->specs
->qual
;
289 bp
= isdefense
? ship
->file
->DBP
: ship
->file
->OBP
;
290 for (n
= 0; n
< NBP
; n
++, bp
++) {
291 if (bp
->turnsent
&& (to
== bp
->toship
|| isdefense
)) {
292 obp
+= bp
->mensent
/ 100
293 ? ship
->specs
->crew1
* qual
: 0;
294 obp
+= (bp
->mensent
% 100)/10
295 ? ship
->specs
->crew2
* qual
: 0;
296 obp
+= bp
->mensent
% 10
297 ? ship
->specs
->crew3
* qual
: 0;
300 if (count
|| isdefense
)
302 OBP
= toughmelee(to
, ship
, 0, count
+ 1);
303 dbp
= toughmelee(ship
, to
, 1, count
+ 1);
304 DBP
= toughmelee(to
, ship
, 1, count
+ 1);
305 if (OBP
> obp
+ 10 || OBP
+ DBP
>= obp
+ dbp
+ 10)
316 sp
->file
->loadwith
= 0;
327 if (sp
->file
->captain
[0] != 0)
329 rig
= sp
->specs
->rig1
;
330 if (windspeed
== 6 || windspeed
== 5 && sp
->specs
->class > 4)
332 if (rig
&& sp
->specs
->crew3
) {
333 close
= closestenemy(sp
, 0, 0);
335 if (range(sp
, close
) > 9)
343 if ((sp
->file
->FS
!= 0) != full
)
344 Write(W_FS
, sp
, 0, full
, 0, 0, 0);