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