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