portside() is left handed
[unix-history] / usr / src / games / sail / dr_3.c
CommitLineData
b860dc81 1#ifndef lint
daf95c1e 2static char *sccsid = "@(#)dr_3.c 2.5 84/01/19";
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 */
6ca45914 11 register int k, l; /* r8, r7 */
28c0fb82
EW
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)
6ca45914 31 *sp->file->movebuf = '\0';
b860dc81 32 else
6ca45914 33 closeon(sp, closest, sp->file->movebuf,
b3a57661
EW
34 ta, ma, af);
35 } else
6ca45914 36 *sp->file->movebuf = '\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))
6ca45914 47 (void) strcpy(sp->file->movebuf, "d");
b3a57661 48 else
6ca45914
EW
49 if (*sp->file->movebuf != 'd')
50 (void) strcat(sp->file->movebuf, "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.
6ca45914 65 * And propagate the nulls at the end of sp->file->movebuf.
b3a57661
EW
66 */
67 n = 0;
68 foreachship(sp) {
6ca45914
EW
69 if (!sp->file->movebuf[k])
70 sp->file->movebuf[k+1] = '\0';
28c0fb82 71 else if (sp->file->dir)
6ca45914 72 step(sp->file->movebuf[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);
6ca45914
EW
101 Write(W_FOUL, sp, 0, l, 0, 0, 0);
102 Write(W_FOUL, sq, 0, n, 0, 0, 0);
b860dc81 103 }
28c0fb82
EW
104 snap++;
105 }
106 if (snap) {
6ca45914
EW
107 sp->file->movebuf[k + 1] = 0;
108 sq->file->movebuf[k + 1] = 0;
28c0fb82 109 sq->file->row = sp->file->row - 1;
b3a57661
EW
110 if (sp->file->dir == 1
111 || sp->file->dir == 5)
28c0fb82
EW
112 sq->file->col =
113 sp->file->col - 1;
b3a57661 114 else
28c0fb82
EW
115 sq->file->col = sp->file->col;
116 sq->file->dir = sp->file->dir;
b860dc81 117 }
759267e0 118 cont2:
b3a57661 119 l++;
b860dc81 120 }
759267e0 121 cont1:
b3a57661 122 n++;
b860dc81
CL
123 }
124 }
b3a57661 125 /*
28c0fb82 126 * Clear old moves. And write out new pos.
b3a57661 127 */
28c0fb82
EW
128 n = 0;
129 foreachship(sp) {
130 if (sp->file->dir != 0) {
6ca45914 131 *sp->file->movebuf = 0;
28c0fb82 132 if (row[n] != sp->file->row)
6ca45914 133 Write(W_ROW, sp, 0, sp->file->row, 0, 0, 0);
28c0fb82 134 if (col[n] != sp->file->col)
6ca45914 135 Write(W_COL, sp, 0, sp->file->col, 0, 0, 0);
28c0fb82 136 if (dir[n] != sp->file->dir)
6ca45914 137 Write(W_DIR, sp, 0, sp->file->dir, 0, 0, 0);
28c0fb82
EW
138 if (drift[n] != sp->file->drift)
139 Write(W_DRIFT, sp, 0, sp->file->drift, 0, 0, 0);
140 }
141 n++;
142 }
b860dc81
CL
143}
144
28c0fb82 145stillmoving(k)
b860dc81
CL
146register int k;
147{
b3a57661 148 register struct ship *sp;
b860dc81 149
28c0fb82 150 foreachship(sp)
6ca45914 151 if (sp->file->movebuf[k])
b3a57661 152 return 1;
b3a57661 153 return 0;
b860dc81
CL
154}
155
156isolated(ship)
b3a57661 157register struct ship *ship;
b860dc81 158{
b3a57661 159 register struct ship *sp;
b860dc81 160
b3a57661
EW
161 foreachship(sp) {
162 if (ship != sp && range(ship, sp) <= 10)
163 return 0;
164 }
165 return 1;
b860dc81
CL
166}
167
168push(from, to)
b3a57661 169register struct ship *from, *to;
b860dc81 170{
b860dc81
CL
171 register int bs, sb;
172
28c0fb82
EW
173 sb = to->specs->guns;
174 bs = from->specs->guns;
b3a57661
EW
175 if (sb > bs)
176 return 1;
b860dc81 177 if (sb < bs)
b3a57661
EW
178 return 0;
179 return from < to;
b860dc81
CL
180}
181
28c0fb82 182step(com, sp, moved)
b860dc81 183char com;
28c0fb82
EW
184register struct ship *sp;
185char *moved;
b860dc81 186{
b860dc81
CL
187 register int dist;
188
b3a57661
EW
189 switch (com) {
190 case 'r':
28c0fb82
EW
191 if (++sp->file->dir == 9)
192 sp->file->dir = 1;
b3a57661
EW
193 break;
194 case 'l':
28c0fb82
EW
195 if (--sp->file->dir == 0)
196 sp->file->dir = 8;
b3a57661 197 break;
daf95c1e
EW
198 case '0': case '1': case '2': case '3':
199 case '4': case '5': case '6': case '7':
28c0fb82 200 if (sp->file->dir % 2 == 0)
b3a57661
EW
201 dist = dtab[com - '0'];
202 else
203 dist = com - '0';
28c0fb82
EW
204 sp->file->row -= dr[sp->file->dir] * dist;
205 sp->file->col -= dc[sp->file->dir] * dist;
206 *moved = 1;
b3a57661
EW
207 break;
208 case 'b':
209 break;
210 case 'd':
28c0fb82
EW
211 if (!*moved) {
212 if (++sp->file->drift > 2 &&
213 (sp->specs->class >= 3 && !snagged(sp)
214 || (turn & 1) == 0)) {
215 sp->file->row -= dr[winddir];
216 sp->file->col -= dc[winddir];
217 }
218 } else
219 sp->file->drift = 0;
b3a57661 220 break;
b860dc81
CL
221 }
222}
223
b3a57661
EW
224sendbp(from, to, sections, isdefense)
225register struct ship *from, *to;
226int sections;
227char isdefense;
b860dc81
CL
228{
229 int n;
b3a57661 230 register struct BP *bp;
b860dc81 231
b3a57661 232 bp = isdefense ? from->file->DBP : from->file->OBP;
daf95c1e 233 for (n = 0; n < NBP && bp[n].turnsent; n++)
b3a57661 234 ;
daf95c1e 235 if (n < NBP && sections) {
b3a57661 236 Write(isdefense ? W_DBP : W_OBP, from, 0,
daf95c1e 237 n, turn, to->file->index, sections);
b3a57661
EW
238 if (isdefense)
239 makesignal(from, "repelling boarders",
240 (struct ship *)0);
b860dc81 241 else
b3a57661 242 makesignal(from, "boarding the %s (%c%c)", to);
b860dc81
CL
243 }
244}
245
b3a57661
EW
246toughmelee(ship, to, isdefense, count)
247register struct ship *ship, *to;
248int isdefense, count;
b860dc81 249{
b3a57661
EW
250 register struct BP *bp;
251 register obp = 0;
252 int n, OBP = 0, DBP = 0, dbp = 0;
b860dc81 253 int qual;
b860dc81 254
b3a57661
EW
255 qual = ship->specs->qual;
256 bp = isdefense ? ship->file->DBP : ship->file->OBP;
257 for (n = 0; n < NBP; n++, bp++) {
258 if (bp->turnsent && (to == bp->toship || isdefense)) {
259 obp += bp->mensent / 100
260 ? ship->specs->crew1 * qual : 0;
261 obp += (bp->mensent % 100)/10
262 ? ship->specs->crew2 * qual : 0;
263 obp += bp->mensent % 10
264 ? ship->specs->crew3 * qual : 0;
b860dc81
CL
265 }
266 }
b3a57661
EW
267 if (count || isdefense)
268 return obp;
269 OBP = toughmelee(to, ship, 0, count + 1);
270 dbp = toughmelee(ship, to, 1, count + 1);
271 DBP = toughmelee(to, ship, 1, count + 1);
b860dc81 272 if (OBP > obp + 10 || OBP + DBP >= obp + dbp + 10)
b3a57661 273 return 1;
b860dc81 274 else
b3a57661 275 return 0;
b860dc81
CL
276}
277
278reload()
279{
b3a57661 280 register struct ship *sp;
b860dc81 281
b3a57661
EW
282 foreachship(sp) {
283 sp->file->loadwith = 0;
284 }
b860dc81
CL
285}
286
287checksails()
288{
b3a57661
EW
289 register struct ship *sp;
290 register int rig, full;
291 struct ship *close;
b860dc81 292
b3a57661
EW
293 foreachship(sp) {
294 if (sp->file->captain[0] != 0)
295 continue;
296 rig = sp->specs->rig1;
297 if (windspeed == 6 || windspeed == 5 && sp->specs->class > 4)
b860dc81 298 rig = 0;
b3a57661
EW
299 if (rig && sp->specs->crew3) {
300 close = closestenemy(sp, 0, 0);
301 if (close != 0) {
302 if (range(sp, close) > 9)
303 full = 1;
304 else
b860dc81 305 full = 0;
b3a57661 306 } else
b860dc81 307 full = 0;
b3a57661
EW
308 } else
309 full = 0;
310 if ((sp->file->FS != 0) != full)
311 Write(W_FS, sp, 0, full, 0, 0, 0);
b860dc81
CL
312 }
313}