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