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