Commit | Line | Data |
---|---|---|
b40b218a | 1 | #ifndef lint |
6c17b19a | 2 | static char *sccsid = "@(#)misc.c 1.3 83/10/10"; |
b40b218a CL |
3 | #endif |
4 | #include "externs.h" | |
5 | ||
6 | #define distance(x,y) (abs(x) >= abs(y) ? abs(x) + abs(y)/2 : abs(y) + abs(x)/2) | |
7 | ||
b3a57661 EW |
8 | /* XXX */ |
9 | range(from, to) | |
10 | struct ship *from, *to; | |
b40b218a | 11 | { |
b3a57661 EW |
12 | register bow1r, bow1c, bow2r, bow2c; |
13 | int stern1r, stern1c, stern2c, stern2r; | |
14 | register int bb, bs, sb, ss, result; | |
b40b218a | 15 | |
b3a57661 | 16 | if (!to->file->dir) |
6c17b19a | 17 | return -1; |
b3a57661 EW |
18 | stern1r = bow1r = from->file->row; |
19 | stern1c = bow1c = from->file->col; | |
20 | stern2r = bow2r = to->file->row; | |
21 | stern2c = bow2c = to->file->col; | |
22 | result = bb = distance(bow2r - bow1r, bow2c - bow1c); | |
23 | if (bb < 5) { | |
24 | stern2r += dr[to->file->dir]; | |
25 | stern2c += dc[to->file->dir]; | |
26 | stern1r += dr[from->file->dir]; | |
27 | stern1c += dc[from->file->dir]; | |
28 | bs = distance((bow2r - stern1r), (bow2c - stern1c)); | |
29 | sb = distance((bow1r - stern2r), (bow1c - stern2c)); | |
30 | ss = distance((stern2r - stern1r) ,(stern2c - stern1c)); | |
31 | result = min(bb, min(bs, min(sb, ss))); | |
b40b218a | 32 | } |
b3a57661 | 33 | return result; |
b40b218a CL |
34 | } |
35 | ||
b3a57661 EW |
36 | struct ship * |
37 | closestenemy(from, side, anyship) | |
38 | register struct ship *from; | |
39 | char side, anyship; | |
b40b218a | 40 | { |
b3a57661 EW |
41 | register struct ship *sp; |
42 | register char a; | |
43 | int olddist = 30000, dist; | |
44 | struct ship *closest = 0; | |
45 | ||
46 | a = capship(from)->nationality; | |
47 | foreachship(sp) { | |
48 | if (sp == from) | |
49 | continue; | |
50 | if (sp->file->dir == 0) | |
51 | continue; | |
52 | if (a == capship(sp)->nationality && !anyship) | |
53 | continue; | |
54 | if (side && gunsbear(from, sp) != side) | |
55 | continue; | |
56 | dist = range(from, sp); | |
57 | if (dist < olddist) { | |
58 | closest = sp; | |
59 | olddist = dist; | |
60 | } | |
b40b218a | 61 | } |
b3a57661 | 62 | return closest; |
b40b218a | 63 | } |
6c17b19a EW |
64 | |
65 | angle(dr, dc) | |
66 | register dr, dc; | |
67 | { | |
68 | register i; | |
69 | ||
70 | if (dc >= 0 && dr > 0) | |
71 | i = 0; | |
72 | else if (dr <= 0 && dc > 0) | |
73 | i = 2; | |
74 | else if (dc <= 0 && dr < 0) | |
75 | i = 4; | |
76 | else | |
77 | i = 6; | |
78 | dr = abs(dr); | |
79 | dc = abs(dc); | |
80 | if ((i == 0 || i == 4) && dc * 2.4 > dr) { | |
81 | i++; | |
82 | if (dc > dr * 2.4) | |
83 | i++; | |
84 | } else if ((i == 2 || i == 6) && dr * 2.4 > dc) { | |
85 | i++; | |
86 | if (dr > dc * 2.4) | |
87 | i++; | |
88 | } | |
89 | return i % 8 + 1; | |
90 | } | |
91 | ||
92 | gunsbear(from, to) /* checks for target bow or stern */ | |
93 | register struct ship *from, *to; | |
94 | { | |
95 | int Dr, Dc, i; | |
96 | register ang; | |
97 | ||
98 | Dr = from->file->row - to->file->row; | |
99 | Dc = to->file->col - from->file->col; | |
100 | for (i = 2; i; i--) { | |
101 | if ((ang = angle(Dr, Dc) - from->file->dir + 1) < 1) | |
102 | ang += 8; | |
103 | if (ang >= 2 && ang <= 4) | |
104 | return 'r'; | |
105 | if (ang >= 6 && ang <= 7) | |
106 | return 'l'; | |
107 | Dr += dr[to->file->dir]; | |
108 | Dc += dc[to->file->dir]; | |
109 | } | |
110 | return 0; | |
111 | } | |
112 | ||
113 | portside(from, on, quick) | |
114 | register struct ship *from, *on; | |
115 | int quick; /* returns true if fromship is */ | |
116 | { /* shooting at onship's starboard side */ | |
117 | register ang; | |
118 | register Dr, Dc; | |
119 | ||
120 | Dr = from->file->row - on->file->row; | |
121 | Dc = on->file->col - from->file->col; | |
122 | if (quick == -1) { | |
123 | Dr += dr[on->file->dir]; | |
124 | Dc += dc[on->file->dir]; | |
125 | } | |
126 | ang = angle(Dr, Dc); | |
127 | if (quick != 0) | |
128 | return ang; | |
129 | ang = (ang + 4 - on->file->dir - 1) % 8 + 1; | |
130 | return ang < 5; | |
131 | } | |
132 | ||
133 | rmend(str) | |
134 | char *str; | |
135 | { | |
136 | register char *p; | |
137 | ||
138 | for (p = str; *p; p++) | |
139 | ; | |
140 | if (p != str) | |
141 | *--p = 0; | |
142 | } | |
143 | ||
144 | colours(sp) | |
145 | register struct ship *sp; | |
146 | { | |
147 | register char flag; | |
148 | ||
149 | if (sp->file->struck) | |
150 | flag = '!'; | |
151 | if (sp->file->explode) | |
152 | flag = '#'; | |
153 | if (sp->file->sink) | |
154 | flag = '~'; | |
155 | if (sp->file->struck) | |
156 | return flag; | |
157 | flag = *countryname[capship(sp)->nationality]; | |
158 | return sp->file->FS ? flag : tolower(flag); | |
159 | } | |
160 | ||
161 | #ifdef notdef | |
162 | #define PI 3.1415926535 | |
163 | ||
164 | float contable[8] = | |
165 | { 1.5708, 0.7854, 0.0, -0.7854, -1.5708, -2.3562, -PI, 2.3562 }; | |
166 | ||
167 | int tantable[40] = { | |
168 | 0,100,197,291,381, | |
169 | 464,540,610,675,733, | |
170 | 785,833,876,915,951, | |
171 | 983,1012,1039,1064,1086, | |
172 | 1107,1126,1144,1161,1176, | |
173 | 1190,1204,1216,1227,1239, | |
174 | 1249,1259,1268,1277,1285, | |
175 | 1293,1300,1307,1313,1470 | |
176 | }; | |
177 | ||
178 | double | |
179 | arctan(y,x) | |
180 | int y,x; | |
181 | { | |
182 | int sx, sy; | |
183 | register int index; | |
184 | ||
185 | sy = y < 0 ? -1 : 1; | |
186 | sx = x < 0 ? -1 : 1; | |
187 | y *= sy; | |
188 | x *= sx; | |
189 | if (!x) | |
190 | return (double) PI/2 * sy; | |
191 | index = 10 * y / x + 0.5; | |
192 | if (index > 39) | |
193 | index = 39; | |
194 | return (double) | |
195 | sy * (sx < 0 ? PI : 0 + sx*((float)tantable[index]/1000)); | |
196 | } | |
197 | #endif |