- add a "frustrating" flag to race maps in mapinfo so they are not playable by default
[divverent/nexuiz.git] / data / qcsrc / menu / nexuiz / util.qc
1 float tooltipdb;
2 void loadTooltips()
3 {
4         tooltipdb = db_load("tooltips.db");
5 }
6 void unloadTooltips()
7 {
8         db_close(tooltipdb);
9         tooltipdb = -1;
10 }
11 string getZonedTooltipForIdentifier(string s)
12 {
13         string t;
14         if(s == "")
15                 return string_null;
16         t = db_get(tooltipdb, s);
17         if(t == "-")
18                 return string_null;
19         if(t != "")
20                 return strzone(t);
21         t = cvar_description(s);
22         if(t != "" && t != "custom cvar")
23                 return strzone(t);
24         dprint("WARNING: no tooltip set for ", s, "\n");
25         return string_null;
26 }
27
28 void forAllDescendants(entity root, void(entity, entity) funcPre, void(entity, entity) funcPost, entity pass)
29 {
30         depthfirst(root, parent, firstChild, nextSibling, funcPre, funcPost, pass);
31 }
32
33 .string cvarName;
34 void SUB_Null_ee(entity e1, entity e2)
35 {
36 }
37 void saveCvarsOf(entity ignore, entity e)
38 {
39         if(e.saveCvars)
40                 e.saveCvars(e);
41 }
42 void loadCvarsOf(entity ignore, entity e)
43 {
44         if(e.loadCvars)
45                 e.loadCvars(e);
46 }
47 void saveAllCvars(entity root)
48 {
49         forAllDescendants(root, saveCvarsOf, SUB_Null_ee, NULL);
50 }
51 void loadAllCvars(entity root)
52 {
53         forAllDescendants(root, loadCvarsOf, SUB_Null_ee, NULL);
54 }
55
56 .string cvarNames_Multi;
57 .void(entity me) saveCvars_Multi;
58 void saveCvarsMulti(entity me)
59 {
60         float n, i;
61         string s;
62
63         me.saveCvars_Multi(me);
64         s = cvar_string(me.cvarName);
65
66         n = tokenize_console(me.cvarNames_Multi);
67         for(i = 0; i < n; ++i)
68                 cvar_set(argv(i), s);
69 }
70 void makeMulti(entity e, string otherCvars)
71 {
72         e.cvarNames_Multi = otherCvars;
73         e.saveCvars_Multi = e.saveCvars;
74         e.saveCvars = saveCvarsMulti;
75 }
76
77 .void(entity me) saveCvars_Callback;
78 .entity saveCvars_Callback_ent;
79 .void(entity me, entity cb) saveCvars_Callback_func;
80 void saveCvarsCallback(entity me)
81 {
82         me.saveCvars_Callback(me);
83         me.saveCvars_Callback_func(me.saveCvars_Callback_ent, me);
84 }
85 void makeCallback(entity e, entity cbent, void(entity, entity) cbfunc)
86 {
87         e.saveCvars_Callback = e.saveCvars;
88         e.saveCvars = saveCvarsCallback;
89         e.saveCvars_Callback_ent = cbent;
90         e.saveCvars_Callback_func = cbfunc;
91 }
92
93 .void(entity) draw_setDependent;
94 .string cvar_setDependent;
95 .float cvarMin_setDependent;
96 .float cvarMax_setDependent;
97 .string cvar2_setDependent;
98 .float cvar2Min_setDependent;
99 .float cvar2Max_setDependent;
100 .string cvar3_setDependent;
101 .float cvar3Min_setDependent;
102 .float cvar3Max_setDependent;
103 .float op_setDependent;
104 .string cvarString_setDependent;
105 .string cvarValue_setDependent;
106 void setDependent_Check(entity e)
107 {
108         float f;
109         string s;
110         if(e.cvarString_setDependent)
111         {
112                 s = cvar_string(e.cvarString_setDependent);
113                 e.disabled = (cvar_string(e.cvarString_setDependent) == e.cvarValue_setDependent);
114         }
115         else
116         {
117                 if(e.cvar_setDependent)
118                 {
119                         f = cvar(e.cvar_setDependent);
120                         if(e.cvarMin_setDependent <= e.cvarMax_setDependent)
121                                 e.disabled = ((f < e.cvarMin_setDependent) || (f > e.cvarMax_setDependent));
122                         else
123                                 e.disabled = ((f >= e.cvarMax_setDependent) && (f <= e.cvarMin_setDependent));
124                 }
125                 if(e.cvar2_setDependent)
126                 {
127                         f = cvar(e.cvar2_setDependent);
128                         if(e.cvar2Min_setDependent <= e.cvar2Max_setDependent)
129                                 e.disabled = (e.disabled + ((f < e.cvar2Min_setDependent) || (f > e.cvar2Max_setDependent)) > e.op_setDependent);
130                         else
131                                 e.disabled = (e.disabled + ((f >= e.cvar2Max_setDependent) && (f <= e.cvar2Min_setDependent)) > e.op_setDependent);
132                 }
133                 if(e.cvar3_setDependent)
134                 {
135                         f = cvar(e.cvar3_setDependent);
136                         if(e.cvar3Min_setDependent <= e.cvar3Max_setDependent)
137                                 e.disabled = (e.disabled + ((f < e.cvar3Min_setDependent) || (f > e.cvar3Max_setDependent)) > e.op_setDependent);
138                         else
139                                 e.disabled = (e.disabled + ((f >= e.cvar3Max_setDependent) && (f <= e.cvar3Min_setDependent)) > e.op_setDependent);
140                 }
141         }
142 }
143 void setDependent_Draw(entity e)
144 {
145         setDependent_Check(e);
146         e.draw_setDependent(e);
147 }
148 void setDependent(entity e, string theCvarName, float theCvarMin, float theCvarMax)
149 {
150         e.draw_setDependent = e.draw;
151         e.cvar_setDependent = theCvarName;
152         e.cvarMin_setDependent = theCvarMin;
153         e.cvarMax_setDependent = theCvarMax;
154         e.cvar2_setDependent = string_null;
155         e.cvar3_setDependent = string_null;
156         e.draw = setDependent_Draw;
157         setDependent_Check(e);
158 }
159 void setDependentStringNotEqual(entity e, string theCvarName, string theCvarValue)
160 {
161         e.draw_setDependent = e.draw;
162         e.cvarString_setDependent = theCvarName;
163         e.cvarValue_setDependent = theCvarValue;
164         e.cvar_setDependent = string_null;
165         e.cvar2_setDependent = string_null;
166         e.cvar3_setDependent = string_null;
167         e.draw = setDependent_Draw;
168         setDependent_Check(e);
169 }
170 void setDependentAND(entity e, string theCvarName, float theCvarMin, float theCvarMax, string theCvar2Name, float theCvar2Min, float theCvar2Max)
171 {
172         e.draw_setDependent = e.draw;
173         e.cvar_setDependent = theCvarName;
174         e.cvarMin_setDependent = theCvarMin;
175         e.cvarMax_setDependent = theCvarMax;
176         e.cvar2_setDependent = theCvar2Name;
177         e.cvar2Min_setDependent = theCvar2Min;
178         e.cvar2Max_setDependent = theCvar2Max;
179         e.cvar3_setDependent = string_null;
180         e.op_setDependent = 0;
181         e.draw = setDependent_Draw;
182         setDependent_Check(e);
183 }
184 void setDependentOR(entity e, string theCvarName, float theCvarMin, float theCvarMax, string theCvar2Name, float theCvar2Min, float theCvar2Max)
185 {
186         e.draw_setDependent = e.draw;
187         e.cvar_setDependent = theCvarName;
188         e.cvarMin_setDependent = theCvarMin;
189         e.cvarMax_setDependent = theCvarMax;
190         e.cvar2_setDependent = theCvar2Name;
191         e.cvar2Min_setDependent = theCvar2Min;
192         e.cvar2Max_setDependent = theCvar2Max;
193         e.cvar3_setDependent = string_null;
194         e.op_setDependent = 1;
195         e.draw = setDependent_Draw;
196         setDependent_Check(e);
197 }
198 void setDependentAND3(entity e, string theCvarName, float theCvarMin, float theCvarMax, string theCvar2Name, float theCvar2Min, float theCvar2Max, string theCvar3Name, float theCvar3Min, float theCvar3Max)
199 {
200         e.draw_setDependent = e.draw;
201         e.cvar_setDependent = theCvarName;
202         e.cvarMin_setDependent = theCvarMin;
203         e.cvarMax_setDependent = theCvarMax;
204         e.cvar2_setDependent = theCvar2Name;
205         e.cvar2Min_setDependent = theCvar2Min;
206         e.cvar2Max_setDependent = theCvar2Max;
207         e.cvar3_setDependent = theCvar3Name;
208         e.cvar3Min_setDependent = theCvar3Min;
209         e.cvar3Max_setDependent = theCvar3Max;
210         e.op_setDependent = 0;
211         e.draw = setDependent_Draw;
212         setDependent_Check(e);
213 }
214
215 // EXTRESPONSE SYSTEM ////////////////////////////////////////////////////////
216
217 float _Nex_ExtResponseSystem_RequestsSent;
218 float _Nex_ExtResponseSystem_VersionHandled;
219 string _Nex_ExtResponseSystem_UpdateTo;
220 float _Nex_ExtResponseSystem_RetryTime;
221 float _Nex_ExtResponseSystem_RetryTime_LastDelay;
222
223 void() Item_Nex_ExtResponseSystem_SendQuery =
224 {
225         dprint("Sending extended response requests...\n");
226         localcmd(strcat("packet 64.22.107.122:27950 \"getExtResponse checkUpdates nexuiz ", cvar_string("g_nexuizversion"), "\"\n"));
227         _Nex_ExtResponseSystem_RequestsSent = TRUE;
228         _Nex_ExtResponseSystem_RetryTime_LastDelay = _Nex_ExtResponseSystem_RetryTime_LastDelay * 2 + 1;
229         _Nex_ExtResponseSystem_RetryTime = time + _Nex_ExtResponseSystem_RetryTime_LastDelay;
230 }
231
232 void(float argc) Item_Nex_ExtResponseSystem_Parse =
233 {
234         dprint("Received extended response packet from ", argv(0), "\n");
235         if(!_Nex_ExtResponseSystem_RequestsSent)
236         {
237                 dprint("  But I haven't sent a request yet! Ignoring.\n");
238                 return;
239         }
240         if(argv(1) == "noUpdateAvailable")
241         {
242                 if(_Nex_ExtResponseSystem_VersionHandled)
243                 {
244                         dprint("  duplicated update notice, ignored\n");
245                         return;
246                 }
247                 _Nex_ExtResponseSystem_VersionHandled = 1;
248         }
249         else if(argv(1) == "updateAvailable")
250         {
251                 if(_Nex_ExtResponseSystem_VersionHandled)
252                 {
253                         dprint("  duplicated update notice, ignored\n");
254                         return;
255                 }
256                 _Nex_ExtResponseSystem_VersionHandled = 1;
257                 _Nex_ExtResponseSystem_UpdateTo = strzone(argv(2)); // note: only one packet can be handled, so this can't be a leak
258         }
259         else
260                 dprint("  UNKNOWN RESPONSE TYPE: ", argv(1), "\n");
261 }
262
263 void() Item_Nex_ExtResponseSystem_CheckForResponse =
264 {
265         local string s;
266         local float argc;
267         while(strlen((s = getextresponse())))
268         {
269                 argc = tokenize_console(s);
270                 Item_Nex_ExtResponseSystem_Parse(argc);
271         }
272 }
273
274 // END OF EXTRESPONSE SYSTEM /////////////////////////////////////////////////
275
276 float preMenuInit()
277 {
278         vector sz;
279         vector boxA, boxB;
280
281         MapInfo_Cache_Create();
282         MapInfo_Enumerate();
283         if(!MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, 0, 0, 1))
284         {
285                 draw_reset();
286
287                 sz = eX * 0.025 + eY * 0.025 * (draw_scale_x / draw_scale_y);
288                 draw_CenterText('0.5 0.5 0' - 1.25 * sz_y * eY, "Autogenerating mapinfo for newly added maps...", sz, '1 1 1', 1, 0);
289
290                 boxA = '0.05 0.5 0' + 0.25 * sz_y * eY;
291                 boxB = '0.95 0.5 0' + 1.25 * sz_y * eY;
292                 draw_Fill(boxA, boxB - boxA, '1 1 1', 1);
293                 
294                 boxA += sz * 0.1;
295                 boxB -= sz * 0.1;
296                 draw_Fill(boxA, boxB - boxA, '0.1 0.1 0.1', 1);
297
298                 boxB_x = boxA_x * (1 - MapInfo_progress) + boxB_x * MapInfo_progress;
299                 draw_Fill(boxA, boxB - boxA, '0 0 1', 1);
300
301                 return FALSE;
302         }
303         return TRUE;
304 }
305
306 string campaign_name_previous;
307 float campaign_won_previous;
308 void postMenuDraw()
309 {
310 }
311 void preMenuDraw()
312 {
313         vector fs, sz, line, mid;
314
315         if(cvar("menu_updatecheck"))
316         {
317                 Item_Nex_ExtResponseSystem_CheckForResponse();
318                 if(!_Nex_ExtResponseSystem_VersionHandled)
319                         if(time > _Nex_ExtResponseSystem_RetryTime)
320                                 Item_Nex_ExtResponseSystem_SendQuery();
321         }
322
323         if(_Nex_ExtResponseSystem_UpdateTo != "")
324         {
325                 fs = ((1/draw_scale_x) * eX + (1/draw_scale_y) * eY) * 12;
326                 line = eY * fs_y;
327                 sz_x = draw_TextWidth("  http://www.nexuiz.com/  ", 0) * fs_x;
328                 sz_y = 3 * fs_y;
329
330                 draw_alpha = sin(time * 0.112 - 0.3) * 0.7;
331                 mid = eX * (0.5 + 0.5 * (1 - sz_x) * cos(time * 0.071))
332                     + eY * (0.5 + 0.5 * (1 - sz_y) * sin(time * 0.071));
333
334                 draw_Fill(mid - 0.5 * sz, sz, '1 1 0', 1);
335                 draw_CenterText(mid - 1 * line, strcat("Update to ", _Nex_ExtResponseSystem_UpdateTo, " now!"), fs, '1 0 0', 1, 0);
336                 draw_CenterText(mid - 0 * line, "http://www.nexuiz.com/", fs, '0 0 1', 1, 0);
337         }
338         if not(campaign_name_previous)
339                 campaign_name_previous = strzone(strcat(campaign_name, "x")); // force unequal
340         if(campaign_name == campaign_name_previous)
341         {
342                 if(cvar(strcat("g_campaign", campaign_name, "_won")))
343                 {
344                         if(!campaign_won_previous)
345                         {
346                                 m_display();
347                                 DialogOpenButton_Click_withCoords(NULL, main.winnerDialog, '0 0 0', eX * conwidth + eY * conheight);
348                         }
349                         campaign_won_previous = 1;
350                 }
351                 else
352                         campaign_won_previous = 0;
353         }
354         else
355         {
356                 strunzone(campaign_name_previous);
357                 campaign_name_previous = strzone(campaign_name);
358                 campaign_won_previous = cvar(strcat("g_campaign", campaign_name, "_won"));
359         }
360 }