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