4 CLASS(XonoticServerList, XonoticListBox)
5 METHOD(XonoticServerList, configureXonoticServerList, void(entity));
6 ATTRIB(XonoticServerList, rowsPerItem, float, 1)
7 METHOD(XonoticServerList, draw, void(entity));
8 METHOD(XonoticServerList, drawListBoxItem, void(entity, int, vector, bool, bool));
9 METHOD(XonoticServerList, doubleClickListBoxItem, void(entity, float, vector));
10 METHOD(XonoticServerList, resizeNotify, void(entity, vector, vector, vector, vector));
11 METHOD(XonoticServerList, keyDown, float(entity, float, float, float));
12 METHOD(XonoticServerList, toggleFavorite, void(entity, string));
14 ATTRIB(XonoticServerList, iconsSizeFactor, float, 0.85)
16 ATTRIB(XonoticServerList, realFontSize, vector, '0 0 0')
17 ATTRIB(XonoticServerList, realUpperMargin, float, 0)
18 ATTRIB(XonoticServerList, columnIconsOrigin, float, 0)
19 ATTRIB(XonoticServerList, columnIconsSize, float, 0)
20 ATTRIB(XonoticServerList, columnPingOrigin, float, 0)
21 ATTRIB(XonoticServerList, columnPingSize, float, 0)
22 ATTRIB(XonoticServerList, columnNameOrigin, float, 0)
23 ATTRIB(XonoticServerList, columnNameSize, float, 0)
24 ATTRIB(XonoticServerList, columnMapOrigin, float, 0)
25 ATTRIB(XonoticServerList, columnMapSize, float, 0)
26 ATTRIB(XonoticServerList, columnTypeOrigin, float, 0)
27 ATTRIB(XonoticServerList, columnTypeSize, float, 0)
28 ATTRIB(XonoticServerList, columnPlayersOrigin, float, 0)
29 ATTRIB(XonoticServerList, columnPlayersSize, float, 0)
30 ATTRIB(XonoticServerList, lockedSelectedItem, bool, true) // initially keep selected the first item of the list, avoiding an unwanted scrolling
32 ATTRIB(XonoticServerList, selectedServer, string, string_null) // to restore selected server when needed
33 METHOD(XonoticServerList, setSelected, void(entity, float));
34 METHOD(XonoticServerList, setSortOrder, void(entity, float, float));
35 ATTRIB(XonoticServerList, filterShowEmpty, float, 1)
36 ATTRIB(XonoticServerList, filterShowFull, float, 1)
37 ATTRIB(XonoticServerList, filterString, string, string_null)
38 ATTRIB(XonoticServerList, controlledTextbox, entity, NULL)
39 ATTRIB(XonoticServerList, ipAddressBox, entity, NULL)
40 ATTRIB(XonoticServerList, favoriteButton, entity, NULL)
41 ATTRIB(XonoticServerList, nextRefreshTime, float, 0)
42 METHOD(XonoticServerList, refreshServerList, void(entity, float)); // refresh mode: REFRESHSERVERLIST_*
43 ATTRIB(XonoticServerList, needsRefresh, float, 1)
44 METHOD(XonoticServerList, focusEnter, void(entity));
45 METHOD(XonoticServerList, positionSortButton, void(entity, entity, float, float, string, void(entity, entity)));
46 ATTRIB(XonoticServerList, sortButton1, entity, NULL)
47 ATTRIB(XonoticServerList, sortButton2, entity, NULL)
48 ATTRIB(XonoticServerList, sortButton3, entity, NULL)
49 ATTRIB(XonoticServerList, sortButton4, entity, NULL)
50 ATTRIB(XonoticServerList, sortButton5, entity, NULL)
51 ATTRIB(XonoticServerList, connectButton, entity, NULL)
52 ATTRIB(XonoticServerList, infoButton, entity, NULL)
53 ATTRIB(XonoticServerList, currentSortOrder, float, 0)
54 ATTRIB(XonoticServerList, currentSortField, float, -1)
56 ATTRIB(XonoticServerList, ipAddressBoxFocused, float, -1)
58 ATTRIB(XonoticServerList, seenIPv4, float, 0)
59 ATTRIB(XonoticServerList, seenIPv6, float, 0)
60 ATTRIB(XonoticServerList, categoriesHeight, float, 1.25)
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 float autocvar_menu_slist_categories;
71 float autocvar_menu_slist_categories_onlyifmultiple;
72 float autocvar_menu_slist_purethreshold;
73 float autocvar_menu_slist_modimpurity;
74 float autocvar_menu_slist_recommendations;
75 float autocvar_menu_slist_recommendations_maxping;
76 float autocvar_menu_slist_recommendations_minfreeslots;
77 float autocvar_menu_slist_recommendations_minhumans;
78 float autocvar_menu_slist_recommendations_purethreshold;
80 // server cache fields
81 #define SLIST_FIELDS \
82 SLIST_FIELD(CNAME, "cname") \
83 SLIST_FIELD(PING, "ping") \
84 SLIST_FIELD(GAME, "game") \
85 SLIST_FIELD(MOD, "mod") \
86 SLIST_FIELD(MAP, "map") \
87 SLIST_FIELD(NAME, "name") \
88 SLIST_FIELD(MAXPLAYERS, "maxplayers") \
89 SLIST_FIELD(NUMPLAYERS, "numplayers") \
90 SLIST_FIELD(NUMHUMANS, "numhumans") \
91 SLIST_FIELD(NUMBOTS, "numbots") \
92 SLIST_FIELD(PROTOCOL, "protocol") \
93 SLIST_FIELD(FREESLOTS, "freeslots") \
94 SLIST_FIELD(PLAYERS, "players") \
95 SLIST_FIELD(QCSTATUS, "qcstatus") \
96 SLIST_FIELD(CATEGORY, "category") \
97 SLIST_FIELD(ISFAVORITE, "isfavorite")
99 #define SLIST_FIELD(suffix,name) float SLIST_FIELD_##suffix;
103 const float REFRESHSERVERLIST_RESORT = 0; // sort the server list again to update for changes to e.g. favorite status, categories
104 const float REFRESHSERVERLIST_REFILTER = 1; // ..., also update filter and sort criteria
105 const float REFRESHSERVERLIST_ASK = 2; // ..., also suggest querying servers now
106 const float REFRESHSERVERLIST_RESET = 3; // ..., also clear the list first
108 // function declarations
109 float IsServerInList(string list, string srv);
110 #define IsFavorite(srv) IsServerInList(cvar_string("net_slist_favorites"), srv)
111 #define IsPromoted(srv) IsServerInList(_Nex_ExtResponseSystem_PromotedServers, srv)
112 #define IsRecommended(srv) IsServerInList(_Nex_ExtResponseSystem_RecommendedServers, srv)
114 entity RetrieveCategoryEnt(float catnum);
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 const int MAX_CATEGORIES = 9;
133 const int CATEGORY_FIRST = 1;
134 entity categories[MAX_CATEGORIES];
135 int 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 int category_name[MAX_CATEGORIES];
145 int category_item[MAX_CATEGORIES];
146 int category_draw_count;
148 #define SLIST_CATEGORIES \
149 SLIST_CATEGORY(CAT_FAVORITED, "", "", ZCTX(_("SLCAT^Favorites"))) \
150 SLIST_CATEGORY(CAT_RECOMMENDED, "", "", ZCTX(_("SLCAT^Recommended"))) \
151 SLIST_CATEGORY(CAT_NORMAL, "", "CAT_SERVERS", ZCTX(_("SLCAT^Normal Servers"))) \
152 SLIST_CATEGORY(CAT_SERVERS, "CAT_NORMAL", "CAT_SERVERS", ZCTX(_("SLCAT^Servers"))) \
153 SLIST_CATEGORY(CAT_XPM, "CAT_NORMAL", "CAT_SERVERS", ZCTX(_("SLCAT^Competitive Mode"))) \
154 SLIST_CATEGORY(CAT_MODIFIED, "", "CAT_SERVERS", ZCTX(_("SLCAT^Modified Servers"))) \
155 SLIST_CATEGORY(CAT_OVERKILL, "", "CAT_SERVERS", ZCTX(_("SLCAT^Overkill Mode"))) \
156 SLIST_CATEGORY(CAT_INSTAGIB, "", "CAT_SERVERS", ZCTX(_("SLCAT^InstaGib Mode"))) \
157 SLIST_CATEGORY(CAT_DEFRAG, "", "CAT_SERVERS", ZCTX(_("SLCAT^Defrag Mode")))
159 #define SLIST_CATEGORY_AUTOCVAR(name) autocvar_menu_slist_categories_##name##_override
160 #define SLIST_CATEGORY(name,enoverride,dioverride,str) \
162 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(int 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 bool 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 int CheckCategoryOverride(int cat)
268 entity catent = RetrieveCategoryEnt(cat);
271 int 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 int CheckCategoryForEntry(int entry)
284 string s, k, v, modtype = "";
285 int j, m, impure = 0, freeslots = 0, sflags = 0;
286 s = gethostcachestring(SLIST_FIELD_QCSTATUS, entry);
287 m = tokenizebyseparator(s, ":");
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 string cname = gethostcachestring(SLIST_FIELD_CNAME, entry);
313 if(IsPromoted(cname)) { return CAT_RECOMMENDED; }
316 float recommended = 0;
317 if(autocvar_menu_slist_recommendations & 1)
319 if(IsRecommended(cname)) { ++recommended; }
320 else { --recommended; }
322 if(autocvar_menu_slist_recommendations & 2)
325 ///// check for minimum free slots
326 (freeslots >= autocvar_menu_slist_recommendations_minfreeslots)
328 && // check for purity requirement
330 (autocvar_menu_slist_recommendations_purethreshold < 0)
332 (impure <= autocvar_menu_slist_recommendations_purethreshold)
335 && // check for minimum amount of humans
337 gethostcachenumber(SLIST_FIELD_NUMHUMANS, entry)
339 autocvar_menu_slist_recommendations_minhumans
342 && // check for maximum latency
344 gethostcachenumber(SLIST_FIELD_PING, entry)
346 autocvar_menu_slist_recommendations_maxping
353 if(recommended > 0) { return CAT_RECOMMENDED; }
357 // if not favorited or recommended, check modname
358 if(modtype != "xonotic")
362 // old servers which don't report their mod name are considered modified now
363 case "": { return CAT_MODIFIED; }
365 case "xpm": { return CAT_XPM; }
367 case "instagib": { return CAT_INSTAGIB; }
368 case "overkill": { return CAT_OVERKILL; }
369 //case "nix": { return CAT_NIX; }
370 //case "newtoys": { return CAT_NEWTOYS; }
372 // "cts" is allowed as compat, xdf is replacement
374 case "xdf": { return CAT_DEFRAG; }
376 default: { LOG_TRACEF("Found strange mod type: %s\n", modtype); return CAT_MODIFIED; }
380 // must be normal or impure server
381 return ((impure > autocvar_menu_slist_purethreshold) ? CAT_MODIFIED : CAT_NORMAL);
384 void XonoticServerList_toggleFavorite(entity me, string srv)
386 string s, s0, s1, s2, srv_resolved, p;
389 srv_resolved = netaddress_resolve(srv, 26000);
390 p = crypto_getidfp(srv_resolved);
391 s = cvar_string("net_slist_favorites");
392 n = tokenize_console(s);
393 for(i = 0; i < n; ++i)
395 if(substring(argv(i), 0, 1) != "[" && strlen(argv(i)) == 44 && strstrofs(argv(i), ".", 0) < 0)
403 if(srv_resolved != netaddress_resolve(argv(i), 26000))
408 s0 = substring(s, 0, argv_end_index(i - 1));
410 s2 = substring(s, argv_start_index(i + 1), -1);
411 if(s0 != "" && s2 != "")
413 cvar_set("net_slist_favorites", strcat(s0, s1, s2));
414 s = cvar_string("net_slist_favorites");
415 n = tokenize_console(s);
426 cvar_set("net_slist_favorites", strcat(s, s1, p));
428 cvar_set("net_slist_favorites", strcat(s, s1, srv));
431 me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
434 void ServerList_Update_favoriteButton(entity btn, entity me)
436 entity e = me.favoriteButton;
437 if(IsFavorite(me.ipAddressBox.text))
439 e.setText(e, _("Remove"));
444 e.setText(e, _("Favorite"));
445 setZonedTooltip(e, _("Bookmark the currently highlighted server so that it's faster to find in the future"), string_null);
449 entity makeXonoticServerList()
452 me = NEW(XonoticServerList);
453 me.configureXonoticServerList(me);
456 void XonoticServerList_configureXonoticServerList(entity me)
458 me.configureXonoticListBox(me);
461 #define SLIST_FIELD(suffix,name) SLIST_FIELD_##suffix = gethostcacheindexforkey(name);
468 void XonoticServerList_setSelected(entity me, int i)
470 me.lockedSelectedItem = false;
471 //int save = me.selectedItem;
472 SUPER(XonoticServerList).setSelected(me, i);
474 if(me.selectedItem == save)
479 if(gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT) != me.nItems)
480 return; // sorry, it would be wrong
482 if(me.selectedServer)
483 strunzone(me.selectedServer);
484 me.selectedServer = strzone(gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
486 me.ipAddressBox.setText(me.ipAddressBox, me.selectedServer);
487 me.ipAddressBox.cursorPos = strlen(me.selectedServer);
488 me.ipAddressBoxFocused = -1;
490 void XonoticServerList_refreshServerList(entity me, int mode)
492 //print("refresh of type ", ftos(mode), "\n");
494 if(mode >= REFRESHSERVERLIST_REFILTER)
499 string s, typestr, modstr;
503 m = strstrofs(s, ":", 0);
506 typestr = substring(s, 0, m);
507 s = substring(s, m + 1, strlen(s) - m - 1);
508 while(substring(s, 0, 1) == " ")
509 s = substring(s, 1, strlen(s) - 1);
514 modstr = cvar_string("menu_slist_modfilter");
516 m = SLIST_MASK_AND - 1;
517 resethostcachemasks();
519 // ping: reject negative ping (no idea why this happens in the first place, engine bug)
520 sethostcachemasknumber(++m, SLIST_FIELD_PING, 0, SLIST_TEST_GREATEREQUAL);
523 if(!me.filterShowFull)
525 sethostcachemasknumber(++m, SLIST_FIELD_FREESLOTS, 1, SLIST_TEST_GREATEREQUAL); // legacy
526 sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, ":S0:", SLIST_TEST_NOTCONTAIN); // g_maxplayers support
530 if(!me.filterShowEmpty)
531 sethostcachemasknumber(++m, SLIST_FIELD_NUMHUMANS, 1, SLIST_TEST_GREATEREQUAL);
533 // gametype filtering
535 sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, strcat(typestr, ":"), SLIST_TEST_STARTSWITH);
540 if(substring(modstr, 0, 1) == "!")
541 sethostcachemaskstring(++m, SLIST_FIELD_MOD, resolvemod(substring(modstr, 1, strlen(modstr) - 1)), SLIST_TEST_NOTEQUAL);
543 sethostcachemaskstring(++m, SLIST_FIELD_MOD, resolvemod(modstr), SLIST_TEST_EQUAL);
547 n = tokenizebyseparator(_Nex_ExtResponseSystem_BannedServers, " ");
548 for(i = 0; i < n; ++i)
550 sethostcachemaskstring(++m, SLIST_FIELD_CNAME, argv(i), SLIST_TEST_NOTSTARTSWITH);
552 m = SLIST_MASK_OR - 1;
555 sethostcachemaskstring(++m, SLIST_FIELD_NAME, s, SLIST_TEST_CONTAINS);
556 sethostcachemaskstring(++m, SLIST_FIELD_MAP, s, SLIST_TEST_CONTAINS);
557 sethostcachemaskstring(++m, SLIST_FIELD_PLAYERS, s, SLIST_TEST_CONTAINS);
558 sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, strcat(s, ":"), SLIST_TEST_STARTSWITH);
562 //listflags |= SLSF_FAVORITES;
563 listflags |= SLSF_CATEGORIES;
564 if(me.currentSortOrder < 0) { listflags |= SLSF_DESCENDING; }
565 sethostcachesort(me.currentSortField, listflags);
569 if(mode >= REFRESHSERVERLIST_ASK)
570 refreshhostcache(mode >= REFRESHSERVERLIST_RESET);
572 void XonoticServerList_focusEnter(entity me)
574 SUPER(XonoticServerList).focusEnter(me);
575 if(time < me.nextRefreshTime)
577 //print("sorry, no refresh yet\n");
580 me.nextRefreshTime = time + 10;
581 me.refreshServerList(me, REFRESHSERVERLIST_ASK);
584 void XonoticServerList_draw(entity me)
587 bool found = false, owned;
589 if(_Nex_ExtResponseSystem_BannedServersNeedsRefresh)
593 _Nex_ExtResponseSystem_BannedServersNeedsRefresh = 0;
596 if(_Nex_ExtResponseSystem_PromotedServersNeedsRefresh)
600 _Nex_ExtResponseSystem_PromotedServersNeedsRefresh = 0;
603 if(_Nex_ExtResponseSystem_RecommendedServersNeedsRefresh)
607 _Nex_ExtResponseSystem_RecommendedServersNeedsRefresh = 0;
610 if(me.currentSortField == -1)
612 me.setSortOrder(me, SLIST_FIELD_PING, +1);
613 me.refreshServerList(me, REFRESHSERVERLIST_RESET);
615 else if(me.needsRefresh == 1)
617 me.needsRefresh = 2; // delay by one frame to make sure "slist" has been executed
619 else if(me.needsRefresh == 2)
622 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
624 else if(me.needsRefresh == 3)
627 me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
630 owned = ((me.selectedServer == me.ipAddressBox.text) && (me.ipAddressBox.text != ""));
632 for(i = 0; i < category_draw_count; ++i) { category_name[i] = -1; category_item[i] = -1; }
633 category_draw_count = 0;
635 if(autocvar_menu_slist_categories >= 0) // if less than 0, don't even draw a category heading for favorites
637 float itemcount = gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT);
638 me.nItems = itemcount;
640 //float visible = floor(me.scrollPos / me.itemHeight);
641 // ^ unfortunately no such optimization can be made-- we must process through the
642 // entire list, otherwise there is no way to know which item is first in its category.
644 // binary search method suggested by div
647 for(x = 1; x <= category_ent_count; ++x) {
649 float last = (itemcount - 1);
654 float catf = gethostcachenumber(SLIST_FIELD_CATEGORY, first);
655 float catl = gethostcachenumber(SLIST_FIELD_CATEGORY, last);
657 // The first one is already > x.
658 // Therefore, category x does not exist.
659 // Higher numbered categories do exist though.
660 } else if (catl < x) {
661 // The last one is < x.
662 // Thus this category - and any following -
665 } else if (catf == x) {
666 // Starts at first. This breaks the loop
667 // invariant in the binary search and thus has
668 // to be handled separately.
669 if(gethostcachenumber(SLIST_FIELD_CATEGORY, first) != x)
670 error("Category mismatch I");
672 if(gethostcachenumber(SLIST_FIELD_CATEGORY, first - 1) == x)
673 error("Category mismatch II");
674 category_name[category_draw_count] = x;
675 category_item[category_draw_count] = first;
676 ++category_draw_count;
679 // At this point, catf <= x < catl, thus
680 // catf < catl, thus first < last.
683 // catf == gethostcachenumber(SLIST_FIELD_CATEGORY(first)
684 // catl == gethostcachenumber(SLIST_FIELD_CATEGORY(last)
687 while (last - first > 1) {
688 float middle = floor((first + last) / 2);
689 // By loop condition, middle != first && middle != last.
690 float cat = gethostcachenumber(SLIST_FIELD_CATEGORY, middle);
700 if(gethostcachenumber(SLIST_FIELD_CATEGORY, last) != x)
701 error("Category mismatch III");
703 if(gethostcachenumber(SLIST_FIELD_CATEGORY, last - 1) == x)
704 error("Category mismatch IV");
705 category_name[category_draw_count] = x;
706 category_item[category_draw_count] = last;
707 ++category_draw_count;
708 begin = last + 1; // already scanned through these, skip 'em
711 begin = last; // already scanned through these, skip 'em
714 if(autocvar_menu_slist_categories_onlyifmultiple && (category_draw_count == 1))
716 category_name[0] = -1;
717 category_item[0] = -1;
718 category_draw_count = 0;
719 me.nItems = itemcount;
722 else { me.nItems = gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT); }
724 me.connectButton.disabled = ((me.nItems == 0) && (me.ipAddressBox.text == ""));
725 me.infoButton.disabled = ((me.nItems == 0) || !owned);
726 me.favoriteButton.disabled = ((me.nItems == 0) && (me.ipAddressBox.text == ""));
728 if(me.lockedSelectedItem)
732 if(gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem) != me.selectedServer)
734 if(me.selectedServer)
735 strunzone(me.selectedServer);
736 me.selectedServer = strzone(gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
741 else if(me.selectedServer)
743 for(i = 0; i < me.nItems; ++i)
745 if(gethostcachestring(SLIST_FIELD_CNAME, i) == me.selectedServer)
747 // don't follow the selected item with SUPER(XonoticServerList).setSelected(me, i);
758 // selected server disappeared, select the last server (scrolling to it)
759 if(me.selectedItem >= me.nItems)
760 SUPER(XonoticServerList).setSelected(me, me.nItems - 1);
761 if(me.selectedServer)
762 strunzone(me.selectedServer);
763 me.selectedServer = strzone(gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
769 if(me.selectedServer != me.ipAddressBox.text)
771 me.ipAddressBox.setText(me.ipAddressBox, me.selectedServer);
772 me.ipAddressBox.cursorPos = strlen(me.selectedServer);
773 me.ipAddressBoxFocused = -1;
777 if(me.ipAddressBoxFocused != me.ipAddressBox.focused)
779 if(me.ipAddressBox.focused || me.ipAddressBoxFocused < 0)
780 ServerList_Update_favoriteButton(NULL, me);
781 me.ipAddressBoxFocused = me.ipAddressBox.focused;
784 SUPER(XonoticServerList).draw(me);
786 void ServerList_PingSort_Click(entity btn, entity me)
788 me.setSortOrder(me, SLIST_FIELD_PING, +1);
790 void ServerList_NameSort_Click(entity btn, entity me)
792 me.setSortOrder(me, SLIST_FIELD_NAME, -1); // why?
794 void ServerList_MapSort_Click(entity btn, entity me)
796 me.setSortOrder(me, SLIST_FIELD_MAP, -1); // why?
798 void ServerList_PlayerSort_Click(entity btn, entity me)
800 me.setSortOrder(me, SLIST_FIELD_NUMHUMANS, -1);
802 void ServerList_TypeSort_Click(entity btn, entity me)
807 m = strstrofs(s, ":", 0);
810 s = substring(s, 0, m);
811 while(substring(s, m+1, 1) == " ") // skip spaces
817 for(i = 1; ; i *= 2) // 20 modes ought to be enough for anyone
819 t = MapInfo_Type_ToString(i);
821 if(t == "") // it repeats (default case)
824 // choose the first one
825 s = MapInfo_Type_ToString(1);
830 // the type was found
831 // choose the next one
832 s = MapInfo_Type_ToString(i * 2);
834 s = MapInfo_Type_ToString(1);
841 s = strcat(s, substring(me.filterString, m+1, strlen(me.filterString) - m - 1));
843 me.controlledTextbox.setText(me.controlledTextbox, s);
844 me.controlledTextbox.keyDown(me.controlledTextbox, K_END, 0, 0);
845 me.controlledTextbox.keyUp(me.controlledTextbox, K_END, 0, 0);
846 //ServerList_Filter_Change(me.controlledTextbox, me);
848 void ServerList_Filter_Change(entity box, entity me)
851 strunzone(me.filterString);
853 me.filterString = strzone(box.text);
855 me.filterString = string_null;
856 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
858 me.ipAddressBox.setText(me.ipAddressBox, "");
859 me.ipAddressBox.cursorPos = 0;
860 me.ipAddressBoxFocused = -1;
862 void ServerList_Categories_Click(entity box, entity me)
864 box.setChecked(box, autocvar_menu_slist_categories = !autocvar_menu_slist_categories);
865 me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
867 me.ipAddressBox.setText(me.ipAddressBox, "");
868 me.ipAddressBox.cursorPos = 0;
869 me.ipAddressBoxFocused = -1;
871 void ServerList_ShowEmpty_Click(entity box, entity me)
873 box.setChecked(box, me.filterShowEmpty = !me.filterShowEmpty);
874 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
876 me.ipAddressBox.setText(me.ipAddressBox, "");
877 me.ipAddressBox.cursorPos = 0;
878 me.ipAddressBoxFocused = -1;
880 void ServerList_ShowFull_Click(entity box, entity me)
882 box.setChecked(box, me.filterShowFull = !me.filterShowFull);
883 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
885 me.ipAddressBox.setText(me.ipAddressBox, "");
886 me.ipAddressBox.cursorPos = 0;
887 me.ipAddressBoxFocused = -1;
889 void XonoticServerList_setSortOrder(entity me, int fld, int direction)
891 if(me.currentSortField == fld)
892 direction = -me.currentSortOrder;
893 me.currentSortOrder = direction;
894 me.currentSortField = fld;
895 me.sortButton1.forcePressed = (fld == SLIST_FIELD_PING);
896 me.sortButton2.forcePressed = (fld == SLIST_FIELD_NAME);
897 me.sortButton3.forcePressed = (fld == SLIST_FIELD_MAP);
898 me.sortButton4.forcePressed = 0;
899 me.sortButton5.forcePressed = (fld == SLIST_FIELD_NUMHUMANS);
901 if(me.selectedServer)
902 strunzone(me.selectedServer);
903 me.selectedServer = string_null;
904 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
906 void XonoticServerList_positionSortButton(entity me, entity btn, float theOrigin, float theSize, string theTitle, void(entity, entity) theFunc)
908 vector originInLBSpace, sizeInLBSpace;
909 originInLBSpace = eY * (-me.itemHeight);
910 sizeInLBSpace = eY * me.itemHeight + eX * (1 - me.controlWidth);
912 vector originInDialogSpace, sizeInDialogSpace;
913 originInDialogSpace = boxToGlobal(originInLBSpace, me.Container_origin, me.Container_size);
914 sizeInDialogSpace = boxToGlobalSize(sizeInLBSpace, me.Container_size);
916 btn.Container_origin_x = originInDialogSpace.x + sizeInDialogSpace.x * theOrigin;
917 btn.Container_size_x = sizeInDialogSpace.x * theSize;
918 btn.setText(btn, theTitle);
919 btn.onClick = theFunc;
920 btn.onClickEntity = me;
923 void XonoticServerList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
925 SUPER(XonoticServerList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
927 me.realFontSize_y = me.fontSize / (absSize.y * me.itemHeight);
928 me.realFontSize_x = me.fontSize / (absSize.x * (1 - me.controlWidth));
929 me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
931 me.columnIconsOrigin = 0;
932 me.columnIconsSize = me.realFontSize.x * 4 * me.iconsSizeFactor;
933 me.columnPingSize = me.realFontSize.x * 3;
934 me.columnMapSize = me.realFontSize.x * 10;
935 me.columnTypeSize = me.realFontSize.x * 4;
936 me.columnPlayersSize = me.realFontSize.x * 5;
937 me.columnNameSize = 1 - me.columnPlayersSize - me.columnMapSize - me.columnPingSize - me.columnIconsSize - me.columnTypeSize - 5 * me.realFontSize.x;
938 me.columnPingOrigin = me.columnIconsOrigin + me.columnIconsSize + me.realFontSize.x;
939 me.columnNameOrigin = me.columnPingOrigin + me.columnPingSize + me.realFontSize.x;
940 me.columnMapOrigin = me.columnNameOrigin + me.columnNameSize + me.realFontSize.x;
941 me.columnTypeOrigin = me.columnMapOrigin + me.columnMapSize + me.realFontSize.x;
942 me.columnPlayersOrigin = me.columnTypeOrigin + me.columnTypeSize + me.realFontSize.x;
944 me.positionSortButton(me, me.sortButton1, me.columnPingOrigin, me.columnPingSize, _("Ping"), ServerList_PingSort_Click);
945 me.positionSortButton(me, me.sortButton2, me.columnNameOrigin, me.columnNameSize, _("Host name"), ServerList_NameSort_Click);
946 me.positionSortButton(me, me.sortButton3, me.columnMapOrigin, me.columnMapSize, _("Map"), ServerList_MapSort_Click);
947 me.positionSortButton(me, me.sortButton4, me.columnTypeOrigin, me.columnTypeSize, _("Type"), ServerList_TypeSort_Click);
948 me.positionSortButton(me, me.sortButton5, me.columnPlayersOrigin, me.columnPlayersSize, _("Players"), ServerList_PlayerSort_Click);
950 int f = me.currentSortField;
953 me.currentSortField = -1;
954 me.setSortOrder(me, f, me.currentSortOrder); // force resetting the sort order
957 void ServerList_Connect_Click(entity btn, entity me)
959 localcmd(sprintf("connect %s\n",
960 ((me.ipAddressBox.text != "") ?
961 me.ipAddressBox.text : me.selectedServer
965 void ServerList_Favorite_Click(entity btn, entity me)
968 ipstr = netaddress_resolve(me.ipAddressBox.text, 26000);
971 m_play_click_sound(MENU_SOUND_SELECT);
972 me.toggleFavorite(me, me.ipAddressBox.text);
973 me.ipAddressBoxFocused = -1;
976 void ServerList_Info_Click(entity btn, entity me)
979 main.serverInfoDialog.loadServerInfo(main.serverInfoDialog, me.selectedItem);
981 vector org = boxToGlobal(eY * (me.selectedItem * me.itemHeight - me.scrollPos), me.origin, me.size);
982 vector sz = boxToGlobalSize(eY * me.itemHeight + eX * (1 - me.controlWidth), me.size);
983 DialogOpenButton_Click_withCoords(me, main.serverInfoDialog, org, sz);
985 void XonoticServerList_doubleClickListBoxItem(entity me, int i, vector where)
987 ServerList_Connect_Click(NULL, me);
989 void XonoticServerList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
991 // layout: Ping, Server name, Map name, NP, TP, MP
998 int freeslots = -1, sflags = -1, j, m;
999 string s, typestr, versionstr, k, v, modname;
1001 //printf("time: %f, i: %d, item: %d, nitems: %d\n", time, i, item, me.nItems);
1003 vector oldscale = draw_scale;
1004 vector oldshift = draw_shift;
1005 #define SET_YRANGE(start,end) \
1006 draw_scale = boxToGlobalSize(eX * 1 + eY * (end - start), oldscale); \
1007 draw_shift = boxToGlobal(eY * start, oldshift, oldscale);
1009 for (j = 0; j < category_draw_count; ++j) {
1010 // Matches exactly the headings with increased height.
1011 if (i == category_item[j])
1015 if (j < category_draw_count)
1017 entity catent = RetrieveCategoryEnt(category_name[j]);
1021 (me.categoriesHeight - 1) / (me.categoriesHeight + 1),
1022 me.categoriesHeight / (me.categoriesHeight + 1)
1025 eY * me.realUpperMargin
1028 eX * (me.columnNameOrigin + (me.columnNameSize - draw_TextWidth(catent.cat_string, 0, me.realFontSize)) * 0.5),
1031 eX * (me.columnNameOrigin),
1032 strcat(catent.cat_string, ":"),
1035 SKINCOLOR_SERVERLIST_CATEGORY,
1036 SKINALPHA_SERVERLIST_CATEGORY,
1039 SET_YRANGE(me.categoriesHeight / (me.categoriesHeight + 1), 1);
1044 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
1047 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
1048 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
1051 s = gethostcachestring(SLIST_FIELD_QCSTATUS, i);
1052 m = tokenizebyseparator(s, ":");
1057 versionstr = argv(1);
1061 for(j = 2; j < m; ++j)
1065 k = substring(argv(j), 0, 1);
1066 v = substring(argv(j), 1, -1);
1070 freeslots = stof(v);
1077 #ifdef COMPAT_NO_MOD_IS_XONOTIC
1079 modname = "Xonotic";
1083 SLIST_FIELD_MOD = gethostcacheindexforkey("mod");
1084 s = gethostcachestring(SLIST_FIELD_MOD, i);
1086 if(modname == "Xonotic")
1090 // list the mods here on which the pure server check actually works
1091 if(modname != "Xonotic")
1092 if(modname != "InstaGib" || modname != "MinstaGib")
1093 if(modname != "CTS")
1094 if(modname != "NIX")
1095 if(modname != "NewToys")
1098 if(gethostcachenumber(SLIST_FIELD_FREESLOTS, i) <= 0)
1099 theAlpha = SKINALPHA_SERVERLIST_FULL;
1100 else if(freeslots == 0)
1101 theAlpha = SKINALPHA_SERVERLIST_FULL; // g_maxplayers support
1102 else if (!gethostcachenumber(SLIST_FIELD_NUMHUMANS, i))
1103 theAlpha = SKINALPHA_SERVERLIST_EMPTY;
1107 p = gethostcachenumber(SLIST_FIELD_PING, i);
1108 const int PING_LOW = 75;
1109 const int PING_MED = 200;
1110 const int PING_HIGH = 500;
1112 theColor = SKINCOLOR_SERVERLIST_LOWPING + (SKINCOLOR_SERVERLIST_MEDPING - SKINCOLOR_SERVERLIST_LOWPING) * (p / PING_LOW);
1113 else if(p < PING_MED)
1114 theColor = SKINCOLOR_SERVERLIST_MEDPING + (SKINCOLOR_SERVERLIST_HIGHPING - SKINCOLOR_SERVERLIST_MEDPING) * ((p - PING_LOW) / (PING_MED - PING_LOW));
1115 else if(p < PING_HIGH)
1117 theColor = SKINCOLOR_SERVERLIST_HIGHPING;
1118 theAlpha *= 1 + (SKINALPHA_SERVERLIST_HIGHPING - 1) * ((p - PING_MED) / (PING_HIGH - PING_MED));
1123 theAlpha *= SKINALPHA_SERVERLIST_HIGHPING;
1126 if(gethostcachenumber(SLIST_FIELD_ISFAVORITE, i))
1128 theColor = theColor * (1 - SKINALPHA_SERVERLIST_FAVORITE) + SKINCOLOR_SERVERLIST_FAVORITE * SKINALPHA_SERVERLIST_FAVORITE;
1129 theAlpha = theAlpha * (1 - SKINALPHA_SERVERLIST_FAVORITE) + SKINALPHA_SERVERLIST_FAVORITE;
1132 s = gethostcachestring(SLIST_FIELD_CNAME, i);
1134 isv4 = isv6 = false;
1135 if(substring(s, 0, 1) == "[")
1140 else if(strstrofs("0123456789", substring(s, 0, 1), 0) >= 0)
1146 q = stof(substring(crypto_getencryptlevel(s), 0, 1));
1147 if((q <= 0 && cvar("crypto_aeslevel") >= 3) || (q >= 3 && cvar("crypto_aeslevel") <= 0))
1149 theColor = SKINCOLOR_SERVERLIST_IMPOSSIBLE;
1150 theAlpha = SKINALPHA_SERVERLIST_IMPOSSIBLE;
1155 if(cvar("crypto_aeslevel") >= 2)
1160 if(cvar("crypto_aeslevel") >= 1)
1170 // 2: AES recommended but not available
1171 // 3: AES possible and will be used
1172 // 4: AES recommended and will be used
1178 vector iconSize = '0 0 0';
1179 iconSize_y = me.realFontSize.y * me.iconsSizeFactor;
1180 iconSize_x = me.realFontSize.x * me.iconsSizeFactor;
1182 vector iconPos = '0 0 0';
1183 iconPos_x = (me.columnIconsSize - 3 * iconSize.x) * 0.5;
1184 iconPos_y = (1 - iconSize.y) * 0.5;
1187 if(me.seenIPv4 && me.seenIPv6)
1190 draw_Picture(iconPos, "icon_ipv6", iconSize, '1 1 1', 1);
1192 draw_Picture(iconPos, "icon_ipv4", iconSize, '1 1 1', 1);
1195 iconPos.x += iconSize.x;
1199 draw_Picture(iconPos, strcat("icon_aeslevel", ftos(q)), iconSize, '1 1 1', 1);
1201 iconPos.x += iconSize.x;
1204 if(modname == "Xonotic")
1207 draw_Picture(iconPos, "icon_pure1", iconSize, '1 1 1', 1);
1211 string icon = strcat("icon_mod_", modname);
1212 if(draw_PictureSize(icon) == '0 0 0')
1216 draw_Picture(iconPos, icon, iconSize, '1 1 1', 1);
1218 draw_Picture(iconPos, icon, iconSize, '1 1 1', SKINALPHA_SERVERLIST_ICON_NONPURE);
1221 iconPos.x += iconSize.x;
1224 if(sflags >= 0 && (sflags & SERVERFLAG_PLAYERSTATS))
1225 draw_Picture(iconPos, "icon_stats1", iconSize, '1 1 1', 1);
1233 draw_Text(me.realUpperMargin * eY + (me.columnPingOrigin + me.columnPingSize - draw_TextWidth(s, 0, me.realFontSize)) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1236 s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_NAME, i), me.columnNameSize, 0, me.realFontSize);
1237 draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, theColor, theAlpha, 0);
1240 s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_MAP, i), me.columnMapSize, 0, me.realFontSize);
1241 draw_Text(me.realUpperMargin * eY + (me.columnMapOrigin + (me.columnMapSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1244 s = draw_TextShortenToWidth(typestr, me.columnTypeSize, 0, me.realFontSize);
1245 draw_Text(me.realUpperMargin * eY + (me.columnTypeOrigin + (me.columnTypeSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1247 // server playercount
1248 s = strcat(ftos(gethostcachenumber(SLIST_FIELD_NUMHUMANS, i)), "/", ftos(gethostcachenumber(SLIST_FIELD_MAXPLAYERS, i)));
1249 draw_Text(me.realUpperMargin * eY + (me.columnPlayersOrigin + (me.columnPlayersSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1252 bool XonoticServerList_keyDown(entity me, int scan, bool ascii, bool shift)
1256 org = boxToGlobal(eY * (me.selectedItem * me.itemHeight - me.scrollPos), me.origin, me.size);
1257 sz = boxToGlobalSize(eY * me.itemHeight + eX * (1 - me.controlWidth), me.size);
1259 if(scan == K_ENTER || scan == K_KP_ENTER)
1261 ServerList_Connect_Click(NULL, me);
1264 else if(scan == K_MOUSE2 || scan == K_SPACE)
1268 m_play_click_sound(MENU_SOUND_OPEN);
1269 main.serverInfoDialog.loadServerInfo(main.serverInfoDialog, me.selectedItem);
1270 DialogOpenButton_Click_withCoords(me, main.serverInfoDialog, org, sz);
1275 else if(scan == K_INS || scan == K_MOUSE3 || scan == K_KP_INS)
1279 me.toggleFavorite(me, me.selectedServer);
1280 me.ipAddressBoxFocused = -1;
1285 else if(SUPER(XonoticServerList).keyDown(me, scan, ascii, shift))
1287 else if(!me.controlledTextbox)
1290 return me.controlledTextbox.keyDown(me.controlledTextbox, scan, ascii, shift);
1293 float XonoticServerList_getTotalHeight(entity me)
1295 float num_normal_rows = me.nItems;
1296 int num_headers = category_draw_count;
1297 return me.itemHeight * (num_normal_rows + me.categoriesHeight * num_headers);
1299 int XonoticServerList_getItemAtPos(entity me, float pos)
1301 pos = pos / me.itemHeight;
1303 for (i = category_draw_count - 1; i >= 0; --i) {
1304 int itemidx = category_item[i];
1305 float itempos = i * me.categoriesHeight + category_item[i];
1306 if (pos >= itempos + me.categoriesHeight + 1)
1307 return itemidx + 1 + floor(pos - (itempos + me.categoriesHeight + 1));
1311 // No category matches? Note that category 0 is... 0. Therefore no headings exist at all.
1314 float XonoticServerList_getItemStart(entity me, int item)
1317 for (i = category_draw_count - 1; i >= 0; --i) {
1318 int itemidx = category_item[i];
1319 float itempos = i * me.categoriesHeight + category_item[i];
1320 if (item >= itemidx + 1)
1321 return (itempos + me.categoriesHeight + 1 + item - (itemidx + 1)) * me.itemHeight;
1322 if (item >= itemidx)
1323 return itempos * me.itemHeight;
1325 // No category matches? Note that category 0 is... 0. Therefore no headings exist at all.
1326 return item * me.itemHeight;
1328 float XonoticServerList_getItemHeight(entity me, int item)
1331 for (i = 0; i < category_draw_count; ++i) {
1332 // Matches exactly the headings with increased height.
1333 if (item == category_item[i])
1334 return me.itemHeight * (me.categoriesHeight + 1);
1336 return me.itemHeight;