xref: /dragonfly/games/sail/dr_2.c (revision 82730a9c)
1 /*-
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)dr_2.c	8.1 (Berkeley) 5/31/93
30  * $FreeBSD: src/games/sail/dr_2.c,v 1.6 1999/11/30 03:49:32 billf Exp $
31  * $DragonFly: src/games/sail/dr_2.c,v 1.3 2006/09/03 17:33:13 pavalos Exp $
32  */
33 
34 #include <string.h>
35 #include "driver.h"
36 
37 #define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
38 
39 static char strend(char *);
40 static int score(char *, struct ship *, struct ship *, char);
41 static void sail_move(char *, struct ship *, unsigned char *, short *, short *, char *);
42 static void try(char *, char *, int, int, int, int, int, struct ship *, struct ship *, int *, int);
43 static void rmend(char *);
44 
45 int dtab[] = {0,1,1,2,3,4,4,5};		/* diagonal distances in x==y */
46 
47 void
48 thinkofgrapples(void)
49 {
50 	struct ship *sp, *sq;
51 	char friendly;
52 
53 	foreachship(sp) {
54 		if (sp->file->captain[0] || sp->file->dir == 0)
55 			continue;
56 		foreachship(sq) {
57 			friendly = sp->nationality == capship(sq)->nationality;
58 			if (!friendly) {
59 				if (sp->file->struck || sp->file->captured != 0)
60 					continue;
61 				if (range(sp, sq) != 1)
62 					continue;
63 				if (grappled2(sp, sq)) {
64 					if (toughmelee(sp, sq, 0, 0))
65 						ungrap(sp, sq);
66 					else
67 						grap(sp, sq);
68 				} else if (couldwin(sp, sq)) {
69 					grap(sp, sq);
70 					sp->file->loadwith = L_GRAPE;
71 				}
72 			} else
73 				ungrap(sp, sq);
74 		}
75 	}
76 }
77 
78 void
79 checkup(void)
80 {
81 	struct ship *sp, *sq;
82 	char explode, sink;
83 
84 	foreachship(sp) {
85 		if (sp->file->dir == 0)
86 			continue;
87 		explode = sp->file->explode;
88 		sink = sp->file->sink;
89 		if (explode != 1 && sink != 1)
90 			continue;
91 		if (die() < 5)
92 			continue;
93 		Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 2, 0, 0, 0);
94 		Write(W_DIR, sp, 0, 0, 0, 0);
95 		if (snagged(sp))
96 			foreachship(sq)
97 				cleansnag(sp, sq, 1);
98 		if (sink != 1) {
99 			makesignal(sp, "exploding!", NULL);
100 			foreachship(sq) {
101 				if (sp != sq && sq->file->dir &&
102 				    range(sp, sq) < 4)
103 					table(RIGGING, L_EXPLODE,
104 					    sp->specs->guns/13, sq, sp, 6);
105 			}
106 		} else
107 			makesignal(sp, "sinking!", NULL);
108 	}
109 }
110 
111 void
112 prizecheck(void)
113 {
114 	struct ship *sp;
115 
116 	foreachship(sp) {
117 		if (sp->file->captured == 0)
118 			continue;
119 		if (sp->file->struck || sp->file->dir == 0)
120 			continue;
121 		if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 >
122 		    sp->file->pcrew * 6) {
123 			Writestr(W_SIGNAL, sp, "prize crew overthrown");
124 			Write(W_POINTS, sp->file->captured, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0);
125 			Write(W_CAPTURED, sp, -1, 0, 0, 0);
126 		}
127 	}
128 }
129 
130 static char
131 strend(char *str)
132 {
133 	char *p;
134 
135 	for (p = str; *p; p++)
136 		;
137 	return p == str ? 0 : p[-1];
138 }
139 
140 void
141 closeon(struct ship *from, struct ship *to, char command[], int ta, int ma, int af)
142 {
143 	int high;
144 	char temp[10];
145 
146 	temp[0] = command[0] = '\0';
147 	high = -30000;
148 	try(command, temp, ma, ta, af, ma, from->file->dir, from, to, &high, 0);
149 }
150 
151 static int
152 score(char movement[], struct ship *ship, struct ship *to, 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 	sail_move(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
164 	if (!*movement)
165 		strcpy(movement, "d");
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 static void
183 sail_move(char *p, struct ship *ship, unsigned char *dir, short *row, short *col, char *drift)
184 {
185 	int dist;
186 	char moved = 0;
187 
188 	for (; *p; p++) {
189 		switch (*p) {
190 		case 'r':
191 			if (++*dir == 9)
192 				*dir = 1;
193 			break;
194 		case 'l':
195 			if (--*dir == 0)
196 				*dir = 8;
197 			break;
198 		case '1': case '2': case '3': case '4':
199 		case '5': case '6': case '7':
200 			moved++;
201 			if (*dir % 2 == 0)
202 				dist = dtab[*p - '0'];
203 			else
204 				dist = *p - '0';
205 			*row -= dr[*dir] * dist;
206 			*col -= dc[*dir] * dist;
207 			break;
208 		}
209 	}
210 	if (!moved) {
211 		if (windspeed != 0 && ++*drift > 2) {
212 			if ((ship->specs->class >= 3 && !snagged(ship))
213 			    || (turn & 1) == 0) {
214 				*row -= dr[winddir];
215 				*col -= dc[winddir];
216 			}
217 		}
218 	} else
219 		*drift = 0;
220 }
221 
222 static void
223 try(char command[], char temp[], int ma, int ta, int af, int vma, int dir, struct ship *f, struct ship *t, int *high, int rakeme)
224 {
225 	int new, n;
226 	char st[4];
227 #define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
228 
229 	if ((n = strend(temp)) < '1' || n > '9')
230 		for (n = 1; vma - n >= 0; n++) {
231 			sprintf(st, "%d", n);
232 			strcat(temp, st);
233 			new = score(temp, f, t, rakeme);
234 			if (new > *high && (!rakeme || rakeyou)) {
235 				*high = new;
236 				strcpy(command, temp);
237 			}
238 			try(command, temp, ma-n, ta, af, vma-n,
239 				dir, f, t, high, rakeme);
240 			rmend(temp);
241 		}
242 	if ((ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r') || !strlen(temp)) {
243 		strcat(temp, "r");
244 		new = score(temp, f, t, rakeme);
245 		if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))) {
246 			*high = new;
247 			strcpy(command, temp);
248 		}
249 		try(command, temp, ma-1, ta-1, af,
250 		    min(ma-1, maxmove(f, (dir == 8 ? 1 : dir+1), 0)),
251 		    (dir == 8 ? 1 : dir+1), f, t, high, rakeme);
252 		rmend(temp);
253 	}
254 	if ((ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r') || !strlen(temp)) {
255 		strcat(temp, "l");
256 		new = score(temp, f, t, rakeme);
257 		if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))) {
258 			*high = new;
259 			strcpy(command, temp);
260 		}
261 		try(command, temp, ma-1, ta-1, af,
262 		    (min(ma-1,maxmove(f, (dir-1 ? dir-1 : 8), 0))),
263 		    (dir-1 ? dir -1 : 8), f, t, high, rakeme);
264 		rmend(temp);
265 	}
266 }
267 
268 static void
269 rmend(char *str)
270 {
271 	char *p;
272 
273 	for (p = str; *p; p++)
274 		;
275 	if (p != str)
276 		*--p = 0;
277 }
278