]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/dr_2.c
1 /* $NetBSD: dr_2.c,v 1.19 2003/08/07 09:37:41 agc 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.19 2003/08/07 09:37:41 agc Exp $");
47 #define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
49 static int str_end(const char *);
50 static int score(struct ship
*, struct ship
*, char *, int);
51 static void move_ship(struct ship
*, const char *, unsigned char *, short *, short *, char *);
52 static void try(struct ship
*, struct ship
*, char *, char *, int, int, int, int, int, int *, int);
53 static void rmend(char *);
55 const int dtab
[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */
64 if (sp
->file
->captain
[0] || sp
->file
->dir
== 0)
67 friendly
= sp
->nationality
== capship(sq
)->nationality
;
69 if (sp
->file
->struck
|| sp
->file
->captured
!= 0)
71 if (range(sp
, sq
) != 1)
73 if (grappled2(sp
, sq
))
74 if (is_toughmelee(sp
, sq
, 0, 0))
78 else if (couldwin(sp
, sq
)) {
80 sp
->file
->loadwith
= L_GRAPE
;
95 if (sp
->file
->dir
== 0)
97 explode
= sp
->file
->explode
;
98 sink
= sp
->file
->sink
;
99 if (explode
!= 1 && sink
!= 1)
103 Write(sink
== 1 ? W_SINK
: W_EXPLODE
, sp
, 2, 0, 0, 0);
104 Write(W_DIR
, sp
, 0, 0, 0, 0);
107 cleansnag(sp
, sq
, 1);
109 makemsg(sp
, "exploding!");
111 if (sp
!= sq
&& sq
->file
->dir
&& range(sp
, sq
) < 4)
112 table(sp
, sq
, RIGGING
, L_EXPLODE
, sp
->specs
->guns
/13, 6);
115 makemsg(sp
, "sinking!");
125 if (sp
->file
->captured
== 0)
127 if (sp
->file
->struck
|| sp
->file
->dir
== 0)
129 if (sp
->specs
->crew1
+ sp
->specs
->crew2
+ sp
->specs
->crew3
> sp
->file
->pcrew
* 6) {
130 Writestr(W_SIGNAL
, sp
, "prize crew overthrown");
131 Write(W_POINTS
, sp
->file
->captured
, sp
->file
->captured
->file
->points
- 2 * sp
->specs
->pts
, 0, 0, 0);
132 Write(W_CAPTURED
, sp
, -1, 0, 0, 0);
138 str_end(const char *str
)
142 for (p
= str
; *p
; p
++)
144 return p
== str
? 0 : p
[-1];
148 closeon(struct ship
*from
, struct ship
*to
, char *command
, int ta
, int ma
, int af
)
153 temp
[0] = command
[0] = '\0';
155 try(from
, to
, command
, temp
, ma
, ta
, af
, ma
, from
->file
->dir
, &high
, 0);
159 score(struct ship
*ship
, struct ship
*to
, char *movement
, int onlytemp
)
162 int row
, col
, dir
, total
, ran
;
163 struct File
*fp
= ship
->file
;
165 if ((dir
= fp
->dir
) == 0)
170 move_ship(ship
, movement
, &fp
->dir
, &fp
->row
, &fp
->col
, &drift
);
172 strcpy(movement
, "d");
174 ran
= range(ship
, to
);
176 if (ran
< 4 && gunsbear(ship
, to
))
178 if ((ran
= portside(ship
, to
, 1) - fp
->dir
) == 4 || ran
== -4)
190 move_ship(struct ship
*ship
, const char *p
, unsigned char *dir
, short *row
, short *col
, char *drift
)
205 case '1': case '2': case '3': case '4':
206 case '5': case '6': case '7':
209 dist
= dtab
[*p
- '0'];
212 *row
-= dr
[*dir
] * dist
;
213 *col
-= dc
[*dir
] * dist
;
218 if (windspeed
!= 0 && ++*drift
> 2) {
219 if ((ship
->specs
->class >= 3 && !snagged(ship
))
220 || (turn
& 1) == 0) {
230 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
)
234 #define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
236 if ((n
= str_end(temp
)) < '1' || n
> '9')
237 for (n
= 1; vma
- n
>= 0; n
++) {
238 sprintf(st
, "%d", n
);
240 new = score(f
, t
, temp
, rakeme
);
241 if (new > *high
&& (!rakeme
|| rakeyou
)) {
243 strcpy(command
, temp
);
245 try(f
, t
, command
, temp
, ma
-n
, ta
, af
, vma
-n
,
249 if ((ma
> 0 && ta
> 0 && (n
= str_end(temp
)) != 'l' && n
!= 'r') || !strlen(temp
)) {
251 new = score(f
, t
, temp
, rakeme
);
252 if (new > *high
&& (!rakeme
|| (gunsbear(f
, t
) && !gunsbear(t
, f
)))) {
254 strcpy(command
, temp
);
256 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
);
259 if ((ma
> 0 && ta
> 0 && (n
= str_end(temp
)) != 'l' && n
!= 'r') || !strlen(temp
)){
261 new = score(f
, t
, temp
, rakeme
);
262 if (new > *high
&& (!rakeme
|| (gunsbear(f
, t
) && !gunsbear(t
, f
)))){
264 strcpy(command
, temp
);
266 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
);
276 for (p
= str
; *p
; p
++)