]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/serverlist.c
Properly handle the cvars for serverlist categories now
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / serverlist.c
1 #ifdef INTERFACE
2 CLASS(XonoticServerList) EXTENDS(XonoticListBox)
3         METHOD(XonoticServerList, configureXonoticServerList, void(entity))
4         ATTRIB(XonoticServerList, rowsPerItem, float, 1)
5         METHOD(XonoticServerList, draw, void(entity))
6         METHOD(XonoticServerList, drawListBoxItem, void(entity, float, vector, float))
7         METHOD(XonoticServerList, clickListBoxItem, void(entity, float, vector))
8         METHOD(XonoticServerList, resizeNotify, void(entity, vector, vector, vector, vector))
9         METHOD(XonoticServerList, keyDown, float(entity, float, float, float))
10         METHOD(XonoticServerList, toggleFavorite, void(entity, string))
11
12         ATTRIB(XonoticServerList, iconsSizeFactor, float, 0.85)
13
14         ATTRIB(XonoticServerList, realFontSize, vector, '0 0 0')
15         ATTRIB(XonoticServerList, realUpperMargin, float, 0)
16         ATTRIB(XonoticServerList, columnIconsOrigin, float, 0)
17         ATTRIB(XonoticServerList, columnIconsSize, float, 0)
18         ATTRIB(XonoticServerList, columnPingOrigin, float, 0)
19         ATTRIB(XonoticServerList, columnPingSize, float, 0)
20         ATTRIB(XonoticServerList, columnNameOrigin, float, 0)
21         ATTRIB(XonoticServerList, columnNameSize, float, 0)
22         ATTRIB(XonoticServerList, columnMapOrigin, float, 0)
23         ATTRIB(XonoticServerList, columnMapSize, float, 0)
24         ATTRIB(XonoticServerList, columnTypeOrigin, float, 0)
25         ATTRIB(XonoticServerList, columnTypeSize, float, 0)
26         ATTRIB(XonoticServerList, columnPlayersOrigin, float, 0)
27         ATTRIB(XonoticServerList, columnPlayersSize, float, 0)
28
29         ATTRIB(XonoticServerList, selectedServer, string, string_null) // to restore selected server when needed
30         METHOD(XonoticServerList, setSelected, void(entity, float))
31         METHOD(XonoticServerList, setSortOrder, void(entity, float, float))
32         ATTRIB(XonoticServerList, filterShowEmpty, float, 1)
33         ATTRIB(XonoticServerList, filterShowFull, float, 1)
34         ATTRIB(XonoticServerList, filterString, string, string_null)
35         ATTRIB(XonoticServerList, controlledTextbox, entity, NULL)
36         ATTRIB(XonoticServerList, ipAddressBox, entity, NULL)
37         ATTRIB(XonoticServerList, favoriteButton, entity, NULL)
38         ATTRIB(XonoticServerList, nextRefreshTime, float, 0)
39         METHOD(XonoticServerList, refreshServerList, void(entity, float)) // refresh mode: REFRESHSERVERLIST_*
40         ATTRIB(XonoticServerList, needsRefresh, float, 1)
41         METHOD(XonoticServerList, focusEnter, void(entity))
42         METHOD(XonoticServerList, positionSortButton, void(entity, entity, float, float, string, void(entity, entity)))
43         ATTRIB(XonoticServerList, sortButton1, entity, NULL)
44         ATTRIB(XonoticServerList, sortButton2, entity, NULL)
45         ATTRIB(XonoticServerList, sortButton3, entity, NULL)
46         ATTRIB(XonoticServerList, sortButton4, entity, NULL)
47         ATTRIB(XonoticServerList, sortButton5, entity, NULL)
48         ATTRIB(XonoticServerList, connectButton, entity, NULL)
49         ATTRIB(XonoticServerList, infoButton, entity, NULL)
50         ATTRIB(XonoticServerList, currentSortOrder, float, 0)
51         ATTRIB(XonoticServerList, currentSortField, float, -1)
52         ATTRIB(XonoticServerList, lastBumpSelectTime, float, 0)
53         ATTRIB(XonoticServerList, lastClickedServer, float, -1)
54         ATTRIB(XonoticServerList, lastClickedTime, float, 0)
55
56         ATTRIB(XonoticServerList, ipAddressBoxFocused, float, -1)
57
58         ATTRIB(XonoticServerList, seenIPv4, float, 0)
59         ATTRIB(XonoticServerList, seenIPv6, float, 0)
60         ATTRIB(XonoticServerList, categoriesHeight, float, 1.25)
61
62         METHOD(XonoticServerList, getTotalHeight, float(entity))
63         METHOD(XonoticServerList, getItemAtPos, float(entity, float))
64         METHOD(XonoticServerList, getItemStart, float(entity, float))
65         METHOD(XonoticServerList, getItemHeight, float(entity, float))
66 ENDCLASS(XonoticServerList)
67 entity makeXonoticServerList();
68
69 #ifndef IMPLEMENTATION
70 float autocvar_menu_slist_categories;
71 float autocvar_menu_slist_categories_onlyifmultiple; 
72 float autocvar_menu_slist_purethreshold;
73 float autocvar_menu_slist_modimpurity;
74 float autocvar_menu_slist_recommendations;
75 float autocvar_menu_slist_recommendations_maxping;
76 float autocvar_menu_slist_recommendations_minfreeslots; 
77 float autocvar_menu_slist_recommendations_minhumans;
78 float autocvar_menu_slist_recommendations_purethreshold; 
79
80 // server cache fields
81 #define SLIST_FIELDS \
82         SLIST_FIELD(CNAME,       "cname") \
83         SLIST_FIELD(PING,        "ping") \
84         SLIST_FIELD(GAME,        "game") \
85         SLIST_FIELD(MOD,         "mod") \
86         SLIST_FIELD(MAP,         "map") \
87         SLIST_FIELD(NAME,        "name") \
88         SLIST_FIELD(MAXPLAYERS,  "maxplayers") \
89         SLIST_FIELD(NUMPLAYERS,  "numplayers") \
90         SLIST_FIELD(NUMHUMANS,   "numhumans") \
91         SLIST_FIELD(NUMBOTS,     "numbots") \
92         SLIST_FIELD(PROTOCOL,    "protocol") \
93         SLIST_FIELD(FREESLOTS,   "freeslots") \
94         SLIST_FIELD(PLAYERS,     "players") \
95         SLIST_FIELD(QCSTATUS,    "qcstatus") \
96         SLIST_FIELD(CATEGORY,    "category") \
97         SLIST_FIELD(ISFAVORITE,  "isfavorite")
98
99 #define SLIST_FIELD(suffix,name) float SLIST_FIELD_##suffix;
100 SLIST_FIELDS
101 #undef SLIST_FIELD
102
103 const float REFRESHSERVERLIST_RESORT = 0;    // sort the server list again to update for changes to e.g. favorite status, categories
104 const float REFRESHSERVERLIST_REFILTER = 1;  // ..., also update filter and sort criteria
105 const float REFRESHSERVERLIST_ASK = 2;       // ..., also suggest querying servers now
106 const float REFRESHSERVERLIST_RESET = 3;     // ..., also clear the list first
107
108 // function declarations
109 float IsServerInList(string list, string srv);
110 #define IsFavorite(srv) IsServerInList(cvar_string("net_slist_favorites"), srv)
111 #define IsPromoted(srv) IsServerInList(_Nex_ExtResponseSystem_PromotedServers, srv)
112 #define IsRecommended(srv) IsServerInList(_Nex_ExtResponseSystem_RecommendedServers, srv)
113
114 entity RetrieveCategoryEnt(float catnum);
115
116 float CheckCategoryOverride(float cat);
117 float CheckCategoryForEntry(float entry); 
118 float m_gethostcachecategory(float entry) { return CheckCategoryOverride(CheckCategoryForEntry(entry)); }
119
120 void RegisterSLCategories();
121
122 void ServerList_Connect_Click(entity btn, entity me);
123 void ServerList_Categories_Click(entity box, entity me);
124 void ServerList_ShowEmpty_Click(entity box, entity me);
125 void ServerList_ShowFull_Click(entity box, entity me);
126 void ServerList_Filter_Change(entity box, entity me);
127 void ServerList_Favorite_Click(entity btn, entity me);
128 void ServerList_Info_Click(entity btn, entity me);
129 void ServerList_Update_favoriteButton(entity btn, entity me);
130
131 // fields for category entities
132 #define MAX_CATEGORIES 9
133 #define CATEGORY_FIRST 1
134 entity categories[MAX_CATEGORIES];
135 float category_ent_count;
136 .string cat_name;
137 .string cat_string;
138 .string cat_enoverride_string;
139 .string cat_dioverride_string;
140 .float cat_enoverride;
141 .float cat_dioverride;
142
143 // fields for drawing categories
144 float category_name[MAX_CATEGORIES];
145 float category_item[MAX_CATEGORIES];
146 float category_draw_count;
147
148 #define SLIST_CATEGORIES \
149         SLIST_CATEGORY(CAT_FAVORITED,    "",            "",             ZCTX(_("SLCAT^Favorites"))) \
150         SLIST_CATEGORY(CAT_RECOMMENDED,  "",            "CAT_SERVERS",  ZCTX(_("SLCAT^Recommended"))) \
151         SLIST_CATEGORY(CAT_NORMAL,       "",            "CAT_SERVERS",  ZCTX(_("SLCAT^Normal Servers"))) \
152         SLIST_CATEGORY(CAT_SERVERS,      "CAT_NORMAL",  "CAT_SERVERS",  ZCTX(_("SLCAT^Servers"))) \
153         SLIST_CATEGORY(CAT_XPM,          "CAT_NORMAL",  "CAT_SERVERS",  ZCTX(_("SLCAT^Competitive Mode"))) \
154         SLIST_CATEGORY(CAT_MODIFIED,     "",            "CAT_SERVERS",  ZCTX(_("SLCAT^Modified Servers"))) \
155         SLIST_CATEGORY(CAT_OVERKILL,     "",            "CAT_SERVERS",  ZCTX(_("SLCAT^Overkill Mode"))) \
156         SLIST_CATEGORY(CAT_MINSTAGIB,    "",            "CAT_SERVERS",  ZCTX(_("SLCAT^MinstaGib Mode"))) \
157         SLIST_CATEGORY(CAT_DEFRAG,       "",            "CAT_SERVERS",  ZCTX(_("SLCAT^Defrag Mode")))
158
159 #define SLIST_CATEGORY_AUTOCVAR(name) autocvar_menu_slist_categories_##name##_override
160 #define SLIST_CATEGORY(name,enoverride,dioverride,str) \
161         float name; \
162         var string SLIST_CATEGORY_AUTOCVAR(name) = enoverride;
163 SLIST_CATEGORIES
164 #undef SLIST_CATEGORY
165
166 #endif
167 #endif
168 #ifdef IMPLEMENTATION
169
170 void RegisterSLCategories()
171 {
172         entity cat;
173         #define SLIST_CATEGORY(name,enoverride,dioverride,str) \
174                 SET_FIELD_COUNT(name, CATEGORY_FIRST, category_ent_count) \
175                 CHECK_MAX_COUNT(name, MAX_CATEGORIES, category_ent_count, "SLIST_CATEGORY") \
176                 cat = spawn(); \
177                 categories[name - 1] = cat; \
178                 cat.classname = "slist_category"; \
179                 cat.cat_name = strzone(#name); \
180                 cat.cat_enoverride_string = strzone(SLIST_CATEGORY_AUTOCVAR(name)); \
181                 cat.cat_dioverride_string = strzone(dioverride); \
182                 cat.cat_string = strzone(str);
183         SLIST_CATEGORIES
184         #undef SLIST_CATEGORY
185
186         float i, x, catnum;
187         string s;
188
189         #define PROCESS_OVERRIDE(override_string,override_field) \
190                 for(i = 0; i < category_ent_count; ++i) \
191                 { \
192                         s = categories[i].override_string; \
193                         if((s != "") && (s != categories[i].cat_name)) \
194                         { \
195                                 catnum = 0; \
196                                 for(x = 0; x < category_ent_count; ++x) \
197                                 { if(categories[x].cat_name == s) { \
198                                         catnum = (x+1); \
199                                         break; \
200                                 } } \
201                                 if(catnum) \
202                                 { \
203                                         strunzone(categories[i].override_string); \
204                                         categories[i].override_field = catnum; \
205                                         continue; \
206                                 } \
207                                 else \
208                                 { \
209                                         print(sprintf( \
210                                                 "RegisterSLCategories(): Improper override '%s' for category '%s'!\n", \
211                                                 s, \
212                                                 categories[i].cat_name \
213                                         )); \
214                                 } \
215                         } \
216                         strunzone(categories[i].override_string); \
217                         categories[i].override_field = 0; \
218                 }
219         PROCESS_OVERRIDE(cat_enoverride_string, cat_enoverride)
220         PROCESS_OVERRIDE(cat_dioverride_string, cat_dioverride)
221         #undef PROCESS_OVERRIDE
222 }
223
224 // Supporting Functions
225 entity RetrieveCategoryEnt(float catnum)
226 {
227         if((catnum > 0) && (catnum <= category_ent_count))
228         {
229                 return categories[catnum - 1];
230         }
231         else
232         {
233                 error(sprintf("RetrieveCategoryEnt(%d): Improper category number!\n", catnum));
234                 return world;
235         }
236 }
237
238 float IsServerInList(string list, string srv)
239 {
240         string p;
241         float i, n;
242         if(srv == "")
243                 return FALSE;
244         srv = netaddress_resolve(srv, 26000);
245         if(srv == "")
246                 return FALSE;
247         p = crypto_getidfp(srv);
248         n = tokenize_console(list);
249         for(i = 0; i < n; ++i)
250         {
251                 if(substring(argv(i), 0, 1) != "[" && strlen(argv(i)) == 44 && strstrofs(argv(i), ".", 0) < 0)
252                 {
253                         if(p)
254                                 if(argv(i) == p)
255                                         return TRUE;
256                 }
257                 else
258                 {
259                         if(srv == netaddress_resolve(argv(i), 26000))
260                                 return TRUE;
261                 }
262         }
263         return FALSE;
264 }
265
266 float CheckCategoryOverride(float cat)
267 {
268         entity catent = RetrieveCategoryEnt(cat);
269         if(catent)
270         {
271                 float override = (autocvar_menu_slist_categories ? catent.cat_enoverride : catent.cat_dioverride); 
272                 if(override) { return override; }
273                 else { return cat; }
274         }
275         else
276         {
277                 error(sprintf("CheckCategoryOverride(%d): Improper category number!\n", cat));
278                 return cat;
279         }
280 }
281
282 float CheckCategoryForEntry(float entry)
283 {
284         string s, k, v, modtype = "";
285         float j, m, impure = 0, freeslots = 0, sflags = 0;
286         s = gethostcachestring(SLIST_FIELD_QCSTATUS, entry);
287         m = tokenizebyseparator(s, ":");
288
289         for(j = 2; j < m; ++j)
290         {
291                 if(argv(j) == "") { break; }
292                 k = substring(argv(j), 0, 1);
293                 v = substring(argv(j), 1, -1);
294                 switch(k)
295                 {
296                         case "P": { impure = stof(v); break; }
297                         case "S": { freeslots = stof(v); break; }
298                         case "F": { sflags = stof(v); break; }
299                         case "M": { modtype = strtolower(v); break; }
300                 }
301         }
302
303         if(modtype != "xonotic") { impure += autocvar_menu_slist_modimpurity; }
304
305         // check if this server is favorited
306         if(gethostcachenumber(SLIST_FIELD_ISFAVORITE, entry)) { return CAT_FAVORITED; }
307
308         // now check if it's recommended
309         if(autocvar_menu_slist_recommendations)
310         {
311                 string cname = gethostcachestring(SLIST_FIELD_CNAME, entry);
312                 
313                 if(IsPromoted(cname)) { return CAT_RECOMMENDED; }
314                 else
315                 {
316                         float recommended = 0;
317                         if(autocvar_menu_slist_recommendations & 1)
318                         {
319                                 if(IsRecommended(cname)) { ++recommended; }
320                                 else { --recommended; }
321                         }
322                         if(autocvar_menu_slist_recommendations & 2)
323                         {
324                                 if(
325                                         ///// check for minimum free slots
326                                         (freeslots >= autocvar_menu_slist_recommendations_minfreeslots)
327                                         
328                                         && // check for purity requirement
329                                         (
330                                                 (autocvar_menu_slist_recommendations_purethreshold < 0)
331                                                 ||
332                                                 (impure <= autocvar_menu_slist_recommendations_purethreshold)
333                                         )
334                                         
335                                         && // check for minimum amount of humans
336                                         (
337                                                 gethostcachenumber(SLIST_FIELD_NUMHUMANS, entry)
338                                                 >=
339                                                 autocvar_menu_slist_recommendations_minhumans
340                                         )
341                                         
342                                         && // check for maximum latency
343                                         (
344                                                 gethostcachenumber(SLIST_FIELD_PING, entry)
345                                                 <=
346                                                 autocvar_menu_slist_recommendations_maxping
347                                         )
348                                 )
349                                         { ++recommended; }
350                                 else
351                                         { --recommended; }
352                         }
353                         if(recommended > 0) { return CAT_RECOMMENDED; }
354                 }
355         }
356
357         // if not favorited or recommended, check modname
358         if(modtype != "xonotic")
359         {
360                 switch(modtype)
361                 {
362                         // old servers which don't report their mod name are considered modified now
363                         case "": { return CAT_MODIFIED; }
364                         
365                         case "xpm": { return CAT_XPM; } 
366                         case "minstagib": { return CAT_MINSTAGIB; }
367                         case "overkill": { return CAT_OVERKILL; }
368                         //case "nix": { return CAT_NIX; }
369                         //case "newtoys": { return CAT_NEWTOYS; }
370
371                         // "cts" is allowed as compat, xdf is replacement
372                         case "cts": 
373                         case "xdf": { return CAT_DEFRAG; }
374                         
375                         default: { dprint(sprintf("Found strange mod type: %s\n", modtype)); return CAT_MODIFIED; }
376                 }
377         }
378
379         // must be normal or impure server
380         return ((impure > autocvar_menu_slist_purethreshold) ? CAT_MODIFIED : CAT_NORMAL);
381 }
382
383 void XonoticServerList_toggleFavorite(entity me, string srv)
384 {
385         string s, s0, s1, s2, srv_resolved, p;
386         float i, n, f;
387         srv_resolved = netaddress_resolve(srv, 26000);
388         p = crypto_getidfp(srv_resolved);
389         s = cvar_string("net_slist_favorites");
390         n = tokenize_console(s);
391         f = 0;
392         for(i = 0; i < n; ++i)
393         {
394                 if(substring(argv(i), 0, 1) != "[" && strlen(argv(i)) == 44 && strstrofs(argv(i), ".", 0) < 0)
395                 {
396                         if(p)
397                                 if(argv(i) != p)
398                                         continue;
399                 }
400                 else
401                 {
402                         if(srv_resolved != netaddress_resolve(argv(i), 26000))
403                                 continue;
404                 }
405                 s0 = s1 = s2 = "";
406                 if(i > 0)
407                         s0 = substring(s, 0, argv_end_index(i - 1));
408                 if(i < n-1)
409                         s2 = substring(s, argv_start_index(i + 1), -1);
410                 if(s0 != "" && s2 != "")
411                         s1 = " ";
412                 cvar_set("net_slist_favorites", strcat(s0, s1, s2));
413                 s = cvar_string("net_slist_favorites");
414                 n = tokenize_console(s);
415                 f = 1;
416                 --i;
417         }
418         
419         if(!f)
420         {
421                 s1 = "";
422                 if(s != "")
423                         s1 = " ";
424                 if(p)
425                         cvar_set("net_slist_favorites", strcat(s, s1, p));
426                 else
427                         cvar_set("net_slist_favorites", strcat(s, s1, srv));
428         }
429
430         me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
431 }
432
433 void ServerList_Update_favoriteButton(entity btn, entity me)
434 {
435         me.favoriteButton.setText(me.favoriteButton,
436                 (IsFavorite(me.ipAddressBox.text) ?
437                         _("Remove") : _("Favorite")
438                 )
439         );
440 }
441
442 entity makeXonoticServerList()
443 {
444         entity me;
445         me = spawnXonoticServerList();
446         me.configureXonoticServerList(me);
447         return me;
448 }
449 void XonoticServerList_configureXonoticServerList(entity me)
450 {
451         me.configureXonoticListBox(me);
452
453         // update field ID's
454         #define SLIST_FIELD(suffix,name) SLIST_FIELD_##suffix = gethostcacheindexforkey(name);
455         SLIST_FIELDS
456         #undef SLIST_FIELD
457
458         // clear list
459         me.nItems = 0;
460 }
461 void XonoticServerList_setSelected(entity me, float i)
462 {
463         float save;
464         save = me.selectedItem;
465         SUPER(XonoticServerList).setSelected(me, i);
466         /*
467         if(me.selectedItem == save)
468                 return;
469         */
470         if(me.nItems == 0)
471                 return;
472         if(gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT) != me.nItems)
473                 return; // sorry, it would be wrong
474
475         if(me.selectedServer)
476                 strunzone(me.selectedServer);
477         me.selectedServer = strzone(gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
478
479         me.ipAddressBox.setText(me.ipAddressBox, me.selectedServer);
480         me.ipAddressBox.cursorPos = strlen(me.selectedServer);
481         me.ipAddressBoxFocused = -1;
482 }
483 void XonoticServerList_refreshServerList(entity me, float mode)
484 {
485         //print("refresh of type ", ftos(mode), "\n");
486
487         if(mode >= REFRESHSERVERLIST_REFILTER)
488         {
489                 float m, i, n;
490                 float listflags = 0;
491                 string s, typestr, modstr;
492
493                 s = me.filterString;
494
495                 m = strstrofs(s, ":", 0);
496                 if(m >= 0)
497                 {
498                         typestr = substring(s, 0, m);
499                         s = substring(s, m + 1, strlen(s) - m - 1);
500                         while(substring(s, 0, 1) == " ")
501                                 s = substring(s, 1, strlen(s) - 1);
502                 }
503                 else
504                         typestr = "";
505
506                 modstr = cvar_string("menu_slist_modfilter");
507
508                 m = SLIST_MASK_AND - 1;
509                 resethostcachemasks();
510
511                 // ping: reject negative ping (no idea why this happens in the first place, engine bug)
512                 sethostcachemasknumber(++m, SLIST_FIELD_PING, 0, SLIST_TEST_GREATEREQUAL);
513
514                 // show full button
515                 if(!me.filterShowFull)
516                 {
517                         sethostcachemasknumber(++m, SLIST_FIELD_FREESLOTS, 1, SLIST_TEST_GREATEREQUAL); // legacy
518                         sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, ":S0:", SLIST_TEST_NOTCONTAIN); // g_maxplayers support
519                 }
520
521                 // show empty button
522                 if(!me.filterShowEmpty)
523                         sethostcachemasknumber(++m, SLIST_FIELD_NUMHUMANS, 1, SLIST_TEST_GREATEREQUAL);
524
525                 // gametype filtering
526                 if(typestr != "")
527                         sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, strcat(typestr, ":"), SLIST_TEST_STARTSWITH);
528
529                 // mod filtering
530                 if(modstr != "")
531                 {
532                         if(substring(modstr, 0, 1) == "!")
533                                 sethostcachemaskstring(++m, SLIST_FIELD_MOD, resolvemod(substring(modstr, 1, strlen(modstr) - 1)), SLIST_TEST_NOTEQUAL);
534                         else
535                                 sethostcachemaskstring(++m, SLIST_FIELD_MOD, resolvemod(modstr), SLIST_TEST_EQUAL);
536                 }
537
538                 // server banning
539                 n = tokenizebyseparator(_Nex_ExtResponseSystem_BannedServers, " ");
540                 for(i = 0; i < n; ++i)
541                         if(argv(i) != "")
542                                 sethostcachemaskstring(++m, SLIST_FIELD_CNAME, argv(i), SLIST_TEST_NOTSTARTSWITH);
543
544                 m = SLIST_MASK_OR - 1;
545                 if(s != "")
546                 {
547                         sethostcachemaskstring(++m, SLIST_FIELD_NAME, s, SLIST_TEST_CONTAINS);
548                         sethostcachemaskstring(++m, SLIST_FIELD_MAP, s, SLIST_TEST_CONTAINS);
549                         sethostcachemaskstring(++m, SLIST_FIELD_PLAYERS, s, SLIST_TEST_CONTAINS);
550                         sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, strcat(s, ":"), SLIST_TEST_STARTSWITH);
551                 }
552
553                 // sorting flags
554                 //listflags |= SLSF_FAVORITES;
555                 listflags |= SLSF_CATEGORIES;
556                 if(me.currentSortOrder < 0) { listflags |= SLSF_DESCENDING; }
557                 sethostcachesort(me.currentSortField, listflags);
558         }
559         
560         resorthostcache();
561         if(mode >= REFRESHSERVERLIST_ASK)
562                 refreshhostcache(mode >= REFRESHSERVERLIST_RESET);
563 }
564 void XonoticServerList_focusEnter(entity me)
565 {
566         if(time < me.nextRefreshTime)
567         {
568                 //print("sorry, no refresh yet\n");
569                 return;
570         }
571         me.nextRefreshTime = time + 10;
572         me.refreshServerList(me, REFRESHSERVERLIST_ASK);
573 }
574
575 void XonoticServerList_draw(entity me)
576 {
577         float i, found, owned;
578
579         if(_Nex_ExtResponseSystem_BannedServersNeedsRefresh)
580         {
581                 if(!me.needsRefresh)
582                         me.needsRefresh = 2;
583                 _Nex_ExtResponseSystem_BannedServersNeedsRefresh = 0;
584         }
585
586         if(_Nex_ExtResponseSystem_PromotedServersNeedsRefresh)
587         {
588                 if(!me.needsRefresh)
589                         me.needsRefresh = 3;
590                 _Nex_ExtResponseSystem_PromotedServersNeedsRefresh = 0;
591         }
592
593         if(_Nex_ExtResponseSystem_RecommendedServersNeedsRefresh)
594         {
595                 if(!me.needsRefresh)
596                         me.needsRefresh = 3;
597                 _Nex_ExtResponseSystem_RecommendedServersNeedsRefresh = 0;
598         }
599
600         if(me.currentSortField == -1)
601         {
602                 me.setSortOrder(me, SLIST_FIELD_PING, +1);
603                 me.refreshServerList(me, REFRESHSERVERLIST_RESET);
604         }
605         else if(me.needsRefresh == 1)
606         {
607                 me.needsRefresh = 2; // delay by one frame to make sure "slist" has been executed
608         }
609         else if(me.needsRefresh == 2)
610         {
611                 me.needsRefresh = 0;
612                 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
613         }
614         else if(me.needsRefresh == 3)
615         {
616                 me.needsRefresh = 0;
617                 me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
618         }
619
620         owned = ((me.selectedServer == me.ipAddressBox.text) && (me.ipAddressBox.text != ""));
621
622         for(i = 0; i < category_draw_count; ++i) { category_name[i] = -1; category_item[i] = -1; }
623         category_draw_count = 0;
624
625         if(autocvar_menu_slist_categories >= 0) // if less than 0, don't even draw a category heading for favorites
626         {
627                 float itemcount = gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT);
628                 me.nItems = itemcount;
629                 
630                 //float visible = floor(me.scrollPos / me.itemHeight);
631                 // ^ unfortunately no such optimization can be made-- we must process through the
632                 // entire list, otherwise there is no way to know which item is first in its category.
633
634                 // binary search method suggested by div
635                 float x;
636                 float begin = 0;
637                 for(x = 1; x <= category_ent_count; ++x) {
638                         float first = begin;
639                         float last = (itemcount - 1);
640                         if (first > last) {
641                                 // List is empty.
642                                 break;
643                         }
644                         float catf = gethostcachenumber(SLIST_FIELD_CATEGORY, first);
645                         float catl = gethostcachenumber(SLIST_FIELD_CATEGORY, last);
646                         if (catf > x) {
647                                 // The first one is already > x.
648                                 // Therefore, category x does not exist.
649                                 // Higher numbered categories do exist though.
650                         } else if (catl < x) {
651                                 // The last one is < x.
652                                 // Thus this category - and any following -
653                                 // don't exist.
654                                 break;
655                         } else if (catf == x) {
656                                 // Starts at first. This breaks the loop
657                                 // invariant in the binary search and thus has
658                                 // to be handled separately.
659                                 if(gethostcachenumber(SLIST_FIELD_CATEGORY, first) != x)
660                                         error("Category mismatch I");
661                                 if(first > 0)
662                                         if(gethostcachenumber(SLIST_FIELD_CATEGORY, first - 1) == x)
663                                                 error("Category mismatch II");
664                                 category_name[category_draw_count] = x;
665                                 category_item[category_draw_count] = first;
666                                 ++category_draw_count;
667                                 begin = first + 1;
668                         } else {
669                                 // At this point, catf <= x < catl, thus
670                                 // catf < catl, thus first < last.
671                                 // INVARIANTS:
672                                 // last - first >= 1
673                                 // catf == gethostcachenumber(SLIST_FIELD_CATEGORY(first)
674                                 // catl == gethostcachenumber(SLIST_FIELD_CATEGORY(last)
675                                 // catf < x
676                                 // catl >= x
677                                 while (last - first > 1) {
678                                         float middle = floor((first + last) / 2);
679                                         // By loop condition, middle != first && middle != last.
680                                         float cat = gethostcachenumber(SLIST_FIELD_CATEGORY, middle);
681                                         if (cat >= x) {
682                                                 last = middle;
683                                                 catl = cat;
684                                         } else {
685                                                 first = middle;
686                                                 catf = cat;
687                                         }
688                                 }
689                                 if (catl == x) {
690                                         if(gethostcachenumber(SLIST_FIELD_CATEGORY, last) != x)
691                                                 error("Category mismatch III");
692                                         if(last > 0)
693                                                 if(gethostcachenumber(SLIST_FIELD_CATEGORY, last - 1) == x)
694                                                         error("Category mismatch IV");
695                                         category_name[category_draw_count] = x;
696                                         category_item[category_draw_count] = last;
697                                         ++category_draw_count;
698                                         begin = last + 1; // already scanned through these, skip 'em
699                                 }
700                                 else
701                                         begin = last; // already scanned through these, skip 'em
702                         }
703                 }
704                 if(autocvar_menu_slist_categories_onlyifmultiple && (category_draw_count == 1))
705                 {
706                         category_name[0] = -1;
707                         category_item[0] = -1;
708                         category_draw_count = 0;
709                         me.nItems = itemcount;
710                 }
711         }
712         else { me.nItems = gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT); }
713
714         me.connectButton.disabled = ((me.nItems == 0) && (me.ipAddressBox.text == ""));
715         me.infoButton.disabled = ((me.nItems == 0) || !owned);
716         me.favoriteButton.disabled = ((me.nItems == 0) && (me.ipAddressBox.text == ""));
717
718         found = 0;
719         if(me.selectedServer)
720         {
721                 for(i = 0; i < me.nItems; ++i)
722                 {
723                         if(gethostcachestring(SLIST_FIELD_CNAME, i) == me.selectedServer)
724                         {
725                                 if(i != me.selectedItem)
726                                 {
727                                         me.lastClickedServer = -1;
728                                         me.selectedItem = i;
729                                 }
730                                 found = 1;
731                                 break;
732                         }
733                 }
734         }
735         if(!found)
736         {
737                 if(me.nItems > 0)
738                 {
739                         if(me.selectedItem >= me.nItems)
740                                 me.selectedItem = me.nItems - 1;
741                         if(me.selectedServer)
742                                 strunzone(me.selectedServer);
743                         me.selectedServer = strzone(gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
744                 }
745         }
746         
747         if(owned)
748         {
749                 if(me.selectedServer != me.ipAddressBox.text)
750                 {
751                         me.ipAddressBox.setText(me.ipAddressBox, me.selectedServer);
752                         me.ipAddressBox.cursorPos = strlen(me.selectedServer);
753                         me.ipAddressBoxFocused = -1;
754                 }
755         }
756
757         if(me.ipAddressBoxFocused != me.ipAddressBox.focused)
758         {
759                 if(me.ipAddressBox.focused || me.ipAddressBoxFocused < 0)
760                         ServerList_Update_favoriteButton(NULL, me);
761                 me.ipAddressBoxFocused = me.ipAddressBox.focused;
762         }
763
764         SUPER(XonoticServerList).draw(me);
765 }
766 void ServerList_PingSort_Click(entity btn, entity me)
767 {
768         me.setSortOrder(me, SLIST_FIELD_PING, +1);
769 }
770 void ServerList_NameSort_Click(entity btn, entity me)
771 {
772         me.setSortOrder(me, SLIST_FIELD_NAME, -1); // why?
773 }
774 void ServerList_MapSort_Click(entity btn, entity me)
775 {
776         me.setSortOrder(me, SLIST_FIELD_MAP, -1); // why?
777 }
778 void ServerList_PlayerSort_Click(entity btn, entity me)
779 {
780         me.setSortOrder(me, SLIST_FIELD_NUMHUMANS, -1);
781 }
782 void ServerList_TypeSort_Click(entity btn, entity me)
783 {
784         string s, t;
785         float i, m;
786         s = me.filterString;
787         m = strstrofs(s, ":", 0);
788         if(m >= 0)
789         {
790                 s = substring(s, 0, m);
791                 while(substring(s, m+1, 1) == " ") // skip spaces
792                         ++m;
793         }
794         else
795                 s = "";
796
797         for(i = 1; ; i *= 2) // 20 modes ought to be enough for anyone
798         {
799                 t = MapInfo_Type_ToString(i);
800                 if(i > 1)
801                         if(t == "") // it repeats (default case)
802                         {
803                                 // no type was found
804                                 // choose the first one
805                                 s = MapInfo_Type_ToString(1);
806                                 break;
807                         }
808                 if(s == t)
809                 {
810                         // the type was found
811                         // choose the next one
812                         s = MapInfo_Type_ToString(i * 2);
813                         if(s == "")
814                                 s = MapInfo_Type_ToString(1);
815                         break;
816                 }
817         }
818
819         if(s != "")
820                 s = strcat(s, ":");
821         s = strcat(s, substring(me.filterString, m+1, strlen(me.filterString) - m - 1));
822
823         me.controlledTextbox.setText(me.controlledTextbox, s);
824         me.controlledTextbox.keyDown(me.controlledTextbox, K_END, 0, 0);
825         me.controlledTextbox.keyUp(me.controlledTextbox, K_END, 0, 0);
826         //ServerList_Filter_Change(me.controlledTextbox, me);
827 }
828 void ServerList_Filter_Change(entity box, entity me)
829 {
830         if(me.filterString)
831                 strunzone(me.filterString);
832         if(box.text != "")
833                 me.filterString = strzone(box.text);
834         else
835                 me.filterString = string_null;
836         me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
837
838         me.ipAddressBox.setText(me.ipAddressBox, "");
839         me.ipAddressBox.cursorPos = 0;
840         me.ipAddressBoxFocused = -1;
841 }
842 void ServerList_Categories_Click(entity box, entity me)
843 {
844         box.setChecked(box, autocvar_menu_slist_categories = !autocvar_menu_slist_categories);
845         me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
846
847         me.ipAddressBox.setText(me.ipAddressBox, "");
848         me.ipAddressBox.cursorPos = 0;
849         me.ipAddressBoxFocused = -1;
850 }
851 void ServerList_ShowEmpty_Click(entity box, entity me)
852 {
853         box.setChecked(box, me.filterShowEmpty = !me.filterShowEmpty);
854         me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
855
856         me.ipAddressBox.setText(me.ipAddressBox, "");
857         me.ipAddressBox.cursorPos = 0;
858         me.ipAddressBoxFocused = -1;
859 }
860 void ServerList_ShowFull_Click(entity box, entity me)
861 {
862         box.setChecked(box, me.filterShowFull = !me.filterShowFull);
863         me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
864
865         me.ipAddressBox.setText(me.ipAddressBox, "");
866         me.ipAddressBox.cursorPos = 0;
867         me.ipAddressBoxFocused = -1;
868 }
869 void XonoticServerList_setSortOrder(entity me, float fld, float direction)
870 {
871         if(me.currentSortField == fld)
872                 direction = -me.currentSortOrder;
873         me.currentSortOrder = direction;
874         me.currentSortField = fld;
875         me.sortButton1.forcePressed = (fld == SLIST_FIELD_PING);
876         me.sortButton2.forcePressed = (fld == SLIST_FIELD_NAME);
877         me.sortButton3.forcePressed = (fld == SLIST_FIELD_MAP);
878         me.sortButton4.forcePressed = 0;
879         me.sortButton5.forcePressed = (fld == SLIST_FIELD_NUMHUMANS);
880         me.selectedItem = 0;
881         if(me.selectedServer)
882                 strunzone(me.selectedServer);
883         me.selectedServer = string_null;
884         me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
885 }
886 void XonoticServerList_positionSortButton(entity me, entity btn, float theOrigin, float theSize, string theTitle, void(entity, entity) theFunc)
887 {
888         vector originInLBSpace, sizeInLBSpace;
889         originInLBSpace = eY * (-me.itemHeight);
890         sizeInLBSpace = eY * me.itemHeight + eX * (1 - me.controlWidth);
891
892         vector originInDialogSpace, sizeInDialogSpace;
893         originInDialogSpace = boxToGlobal(originInLBSpace, me.Container_origin, me.Container_size);
894         sizeInDialogSpace = boxToGlobalSize(sizeInLBSpace, me.Container_size);
895
896         btn.Container_origin_x = originInDialogSpace_x + sizeInDialogSpace_x * theOrigin;
897         btn.Container_size_x   =                         sizeInDialogSpace_x * theSize;
898         btn.setText(btn, theTitle);
899         btn.onClick = theFunc;
900         btn.onClickEntity = me;
901         btn.resized = 1;
902 }
903 void XonoticServerList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
904 {
905         SUPER(XonoticServerList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
906
907         me.realFontSize_y = me.fontSize / (absSize_y * me.itemHeight);
908         me.realFontSize_x = me.fontSize / (absSize_x * (1 - me.controlWidth));
909         me.realUpperMargin = 0.5 * (1 - me.realFontSize_y);
910
911         me.columnIconsOrigin = 0;
912         me.columnIconsSize = me.realFontSize_x * 4 * me.iconsSizeFactor;
913         me.columnPingSize = me.realFontSize_x * 3;
914         me.columnMapSize = me.realFontSize_x * 10;
915         me.columnTypeSize = me.realFontSize_x * 4;
916         me.columnPlayersSize = me.realFontSize_x * 5;
917         me.columnNameSize = 1 - me.columnPlayersSize - me.columnMapSize - me.columnPingSize - me.columnIconsSize - me.columnTypeSize - 5 * me.realFontSize_x;
918         me.columnPingOrigin = me.columnIconsOrigin + me.columnIconsSize + me.realFontSize_x;
919         me.columnNameOrigin = me.columnPingOrigin + me.columnPingSize + me.realFontSize_x;
920         me.columnMapOrigin = me.columnNameOrigin + me.columnNameSize + me.realFontSize_x;
921         me.columnTypeOrigin = me.columnMapOrigin + me.columnMapSize + me.realFontSize_x;
922         me.columnPlayersOrigin = me.columnTypeOrigin + me.columnTypeSize + me.realFontSize_x;
923
924         me.positionSortButton(me, me.sortButton1, me.columnPingOrigin, me.columnPingSize, _("Ping"), ServerList_PingSort_Click);
925         me.positionSortButton(me, me.sortButton2, me.columnNameOrigin, me.columnNameSize, _("Host name"), ServerList_NameSort_Click);
926         me.positionSortButton(me, me.sortButton3, me.columnMapOrigin, me.columnMapSize, _("Map"), ServerList_MapSort_Click);
927         me.positionSortButton(me, me.sortButton4, me.columnTypeOrigin, me.columnTypeSize, _("Type"), ServerList_TypeSort_Click);
928         me.positionSortButton(me, me.sortButton5, me.columnPlayersOrigin, me.columnPlayersSize, _("Players"), ServerList_PlayerSort_Click);
929
930         float f;
931         f = me.currentSortField;
932         if(f >= 0)
933         {
934                 me.currentSortField = -1;
935                 me.setSortOrder(me, f, me.currentSortOrder); // force resetting the sort order
936         }
937 }
938 void ServerList_Connect_Click(entity btn, entity me)
939 {
940         localcmd(sprintf("connect %s\n",
941                 ((me.ipAddressBox.text != "") ?
942                         me.ipAddressBox.text : me.selectedServer
943                 )
944         ));
945 }
946 void ServerList_Favorite_Click(entity btn, entity me)
947 {
948         string ipstr;
949         ipstr = netaddress_resolve(me.ipAddressBox.text, 26000);
950         if(ipstr != "")
951         {
952                 me.toggleFavorite(me, me.ipAddressBox.text);
953                 me.ipAddressBoxFocused = -1;
954         }
955 }
956 void ServerList_Info_Click(entity btn, entity me)
957 {
958         if (me.nItems != 0)
959                 main.serverInfoDialog.loadServerInfo(main.serverInfoDialog, me.selectedItem);
960         DialogOpenButton_Click(me, main.serverInfoDialog);
961 }
962 void XonoticServerList_clickListBoxItem(entity me, float i, vector where)
963 {
964         if(i == me.lastClickedServer)
965                 if(time < me.lastClickedTime + 0.3)
966                 {
967                         // DOUBLE CLICK!
968                         ServerList_Connect_Click(NULL, me);
969                 }
970         me.lastClickedServer = i;
971         me.lastClickedTime = time;
972 }
973 void XonoticServerList_drawListBoxItem(entity me, float i, vector absSize, float isSelected)
974 {
975         // layout: Ping, Server name, Map name, NP, TP, MP
976         float p, q;
977         float isv4, isv6;
978         vector theColor;
979         float theAlpha;
980         float m, pure, freeslots, j, sflags;
981         string s, typestr, versionstr, k, v, modname;
982
983         //print(sprintf("time: %f, i: %d, item: %d, nitems: %d\n", time, i, item, me.nItems));
984
985         vector oldscale = draw_scale;
986         vector oldshift = draw_shift;
987 #define SET_YRANGE(start,end) \
988         draw_scale = boxToGlobalSize(eX * 1 + eY * (end - start), oldscale); \
989         draw_shift = boxToGlobal(eY * start, oldshift, oldscale);
990
991         for (j = 0; j < category_draw_count; ++j) {
992                 // Matches exactly the headings with increased height.
993                 if (i == category_item[j])
994                         break;
995         }
996
997         if (j < category_draw_count)
998         {
999                 entity catent = RetrieveCategoryEnt(category_name[j]);
1000                 if(catent)
1001                 {
1002                         SET_YRANGE(
1003                                 (me.categoriesHeight - 1) / (me.categoriesHeight + 1),
1004                                 me.categoriesHeight / (me.categoriesHeight + 1)
1005                         );
1006                         draw_Text(
1007                                 eY * me.realUpperMargin
1008                                 +
1009 #if 0
1010                                 eX * (me.columnNameOrigin + (me.columnNameSize - draw_TextWidth(catent.cat_string, 0, me.realFontSize)) * 0.5),
1011                                 catent.cat_string,
1012 #else
1013                                 eX * (me.columnNameOrigin),
1014                                 strcat(catent.cat_string, ":"),
1015 #endif
1016                                 me.realFontSize,
1017                                 '1 1 1',
1018                                 SKINALPHA_TEXT,
1019                                 0
1020                         );
1021                         SET_YRANGE(me.categoriesHeight / (me.categoriesHeight + 1), 1);
1022                 }
1023         }
1024         
1025         if(isSelected)
1026                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
1027
1028         s = gethostcachestring(SLIST_FIELD_QCSTATUS, i);
1029         m = tokenizebyseparator(s, ":");
1030         typestr = "";
1031         if(m >= 2)
1032         {
1033                 typestr = argv(0);
1034                 versionstr = argv(1);
1035         }
1036         freeslots = -1;
1037         sflags = -1;
1038         modname = "";
1039         pure = 0;
1040         for(j = 2; j < m; ++j)
1041         {
1042                 if(argv(j) == "")
1043                         break;
1044                 k = substring(argv(j), 0, 1);
1045                 v = substring(argv(j), 1, -1);
1046                 if(k == "P")
1047                         pure = stof(v);
1048                 else if(k == "S")
1049                         freeslots = stof(v);
1050                 else if(k == "F")
1051                         sflags = stof(v);
1052                 else if(k == "M")
1053                         modname = v;
1054         }
1055
1056 #ifdef COMPAT_NO_MOD_IS_XONOTIC
1057         if(modname == "")
1058                 modname = "Xonotic";
1059 #endif
1060
1061         /*
1062         SLIST_FIELD_MOD = gethostcacheindexforkey("mod");
1063         s = gethostcachestring(SLIST_FIELD_MOD, i);
1064         if(s != "data")
1065                 if(modname == "Xonotic")
1066                         modname = s;
1067         */
1068
1069         // list the mods here on which the pure server check actually works
1070         if(modname != "Xonotic")
1071         if(modname != "MinstaGib")
1072         if(modname != "CTS")
1073         if(modname != "NIX")
1074         if(modname != "NewToys")
1075                 pure = 0;
1076
1077         if(gethostcachenumber(SLIST_FIELD_FREESLOTS, i) <= 0)
1078                 theAlpha = SKINALPHA_SERVERLIST_FULL;
1079         else if(freeslots == 0)
1080                 theAlpha = SKINALPHA_SERVERLIST_FULL; // g_maxplayers support
1081         else if not(gethostcachenumber(SLIST_FIELD_NUMHUMANS, i))
1082                 theAlpha = SKINALPHA_SERVERLIST_EMPTY;
1083         else
1084                 theAlpha = 1;
1085
1086         p = gethostcachenumber(SLIST_FIELD_PING, i);
1087 #define PING_LOW 75
1088 #define PING_MED 200
1089 #define PING_HIGH 500
1090         if(p < PING_LOW)
1091                 theColor = SKINCOLOR_SERVERLIST_LOWPING + (SKINCOLOR_SERVERLIST_MEDPING - SKINCOLOR_SERVERLIST_LOWPING) * (p / PING_LOW);
1092         else if(p < PING_MED)
1093                 theColor = SKINCOLOR_SERVERLIST_MEDPING + (SKINCOLOR_SERVERLIST_HIGHPING - SKINCOLOR_SERVERLIST_MEDPING) * ((p - PING_LOW) / (PING_MED - PING_LOW));
1094         else if(p < PING_HIGH)
1095         {
1096                 theColor = SKINCOLOR_SERVERLIST_HIGHPING;
1097                 theAlpha *= 1 + (SKINALPHA_SERVERLIST_HIGHPING - 1) * ((p - PING_MED) / (PING_HIGH - PING_MED));
1098         }
1099         else
1100         {
1101                 theColor = eX;
1102                 theAlpha *= SKINALPHA_SERVERLIST_HIGHPING;
1103         }
1104
1105         if(gethostcachenumber(SLIST_FIELD_ISFAVORITE, i))
1106         {
1107                 theColor = theColor * (1 - SKINALPHA_SERVERLIST_FAVORITE) + SKINCOLOR_SERVERLIST_FAVORITE * SKINALPHA_SERVERLIST_FAVORITE;
1108                 theAlpha = theAlpha * (1 - SKINALPHA_SERVERLIST_FAVORITE) + SKINALPHA_SERVERLIST_FAVORITE;
1109         }
1110
1111         s = gethostcachestring(SLIST_FIELD_CNAME, i);
1112
1113         isv4 = isv6 = 0;
1114         if(substring(s, 0, 1) == "[")
1115         {
1116                 isv6 = 1;
1117                 me.seenIPv6 += 1;
1118         }
1119         else if(strstrofs("0123456789", substring(s, 0, 1), 0) >= 0)
1120         {
1121                 isv4 = 1;
1122                 me.seenIPv4 += 1;
1123         }
1124
1125         q = stof(substring(crypto_getencryptlevel(s), 0, 1));
1126         if((q <= 0 && cvar("crypto_aeslevel") >= 3) || (q >= 3 && cvar("crypto_aeslevel") <= 0))
1127         {
1128                 theColor = SKINCOLOR_SERVERLIST_IMPOSSIBLE;
1129                 theAlpha = SKINALPHA_SERVERLIST_IMPOSSIBLE;
1130         }
1131
1132         if(q == 1)
1133         {
1134                 if(cvar("crypto_aeslevel") >= 2)
1135                         q |= 4;
1136         }
1137         if(q == 2)
1138         {
1139                 if(cvar("crypto_aeslevel") >= 1)
1140                         q |= 4;
1141         }
1142         if(q == 3)
1143                 q = 5;
1144         else if(q >= 3)
1145                 q -= 2;
1146         // possible status:
1147         // 0: crypto off
1148         // 1: AES possible
1149         // 2: AES recommended but not available
1150         // 3: AES possible and will be used
1151         // 4: AES recommended and will be used
1152         // 5: AES required
1153
1154         // --------------
1155         //  RENDER ICONS
1156         // --------------
1157         vector iconSize = '0 0 0';
1158         iconSize_y = me.realFontSize_y * me.iconsSizeFactor;
1159         iconSize_x = me.realFontSize_x * me.iconsSizeFactor;
1160
1161         vector iconPos = '0 0 0';
1162         iconPos_x = (me.columnIconsSize - 3 * iconSize_x) * 0.5;
1163         iconPos_y = (1 - iconSize_y) * 0.5;
1164
1165         string n;
1166
1167         if not(me.seenIPv4 && me.seenIPv6)
1168         {
1169                 iconPos_x += iconSize_x * 0.5;
1170         }
1171         else if(me.seenIPv4 && me.seenIPv6)
1172         {
1173                 n = string_null;
1174                 if(isv6)
1175                         draw_PreloadPictureWithFlags(n = strcat(SKINGFX_SERVERLIST_ICON, "_ipv6"), 0); // PRECACHE_PIC_MIPMAP
1176                 else if(isv4)
1177                         draw_PreloadPictureWithFlags(n = strcat(SKINGFX_SERVERLIST_ICON, "_ipv4"), 0); // PRECACHE_PIC_MIPMAP
1178                 if(n)
1179                         draw_Picture(iconPos, n, iconSize, '1 1 1', 1);
1180                 iconPos_x += iconSize_x;
1181         }
1182
1183         if(q > 0)
1184         {
1185                 draw_PreloadPictureWithFlags(n = strcat(SKINGFX_SERVERLIST_ICON, "_aeslevel", ftos(q)), 0); // PRECACHE_PIC_MIPMAP
1186                 draw_Picture(iconPos, n, iconSize, '1 1 1', 1);
1187         }
1188         iconPos_x += iconSize_x;
1189
1190         if(modname == "Xonotic")
1191         {
1192                 if(pure == 0)
1193                 {
1194                         draw_PreloadPictureWithFlags(n = strcat(SKINGFX_SERVERLIST_ICON, "_pure1"), PRECACHE_PIC_MIPMAP);
1195                         draw_Picture(iconPos, n, iconSize, '1 1 1', 1);
1196                 }
1197         }
1198         else
1199         {
1200                 draw_PreloadPictureWithFlags(n = strcat(SKINGFX_SERVERLIST_ICON, "_mod_", modname), PRECACHE_PIC_MIPMAP);
1201                 if(draw_PictureSize(n) == '0 0 0')
1202                         draw_PreloadPictureWithFlags(n = strcat(SKINGFX_SERVERLIST_ICON, "_mod_"), PRECACHE_PIC_MIPMAP);
1203                 if(pure == 0)
1204                         draw_Picture(iconPos, n, iconSize, '1 1 1', 1);
1205                 else
1206                         draw_Picture(iconPos, n, iconSize, '1 1 1', SKINALPHA_SERVERLIST_ICON_NONPURE);
1207         }
1208         iconPos_x += iconSize_x;
1209
1210         if(sflags >= 0 && (sflags & SERVERFLAG_PLAYERSTATS))
1211         {
1212                 draw_PreloadPictureWithFlags(n = strcat(SKINGFX_SERVERLIST_ICON, "_stats1"), 0); // PRECACHE_PIC_MIPMAP
1213                 draw_Picture(iconPos, n, iconSize, '1 1 1', 1);
1214         }
1215         iconPos_x += iconSize_x;
1216         
1217         // --------------
1218         //  RENDER TEXT
1219         // --------------
1220         
1221         // ping
1222         s = ftos(p);
1223         draw_Text(me.realUpperMargin * eY + (me.columnPingOrigin + me.columnPingSize - draw_TextWidth(s, 0, me.realFontSize)) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1224
1225         // server name
1226         s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_NAME, i), me.columnNameSize, 0, me.realFontSize);
1227         draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, theColor, theAlpha, 0);
1228
1229         // server map
1230         s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_MAP, i), me.columnMapSize, 0, me.realFontSize);
1231         draw_Text(me.realUpperMargin * eY + (me.columnMapOrigin + (me.columnMapSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1232
1233         // server gametype
1234         s = draw_TextShortenToWidth(typestr, me.columnTypeSize, 0, me.realFontSize);
1235         draw_Text(me.realUpperMargin * eY + (me.columnTypeOrigin + (me.columnTypeSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1236
1237         // server playercount
1238         s = strcat(ftos(gethostcachenumber(SLIST_FIELD_NUMHUMANS, i)), "/", ftos(gethostcachenumber(SLIST_FIELD_MAXPLAYERS, i)));
1239         draw_Text(me.realUpperMargin * eY + (me.columnPlayersOrigin + (me.columnPlayersSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1240 }
1241
1242 float XonoticServerList_keyDown(entity me, float scan, float ascii, float shift)
1243 {
1244         float i = me.selectedItem;
1245         vector org, sz;
1246
1247         org = boxToGlobal(eY * (me.selectedItem * me.itemHeight - me.scrollPos), me.origin, me.size);
1248         sz = boxToGlobalSize(eY * me.itemHeight + eX * (1 - me.controlWidth), me.size);
1249
1250         me.lastBumpSelectTime = 0;
1251
1252         if(scan == K_ENTER || scan == K_KP_ENTER)
1253         {
1254                 ServerList_Connect_Click(NULL, me);
1255                 return 1;
1256         }
1257         else if(scan == K_MOUSE2 || scan == K_SPACE)
1258         {
1259                 if(me.nItems != 0)
1260                 {
1261                         main.serverInfoDialog.loadServerInfo(main.serverInfoDialog, i);
1262                         DialogOpenButton_Click_withCoords(me, main.serverInfoDialog, org, sz);
1263                         return 1;
1264                 }
1265                 return 0;
1266         }
1267         else if(scan == K_INS || scan == K_MOUSE3 || scan == K_KP_INS)
1268         {
1269                 if(me.nItems != 0)
1270                 {
1271                         me.toggleFavorite(me, me.selectedServer);
1272                         me.ipAddressBoxFocused = -1;
1273                         return 1;
1274                 }
1275                 return 0;
1276         }
1277         else if(SUPER(XonoticServerList).keyDown(me, scan, ascii, shift))
1278                 return 1;
1279         else if(!me.controlledTextbox)
1280                 return 0;
1281         else
1282                 return me.controlledTextbox.keyDown(me.controlledTextbox, scan, ascii, shift);
1283 }
1284
1285 float XonoticServerList_getTotalHeight(entity me) {
1286         float num_normal_rows = me.nItems;
1287         float num_headers = category_draw_count;
1288         return me.itemHeight * (num_normal_rows + me.categoriesHeight * num_headers);
1289 }
1290 float XonoticServerList_getItemAtPos(entity me, float pos) {
1291         pos = pos / me.itemHeight;
1292         float i;
1293         for (i = category_draw_count - 1; i >= 0; --i) {
1294                 float itemidx = category_item[i];
1295                 float itempos = i * me.categoriesHeight + category_item[i];
1296                 if (pos >= itempos + me.categoriesHeight + 1)
1297                         return itemidx + 1 + floor(pos - (itempos + me.categoriesHeight + 1));
1298                 if (pos >= itempos)
1299                         return itemidx;
1300         }
1301         // No category matches? Note that category 0 is... 0. Therefore no headings exist at all.
1302         return floor(pos);
1303 }
1304 float XonoticServerList_getItemStart(entity me, float item) {
1305         float i;
1306         for (i = category_draw_count - 1; i >= 0; --i) {
1307                 float itemidx = category_item[i];
1308                 float itempos = i * me.categoriesHeight + category_item[i];
1309                 if (item >= itemidx + 1)
1310                         return (itempos + me.categoriesHeight + 1 + item - (itemidx + 1)) * me.itemHeight;
1311                 if (item >= itemidx)
1312                         return itempos * me.itemHeight;
1313         }
1314         // No category matches? Note that category 0 is... 0. Therefore no headings exist at all.
1315         return item * me.itemHeight;
1316 }
1317 float XonoticServerList_getItemHeight(entity me, float item) {
1318         float i;
1319         for (i = 0; i < category_draw_count; ++i) {
1320                 // Matches exactly the headings with increased height.
1321                 if (item == category_item[i])
1322                         return me.itemHeight * (me.categoriesHeight + 1);
1323         }
1324         return me.itemHeight;
1325 }
1326
1327 #endif