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