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