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