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