3
|
1 /* $Id: util.c,v 1.1.1.1 1997/12/06 05:41:31 darius Exp $ */
|
|
2
|
|
3 /*
|
|
4 * util.c
|
|
5 * added functionality to gettarget() - Bill Dyess 10/6/93
|
|
6 */
|
|
7 #include "copyright.h"
|
|
8
|
|
9 #include <stdio.h>
|
|
10 #include <math.h>
|
|
11 #include <signal.h>
|
|
12 #include <stdlib.h>
|
|
13 #include "Wlib.h"
|
|
14 #include "defs.h"
|
|
15 #include "struct.h"
|
|
16 #include "data.h"
|
|
17 #include "proto.h"
|
|
18 #include "gameconf.h"
|
|
19
|
|
20 #if 0
|
|
21 /*
|
|
22 ** Provide the angular distance between two angles.
|
|
23 */
|
|
24 angdist(x, y)
|
|
25 unsigned char x, y;
|
|
26 {
|
|
27 register unsigned char res;
|
|
28
|
|
29 if (x > y)
|
|
30 res = x - y;
|
|
31 else
|
|
32 res = y - x;
|
|
33 if (res > 128)
|
|
34 return (256 - (int) res);
|
|
35 return ((int) res);
|
|
36 }
|
|
37 #endif
|
|
38
|
|
39 double
|
|
40 hypot2(x, y)
|
|
41 double x, y;
|
|
42 {
|
|
43 return sqrt(x * x + y * y);
|
|
44 }
|
|
45
|
|
46 /*
|
|
47 * * Find the object nearest mouse. Returns a pointer to an * obtype
|
|
48 * structure. This is used for info and locking on. *
|
|
49 *
|
|
50 * Because we are never interested in it, this function will * never return
|
|
51 * your own ship as the target. *
|
|
52 *
|
|
53 * Finally, this only works on the two main windows
|
|
54 */
|
|
55
|
|
56 static struct obtype _target;
|
|
57
|
|
58 struct obtype *
|
|
59 gettarget(ww, x, y, targtype)
|
|
60 W_Window ww;
|
|
61 int x, y;
|
|
62 int targtype;
|
|
63 {
|
|
64 /* now can get the closest friendly/enemy player or planet. Use
|
|
65 TARG_FRIENDLY or TARG_ENEMY or'd with TARG_PLAYER or TARG_PLANET */
|
|
66 register int i;
|
|
67 register struct player *j;
|
|
68 register struct planet *k;
|
|
69 int g_x, g_y, friendly;
|
|
70 double dist, closedist;
|
|
71 int slotnum, width;
|
|
72
|
|
73 if (ww == mapw) {
|
|
74 register gwidth, offsetx, offsety;
|
|
75 if (blk_zoom) {
|
|
76 gwidth = blk_gwidth / 2;
|
|
77 offsetx = zoom_offset(me->p_x);
|
|
78 offsety = zoom_offset(me->p_y);
|
|
79 } else {
|
|
80 gwidth = blk_gwidth;
|
|
81 offsetx = 0;
|
|
82 offsety = 0;
|
|
83 }
|
|
84 g_x = x * (gwidth / WINSIDE) + offsetx;
|
|
85 g_y = y * (gwidth / WINSIDE) + offsety;
|
|
86
|
|
87 } else if (ww == playerw) {
|
|
88 if (targtype & TARG_PLAYER) {
|
|
89 W_TranslatePoints(playerw, &x, &y);
|
|
90 y -= 2;
|
|
91 if (y < 0)
|
|
92 y = 0;
|
|
93 if (*playerList == 0 || *playerList == ',') {
|
|
94 if (y > 15)
|
|
95 y = 15;
|
|
96 slotnum = y;
|
|
97 width = W_WindowWidth(ww);
|
|
98 if (x > width / 2)
|
|
99 slotnum += 16;
|
|
100 } else {
|
|
101 slotnum = y;
|
|
102 }
|
|
103 if (slot[slotnum] != -1 &&
|
|
104 (paradise || !(players[slot[slotnum]].p_flags & PFCLOAK))) {
|
|
105 /* don't show info for cloakers on Bronco servers */
|
|
106 _target.o_type = PLAYERTYPE;
|
|
107 _target.o_num = slot[slotnum];
|
|
108 return &_target;
|
|
109 }
|
|
110 return NULL; /* no target found */
|
|
111 } else {
|
|
112 g_x = me->p_x + ((x - WINSIDE / 2) * SCALE);
|
|
113 g_y = me->p_y + ((y - WINSIDE / 2) * SCALE);
|
|
114 }
|
|
115 } else if (ww) { /* tactical window */
|
|
116 g_x = me->p_x + ((x - WINSIDE / 2) * SCALE);
|
|
117 g_y = me->p_y + ((y - WINSIDE / 2) * SCALE);
|
|
118 } else {
|
|
119 g_x = x;
|
|
120 g_y = y;
|
|
121 }
|
|
122 closedist = blk_gwidth;
|
|
123
|
|
124 if (targtype & TARG_ASTRAL) {
|
|
125 for (i = 0, k = &planets[0]; i < nplanets; i++, k++) {
|
|
126 int ptype = 0;
|
|
127 friendly = friendlyPlanet(k);
|
|
128 if (friendly && (targtype & TARG_ENEMY))
|
|
129 continue;
|
|
130 if (!friendly && (targtype & TARG_FRIENDLY))
|
|
131 continue;
|
|
132 if (k->pl_owner != idx_to_mask(me->p_teami) && (targtype & TARG_TEAM))
|
|
133 continue;
|
|
134 switch (PL_TYPE(*k)) {
|
|
135 case PLPLANET:
|
|
136 ptype = TARG_PLANET;
|
|
137 break;
|
|
138 case PLSTAR:
|
|
139 ptype = TARG_STAR;
|
|
140 break;
|
|
141 case PLAST:
|
|
142 ptype = TARG_PLANET;
|
|
143 break;
|
|
144 case PLNEB:
|
|
145 ptype = TARG_NEBULA;
|
|
146 break;
|
|
147 case PLBHOLE:
|
|
148 ptype = TARG_BLACKHOLE;
|
|
149 break;
|
|
150 case PLPULSAR:
|
|
151 ptype = TARG_STAR;
|
|
152 break;
|
|
153 }
|
|
154 if (!(ptype & targtype))
|
|
155 continue;
|
|
156 dist = hypot((double) (g_x - k->pl_x), (double) (g_y - k->pl_y));
|
|
157 if (dist < closedist) {
|
|
158 _target.o_type = PLANETTYPE;
|
|
159 _target.o_num = i;
|
|
160 closedist = dist;
|
|
161 }
|
|
162 }
|
|
163 }
|
|
164 if (targtype & TARG_PLAYER) {
|
|
165 for (i = 0, j = &players[i]; i < nplayers; i++, j++) {
|
|
166 if (j->p_status != PALIVE)
|
|
167 continue;
|
|
168 if ((j->p_flags & PFCLOAK) && (!(targtype & TARG_CLOAK)) && j != me)
|
|
169 continue;
|
|
170 if (j == me && !(targtype & TARG_SELF))
|
|
171 continue;
|
|
172 friendly = friendlyPlayer(j);
|
|
173 if (friendly && (targtype & TARG_ENEMY))
|
|
174 continue;
|
|
175 if (!friendly && (targtype & TARG_FRIENDLY))
|
|
176 continue;
|
|
177 if (j->p_teami != me->p_teami && targtype & TARG_TEAM)
|
|
178 continue;
|
|
179 if (!(targtype & (isBase(j->p_ship->s_type)
|
|
180 ? TARG_BASE
|
|
181 : TARG_SHIP)))
|
|
182 continue;
|
|
183 dist = hypot((double) (g_x - j->p_x), (double) (g_y - j->p_y));
|
|
184 if (dist <= closedist) {
|
|
185 _target.o_type = PLAYERTYPE;
|
|
186 _target.o_num = i;
|
|
187 closedist = dist;
|
|
188 }
|
|
189 }
|
|
190 }
|
|
191 if (closedist == blk_gwidth) { /* Didn't get one. bad news */
|
|
192 _target.o_type = PLAYERTYPE;
|
|
193 _target.o_num = me->p_no; /* Return myself. Oh well... */
|
|
194 return (&_target);
|
|
195 } else {
|
|
196 return (&_target);
|
|
197 }
|
|
198 }
|
|
199
|
|
200 #ifdef hpux
|
|
201
|
|
202 srandom(foo)
|
|
203 int foo;
|
|
204 {
|
|
205 rand(foo);
|
|
206 }
|
|
207
|
|
208 random()
|
|
209 {
|
|
210 return (rand());
|
|
211 }
|
|
212
|
|
213 #include <time.h>
|
|
214 #include <sys/resource.h>
|
|
215
|
|
216 #include <sys/signal.h>
|
|
217
|
|
218 void (*
|
|
219 signal(sig, funct)) ()
|
|
220 int sig;
|
|
221 void (*funct) ();
|
|
222 {
|
|
223 struct sigvec vec, oldvec;
|
|
224
|
|
225 sigvector(sig, 0, &vec);
|
|
226 vec.sv_handler = funct;
|
|
227 sigvector(sig, &vec, (struct sigvec *) 0);
|
|
228 }
|
|
229 #endif /* hpux */
|
|
230
|
|
231
|
|
232 char *
|
|
233 team_bit_string(mask)
|
|
234 int mask;
|
|
235 {
|
|
236 static char visitorstring[16]; /* better not have more than 16 teams */
|
|
237 int i;
|
|
238
|
|
239 for (i = 0; i < number_of_teams; i++) {
|
|
240 visitorstring[i] = (mask & (1 << i)) ? teaminfo[i].letter : ' ';
|
|
241 }
|
|
242 visitorstring[i] = 0;
|
|
243 return visitorstring;
|
|
244 }
|
|
245
|
|
246 /* getTargetID returns an id stuct containing then name of the object, the
|
|
247 type of object, the number of the object, the
|
|
248 team the object belongs to, and . Used a lot in the macro code.
|
|
249 [BDyess] */
|
|
250
|
|
251 struct id *
|
|
252 getTargetID(ww, x, y, targtype)
|
|
253 W_Window ww;
|
|
254 int x, y;
|
|
255 int targtype;
|
|
256 {
|
|
257 struct obtype *target;
|
|
258 static struct id buf;
|
|
259 struct player *j;
|
|
260 struct planet *k;
|
|
261
|
|
262 target = gettarget(ww, x, y, targtype);
|
|
263 if (target->o_type == PLAYERTYPE) {
|
|
264 buf.type = PLAYERTYPE;
|
|
265 j = &players[target->o_num];
|
|
266 buf.name = j->p_name;
|
|
267 buf.number = target->o_num;
|
|
268 buf.team = j->p_teami;
|
|
269 buf.mapstring[0] = j->p_mapchars[0];
|
|
270 buf.mapstring[1] = j->p_mapchars[1];
|
|
271 buf.mapstring[2] = 0;
|
|
272 } else if (target->o_type == PLANETTYPE) {
|
|
273 buf.type = PLANETTYPE;
|
|
274 k = &planets[target->o_num];
|
|
275 buf.name = k->pl_name;
|
|
276 buf.number = target->o_num;
|
|
277 buf.team = mask_to_idx(k->pl_owner);
|
|
278 if (0 == strncmp(k->pl_name, "New ", 4)) {
|
|
279 strncpy(buf.mapstring, k->pl_name + 4, 3);
|
|
280 } else if (0 == strncmp(k->pl_name, "Planet ", 7)) {
|
|
281 strncpy(buf.mapstring, k->pl_name + 7, 3);
|
|
282 } else
|
|
283 strncpy(buf.mapstring, k->pl_name, 3);
|
|
284 buf.mapstring[3] = 0;
|
|
285 } else { /* tried to find something that doesn't exist */
|
|
286 return NULL;
|
|
287 }
|
|
288 return &buf;
|
|
289 }
|