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