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