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