date and time created 88/06/01 18:39:47 by bostic
[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 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of California at Berkeley. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
b5f0675e
EW
11 */
12
717de9f0 13#ifndef lint
c0ca48ec
KB
14static char sccsid[] = "@(#)dr_2.c 5.2 (Berkeley) %G%";
15#endif /* not lint */
717de9f0 16
b3a57661
EW
17#include "driver.h"
18
19#define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
717de9f0
CL
20
21thinkofgrapples()
22{
b3a57661
EW
23 register struct ship *sp, *sq;
24 char friendly;
717de9f0 25
b3a57661
EW
26 foreachship(sp) {
27 if (sp->file->captain[0] || sp->file->dir == 0)
28 continue;
29 foreachship(sq) {
30 friendly = sp->nationality == capship(sq)->nationality;
31 if (!friendly) {
32 if (sp->file->struck || sp->file->captured != 0)
33 continue;
34 if (range(sp, sq) != 1)
35 continue;
36 if (grappled2(sp, sq))
37 if (toughmelee(sp, sq, 0, 0))
38 ungrap(sp, sq);
39 else
40 grap(sp, sq);
41 else if (couldwin(sp, sq)) {
42 grap(sp, sq);
43 sp->file->loadwith = L_GRAPE;
717de9f0 44 }
b3a57661
EW
45 } else
46 ungrap(sp, sq);
717de9f0
CL
47 }
48 }
49}
50
717de9f0
CL
51checkup()
52{
b3a57661
EW
53 register struct ship *sp, *sq;
54 register char explode, sink;
717de9f0 55
b3a57661 56 foreachship(sp) {
8b4ea3df
EW
57 if (sp->file->dir == 0)
58 continue;
b3a57661
EW
59 explode = sp->file->explode;
60 sink = sp->file->sink;
b3a57661
EW
61 if (explode != 1 && sink != 1)
62 continue;
8b4ea3df
EW
63 if (die() < 5)
64 continue;
65 Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 0, 2, 0, 0, 0);
6ca45914
EW
66 Write(W_DIR, sp, 0, 0, 0, 0, 0);
67 if (snagged(sp))
68 foreachship(sq)
69 cleansnag(sp, sq, 1);
b3a57661
EW
70 if (sink != 1) {
71 makesignal(sp, "exploding!", (struct ship *)0);
72 foreachship(sq) {
73 if (sp != sq && sq->file->dir && range(sp, sq) < 4)
74 table(RIGGING, L_EXPLODE, sp->specs->guns/13, sq, sp, 6);
75 }
76 } else
77 makesignal(sp, "sinking!", (struct ship *)0);
717de9f0
CL
78 }
79}
80
81prizecheck()
82{
b3a57661 83 register struct ship *sp;
717de9f0 84
b3a57661
EW
85 foreachship(sp) {
86 if (sp->file->captured == 0)
87 continue;
88 if (sp->file->struck || sp->file->dir == 0)
89 continue;
0483f58f 90 if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 > sp->file->pcrew * 6) {
b3a57661
EW
91 Write(W_SIGNAL, sp, 1,
92 (int)"prize crew overthrown", 0, 0, 0);
0483f58f
EW
93 Write(W_POINTS, sp->file->captured, 0, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0);
94 Write(W_CAPTURED, sp, 0, -1, 0, 0, 0);
717de9f0
CL
95 }
96 }
97}
98
b3a57661 99strend(str)
717de9f0
CL
100char *str;
101{
b3a57661 102 register char *p;
717de9f0 103
b3a57661
EW
104 for (p = str; *p; p++)
105 ;
106 return p == str ? 0 : p[-1];
717de9f0
CL
107}
108
b3a57661
EW
109closeon(from, to, command, ta, ma, af)
110register struct ship *from, *to;
111char command[];
112int ma, ta, af;
717de9f0
CL
113{
114 int high;
115 char temp[10];
116
117 temp[0] = command[0] = '\0';
118 high = -30000;
b3a57661 119 try(command, temp, ma, ta, af, ma, from->file->dir, from, to, &high, 0);
717de9f0
CL
120}
121
b3a57661 122int dtab[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */
717de9f0 123
b3a57661
EW
124score(movement, ship, to, onlytemp)
125char movement[];
126register struct ship *ship, *to;
127char onlytemp;
717de9f0 128{
1eda8103
EW
129 char drift;
130 int row, col, dir, total, ran;
b3a57661 131 register struct File *fp = ship->file;
717de9f0 132
b3a57661
EW
133 if ((dir = fp->dir) == 0)
134 return 0;
135 row = fp->row;
136 col = fp->col;
137 drift = fp->drift;
138 move(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
139 if (!*movement)
140 (void) strcpy(movement, "d");
141
142 ran = range(ship, to);
143 total = -50 * ran;
144 if (ran < 4 && gunsbear(ship, to))
145 total += 60;
146 if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
147 total = -30000;
148
149 if (!onlytemp) {
150 fp->row = row;
151 fp->col = col;
152 fp->dir = dir;
153 }
154 return total;
155}
156
b3a57661
EW
157move(p, ship, dir, row, col, drift)
158register char *p;
159register struct ship *ship;
1eda8103
EW
160register char *dir;
161register short *row, *col;
162register char *drift;
b3a57661
EW
163{
164 int dist;
165 char moved = 0;
166
167 for (; *p; p++) {
168 switch (*p) {
169 case 'r':
170 if (++*dir == 9)
171 *dir = 1;
172 break;
173 case 'l':
174 if (--*dir == 0)
175 *dir = 8;
176 break;
177 case '1': case '2': case '3': case '4':
178 case '5': case '6': case '7':
179 moved++;
180 if (*dir % 2 == 0)
181 dist = dtab[*p - '0'];
182 else
183 dist = *p - '0';
184 *row -= dr[*dir] * dist;
185 *col -= dc[*dir] * dist;
186 break;
717de9f0 187 }
717de9f0 188 }
b3a57661 189 if (!moved) {
9d517234 190 if (windspeed != 0 && ++*drift > 2) {
b3a57661 191 if (ship->specs->class >= 3 && !snagged(ship)
acfebbf7 192 || (turn & 1) == 0) {
b3a57661
EW
193 *row -= dr[winddir];
194 *col -= dc[winddir];
195 }
196 }
197 } else
acfebbf7 198 *drift = 0;
717de9f0
CL
199}
200
b3a57661
EW
201try(command, temp, ma, ta, af, vma, dir, f, t, high, rakeme)
202register struct ship *f, *t;
203int ma, ta, af, *high, rakeme;
204char command[], temp[];
717de9f0
CL
205{
206 register int new, n;
207 char st[4];
b3a57661 208#define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
717de9f0
CL
209
210 if ((n = strend(temp)) < '1' || n > '9')
b3a57661
EW
211 for (n = 1; vma - n >= 0; n++) {
212 (void) sprintf(st, "%d", n);
213 (void) strcat(temp, st);
214 new = score(temp, f, t, rakeme);
215 if (new > *high && (!rakeme || rakeyou)) {
717de9f0 216 *high = new;
b3a57661 217 (void) strcpy(command, temp);
717de9f0 218 }
b3a57661
EW
219 try(command, temp, ma-n, ta, af, vma-n,
220 dir, f, t, high, rakeme);
717de9f0
CL
221 rmend(temp);
222 }
b3a57661
EW
223 if (ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r' || !strlen(temp)) {
224 (void) strcat(temp, "r");
225 new = score(temp, f, t, rakeme);
226 if (new > *high && (!rakeme || gunsbear(f, t) && !gunsbear(t, f))) {
717de9f0 227 *high = new;
b3a57661 228 (void) strcpy(command, temp);
717de9f0 229 }
b3a57661 230 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
231 rmend(temp);
232 }
b3a57661
EW
233 if ((ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r') || !strlen(temp)){
234 (void) strcat(temp, "l");
235 new = score(temp, f, t, rakeme);
236 if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
717de9f0 237 *high = new;
b3a57661 238 (void) strcpy(command, temp);
717de9f0 239 }
b3a57661 240 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
241 rmend(temp);
242 }
243}
9224751f
EW
244
245rmend(str)
246char *str;
247{
248 register char *p;
249
250 for (p = str; *p; p++)
251 ;
252 if (p != str)
253 *--p = 0;
254}