1 /* $NetBSD: dr_2.c,v 1.24 2009/03/14 20:04:43 dholland Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)dr_2.c 8.1 (Berkeley) 5/31/93";
37 __RCSID("$NetBSD: dr_2.c,v 1.24 2009/03/14 20:04:43 dholland Exp $");
48 #define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
50 static int str_end(const char *);
51 static int score(struct ship
*, struct ship
*, char *, size_t, int);
52 static void move_ship(struct ship
*, const char *, unsigned char *,
53 short *, short *, int *);
54 static void try(struct ship
*f
, struct ship
*t
,
55 char *command
, size_t commandmax
, char *temp
, size_t tempmax
,
56 int ma
, int ta
, bool af
, int vma
, int dir
, int *high
,
58 static void rmend(char *);
60 const int dtab
[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */
69 if (sp
->file
->captain
[0] || sp
->file
->dir
== 0)
72 friendly
= sp
->nationality
== capship(sq
)->nationality
;
74 if (sp
->file
->struck
|| sp
->file
->captured
!= 0)
76 if (range(sp
, sq
) != 1)
78 if (grappled2(sp
, sq
)) {
79 if (is_toughmelee(sp
, sq
, 0, 0))
83 } else if (couldwin(sp
, sq
)) {
85 sp
->file
->loadwith
= L_GRAPE
;
100 if (sp
->file
->dir
== 0)
102 explode
= sp
->file
->explode
;
103 sink
= sp
->file
->sink
;
104 if (explode
!= 1 && sink
!= 1)
116 cleansnag(sp
, sq
, 1);
118 makemsg(sp
, "exploding!");
120 if (sp
!= sq
&& sq
->file
->dir
&&
122 table(sp
, sq
, RIGGING
, L_EXPLODE
,
123 sp
->specs
->guns
/13, 6);
126 makemsg(sp
, "sinking!");
137 if (sp
->file
->captured
== 0)
139 if (sp
->file
->struck
|| sp
->file
->dir
== 0)
141 if (sp
->specs
->crew1
+ sp
->specs
->crew2
+ sp
->specs
->crew3
>
142 sp
->file
->pcrew
* 6) {
143 send_signal(sp
, "prize crew overthrown");
144 send_points(sp
->file
->captured
,
145 sp
->file
->captured
->file
->points
146 - 2 * sp
->specs
->pts
);
147 send_captured(sp
, -1);
153 str_end(const char *str
)
157 for (p
= str
; *p
; p
++)
159 return p
== str
? 0 : p
[-1];
163 closeon(struct ship
*from
, struct ship
*to
, char *command
, size_t commandmax
,
164 int ta
, int ma
, bool af
)
169 temp
[0] = command
[0] = '\0';
171 try(from
, to
, command
, commandmax
, temp
, sizeof(temp
),
172 ma
, ta
, af
, ma
, from
->file
->dir
, &high
, 0);
176 score(struct ship
*ship
, struct ship
*to
, char *movement
, size_t movementmax
,
180 int row
, col
, dir
, total
, ran
;
181 struct File
*fp
= ship
->file
;
183 if ((dir
= fp
->dir
) == 0)
188 move_ship(ship
, movement
, &fp
->dir
, &fp
->row
, &fp
->col
, &drift
);
190 strlcpy(movement
, "d", movementmax
);
192 ran
= range(ship
, to
);
194 if (ran
< 4 && gunsbear(ship
, to
))
196 if ((ran
= portside(ship
, to
, 1) - fp
->dir
) == 4 || ran
== -4)
208 move_ship(struct ship
*ship
, const char *p
, unsigned char *dir
,
209 short *row
, short *col
, int *drift
)
224 case '1': case '2': case '3': case '4':
225 case '5': case '6': case '7':
228 dist
= dtab
[*p
- '0'];
231 *row
-= dr
[*dir
] * dist
;
232 *col
-= dc
[*dir
] * dist
;
237 if (windspeed
!= 0 && ++*drift
> 2) {
238 if ((ship
->specs
->class >= 3 && !snagged(ship
))
239 || (turn
& 1) == 0) {
249 try(struct ship
*f
, struct ship
*t
,
250 char *command
, size_t commandmax
,
251 char *temp
, size_t tempmax
,
252 int ma
, int ta
, bool af
, int vma
, int dir
, int *high
, int rakeme
)
256 #define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
258 if ((n
= str_end(temp
)) < '1' || n
> '9')
259 for (n
= 1; vma
- n
>= 0; n
++) {
260 snprintf(st
, sizeof(st
), "%d", n
);
261 strlcat(temp
, st
, tempmax
);
262 new = score(f
, t
, temp
, tempmax
, rakeme
);
263 if (new > *high
&& (!rakeme
|| rakeyou
)) {
265 strlcpy(command
, temp
, commandmax
);
267 try(f
, t
, command
, commandmax
, temp
, tempmax
,
272 if ((ma
> 0 && ta
> 0 && (n
= str_end(temp
)) != 'l' && n
!= 'r') ||
274 strlcat(temp
, "r", tempmax
);
275 new = score(f
, t
, temp
, tempmax
, rakeme
);
276 if (new > *high
&& (!rakeme
||
277 (gunsbear(f
, t
) && !gunsbear(t
, f
)))) {
279 strlcpy(command
, temp
, commandmax
);
281 try(f
, t
, command
, commandmax
, temp
, tempmax
,
283 min(ma
-1, maxmove(f
, (dir
== 8 ? 1 : dir
+1), 0)),
284 (dir
== 8 ? 1 : dir
+1), high
, rakeme
);
287 if ((ma
> 0 && ta
> 0 && (n
= str_end(temp
)) != 'l' && n
!= 'r') ||
289 strlcat(temp
, "l", sizeof(temp
));
290 new = score(f
, t
, temp
, tempmax
, rakeme
);
291 if (new > *high
&& (!rakeme
||
292 (gunsbear(f
, t
) && !gunsbear(t
, f
)))) {
294 strlcpy(command
, temp
, commandmax
);
296 try(f
, t
, command
, commandmax
, temp
, tempmax
,
298 (min(ma
-1,maxmove(f
, (dir
-1 ? dir
-1 : 8), 0))),
299 (dir
-1 ? dir
-1 : 8), high
, rakeme
);
309 for (p
= str
; *p
; p
++)