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