xref: /openbsd/games/sail/dr_3.c (revision 898184e3)
1 /*	$OpenBSD: dr_3.c,v 1.5 2009/10/27 23:59:27 deraadt Exp $	*/
2 /*	$NetBSD: dr_3.c,v 1.3 1995/04/22 10:36:49 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include "driver.h"
34 #include <stdlib.h>
35 
36 void
37 moveall()		/* move all comp ships */
38 {
39 	struct ship *sp, *sq;		/* r11, r10 */
40 	int n;				/* r9 */
41 	int k, l;			/* r8, r7 */
42 	int row[NSHIP], col[NSHIP], dir[NSHIP], drift[NSHIP];
43 	char moved[NSHIP];
44 
45 	/*
46 	 * first try to create moves for OUR ships
47 	 */
48 	foreachship(sp) {
49 		struct ship *closest;
50 		int ma, ta;
51 		char af;
52 
53 		if (sp->file->captain[0] || sp->file->dir == 0)
54 			continue;
55 		if (!sp->file->struck && windspeed && !snagged(sp)
56 		    && sp->specs->crew3) {
57 			ta = maxturns(sp, &af);
58 			ma = maxmove(sp, sp->file->dir, 0);
59 			closest = closestenemy(sp, 0, 0);
60 			if (closest == 0)
61 				*sp->file->movebuf = '\0';
62 			else
63 				closeon(sp, closest, sp->file->movebuf,
64 					sizeof sp->file->movebuf,
65 					ta, ma, af);
66 		} else
67 			*sp->file->movebuf = '\0';
68 	}
69 	/*
70 	 * Then execute the moves for ALL ships (dead ones too),
71 	 * checking for collisions and snags at each step.
72 	 * The old positions are saved in row[], col[], dir[].
73 	 * At the end, we compare and write out the changes.
74 	 */
75 	n = 0;
76 	foreachship(sp) {
77 		if (snagged(sp))
78 			(void) strlcpy(sp->file->movebuf, "d",
79 			    sizeof sp->file->movebuf);
80 		else
81 			if (*sp->file->movebuf != 'd')
82 				(void) strlcat(sp->file->movebuf, "d",
83 					sizeof sp->file->movebuf);
84 		row[n] = sp->file->row;
85 		col[n] = sp->file->col;
86 		dir[n] = sp->file->dir;
87 		drift[n] = sp->file->drift;
88 		moved[n] = 0;
89 		n++;
90 	}
91 	/*
92 	 * Now resolve collisions.
93 	 * This is the tough part.
94 	 */
95 	for (k = 0; stillmoving(k); k++) {
96 		/*
97 		 * Step once.
98 		 * And propagate the nulls at the end of sp->file->movebuf.
99 		 */
100 		n = 0;
101 		foreachship(sp) {
102 			if (!sp->file->movebuf[k])
103 				sp->file->movebuf[k+1] = '\0';
104 			else if (sp->file->dir)
105 				step(sp->file->movebuf[k], sp, &moved[n]);
106 			n++;
107 		}
108 		/*
109 		 * The real stuff.
110 		 */
111 		n = 0;
112 		foreachship(sp) {
113 			if (sp->file->dir == 0 || is_isolated(sp))
114 				goto cont1;
115 			l = 0;
116 			foreachship(sq) {
117 				char snap = 0;
118 
119 				if (sp == sq)
120 					goto cont2;
121 				if (sq->file->dir == 0)
122 					goto cont2;
123 				if (!push(sp, sq))
124 					goto cont2;
125 				if (snagged2(sp, sq) && range(sp, sq) > 1)
126 					snap++;
127 				if (!range(sp, sq) && !fouled2(sp, sq)) {
128 					makesignal(sp, "collision with $$", sq);
129 					if (die() < 4) {
130 						makesignal(sp, "fouled with $$",
131 							sq);
132 						Write(W_FOUL, sp, l, 0, 0, 0);
133 						Write(W_FOUL, sq, n, 0, 0, 0);
134 					}
135 					snap++;
136 				}
137 				if (snap) {
138 					sp->file->movebuf[k + 1] = 0;
139 					sq->file->movebuf[k + 1] = 0;
140 					sq->file->row = sp->file->row - 1;
141 					if (sp->file->dir == 1
142 					    || sp->file->dir == 5)
143 						sq->file->col =
144 							sp->file->col - 1;
145 					else
146 						sq->file->col = sp->file->col;
147 					sq->file->dir = sp->file->dir;
148 				}
149 			cont2:
150 				l++;
151 			}
152 		cont1:
153 			n++;
154 		}
155 	}
156 	/*
157 	 * Clear old moves.  And write out new pos.
158 	 */
159 	n = 0;
160 	foreachship(sp) {
161 		if (sp->file->dir != 0) {
162 			*sp->file->movebuf = 0;
163 			if (row[n] != sp->file->row)
164 				Write(W_ROW, sp, sp->file->row, 0, 0, 0);
165 			if (col[n] != sp->file->col)
166 				Write(W_COL, sp, sp->file->col, 0, 0, 0);
167 			if (dir[n] != sp->file->dir)
168 				Write(W_DIR, sp, sp->file->dir, 0, 0, 0);
169 			if (drift[n] != sp->file->drift)
170 				Write(W_DRIFT, sp, sp->file->drift, 0, 0, 0);
171 		}
172 		n++;
173 	}
174 }
175 
176 int
177 stillmoving(k)
178 	int k;
179 {
180 	struct ship *sp;
181 
182 	foreachship(sp)
183 		if (sp->file->movebuf[k])
184 			return 1;
185 	return 0;
186 }
187 
188 int
189 is_isolated(ship)
190 	struct ship *ship;
191 {
192 	struct ship *sp;
193 
194 	foreachship(sp) {
195 		if (ship != sp && range(ship, sp) <= 10)
196 			return 0;
197 	}
198 	return 1;
199 }
200 
201 int
202 push(from, to)
203 	struct ship *from, *to;
204 {
205 	int bs, sb;
206 
207 	sb = to->specs->guns;
208 	bs = from->specs->guns;
209 	if (sb > bs)
210 		return 1;
211 	if (sb < bs)
212 		return 0;
213 	return from < to;
214 }
215 
216 void
217 step(com, sp, moved)
218 	char com;
219 	struct ship *sp;
220 	char *moved;
221 {
222 	int dist;
223 
224 	switch (com) {
225 	case 'r':
226 		if (++sp->file->dir == 9)
227 			sp->file->dir = 1;
228 		break;
229 	case 'l':
230 		if (--sp->file->dir == 0)
231 			sp->file->dir = 8;
232 		break;
233 		case '0': case '1': case '2': case '3':
234 		case '4': case '5': case '6': case '7':
235 		if (sp->file->dir % 2 == 0)
236 			dist = dtab[com - '0'];
237 		else
238 			dist = com - '0';
239 		sp->file->row -= dr[sp->file->dir] * dist;
240 		sp->file->col -= dc[sp->file->dir] * dist;
241 		*moved = 1;
242 		break;
243 	case 'b':
244 		break;
245 	case 'd':
246 		if (!*moved) {
247 			if (windspeed != 0 && ++sp->file->drift > 2 &&
248 			    ((sp->specs->class >= 3 && !snagged(sp))
249 			     || (turn & 1) == 0)) {
250 				sp->file->row -= dr[winddir];
251 				sp->file->col -= dc[winddir];
252 			}
253 		} else
254 			sp->file->drift = 0;
255 		break;
256 	}
257 }
258 
259 void
260 sendbp(from, to, sections, isdefense)
261 	struct ship *from, *to;
262 	int sections;
263 	char isdefense;
264 {
265 	int n;
266 	struct BP *bp;
267 
268 	bp = isdefense ? from->file->DBP : from->file->OBP;
269 	for (n = 0; n < NBP && bp[n].turnsent; n++)
270 		;
271 	if (n < NBP && sections) {
272 		Write(isdefense ? W_DBP : W_OBP, from,
273 			n, turn, to->file->index, sections);
274 		if (isdefense)
275 			makemsg(from, "repelling boarders");
276 		else
277 			makesignal(from, "boarding the $$", to);
278 	}
279 }
280 
281 int
282 is_toughmelee(ship, to, isdefense, count)
283 	struct ship *ship, *to;
284 	int isdefense, count;
285 {
286 	struct BP *bp;
287 	int obp = 0;
288 	int n, OBP = 0, DBP = 0, dbp = 0;
289 	int qual;
290 
291 	qual = ship->specs->qual;
292 	bp = isdefense ? ship->file->DBP : ship->file->OBP;
293 	for (n = 0; n < NBP; n++, bp++) {
294 		if (bp->turnsent && (to == bp->toship || isdefense)) {
295 			obp += bp->mensent / 100
296 				? ship->specs->crew1 * qual : 0;
297 			obp += (bp->mensent % 100)/10
298 				? ship->specs->crew2 * qual : 0;
299 			obp += bp->mensent % 10
300 				? ship->specs->crew3 * qual : 0;
301 		}
302 	}
303 	if (count || isdefense)
304 		return obp;
305 	OBP = is_toughmelee(to, ship, 0, count + 1);
306 	dbp = is_toughmelee(ship, to, 1, count + 1);
307 	DBP = is_toughmelee(to, ship, 1, count + 1);
308 	if (OBP > obp + 10 || OBP + DBP >= obp + dbp + 10)
309 		return 1;
310 	else
311 		return 0;
312 }
313 
314 void
315 reload()
316 {
317 	struct ship *sp;
318 
319 	foreachship(sp) {
320 		sp->file->loadwith = 0;
321 	}
322 }
323 
324 void
325 checksails()
326 {
327 	struct ship *sp;
328 	int rig, full;
329 	struct ship *close;
330 
331 	foreachship(sp) {
332 		if (sp->file->captain[0] != 0)
333 			continue;
334 		rig = sp->specs->rig1;
335 		if (windspeed == 6 || (windspeed == 5 && sp->specs->class > 4))
336 			rig = 0;
337 		if (rig && sp->specs->crew3) {
338 			close = closestenemy(sp, 0, 0);
339 			if (close != 0) {
340 				if (range(sp, close) > 9)
341 					full = 1;
342 				else
343 					full = 0;
344 			} else
345 				full = 0;
346 		} else
347 			full = 0;
348 		if ((sp->file->FS != 0) != full)
349 			Write(W_FS, sp, full, 0, 0, 0);
350 	}
351 }
352