gcc41: install mm_malloc.h
[dragonfly.git] / games / sail / dr_2.c
blob79fca2144963d57905e47d15c51844005a5498dd
1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#)dr_2.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/games/sail/dr_2.c,v 1.6 1999/11/30 03:49:32 billf Exp $
35 * $DragonFly: src/games/sail/dr_2.c,v 1.3 2006/09/03 17:33:13 pavalos Exp $
38 #include <string.h>
39 #include "driver.h"
41 #define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
43 static char strend(char *);
44 static int score(char *, struct ship *, struct ship *, char);
45 static void sail_move(char *, struct ship *, unsigned char *, short *, short *, char *);
46 static void try(char *, char *, int, int, int, int, int, struct ship *, struct ship *, int *, int);
47 static void rmend(char *);
49 void
50 thinkofgrapples(void)
52 struct ship *sp, *sq;
53 char friendly;
55 foreachship(sp) {
56 if (sp->file->captain[0] || sp->file->dir == 0)
57 continue;
58 foreachship(sq) {
59 friendly = sp->nationality == capship(sq)->nationality;
60 if (!friendly) {
61 if (sp->file->struck || sp->file->captured != 0)
62 continue;
63 if (range(sp, sq) != 1)
64 continue;
65 if (grappled2(sp, sq))
66 if (toughmelee(sp, sq, 0, 0))
67 ungrap(sp, sq);
68 else
69 grap(sp, sq);
70 else if (couldwin(sp, sq)) {
71 grap(sp, sq);
72 sp->file->loadwith = L_GRAPE;
74 } else
75 ungrap(sp, sq);
80 void
81 checkup(void)
83 struct ship *sp, *sq;
84 char explode, sink;
86 foreachship(sp) {
87 if (sp->file->dir == 0)
88 continue;
89 explode = sp->file->explode;
90 sink = sp->file->sink;
91 if (explode != 1 && sink != 1)
92 continue;
93 if (die() < 5)
94 continue;
95 Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 2, 0, 0, 0);
96 Write(W_DIR, sp, 0, 0, 0, 0);
97 if (snagged(sp))
98 foreachship(sq)
99 cleansnag(sp, sq, 1);
100 if (sink != 1) {
101 makesignal(sp, "exploding!", NULL);
102 foreachship(sq) {
103 if (sp != sq && sq->file->dir && range(sp, sq) < 4)
104 table(RIGGING, L_EXPLODE, sp->specs->guns/13, sq, sp, 6);
106 } else
107 makesignal(sp, "sinking!", NULL);
111 void
112 prizecheck(void)
114 struct ship *sp;
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 > sp->file->pcrew * 6) {
122 Writestr(W_SIGNAL, sp, "prize crew overthrown");
123 Write(W_POINTS, sp->file->captured, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0);
124 Write(W_CAPTURED, sp, -1, 0, 0, 0);
129 static char
130 strend(char *str)
132 char *p;
134 for (p = str; *p; p++)
136 return p == str ? 0 : p[-1];
139 void
140 closeon(struct ship *from, struct ship *to, char command[], int ta, int ma, int af)
142 int high;
143 char temp[10];
145 temp[0] = command[0] = '\0';
146 high = -30000;
147 try(command, temp, ma, ta, af, ma, from->file->dir, from, to, &high, 0);
150 int dtab[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */
152 static int
153 score(char movement[], struct ship *ship, struct ship *to, char onlytemp)
155 char drift;
156 int row, col, dir, total, ran;
157 struct File *fp = ship->file;
159 if ((dir = fp->dir) == 0)
160 return 0;
161 row = fp->row;
162 col = fp->col;
163 drift = fp->drift;
164 sail_move(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
165 if (!*movement)
166 strcpy(movement, "d");
168 ran = range(ship, to);
169 total = -50 * ran;
170 if (ran < 4 && gunsbear(ship, to))
171 total += 60;
172 if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
173 total = -30000;
175 if (!onlytemp) {
176 fp->row = row;
177 fp->col = col;
178 fp->dir = dir;
180 return total;
183 static void
184 sail_move(char *p, struct ship *ship, unsigned char *dir, short *row, short *col, char *drift)
186 int dist;
187 char moved = 0;
189 for (; *p; p++) {
190 switch (*p) {
191 case 'r':
192 if (++*dir == 9)
193 *dir = 1;
194 break;
195 case 'l':
196 if (--*dir == 0)
197 *dir = 8;
198 break;
199 case '1': case '2': case '3': case '4':
200 case '5': case '6': case '7':
201 moved++;
202 if (*dir % 2 == 0)
203 dist = dtab[*p - '0'];
204 else
205 dist = *p - '0';
206 *row -= dr[*dir] * dist;
207 *col -= dc[*dir] * dist;
208 break;
211 if (!moved) {
212 if (windspeed != 0 && ++*drift > 2) {
213 if ((ship->specs->class >= 3 && !snagged(ship))
214 || (turn & 1) == 0) {
215 *row -= dr[winddir];
216 *col -= dc[winddir];
219 } else
220 *drift = 0;
223 static void
224 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)
226 int new, n;
227 char st[4];
228 #define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
230 if ((n = strend(temp)) < '1' || n > '9')
231 for (n = 1; vma - n >= 0; n++) {
232 sprintf(st, "%d", n);
233 strcat(temp, st);
234 new = score(temp, f, t, rakeme);
235 if (new > *high && (!rakeme || rakeyou)) {
236 *high = new;
237 strcpy(command, temp);
239 try(command, temp, ma-n, ta, af, vma-n,
240 dir, f, t, high, rakeme);
241 rmend(temp);
243 if ((ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r') || !strlen(temp)) {
244 strcat(temp, "r");
245 new = score(temp, f, t, rakeme);
246 if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))) {
247 *high = new;
248 strcpy(command, temp);
250 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);
251 rmend(temp);
253 if ((ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r') || !strlen(temp)){
254 strcat(temp, "l");
255 new = score(temp, f, t, rakeme);
256 if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
257 *high = new;
258 strcpy(command, temp);
260 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);
261 rmend(temp);
265 static void
266 rmend(char *str)
268 char *p;
270 for (p = str; *p; p++)
272 if (p != str)
273 *--p = 0;