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.3 2006/09/03 17:33:13 pavalos Exp $
40 static bool stillmoving(int);
41 static bool isolated(struct ship
*);
42 static bool push(struct ship
*, struct ship
*);
43 static void step(char, struct ship
*, char *);
46 moveall(void) /* move all comp ships */
48 struct ship
*sp
, *sq
; /* r11, r10 */
50 int k
, l
; /* r8, r7 */
51 int row
[NSHIP
], col
[NSHIP
], dir
[NSHIP
], drift
[NSHIP
];
55 * first try to create moves for OUR ships
62 if (sp
->file
->captain
[0] || sp
->file
->dir
== 0)
64 if (!sp
->file
->struck
&& windspeed
&& !snagged(sp
)
65 && sp
->specs
->crew3
) {
66 ta
= maxturns(sp
, &af
);
67 ma
= maxmove(sp
, sp
->file
->dir
, 0);
68 closest
= closestenemy(sp
, 0, 0);
70 *sp
->file
->movebuf
= '\0';
72 closeon(sp
, closest
, sp
->file
->movebuf
,
75 *sp
->file
->movebuf
= '\0';
78 * Then execute the moves for ALL ships (dead ones too),
79 * checking for collisions and snags at each step.
80 * The old positions are saved in row[], col[], dir[].
81 * At the end, we compare and write out the changes.
86 strcpy(sp
->file
->movebuf
, "d");
88 if (*sp
->file
->movebuf
!= 'd')
89 strcat(sp
->file
->movebuf
, "d");
90 row
[n
] = sp
->file
->row
;
91 col
[n
] = sp
->file
->col
;
92 dir
[n
] = sp
->file
->dir
;
93 drift
[n
] = sp
->file
->drift
;
98 * Now resolve collisions.
99 * This is the tough part.
101 for (k
= 0; stillmoving(k
); k
++) {
104 * And propagate the nulls at the end of sp->file->movebuf.
108 if (!sp
->file
->movebuf
[k
])
109 sp
->file
->movebuf
[k
+1] = '\0';
110 else if (sp
->file
->dir
)
111 step(sp
->file
->movebuf
[k
], sp
, &moved
[n
]);
119 if (sp
->file
->dir
== 0 || isolated(sp
))
127 if (sq
->file
->dir
== 0)
131 if (snagged2(sp
, sq
) && range(sp
, sq
) > 1)
133 if (!range(sp
, sq
) && !fouled2(sp
, sq
)) {
135 "collision with %s (%c%c)", sq
);
138 "fouled with %s (%c%c)",
140 Write(W_FOUL
, sp
, 0, l
, 0, 0, 0);
141 Write(W_FOUL
, sq
, 0, n
, 0, 0, 0);
146 sp
->file
->movebuf
[k
+ 1] = 0;
147 sq
->file
->movebuf
[k
+ 1] = 0;
148 sq
->file
->row
= sp
->file
->row
- 1;
149 if (sp
->file
->dir
== 1
150 || sp
->file
->dir
== 5)
154 sq
->file
->col
= sp
->file
->col
;
155 sq
->file
->dir
= sp
->file
->dir
;
165 * Clear old moves. And write out new pos.
169 if (sp
->file
->dir
!= 0) {
170 *sp
->file
->movebuf
= 0;
171 if (row
[n
] != sp
->file
->row
)
172 Write(W_ROW
, sp
, 0, sp
->file
->row
, 0, 0, 0);
173 if (col
[n
] != sp
->file
->col
)
174 Write(W_COL
, sp
, 0, sp
->file
->col
, 0, 0, 0);
175 if (dir
[n
] != sp
->file
->dir
)
176 Write(W_DIR
, sp
, 0, sp
->file
->dir
, 0, 0, 0);
177 if (drift
[n
] != sp
->file
->drift
)
178 Write(W_DRIFT
, sp
, 0, sp
->file
->drift
, 0, 0, 0);
190 if (sp
->file
->movebuf
[k
])
196 isolated(struct ship
*ship
)
201 if (ship
!= sp
&& range(ship
, sp
) <= 10)
208 push(struct ship
*from
, struct ship
*to
)
212 sb
= to
->specs
->guns
;
213 bs
= from
->specs
->guns
;
222 step(char com
, struct ship
*sp
, char *moved
)
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
];
262 sendbp(struct ship
*from
, struct ship
*to
, int sections
, char isdefense
)
267 bp
= isdefense
? from
->file
->DBP
: from
->file
->OBP
;
268 for (n
= 0; n
< NBP
&& bp
[n
].turnsent
; n
++)
270 if (n
< NBP
&& sections
) {
271 Write(isdefense
? W_DBP
: W_OBP
, from
, 0,
272 n
, turn
, to
->file
->index
, sections
);
274 makesignal(from
, "repelling boarders",
277 makesignal(from
, "boarding the %s (%c%c)", to
);
282 toughmelee(struct ship
*ship
, struct ship
*to
, int isdefense
, int count
)
286 int n
, OBP
= 0, DBP
= 0, dbp
= 0;
289 qual
= ship
->specs
->qual
;
290 bp
= isdefense
? ship
->file
->DBP
: ship
->file
->OBP
;
291 for (n
= 0; n
< NBP
; n
++, bp
++) {
292 if (bp
->turnsent
&& (to
== bp
->toship
|| isdefense
)) {
293 obp
+= bp
->mensent
/ 100
294 ? ship
->specs
->crew1
* qual
: 0;
295 obp
+= (bp
->mensent
% 100)/10
296 ? ship
->specs
->crew2
* qual
: 0;
297 obp
+= bp
->mensent
% 10
298 ? ship
->specs
->crew3
* qual
: 0;
301 if (count
|| isdefense
)
303 OBP
= toughmelee(to
, ship
, 0, count
+ 1);
304 dbp
= toughmelee(ship
, to
, 1, count
+ 1);
305 DBP
= toughmelee(to
, ship
, 1, count
+ 1);
306 if (OBP
> obp
+ 10 || OBP
+ DBP
>= obp
+ dbp
+ 10)
318 sp
->file
->loadwith
= 0;
327 struct ship
*closest
;
330 if (sp
->file
->captain
[0] != 0)
332 rig
= sp
->specs
->rig1
;
333 if (windspeed
== 6 || (windspeed
== 5 && sp
->specs
->class > 4))
335 if (rig
&& sp
->specs
->crew3
) {
336 closest
= closestenemy(sp
, 0, 0);
338 if (range(sp
, closest
) > 9)
346 if ((sp
->file
->FS
!= 0) != full
)
347 Write(W_FS
, sp
, 0, full
, 0, 0, 0);