fixed explode
[unix-history] / usr / src / games / sail / dr_3.c
CommitLineData
b860dc81 1#ifndef lint
28c0fb82 2static char *sccsid = "@(#)dr_3.c 2.3 83/12/12";
b860dc81 3#endif
b860dc81 4
b3a57661 5#include "driver.h"
b860dc81
CL
6
7moveall() /* move all comp ships */
8{
b3a57661
EW
9 register struct ship *sp, *sq; /* r11, r10 */
10 register int n; /* r9 */
28c0fb82
EW
11 register int k, l, m; /* r8, r7, r6, */
12 int row[NSHIP], col[NSHIP], dir[NSHIP], drift[NSHIP];
13 char moved[NSHIP];
b860dc81 14
b3a57661
EW
15 /*
16 * first try to create moves for OUR ships
17 */
18 foreachship(sp) {
28c0fb82
EW
19 struct ship *closest;
20 int ma, ta;
21 char af;
22
6c17b19a 23 if (sp->file->captain[0] || sp->file->dir == 0)
b3a57661
EW
24 continue;
25 if (!sp->file->struck && windspeed && !snagged(sp)
26 && sp->specs->crew3) {
6c17b19a 27 ta = maxturns(sp, &af);
b3a57661
EW
28 ma = maxmove(sp, sp->file->dir, 0);
29 closest = closestenemy(sp, 0, 0);
30 if (closest == 0)
31 *sp->file->last = '\0';
b860dc81 32 else
b3a57661
EW
33 closeon(sp, closest, sp->file->last,
34 ta, ma, af);
35 } else
36 *sp->file->last = '\0';
b860dc81 37 }
b3a57661
EW
38 /*
39 * Then execute the moves for ALL ships (dead ones too),
28c0fb82
EW
40 * checking for collisions and snags at each step.
41 * The old positions are saved in row[], col[], dir[].
42 * At the end, we compare and write out the changes.
b3a57661
EW
43 */
44 n = 0;
45 foreachship(sp) {
46 if (snagged(sp))
28c0fb82 47 strcpy(sp->file->last, "d");
b3a57661 48 else
28c0fb82
EW
49 if (*sp->file->last != 'd')
50 strcat(sp->file->last, "d");
b3a57661
EW
51 row[n] = sp->file->row;
52 col[n] = sp->file->col;
53 dir[n] = sp->file->dir;
28c0fb82
EW
54 drift[n] = sp->file->drift;
55 moved[n] = 0;
b3a57661 56 n++;
b860dc81 57 }
b3a57661
EW
58 /*
59 * Now resolve collisions.
60 * This is the tough part.
61 */
28c0fb82 62 for (k = 0; stillmoving(k); k++) {
b3a57661
EW
63 /*
64 * Step once.
28c0fb82 65 * And propagate the nulls at the end of sp->file->last.
b3a57661
EW
66 */
67 n = 0;
68 foreachship(sp) {
28c0fb82
EW
69 if (!sp->file->last[k])
70 sp->file->last[k+1] = '\0';
71 else if (sp->file->dir)
72 step(sp->file->last[k], sp, &moved[n]);
b3a57661 73 n++;
b860dc81 74 }
b3a57661
EW
75 /*
76 * The real stuff.
77 */
78 n = 0;
79 foreachship(sp) {
28c0fb82 80 if (sp->file->dir == 0 || isolated(sp))
759267e0 81 goto cont1;
b3a57661
EW
82 l = 0;
83 foreachship(sq) {
28c0fb82
EW
84 char snap = 0;
85
759267e0
EW
86 if (sp == sq)
87 goto cont2;
28c0fb82 88 if (sq->file->dir == 0)
759267e0 89 goto cont2;
28c0fb82
EW
90 if (!push(sp, sq))
91 goto cont2;
92 if (snagged2(sp, sq) && range(sp, sq) > 1)
93 snap++;
94 if (!range(sp, sq) && !fouled2(sp, sq)) {
b3a57661
EW
95 makesignal(sp,
96 "collision with %s (%c%c)", sq);
97 if (die() < 4) {
98 makesignal(sp,
99 "fouled with %s (%c%c)",
100 sq);
101 for (m = 0; m < NSHIP && sp->file->fouls[m].turnfoul; m++)
102 ;
103 if (m < NSHIP)
104 Write(W_FOUL, sp, 0,
105 m, turn, l, 0);
106 for (m = 0; m < NSHIP && sq->file->fouls[m].turnfoul; m++)
107 ;
108 if (m < NSHIP)
109 Write(W_FOUL, sq, 0,
110 m, turn, n, 0);
b860dc81 111 }
28c0fb82
EW
112 snap++;
113 }
114 if (snap) {
115 sp->file->last[k + 1] = 0;
116 sq->file->last[k + 1] = 0;
117 sq->file->row = sp->file->row - 1;
b3a57661
EW
118 if (sp->file->dir == 1
119 || sp->file->dir == 5)
28c0fb82
EW
120 sq->file->col =
121 sp->file->col - 1;
b3a57661 122 else
28c0fb82
EW
123 sq->file->col = sp->file->col;
124 sq->file->dir = sp->file->dir;
b860dc81 125 }
759267e0 126 cont2:
b3a57661 127 l++;
b860dc81 128 }
759267e0 129 cont1:
b3a57661 130 n++;
b860dc81
CL
131 }
132 }
b3a57661 133 /*
28c0fb82 134 * Clear old moves. And write out new pos.
b3a57661 135 */
28c0fb82
EW
136 n = 0;
137 foreachship(sp) {
138 if (sp->file->dir != 0) {
139 *sp->file->last = 0;
140 if (row[n] != sp->file->row)
141 Write(W_SHIPROW, sp, 0, sp->file->row, 0, 0, 0);
142 if (col[n] != sp->file->col)
143 Write(W_SHIPCOL, sp, 0, sp->file->col, 0, 0, 0);
144 if (dir[n] != sp->file->dir)
145 Write(W_SHIPDIR, sp, 0, sp->file->dir, 0, 0, 0);
146 if (drift[n] != sp->file->drift)
147 Write(W_DRIFT, sp, 0, sp->file->drift, 0, 0, 0);
148 }
149 n++;
150 }
b860dc81
CL
151}
152
28c0fb82 153stillmoving(k)
b860dc81
CL
154register int k;
155{
b3a57661 156 register struct ship *sp;
b860dc81 157
28c0fb82
EW
158 foreachship(sp)
159 if (sp->file->last[k])
b3a57661 160 return 1;
b3a57661 161 return 0;
b860dc81
CL
162}
163
164isolated(ship)
b3a57661 165register struct ship *ship;
b860dc81 166{
b3a57661 167 register struct ship *sp;
b860dc81 168
b3a57661
EW
169 foreachship(sp) {
170 if (ship != sp && range(ship, sp) <= 10)
171 return 0;
172 }
173 return 1;
b860dc81
CL
174}
175
176push(from, to)
b3a57661 177register struct ship *from, *to;
b860dc81 178{
b860dc81
CL
179 register int bs, sb;
180
28c0fb82
EW
181 sb = to->specs->guns;
182 bs = from->specs->guns;
b3a57661
EW
183 if (sb > bs)
184 return 1;
b860dc81 185 if (sb < bs)
b3a57661
EW
186 return 0;
187 return from < to;
b860dc81
CL
188}
189
28c0fb82 190step(com, sp, moved)
b860dc81 191char com;
28c0fb82
EW
192register struct ship *sp;
193char *moved;
b860dc81 194{
b860dc81
CL
195 register int dist;
196
b3a57661
EW
197 switch (com) {
198 case 'r':
28c0fb82
EW
199 if (++sp->file->dir == 9)
200 sp->file->dir = 1;
b3a57661
EW
201 break;
202 case 'l':
28c0fb82
EW
203 if (--sp->file->dir == 0)
204 sp->file->dir = 8;
b3a57661
EW
205 break;
206 case '0': case '1': case '2': case '3':
207 case '4': case '5': case '6': case '7':
28c0fb82 208 if (sp->file->dir % 2 == 0)
b3a57661
EW
209 dist = dtab[com - '0'];
210 else
211 dist = com - '0';
28c0fb82
EW
212 sp->file->row -= dr[sp->file->dir] * dist;
213 sp->file->col -= dc[sp->file->dir] * dist;
214 *moved = 1;
b3a57661
EW
215 break;
216 case 'b':
217 break;
218 case 'd':
28c0fb82
EW
219 if (!*moved) {
220 if (++sp->file->drift > 2 &&
221 (sp->specs->class >= 3 && !snagged(sp)
222 || (turn & 1) == 0)) {
223 sp->file->row -= dr[winddir];
224 sp->file->col -= dc[winddir];
225 }
226 } else
227 sp->file->drift = 0;
b3a57661 228 break;
b860dc81
CL
229 }
230}
231
b3a57661
EW
232sendbp(from, to, sections, isdefense)
233register struct ship *from, *to;
234int sections;
235char isdefense;
b860dc81
CL
236{
237 int n;
b3a57661 238 register struct BP *bp;
b860dc81 239
b3a57661
EW
240 bp = isdefense ? from->file->DBP : from->file->OBP;
241 for (n = 0; n < 3 && bp[n].turnsent; n++)
242 ;
243 if (n < 3 && sections) {
244 Write(isdefense ? W_DBP : W_OBP, from, 0,
245 turn, to-SHIP(0), sections, 0);
246 if (isdefense)
247 makesignal(from, "repelling boarders",
248 (struct ship *)0);
b860dc81 249 else
b3a57661 250 makesignal(from, "boarding the %s (%c%c)", to);
b860dc81
CL
251 }
252}
253
b3a57661
EW
254toughmelee(ship, to, isdefense, count)
255register struct ship *ship, *to;
256int isdefense, count;
b860dc81 257{
b3a57661
EW
258 register struct BP *bp;
259 register obp = 0;
260 int n, OBP = 0, DBP = 0, dbp = 0;
b860dc81 261 int qual;
b860dc81 262
b3a57661
EW
263 qual = ship->specs->qual;
264 bp = isdefense ? ship->file->DBP : ship->file->OBP;
265 for (n = 0; n < NBP; n++, bp++) {
266 if (bp->turnsent && (to == bp->toship || isdefense)) {
267 obp += bp->mensent / 100
268 ? ship->specs->crew1 * qual : 0;
269 obp += (bp->mensent % 100)/10
270 ? ship->specs->crew2 * qual : 0;
271 obp += bp->mensent % 10
272 ? ship->specs->crew3 * qual : 0;
b860dc81
CL
273 }
274 }
b3a57661
EW
275 if (count || isdefense)
276 return obp;
277 OBP = toughmelee(to, ship, 0, count + 1);
278 dbp = toughmelee(ship, to, 1, count + 1);
279 DBP = toughmelee(to, ship, 1, count + 1);
b860dc81 280 if (OBP > obp + 10 || OBP + DBP >= obp + dbp + 10)
b3a57661 281 return 1;
b860dc81 282 else
b3a57661 283 return 0;
b860dc81
CL
284}
285
286reload()
287{
b3a57661 288 register struct ship *sp;
b860dc81 289
b3a57661
EW
290 foreachship(sp) {
291 sp->file->loadwith = 0;
292 }
b860dc81
CL
293}
294
295checksails()
296{
b3a57661
EW
297 register struct ship *sp;
298 register int rig, full;
299 struct ship *close;
b860dc81 300
b3a57661
EW
301 foreachship(sp) {
302 if (sp->file->captain[0] != 0)
303 continue;
304 rig = sp->specs->rig1;
305 if (windspeed == 6 || windspeed == 5 && sp->specs->class > 4)
b860dc81 306 rig = 0;
b3a57661
EW
307 if (rig && sp->specs->crew3) {
308 close = closestenemy(sp, 0, 0);
309 if (close != 0) {
310 if (range(sp, close) > 9)
311 full = 1;
312 else
b860dc81 313 full = 0;
b3a57661 314 } else
b860dc81 315 full = 0;
b3a57661
EW
316 } else
317 full = 0;
318 if ((sp->file->FS != 0) != full)
319 Write(W_FS, sp, 0, full, 0, 0, 0);
b860dc81
CL
320 }
321}