]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/dr_2.c
1 /* $NetBSD: dr_2.c,v 1.21 2009/03/14 17:10:01 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.21 2009/03/14 17:10:01 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 *, int);
52 static void move_ship(struct ship
*, const char *, unsigned char *, short *, short *, int *);
53 static void try(struct ship
*, struct ship
*, char *, char *, int, int, int, int, int, int *, int);
54 static void rmend(char *);
56 const int dtab
[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */
65 if (sp
->file
->captain
[0] || sp
->file
->dir
== 0)
68 friendly
= sp
->nationality
== capship(sq
)->nationality
;
70 if (sp
->file
->struck
|| sp
->file
->captured
!= 0)
72 if (range(sp
, sq
) != 1)
74 if (grappled2(sp
, sq
)) {
75 if (is_toughmelee(sp
, sq
, 0, 0))
79 } else if (couldwin(sp
, sq
)) {
81 sp
->file
->loadwith
= L_GRAPE
;
96 if (sp
->file
->dir
== 0)
98 explode
= sp
->file
->explode
;
99 sink
= sp
->file
->sink
;
100 if (explode
!= 1 && sink
!= 1)
104 Write(sink
== 1 ? W_SINK
: W_EXPLODE
, sp
, 2, 0, 0, 0);
105 Write(W_DIR
, sp
, 0, 0, 0, 0);
108 cleansnag(sp
, sq
, 1);
110 makemsg(sp
, "exploding!");
112 if (sp
!= sq
&& sq
->file
->dir
&& range(sp
, sq
) < 4)
113 table(sp
, sq
, RIGGING
, L_EXPLODE
, sp
->specs
->guns
/13, 6);
116 makemsg(sp
, "sinking!");
126 if (sp
->file
->captured
== 0)
128 if (sp
->file
->struck
|| sp
->file
->dir
== 0)
130 if (sp
->specs
->crew1
+ sp
->specs
->crew2
+ sp
->specs
->crew3
> sp
->file
->pcrew
* 6) {
131 Writestr(W_SIGNAL
, sp
, "prize crew overthrown");
132 Write(W_POINTS
, sp
->file
->captured
, sp
->file
->captured
->file
->points
- 2 * sp
->specs
->pts
, 0, 0, 0);
133 Write(W_CAPTURED
, sp
, -1, 0, 0, 0);
139 str_end(const char *str
)
143 for (p
= str
; *p
; p
++)
145 return p
== str
? 0 : p
[-1];
149 closeon(struct ship
*from
, struct ship
*to
, char *command
, int ta
, int ma
, int af
)
154 temp
[0] = command
[0] = '\0';
156 try(from
, to
, command
, temp
, ma
, ta
, af
, ma
, from
->file
->dir
, &high
, 0);
160 score(struct ship
*ship
, struct ship
*to
, char *movement
, int onlytemp
)
163 int row
, col
, dir
, total
, ran
;
164 struct File
*fp
= ship
->file
;
166 if ((dir
= fp
->dir
) == 0)
171 move_ship(ship
, movement
, &fp
->dir
, &fp
->row
, &fp
->col
, &drift
);
173 strcpy(movement
, "d");
175 ran
= range(ship
, to
);
177 if (ran
< 4 && gunsbear(ship
, to
))
179 if ((ran
= portside(ship
, to
, 1) - fp
->dir
) == 4 || ran
== -4)
191 move_ship(struct ship
*ship
, const char *p
, unsigned char *dir
, short *row
, short *col
, int *drift
)
206 case '1': case '2': case '3': case '4':
207 case '5': case '6': case '7':
210 dist
= dtab
[*p
- '0'];
213 *row
-= dr
[*dir
] * dist
;
214 *col
-= dc
[*dir
] * dist
;
219 if (windspeed
!= 0 && ++*drift
> 2) {
220 if ((ship
->specs
->class >= 3 && !snagged(ship
))
221 || (turn
& 1) == 0) {
231 try(struct ship
*f
, struct ship
*t
, char *command
, char *temp
, int ma
, int ta
, int af
, int vma
, int dir
, int *high
, int rakeme
)
235 #define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
237 if ((n
= str_end(temp
)) < '1' || n
> '9')
238 for (n
= 1; vma
- n
>= 0; n
++) {
239 sprintf(st
, "%d", n
);
241 new = score(f
, t
, temp
, rakeme
);
242 if (new > *high
&& (!rakeme
|| rakeyou
)) {
244 strcpy(command
, temp
);
246 try(f
, t
, command
, temp
, ma
-n
, ta
, af
, vma
-n
,
250 if ((ma
> 0 && ta
> 0 && (n
= str_end(temp
)) != 'l' && n
!= 'r') || !strlen(temp
)) {
252 new = score(f
, t
, temp
, rakeme
);
253 if (new > *high
&& (!rakeme
|| (gunsbear(f
, t
) && !gunsbear(t
, f
)))) {
255 strcpy(command
, temp
);
257 try(f
, t
, command
, temp
, ma
-1, ta
-1, af
, min(ma
-1, maxmove(f
, (dir
== 8 ? 1 : dir
+1), 0)), (dir
== 8 ? 1 : dir
+1), high
, rakeme
);
260 if ((ma
> 0 && ta
> 0 && (n
= str_end(temp
)) != 'l' && n
!= 'r') || !strlen(temp
)){
262 new = score(f
, t
, temp
, rakeme
);
263 if (new > *high
&& (!rakeme
|| (gunsbear(f
, t
) && !gunsbear(t
, f
)))){
265 strcpy(command
, temp
);
267 try(f
, t
, command
, temp
, ma
-1, ta
-1, af
, (min(ma
-1,maxmove(f
, (dir
-1 ? dir
-1 : 8), 0))), (dir
-1 ? dir
-1 : 8), high
, rakeme
);
277 for (p
= str
; *p
; p
++)