]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/dr_2.c
1 /* $NetBSD: dr_2.c,v 1.23 2009/03/14 19:35:13 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.23 2009/03/14 19:35:13 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
, int 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)
108 Write(sink
== 1 ? W_SINK
: W_EXPLODE
, sp
, 2, 0, 0, 0);
109 Write(W_DIR
, sp
, 0, 0, 0, 0);
112 cleansnag(sp
, sq
, 1);
114 makemsg(sp
, "exploding!");
116 if (sp
!= sq
&& sq
->file
->dir
&&
118 table(sp
, sq
, RIGGING
, L_EXPLODE
,
119 sp
->specs
->guns
/13, 6);
122 makemsg(sp
, "sinking!");
133 if (sp
->file
->captured
== 0)
135 if (sp
->file
->struck
|| sp
->file
->dir
== 0)
137 if (sp
->specs
->crew1
+ sp
->specs
->crew2
+ sp
->specs
->crew3
>
138 sp
->file
->pcrew
* 6) {
139 Writestr(W_SIGNAL
, sp
, "prize crew overthrown");
140 Write(W_POINTS
, sp
->file
->captured
,
141 sp
->file
->captured
->file
->points
142 - 2 * sp
->specs
->pts
,
144 Write(W_CAPTURED
, sp
, -1, 0, 0, 0);
150 str_end(const char *str
)
154 for (p
= str
; *p
; p
++)
156 return p
== str
? 0 : p
[-1];
160 closeon(struct ship
*from
, struct ship
*to
, char *command
, size_t commandmax
,
161 int ta
, int ma
, int af
)
166 temp
[0] = command
[0] = '\0';
168 try(from
, to
, command
, commandmax
, temp
, sizeof(temp
),
169 ma
, ta
, af
, ma
, from
->file
->dir
, &high
, 0);
173 score(struct ship
*ship
, struct ship
*to
, char *movement
, size_t movementmax
,
177 int row
, col
, dir
, total
, ran
;
178 struct File
*fp
= ship
->file
;
180 if ((dir
= fp
->dir
) == 0)
185 move_ship(ship
, movement
, &fp
->dir
, &fp
->row
, &fp
->col
, &drift
);
187 strlcpy(movement
, "d", movementmax
);
189 ran
= range(ship
, to
);
191 if (ran
< 4 && gunsbear(ship
, to
))
193 if ((ran
= portside(ship
, to
, 1) - fp
->dir
) == 4 || ran
== -4)
205 move_ship(struct ship
*ship
, const char *p
, unsigned char *dir
,
206 short *row
, short *col
, int *drift
)
221 case '1': case '2': case '3': case '4':
222 case '5': case '6': case '7':
225 dist
= dtab
[*p
- '0'];
228 *row
-= dr
[*dir
] * dist
;
229 *col
-= dc
[*dir
] * dist
;
234 if (windspeed
!= 0 && ++*drift
> 2) {
235 if ((ship
->specs
->class >= 3 && !snagged(ship
))
236 || (turn
& 1) == 0) {
246 try(struct ship
*f
, struct ship
*t
,
247 char *command
, size_t commandmax
,
248 char *temp
, size_t tempmax
,
249 int ma
, int ta
, int af
, int vma
, int dir
, int *high
, int rakeme
)
253 #define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
255 if ((n
= str_end(temp
)) < '1' || n
> '9')
256 for (n
= 1; vma
- n
>= 0; n
++) {
257 snprintf(st
, sizeof(st
), "%d", n
);
258 strlcat(temp
, st
, tempmax
);
259 new = score(f
, t
, temp
, tempmax
, rakeme
);
260 if (new > *high
&& (!rakeme
|| rakeyou
)) {
262 strlcpy(command
, temp
, commandmax
);
264 try(f
, t
, command
, commandmax
, temp
, tempmax
,
269 if ((ma
> 0 && ta
> 0 && (n
= str_end(temp
)) != 'l' && n
!= 'r') ||
271 strlcat(temp
, "r", tempmax
);
272 new = score(f
, t
, temp
, tempmax
, rakeme
);
273 if (new > *high
&& (!rakeme
||
274 (gunsbear(f
, t
) && !gunsbear(t
, f
)))) {
276 strlcpy(command
, temp
, commandmax
);
278 try(f
, t
, command
, commandmax
, temp
, tempmax
,
280 min(ma
-1, maxmove(f
, (dir
== 8 ? 1 : dir
+1), 0)),
281 (dir
== 8 ? 1 : dir
+1), high
, rakeme
);
284 if ((ma
> 0 && ta
> 0 && (n
= str_end(temp
)) != 'l' && n
!= 'r') ||
286 strlcat(temp
, "l", sizeof(temp
));
287 new = score(f
, t
, temp
, tempmax
, rakeme
);
288 if (new > *high
&& (!rakeme
||
289 (gunsbear(f
, t
) && !gunsbear(t
, f
)))) {
291 strlcpy(command
, temp
, commandmax
);
293 try(f
, t
, command
, commandmax
, temp
, tempmax
,
295 (min(ma
-1,maxmove(f
, (dir
-1 ? dir
-1 : 8), 0))),
296 (dir
-1 ? dir
-1 : 8), high
, rakeme
);
306 for (p
= str
; *p
; p
++)