]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/util.qc
New cvar menu_tootips
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / util.qc
1 float GL_CheckExtension(string ext)
2 {
3         return (strstrofs(strcat(" ", cvar_string("gl_info_extensions"), " "), strcat(" ", ext, " "), 0) >= 0);
4 }
5
6 float GL_Have_TextureCompression()
7 {
8         return (GL_CheckExtension("GL_EXT_texture_compression_s3tc") && GL_CheckExtension("GL_ARB_texture_compression"));
9 }
10
11 float tooltipdb;
12 void loadTooltips()
13 {
14         tooltipdb = db_load("tooltips.db");
15 }
16 void unloadTooltips()
17 {
18         db_close(tooltipdb);
19         tooltipdb = -1;
20 }
21 string getZonedTooltipForIdentifier(string s)
22 {
23         string t;
24         if(s == "" || tooltipdb < 0)
25                 return string_null;
26         t = db_get(tooltipdb, s);
27         if(t == "-")
28                 return string_null;
29         if(t != "")
30                 return strzone(t);
31         t = cvar_description(s);
32         if(t != "" && t != "custom cvar")
33                 return strzone(t);
34         dprint("WARNING: no tooltip set for ", s, "\n");
35         return string_null;
36 }
37
38 void forAllDescendants(entity root, void(entity, entity) funcPre, void(entity, entity) funcPost, entity pass)
39 {
40         depthfirst(root, parent, firstChild, nextSibling, funcPre, funcPost, pass);
41 }
42
43 .string cvarName;
44 void SUB_Null_ee(entity e1, entity e2)
45 {
46 }
47 void saveCvarsOf(entity ignore, entity e)
48 {
49         if(e.saveCvars)
50                 e.saveCvars(e);
51 }
52 void loadCvarsOf(entity ignore, entity e)
53 {
54         if(e.loadCvars)
55                 e.loadCvars(e);
56 }
57 void saveAllCvars(entity root)
58 {
59         forAllDescendants(root, saveCvarsOf, SUB_Null_ee, NULL);
60 }
61 void loadAllCvars(entity root)
62 {
63         forAllDescendants(root, loadCvarsOf, SUB_Null_ee, NULL);
64 }
65
66 .string cvarNames_Multi;
67 .void(entity me) saveCvars_Multi;
68 string getCvarsMulti(entity me)
69 {
70         if (me.cvarNames_Multi)
71                 return me.cvarNames_Multi;
72         return string_null;
73 }
74 void saveCvarsMulti(entity me)
75 {
76         float n, i;
77         string s;
78
79         me.saveCvars_Multi(me);
80         s = cvar_string(me.cvarName);
81
82         n = tokenize_console(me.cvarNames_Multi);
83         for(i = 0; i < n; ++i)
84                 cvar_set(argv(i), s);
85 }
86 void makeMulti(entity e, string otherCvars)
87 {
88         e.cvarNames_Multi = otherCvars;
89         e.saveCvars_Multi = e.saveCvars;
90         e.saveCvars = saveCvarsMulti;
91 }
92
93 .void(entity me) saveCvars_Callback;
94 .entity saveCvars_Callback_ent;
95 .void(entity me, entity cb) saveCvars_Callback_func;
96 void saveCvarsCallback(entity me)
97 {
98         me.saveCvars_Callback(me);
99         me.saveCvars_Callback_func(me.saveCvars_Callback_ent, me);
100 }
101 void makeCallback(entity e, entity cbent, void(entity, entity) cbfunc)
102 {
103         e.saveCvars_Callback = e.saveCvars;
104         e.saveCvars = saveCvarsCallback;
105         e.saveCvars_Callback_ent = cbent;
106         e.saveCvars_Callback_func = cbfunc;
107 }
108
109 .void(entity) draw_setDependent;
110 .string cvar_setDependent;
111 .float cvarMin_setDependent;
112 .float cvarMax_setDependent;
113 .string cvar2_setDependent;
114 .float cvar2Min_setDependent;
115 .float cvar2Max_setDependent;
116 .string cvar3_setDependent;
117 .float cvar3Min_setDependent;
118 .float cvar3Max_setDependent;
119 .float op_setDependent;
120 .string cvarString_setDependent;
121 .string cvarValue_setDependent;
122 .float(entity) func_setDependent;
123 void setDependent_Check(entity e)
124 {
125         float f;
126         string s;
127         if(e.func_setDependent)
128         {
129                 e.disabled = !(e.func_setDependent(e));
130         }
131         else if(e.cvarString_setDependent)
132         {
133                 s = cvar_string(e.cvarString_setDependent);
134                 e.disabled = (cvar_string(e.cvarString_setDependent) == e.cvarValue_setDependent);
135         }
136         else
137         {
138                 if(e.cvar_setDependent)
139                 {
140                         f = cvar(e.cvar_setDependent);
141                         if(e.cvarMin_setDependent <= e.cvarMax_setDependent)
142                                 e.disabled = ((f < e.cvarMin_setDependent) || (f > e.cvarMax_setDependent));
143                         else
144                                 e.disabled = ((f >= e.cvarMax_setDependent) && (f <= e.cvarMin_setDependent));
145                 }
146                 if(e.cvar2_setDependent)
147                 {
148                         f = cvar(e.cvar2_setDependent);
149                         if(e.cvar2Min_setDependent <= e.cvar2Max_setDependent)
150                                 e.disabled = (e.disabled + ((f < e.cvar2Min_setDependent) || (f > e.cvar2Max_setDependent)) > e.op_setDependent);
151                         else
152                                 e.disabled = (e.disabled + ((f >= e.cvar2Max_setDependent) && (f <= e.cvar2Min_setDependent)) > e.op_setDependent);
153                 }
154                 if(e.cvar3_setDependent)
155                 {
156                         f = cvar(e.cvar3_setDependent);
157                         if(e.cvar3Min_setDependent <= e.cvar3Max_setDependent)
158                                 e.disabled = (e.disabled + ((f < e.cvar3Min_setDependent) || (f > e.cvar3Max_setDependent)) > e.op_setDependent);
159                         else
160                                 e.disabled = (e.disabled + ((f >= e.cvar3Max_setDependent) && (f <= e.cvar3Min_setDependent)) > e.op_setDependent);
161                 }
162         }
163 }
164 void setDependent_Draw(entity e)
165 {
166         setDependent_Check(e);
167         e.draw_setDependent(e);
168 }
169 void setDependent(entity e, string theCvarName, float theCvarMin, float theCvarMax)
170 {
171         e.draw_setDependent = e.draw;
172         e.cvar_setDependent = theCvarName;
173         e.cvarMin_setDependent = theCvarMin;
174         e.cvarMax_setDependent = theCvarMax;
175         e.cvar2_setDependent = string_null;
176         e.cvar3_setDependent = string_null;
177         e.func_setDependent = func_null;
178         e.draw = setDependent_Draw;
179         setDependent_Check(e);
180 }
181 void setDependentStringNotEqual(entity e, string theCvarName, string theCvarValue)
182 {
183         e.draw_setDependent = e.draw;
184         e.cvarString_setDependent = theCvarName;
185         e.cvarValue_setDependent = theCvarValue;
186         e.cvar_setDependent = string_null;
187         e.cvar2_setDependent = string_null;
188         e.cvar3_setDependent = string_null;
189         e.func_setDependent = func_null;
190         e.draw = setDependent_Draw;
191         setDependent_Check(e);
192 }
193 void setDependentAND(entity e, string theCvarName, float theCvarMin, float theCvarMax, string theCvar2Name, float theCvar2Min, float theCvar2Max)
194 {
195         e.draw_setDependent = e.draw;
196         e.cvar_setDependent = theCvarName;
197         e.cvarMin_setDependent = theCvarMin;
198         e.cvarMax_setDependent = theCvarMax;
199         e.cvar2_setDependent = theCvar2Name;
200         e.cvar2Min_setDependent = theCvar2Min;
201         e.cvar2Max_setDependent = theCvar2Max;
202         e.cvar3_setDependent = string_null;
203         e.op_setDependent = 0;
204         e.func_setDependent = func_null;
205         e.draw = setDependent_Draw;
206         setDependent_Check(e);
207 }
208 void setDependentOR(entity e, string theCvarName, float theCvarMin, float theCvarMax, string theCvar2Name, float theCvar2Min, float theCvar2Max)
209 {
210         e.draw_setDependent = e.draw;
211         e.cvar_setDependent = theCvarName;
212         e.cvarMin_setDependent = theCvarMin;
213         e.cvarMax_setDependent = theCvarMax;
214         e.cvar2_setDependent = theCvar2Name;
215         e.cvar2Min_setDependent = theCvar2Min;
216         e.cvar2Max_setDependent = theCvar2Max;
217         e.cvar3_setDependent = string_null;
218         e.op_setDependent = 1;
219         e.func_setDependent = func_null;
220         e.draw = setDependent_Draw;
221         setDependent_Check(e);
222 }
223 void setDependentAND3(entity e, string theCvarName, float theCvarMin, float theCvarMax, string theCvar2Name, float theCvar2Min, float theCvar2Max, string theCvar3Name, float theCvar3Min, float theCvar3Max)
224 {
225         e.draw_setDependent = e.draw;
226         e.cvar_setDependent = theCvarName;
227         e.cvarMin_setDependent = theCvarMin;
228         e.cvarMax_setDependent = theCvarMax;
229         e.cvar2_setDependent = theCvar2Name;
230         e.cvar2Min_setDependent = theCvar2Min;
231         e.cvar2Max_setDependent = theCvar2Max;
232         e.cvar3_setDependent = theCvar3Name;
233         e.cvar3Min_setDependent = theCvar3Min;
234         e.cvar3Max_setDependent = theCvar3Max;
235         e.op_setDependent = 0;
236         e.func_setDependent = func_null;
237         e.draw = setDependent_Draw;
238         setDependent_Check(e);
239 }
240 void setDependentWeird(entity e, float(entity) func)
241 {
242         e.draw_setDependent = e.draw;
243         e.func_setDependent = func;
244         e.draw = setDependent_Draw;
245         setDependent_Check(e);
246 }
247
248 // URI SYSTEM ////////////////////////////////////////////////////////
249
250 float _Nex_ExtResponseSystem_Queried;
251 string _Nex_ExtResponseSystem_UpdateTo;
252
253 void URI_Get_Callback(float id, float status, string data)
254 {
255         if (id == URI_GET_DISCARD)
256         {
257                 // discard
258         }
259         else if(id == URI_GET_UPDATENOTIFICATION)
260         {
261                 UpdateNotification_URI_Get_Callback(id, status, data);
262         }
263         else if(id >= URI_GET_CURL && id <= URI_GET_CURL_END)
264         {
265                 Curl_URI_Get_Callback(id, status, data);
266         }
267         else
268         {
269                 print("Received HTTP request data for an invalid id ", ftos(id), ".\n");
270         }
271 }
272
273 void UpdateNotification_URI_Get_Callback(float id, float status, string data)
274 {
275         float n;
276
277         if(_Nex_ExtResponseSystem_UpdateTo)
278         {
279                 print("error: UpdateNotification_URI_Get_Callback has been called before\n");
280                 return;
281         }
282         if(status != 0)
283         {
284                 print(sprintf("error receiving update notification: status is %d\n", status));
285                 return;
286         }
287         if(substring(data, 0, 1) == "<")
288         {
289                 print("error: received HTML instead of an update notification\n");
290                 return;
291         }
292         if(strstrofs(data, "\r", 0) != -1)
293         {
294                 print("error: received carriage returns from update notification server\n");
295                 return;
296         }
297
298         if(data == "")
299                 n = 0;
300         else
301                 n = tokenizebyseparator(data, "\n");
302         
303         if(n >= 1)
304         {
305                 _Nex_ExtResponseSystem_UpdateTo = argv(0);
306
307                 if(vercmp(cvar_string("g_xonoticversion"), _Nex_ExtResponseSystem_UpdateTo) >= 0)
308                 {
309                         _Nex_ExtResponseSystem_UpdateTo = ""; // no update needed
310                 }
311                 else
312                 {
313                         // update needed
314                         if(n >= 2)
315                                 print(sprintf("Update can be downloaded at:\n%s\n", argv(1)));
316                 }
317
318                 _Nex_ExtResponseSystem_UpdateTo = strzone(_Nex_ExtResponseSystem_UpdateTo);
319         }
320 }
321
322 // END OF URI SYSTEM ////////////////////////////////////////////////////////
323
324 float preMenuInit()
325 {
326         vector sz;
327         vector boxA, boxB;
328
329         MapInfo_Cache_Create();
330         MapInfo_Enumerate();
331         if(!MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, 0, 0, 1))
332         {
333                 draw_reset_cropped();
334
335                 sz = eX * 0.025 + eY * 0.025 * (draw_scale_x / draw_scale_y);
336                 draw_CenterText('0.5 0.5 0' - 1.25 * sz_y * eY, "Autogenerating mapinfo for newly added maps...", sz, '1 1 1', 1, 0);
337
338                 boxA = '0.05 0.5 0' + 0.25 * sz_y * eY;
339                 boxB = '0.95 0.5 0' + 1.25 * sz_y * eY;
340                 draw_Fill(boxA, boxB - boxA, '1 1 1', 1);
341                 
342                 boxA += sz * 0.1;
343                 boxB -= sz * 0.1;
344                 draw_Fill(boxA, boxB - boxA, '0.1 0.1 0.1', 1);
345
346                 boxB_x = boxA_x * (1 - MapInfo_progress) + boxB_x * MapInfo_progress;
347                 draw_Fill(boxA, boxB - boxA, '0 0 1', 1);
348
349                 return FALSE;
350         }
351         return TRUE;
352 }
353
354 string campaign_name_previous;
355 float campaign_won_previous;
356 #ifdef WATERMARK
357 var string autocvar_menu_watermark = WATERMARK();
358 #else
359 var string autocvar_menu_watermark = "";
360 #endif
361 void postMenuDraw()
362 {
363         if(autocvar_menu_watermark != "")
364         {
365                 vector fs = '48 48 0';
366                 draw_CenterText('0.5 0.1 0', autocvar_menu_watermark, globalToBoxSize('32 32 0', draw_scale), '1 1 1', 0.05, 1);
367         }
368 }
369 void preMenuDraw()
370 {
371         vector fs, sz, line, mid;
372
373         if(cvar("menu_updatecheck"))
374         {
375                 if(!_Nex_ExtResponseSystem_Queried)
376                 {
377                         _Nex_ExtResponseSystem_Queried = 1;
378                         float startcnt;
379                         string uri;
380
381                         cvar_set("cl_startcount", ftos(startcnt = cvar("cl_startcount") + 1));
382
383                         // for privacy, munge the start count a little
384                         startcnt = floor((floor(startcnt / 10) + random()) * 10);
385                         uri = sprintf("http://www.xonotic.org/dl/checkupdate.txt?version=%s&cnt=%d", uri_escape(cvar_string("g_xonoticversion")), startcnt);
386
387 #ifdef CVAR_POPCON
388                         float cvar_handle, popcon_handle;
389                         float n, i, j;
390                         string k, s;
391                         cvar_handle = buf_create();
392                         buf_cvarlist(cvar_handle, "", "");
393                         n = buf_getsize(cvar_handle);
394                         popcon_handle = buf_create();
395                         for(i= 0, j = 0; i < n; ++i)
396                         {
397                                 k = bufstr_get(cvar_handle, i);
398                                 if(!(cvar_type(k) & CVAR_TYPEFLAG_SAVED))
399                                         continue;
400                                 s = sprintf("%s=%d", uri_escape(k), cvar_string(k) != cvar_defstring(k));
401                                 bufstr_set(popcon_handle, j, s);
402                                 ++j;
403                         }
404                         buf_del(cvar_handle);
405                         uri_postbuf(
406                                 uri, URI_GET_UPDATENOTIFICATION,
407                                 "application/x-www-form-urlencoded",
408                                 "&",
409                                 popcon_handle
410                         );
411                         buf_del(popcon_handle);
412 #else
413                         uri_get(uri, URI_GET_UPDATENOTIFICATION);
414 #endif
415                 }
416         }
417
418         if(_Nex_ExtResponseSystem_UpdateTo != "")
419         {
420                 // TODO rather turn this into a dialog
421                 fs = ((1/draw_scale_x) * eX + (1/draw_scale_y) * eY) * 12;
422                 line = eY * fs_y;
423                 sz_x = draw_TextWidth("  http://www.xonotic.com/  ", 0, fs);
424                 sz_y = 3 * fs_y;
425
426                 draw_alpha = sin(time * 0.112 - 0.3) * 10;
427                 mid = eX * (0.5 + 0.5 * (1 - sz_x) * cos(time * 0.071))
428                     + eY * (0.5 + 0.5 * (1 - sz_y) * sin(time * 0.071));
429
430                 draw_Fill(mid - 0.5 * sz, sz, '1 1 0', 1);
431                 draw_CenterText(mid - 1 * line, strcat("Update to ", _Nex_ExtResponseSystem_UpdateTo, " now!"), fs, '1 0 0', 1, 0);
432                 draw_CenterText(mid - 0 * line, "http://www.xonotic.org/", fs, '0 0 1', 1, 0);
433         }
434         if not(campaign_name_previous)
435                 campaign_name_previous = strzone(strcat(campaign_name, "x")); // force unequal
436         if(campaign_name == campaign_name_previous)
437         {
438                 if(cvar(strcat("g_campaign", campaign_name, "_won")))
439                 {
440                         if(!campaign_won_previous)
441                         {
442                                 m_display();
443                                 DialogOpenButton_Click_withCoords(NULL, main.winnerDialog, '0 0 0', eX * conwidth + eY * conheight);
444                         }
445                         campaign_won_previous = 1;
446                 }
447                 else
448                         campaign_won_previous = 0;
449         }
450         else
451         {
452                 strunzone(campaign_name_previous);
453                 campaign_name_previous = strzone(campaign_name);
454                 campaign_won_previous = cvar(strcat("g_campaign", campaign_name, "_won"));
455         }
456 }
457
458 string resolvemod(string m)
459 {
460         if(m == "=")
461                 return getcurrentmod();
462         else
463                 return m;
464 }
465
466 string HUD_Panel_GetSettingName(float theSetting)
467 {
468         switch(theSetting) {
469                 case HUD_MENU_ENABLE: return ""; break;
470                 default: return "";
471         }
472 }
473
474 float updateCompression()
475 {
476         float fh;
477         float have_dds, have_jpg, have_tga;
478         float can_dds;
479         if((have_dds = ((fh = fopen("dds/particles/particlefont.dds", FILE_READ)) >= 0)))
480                 fclose(fh);
481         if((have_jpg = ((fh = fopen("particles/particlefont.jpg", FILE_READ)) >= 0)))
482                 fclose(fh);
483         if((have_tga = ((fh = fopen("particles/particlefont.tga", FILE_READ)) >= 0)))
484                 fclose(fh);
485         can_dds = GL_Have_TextureCompression();
486         if(have_dds && (have_jpg || have_tga))
487         {
488                 // both? Let's only use good quality precompressed files
489                 // but ONLY if we actually support it!
490                 if(can_dds)
491                 {
492                         cvar_set("gl_texturecompression", "0");
493                         return 1;
494                 }
495                 else
496                 {
497                         cvar_set("gl_texturecompression", "0");
498                         cvar_set("r_texture_dds_load", "0");
499                         return 0;
500                 }
501         }
502         else if(have_dds)
503         {
504                 // DDS only? We probably always want texture compression
505                 cvar_set("gl_texturecompression", "1");
506                 cvar_set("r_texture_dds_load", "1");
507                 if(!can_dds)
508                         print("^1ERROR: Texture compression is required but not supported.\n^1Expect visual problems.\n");
509                 return 0;
510         }
511         else
512         {
513                 // TGA only? Allow runtime compression
514                 if(can_dds)
515                 {
516                         cvar_set("gl_texturecompression", cvar_string("r_texture_dds_load"));
517                         return 2;
518                 }
519                 else
520                 {
521                         cvar_set("gl_texturecompression", "0");
522                         cvar_set("r_texture_dds_load", "0");
523                         return 0;
524                 }
525         }
526 }