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