2 CLASS(XonoticServerList) EXTENDS(XonoticListBox)
3 METHOD(XonoticServerList, configureXonoticServerList, void(entity))
4 ATTRIB(XonoticServerList, rowsPerItem, float, 1)
5 METHOD(XonoticServerList, draw, void(entity))
6 METHOD(XonoticServerList, drawListBoxItem, void(entity, float, vector, float))
7 METHOD(XonoticServerList, clickListBoxItem, void(entity, float, vector))
8 METHOD(XonoticServerList, resizeNotify, void(entity, vector, vector, vector, vector))
9 METHOD(XonoticServerList, keyDown, float(entity, float, float, float))
10 METHOD(XonoticServerList, toggleFavorite, void(entity, string))
12 ATTRIB(XonoticServerList, iconsSizeFactor, float, 0.85)
14 ATTRIB(XonoticServerList, realFontSize, vector, '0 0 0')
15 ATTRIB(XonoticServerList, realUpperMargin, float, 0)
16 ATTRIB(XonoticServerList, columnIconsOrigin, float, 0)
17 ATTRIB(XonoticServerList, columnIconsSize, float, 0)
18 ATTRIB(XonoticServerList, columnPingOrigin, float, 0)
19 ATTRIB(XonoticServerList, columnPingSize, float, 0)
20 ATTRIB(XonoticServerList, columnNameOrigin, float, 0)
21 ATTRIB(XonoticServerList, columnNameSize, float, 0)
22 ATTRIB(XonoticServerList, columnMapOrigin, float, 0)
23 ATTRIB(XonoticServerList, columnMapSize, float, 0)
24 ATTRIB(XonoticServerList, columnTypeOrigin, float, 0)
25 ATTRIB(XonoticServerList, columnTypeSize, float, 0)
26 ATTRIB(XonoticServerList, columnPlayersOrigin, float, 0)
27 ATTRIB(XonoticServerList, columnPlayersSize, float, 0)
29 ATTRIB(XonoticServerList, selectedServer, string, string_null) // to restore selected server when needed
30 METHOD(XonoticServerList, setSelected, void(entity, float))
31 METHOD(XonoticServerList, setSortOrder, void(entity, float, float))
32 ATTRIB(XonoticServerList, filterShowEmpty, float, 1)
33 ATTRIB(XonoticServerList, filterShowFull, float, 1)
34 ATTRIB(XonoticServerList, filterString, string, string_null)
35 ATTRIB(XonoticServerList, controlledTextbox, entity, NULL)
36 ATTRIB(XonoticServerList, ipAddressBox, entity, NULL)
37 ATTRIB(XonoticServerList, favoriteButton, entity, NULL)
38 ATTRIB(XonoticServerList, nextRefreshTime, float, 0)
39 METHOD(XonoticServerList, refreshServerList, void(entity, float)) // refresh mode: REFRESHSERVERLIST_*
40 ATTRIB(XonoticServerList, needsRefresh, float, 1)
41 METHOD(XonoticServerList, focusEnter, void(entity))
42 METHOD(XonoticServerList, positionSortButton, void(entity, entity, float, float, string, void(entity, entity)))
43 ATTRIB(XonoticServerList, sortButton1, entity, NULL)
44 ATTRIB(XonoticServerList, sortButton2, entity, NULL)
45 ATTRIB(XonoticServerList, sortButton3, entity, NULL)
46 ATTRIB(XonoticServerList, sortButton4, entity, NULL)
47 ATTRIB(XonoticServerList, sortButton5, entity, NULL)
48 ATTRIB(XonoticServerList, connectButton, entity, NULL)
49 ATTRIB(XonoticServerList, infoButton, entity, NULL)
50 ATTRIB(XonoticServerList, currentSortOrder, float, 0)
51 ATTRIB(XonoticServerList, currentSortField, float, -1)
52 ATTRIB(XonoticServerList, lastBumpSelectTime, float, 0)
53 ATTRIB(XonoticServerList, lastClickedServer, float, -1)
54 ATTRIB(XonoticServerList, lastClickedTime, float, 0)
56 ATTRIB(XonoticServerList, ipAddressBoxFocused, float, -1)
58 ATTRIB(XonoticServerList, seenIPv4, float, 0)
59 ATTRIB(XonoticServerList, seenIPv6, float, 0)
60 ATTRIB(XonoticServerList, categoriesHeight, float, 1.5)
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();
69 #ifndef IMPLEMENTATION
70 var float autocvar_menu_slist_categories = TRUE;
71 var float autocvar_menu_slist_categories_onlyifmultiple = TRUE;
72 var float autocvar_menu_slist_purethreshold = 10;
73 var float autocvar_menu_slist_modimpurity = 10;
74 var float autocvar_menu_slist_recommendations = 2;
75 var float autocvar_menu_slist_recommendations_maxping = 150;
76 var float autocvar_menu_slist_recommendations_minfreeslots = 1;
77 var float autocvar_menu_slist_recommendations_minhumans = 1;
78 var float autocvar_menu_slist_recommendations_purethreshold = -1;
79 //var string autocvar_menu_slist_recommended = "76.124.107.5:26004";
81 // server cache fields
82 #define SLIST_FIELDS \
83 SLIST_FIELD(CNAME, "cname") \
84 SLIST_FIELD(PING, "ping") \
85 SLIST_FIELD(GAME, "game") \
86 SLIST_FIELD(MOD, "mod") \
87 SLIST_FIELD(MAP, "map") \
88 SLIST_FIELD(NAME, "name") \
89 SLIST_FIELD(MAXPLAYERS, "maxplayers") \
90 SLIST_FIELD(NUMPLAYERS, "numplayers") \
91 SLIST_FIELD(NUMHUMANS, "numhumans") \
92 SLIST_FIELD(NUMBOTS, "numbots") \
93 SLIST_FIELD(PROTOCOL, "protocol") \
94 SLIST_FIELD(FREESLOTS, "freeslots") \
95 SLIST_FIELD(PLAYERS, "players") \
96 SLIST_FIELD(QCSTATUS, "qcstatus") \
97 SLIST_FIELD(CATEGORY, "category") \
98 SLIST_FIELD(ISFAVORITE, "isfavorite")
100 #define SLIST_FIELD(suffix,name) float SLIST_FIELD_##suffix;
104 const float REFRESHSERVERLIST_RESORT = 0; // sort the server list again to update for changes to e.g. favorite status, categories
105 const float REFRESHSERVERLIST_REFILTER = 1; // ..., also update filter and sort criteria
106 const float REFRESHSERVERLIST_ASK = 2; // ..., also suggest querying servers now
107 const float REFRESHSERVERLIST_RESET = 3; // ..., also clear the list first
109 // function declarations
110 entity RetrieveCategoryEnt(float catnum);
112 float IsServerInList(string list, string srv);
113 #define IsFavorite(srv) IsServerInList(cvar_string("net_slist_favorites"), srv)
114 #define IsRecommended(srv) IsServerInList(_Nex_ExtResponseSystem_RecommendedServers, srv)
116 float CheckCategoryOverride(float cat);
117 float CheckCategoryForEntry(float entry);
118 float m_gethostcachecategory(float entry) { return CheckCategoryOverride(CheckCategoryForEntry(entry)); }
120 void RegisterSLCategories();
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);
131 // fields for category entities
132 #define MAX_CATEGORIES 9
133 #define CATEGORY_FIRST 1
134 entity categories[MAX_CATEGORIES];
135 float category_ent_count;
138 .string cat_enoverride_string;
139 .string cat_dioverride_string;
140 .float cat_enoverride;
141 .float cat_dioverride;
143 // fields for drawing categories
144 float category_name[MAX_CATEGORIES];
145 float category_item[MAX_CATEGORIES];
146 float category_draw_count;
148 #define SLIST_CATEGORIES \
149 SLIST_CATEGORY(CAT_FAVORITED, "", "", ZCTX(_("SLCAT^Favorites"))) \
150 SLIST_CATEGORY(CAT_RECOMMENDED, "", "CAT_SERVERS", 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_MINSTAGIB, "", "CAT_SERVERS", ZCTX(_("SLCAT^MinstaGib Mode"))) \
157 SLIST_CATEGORY(CAT_DEFRAG, "", "CAT_SERVERS", ZCTX(_("SLCAT^Defrag Mode")))
159 #define SLIST_CATEGORY_AUTOCVAR(name) autocvar_menu_slist_categories_##name##_override
160 #define SLIST_CATEGORY(name,enoverride,dioverride,str) \
162 var string SLIST_CATEGORY_AUTOCVAR(name) = enoverride;
164 #undef SLIST_CATEGORY
168 #ifdef IMPLEMENTATION
170 void RegisterSLCategories()
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") \
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);
184 #undef SLIST_CATEGORY
189 #define PROCESS_OVERRIDE(override_string,override_field) \
190 for(i = 0; i < category_ent_count; ++i) \
192 s = categories[i].override_string; \
193 if((s != "") && (s != categories[i].cat_name)) \
196 for(x = 0; x < category_ent_count; ++x) \
197 { if(categories[x].cat_name == s) { \
203 strunzone(categories[i].override_string); \
204 categories[i].override_field = catnum; \
210 "RegisterSLCategories(): Improper override '%s' for category '%s'!\n", \
212 categories[i].cat_name \
216 strunzone(categories[i].override_string); \
217 categories[i].override_field = 0; \
219 PROCESS_OVERRIDE(cat_enoverride_string, cat_enoverride)
220 PROCESS_OVERRIDE(cat_dioverride_string, cat_dioverride)
221 #undef PROCESS_OVERRIDE
224 // Supporting Functions
225 entity RetrieveCategoryEnt(float catnum)
227 if((catnum > 0) && (catnum <= category_ent_count))
229 return categories[catnum - 1];
233 error(sprintf("RetrieveCategoryEnt(%d): Improper category number!\n", catnum));
238 float IsServerInList(string list, string srv)
244 srv = netaddress_resolve(srv, 26000);
247 p = crypto_getidfp(srv);
248 n = tokenize_console(list);
249 for(i = 0; i < n; ++i)
251 if(substring(argv(i), 0, 1) != "[" && strlen(argv(i)) == 44 && strstrofs(argv(i), ".", 0) < 0)
259 if(srv == netaddress_resolve(argv(i), 26000))
266 float CheckCategoryOverride(float cat)
268 entity catent = RetrieveCategoryEnt(cat);
271 float override = (autocvar_menu_slist_categories ? catent.cat_enoverride : catent.cat_dioverride);
272 if(override) { return override; }
277 error(sprintf("CheckCategoryOverride(%d): Improper category number!\n", cat));
282 float CheckCategoryForEntry(float entry)
284 string s, k, v, modtype = "";
285 float j, m, impure = 0, freeslots = 0, sflags = 0;
286 s = gethostcachestring(SLIST_FIELD_QCSTATUS, entry);
287 m = tokenizebyseparator(s, ":");
289 for(j = 2; j < m; ++j)
291 if(argv(j) == "") { break; }
292 k = substring(argv(j), 0, 1);
293 v = substring(argv(j), 1, -1);
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; }
303 if(modtype != "xonotic") { impure += autocvar_menu_slist_modimpurity; }
305 // check if this server is favorited
306 if(gethostcachenumber(SLIST_FIELD_ISFAVORITE, entry)) { return CAT_FAVORITED; }
308 // now check if it's recommended
309 if(autocvar_menu_slist_recommendations)
311 float recommended = 0;
312 if(autocvar_menu_slist_recommendations & 1)
314 if(IsRecommended(gethostcachestring(SLIST_FIELD_CNAME, entry)))
319 if(autocvar_menu_slist_recommendations & 2)
322 (freeslots >= autocvar_menu_slist_recommendations_minfreeslots)
325 (autocvar_menu_slist_recommendations_purethreshold < 0)
327 (impure <= autocvar_menu_slist_recommendations_purethreshold)
331 gethostcachenumber(SLIST_FIELD_NUMHUMANS, entry)
333 autocvar_menu_slist_recommendations_minhumans
337 gethostcachenumber(SLIST_FIELD_PING, entry)
339 autocvar_menu_slist_recommendations_maxping
346 if(recommended > 0) { return CAT_RECOMMENDED; }
349 // if not favorited or recommended, check modname
350 if(modtype != "xonotic")
354 // old servers which don't report their mod name are considered modified now
355 case "": { return CAT_MODIFIED; }
357 case "xpm": { return CAT_XPM; }
358 case "minstagib": { return CAT_MINSTAGIB; }
359 case "overkill": { return CAT_OVERKILL; }
360 //case "nix": { return CAT_NIX; }
361 //case "newtoys": { return CAT_NEWTOYS; }
363 // "cts" is allowed as compat, xdf is replacement
365 case "xdf": { return CAT_DEFRAG; }
367 default: { dprint(sprintf("Found strange mod type: %s\n", modtype)); return CAT_MODIFIED; }
371 // must be normal or impure server
372 return ((impure > autocvar_menu_slist_purethreshold) ? CAT_MODIFIED : CAT_NORMAL);
375 float CheckItemNumber(float num)
379 if not(category_draw_count) { return num; } // there are no categories to process
381 for(i = 0, n = 1; n <= category_draw_count; ++i, ++n)
383 if(category_item[i] == (num - i)) { return -category_name[i]; }
384 else if(n == category_draw_count) { return (num - n); }
385 else if((num - i) <= category_item[n]) { return (num - n); }
388 // should never hit this point
389 error(sprintf("CheckItemNumber(%d): Function fell through without normal return!\n", num));
393 void XonoticServerList_toggleFavorite(entity me, string srv)
395 string s, s0, s1, s2, srv_resolved, p;
397 srv_resolved = netaddress_resolve(srv, 26000);
398 p = crypto_getidfp(srv_resolved);
399 s = cvar_string("net_slist_favorites");
400 n = tokenize_console(s);
402 for(i = 0; i < n; ++i)
404 if(substring(argv(i), 0, 1) != "[" && strlen(argv(i)) == 44 && strstrofs(argv(i), ".", 0) < 0)
412 if(srv_resolved != netaddress_resolve(argv(i), 26000))
417 s0 = substring(s, 0, argv_end_index(i - 1));
419 s2 = substring(s, argv_start_index(i + 1), -1);
420 if(s0 != "" && s2 != "")
422 cvar_set("net_slist_favorites", strcat(s0, s1, s2));
423 s = cvar_string("net_slist_favorites");
424 n = tokenize_console(s);
435 cvar_set("net_slist_favorites", strcat(s, s1, p));
437 cvar_set("net_slist_favorites", strcat(s, s1, srv));
440 me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
443 void ServerList_Update_favoriteButton(entity btn, entity me)
445 me.favoriteButton.setText(me.favoriteButton,
446 (IsFavorite(me.ipAddressBox.text) ?
447 _("Remove") : _("Bookmark")
452 entity makeXonoticServerList()
455 me = spawnXonoticServerList();
456 me.configureXonoticServerList(me);
459 void XonoticServerList_configureXonoticServerList(entity me)
461 me.configureXonoticListBox(me);
464 #define SLIST_FIELD(suffix,name) SLIST_FIELD_##suffix = gethostcacheindexforkey(name);
471 void XonoticServerList_setSelected(entity me, float i)
473 // todo: add logic to skip categories
475 save = me.selectedItem;
476 SUPER(XonoticServerList).setSelected(me, i);
478 if(me.selectedItem == save)
484 //if(gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT) != CheckItemNumber(me.nItems))
485 // { error("^1XonoticServerList_setSelected(); ERROR: ^7Host cache viewcount mismatches nItems!\n"); return; } // sorry, it would be wrong
486 // ^ todo: make this work somehow?
488 #define SET_SELECTED_SERVER(cachenum) \
489 if(me.selectedServer) { strunzone(me.selectedServer); } \
490 me.selectedServer = strzone(gethostcachestring(SLIST_FIELD_CNAME, cachenum)); \
491 me.ipAddressBox.setText(me.ipAddressBox, me.selectedServer); \
492 me.ipAddressBox.cursorPos = strlen(me.selectedServer); \
493 me.ipAddressBoxFocused = -1; \
496 num = CheckItemNumber(me.selectedItem);
498 if(num >= 0) { SET_SELECTED_SERVER(num); }
499 else if(save > me.selectedItem)
501 if(me.selectedItem == 0) { return; }
504 if(me.lastClickedTime >= me.lastBumpSelectTime)
506 SUPER(XonoticServerList).setSelected(me, me.selectedItem - 1);
507 num = CheckItemNumber(me.selectedItem);
510 me.lastBumpSelectTime = time;
511 SET_SELECTED_SERVER(num);
516 else if(save < me.selectedItem)
518 if(me.selectedItem == me.nItems) { return; }
521 if(me.lastClickedTime >= me.lastBumpSelectTime)
523 SUPER(XonoticServerList).setSelected(me, me.selectedItem + 1);
524 num = CheckItemNumber(me.selectedItem);
527 me.lastBumpSelectTime = time;
528 SET_SELECTED_SERVER(num);
535 void XonoticServerList_refreshServerList(entity me, float mode)
537 //print("refresh of type ", ftos(mode), "\n");
539 if(mode >= REFRESHSERVERLIST_REFILTER)
543 string s, typestr, modstr;
547 m = strstrofs(s, ":", 0);
550 typestr = substring(s, 0, m);
551 s = substring(s, m + 1, strlen(s) - m - 1);
552 while(substring(s, 0, 1) == " ")
553 s = substring(s, 1, strlen(s) - 1);
558 modstr = cvar_string("menu_slist_modfilter");
560 m = SLIST_MASK_AND - 1;
561 resethostcachemasks();
563 // ping: reject negative ping (no idea why this happens in the first place, engine bug)
564 sethostcachemasknumber(++m, SLIST_FIELD_PING, 0, SLIST_TEST_GREATEREQUAL);
567 if(!me.filterShowFull)
569 sethostcachemasknumber(++m, SLIST_FIELD_FREESLOTS, 1, SLIST_TEST_GREATEREQUAL); // legacy
570 sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, ":S0:", SLIST_TEST_NOTCONTAIN); // g_maxplayers support
574 if(!me.filterShowEmpty)
575 sethostcachemasknumber(++m, SLIST_FIELD_NUMHUMANS, 1, SLIST_TEST_GREATEREQUAL);
577 // gametype filtering
579 sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, strcat(typestr, ":"), SLIST_TEST_STARTSWITH);
584 if(substring(modstr, 0, 1) == "!")
585 sethostcachemaskstring(++m, SLIST_FIELD_MOD, resolvemod(substring(modstr, 1, strlen(modstr) - 1)), SLIST_TEST_NOTEQUAL);
587 sethostcachemaskstring(++m, SLIST_FIELD_MOD, resolvemod(modstr), SLIST_TEST_EQUAL);
591 n = tokenizebyseparator(_Nex_ExtResponseSystem_BannedServers, " ");
592 for(i = 0; i < n; ++i)
594 sethostcachemaskstring(++m, SLIST_FIELD_CNAME, argv(i), SLIST_TEST_NOTSTARTSWITH);
596 m = SLIST_MASK_OR - 1;
599 sethostcachemaskstring(++m, SLIST_FIELD_NAME, s, SLIST_TEST_CONTAINS);
600 sethostcachemaskstring(++m, SLIST_FIELD_MAP, s, SLIST_TEST_CONTAINS);
601 sethostcachemaskstring(++m, SLIST_FIELD_PLAYERS, s, SLIST_TEST_CONTAINS);
602 sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, strcat(s, ":"), SLIST_TEST_STARTSWITH);
606 //listflags |= SLSF_FAVORITES;
607 listflags |= SLSF_CATEGORIES;
608 if(me.currentSortOrder < 0) { listflags |= SLSF_DESCENDING; }
609 sethostcachesort(me.currentSortField, listflags);
613 if(mode >= REFRESHSERVERLIST_ASK)
614 refreshhostcache(mode >= REFRESHSERVERLIST_RESET);
616 void XonoticServerList_focusEnter(entity me)
618 if(time < me.nextRefreshTime)
620 //print("sorry, no refresh yet\n");
623 me.nextRefreshTime = time + 10;
624 me.refreshServerList(me, REFRESHSERVERLIST_ASK);
627 void XonoticServerList_draw(entity me)
629 float i, found, owned, num;
631 if(_Nex_ExtResponseSystem_BannedServersNeedsRefresh)
635 _Nex_ExtResponseSystem_BannedServersNeedsRefresh = 0;
638 if(_Nex_ExtResponseSystem_RecommendedServersNeedsRefresh)
642 _Nex_ExtResponseSystem_RecommendedServersNeedsRefresh = 0;
645 if(me.currentSortField == -1)
647 me.setSortOrder(me, SLIST_FIELD_PING, +1);
648 me.refreshServerList(me, REFRESHSERVERLIST_RESET);
650 else if(me.needsRefresh == 1)
652 me.needsRefresh = 2; // delay by one frame to make sure "slist" has been executed
654 else if(me.needsRefresh == 2)
657 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
659 else if(me.needsRefresh == 3)
662 me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
665 owned = ((me.selectedServer == me.ipAddressBox.text) && (me.ipAddressBox.text != ""));
667 for(i = 0; i < category_draw_count; ++i) { category_name[i] = -1; category_item[i] = -1; }
668 category_draw_count = 0;
670 if(autocvar_menu_slist_categories >= 0) // if less than 0, don't even draw a category heading for favorites
672 float itemcount = gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT);
673 me.nItems = itemcount;
675 //float visible = floor(me.scrollPos / me.itemHeight);
676 // ^ unfortunately no such optimization can be made-- we must process through the
677 // entire list, otherwise there is no way to know which item is first in its category.
679 // binary search method suggested by div
682 for(x = 1; x <= category_ent_count; ++x) {
684 float last = (itemcount - 1);
689 float catf = gethostcachenumber(SLIST_FIELD_CATEGORY, first);
690 float catl = gethostcachenumber(SLIST_FIELD_CATEGORY, last);
692 // The first one is already > x.
693 // Therefore, category x does not exist.
694 // Higher numbered categories do exist though.
695 } else if (catl < x) {
696 // The last one is < x.
697 // Thus this category - and any following -
700 } else if (catf == x) {
701 // Starts at first. This breaks the loop
702 // invariant in the binary search and thus has
703 // to be handled separately.
704 print(sprintf("hit: x='%d', dc='%d', first='%d', last='%d', catf='%d', catl='%d'.\n", x, category_draw_count, first, last, catf, catl));
705 if(gethostcachenumber(SLIST_FIELD_CATEGORY, first) != x)
706 error("Category mismatch I");
708 if(gethostcachenumber(SLIST_FIELD_CATEGORY, first - 1) == x)
709 error("Category mismatch II");
710 category_name[category_draw_count] = x;
711 category_item[category_draw_count] = first;
712 ++category_draw_count;
716 // At this point, catf <= x < catl, thus
717 // catf < catl, thus first < last.
720 // catf == gethostcachenumber(SLIST_FIELD_CATEGORY(first)
721 // catl == gethostcachenumber(SLIST_FIELD_CATEGORY(last)
724 while (last - first > 1) {
725 float middle = floor((first + last) / 2);
726 // By loop condition, middle != first && middle != last.
727 float cat = gethostcachenumber(SLIST_FIELD_CATEGORY, middle);
737 print(sprintf("hit: x='%d', dc='%d', first='%d', last='%d', catf='%d', catl='%d'.\n", x, category_draw_count, first, last, catf, catl));
738 if(gethostcachenumber(SLIST_FIELD_CATEGORY, last) != x)
739 error("Category mismatch III");
741 if(gethostcachenumber(SLIST_FIELD_CATEGORY, last - 1) == x)
742 error("Category mismatch IV");
743 category_name[category_draw_count] = x;
744 category_item[category_draw_count] = last;
745 ++category_draw_count;
747 begin = last + 1; // already scanned through these, skip 'em
750 begin = last; // already scanned through these, skip 'em
754 if(autocvar_menu_slist_categories_onlyifmultiple && (category_draw_count == 1))
756 category_name[0] = -1;
757 category_item[0] = -1;
758 category_draw_count = 0;
759 me.nItems = itemcount;
762 else { me.nItems = gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT); }
764 me.connectButton.disabled = ((me.nItems == 0) && (me.ipAddressBox.text == ""));
765 me.infoButton.disabled = ((me.nItems == 0) || !owned);
766 me.favoriteButton.disabled = ((me.nItems == 0) && (me.ipAddressBox.text == ""));
769 if(me.selectedServer)
771 for(i = 0; i < me.nItems; ++i)
773 num = CheckItemNumber(i);
776 if(gethostcachestring(SLIST_FIELD_CNAME, num) == me.selectedServer)
778 if(i != me.selectedItem)
780 me.lastClickedServer = -1;
793 if(me.selectedItem >= me.nItems) { me.selectedItem = me.nItems - 1; }
794 if(me.selectedServer) { strunzone(me.selectedServer); }
796 num = CheckItemNumber(me.selectedItem);
797 if(num >= 0) { me.selectedServer = strzone(gethostcachestring(SLIST_FIELD_CNAME, num)); }
803 if(me.selectedServer != me.ipAddressBox.text)
805 me.ipAddressBox.setText(me.ipAddressBox, me.selectedServer);
806 me.ipAddressBox.cursorPos = strlen(me.selectedServer);
807 me.ipAddressBoxFocused = -1;
811 if(me.ipAddressBoxFocused != me.ipAddressBox.focused)
813 if(me.ipAddressBox.focused || me.ipAddressBoxFocused < 0)
814 ServerList_Update_favoriteButton(NULL, me);
815 me.ipAddressBoxFocused = me.ipAddressBox.focused;
818 SUPER(XonoticServerList).draw(me);
820 void ServerList_PingSort_Click(entity btn, entity me)
822 me.setSortOrder(me, SLIST_FIELD_PING, +1);
824 void ServerList_NameSort_Click(entity btn, entity me)
826 me.setSortOrder(me, SLIST_FIELD_NAME, -1); // why?
828 void ServerList_MapSort_Click(entity btn, entity me)
830 me.setSortOrder(me, SLIST_FIELD_MAP, -1); // why?
832 void ServerList_PlayerSort_Click(entity btn, entity me)
834 me.setSortOrder(me, SLIST_FIELD_NUMHUMANS, -1);
836 void ServerList_TypeSort_Click(entity btn, entity me)
841 m = strstrofs(s, ":", 0);
844 s = substring(s, 0, m);
845 while(substring(s, m+1, 1) == " ") // skip spaces
851 for(i = 1; ; i *= 2) // 20 modes ought to be enough for anyone
853 t = MapInfo_Type_ToString(i);
855 if(t == "") // it repeats (default case)
858 // choose the first one
859 s = MapInfo_Type_ToString(1);
864 // the type was found
865 // choose the next one
866 s = MapInfo_Type_ToString(i * 2);
868 s = MapInfo_Type_ToString(1);
875 s = strcat(s, substring(me.filterString, m+1, strlen(me.filterString) - m - 1));
877 me.controlledTextbox.setText(me.controlledTextbox, s);
878 me.controlledTextbox.keyDown(me.controlledTextbox, K_END, 0, 0);
879 me.controlledTextbox.keyUp(me.controlledTextbox, K_END, 0, 0);
880 //ServerList_Filter_Change(me.controlledTextbox, me);
882 void ServerList_Filter_Change(entity box, entity me)
885 strunzone(me.filterString);
887 me.filterString = strzone(box.text);
889 me.filterString = string_null;
890 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
892 me.ipAddressBox.setText(me.ipAddressBox, "");
893 me.ipAddressBox.cursorPos = 0;
894 me.ipAddressBoxFocused = -1;
896 void ServerList_Categories_Click(entity box, entity me)
898 box.setChecked(box, autocvar_menu_slist_categories = !autocvar_menu_slist_categories);
899 me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
901 me.ipAddressBox.setText(me.ipAddressBox, "");
902 me.ipAddressBox.cursorPos = 0;
903 me.ipAddressBoxFocused = -1;
905 void ServerList_ShowEmpty_Click(entity box, entity me)
907 box.setChecked(box, me.filterShowEmpty = !me.filterShowEmpty);
908 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
910 me.ipAddressBox.setText(me.ipAddressBox, "");
911 me.ipAddressBox.cursorPos = 0;
912 me.ipAddressBoxFocused = -1;
914 void ServerList_ShowFull_Click(entity box, entity me)
916 box.setChecked(box, me.filterShowFull = !me.filterShowFull);
917 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
919 me.ipAddressBox.setText(me.ipAddressBox, "");
920 me.ipAddressBox.cursorPos = 0;
921 me.ipAddressBoxFocused = -1;
923 void XonoticServerList_setSortOrder(entity me, float fld, float direction)
925 if(me.currentSortField == fld)
926 direction = -me.currentSortOrder;
927 me.currentSortOrder = direction;
928 me.currentSortField = fld;
929 me.sortButton1.forcePressed = (fld == SLIST_FIELD_PING);
930 me.sortButton2.forcePressed = (fld == SLIST_FIELD_NAME);
931 me.sortButton3.forcePressed = (fld == SLIST_FIELD_MAP);
932 me.sortButton4.forcePressed = 0;
933 me.sortButton5.forcePressed = (fld == SLIST_FIELD_NUMHUMANS);
935 if(me.selectedServer)
936 strunzone(me.selectedServer);
937 me.selectedServer = string_null;
938 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
940 void XonoticServerList_positionSortButton(entity me, entity btn, float theOrigin, float theSize, string theTitle, void(entity, entity) theFunc)
942 vector originInLBSpace, sizeInLBSpace;
943 originInLBSpace = eY * (-me.itemHeight);
944 sizeInLBSpace = eY * me.itemHeight + eX * (1 - me.controlWidth);
946 vector originInDialogSpace, sizeInDialogSpace;
947 originInDialogSpace = boxToGlobal(originInLBSpace, me.Container_origin, me.Container_size);
948 sizeInDialogSpace = boxToGlobalSize(sizeInLBSpace, me.Container_size);
950 btn.Container_origin_x = originInDialogSpace_x + sizeInDialogSpace_x * theOrigin;
951 btn.Container_size_x = sizeInDialogSpace_x * theSize;
952 btn.setText(btn, theTitle);
953 btn.onClick = theFunc;
954 btn.onClickEntity = me;
957 void XonoticServerList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
959 SUPER(XonoticServerList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
961 me.realFontSize_y = me.fontSize / (absSize_y * me.itemHeight);
962 me.realFontSize_x = me.fontSize / (absSize_x * (1 - me.controlWidth));
963 me.realUpperMargin = 0.5 * (1 - me.realFontSize_y);
965 me.columnIconsOrigin = 0;
966 me.columnIconsSize = me.realFontSize_x * 4 * me.iconsSizeFactor;
967 me.columnPingSize = me.realFontSize_x * 3;
968 me.columnMapSize = me.realFontSize_x * 10;
969 me.columnTypeSize = me.realFontSize_x * 4;
970 me.columnPlayersSize = me.realFontSize_x * 5;
971 me.columnNameSize = 1 - me.columnPlayersSize - me.columnMapSize - me.columnPingSize - me.columnIconsSize - me.columnTypeSize - 5 * me.realFontSize_x;
972 me.columnPingOrigin = me.columnIconsOrigin + me.columnIconsSize + me.realFontSize_x;
973 me.columnNameOrigin = me.columnPingOrigin + me.columnPingSize + me.realFontSize_x;
974 me.columnMapOrigin = me.columnNameOrigin + me.columnNameSize + me.realFontSize_x;
975 me.columnTypeOrigin = me.columnMapOrigin + me.columnMapSize + me.realFontSize_x;
976 me.columnPlayersOrigin = me.columnTypeOrigin + me.columnTypeSize + me.realFontSize_x;
978 me.positionSortButton(me, me.sortButton1, me.columnPingOrigin, me.columnPingSize, _("Ping"), ServerList_PingSort_Click);
979 me.positionSortButton(me, me.sortButton2, me.columnNameOrigin, me.columnNameSize, _("Host name"), ServerList_NameSort_Click);
980 me.positionSortButton(me, me.sortButton3, me.columnMapOrigin, me.columnMapSize, _("Map"), ServerList_MapSort_Click);
981 me.positionSortButton(me, me.sortButton4, me.columnTypeOrigin, me.columnTypeSize, _("Type"), ServerList_TypeSort_Click);
982 me.positionSortButton(me, me.sortButton5, me.columnPlayersOrigin, me.columnPlayersSize, _("Players"), ServerList_PlayerSort_Click);
985 f = me.currentSortField;
988 me.currentSortField = -1;
989 me.setSortOrder(me, f, me.currentSortOrder); // force resetting the sort order
992 void ServerList_Connect_Click(entity btn, entity me)
994 localcmd(sprintf("connect %s\n",
995 ((me.ipAddressBox.text != "") ?
996 me.ipAddressBox.text : me.selectedServer
1000 void ServerList_Favorite_Click(entity btn, entity me)
1003 ipstr = netaddress_resolve(me.ipAddressBox.text, 26000);
1006 me.toggleFavorite(me, me.ipAddressBox.text);
1007 me.ipAddressBoxFocused = -1;
1010 void ServerList_Info_Click(entity btn, entity me)
1012 main.serverInfoDialog.loadServerInfo(main.serverInfoDialog, CheckItemNumber(me.selectedItem));
1013 DialogOpenButton_Click(me, main.serverInfoDialog);
1015 void XonoticServerList_clickListBoxItem(entity me, float i, vector where)
1017 float num = CheckItemNumber(i);
1020 if(num == me.lastClickedServer)
1021 if(time < me.lastClickedTime + 0.3)
1024 ServerList_Connect_Click(NULL, me);
1026 me.lastClickedServer = num;
1027 me.lastClickedTime = time;
1030 void XonoticServerList_drawListBoxItem(entity me, float i, vector absSize, float isSelected)
1032 // layout: Ping, Server name, Map name, NP, TP, MP
1037 float m, pure, freeslots, j, sflags;
1038 string s, typestr, versionstr, k, v, modname;
1040 float item = CheckItemNumber(i);
1041 //print(sprintf("time: %f, i: %d, item: %d, nitems: %d\n", time, i, item, me.nItems));
1043 vector oldscale = draw_scale;
1044 vector oldshift = draw_shift;
1045 #define SET_YRANGE(start,end) \
1046 draw_scale = boxToGlobalSize(eX * 1 + eY * (end - start), oldscale); \
1047 draw_shift = boxToGlobal(eY * start, oldshift, oldscale);
1051 entity catent = RetrieveCategoryEnt(-item);
1054 float delta = (1 - 1 / me.categoriesHeight);
1055 SET_YRANGE(delta, 1); // bottom align
1057 eY * me.realUpperMargin
1059 eX * (me.columnNameOrigin + (me.columnNameSize - draw_TextWidth(catent.cat_string, 0, me.realFontSize)) * 0.5),
1072 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
1074 s = gethostcachestring(SLIST_FIELD_QCSTATUS, item);
1075 m = tokenizebyseparator(s, ":");
1080 versionstr = argv(1);
1086 for(j = 2; j < m; ++j)
1090 k = substring(argv(j), 0, 1);
1091 v = substring(argv(j), 1, -1);
1095 freeslots = stof(v);
1102 #ifdef COMPAT_NO_MOD_IS_XONOTIC
1104 modname = "Xonotic";
1108 SLIST_FIELD_MOD = gethostcacheindexforkey("mod");
1109 s = gethostcachestring(SLIST_FIELD_MOD, item);
1111 if(modname == "Xonotic")
1115 // list the mods here on which the pure server check actually works
1116 if(modname != "Xonotic")
1117 if(modname != "MinstaGib")
1118 if(modname != "CTS")
1119 if(modname != "NIX")
1120 if(modname != "NewToys")
1123 if(gethostcachenumber(SLIST_FIELD_FREESLOTS, item) <= 0)
1124 theAlpha = SKINALPHA_SERVERLIST_FULL;
1125 else if(freeslots == 0)
1126 theAlpha = SKINALPHA_SERVERLIST_FULL; // g_maxplayers support
1127 else if not(gethostcachenumber(SLIST_FIELD_NUMHUMANS, item))
1128 theAlpha = SKINALPHA_SERVERLIST_EMPTY;
1132 p = gethostcachenumber(SLIST_FIELD_PING, item);
1134 #define PING_MED 200
1135 #define PING_HIGH 500
1137 theColor = SKINCOLOR_SERVERLIST_LOWPING + (SKINCOLOR_SERVERLIST_MEDPING - SKINCOLOR_SERVERLIST_LOWPING) * (p / PING_LOW);
1138 else if(p < PING_MED)
1139 theColor = SKINCOLOR_SERVERLIST_MEDPING + (SKINCOLOR_SERVERLIST_HIGHPING - SKINCOLOR_SERVERLIST_MEDPING) * ((p - PING_LOW) / (PING_MED - PING_LOW));
1140 else if(p < PING_HIGH)
1142 theColor = SKINCOLOR_SERVERLIST_HIGHPING;
1143 theAlpha *= 1 + (SKINALPHA_SERVERLIST_HIGHPING - 1) * ((p - PING_MED) / (PING_HIGH - PING_MED));
1148 theAlpha *= SKINALPHA_SERVERLIST_HIGHPING;
1151 if(gethostcachenumber(SLIST_FIELD_ISFAVORITE, item))
1153 theColor = theColor * (1 - SKINALPHA_SERVERLIST_FAVORITE) + SKINCOLOR_SERVERLIST_FAVORITE * SKINALPHA_SERVERLIST_FAVORITE;
1154 theAlpha = theAlpha * (1 - SKINALPHA_SERVERLIST_FAVORITE) + SKINALPHA_SERVERLIST_FAVORITE;
1157 s = gethostcachestring(SLIST_FIELD_CNAME, item);
1160 if(substring(s, 0, 1) == "[")
1165 else if(strstrofs("0123456789", substring(s, 0, 1), 0) >= 0)
1171 q = stof(substring(crypto_getencryptlevel(s), 0, 1));
1172 if((q <= 0 && cvar("crypto_aeslevel") >= 3) || (q >= 3 && cvar("crypto_aeslevel") <= 0))
1174 theColor = SKINCOLOR_SERVERLIST_IMPOSSIBLE;
1175 theAlpha = SKINALPHA_SERVERLIST_IMPOSSIBLE;
1180 if(cvar("crypto_aeslevel") >= 2)
1185 if(cvar("crypto_aeslevel") >= 1)
1195 // 2: AES recommended but not available
1196 // 3: AES possible and will be used
1197 // 4: AES recommended and will be used
1203 vector iconSize = '0 0 0';
1204 iconSize_y = me.realFontSize_y * me.iconsSizeFactor;
1205 iconSize_x = me.realFontSize_x * me.iconsSizeFactor;
1207 vector iconPos = '0 0 0';
1208 iconPos_x = (me.columnIconsSize - 3 * iconSize_x) * 0.5;
1209 iconPos_y = (1 - iconSize_y) * 0.5;
1213 if not(me.seenIPv4 && me.seenIPv6)
1215 iconPos_x += iconSize_x * 0.5;
1217 else if(me.seenIPv4 && me.seenIPv6)
1221 draw_PreloadPictureWithFlags(n = strcat(SKINGFX_SERVERLIST_ICON, "_ipv6"), 0); // PRECACHE_PIC_MIPMAP
1223 draw_PreloadPictureWithFlags(n = strcat(SKINGFX_SERVERLIST_ICON, "_ipv4"), 0); // PRECACHE_PIC_MIPMAP
1225 draw_Picture(iconPos, n, iconSize, '1 1 1', 1);
1226 iconPos_x += iconSize_x;
1231 draw_PreloadPictureWithFlags(n = strcat(SKINGFX_SERVERLIST_ICON, "_aeslevel", ftos(q)), 0); // PRECACHE_PIC_MIPMAP
1232 draw_Picture(iconPos, n, iconSize, '1 1 1', 1);
1234 iconPos_x += iconSize_x;
1236 if(modname == "Xonotic")
1240 draw_PreloadPictureWithFlags(n = strcat(SKINGFX_SERVERLIST_ICON, "_pure1"), PRECACHE_PIC_MIPMAP);
1241 draw_Picture(iconPos, n, iconSize, '1 1 1', 1);
1246 draw_PreloadPictureWithFlags(n = strcat(SKINGFX_SERVERLIST_ICON, "_mod_", modname), PRECACHE_PIC_MIPMAP);
1247 if(draw_PictureSize(n) == '0 0 0')
1248 draw_PreloadPictureWithFlags(n = strcat(SKINGFX_SERVERLIST_ICON, "_mod_"), PRECACHE_PIC_MIPMAP);
1250 draw_Picture(iconPos, n, iconSize, '1 1 1', 1);
1252 draw_Picture(iconPos, n, iconSize, '1 1 1', SKINALPHA_SERVERLIST_ICON_NONPURE);
1254 iconPos_x += iconSize_x;
1256 if(sflags >= 0 && (sflags & SERVERFLAG_PLAYERSTATS))
1258 draw_PreloadPictureWithFlags(n = strcat(SKINGFX_SERVERLIST_ICON, "_stats1"), 0); // PRECACHE_PIC_MIPMAP
1259 draw_Picture(iconPos, n, iconSize, '1 1 1', 1);
1261 iconPos_x += iconSize_x;
1269 draw_Text(me.realUpperMargin * eY + (me.columnPingOrigin + me.columnPingSize - draw_TextWidth(s, 0, me.realFontSize)) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1272 s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_NAME, item), me.columnNameSize, 0, me.realFontSize);
1273 draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, theColor, theAlpha, 0);
1276 s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_MAP, item), me.columnMapSize, 0, me.realFontSize);
1277 draw_Text(me.realUpperMargin * eY + (me.columnMapOrigin + (me.columnMapSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1280 s = draw_TextShortenToWidth(typestr, me.columnTypeSize, 0, me.realFontSize);
1281 draw_Text(me.realUpperMargin * eY + (me.columnTypeOrigin + (me.columnTypeSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1283 // server playercount
1284 s = strcat(ftos(gethostcachenumber(SLIST_FIELD_NUMHUMANS, item)), "/", ftos(gethostcachenumber(SLIST_FIELD_MAXPLAYERS, item)));
1285 draw_Text(me.realUpperMargin * eY + (me.columnPlayersOrigin + (me.columnPlayersSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1288 float XonoticServerList_keyDown(entity me, float scan, float ascii, float shift)
1290 float i = CheckItemNumber(me.selectedItem);
1293 org = boxToGlobal(eY * (me.selectedItem * me.itemHeight - me.scrollPos), me.origin, me.size);
1294 sz = boxToGlobalSize(eY * me.itemHeight + eX * (1 - me.controlWidth), me.size);
1296 me.lastBumpSelectTime = 0;
1298 if(scan == K_ENTER || scan == K_KP_ENTER)
1300 ServerList_Connect_Click(NULL, me);
1303 else if(scan == K_MOUSE2 || scan == K_SPACE)
1305 if((me.nItems != 0) && (i >= 0))
1307 main.serverInfoDialog.loadServerInfo(main.serverInfoDialog, i);
1308 DialogOpenButton_Click_withCoords(me, main.serverInfoDialog, org, sz);
1313 else if(scan == K_INS || scan == K_MOUSE3 || scan == K_KP_INS)
1315 if((me.nItems != 0) && (i >= 0))
1317 me.toggleFavorite(me, me.selectedServer);
1318 me.ipAddressBoxFocused = -1;
1323 else if(SUPER(XonoticServerList).keyDown(me, scan, ascii, shift))
1325 else if(!me.controlledTextbox)
1328 return me.controlledTextbox.keyDown(me.controlledTextbox, scan, ascii, shift);
1331 float XonoticServerList_getTotalHeight(entity me) {
1332 float height_of_normal_rows = me.nItems - category_draw_count;
1333 float height_of_category_headers = me.categoriesHeight * category_draw_count;
1334 return me.itemHeight * (height_of_normal_rows + height_of_category_headers);
1336 float XonoticServerList_getItemAtPos(entity me, float pos) {
1339 pos = pos / me.itemHeight;
1341 for (i = 0; i < category_draw_count; ++i) {
1342 float first = i + category_item[i];
1343 float firstPos = i * me.categoriesHeight + category_item[i];
1344 if (pos >= firstPos)
1346 if (pos >= firstPos + me.categoriesHeight)
1347 ret = first + 1 + floor(pos - firstPos - me.categoriesHeight);
1351 float XonoticServerList_getItemStart(entity me, float item) {
1354 for (i = 0; i < category_draw_count; ++i) {
1355 float first = i + category_item[i];
1357 start += me.categoriesHeight - 1;
1359 return me.itemHeight * start;
1361 float XonoticServerList_getItemHeight(entity me, float item) {
1363 for (i = 0; i < category_draw_count; ++i) {
1364 float first = i + category_item[i];
1366 return me.itemHeight * me.categoriesHeight;
1368 return me.itemHeight;