]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/teamradar.qc
r6574 | tzork | 2009-04-23 14:56:15 +0200 (Thu, 23 Apr 2009) | 1 line
[divverent/nexuiz.git] / data / qcsrc / client / teamradar.qc
1 float teamradar_angle; // player yaw angle
2 vector teamradar_origin3d_in_texcoord; // player origin
3 vector teamradar_origin2d; // 2D origin
4 vector teamradar_size2d; // 2D size
5 float teamradar_size; // 2D scale factor
6 float cl_teamradar_scale; // window size = ...qu
7 float v_flipped;
8
9 float vlen2d(vector v)
10 {
11         return sqrt(v_x * v_x + v_y * v_y);
12 }
13
14 float vlen_maxnorm2d(vector v)
15 {
16         return max4(v_x, v_y, -v_x, -v_y);
17 }
18
19 float vlen_minnorm2d(vector v)
20 {
21         float a, b;
22         a = max(v_x, -v_x);
23         b = max(v_y, -v_y);
24         return min(a, b);
25         // return min(max(v_x, -v_x), max(v_y, -v_y)); // fteqcc sucks
26 }
27
28 vector teamradar_3dcoord_to_texcoord(vector in)
29 {
30         vector out;
31         out_x = (in_x - mi_picmin_x) / (mi_picmax_x - mi_picmin_x);
32         out_y = (in_y - mi_picmin_y) / (mi_picmax_y - mi_picmin_y);
33         out_z = 0;
34         return out;
35 }
36
37 vector teamradar_texcoord_to_2dcoord(vector in)
38 {
39         vector out;
40         in -= teamradar_origin3d_in_texcoord;
41
42         out = rotate(in, teamradar_angle * DEG2RAD);
43         out_y = - out_y; // screen space is reversed
44
45         out = out * teamradar_size;
46         if(v_flipped)
47                 out_x = -out_x;
48         out += teamradar_origin2d;
49         return out;
50 }
51
52 vector yinvert(vector v)
53 {
54         v_y = 1 - v_y;
55         return v;
56 }
57
58 void draw_teamradar_background(float bg, float fg)
59 {
60         float fga;
61         vector fgc;
62         if(bg > 0)
63         {
64                 R_BeginPolygon("", 0);
65                 R_PolygonVertex('1 0 0' * (teamradar_origin2d_x - teamradar_size2d_x * 0.5) + '0 1 0' * (teamradar_origin2d_y - teamradar_size2d_y * 0.5), '0 0 0', '0 0 0', bg);
66                 R_PolygonVertex('1 0 0' * (teamradar_origin2d_x + teamradar_size2d_x * 0.5) + '0 1 0' * (teamradar_origin2d_y - teamradar_size2d_y * 0.5), '0 0 0', '0 0 0', bg);
67                 R_PolygonVertex('1 0 0' * (teamradar_origin2d_x + teamradar_size2d_x * 0.5) + '0 1 0' * (teamradar_origin2d_y + teamradar_size2d_y * 0.5), '0 0 0', '0 0 0', bg);
68                 R_PolygonVertex('1 0 0' * (teamradar_origin2d_x - teamradar_size2d_x * 0.5) + '0 1 0' * (teamradar_origin2d_y + teamradar_size2d_y * 0.5), '0 0 0', '0 0 0', bg);
69                 R_EndPolygon();
70         }
71
72         if(fg > 0 && minimapname != "")
73         {
74                 if(csqc_flags & CSQC_FLAG_READPICTURE) // not 2.4.2
75                 {
76                         fga = 1;
77                         fgc = '1 1 1' * fg;
78                         R_BeginPolygon(minimapname, DRAWFLAG_SCREEN | DRAWFLAG_MIPMAP);
79                 }
80                 else
81                 {
82                         fga = fg;
83                         fgc = '1 1 1';
84                         R_BeginPolygon(minimapname, DRAWFLAG_ADDITIVE);
85                 }
86                 if(v_flipped)
87                 {
88                         R_PolygonVertex(teamradar_texcoord_to_2dcoord(mi_pictexcoord3), yinvert(mi_pictexcoord3), fgc, fga);
89                         R_PolygonVertex(teamradar_texcoord_to_2dcoord(mi_pictexcoord2), yinvert(mi_pictexcoord2), fgc, fga);
90                         R_PolygonVertex(teamradar_texcoord_to_2dcoord(mi_pictexcoord1), yinvert(mi_pictexcoord1), fgc, fga);
91                         R_PolygonVertex(teamradar_texcoord_to_2dcoord(mi_pictexcoord0), yinvert(mi_pictexcoord0), fgc, fga);
92                 }
93                 else
94                 {
95                         R_PolygonVertex(teamradar_texcoord_to_2dcoord(mi_pictexcoord0), yinvert(mi_pictexcoord0), fgc, fga);
96                         R_PolygonVertex(teamradar_texcoord_to_2dcoord(mi_pictexcoord1), yinvert(mi_pictexcoord1), fgc, fga);
97                         R_PolygonVertex(teamradar_texcoord_to_2dcoord(mi_pictexcoord2), yinvert(mi_pictexcoord2), fgc, fga);
98                         R_PolygonVertex(teamradar_texcoord_to_2dcoord(mi_pictexcoord3), yinvert(mi_pictexcoord3), fgc, fga);
99                 }
100                 R_EndPolygon();
101         }
102 }
103
104 void(vector coord3d, vector pangles, vector rgb) draw_teamradar_player =
105 {
106         vector coord, rgb2;
107
108         coord = teamradar_texcoord_to_2dcoord(teamradar_3dcoord_to_texcoord(coord3d));
109
110         makevectors(pangles - '0 1 0' * teamradar_angle);
111         if(v_flipped)
112         {
113                 v_forward_x = -v_forward_x;
114                 v_right_x = -v_right_x;
115                 v_up_x = -v_up_x;
116         }
117         v_forward_z = 0;
118         v_forward = normalize(v_forward);
119         v_forward_y *= -1.0;
120         v_right_x = -v_forward_y;
121         v_right_y = v_forward_x;
122
123         if(rgb == '1 1 1')
124                 rgb2 = '0 0 0';
125         else
126                 rgb2 = '1 1 1';
127
128         R_BeginPolygon("", 0);
129         R_PolygonVertex(coord+v_forward*3, '0 0 0', rgb2, 1);
130         R_PolygonVertex(coord+v_right*4-v_forward*2.5, '0 1 0', rgb2, 1);
131         R_PolygonVertex(coord-v_forward*2, '1 0 0', rgb2, 1);
132         R_PolygonVertex(coord-v_right*4-v_forward*2.5, '1 1 0', rgb2, 1);
133         R_EndPolygon();
134
135         R_BeginPolygon("", 0);
136         R_PolygonVertex(coord+v_forward*2, '0 0 0', rgb, 1);
137         R_PolygonVertex(coord+v_right*3-v_forward*2, '0 1 0', rgb, 1);
138         R_PolygonVertex(coord-v_forward, '1 0 0', rgb, 1);
139         R_PolygonVertex(coord-v_right*3-v_forward*2, '1 1 0', rgb, 1);
140         R_EndPolygon();
141 };
142
143 void draw_teamradar_icon(vector coord, float icon, entity pingdata, vector rgb, float a)
144 {
145         float dt;
146         vector v;
147         float i;
148
149         coord = teamradar_texcoord_to_2dcoord(teamradar_3dcoord_to_texcoord(coord));
150         drawpic(coord - '4 4 0', strcat("gfx/teamradar_icon_", ftos(icon)), '8 8 0', rgb, a, 0);
151
152         if(pingdata)
153         {
154                 for(i = 0; i < MAX_TEAMRADAR_TIMES; ++i)
155                 {
156                         dt = pingdata.(teamradar_times[i]);
157                         if(dt == 0)
158                                 continue;
159                         dt = time - dt;
160                         if(dt > 1)
161                                 continue;
162                         v = '2 2 0' * teamradar_size * dt;
163                         drawpic(coord - 0.5 * v, "gfx/teamradar_ping", v, '1 1 1', 1 - dt, DRAWFLAG_ADDITIVE);
164                 }
165         }
166 }
167
168 void draw_teamradar_link(vector start, vector end, float colors)
169 {
170         vector c0, c1, norm;
171
172         start = teamradar_texcoord_to_2dcoord(teamradar_3dcoord_to_texcoord(start));
173         end = teamradar_texcoord_to_2dcoord(teamradar_3dcoord_to_texcoord(end));
174         norm = normalize(start - end);
175         norm_z = norm_x;
176         norm_x = -norm_y;
177         norm_y = norm_z;
178         norm_z = 0;
179
180         c0 = colormapPaletteColor(colors & 0x0F, FALSE);
181         c1 = colormapPaletteColor((colors & 0xF0) / 0x10, FALSE);
182
183         R_BeginPolygon("", 0);
184         R_PolygonVertex(start - norm, '0 0 0', c0, 1);
185         R_PolygonVertex(start + norm, '0 1 0', c0, 1);
186         R_PolygonVertex(end + norm, '1 1 0', c1, 1);
187         R_PolygonVertex(end - norm, '1 0 0', c1, 1);
188         R_EndPolygon();
189 }
190
191 float cl_teamradar_scale;
192 float cl_teamradar_background_alpha;
193 float cl_teamradar_foreground_alpha;
194 float cl_teamradar_rotation;
195 vector cl_teamradar_size;
196 vector cl_teamradar_position;
197 float cl_teamradar_zoommode;
198
199 void teamradar_loadcvars()
200 {
201         v_flipped = cvar("v_flipped");
202         if(ons_showmap)
203         {
204                 cl_teamradar_scale = 42; // dummy, not used (see zoommode)
205                 cl_teamradar_background_alpha = 1;
206                 cl_teamradar_foreground_alpha = 1;
207                 cl_teamradar_rotation = cvar("cl_teamradar_rotation");
208                 if(!cl_teamradar_rotation)
209                         cl_teamradar_rotation = 4;
210                 cl_teamradar_size = '256 256 0'; // TODO make somewhat variable?
211                 cl_teamradar_position = '0.5 0.5 0';
212                 cl_teamradar_zoommode = 3;
213         }
214         else
215         {
216                 cl_teamradar_scale = cvar("cl_teamradar_scale");
217                 cl_teamradar_background_alpha = cvar("cl_teamradar_background_alpha");
218                 cl_teamradar_foreground_alpha = cvar("cl_teamradar_foreground_alpha");
219                 cl_teamradar_rotation = cvar("cl_teamradar_rotation");
220                 cl_teamradar_size = stov(cvar_string("cl_teamradar_size"));
221                 cl_teamradar_position = stov(cvar_string("cl_teamradar_position"));
222                 cl_teamradar_zoommode = cvar("cl_teamradar_zoommode");
223
224                 // others default to 0
225                 // match this to defaultNexuiz.cfg!
226                 if(!cl_teamradar_scale) cl_teamradar_scale = 4096;
227                 if(!cl_teamradar_background_alpha) cl_teamradar_background_alpha = 0.4;
228                 if(!cl_teamradar_foreground_alpha) cl_teamradar_foreground_alpha = 0.8;
229                 if(!cl_teamradar_size_x) cl_teamradar_size_x = 128;
230                 if(!cl_teamradar_size_y) cl_teamradar_size_y = cl_teamradar_size_x;
231
232                 cl_teamradar_size_z = 0;
233                 cl_teamradar_position_z = 0;
234         }
235 }
236
237 void() teamradar_view =
238 {
239         local float color, color2;
240         local vector rgb;
241         local entity tm;
242         float scale2d, normalsize, bigsize;
243         float f;
244
245         if(minimapname == "" && !ons_showmap)
246                 return;
247
248         teamradar_loadcvars();
249
250         switch(cl_teamradar_zoommode)
251         {
252                 default:
253                 case 0:
254                         f = current_zoomfraction;
255                         break;
256                 case 1:
257                         f = 1 - current_zoomfraction;
258                         break;
259                 case 2:
260                         f = 0;
261                         break;
262                 case 3:
263                         f = 1;
264                         break;
265         }
266
267         switch(cl_teamradar_rotation)
268         {
269                 case 0:
270                         teamradar_angle = view_angles_y - 90;
271                         break;
272                 default:
273                         teamradar_angle = 90 * cl_teamradar_rotation;
274                         break;
275         }
276
277         scale2d = vlen_maxnorm2d(mi_picmax - mi_picmin);
278         teamradar_size2d = cl_teamradar_size;
279         teamradar_origin2d =
280                   '1 0 0' * (0.5 * teamradar_size2d_x + cl_teamradar_position_x * (vid_conwidth - teamradar_size2d_x))
281                 + '0 1 0' * (0.5 * teamradar_size2d_y + cl_teamradar_position_y * (vid_conheight - teamradar_size2d_y));
282
283         // pixels per world qu to match the teamradar_size2d_x range in the longest dimension
284         if(cl_teamradar_rotation == 0)
285         {
286                 // max-min distance must fit the radar in any rotation
287                 bigsize = vlen_minnorm2d(teamradar_size2d) * scale2d / (1.05 * vlen2d(mi_max - mi_min));
288         }
289         else
290         {
291                 vector c0, c1, c2, c3, span;
292                 c0 = rotate(mi_min, teamradar_angle * DEG2RAD);
293                 c1 = rotate(mi_max, teamradar_angle * DEG2RAD);
294                 c2 = rotate('1 0 0' * mi_min_x + '0 1 0' * mi_max_y, teamradar_angle * DEG2RAD);
295                 c3 = rotate('1 0 0' * mi_max_x + '0 1 0' * mi_min_y, teamradar_angle * DEG2RAD);
296                 span = '0 0 0';
297                 span_x = max4(c0_x, c1_x, c2_x, c3_x) - min4(c0_x, c1_x, c2_x, c3_x);
298                 span_y = max4(c0_y, c1_y, c2_y, c3_y) - min4(c0_y, c1_y, c2_y, c3_y);
299
300                 // max-min distance must fit the radar in x=x, y=y
301                 bigsize = min(
302                         teamradar_size2d_x * scale2d / (1.05 * span_x),
303                         teamradar_size2d_y * scale2d / (1.05 * span_y)
304                 );
305         }
306
307         normalsize = vlen_maxnorm2d(teamradar_size2d) * scale2d / cl_teamradar_scale;
308         if(bigsize > normalsize)
309                 normalsize = bigsize;
310
311         teamradar_size =
312                   f * bigsize
313                 + (1 - f) * normalsize;
314         teamradar_origin3d_in_texcoord = teamradar_3dcoord_to_texcoord(
315                   f * (mi_min + mi_max) * 0.5
316                 + (1 - f) * view_origin);
317
318         color = GetPlayerColor(player_localentnum-1);
319         rgb = GetTeamRGB(color);
320
321         drawsetcliparea(
322                 teamradar_origin2d_x - teamradar_size2d_x * 0.5,
323                 teamradar_origin2d_y - teamradar_size2d_y * 0.5,
324                 teamradar_size2d_x,
325                 teamradar_size2d_y
326         );
327
328         draw_teamradar_background(cl_teamradar_background_alpha, cl_teamradar_foreground_alpha);
329
330         if(ons_showmap)
331         {
332                 drawresetcliparea();
333
334                 vector frame_origin, frame_size;
335                 frame_origin = frame_size = '0 0 0';
336
337                 frame_origin_x = teamradar_origin2d_x - teamradar_size2d_x * 0.55859375;
338                 frame_origin_y = teamradar_origin2d_y - teamradar_size2d_y * 0.55859375;
339                 frame_size_x = teamradar_size2d_x * 1.1171875;
340                 frame_size_y = teamradar_size2d_y * 1.1171875;
341                 drawpic(frame_origin, "gfx/ons-frame.tga", frame_size, '1 1 1', 1, 0);
342                 drawpic(frame_origin, "gfx/ons-frame-team.tga", frame_size, rgb, 1, 0);
343
344                 drawsetcliparea(
345                         teamradar_origin2d_x - teamradar_size2d_x * 0.5,
346                         teamradar_origin2d_y - teamradar_size2d_y * 0.5,
347                         teamradar_size2d_x,
348                         teamradar_size2d_y
349                 );
350         }
351
352         for(tm = world; (tm = find(tm, classname, "radarlink")); )
353                 draw_teamradar_link(tm.origin, tm.velocity, tm.team);
354         for(tm = world; (tm = findflags(tm, teamradar_icon, 0xFFFFFF)); )
355                 draw_teamradar_icon(tm.origin, tm.teamradar_icon, tm, tm.teamradar_color, tm.alpha);
356         for(tm = world; (tm = find(tm, classname, "entcs_receiver")); )
357         {
358                 color2 = GetPlayerColor(tm.sv_entnum);
359                 //if(color == COLOR_SPECTATOR || color == color2)
360                         draw_teamradar_player(tm.origin, tm.angles, GetTeamRGB(color2));
361         }
362         draw_teamradar_player(view_origin, view_angles, '1 1 1');
363
364         drawresetcliparea();
365 };
366
367
368
369 // radar links
370
371 void Ent_RadarLink()
372 {
373         float sendflags;
374         sendflags = ReadByte();
375
376         InterpolateOrigin_Undo();
377
378         self.iflags = IFLAG_VELOCITY;
379         self.classname = "radarlink";
380
381         if(sendflags & 1)
382         {
383                 self.origin_x = ReadCoord();
384                 self.origin_y = ReadCoord();
385                 self.origin_z = ReadCoord();
386         }
387
388         if(sendflags & 2)
389         {
390                 self.velocity_x = ReadCoord();
391                 self.velocity_y = ReadCoord();
392                 self.velocity_z = ReadCoord();
393         }
394
395         if(sendflags & 4)
396         {
397                 self.team = ReadByte();
398         }
399
400         InterpolateOrigin_Note();
401 }