setuid(getuid()) after opening sync file
[unix-history] / usr / src / games / sail / dr_2.c
CommitLineData
717de9f0 1#ifndef lint
6ca45914 2static char *sccsid = "@(#)dr_2.c 2.4 83/12/17";
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 struct ship *sp, *sq;
42 register char explode, sink;
717de9f0 43
b3a57661 44 foreachship(sp) {
8b4ea3df
EW
45 if (sp->file->dir == 0)
46 continue;
b3a57661
EW
47 explode = sp->file->explode;
48 sink = sp->file->sink;
b3a57661
EW
49 if (explode != 1 && sink != 1)
50 continue;
8b4ea3df
EW
51 if (die() < 5)
52 continue;
53 Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 0, 2, 0, 0, 0);
6ca45914
EW
54 Write(W_DIR, sp, 0, 0, 0, 0, 0);
55 if (snagged(sp))
56 foreachship(sq)
57 cleansnag(sp, sq, 1);
b3a57661
EW
58 if (sink != 1) {
59 makesignal(sp, "exploding!", (struct ship *)0);
60 foreachship(sq) {
61 if (sp != sq && sq->file->dir && range(sp, sq) < 4)
62 table(RIGGING, L_EXPLODE, sp->specs->guns/13, sq, sp, 6);
63 }
64 } else
65 makesignal(sp, "sinking!", (struct ship *)0);
717de9f0
CL
66 }
67}
68
69prizecheck()
70{
b3a57661
EW
71 register struct ship *sp;
72 register int prisoners, points;
717de9f0 73
b3a57661
EW
74 foreachship(sp) {
75 if (sp->file->captured == 0)
76 continue;
77 if (sp->file->struck || sp->file->dir == 0)
78 continue;
79 prisoners = sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3;
80 if (prisoners > sp->file->pcrew * 6) {
81 Write(W_CAPTURED, sp, 0, -1, 0, 0, 0);
82 Write(W_SIGNAL, sp, 1,
83 (int)"prize crew overthrown", 0, 0, 0);
84 points = sp->file->captured->file->points
85 - 2 * sp->specs->pts;
86 Write(W_POINTS, sp->file->captured, 0, points, 0, 0, 0);
717de9f0
CL
87 }
88 }
89}
90
b3a57661 91strend(str)
717de9f0
CL
92char *str;
93{
b3a57661 94 register char *p;
717de9f0 95
b3a57661
EW
96 for (p = str; *p; p++)
97 ;
98 return p == str ? 0 : p[-1];
717de9f0
CL
99}
100
b3a57661
EW
101closeon(from, to, command, ta, ma, af)
102register struct ship *from, *to;
103char command[];
104int ma, ta, af;
717de9f0
CL
105{
106 int high;
107 char temp[10];
108
109 temp[0] = command[0] = '\0';
110 high = -30000;
b3a57661 111 try(command, temp, ma, ta, af, ma, from->file->dir, from, to, &high, 0);
717de9f0
CL
112}
113
b3a57661 114int dtab[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */
717de9f0 115
b3a57661
EW
116score(movement, ship, to, onlytemp)
117char movement[];
118register struct ship *ship, *to;
119char onlytemp;
717de9f0 120{
1eda8103
EW
121 char drift;
122 int row, col, dir, total, ran;
b3a57661 123 register struct File *fp = ship->file;
717de9f0 124
b3a57661
EW
125 if ((dir = fp->dir) == 0)
126 return 0;
127 row = fp->row;
128 col = fp->col;
129 drift = fp->drift;
130 move(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
131 if (!*movement)
132 (void) strcpy(movement, "d");
133
134 ran = range(ship, to);
135 total = -50 * ran;
136 if (ran < 4 && gunsbear(ship, to))
137 total += 60;
138 if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
139 total = -30000;
140
141 if (!onlytemp) {
142 fp->row = row;
143 fp->col = col;
144 fp->dir = dir;
145 }
146 return total;
147}
148
b3a57661
EW
149move(p, ship, dir, row, col, drift)
150register char *p;
151register struct ship *ship;
1eda8103
EW
152register char *dir;
153register short *row, *col;
154register char *drift;
b3a57661
EW
155{
156 int dist;
157 char moved = 0;
158
159 for (; *p; p++) {
160 switch (*p) {
161 case 'r':
162 if (++*dir == 9)
163 *dir = 1;
164 break;
165 case 'l':
166 if (--*dir == 0)
167 *dir = 8;
168 break;
169 case '1': case '2': case '3': case '4':
170 case '5': case '6': case '7':
171 moved++;
172 if (*dir % 2 == 0)
173 dist = dtab[*p - '0'];
174 else
175 dist = *p - '0';
176 *row -= dr[*dir] * dist;
177 *col -= dc[*dir] * dist;
178 break;
717de9f0 179 }
717de9f0 180 }
b3a57661
EW
181 if (!windspeed)
182 *drift = 1;
183 if (!moved) {
184 if (++*drift > 2) {
185 if (ship->specs->class >= 3 && !snagged(ship)
acfebbf7 186 || (turn & 1) == 0) {
b3a57661
EW
187 *row -= dr[winddir];
188 *col -= dc[winddir];
189 }
190 }
191 } else
acfebbf7 192 *drift = 0;
717de9f0
CL
193}
194
b3a57661
EW
195try(command, temp, ma, ta, af, vma, dir, f, t, high, rakeme)
196register struct ship *f, *t;
197int ma, ta, af, *high, rakeme;
198char command[], temp[];
717de9f0
CL
199{
200 register int new, n;
201 char st[4];
b3a57661 202#define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
717de9f0
CL
203
204 if ((n = strend(temp)) < '1' || n > '9')
b3a57661
EW
205 for (n = 1; vma - n >= 0; n++) {
206 (void) sprintf(st, "%d", n);
207 (void) strcat(temp, st);
208 new = score(temp, f, t, rakeme);
209 if (new > *high && (!rakeme || rakeyou)) {
717de9f0 210 *high = new;
b3a57661 211 (void) strcpy(command, temp);
717de9f0 212 }
b3a57661
EW
213 try(command, temp, ma-n, ta, af, vma-n,
214 dir, f, t, high, rakeme);
717de9f0
CL
215 rmend(temp);
216 }
b3a57661
EW
217 if (ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r' || !strlen(temp)) {
218 (void) strcat(temp, "r");
219 new = score(temp, f, t, rakeme);
220 if (new > *high && (!rakeme || gunsbear(f, t) && !gunsbear(t, f))) {
717de9f0 221 *high = new;
b3a57661 222 (void) strcpy(command, temp);
717de9f0 223 }
b3a57661 224 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
225 rmend(temp);
226 }
b3a57661
EW
227 if ((ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r') || !strlen(temp)){
228 (void) strcat(temp, "l");
229 new = score(temp, f, t, rakeme);
230 if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
717de9f0 231 *high = new;
b3a57661 232 (void) strcpy(command, temp);
717de9f0 233 }
b3a57661 234 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
235 rmend(temp);
236 }
237}
9224751f
EW
238
239rmend(str)
240char *str;
241{
242 register char *p;
243
244 for (p = str; *p; p++)
245 ;
246 if (p != str)
247 *--p = 0;
248}