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