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