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