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