xref: /original-bsd/games/sail/dr_2.c (revision 2301fdfb)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)dr_2.c	5.3 (Berkeley) 06/18/88";
20 #endif /* not lint */
21 
22 #include "driver.h"
23 
24 #define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
25 
26 thinkofgrapples()
27 {
28 	register struct ship *sp, *sq;
29 	char friendly;
30 
31 	foreachship(sp) {
32 		if (sp->file->captain[0] || sp->file->dir == 0)
33 			continue;
34 		foreachship(sq) {
35 			friendly = sp->nationality == capship(sq)->nationality;
36 			if (!friendly) {
37 				if (sp->file->struck || sp->file->captured != 0)
38 					continue;
39 				if (range(sp, sq) != 1)
40 					continue;
41 				if (grappled2(sp, sq))
42 					if (toughmelee(sp, sq, 0, 0))
43 						ungrap(sp, sq);
44 					else
45 						grap(sp, sq);
46 				else if (couldwin(sp, sq)) {
47 					grap(sp, sq);
48 					sp->file->loadwith = L_GRAPE;
49 				}
50 			} else
51 				ungrap(sp, sq);
52 		}
53 	}
54 }
55 
56 checkup()
57 {
58 	register struct ship *sp, *sq;
59 	register char explode, sink;
60 
61 	foreachship(sp) {
62 		if (sp->file->dir == 0)
63 			continue;
64 		explode = sp->file->explode;
65 		sink = sp->file->sink;
66 		if (explode != 1 && sink != 1)
67 			continue;
68 		if (die() < 5)
69 			continue;
70 		Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 0, 2, 0, 0, 0);
71 		Write(W_DIR, sp, 0, 0, 0, 0, 0);
72 		if (snagged(sp))
73 			foreachship(sq)
74 				cleansnag(sp, sq, 1);
75 		if (sink != 1) {
76 			makesignal(sp, "exploding!", (struct ship *)0);
77 			foreachship(sq) {
78 				if (sp != sq && sq->file->dir && range(sp, sq) < 4)
79 					table(RIGGING, L_EXPLODE, sp->specs->guns/13, sq, sp, 6);
80 			}
81 		} else
82 			makesignal(sp, "sinking!", (struct ship *)0);
83 	}
84 }
85 
86 prizecheck()
87 {
88 	register struct ship *sp;
89 
90 	foreachship(sp) {
91 		if (sp->file->captured == 0)
92 			continue;
93 		if (sp->file->struck || sp->file->dir == 0)
94 			continue;
95 		if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 > sp->file->pcrew * 6) {
96 			Write(W_SIGNAL, sp, 1,
97 				(int)"prize crew overthrown", 0, 0, 0);
98 			Write(W_POINTS, sp->file->captured, 0, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0);
99 			Write(W_CAPTURED, sp, 0, -1, 0, 0, 0);
100 		}
101 	}
102 }
103 
104 strend(str)
105 char *str;
106 {
107 	register char *p;
108 
109 	for (p = str; *p; p++)
110 		;
111 	return p == str ? 0 : p[-1];
112 }
113 
114 closeon(from, to, command, ta, ma, af)
115 register struct ship *from, *to;
116 char command[];
117 int ma, ta, af;
118 {
119 	int high;
120 	char temp[10];
121 
122 	temp[0] = command[0] = '\0';
123 	high = -30000;
124 	try(command, temp, ma, ta, af, ma, from->file->dir, from, to, &high, 0);
125 }
126 
127 int dtab[] = {0,1,1,2,3,4,4,5};		/* diagonal distances in x==y */
128 
129 score(movement, ship, to, onlytemp)
130 char movement[];
131 register struct ship *ship, *to;
132 char onlytemp;
133 {
134 	char drift;
135 	int row, col, dir, total, ran;
136 	register struct File *fp = ship->file;
137 
138 	if ((dir = fp->dir) == 0)
139 		return 0;
140 	row = fp->row;
141 	col = fp->col;
142 	drift = fp->drift;
143 	move(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
144 	if (!*movement)
145 		(void) strcpy(movement, "d");
146 
147 	ran = range(ship, to);
148 	total = -50 * ran;
149 	if (ran < 4 && gunsbear(ship, to))
150 		total += 60;
151 	if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
152 		total = -30000;
153 
154 	if (!onlytemp) {
155 		fp->row = row;
156 		fp->col = col;
157 		fp->dir = dir;
158 	}
159 	return total;
160 }
161 
162 move(p, ship, dir, row, col, drift)
163 register char *p;
164 register struct ship *ship;
165 register char *dir;
166 register short *row, *col;
167 register char *drift;
168 {
169 	int dist;
170 	char moved = 0;
171 
172 	for (; *p; p++) {
173 		switch (*p) {
174 		case 'r':
175 			if (++*dir == 9)
176 				*dir = 1;
177 			break;
178 		case 'l':
179 			if (--*dir == 0)
180 				*dir = 8;
181 			break;
182 		case '1': case '2': case '3': case '4':
183 		case '5': case '6': case '7':
184 			moved++;
185 			if (*dir % 2 == 0)
186 				dist = dtab[*p - '0'];
187 			else
188 				dist = *p - '0';
189 			*row -= dr[*dir] * dist;
190 			*col -= dc[*dir] * dist;
191 			break;
192 		}
193 	}
194 	if (!moved) {
195 		if (windspeed != 0 && ++*drift > 2) {
196 			if (ship->specs->class >= 3 && !snagged(ship)
197 			    || (turn & 1) == 0) {
198 				*row -= dr[winddir];
199 				*col -= dc[winddir];
200 			}
201 		}
202 	} else
203 		*drift = 0;
204 }
205 
206 try(command, temp, ma, ta, af, vma, dir, f, t, high, rakeme)
207 register struct ship *f, *t;
208 int ma, ta, af, *high, rakeme;
209 char command[], temp[];
210 {
211 	register int new, n;
212 	char st[4];
213 #define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
214 
215 	if ((n = strend(temp)) < '1' || n > '9')
216 		for (n = 1; vma - n >= 0; n++) {
217 			(void) sprintf(st, "%d", n);
218 			(void) strcat(temp, st);
219 			new = score(temp, f, t, rakeme);
220 			if (new > *high && (!rakeme || rakeyou)) {
221 				*high = new;
222 				(void) strcpy(command, temp);
223 			}
224 			try(command, temp, ma-n, ta, af, vma-n,
225 				dir, f, t, high, rakeme);
226 			rmend(temp);
227 		}
228 	if (ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r' || !strlen(temp)) {
229 		(void) strcat(temp, "r");
230 		new = score(temp, f, t, rakeme);
231 		if (new > *high && (!rakeme || gunsbear(f, t) && !gunsbear(t, f))) {
232 			*high = new;
233 			(void) strcpy(command, temp);
234 		}
235 		try(command, temp, 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);
236 		rmend(temp);
237 	}
238 	if ((ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r') || !strlen(temp)){
239 		(void) strcat(temp, "l");
240 		new = score(temp, f, t, rakeme);
241 		if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
242 			*high = new;
243 			(void) strcpy(command, temp);
244 		}
245 		try(command, temp, 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);
246 		rmend(temp);
247 	}
248 }
249 
250 rmend(str)
251 char *str;
252 {
253 	register char *p;
254 
255 	for (p = str; *p; p++)
256 		;
257 	if (p != str)
258 		*--p = 0;
259 }
260