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