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