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