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