]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/dr_2.c
e464a0f001fec8871a9386767e1e1c2d8faa7833
[bsdgames-darwin.git] / sail / dr_2.c
1 /* $NetBSD: dr_2.c,v 1.17 2001/01/04 06:16:51 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.17 2001/01/04 06:16:51 jwise Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include "extern.h"
49 #include "driver.h"
50
51 #define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
52
53 void thinkofgrapples(void);
54 void checkup(void);
55 void prizecheck(void);
56 static int str_end(const char *);
57 void closeon(struct ship *, struct ship *, char *, int, int, int);
58 static int score(struct ship *, struct ship *, char *, int);
59 static void move_ship(struct ship *, const char *, unsigned char *, short *, short *, char *);
60 static void try(struct ship *, struct ship *, char *, char *, int, int, int, int, int, int *, int);
61 static void rmend(char *);
62
63 const int dtab[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */
64
65 void
66 thinkofgrapples(void)
67 {
68 struct ship *sp, *sq;
69 char friendly;
70
71 foreachship(sp) {
72 if (sp->file->captain[0] || sp->file->dir == 0)
73 continue;
74 foreachship(sq) {
75 friendly = sp->nationality == capship(sq)->nationality;
76 if (!friendly) {
77 if (sp->file->struck || sp->file->captured != 0)
78 continue;
79 if (range(sp, sq) != 1)
80 continue;
81 if (grappled2(sp, sq))
82 if (is_toughmelee(sp, sq, 0, 0))
83 ungrap(sp, sq);
84 else
85 grap(sp, sq);
86 else if (couldwin(sp, sq)) {
87 grap(sp, sq);
88 sp->file->loadwith = L_GRAPE;
89 }
90 } else
91 ungrap(sp, sq);
92 }
93 }
94 }
95
96 void
97 checkup(void)
98 {
99 struct ship *sp, *sq;
100 char explode, sink;
101
102 foreachship(sp) {
103 if (sp->file->dir == 0)
104 continue;
105 explode = sp->file->explode;
106 sink = sp->file->sink;
107 if (explode != 1 && sink != 1)
108 continue;
109 if (dieroll() < 5)
110 continue;
111 Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 2, 0, 0, 0);
112 Write(W_DIR, sp, 0, 0, 0, 0);
113 if (snagged(sp))
114 foreachship(sq)
115 cleansnag(sp, sq, 1);
116 if (sink != 1) {
117 makemsg(sp, "exploding!");
118 foreachship(sq) {
119 if (sp != sq && sq->file->dir && range(sp, sq) < 4)
120 table(sp, sq, RIGGING, L_EXPLODE, sp->specs->guns/13, 6);
121 }
122 } else
123 makemsg(sp, "sinking!");
124 }
125 }
126
127 void
128 prizecheck(void)
129 {
130 struct ship *sp;
131
132 foreachship(sp) {
133 if (sp->file->captured == 0)
134 continue;
135 if (sp->file->struck || sp->file->dir == 0)
136 continue;
137 if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 > sp->file->pcrew * 6) {
138 Writestr(W_SIGNAL, sp, "prize crew overthrown");
139 Write(W_POINTS, sp->file->captured, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0);
140 Write(W_CAPTURED, sp, -1, 0, 0, 0);
141 }
142 }
143 }
144
145 static int
146 str_end(const char *str)
147 {
148 const char *p;
149
150 for (p = str; *p; p++)
151 ;
152 return p == str ? 0 : p[-1];
153 }
154
155 void
156 closeon(struct ship *from, struct ship *to, char *command, int ta, int ma, int af)
157 {
158 int high;
159 char temp[10];
160
161 temp[0] = command[0] = '\0';
162 high = -30000;
163 try(from, to, command, temp, ma, ta, af, ma, from->file->dir, &high, 0);
164 }
165
166 static int
167 score(struct ship *ship, struct ship *to, char *movement, int onlytemp)
168 {
169 char drift;
170 int row, col, dir, total, ran;
171 struct File *fp = ship->file;
172
173 if ((dir = fp->dir) == 0)
174 return 0;
175 row = fp->row;
176 col = fp->col;
177 drift = fp->drift;
178 move_ship(ship, movement, &fp->dir, &fp->row, &fp->col, &drift);
179 if (!*movement)
180 strcpy(movement, "d");
181
182 ran = range(ship, to);
183 total = -50 * ran;
184 if (ran < 4 && gunsbear(ship, to))
185 total += 60;
186 if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
187 total = -30000;
188
189 if (!onlytemp) {
190 fp->row = row;
191 fp->col = col;
192 fp->dir = dir;
193 }
194 return total;
195 }
196
197 static void
198 move_ship(struct ship *ship, const char *p, unsigned char *dir, short *row, short *col, char *drift)
199 {
200 int dist;
201 char moved = 0;
202
203 for (; *p; p++) {
204 switch (*p) {
205 case 'r':
206 if (++*dir == 9)
207 *dir = 1;
208 break;
209 case 'l':
210 if (--*dir == 0)
211 *dir = 8;
212 break;
213 case '1': case '2': case '3': case '4':
214 case '5': case '6': case '7':
215 moved++;
216 if (*dir % 2 == 0)
217 dist = dtab[*p - '0'];
218 else
219 dist = *p - '0';
220 *row -= dr[*dir] * dist;
221 *col -= dc[*dir] * dist;
222 break;
223 }
224 }
225 if (!moved) {
226 if (windspeed != 0 && ++*drift > 2) {
227 if ((ship->specs->class >= 3 && !snagged(ship))
228 || (turn & 1) == 0) {
229 *row -= dr[winddir];
230 *col -= dc[winddir];
231 }
232 }
233 } else
234 *drift = 0;
235 }
236
237 static void
238 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)
239 {
240 int new, n;
241 char st[4];
242 #define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
243
244 if ((n = str_end(temp)) < '1' || n > '9')
245 for (n = 1; vma - n >= 0; n++) {
246 sprintf(st, "%d", n);
247 strcat(temp, st);
248 new = score(f, t, temp, rakeme);
249 if (new > *high && (!rakeme || rakeyou)) {
250 *high = new;
251 strcpy(command, temp);
252 }
253 try(f, t, command, temp, ma-n, ta, af, vma-n,
254 dir, high, rakeme);
255 rmend(temp);
256 }
257 if ((ma > 0 && ta > 0 && (n = str_end(temp)) != 'l' && n != 'r') || !strlen(temp)) {
258 strcat(temp, "r");
259 new = score(f, t, temp, rakeme);
260 if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))) {
261 *high = new;
262 strcpy(command, temp);
263 }
264 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);
265 rmend(temp);
266 }
267 if ((ma > 0 && ta > 0 && (n = str_end(temp)) != 'l' && n != 'r') || !strlen(temp)){
268 strcat(temp, "l");
269 new = score(f, t, temp, rakeme);
270 if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
271 *high = new;
272 strcpy(command, temp);
273 }
274 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);
275 rmend(temp);
276 }
277 }
278
279 static void
280 rmend(char *str)
281 {
282 char *p;
283
284 for (p = str; *p; p++)
285 ;
286 if (p != str)
287 *--p = 0;
288 }