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