]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/radar.qc
Merge branch 'terencehill/hud_cvar_exporter' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / panel / radar.qc
1 #include "radar.qh"
2
3 #include <client/autocvars.qh>
4 #include <client/defs.qh>
5 #include <client/miscfunctions.qh>
6 #include <common/ent_cs.qh>
7 #include <common/mapinfo.qh>
8 #include <client/mapvoting.qh>
9 #include <client/resources.qh>
10 #include <client/teamradar.qh>
11 #include <common/mutators/mutator/waypoints/all.qh>
12
13 // Radar (#6)
14
15 void HUD_Radar_Export(int fh)
16 {
17         // allow saving cvars that aesthetically change the panel into hud skin files
18         HUD_Write_Cvar("hud_panel_radar_foreground_alpha");
19         HUD_Write_Cvar("hud_panel_radar_rotation");
20         HUD_Write_Cvar("hud_panel_radar_zoommode");
21         HUD_Write_Cvar("hud_panel_radar_scale");
22         HUD_Write_Cvar("hud_panel_radar_maximized_scale");
23         HUD_Write_Cvar("hud_panel_radar_maximized_size");
24         HUD_Write_Cvar("hud_panel_radar_maximized_rotation");
25         HUD_Write_Cvar("hud_panel_radar_maximized_zoommode");
26 }
27
28 bool HUD_Radar_Clickable()
29 {
30         return hud_panel_radar_mouse && !hud_panel_radar_temp_hidden;
31 }
32
33 void HUD_Radar_Show_Maximized(bool doshow, bool clickable)
34 {
35         TC(bool, doshow);
36         hud_panel_radar_maximized = doshow;
37         hud_panel_radar_temp_hidden = 0;
38
39         if ( doshow )
40         {
41                 if (clickable)
42                 {
43                         hud_panel_radar_mouse = 1;
44
45                         // we must unset the player's buttons, as they aren't released elsewhere
46                         localcmd("-fire\n");
47                         localcmd("-fire2\n");
48                         localcmd("-use\n");
49                         localcmd("-hook\n");
50                         localcmd("-jump\n");
51                 }
52         }
53         else if ( hud_panel_radar_mouse )
54         {
55                 hud_panel_radar_mouse = 0;
56                 mouseClicked = 0;
57         }
58 }
59 void HUD_Radar_Hide_Maximized()
60 {
61         HUD_Radar_Show_Maximized(false,false);
62 }
63
64
65 float HUD_Radar_InputEvent(int bInputType, float nPrimary, float nSecondary)
66 {
67         TC(int, bInputType);
68         if(!hud_panel_radar_maximized || !hud_panel_radar_mouse ||
69                 autocvar__hud_configure || mv_active)
70                 return false;
71
72         if(bInputType == 3)
73         {
74                 mousepos_x = nPrimary;
75                 mousepos_y = nSecondary;
76                 return true;
77         }
78
79         if(nPrimary == K_MOUSE1)
80         {
81                 if(bInputType == 0) // key pressed
82                         mouseClicked |= S_MOUSE1;
83                 else if(bInputType == 1) // key released
84                         mouseClicked -= (mouseClicked & S_MOUSE1);
85         }
86         else if(nPrimary == K_MOUSE2)
87         {
88                 if(bInputType == 0) // key pressed
89                         mouseClicked |= S_MOUSE2;
90                 else if(bInputType == 1) // key released
91                         mouseClicked -= (mouseClicked & S_MOUSE2);
92         }
93         else if ( nPrimary == K_ESCAPE && bInputType == 0 )
94         {
95                 HUD_Radar_Hide_Maximized();
96         }
97         else
98         {
99                 // allow console/use binds to work without hiding the map
100                 string con_keys = strcat(findkeysforcommand("toggleconsole", 0), " ", findkeysforcommand("+use", 0)) ;
101                 int keys = tokenize(con_keys); // findkeysforcommand returns data for this
102                 int i;
103                 for (i = 0; i < keys; ++i)
104                 {
105                         if(nPrimary == stof(argv(i)))
106                                 return false;
107                 }
108
109                 if ( STAT(HEALTH) <= 0 )
110                 {
111                         // Show scoreboard
112                         if ( bInputType < 2 )
113                         {
114                                 con_keys = findkeysforcommand("+showscores", 0);
115                                 keys = tokenize(con_keys);
116                                 for (i = 0; i < keys; ++i)
117                                 {
118                                         if ( nPrimary == stof(argv(i)) )
119                                         {
120                                                 hud_panel_radar_temp_hidden = bInputType == 0;
121                                                 return false;
122                                         }
123                                 }
124                         }
125                 }
126                 else if ( bInputType == 0 )
127                         HUD_Radar_Hide_Maximized();
128
129                 return false;
130         }
131
132         return true;
133 }
134
135 void HUD_Radar_Mouse()
136 {
137         if ( !hud_panel_radar_mouse ) return;
138         if(mv_active) return;
139
140         if ( intermission )
141         {
142                 HUD_Radar_Hide_Maximized();
143                 return;
144         }
145
146         if(mouseClicked & S_MOUSE2)
147         {
148                 HUD_Radar_Hide_Maximized();
149                 return;
150         }
151
152         panel = HUD_PANEL(RADAR);
153         HUD_Panel_LoadCvars();
154
155
156         panel_size = autocvar_hud_panel_radar_maximized_size;
157         panel_size_x = bound(0.2, panel_size_x, 1) * vid_conwidth;
158         panel_size_y = bound(0.2, panel_size_y, 1) * vid_conheight;
159         panel_pos_x = (vid_conwidth - panel_size_x) / 2;
160         panel_pos_y = (vid_conheight - panel_size_y) / 2;
161
162         if(mouseClicked & S_MOUSE1)
163         {
164                 // click outside
165                 if ( mousepos_x < panel_pos_x || mousepos_x > panel_pos_x + panel_size_x ||
166                          mousepos_y < panel_pos_y || mousepos_y > panel_pos_y + panel_size_y )
167                 {
168                         HUD_Radar_Hide_Maximized();
169                         return;
170                 }
171                 vector pos = teamradar_texcoord_to_3dcoord(teamradar_2dcoord_to_texcoord(mousepos),view_origin_z);
172                 localcmd(sprintf("cmd ons_spawn %f %f %f",pos_x,pos_y,pos_z));
173
174                 HUD_Radar_Hide_Maximized();
175                 return;
176         }
177 }
178
179 void HUD_Radar()
180 {
181         if (!autocvar__hud_configure)
182         {
183                 if (hud_panel_radar_maximized)
184                 {
185                         if (!hud_draw_maximized) return;
186                 }
187                 else
188                 {
189                         if (autocvar_hud_panel_radar == 0) return;
190                         if (autocvar_hud_panel_radar != 2 && !teamplay) return;
191                         if(radar_panel_modified)
192                         {
193                                 panel.update_time = time; // forces reload of panel attributes
194                                 radar_panel_modified = false;
195                         }
196                 }
197         }
198
199         if ( hud_panel_radar_temp_hidden )
200                 return;
201
202         HUD_Panel_LoadCvars();
203
204         float f = 0;
205
206         if (hud_panel_radar_maximized && !autocvar__hud_configure)
207         {
208                 panel_size = autocvar_hud_panel_radar_maximized_size;
209                 panel_size.x = bound(0.2, panel_size.x, 1) * vid_conwidth;
210                 panel_size.y = bound(0.2, panel_size.y, 1) * vid_conheight;
211                 panel_pos.x = (vid_conwidth - panel_size.x) / 2;
212                 panel_pos.y = (vid_conheight - panel_size.y) / 2;
213
214                 string panel_bg;
215                 panel_bg = strcat(hud_skin_path, "/border_default"); // always use the default border when maximized
216                 if(precache_pic(panel_bg) == "")
217                         panel_bg = "gfx/hud/default/border_default"; // fallback
218                 if(!radar_panel_modified && panel_bg != panel.current_panel_bg)
219                         radar_panel_modified = true;
220                 strcpy(panel.current_panel_bg, panel_bg);
221
222                 switch(hud_panel_radar_maximized_zoommode)
223                 {
224                         default:
225                         case 0:
226                                 f = current_zoomfraction;
227                                 break;
228                         case 1:
229                                 f = 1 - current_zoomfraction;
230                                 break;
231                         case 2:
232                                 f = 0;
233                                 break;
234                         case 3:
235                                 f = 1;
236                                 break;
237                 }
238
239                 switch(hud_panel_radar_maximized_rotation)
240                 {
241                         case 0:
242                                 teamradar_angle = view_angles.y - 90;
243                                 break;
244                         default:
245                                 teamradar_angle = 90 * hud_panel_radar_maximized_rotation;
246                                 break;
247                 }
248         }
249         if (!hud_panel_radar_maximized && !autocvar__hud_configure)
250         {
251                 switch(hud_panel_radar_zoommode)
252                 {
253                         default:
254                         case 0:
255                                 f = current_zoomfraction;
256                                 break;
257                         case 1:
258                                 f = 1 - current_zoomfraction;
259                                 break;
260                         case 2:
261                                 f = 0;
262                                 break;
263                         case 3:
264                                 f = 1;
265                                 break;
266                 }
267
268                 switch(hud_panel_radar_rotation)
269                 {
270                         case 0:
271                                 teamradar_angle = view_angles.y - 90;
272                                 break;
273                         default:
274                                 teamradar_angle = 90 * hud_panel_radar_rotation;
275                                 break;
276                 }
277         }
278
279         vector pos, mySize;
280         pos = panel_pos;
281         mySize = panel_size;
282
283         if (autocvar_hud_panel_radar_dynamichud)
284                 HUD_Scale_Enable();
285         else
286                 HUD_Scale_Disable();
287         HUD_Panel_DrawBg();
288         if(panel_bg_padding)
289         {
290                 pos += '1 1 0' * panel_bg_padding;
291                 mySize -= '2 2 0' * panel_bg_padding;
292         }
293
294         int color2;
295         float scale2d, normalsize, bigsize;
296
297         teamradar_origin2d = HUD_Shift(pos + 0.5 * mySize);
298         teamradar_size2d = mySize;
299
300         if(minimapname == "")
301                 return;
302
303         teamradar_loadcvars();
304
305         scale2d = vlen_maxnorm2d(mi_picmax - mi_picmin);
306         teamradar_size2d = HUD_Scale(mySize);
307
308         teamradar_extraclip_mins = teamradar_extraclip_maxs = '0 0 0'; // we always center
309
310         // pixels per world qu to match the teamradar_size2d_x range in the longest dimension
311         if((hud_panel_radar_rotation == 0 && !hud_panel_radar_maximized) || (hud_panel_radar_maximized_rotation == 0 && hud_panel_radar_maximized))
312         {
313                 // max-min distance must fit the radar in any rotation
314                 bigsize = vlen_minnorm2d(teamradar_size2d) * scale2d / (1.05 * vlen(vec2(mi_scale)));
315         }
316         else
317         {
318                 vector c0, c1, c2, c3, span;
319                 c0 = Rotate(mi_min, teamradar_angle * DEG2RAD);
320                 c1 = Rotate(mi_max, teamradar_angle * DEG2RAD);
321                 c2 = Rotate('1 0 0' * mi_min.x + '0 1 0' * mi_max.y, teamradar_angle * DEG2RAD);
322                 c3 = Rotate('1 0 0' * mi_max.x + '0 1 0' * mi_min.y, teamradar_angle * DEG2RAD);
323                 span = '0 0 0';
324                 span.x = max(c0_x, c1_x, c2_x, c3_x) - min(c0_x, c1_x, c2_x, c3_x);
325                 span.y = max(c0_y, c1_y, c2_y, c3_y) - min(c0_y, c1_y, c2_y, c3_y);
326
327                 // max-min distance must fit the radar in x=x, y=y
328                 bigsize = min(
329                         teamradar_size2d.x * scale2d / (1.05 * span.x),
330                         teamradar_size2d.y * scale2d / (1.05 * span.y)
331                 );
332         }
333
334         normalsize = vlen_maxnorm2d(teamradar_size2d) * scale2d / hud_panel_radar_scale;
335         if(bigsize > normalsize)
336                 normalsize = bigsize;
337
338         teamradar_size =
339                   f * bigsize
340                 + (1 - f) * normalsize;
341         teamradar_origin3d_in_texcoord = teamradar_3dcoord_to_texcoord(
342                   f * mi_center
343                 + (1 - f) * view_origin);
344
345         drawsetcliparea(
346                 pos.x,
347                 pos.y,
348                 mySize.x,
349                 mySize.y
350         );
351
352         draw_teamradar_background(hud_panel_radar_foreground_alpha);
353
354         IL_EACH(g_radarlinks, true, draw_teamradar_link(it.origin, it.velocity, it.team));
355
356         IL_EACH(g_radaricons, it.teamradar_icon, {
357                 if ( hud_panel_radar_mouse )
358                 if ( GetResource(it, RES_HEALTH) >= 0 )
359                 if ( it.team == myteam + 1 || ISGAMETYPE(RACE) || !teamplay )
360                 {
361                         vector coord = teamradar_texcoord_to_2dcoord(teamradar_3dcoord_to_texcoord(it.origin));
362                         if(vdist((mousepos - coord), <, 8))
363                         {
364                                 vector brightcolor;
365                                 brightcolor_x = min(1,it.teamradar_color_x*1.5);
366                                 brightcolor_y = min(1,it.teamradar_color_y*1.5);
367                                 brightcolor_z = min(1,it.teamradar_color_z*1.5);
368                                 drawpic(coord - '8 8 0', "gfx/teamradar_icon_glow", '16 16 0', brightcolor, panel_fg_alpha, 0);
369                         }
370                 }
371                 entity icon = RadarIcons_from(it.teamradar_icon);
372                 draw_teamradar_icon(it.origin, icon, it, spritelookupcolor(it, icon.netname, it.teamradar_color), panel_fg_alpha);
373         });
374         AL_EACH(_entcs, e, it != NULL, {
375                 if (!it.m_entcs_private) continue;
376                 if (it.sv_entnum == current_player) continue;
377                 color2 = entcs_GetTeam(it.sv_entnum);
378                 draw_teamradar_player(it.origin, it.angles, Team_ColorRGB(color2));
379         });
380         draw_teamradar_player(entcs_receiver(current_player).origin, view_angles, '1 1 1');
381
382         drawresetcliparea();
383
384         if ( hud_panel_radar_mouse )
385         {
386                 string message = _("Click to select teleport destination");
387
388                 if ( STAT(HEALTH) <= 0 )
389                 {
390                         message = _("Click to select spawn location");
391                 }
392
393                 drawcolorcodedstring(pos + '0.5 0 0' * (mySize_x - stringwidth(message, true, hud_fontsize)) - '0 1 0' * hud_fontsize_y * 2,
394                                                          message, hud_fontsize, hud_panel_radar_foreground_alpha, DRAWFLAG_NORMAL);
395
396                 hud_panel_radar_bottom = pos_y + mySize_y + hud_fontsize_y;
397         }
398 }