1 #include "serverlist.qh"
5 #include "mainwindow.qh"
6 #include "dialog_multiplayer_join_serverinfo.qh"
7 #include <common/mapinfo.qh>
9 void RegisterSLCategories()
12 #define SLIST_CATEGORY(name,enoverride,dioverride,str) \
13 SET_FIELD_COUNT(name, CATEGORY_FIRST, category_ent_count) \
14 CHECK_MAX_COUNT(name, MAX_CATEGORIES, category_ent_count, "SLIST_CATEGORY") \
15 cat = categories[name - 1] = new(slist_category); \
16 cat.cat_name = #name; \
17 cat.cat_enoverride_string = strzone(SLIST_CATEGORY_AUTOCVAR(name)); \
18 cat.cat_dioverride_string = strzone(dioverride); \
19 cat.cat_string = strzone(str);
26 #define PROCESS_OVERRIDE(override_string,override_field) \
27 for(i = 0; i < category_ent_count; ++i) \
29 s = categories[i].override_string; \
30 if((s != "") && (s != categories[i].cat_name)) \
33 for(x = 0; x < category_ent_count; ++x) \
34 { if(categories[x].cat_name == s) { \
40 strunzone(categories[i].override_string); \
41 categories[i].override_string = string_null; \
42 categories[i].override_field = catnum; \
48 "RegisterSLCategories(): Improper override '%s' for category '%s'!\n", \
50 categories[i].cat_name \
54 strunzone(categories[i].override_string); \
55 categories[i].override_string = string_null; \
56 categories[i].override_field = 0; \
58 PROCESS_OVERRIDE(cat_enoverride_string, cat_enoverride)
59 PROCESS_OVERRIDE(cat_dioverride_string, cat_dioverride)
60 #undef PROCESS_OVERRIDE
63 // Supporting Functions
64 entity RetrieveCategoryEnt(int catnum)
66 if((catnum > 0) && (catnum <= category_ent_count))
68 return categories[catnum - 1];
72 error(sprintf("RetrieveCategoryEnt(%d): Improper category number!\n", catnum));
77 bool IsServerInList(string list, string srv)
83 srv = netaddress_resolve(srv, 26000);
86 p = crypto_getidfp(srv);
87 n = tokenize_console(list);
88 for(i = 0; i < n; ++i)
90 if(substring(argv(i), 0, 1) != "[" && strlen(argv(i)) == 44 && strstrofs(argv(i), ".", 0) < 0)
98 if(srv == netaddress_resolve(argv(i), 26000))
105 int CheckCategoryOverride(int cat)
107 entity catent = RetrieveCategoryEnt(cat);
110 int override = (autocvar_menu_slist_categories ? catent.cat_enoverride : catent.cat_dioverride);
111 if(override) { return override; }
116 error(sprintf("CheckCategoryOverride(%d): Improper category number!\n", cat));
121 int CheckCategoryForEntry(int entry)
123 string s, k, v, modtype = "";
124 int j, m, impure = 0, freeslots = 0, sflags = 0;
125 s = gethostcachestring(SLIST_FIELD_QCSTATUS, entry);
126 m = tokenizebyseparator(s, ":");
128 for(j = 2; j < m; ++j)
130 if(argv(j) == "") { break; }
131 k = substring(argv(j), 0, 1);
132 v = substring(argv(j), 1, -1);
135 case "P": { impure = stof(v); break; }
136 case "S": { freeslots = stof(v); break; }
137 case "F": { sflags = stof(v); break; }
138 case "M": { modtype = strtolower(v); break; }
142 if(modtype != "xonotic") { impure += autocvar_menu_slist_modimpurity; }
144 // check if this server is favorited
145 if(gethostcachenumber(SLIST_FIELD_ISFAVORITE, entry)) { return CAT_FAVORITED; }
147 // now check if it's recommended
148 if(autocvar_menu_slist_recommendations)
150 string cname = gethostcachestring(SLIST_FIELD_CNAME, entry);
152 if(IsPromoted(cname)) { return CAT_RECOMMENDED; }
155 float recommended = 0;
156 if(autocvar_menu_slist_recommendations & 1)
158 if(IsRecommended(cname)) { ++recommended; }
159 else { --recommended; }
161 if(autocvar_menu_slist_recommendations & 2)
164 ///// check for minimum free slots
165 (freeslots >= autocvar_menu_slist_recommendations_minfreeslots)
167 && // check for purity requirement
169 (autocvar_menu_slist_recommendations_purethreshold < 0)
171 (impure <= autocvar_menu_slist_recommendations_purethreshold)
174 && // check for minimum amount of humans
176 gethostcachenumber(SLIST_FIELD_NUMHUMANS, entry)
178 autocvar_menu_slist_recommendations_minhumans
181 && // check for maximum latency
183 gethostcachenumber(SLIST_FIELD_PING, entry)
185 autocvar_menu_slist_recommendations_maxping
192 if(recommended > 0) { return CAT_RECOMMENDED; }
196 // if not favorited or recommended, check modname
197 if(modtype != "xonotic")
201 // old servers which don't report their mod name are considered modified now
202 case "": { return CAT_MODIFIED; }
204 case "xpm": { return CAT_XPM; }
206 case "instagib": { return CAT_INSTAGIB; }
207 case "overkill": { return CAT_OVERKILL; }
208 //case "nix": { return CAT_NIX; }
209 //case "newtoys": { return CAT_NEWTOYS; }
211 // "cts" is allowed as compat, xdf is replacement
213 case "xdf": { return CAT_DEFRAG; }
215 default: { LOG_TRACEF("Found strange mod type: %s\n", modtype); return CAT_MODIFIED; }
219 // must be normal or impure server
220 return ((impure > autocvar_menu_slist_purethreshold) ? CAT_MODIFIED : CAT_NORMAL);
223 METHOD(XonoticServerList, toggleFavorite, void(XonoticServerList this, string srv))
226 string srv_resolved = netaddress_resolve(srv, 26000);
227 string p = crypto_getidfp(srv_resolved);
228 string s = cvar_string("net_slist_favorites");
230 for (int i = 0, n = tokenize_console(s); i < n; ++i)
233 if (substring(argv(i), 0, 1) != "[" && strlen(argv(i)) == 44 && strstrofs(argv(i), ".", 0) < 0)
235 // it's a pubkey hash
236 match = (p && p == argv(i));
241 match = (srv_resolved == netaddress_resolve(argv(i), 26000));
243 if (!match) continue;
246 string before = (i > 0) ? substring(s, 0, argv_end_index(i - 1)) : "";
247 string after = (i < n - 1) ? substring(s, argv_start_index(i + 1), -1) : "";
248 s = strcat(before, (before != "" && after != "" ? " " : ""), after);
251 // TODO: why not break here?
252 n = tokenize_console(s);
253 --i; // offset the increment that is about to happen
257 ret = strcat(s, (s != "" ? " " : ""), p ? p : srv);
259 cvar_set("net_slist_favorites", ret);
260 this.refreshServerList(this, REFRESHSERVERLIST_RESORT);
263 void ServerList_Update_favoriteButton(entity btn, entity me)
265 entity e = me.favoriteButton;
266 if(IsFavorite(me.ipAddressBox.text))
268 e.setText(e, _("Remove"));
273 e.setText(e, _("Favorite"));
274 setZonedTooltip(e, _("Bookmark the currently highlighted server so that it's faster to find in the future"), string_null);
278 entity makeXonoticServerList()
281 me = NEW(XonoticServerList);
282 me.configureXonoticServerList(me);
285 void XonoticServerList_configureXonoticServerList(entity me)
287 me.configureXonoticListBox(me);
290 #define SLIST_FIELD(suffix,name) SLIST_FIELD_##suffix = gethostcacheindexforkey(name);
297 void XonoticServerList_setSelected(entity me, int i)
299 me.lockedSelectedItem = false;
300 //int save = me.selectedItem;
301 SUPER(XonoticServerList).setSelected(me, i);
303 if(me.selectedItem == save)
308 if(gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT) != me.nItems)
309 return; // sorry, it would be wrong
311 if(me.selectedServer)
312 strunzone(me.selectedServer);
313 me.selectedServer = strzone(gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
315 me.ipAddressBox.setText(me.ipAddressBox, me.selectedServer);
316 me.ipAddressBox.cursorPos = strlen(me.selectedServer);
317 me.ipAddressBoxFocused = -1;
319 void XonoticServerList_refreshServerList(entity me, int mode)
321 //print("refresh of type ", ftos(mode), "\n");
323 if(mode >= REFRESHSERVERLIST_REFILTER)
328 string s, typestr, modstr;
332 m = strstrofs(s, ":", 0);
335 typestr = substring(s, 0, m);
336 s = substring(s, m + 1, strlen(s) - m - 1);
337 while(substring(s, 0, 1) == " ")
338 s = substring(s, 1, strlen(s) - 1);
343 modstr = cvar_string("menu_slist_modfilter");
345 m = SLIST_MASK_AND - 1;
346 resethostcachemasks();
348 // ping: reject negative ping (no idea why this happens in the first place, engine bug)
349 sethostcachemasknumber(++m, SLIST_FIELD_PING, 0, SLIST_TEST_GREATEREQUAL);
352 if(!me.filterShowFull)
354 sethostcachemasknumber(++m, SLIST_FIELD_FREESLOTS, 1, SLIST_TEST_GREATEREQUAL); // legacy
355 sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, ":S0:", SLIST_TEST_NOTCONTAIN); // g_maxplayers support
359 if(!me.filterShowEmpty)
360 sethostcachemasknumber(++m, SLIST_FIELD_NUMHUMANS, 1, SLIST_TEST_GREATEREQUAL);
362 // gametype filtering
364 sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, strcat(typestr, ":"), SLIST_TEST_STARTSWITH);
369 if(substring(modstr, 0, 1) == "!")
370 sethostcachemaskstring(++m, SLIST_FIELD_MOD, resolvemod(substring(modstr, 1, strlen(modstr) - 1)), SLIST_TEST_NOTEQUAL);
372 sethostcachemaskstring(++m, SLIST_FIELD_MOD, resolvemod(modstr), SLIST_TEST_EQUAL);
376 n = tokenizebyseparator(_Nex_ExtResponseSystem_BannedServers, " ");
377 for(i = 0; i < n; ++i)
379 sethostcachemaskstring(++m, SLIST_FIELD_CNAME, argv(i), SLIST_TEST_NOTSTARTSWITH);
381 m = SLIST_MASK_OR - 1;
384 sethostcachemaskstring(++m, SLIST_FIELD_NAME, s, SLIST_TEST_CONTAINS);
385 sethostcachemaskstring(++m, SLIST_FIELD_MAP, s, SLIST_TEST_CONTAINS);
386 sethostcachemaskstring(++m, SLIST_FIELD_PLAYERS, s, SLIST_TEST_CONTAINS);
387 sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, strcat(s, ":"), SLIST_TEST_STARTSWITH);
391 //listflags |= SLSF_FAVORITES;
392 listflags |= SLSF_CATEGORIES;
393 if(me.currentSortOrder < 0) { listflags |= SLSF_DESCENDING; }
394 sethostcachesort(me.currentSortField, listflags);
398 if(mode >= REFRESHSERVERLIST_ASK)
399 refreshhostcache(mode >= REFRESHSERVERLIST_RESET);
401 void XonoticServerList_focusEnter(entity me)
403 SUPER(XonoticServerList).focusEnter(me);
404 if(time < me.nextRefreshTime)
406 //print("sorry, no refresh yet\n");
409 me.nextRefreshTime = time + 10;
410 me.refreshServerList(me, REFRESHSERVERLIST_ASK);
413 void XonoticServerList_draw(entity me)
416 bool found = false, owned;
418 if(_Nex_ExtResponseSystem_BannedServersNeedsRefresh)
422 _Nex_ExtResponseSystem_BannedServersNeedsRefresh = 0;
425 if(_Nex_ExtResponseSystem_PromotedServersNeedsRefresh)
429 _Nex_ExtResponseSystem_PromotedServersNeedsRefresh = 0;
432 if(_Nex_ExtResponseSystem_RecommendedServersNeedsRefresh)
436 _Nex_ExtResponseSystem_RecommendedServersNeedsRefresh = 0;
439 if(me.currentSortField == -1)
441 me.setSortOrder(me, SLIST_FIELD_PING, +1);
442 me.refreshServerList(me, REFRESHSERVERLIST_RESET);
444 else if(me.needsRefresh == 1)
446 me.needsRefresh = 2; // delay by one frame to make sure "slist" has been executed
448 else if(me.needsRefresh == 2)
451 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
453 else if(me.needsRefresh == 3)
456 me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
459 owned = ((me.selectedServer == me.ipAddressBox.text) && (me.ipAddressBox.text != ""));
461 for(i = 0; i < category_draw_count; ++i) { category_name[i] = -1; category_item[i] = -1; }
462 category_draw_count = 0;
464 if(autocvar_menu_slist_categories >= 0) // if less than 0, don't even draw a category heading for favorites
466 float itemcount = gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT);
467 me.nItems = itemcount;
469 //float visible = floor(me.scrollPos / me.itemHeight);
470 // ^ unfortunately no such optimization can be made-- we must process through the
471 // entire list, otherwise there is no way to know which item is first in its category.
473 // binary search method suggested by div
476 for(x = 1; x <= category_ent_count; ++x) {
478 float last = (itemcount - 1);
483 float catf = gethostcachenumber(SLIST_FIELD_CATEGORY, first);
484 float catl = gethostcachenumber(SLIST_FIELD_CATEGORY, last);
486 // The first one is already > x.
487 // Therefore, category x does not exist.
488 // Higher numbered categories do exist though.
489 } else if (catl < x) {
490 // The last one is < x.
491 // Thus this category - and any following -
494 } else if (catf == x) {
495 // Starts at first. This breaks the loop
496 // invariant in the binary search and thus has
497 // to be handled separately.
498 if(gethostcachenumber(SLIST_FIELD_CATEGORY, first) != x)
499 error("Category mismatch I");
501 if(gethostcachenumber(SLIST_FIELD_CATEGORY, first - 1) == x)
502 error("Category mismatch II");
503 category_name[category_draw_count] = x;
504 category_item[category_draw_count] = first;
505 ++category_draw_count;
508 // At this point, catf <= x < catl, thus
509 // catf < catl, thus first < last.
512 // catf == gethostcachenumber(SLIST_FIELD_CATEGORY(first)
513 // catl == gethostcachenumber(SLIST_FIELD_CATEGORY(last)
516 while (last - first > 1) {
517 float middle = floor((first + last) / 2);
518 // By loop condition, middle != first && middle != last.
519 float cat = gethostcachenumber(SLIST_FIELD_CATEGORY, middle);
529 if(gethostcachenumber(SLIST_FIELD_CATEGORY, last) != x)
530 error("Category mismatch III");
532 if(gethostcachenumber(SLIST_FIELD_CATEGORY, last - 1) == x)
533 error("Category mismatch IV");
534 category_name[category_draw_count] = x;
535 category_item[category_draw_count] = last;
536 ++category_draw_count;
537 begin = last + 1; // already scanned through these, skip 'em
540 begin = last; // already scanned through these, skip 'em
543 if(autocvar_menu_slist_categories_onlyifmultiple && (category_draw_count == 1))
545 category_name[0] = -1;
546 category_item[0] = -1;
547 category_draw_count = 0;
548 me.nItems = itemcount;
551 else { me.nItems = gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT); }
553 me.connectButton.disabled = ((me.nItems == 0) && (me.ipAddressBox.text == ""));
554 me.infoButton.disabled = ((me.nItems == 0) || !owned);
555 me.favoriteButton.disabled = ((me.nItems == 0) && (me.ipAddressBox.text == ""));
557 if(me.lockedSelectedItem)
561 if(gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem) != me.selectedServer)
563 if(me.selectedServer)
564 strunzone(me.selectedServer);
565 me.selectedServer = strzone(gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
570 else if(me.selectedServer)
572 for(i = 0; i < me.nItems; ++i)
574 if(gethostcachestring(SLIST_FIELD_CNAME, i) == me.selectedServer)
576 // don't follow the selected item with SUPER(XonoticServerList).setSelected(me, i);
587 // selected server disappeared, select the last server (scrolling to it)
588 if(me.selectedItem >= me.nItems)
589 SUPER(XonoticServerList).setSelected(me, me.nItems - 1);
590 if(me.selectedServer)
591 strunzone(me.selectedServer);
592 me.selectedServer = strzone(gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
598 if(me.selectedServer != me.ipAddressBox.text)
600 me.ipAddressBox.setText(me.ipAddressBox, me.selectedServer);
601 me.ipAddressBox.cursorPos = strlen(me.selectedServer);
602 me.ipAddressBoxFocused = -1;
606 if(me.ipAddressBoxFocused != me.ipAddressBox.focused)
608 if(me.ipAddressBox.focused || me.ipAddressBoxFocused < 0)
609 ServerList_Update_favoriteButton(NULL, me);
610 me.ipAddressBoxFocused = me.ipAddressBox.focused;
613 SUPER(XonoticServerList).draw(me);
615 void ServerList_PingSort_Click(entity btn, entity me)
617 me.setSortOrder(me, SLIST_FIELD_PING, +1);
619 void ServerList_NameSort_Click(entity btn, entity me)
621 me.setSortOrder(me, SLIST_FIELD_NAME, -1); // why?
623 void ServerList_MapSort_Click(entity btn, entity me)
625 me.setSortOrder(me, SLIST_FIELD_MAP, -1); // why?
627 void ServerList_PlayerSort_Click(entity btn, entity me)
629 me.setSortOrder(me, SLIST_FIELD_NUMHUMANS, -1);
631 void ServerList_TypeSort_Click(entity btn, entity me)
636 m = strstrofs(s, ":", 0);
639 s = substring(s, 0, m);
640 while(substring(s, m+1, 1) == " ") // skip spaces
646 for(i = 1; ; i *= 2) // 20 modes ought to be enough for anyone
648 t = MapInfo_Type_ToString(i);
650 if(t == "") // it repeats (default case)
653 // choose the first one
654 s = MapInfo_Type_ToString(1);
659 // the type was found
660 // choose the next one
661 s = MapInfo_Type_ToString(i * 2);
663 s = MapInfo_Type_ToString(1);
670 s = strcat(s, substring(me.filterString, m+1, strlen(me.filterString) - m - 1));
672 me.controlledTextbox.setText(me.controlledTextbox, s);
673 me.controlledTextbox.keyDown(me.controlledTextbox, K_END, 0, 0);
674 me.controlledTextbox.keyUp(me.controlledTextbox, K_END, 0, 0);
675 //ServerList_Filter_Change(me.controlledTextbox, me);
677 void ServerList_Filter_Change(entity box, entity me)
680 strunzone(me.filterString);
682 me.filterString = strzone(box.text);
684 me.filterString = string_null;
685 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
687 me.ipAddressBox.setText(me.ipAddressBox, "");
688 me.ipAddressBox.cursorPos = 0;
689 me.ipAddressBoxFocused = -1;
691 void ServerList_Categories_Click(entity box, entity me)
693 box.setChecked(box, autocvar_menu_slist_categories = !autocvar_menu_slist_categories);
694 me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
696 me.ipAddressBox.setText(me.ipAddressBox, "");
697 me.ipAddressBox.cursorPos = 0;
698 me.ipAddressBoxFocused = -1;
700 void ServerList_ShowEmpty_Click(entity box, entity me)
702 box.setChecked(box, me.filterShowEmpty = !me.filterShowEmpty);
703 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
705 me.ipAddressBox.setText(me.ipAddressBox, "");
706 me.ipAddressBox.cursorPos = 0;
707 me.ipAddressBoxFocused = -1;
709 void ServerList_ShowFull_Click(entity box, entity me)
711 box.setChecked(box, me.filterShowFull = !me.filterShowFull);
712 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
714 me.ipAddressBox.setText(me.ipAddressBox, "");
715 me.ipAddressBox.cursorPos = 0;
716 me.ipAddressBoxFocused = -1;
718 void XonoticServerList_setSortOrder(entity me, int fld, int direction)
720 if(me.currentSortField == fld)
721 direction = -me.currentSortOrder;
722 me.currentSortOrder = direction;
723 me.currentSortField = fld;
724 me.sortButton1.forcePressed = (fld == SLIST_FIELD_PING);
725 me.sortButton2.forcePressed = (fld == SLIST_FIELD_NAME);
726 me.sortButton3.forcePressed = (fld == SLIST_FIELD_MAP);
727 me.sortButton4.forcePressed = 0;
728 me.sortButton5.forcePressed = (fld == SLIST_FIELD_NUMHUMANS);
730 if(me.selectedServer)
731 strunzone(me.selectedServer);
732 me.selectedServer = string_null;
733 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
735 void XonoticServerList_positionSortButton(entity me, entity btn, float theOrigin, float theSize, string theTitle, void(entity, entity) theFunc)
737 vector originInLBSpace, sizeInLBSpace;
738 originInLBSpace = eY * (-me.itemHeight);
739 sizeInLBSpace = eY * me.itemHeight + eX * (1 - me.controlWidth);
741 vector originInDialogSpace, sizeInDialogSpace;
742 originInDialogSpace = boxToGlobal(originInLBSpace, me.Container_origin, me.Container_size);
743 sizeInDialogSpace = boxToGlobalSize(sizeInLBSpace, me.Container_size);
745 btn.Container_origin_x = originInDialogSpace.x + sizeInDialogSpace.x * theOrigin;
746 btn.Container_size_x = sizeInDialogSpace.x * theSize;
747 btn.setText(btn, theTitle);
748 btn.onClick = theFunc;
749 btn.onClickEntity = me;
752 void XonoticServerList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
754 SUPER(XonoticServerList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
756 me.realFontSize_y = me.fontSize / (absSize.y * me.itemHeight);
757 me.realFontSize_x = me.fontSize / (absSize.x * (1 - me.controlWidth));
758 me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
760 me.columnIconsOrigin = 0;
761 me.columnIconsSize = me.realFontSize.x * 4 * me.iconsSizeFactor;
762 me.columnPingSize = me.realFontSize.x * 3;
763 me.columnMapSize = me.realFontSize.x * 10;
764 me.columnTypeSize = me.realFontSize.x * 4;
765 me.columnPlayersSize = me.realFontSize.x * 5;
766 me.columnNameSize = 1 - me.columnPlayersSize - me.columnMapSize - me.columnPingSize - me.columnIconsSize - me.columnTypeSize - 5 * me.realFontSize.x;
767 me.columnPingOrigin = me.columnIconsOrigin + me.columnIconsSize + me.realFontSize.x;
768 me.columnNameOrigin = me.columnPingOrigin + me.columnPingSize + me.realFontSize.x;
769 me.columnMapOrigin = me.columnNameOrigin + me.columnNameSize + me.realFontSize.x;
770 me.columnTypeOrigin = me.columnMapOrigin + me.columnMapSize + me.realFontSize.x;
771 me.columnPlayersOrigin = me.columnTypeOrigin + me.columnTypeSize + me.realFontSize.x;
773 me.positionSortButton(me, me.sortButton1, me.columnPingOrigin, me.columnPingSize, _("Ping"), ServerList_PingSort_Click);
774 me.positionSortButton(me, me.sortButton2, me.columnNameOrigin, me.columnNameSize, _("Host name"), ServerList_NameSort_Click);
775 me.positionSortButton(me, me.sortButton3, me.columnMapOrigin, me.columnMapSize, _("Map"), ServerList_MapSort_Click);
776 me.positionSortButton(me, me.sortButton4, me.columnTypeOrigin, me.columnTypeSize, _("Type"), ServerList_TypeSort_Click);
777 me.positionSortButton(me, me.sortButton5, me.columnPlayersOrigin, me.columnPlayersSize, _("Players"), ServerList_PlayerSort_Click);
779 int f = me.currentSortField;
782 me.currentSortField = -1;
783 me.setSortOrder(me, f, me.currentSortOrder); // force resetting the sort order
786 void ServerList_Connect_Click(entity btn, entity me)
788 localcmd(sprintf("connect %s\n",
789 ((me.ipAddressBox.text != "") ?
790 me.ipAddressBox.text : me.selectedServer
794 void ServerList_Favorite_Click(entity btn, entity this)
796 string addr = this.ipAddressBox.text;
797 string ipstr = netaddress_resolve(addr, 26000);
798 if (ipstr == "") return;
799 m_play_click_sound(MENU_SOUND_SELECT);
800 this.toggleFavorite(this, addr);
801 this.ipAddressBoxFocused = -1;
803 void ServerList_Info_Click(entity btn, entity me)
806 main.serverInfoDialog.loadServerInfo(main.serverInfoDialog, me.selectedItem);
808 vector org = boxToGlobal(eY * (me.selectedItem * me.itemHeight - me.scrollPos), me.origin, me.size);
809 vector sz = boxToGlobalSize(eY * me.itemHeight + eX * (1 - me.controlWidth), me.size);
810 DialogOpenButton_Click_withCoords(me, main.serverInfoDialog, org, sz);
812 void XonoticServerList_doubleClickListBoxItem(entity me, int i, vector where)
814 ServerList_Connect_Click(NULL, me);
816 void XonoticServerList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
818 // layout: Ping, Server name, Map name, NP, TP, MP
824 bool pure = false, pure_available = false;
825 int freeslots = -1, sflags = -1, j, m;
826 string s, typestr, versionstr, k, v, modname;
828 //printf("time: %f, i: %d, item: %d, nitems: %d\n", time, i, item, me.nItems);
830 vector oldscale = draw_scale;
831 vector oldshift = draw_shift;
832 #define SET_YRANGE(start,end) \
833 draw_scale = boxToGlobalSize(eX * 1 + eY * (end - start), oldscale); \
834 draw_shift = boxToGlobal(eY * start, oldshift, oldscale);
836 for (j = 0; j < category_draw_count; ++j) {
837 // Matches exactly the headings with increased height.
838 if (i == category_item[j])
842 if (j < category_draw_count)
844 entity catent = RetrieveCategoryEnt(category_name[j]);
848 (me.categoriesHeight - 1) / (me.categoriesHeight + 1),
849 me.categoriesHeight / (me.categoriesHeight + 1)
852 eY * me.realUpperMargin
855 eX * (me.columnNameOrigin + (me.columnNameSize - draw_TextWidth(catent.cat_string, 0, me.realFontSize)) * 0.5),
858 eX * (me.columnNameOrigin),
859 strcat(catent.cat_string, ":"),
862 SKINCOLOR_SERVERLIST_CATEGORY,
863 SKINALPHA_SERVERLIST_CATEGORY,
866 SET_YRANGE(me.categoriesHeight / (me.categoriesHeight + 1), 1);
871 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
874 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
875 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
878 s = gethostcachestring(SLIST_FIELD_QCSTATUS, i);
879 m = tokenizebyseparator(s, ":");
884 versionstr = argv(1);
888 for(j = 2; j < m; ++j)
892 k = substring(argv(j), 0, 1);
893 v = substring(argv(j), 1, -1);
896 pure = (stof(v) == 0);
897 pure_available = true;
907 #ifdef COMPAT_NO_MOD_IS_XONOTIC
912 string original_modname = modname;
913 modname = strtolower(modname);
916 SLIST_FIELD_MOD = gethostcacheindexforkey("mod");
917 s = gethostcachestring(SLIST_FIELD_MOD, i);
919 if(modname == "xonotic")
923 // list the mods here on which the pure server check actually works
924 if(modname != "xonotic")
925 if(modname != "instagib" || modname != "minstagib")
928 if(modname != "newtoys")
929 pure_available = false;
931 if(gethostcachenumber(SLIST_FIELD_FREESLOTS, i) <= 0)
932 theAlpha = SKINALPHA_SERVERLIST_FULL;
933 else if(freeslots == 0)
934 theAlpha = SKINALPHA_SERVERLIST_FULL; // g_maxplayers support
935 else if (!gethostcachenumber(SLIST_FIELD_NUMHUMANS, i))
936 theAlpha = SKINALPHA_SERVERLIST_EMPTY;
940 p = gethostcachenumber(SLIST_FIELD_PING, i);
941 const int PING_LOW = 75;
942 const int PING_MED = 200;
943 const int PING_HIGH = 500;
945 theColor = SKINCOLOR_SERVERLIST_LOWPING + (SKINCOLOR_SERVERLIST_MEDPING - SKINCOLOR_SERVERLIST_LOWPING) * (p / PING_LOW);
946 else if(p < PING_MED)
947 theColor = SKINCOLOR_SERVERLIST_MEDPING + (SKINCOLOR_SERVERLIST_HIGHPING - SKINCOLOR_SERVERLIST_MEDPING) * ((p - PING_LOW) / (PING_MED - PING_LOW));
948 else if(p < PING_HIGH)
950 theColor = SKINCOLOR_SERVERLIST_HIGHPING;
951 theAlpha *= 1 + (SKINALPHA_SERVERLIST_HIGHPING - 1) * ((p - PING_MED) / (PING_HIGH - PING_MED));
956 theAlpha *= SKINALPHA_SERVERLIST_HIGHPING;
959 if(gethostcachenumber(SLIST_FIELD_ISFAVORITE, i))
961 theColor = theColor * (1 - SKINALPHA_SERVERLIST_FAVORITE) + SKINCOLOR_SERVERLIST_FAVORITE * SKINALPHA_SERVERLIST_FAVORITE;
962 theAlpha = theAlpha * (1 - SKINALPHA_SERVERLIST_FAVORITE) + SKINALPHA_SERVERLIST_FAVORITE;
965 s = gethostcachestring(SLIST_FIELD_CNAME, i);
968 if(substring(s, 0, 1) == "[")
973 else if(strstrofs("0123456789", substring(s, 0, 1), 0) >= 0)
979 q = stof(substring(crypto_getencryptlevel(s), 0, 1));
980 if((q <= 0 && cvar("crypto_aeslevel") >= 3) || (q >= 3 && cvar("crypto_aeslevel") <= 0))
982 theColor = SKINCOLOR_SERVERLIST_IMPOSSIBLE;
983 theAlpha = SKINALPHA_SERVERLIST_IMPOSSIBLE;
988 if(cvar("crypto_aeslevel") >= 2)
993 if(cvar("crypto_aeslevel") >= 1)
1003 // 2: AES recommended but not available
1004 // 3: AES possible and will be used
1005 // 4: AES recommended and will be used
1011 vector iconSize = '0 0 0';
1012 iconSize_y = me.realFontSize.y * me.iconsSizeFactor;
1013 iconSize_x = me.realFontSize.x * me.iconsSizeFactor;
1015 vector iconPos = '0 0 0';
1016 iconPos_x = (me.columnIconsSize - 3 * iconSize.x) * 0.5;
1017 iconPos_y = (1 - iconSize.y) * 0.5;
1020 if(me.seenIPv4 && me.seenIPv6)
1023 draw_Picture(iconPos, "icon_ipv6", iconSize, '1 1 1', 1);
1025 draw_Picture(iconPos, "icon_ipv4", iconSize, '1 1 1', 1);
1028 iconPos.x += iconSize.x;
1032 draw_Picture(iconPos, strcat("icon_aeslevel", ftos(q)), iconSize, '1 1 1', 1);
1034 iconPos.x += iconSize.x;
1037 if(modname == "xonotic")
1039 // Here, pure_available should always be set. If not, consider
1041 if(pure_available && pure)
1042 draw_Picture(iconPos, "icon_pure1", iconSize, '1 1 1', 1);
1046 string icon = strcat("icon_mod_", modname);
1047 if(draw_PictureSize(icon) == '0 0 0')
1050 // In mods, pure_available not being present indicates
1051 // non-support of the feature. Just show the mod icon as is
1053 if(pure_available && !pure)
1054 draw_Picture(iconPos, icon, iconSize, '1 1 1', SKINALPHA_SERVERLIST_ICON_NONPURE);
1056 draw_Picture(iconPos, icon, iconSize, '1 1 1', 1);
1059 iconPos.x += iconSize.x;
1062 if(sflags >= 0 && (sflags & SERVERFLAG_PLAYERSTATS))
1063 draw_Picture(iconPos, "icon_stats1", iconSize, '1 1 1', 1);
1065 if(isFocused && me.mouseOverIcons && !me.tooltip)
1068 if(me.seenIPv4 && me.seenIPv6)
1069 t = strcat(t, (isv6) ? "IPv6, " : "IPv4, ");
1070 t = strcat(t, _("encryption:"), " ", (q ? sprintf(_("AES level %d"), q) : ZCTX(_("ENC^none"))), ", ");
1071 t = strcat(t, sprintf(_("mod: %s"), ((modname == "xonotic") ? ZCTX(_("MOD^Default")) : original_modname)));
1073 t = strcat(t, sprintf(_(" (%s)"), (pure) ? _("official settings") : _("modified settings")));
1074 t = strcat(t, ", ");
1075 t = strcat(t, ((sflags >= 0 && (sflags & SERVERFLAG_PLAYERSTATS)) ? _("stats enabled") : _("stats disabled")));
1076 setZonedTooltip(me, t, string_null);
1084 draw_Text(me.realUpperMargin * eY + (me.columnPingOrigin + me.columnPingSize - draw_TextWidth(s, 0, me.realFontSize)) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1087 s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_NAME, i), me.columnNameSize, 0, me.realFontSize);
1088 draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, theColor, theAlpha, 0);
1091 s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_MAP, i), me.columnMapSize, 0, me.realFontSize);
1092 draw_Text(me.realUpperMargin * eY + (me.columnMapOrigin + (me.columnMapSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1095 s = draw_TextShortenToWidth(typestr, me.columnTypeSize, 0, me.realFontSize);
1096 draw_Text(me.realUpperMargin * eY + (me.columnTypeOrigin + (me.columnTypeSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1098 // server playercount
1099 s = strcat(ftos(gethostcachenumber(SLIST_FIELD_NUMHUMANS, i)), "/", ftos(gethostcachenumber(SLIST_FIELD_MAXPLAYERS, i)));
1100 draw_Text(me.realUpperMargin * eY + (me.columnPlayersOrigin + (me.columnPlayersSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1103 void XonoticServerList_focusedItemChangeNotify(entity me)
1108 float XonoticServerList_mouseMove(entity me, vector pos)
1110 if(!SUPER(XonoticServerList).mouseMove(me, pos))
1112 me.mouseOverIcons = false;
1117 me.mouseOverIcons = (pos_x <= me.columnIconsSize);
1118 if(!me.mouseOverIcons)
1123 bool XonoticServerList_keyDown(entity me, int scan, bool ascii, bool shift)
1127 org = boxToGlobal(eY * (me.selectedItem * me.itemHeight - me.scrollPos), me.origin, me.size);
1128 sz = boxToGlobalSize(eY * me.itemHeight + eX * (1 - me.controlWidth), me.size);
1130 if(scan == K_ENTER || scan == K_KP_ENTER)
1132 ServerList_Connect_Click(NULL, me);
1135 else if(scan == K_MOUSE2 || scan == K_SPACE)
1139 m_play_click_sound(MENU_SOUND_OPEN);
1140 main.serverInfoDialog.loadServerInfo(main.serverInfoDialog, me.selectedItem);
1141 DialogOpenButton_Click_withCoords(me, main.serverInfoDialog, org, sz);
1146 else if(scan == K_INS || scan == K_MOUSE3 || scan == K_KP_INS)
1150 me.toggleFavorite(me, me.selectedServer);
1151 me.ipAddressBoxFocused = -1;
1156 else if(SUPER(XonoticServerList).keyDown(me, scan, ascii, shift))
1158 else if(!me.controlledTextbox)
1161 return me.controlledTextbox.keyDown(me.controlledTextbox, scan, ascii, shift);
1164 float XonoticServerList_getTotalHeight(entity me)
1166 float num_normal_rows = me.nItems;
1167 int num_headers = category_draw_count;
1168 return me.itemHeight * (num_normal_rows + me.categoriesHeight * num_headers);
1170 int XonoticServerList_getItemAtPos(entity me, float pos)
1172 pos = pos / me.itemHeight;
1174 for (i = category_draw_count - 1; i >= 0; --i) {
1175 int itemidx = category_item[i];
1176 float itempos = i * me.categoriesHeight + category_item[i];
1177 if (pos >= itempos + me.categoriesHeight + 1)
1178 return itemidx + 1 + floor(pos - (itempos + me.categoriesHeight + 1));
1182 // No category matches? Note that category 0 is... 0. Therefore no headings exist at all.
1185 float XonoticServerList_getItemStart(entity me, int item)
1188 for (i = category_draw_count - 1; i >= 0; --i) {
1189 int itemidx = category_item[i];
1190 float itempos = i * me.categoriesHeight + category_item[i];
1191 if (item >= itemidx + 1)
1192 return (itempos + me.categoriesHeight + 1 + item - (itemidx + 1)) * me.itemHeight;
1193 if (item >= itemidx)
1194 return itempos * me.itemHeight;
1196 // No category matches? Note that category 0 is... 0. Therefore no headings exist at all.
1197 return item * me.itemHeight;
1199 float XonoticServerList_getItemHeight(entity me, int item)
1202 for (i = 0; i < category_draw_count; ++i) {
1203 // Matches exactly the headings with increased height.
1204 if (item == category_item[i])
1205 return me.itemHeight * (me.categoriesHeight + 1);
1207 return me.itemHeight;