xref: /openbsd/games/sail/dr_2.c (revision 891d7ab6)
1 /*	$OpenBSD: dr_2.c,v 1.5 2009/10/27 23:59:27 deraadt Exp $	*/
2 /*	$NetBSD: dr_2.c,v 1.4 1995/04/24 12:25:12 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 #define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
37 
38 void
39 thinkofgrapples()
40 {
41 	struct ship *sp, *sq;
42 	char friendly;
43 
44 	foreachship(sp) {
45 		if (sp->file->captain[0] || sp->file->dir == 0)
46 			continue;
47 		foreachship(sq) {
48 			friendly = sp->nationality == capship(sq)->nationality;
49 			if (!friendly) {
50 				if (sp->file->struck || sp->file->captured != 0)
51 					continue;
52 				if (range(sp, sq) != 1)
53 					continue;
54 				if (grappled2(sp, sq)) {
55 					if (is_toughmelee(sp, sq, 0, 0))
56 						ungrap(sp, sq);
57 					else
58 						grap(sp, sq);
59 				} else if (couldwin(sp, sq)) {
60 					grap(sp, sq);
61 					sp->file->loadwith = L_GRAPE;
62 				}
63 			} else
64 				ungrap(sp, sq);
65 		}
66 	}
67 }
68 
69 void
70 checkup()
71 {
72 	struct ship *sp, *sq;
73 	char explode, sink;
74 
75 	foreachship(sp) {
76 		if (sp->file->dir == 0)
77 			continue;
78 		explode = sp->file->explode;
79 		sink = sp->file->sink;
80 		if (explode != 1 && sink != 1)
81 			continue;
82 		if (die() < 5)
83 			continue;
84 		Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 2, 0, 0, 0);
85 		Write(W_DIR, sp, 0, 0, 0, 0);
86 		if (snagged(sp))
87 			foreachship(sq)
88 				cleansnag(sp, sq, 1);
89 		if (sink != 1) {
90 			makemsg(sp, "exploding!");
91 			foreachship(sq) {
92 				if (sp != sq && sq->file->dir && range(sp, sq) < 4)
93 					table(RIGGING, L_EXPLODE, sp->specs->guns/13, sq, sp, 6);
94 			}
95 		} else
96 			makemsg(sp, "sinking!");
97 	}
98 }
99 
100 void
101 prizecheck()
102 {
103 	struct ship *sp;
104 
105 	foreachship(sp) {
106 		if (sp->file->captured == 0)
107 			continue;
108 		if (sp->file->struck || sp->file->dir == 0)
109 			continue;
110 		if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 > sp->file->pcrew * 6) {
111 			Writestr(W_SIGNAL, sp, "prize crew overthrown");
112 			Write(W_POINTS, sp->file->captured, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0);
113 			Write(W_CAPTURED, sp, -1, 0, 0, 0);
114 		}
115 	}
116 }
117 
118 int
119 str_end(str)
120 	const char *str;
121 {
122 	const char *p;
123 
124 	for (p = str; *p; p++)
125 		;
126 	return p == str ? 0 : p[-1];
127 }
128 
129 void
130 closeon(from, to, command, commandl, ta, ma, af)
131 	struct ship *from, *to;
132 	char command[];
133 	size_t commandl;
134 	int ma, ta, af;
135 {
136 	int high;
137 	char temp[10];
138 
139 	temp[0] = command[0] = '\0';
140 	high = -30000;
141 	try(command, commandl, temp, sizeof temp, ma, ta, af, ma, from->file->dir,
142 	    from, to, &high, 0);
143 }
144 
145 const int dtab[] = {0,1,1,2,3,4,4,5};	/* diagonal distances in x==y */
146 
147 int
148 score(movement, movementl, ship, to, onlytemp)
149 	char movement[];
150 	size_t movementl;
151 	struct ship *ship, *to;
152 	char onlytemp;
153 {
154 	char drift;
155 	int row, col, dir, total, ran;
156 	struct File *fp = ship->file;
157 
158 	if ((dir = fp->dir) == 0)
159 		return 0;
160 	row = fp->row;
161 	col = fp->col;
162 	drift = fp->drift;
163 	move_ship(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
164 	if (!*movement)
165 		(void) strlcpy(movement, "d", movementl);
166 
167 	ran = range(ship, to);
168 	total = -50 * ran;
169 	if (ran < 4 && gunsbear(ship, to))
170 		total += 60;
171 	if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
172 		total = -30000;
173 
174 	if (!onlytemp) {
175 		fp->row = row;
176 		fp->col = col;
177 		fp->dir = dir;
178 	}
179 	return total;
180 }
181 
182 void
183 move_ship(p, ship, dir, row, col, drift)
184 	const char *p;
185 	struct ship *ship;
186 	unsigned char *dir;
187 	short *row, *col;
188 	char *drift;
189 {
190 	int dist;
191 	char moved = 0;
192 
193 	for (; *p; p++) {
194 		switch (*p) {
195 		case 'r':
196 			if (++*dir == 9)
197 				*dir = 1;
198 			break;
199 		case 'l':
200 			if (--*dir == 0)
201 				*dir = 8;
202 			break;
203 		case '1': case '2': case '3': case '4':
204 		case '5': case '6': case '7':
205 			moved++;
206 			if (*dir % 2 == 0)
207 				dist = dtab[*p - '0'];
208 			else
209 				dist = *p - '0';
210 			*row -= dr[*dir] * dist;
211 			*col -= dc[*dir] * dist;
212 			break;
213 		}
214 	}
215 	if (!moved) {
216 		if (windspeed != 0 && ++*drift > 2) {
217 			if ((ship->specs->class >= 3 && !snagged(ship))
218 			    || (turn & 1) == 0) {
219 				*row -= dr[winddir];
220 				*col -= dc[winddir];
221 			}
222 		}
223 	} else
224 		*drift = 0;
225 }
226 
227 void
228 try(command, commandl, temp, templ, ma, ta, af, vma, dir, f, t, high, rakeme)
229 	char command[];
230 	size_t commandl;
231 	char temp[];
232 	size_t templ;
233 	int ma, ta, af, vma, dir;
234 	struct ship *f, *t;
235 	int *high, rakeme;
236 {
237 	int new, n;
238 	char st[4];
239 #define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
240 
241 	if ((n = str_end(temp)) < '1' || n > '9')
242 		for (n = 1; vma - n >= 0; n++) {
243 			(void) snprintf(st, sizeof st, "%d", n);
244 			(void) strlcat(temp, st, templ);
245 			new = score(temp, templ, f, t, rakeme);
246 			if (new > *high && (!rakeme || rakeyou)) {
247 				*high = new;
248 				(void) strlcpy(command, temp, commandl);
249 			}
250 			try(command, commandl, temp, templ, ma-n, ta, af, vma-n,
251 				dir, f, t, high, rakeme);
252 			rmend(temp);
253 		}
254 	if ((ma > 0 && ta > 0 && (n = str_end(temp)) != 'l' && n != 'r') || !strlen(temp)) {
255 		(void) strlcat(temp, "r", templ);
256 		new = score(temp, templ, f, t, rakeme);
257 		if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))) {
258 			*high = new;
259 			(void) strlcpy(command, temp, commandl);
260 		}
261 		try(command, commandl, temp, templ, 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);
262 		rmend(temp);
263 	}
264 	if ((ma > 0 && ta > 0 && (n = str_end(temp)) != 'l' && n != 'r') || !strlen(temp)){
265 		(void) strlcat(temp, "l", templ);
266 		new = score(temp, templ, f, t, rakeme);
267 		if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
268 			*high = new;
269 			(void) strlcpy(command, temp, commandl);
270 		}
271 		try(command, commandl, temp, templ, 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);
272 		rmend(temp);
273 	}
274 }
275 
276 void
277 rmend(str)
278 	char *str;
279 {
280 	char *p;
281 
282 	for (p = str; *p; p++)
283 		;
284 	if (p != str)
285 		*--p = 0;
286 }
287