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