dr_2.c revision 1.14 1 /* $NetBSD: dr_2.c,v 1.14 2001/01/04 01:53:24 jwise Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)dr_2.c 8.1 (Berkeley) 5/31/93";
40 #else
41 __RCSID("$NetBSD: dr_2.c,v 1.14 2001/01/04 01:53:24 jwise Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include "driver.h"
46 #include <stdlib.h>
47
48 #define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
49
50 void thinkofgrapples(void);
51 void checkup(void);
52 void prizecheck(void);
53 static int str_end(const char *);
54 void closeon(struct ship *, struct ship *, char *, int, int, int);
55 static int score(char *, struct ship *, struct ship *, int);
56 static void move_ship(const char *, struct ship *, unsigned char *, short *, short *, char *);
57 static void try(char *, char *, int, int, int, int, int, struct ship *, struct ship *, int *, int);
58 static void rmend(char *);
59
60 const int dtab[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */
61
62 void
63 thinkofgrapples(void)
64 {
65 struct ship *sp, *sq;
66 char friendly;
67
68 foreachship(sp) {
69 if (sp->file->captain[0] || sp->file->dir == 0)
70 continue;
71 foreachship(sq) {
72 friendly = sp->nationality == capship(sq)->nationality;
73 if (!friendly) {
74 if (sp->file->struck || sp->file->captured != 0)
75 continue;
76 if (range(sp, sq) != 1)
77 continue;
78 if (grappled2(sp, sq))
79 if (is_toughmelee(sp, sq, 0, 0))
80 ungrap(sp, sq);
81 else
82 grap(sp, sq);
83 else if (couldwin(sp, sq)) {
84 grap(sp, sq);
85 sp->file->loadwith = L_GRAPE;
86 }
87 } else
88 ungrap(sp, sq);
89 }
90 }
91 }
92
93 void
94 checkup(void)
95 {
96 struct ship *sp, *sq;
97 char explode, sink;
98
99 foreachship(sp) {
100 if (sp->file->dir == 0)
101 continue;
102 explode = sp->file->explode;
103 sink = sp->file->sink;
104 if (explode != 1 && sink != 1)
105 continue;
106 if (dieroll() < 5)
107 continue;
108 Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 2, 0, 0, 0);
109 Write(W_DIR, sp, 0, 0, 0, 0);
110 if (snagged(sp))
111 foreachship(sq)
112 cleansnag(sp, sq, 1);
113 if (sink != 1) {
114 makemsg(sp, "exploding!");
115 foreachship(sq) {
116 if (sp != sq && sq->file->dir && range(sp, sq) < 4)
117 table(RIGGING, L_EXPLODE, sp->specs->guns/13, sq, sp, 6);
118 }
119 } else
120 makemsg(sp, "sinking!");
121 }
122 }
123
124 void
125 prizecheck(void)
126 {
127 struct ship *sp;
128
129 foreachship(sp) {
130 if (sp->file->captured == 0)
131 continue;
132 if (sp->file->struck || sp->file->dir == 0)
133 continue;
134 if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 > sp->file->pcrew * 6) {
135 Writestr(W_SIGNAL, sp, "prize crew overthrown");
136 Write(W_POINTS, sp->file->captured, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0);
137 Write(W_CAPTURED, sp, -1, 0, 0, 0);
138 }
139 }
140 }
141
142 static int
143 str_end(const char *str)
144 {
145 const char *p;
146
147 for (p = str; *p; p++)
148 ;
149 return p == str ? 0 : p[-1];
150 }
151
152 void
153 closeon(struct ship *from, struct ship *to, char *command, int ta, int ma, int af)
154 {
155 int high;
156 char temp[10];
157
158 temp[0] = command[0] = '\0';
159 high = -30000;
160 try(command, temp, ma, ta, af, ma, from->file->dir, from, to, &high, 0);
161 }
162
163 static int
164 score(char *movement, struct ship *ship, struct ship *to, int onlytemp)
165 {
166 char drift;
167 int row, col, dir, total, ran;
168 struct File *fp = ship->file;
169
170 if ((dir = fp->dir) == 0)
171 return 0;
172 row = fp->row;
173 col = fp->col;
174 drift = fp->drift;
175 move_ship(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
176 if (!*movement)
177 strcpy(movement, "d");
178
179 ran = range(ship, to);
180 total = -50 * ran;
181 if (ran < 4 && gunsbear(ship, to))
182 total += 60;
183 if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
184 total = -30000;
185
186 if (!onlytemp) {
187 fp->row = row;
188 fp->col = col;
189 fp->dir = dir;
190 }
191 return total;
192 }
193
194 static void
195 move_ship(const char *p, struct ship *ship, unsigned char *dir, short *row, short *col, char *drift)
196 {
197 int dist;
198 char moved = 0;
199
200 for (; *p; p++) {
201 switch (*p) {
202 case 'r':
203 if (++*dir == 9)
204 *dir = 1;
205 break;
206 case 'l':
207 if (--*dir == 0)
208 *dir = 8;
209 break;
210 case '1': case '2': case '3': case '4':
211 case '5': case '6': case '7':
212 moved++;
213 if (*dir % 2 == 0)
214 dist = dtab[*p - '0'];
215 else
216 dist = *p - '0';
217 *row -= dr[*dir] * dist;
218 *col -= dc[*dir] * dist;
219 break;
220 }
221 }
222 if (!moved) {
223 if (windspeed != 0 && ++*drift > 2) {
224 if ((ship->specs->class >= 3 && !snagged(ship))
225 || (turn & 1) == 0) {
226 *row -= dr[winddir];
227 *col -= dc[winddir];
228 }
229 }
230 } else
231 *drift = 0;
232 }
233
234 static void
235 try(char *command, char *temp, int ma, int ta, int af, int vma, int dir, struct ship *f, struct ship *t, int *high, int rakeme)
236 {
237 int new, n;
238 char st[4];
239 #define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
240
241 if ((n = str_end(temp)) < '1' || n > '9')
242 for (n = 1; vma - n >= 0; n++) {
243 sprintf(st, "%d", n);
244 strcat(temp, st);
245 new = score(temp, f, t, rakeme);
246 if (new > *high && (!rakeme || rakeyou)) {
247 *high = new;
248 strcpy(command, temp);
249 }
250 try(command, temp, ma-n, ta, af, vma-n,
251 dir, f, t, high, rakeme);
252 rmend(temp);
253 }
254 if ((ma > 0 && ta > 0 && (n = str_end(temp)) != 'l' && n != 'r') || !strlen(temp)) {
255 strcat(temp, "r");
256 new = score(temp, f, t, rakeme);
257 if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))) {
258 *high = new;
259 strcpy(command, temp);
260 }
261 try(command, temp, ma-1, ta-1, af, min(ma-1, maxmove(f, (dir == 8 ? 1 : dir+1), 0)), (dir == 8 ? 1 : dir+1),f,t,high,rakeme);
262 rmend(temp);
263 }
264 if ((ma > 0 && ta > 0 && (n = str_end(temp)) != 'l' && n != 'r') || !strlen(temp)){
265 strcat(temp, "l");
266 new = score(temp, f, t, rakeme);
267 if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
268 *high = new;
269 strcpy(command, temp);
270 }
271 try(command, temp, ma-1, ta-1, af, (min(ma-1,maxmove(f, (dir-1 ? dir-1 : 8), 0))), (dir-1 ? dir -1 : 8), f, t, high, rakeme);
272 rmend(temp);
273 }
274 }
275
276 static void
277 rmend(char *str)
278 {
279 char *p;
280
281 for (p = str; *p; p++)
282 ;
283 if (p != str)
284 *--p = 0;
285 }
286