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