Add copyright
[unix-history] / usr / src / games / sail / dr_2.c
CommitLineData
b5f0675e 1/*
1e008c14
DF
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
b5f0675e
EW
5 */
6
717de9f0 7#ifndef lint
1e008c14
DF
8static char sccsid[] = "@(#)dr_2.c 5.1 (Berkeley) %G%";
9#endif not lint
717de9f0 10
b3a57661
EW
11#include "driver.h"
12
13#define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
717de9f0
CL
14
15thinkofgrapples()
16{
b3a57661
EW
17 register struct ship *sp, *sq;
18 char friendly;
717de9f0 19
b3a57661
EW
20 foreachship(sp) {
21 if (sp->file->captain[0] || sp->file->dir == 0)
22 continue;
23 foreachship(sq) {
24 friendly = sp->nationality == capship(sq)->nationality;
25 if (!friendly) {
26 if (sp->file->struck || sp->file->captured != 0)
27 continue;
28 if (range(sp, sq) != 1)
29 continue;
30 if (grappled2(sp, sq))
31 if (toughmelee(sp, sq, 0, 0))
32 ungrap(sp, sq);
33 else
34 grap(sp, sq);
35 else if (couldwin(sp, sq)) {
36 grap(sp, sq);
37 sp->file->loadwith = L_GRAPE;
717de9f0 38 }
b3a57661
EW
39 } else
40 ungrap(sp, sq);
717de9f0
CL
41 }
42 }
43}
44
717de9f0
CL
45checkup()
46{
b3a57661
EW
47 register struct ship *sp, *sq;
48 register char explode, sink;
717de9f0 49
b3a57661 50 foreachship(sp) {
8b4ea3df
EW
51 if (sp->file->dir == 0)
52 continue;
b3a57661
EW
53 explode = sp->file->explode;
54 sink = sp->file->sink;
b3a57661
EW
55 if (explode != 1 && sink != 1)
56 continue;
8b4ea3df
EW
57 if (die() < 5)
58 continue;
59 Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 0, 2, 0, 0, 0);
6ca45914
EW
60 Write(W_DIR, sp, 0, 0, 0, 0, 0);
61 if (snagged(sp))
62 foreachship(sq)
63 cleansnag(sp, sq, 1);
b3a57661
EW
64 if (sink != 1) {
65 makesignal(sp, "exploding!", (struct ship *)0);
66 foreachship(sq) {
67 if (sp != sq && sq->file->dir && range(sp, sq) < 4)
68 table(RIGGING, L_EXPLODE, sp->specs->guns/13, sq, sp, 6);
69 }
70 } else
71 makesignal(sp, "sinking!", (struct ship *)0);
717de9f0
CL
72 }
73}
74
75prizecheck()
76{
b3a57661 77 register struct ship *sp;
717de9f0 78
b3a57661
EW
79 foreachship(sp) {
80 if (sp->file->captured == 0)
81 continue;
82 if (sp->file->struck || sp->file->dir == 0)
83 continue;
0483f58f 84 if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 > sp->file->pcrew * 6) {
b3a57661
EW
85 Write(W_SIGNAL, sp, 1,
86 (int)"prize crew overthrown", 0, 0, 0);
0483f58f
EW
87 Write(W_POINTS, sp->file->captured, 0, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0);
88 Write(W_CAPTURED, sp, 0, -1, 0, 0, 0);
717de9f0
CL
89 }
90 }
91}
92
b3a57661 93strend(str)
717de9f0
CL
94char *str;
95{
b3a57661 96 register char *p;
717de9f0 97
b3a57661
EW
98 for (p = str; *p; p++)
99 ;
100 return p == str ? 0 : p[-1];
717de9f0
CL
101}
102
b3a57661
EW
103closeon(from, to, command, ta, ma, af)
104register struct ship *from, *to;
105char command[];
106int ma, ta, af;
717de9f0
CL
107{
108 int high;
109 char temp[10];
110
111 temp[0] = command[0] = '\0';
112 high = -30000;
b3a57661 113 try(command, temp, ma, ta, af, ma, from->file->dir, from, to, &high, 0);
717de9f0
CL
114}
115
b3a57661 116int dtab[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */
717de9f0 117
b3a57661
EW
118score(movement, ship, to, onlytemp)
119char movement[];
120register struct ship *ship, *to;
121char onlytemp;
717de9f0 122{
1eda8103
EW
123 char drift;
124 int row, col, dir, total, ran;
b3a57661 125 register struct File *fp = ship->file;
717de9f0 126
b3a57661
EW
127 if ((dir = fp->dir) == 0)
128 return 0;
129 row = fp->row;
130 col = fp->col;
131 drift = fp->drift;
132 move(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
133 if (!*movement)
134 (void) strcpy(movement, "d");
135
136 ran = range(ship, to);
137 total = -50 * ran;
138 if (ran < 4 && gunsbear(ship, to))
139 total += 60;
140 if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
141 total = -30000;
142
143 if (!onlytemp) {
144 fp->row = row;
145 fp->col = col;
146 fp->dir = dir;
147 }
148 return total;
149}
150
b3a57661
EW
151move(p, ship, dir, row, col, drift)
152register char *p;
153register struct ship *ship;
1eda8103
EW
154register char *dir;
155register short *row, *col;
156register char *drift;
b3a57661
EW
157{
158 int dist;
159 char moved = 0;
160
161 for (; *p; p++) {
162 switch (*p) {
163 case 'r':
164 if (++*dir == 9)
165 *dir = 1;
166 break;
167 case 'l':
168 if (--*dir == 0)
169 *dir = 8;
170 break;
171 case '1': case '2': case '3': case '4':
172 case '5': case '6': case '7':
173 moved++;
174 if (*dir % 2 == 0)
175 dist = dtab[*p - '0'];
176 else
177 dist = *p - '0';
178 *row -= dr[*dir] * dist;
179 *col -= dc[*dir] * dist;
180 break;
717de9f0 181 }
717de9f0 182 }
b3a57661 183 if (!moved) {
9d517234 184 if (windspeed != 0 && ++*drift > 2) {
b3a57661 185 if (ship->specs->class >= 3 && !snagged(ship)
acfebbf7 186 || (turn & 1) == 0) {
b3a57661
EW
187 *row -= dr[winddir];
188 *col -= dc[winddir];
189 }
190 }
191 } else
acfebbf7 192 *drift = 0;
717de9f0
CL
193}
194
b3a57661
EW
195try(command, temp, ma, ta, af, vma, dir, f, t, high, rakeme)
196register struct ship *f, *t;
197int ma, ta, af, *high, rakeme;
198char command[], temp[];
717de9f0
CL
199{
200 register int new, n;
201 char st[4];
b3a57661 202#define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
717de9f0
CL
203
204 if ((n = strend(temp)) < '1' || n > '9')
b3a57661
EW
205 for (n = 1; vma - n >= 0; n++) {
206 (void) sprintf(st, "%d", n);
207 (void) strcat(temp, st);
208 new = score(temp, f, t, rakeme);
209 if (new > *high && (!rakeme || rakeyou)) {
717de9f0 210 *high = new;
b3a57661 211 (void) strcpy(command, temp);
717de9f0 212 }
b3a57661
EW
213 try(command, temp, ma-n, ta, af, vma-n,
214 dir, f, t, high, rakeme);
717de9f0
CL
215 rmend(temp);
216 }
b3a57661
EW
217 if (ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r' || !strlen(temp)) {
218 (void) strcat(temp, "r");
219 new = score(temp, f, t, rakeme);
220 if (new > *high && (!rakeme || gunsbear(f, t) && !gunsbear(t, f))) {
717de9f0 221 *high = new;
b3a57661 222 (void) strcpy(command, temp);
717de9f0 223 }
b3a57661 224 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);
717de9f0
CL
225 rmend(temp);
226 }
b3a57661
EW
227 if ((ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r') || !strlen(temp)){
228 (void) strcat(temp, "l");
229 new = score(temp, f, t, rakeme);
230 if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
717de9f0 231 *high = new;
b3a57661 232 (void) strcpy(command, temp);
717de9f0 233 }
b3a57661 234 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);
717de9f0
CL
235 rmend(temp);
236 }
237}
9224751f
EW
238
239rmend(str)
240char *str;
241{
242 register char *p;
243
244 for (p = str; *p; p++)
245 ;
246 if (p != str)
247 *--p = 0;
248}