rearranged player and screen stuff
[unix-history] / usr / src / games / sail / dr_2.c
CommitLineData
717de9f0 1#ifndef lint
acfebbf7 2static char *sccsid = "@(#)dr_2.c 1.6 83/10/27";
717de9f0 3#endif
717de9f0 4
b3a57661
EW
5#include "driver.h"
6
7#define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
717de9f0
CL
8
9thinkofgrapples()
10{
b3a57661
EW
11 register struct ship *sp, *sq;
12 char friendly;
717de9f0 13
b3a57661
EW
14 foreachship(sp) {
15 if (sp->file->captain[0] || sp->file->dir == 0)
16 continue;
17 foreachship(sq) {
18 friendly = sp->nationality == capship(sq)->nationality;
19 if (!friendly) {
20 if (sp->file->struck || sp->file->captured != 0)
21 continue;
22 if (range(sp, sq) != 1)
23 continue;
24 if (grappled2(sp, sq))
25 if (toughmelee(sp, sq, 0, 0))
26 ungrap(sp, sq);
27 else
28 grap(sp, sq);
29 else if (couldwin(sp, sq)) {
30 grap(sp, sq);
31 sp->file->loadwith = L_GRAPE;
717de9f0 32 }
b3a57661
EW
33 } else
34 ungrap(sp, sq);
717de9f0
CL
35 }
36 }
37}
38
717de9f0
CL
39checkup()
40{
b3a57661
EW
41 register int k;
42 register struct ship *sp, *sq;
43 register char explode, sink;
717de9f0 44
6c17b19a 45 /*
717de9f0 46 readpos();
6c17b19a 47 */
b3a57661
EW
48 foreachship(sp) {
49 explode = sp->file->explode;
50 sink = sp->file->sink;
51 if (die() < 5)
52 continue;
53 if (explode != 1 && sink != 1)
54 continue;
55 Write(sink ? W_SINK : W_EXPLODE, sp, 0, 2, 0, 0, 0);
56 sp->file->dir = 0; /* hopefully enough to kill ship */
57 /* Write(n, 0, 10, 0); XXX */
58 Write(W_CLASS, sp, 0, 0, 0, 0, 0);
59 if (fouled(sp) || grappled(sp)) {
60 for (k = 0; k < NSHIP; k++) {
61 if (sp->file->fouls[k].turnfoul)
62 cleanfoul(sp,
63 sp->file->fouls[k].toship, k);
717de9f0 64 }
b3a57661
EW
65 for (k = 0; k < NSHIP; k++) {
66 if (sp->file->grapples[k].turnfoul)
67 cleangrapple(sp,
68 sp->file->grapples[k].toship,
69 k);
717de9f0
CL
70 }
71 }
b3a57661
EW
72 if (sink != 1) {
73 makesignal(sp, "exploding!", (struct ship *)0);
74 foreachship(sq) {
75 if (sp != sq && sq->file->dir && range(sp, sq) < 4)
76 table(RIGGING, L_EXPLODE, sp->specs->guns/13, sq, sp, 6);
77 }
78 } else
79 makesignal(sp, "sinking!", (struct ship *)0);
717de9f0
CL
80 }
81}
82
83prizecheck()
84{
b3a57661
EW
85 register struct ship *sp;
86 register int prisoners, points;
717de9f0 87
b3a57661
EW
88 foreachship(sp) {
89 if (sp->file->captured == 0)
90 continue;
91 if (sp->file->struck || sp->file->dir == 0)
92 continue;
93 prisoners = sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3;
94 if (prisoners > sp->file->pcrew * 6) {
95 Write(W_CAPTURED, sp, 0, -1, 0, 0, 0);
96 Write(W_SIGNAL, sp, 1,
97 (int)"prize crew overthrown", 0, 0, 0);
98 points = sp->file->captured->file->points
99 - 2 * sp->specs->pts;
100 Write(W_POINTS, sp->file->captured, 0, points, 0, 0, 0);
717de9f0
CL
101 }
102 }
103}
104
b3a57661 105strend(str)
717de9f0
CL
106char *str;
107{
b3a57661 108 register char *p;
717de9f0 109
b3a57661
EW
110 for (p = str; *p; p++)
111 ;
112 return p == str ? 0 : p[-1];
717de9f0
CL
113}
114
b3a57661
EW
115closeon(from, to, command, ta, ma, af)
116register struct ship *from, *to;
117char command[];
118int ma, ta, af;
717de9f0
CL
119{
120 int high;
121 char temp[10];
122
123 temp[0] = command[0] = '\0';
124 high = -30000;
b3a57661 125 try(command, temp, ma, ta, af, ma, from->file->dir, from, to, &high, 0);
717de9f0
CL
126}
127
b3a57661 128int dtab[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */
717de9f0 129
b3a57661
EW
130score(movement, ship, to, onlytemp)
131char movement[];
132register struct ship *ship, *to;
133char onlytemp;
717de9f0 134{
1eda8103
EW
135 char drift;
136 int row, col, dir, total, ran;
b3a57661 137 register struct File *fp = ship->file;
717de9f0 138
b3a57661
EW
139 if ((dir = fp->dir) == 0)
140 return 0;
141 row = fp->row;
142 col = fp->col;
143 drift = fp->drift;
144 move(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
145 if (!*movement)
146 (void) strcpy(movement, "d");
147
148 ran = range(ship, to);
149 total = -50 * ran;
150 if (ran < 4 && gunsbear(ship, to))
151 total += 60;
152 if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
153 total = -30000;
154
155 if (!onlytemp) {
156 fp->row = row;
157 fp->col = col;
158 fp->dir = dir;
159 }
160 return total;
161}
162
163moveship(ship, movement)
164struct ship *ship;
165char *movement;
166{
b3a57661 167 register struct File *fp = ship->file;
9224751f 168 char drift = fp->drift;
6c17b19a
EW
169 int row = fp->row;
170 int col = fp->col;
171 int dir = fp->dir;
b3a57661
EW
172
173 if (fp->dir == 0)
174 return;
b3a57661
EW
175 move(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
176 if (drift > 2 || *movement == 0)
177 (void) strcat(movement, "d");
6c17b19a 178 if (fp->drift != drift)
b3a57661 179 Write(W_DRIFT, ship, 0, drift, 0, 0, 0);
6c17b19a 180 if (fp->row != row)
b3a57661 181 Write(W_SHIPROW, ship, 0, fp->row, 0, 0, 0);
6c17b19a 182 if (fp->col != col)
b3a57661 183 Write(W_SHIPCOL, ship, 0, fp->col, 0, 0, 0);
6c17b19a 184 if (fp->dir != dir)
b3a57661
EW
185 Write(W_SHIPDIR, ship, 0, fp->dir, 0, 0, 0);
186}
187
188move(p, ship, dir, row, col, drift)
189register char *p;
190register struct ship *ship;
1eda8103
EW
191register char *dir;
192register short *row, *col;
193register char *drift;
b3a57661
EW
194{
195 int dist;
196 char moved = 0;
197
198 for (; *p; p++) {
199 switch (*p) {
200 case 'r':
201 if (++*dir == 9)
202 *dir = 1;
203 break;
204 case 'l':
205 if (--*dir == 0)
206 *dir = 8;
207 break;
208 case '1': case '2': case '3': case '4':
209 case '5': case '6': case '7':
210 moved++;
211 if (*dir % 2 == 0)
212 dist = dtab[*p - '0'];
213 else
214 dist = *p - '0';
215 *row -= dr[*dir] * dist;
216 *col -= dc[*dir] * dist;
217 break;
717de9f0 218 }
717de9f0 219 }
b3a57661
EW
220 if (!windspeed)
221 *drift = 1;
222 if (!moved) {
223 if (++*drift > 2) {
224 if (ship->specs->class >= 3 && !snagged(ship)
acfebbf7 225 || (turn & 1) == 0) {
b3a57661
EW
226 *row -= dr[winddir];
227 *col -= dc[winddir];
228 }
229 }
230 } else
acfebbf7 231 *drift = 0;
717de9f0
CL
232}
233
b3a57661
EW
234try(command, temp, ma, ta, af, vma, dir, f, t, high, rakeme)
235register struct ship *f, *t;
236int ma, ta, af, *high, rakeme;
237char command[], temp[];
717de9f0
CL
238{
239 register int new, n;
240 char st[4];
b3a57661 241#define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
717de9f0
CL
242
243 if ((n = strend(temp)) < '1' || n > '9')
b3a57661
EW
244 for (n = 1; vma - n >= 0; n++) {
245 (void) sprintf(st, "%d", n);
246 (void) strcat(temp, st);
247 new = score(temp, f, t, rakeme);
248 if (new > *high && (!rakeme || rakeyou)) {
717de9f0 249 *high = new;
b3a57661 250 (void) strcpy(command, temp);
717de9f0 251 }
b3a57661
EW
252 try(command, temp, ma-n, ta, af, vma-n,
253 dir, f, t, high, rakeme);
717de9f0
CL
254 rmend(temp);
255 }
b3a57661
EW
256 if (ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r' || !strlen(temp)) {
257 (void) strcat(temp, "r");
258 new = score(temp, f, t, rakeme);
259 if (new > *high && (!rakeme || gunsbear(f, t) && !gunsbear(t, f))) {
717de9f0 260 *high = new;
b3a57661 261 (void) strcpy(command, temp);
717de9f0 262 }
b3a57661 263 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
264 rmend(temp);
265 }
b3a57661
EW
266 if ((ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r') || !strlen(temp)){
267 (void) strcat(temp, "l");
268 new = score(temp, f, t, rakeme);
269 if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
717de9f0 270 *high = new;
b3a57661 271 (void) strcpy(command, temp);
717de9f0 272 }
b3a57661 273 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
274 rmend(temp);
275 }
276}
9224751f
EW
277
278rmend(str)
279char *str;
280{
281 register char *p;
282
283 for (p = str; *p; p++)
284 ;
285 if (p != str)
286 *--p = 0;
287}