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