1 #include "serverlist.qh"
5 #include "mainwindow.qh"
6 #include "dialog_multiplayer_join_serverinfo.qh"
7 #include <common/mapinfo.qh>
9 void SL_ProcessCategoryOverrides(.string override_field_string, .float override_field)
11 for (int i = 0; i < category_ent_count; ++i)
13 string s = categories[i].override_field_string;
14 if (s != "" && s != categories[i].cat_name)
17 for (int x = 0; x < category_ent_count; ++x)
19 if(categories[x].cat_name == s)
27 strfree(categories[i].override_field_string);
28 categories[i].override_field = catnum;
31 LOG_INFOF("RegisterSLCategories(): Improper override '%s' for category '%s'!", s, categories[i].cat_name);
33 strfree(categories[i].override_field_string);
34 categories[i].override_field = 0;
38 void RegisterSLCategories()
41 #define SLIST_CATEGORY(name,enoverride,dioverride,str) \
42 SET_FIELD_COUNT(name, CATEGORY_FIRST, category_ent_count) \
43 CHECK_MAX_COUNT(name, MAX_CATEGORIES, category_ent_count, "SLIST_CATEGORY") \
44 cat = categories[name - 1] = new(slist_category); \
45 cat.cat_name = #name; \
46 cat.cat_enoverride_string = strzone(SLIST_CATEGORY_AUTOCVAR(name)); \
47 cat.cat_dioverride_string = strzone(dioverride); \
48 cat.cat_string = strzone(str);
52 SL_ProcessCategoryOverrides(cat_enoverride_string, cat_enoverride);
53 SL_ProcessCategoryOverrides(cat_dioverride_string, cat_dioverride);
56 // Supporting Functions
57 entity RetrieveCategoryEnt(int catnum)
59 if((catnum > 0) && (catnum <= category_ent_count))
61 return categories[catnum - 1];
65 error(sprintf("RetrieveCategoryEnt(%d): Improper category number!\n", catnum));
70 bool IsServerInList(string list, string srv)
74 srv = netaddress_resolve(srv, 26000);
77 string p = crypto_getidfp(srv);
78 int n = tokenize_console(list);
79 for(int i = 0; i < n; ++i)
81 if(substring(argv(i), 0, 1) != "[" && strlen(argv(i)) == 44 && strstrofs(argv(i), ".", 0) < 0)
88 if(srv == netaddress_resolve(argv(i), 26000))
95 int CategoryOverride(int cat)
97 entity catent = RetrieveCategoryEnt(cat);
100 int override = (autocvar_menu_slist_categories ? catent.cat_enoverride : catent.cat_dioverride);
101 if(override) { return override; }
106 error(sprintf("CategoryOverride(%d): Improper category number!\n", cat));
111 int CategoryForEntry(int entry)
113 string s, k, v, modtype = "";
114 int j, m, impure = 0, freeslots = 0, sflags = 0;
115 s = gethostcachestring(SLIST_FIELD_QCSTATUS, entry);
116 m = tokenizebyseparator(s, ":");
118 for(j = 2; j < m; ++j)
120 if(argv(j) == "") { break; }
121 k = substring(argv(j), 0, 1);
122 v = substring(argv(j), 1, -1);
125 case "P": { impure = stof(v); break; }
126 case "S": { freeslots = stof(v); break; }
127 case "F": { sflags = stof(v); break; }
128 case "M": { modtype = strtolower(v); break; }
132 if(modtype != "xonotic") { impure += autocvar_menu_slist_modimpurity; }
134 // check if this server is favorited
135 if(gethostcachenumber(SLIST_FIELD_ISFAVORITE, entry)) { return CAT_FAVORITED; }
137 // now check if it's recommended
138 if(autocvar_menu_slist_recommendations)
140 string cname = gethostcachestring(SLIST_FIELD_CNAME, entry);
142 if(IsPromoted(cname)) { return CAT_RECOMMENDED; }
145 float recommended = 0;
146 if(autocvar_menu_slist_recommendations & 1)
148 if(IsRecommended(cname)) { ++recommended; }
149 else { --recommended; }
151 if(autocvar_menu_slist_recommendations & 2)
154 ///// check for minimum free slots
155 (freeslots >= autocvar_menu_slist_recommendations_minfreeslots)
157 && // check for purity requirement
159 (autocvar_menu_slist_recommendations_purethreshold < 0)
161 (impure <= autocvar_menu_slist_recommendations_purethreshold)
164 && // check for minimum amount of humans
166 gethostcachenumber(SLIST_FIELD_NUMHUMANS, entry)
168 autocvar_menu_slist_recommendations_minhumans
171 && // check for maximum latency
173 gethostcachenumber(SLIST_FIELD_PING, entry)
175 autocvar_menu_slist_recommendations_maxping
182 if(recommended > 0) { return CAT_RECOMMENDED; }
186 // if not favorited or recommended, check modname
187 if(modtype != "xonotic")
191 // old servers which don't report their mod name are considered modified now
192 case "": { return CAT_MODIFIED; }
194 case "xpm": { return CAT_XPM; }
196 case "instagib": { return CAT_INSTAGIB; }
197 case "overkill": { return CAT_OVERKILL; }
199 // "cts" is allowed as compat, xdf is replacement
201 case "xdf": { return CAT_DEFRAG; }
203 default: { LOG_TRACEF("Found strange mod type: %s", modtype); return CAT_MODIFIED; }
207 // must be normal or impure server
208 return ((impure > autocvar_menu_slist_purethreshold) ? CAT_MODIFIED : CAT_NORMAL);
211 METHOD(XonoticServerList, toggleFavorite, void(XonoticServerList this, string srv))
214 string srv_resolved = netaddress_resolve(srv, 26000);
215 string p = crypto_getidfp(srv_resolved);
216 string s = cvar_string("net_slist_favorites");
218 for (int i = 0, n = tokenize_console(s); i < n; ++i)
221 if (substring(argv(i), 0, 1) != "[" && strlen(argv(i)) == 44 && strstrofs(argv(i), ".", 0) < 0)
223 // it's a pubkey hash
224 match = (p && p == argv(i));
229 match = (srv_resolved == netaddress_resolve(argv(i), 26000));
231 if (!match) continue;
234 string before = (i > 0) ? substring(s, 0, argv_end_index(i - 1)) : "";
235 string after = (i < n - 1) ? substring(s, argv_start_index(i + 1), -1) : "";
236 s = strcat(before, (before != "" && after != "" ? " " : ""), after);
239 // TODO: why not break here?
240 n = tokenize_console(s);
241 --i; // offset the increment that is about to happen
245 ret = strcat(s, (s != "" ? " " : ""), p ? p : srv);
247 cvar_set("net_slist_favorites", ret);
248 this.refreshServerList(this, REFRESHSERVERLIST_RESORT);
251 void ServerList_Update_favoriteButton(entity btn, entity me)
253 entity e = me.favoriteButton;
254 if(IsFavorite(me.ipAddressBox.text))
256 e.setText(e, ZCTX(_("SERVER^Remove favorite")));
257 setZonedTooltip(e, _("Remove the currently highlighted server from bookmarks"), string_null);
261 e.setText(e, ZCTX(_("SERVER^Favorite")));
262 setZonedTooltip(e, _("Bookmark the currently highlighted server so that it's faster to find in the future"), string_null);
266 entity makeXonoticServerList()
269 me = NEW(XonoticServerList);
270 me.configureXonoticServerList(me);
273 void XonoticServerList_configureXonoticServerList(entity me)
275 me.configureXonoticListBox(me);
278 #define SLIST_FIELD(suffix,name) SLIST_FIELD_##suffix = gethostcacheindexforkey(name);
285 void XonoticServerList_setSelected(entity me, int i)
287 me.lockedSelectedItem = false;
288 //int save = me.selectedItem;
289 SUPER(XonoticServerList).setSelected(me, i);
291 if(me.selectedItem == save)
296 if(gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT) != me.nItems)
297 return; // sorry, it would be wrong
299 strcpy(me.selectedServer, gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
301 me.ipAddressBox.setText(me.ipAddressBox, me.selectedServer);
302 me.ipAddressBox.cursorPos = strlen(me.selectedServer);
303 me.ipAddressBoxFocused = -1;
305 void XonoticServerList_refreshServerList(entity me, int mode)
307 //print("refresh of type ", ftos(mode), "\n");
309 if(mode >= REFRESHSERVERLIST_REFILTER)
311 string s = me.filterString;
314 int m = strstrofs(s, ":", 0);
317 typestr = substring(s, 0, m);
318 s = substring(s, m + 1, strlen(s) - m - 1);
319 while(substring(s, 0, 1) == " ")
320 s = substring(s, 1, strlen(s) - 1);
325 string modstr = cvar_string("menu_slist_modfilter");
327 m = SLIST_MASK_AND - 1;
328 resethostcachemasks();
330 // ping: reject negative ping (no idea why this happens in the first place, engine bug)
331 sethostcachemasknumber(++m, SLIST_FIELD_PING, 0, SLIST_TEST_GREATEREQUAL);
334 if(!me.filterShowFull)
336 sethostcachemasknumber(++m, SLIST_FIELD_FREESLOTS, 1, SLIST_TEST_GREATEREQUAL); // legacy
337 sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, ":S0:", SLIST_TEST_NOTCONTAIN); // g_maxplayers support
341 if(!me.filterShowEmpty)
342 sethostcachemasknumber(++m, SLIST_FIELD_NUMHUMANS, 1, SLIST_TEST_GREATEREQUAL);
345 if(!me.filterShowLaggy && autocvar_menu_slist_maxping > 0)
346 sethostcachemasknumber(++m, SLIST_FIELD_PING, autocvar_menu_slist_maxping, SLIST_TEST_LESSEQUAL);
348 // gametype filtering
350 sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, strcat(typestr, ":"), SLIST_TEST_STARTSWITH);
355 if(substring(modstr, 0, 1) == "!")
356 sethostcachemaskstring(++m, SLIST_FIELD_MOD, resolvemod(substring(modstr, 1, strlen(modstr) - 1)), SLIST_TEST_NOTEQUAL);
358 sethostcachemaskstring(++m, SLIST_FIELD_MOD, resolvemod(modstr), SLIST_TEST_EQUAL);
362 int n = tokenizebyseparator(_Nex_ExtResponseSystem_BannedServers, " ");
363 for(int i = 0; i < n; ++i)
365 sethostcachemaskstring(++m, SLIST_FIELD_CNAME, argv(i), SLIST_TEST_NOTSTARTSWITH);
367 m = SLIST_MASK_OR - 1;
370 sethostcachemaskstring(++m, SLIST_FIELD_NAME, s, SLIST_TEST_CONTAINS);
371 sethostcachemaskstring(++m, SLIST_FIELD_MAP, s, SLIST_TEST_CONTAINS);
372 sethostcachemaskstring(++m, SLIST_FIELD_PLAYERS, s, SLIST_TEST_CONTAINS);
373 sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, strcat(s, ":"), SLIST_TEST_STARTSWITH);
376 int sorting_flags = 0;
377 //sorting_flags |= SLSF_FAVORITES;
378 sorting_flags |= SLSF_CATEGORIES;
379 if(me.currentSortOrder < 0) { sorting_flags |= SLSF_DESCENDING; }
380 sethostcachesort(me.currentSortField, sorting_flags);
384 if(mode >= REFRESHSERVERLIST_ASK)
385 refreshhostcache(mode >= REFRESHSERVERLIST_RESET);
387 void XonoticServerList_focusEnter(entity me)
389 SUPER(XonoticServerList).focusEnter(me);
390 if(time < me.nextRefreshTime)
392 //print("sorry, no refresh yet\n");
395 me.nextRefreshTime = time + 10;
396 me.refreshServerList(me, REFRESHSERVERLIST_ASK);
399 void XonoticServerList_draw(entity me)
401 me.serversHeight = (autocvar_menu_slist_categories > 0 ? 1.0 : me.categoriesHeight);
403 if(_Nex_ExtResponseSystem_BannedServersNeedsRefresh)
407 _Nex_ExtResponseSystem_BannedServersNeedsRefresh = 0;
410 if(_Nex_ExtResponseSystem_PromotedServersNeedsRefresh)
414 _Nex_ExtResponseSystem_PromotedServersNeedsRefresh = 0;
417 if(_Nex_ExtResponseSystem_RecommendedServersNeedsRefresh)
421 _Nex_ExtResponseSystem_RecommendedServersNeedsRefresh = 0;
424 if(me.currentSortField == -1)
426 me.setSortOrder(me, SLIST_FIELD_PING, +1);
427 me.refreshServerList(me, REFRESHSERVERLIST_RESET);
429 else if(me.needsRefresh == 1)
431 me.needsRefresh = 2; // delay by one frame to make sure "slist" has been executed
433 else if(me.needsRefresh == 2)
436 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
438 else if(me.needsRefresh == 3)
441 me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
444 bool owned = ((me.selectedServer == me.ipAddressBox.text) && (me.ipAddressBox.text != ""));
446 for(int i = 0; i < category_draw_count; ++i) { category_name[i] = -1; category_item[i] = -1; }
447 category_draw_count = 0;
449 if(autocvar_menu_slist_categories >= 0) // if less than 0, don't even draw a category heading for favorites
451 float itemcount = gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT);
452 me.nItems = itemcount;
454 //float visible = floor(me.scrollPos / me.itemHeight);
455 // ^ unfortunately no such optimization can be made-- we must process through the
456 // entire list, otherwise there is no way to know which item is first in its category.
458 // binary search method suggested by div
460 for(int j = 1; j <= category_ent_count; ++j) {
462 float last = (itemcount - 1);
467 float catf = gethostcachenumber(SLIST_FIELD_CATEGORY, first);
468 float catl = gethostcachenumber(SLIST_FIELD_CATEGORY, last);
470 // The first one is already > j.
471 // Therefore, category j does not exist.
472 // Higher numbered categories do exist though.
473 } else if (catl < j) {
474 // The last one is < j.
475 // Thus this category - and any following -
478 } else if (catf == j) {
479 // Starts at first. This breaks the loop
480 // invariant in the binary search and thus has
481 // to be handled separately.
482 if(gethostcachenumber(SLIST_FIELD_CATEGORY, first) != j)
483 error("Category mismatch I");
485 if(gethostcachenumber(SLIST_FIELD_CATEGORY, first - 1) == j)
486 error("Category mismatch II");
487 category_name[category_draw_count] = j;
488 category_item[category_draw_count] = first;
489 ++category_draw_count;
492 // At this point, catf <= j < catl, thus
493 // catf < catl, thus first < last.
496 // catf == gethostcachenumber(SLIST_FIELD_CATEGORY(first)
497 // catl == gethostcachenumber(SLIST_FIELD_CATEGORY(last)
500 while (last - first > 1) {
501 float middle = floor((first + last) / 2);
502 // By loop condition, middle != first && middle != last.
503 float cat = gethostcachenumber(SLIST_FIELD_CATEGORY, middle);
513 if(gethostcachenumber(SLIST_FIELD_CATEGORY, last) != j)
514 error("Category mismatch III");
516 if(gethostcachenumber(SLIST_FIELD_CATEGORY, last - 1) == j)
517 error("Category mismatch IV");
518 category_name[category_draw_count] = j;
519 category_item[category_draw_count] = last;
520 ++category_draw_count;
521 begin = last + 1; // already scanned through these, skip 'em
524 begin = last; // already scanned through these, skip 'em
527 if(autocvar_menu_slist_categories_onlyifmultiple && (category_draw_count == 1))
529 category_name[0] = -1;
530 category_item[0] = -1;
531 category_draw_count = 0;
532 me.nItems = itemcount;
535 else { me.nItems = gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT); }
537 me.connectButton.disabled = (me.ipAddressBox.text == "");
538 //me.disconnectButton.disabled = (!(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)));
539 me.infoButton.disabled = !owned;
540 me.favoriteButton.disabled = (me.ipAddressBox.text == "");
543 if(me.lockedSelectedItem)
547 if(gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem) != me.selectedServer)
549 strcpy(me.selectedServer, gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
554 else if(me.selectedServer)
556 for(int i = 0; i < me.nItems; ++i)
558 if(gethostcachestring(SLIST_FIELD_CNAME, i) == me.selectedServer)
560 // don't follow the selected item with SUPER(XonoticServerList).setSelected(me, i);
571 // selected server disappeared, select the last server (scrolling to it)
572 if(me.selectedItem >= me.nItems)
573 SUPER(XonoticServerList).setSelected(me, me.nItems - 1);
574 strcpy(me.selectedServer, gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
580 if(me.selectedServer != me.ipAddressBox.text)
582 me.ipAddressBox.setText(me.ipAddressBox, me.selectedServer);
583 me.ipAddressBox.cursorPos = strlen(me.selectedServer);
584 me.ipAddressBoxFocused = -1;
588 if(me.ipAddressBoxFocused != me.ipAddressBox.focused)
590 if(me.ipAddressBox.focused || me.ipAddressBoxFocused < 0)
591 ServerList_Update_favoriteButton(NULL, me);
592 me.ipAddressBoxFocused = me.ipAddressBox.focused;
595 SUPER(XonoticServerList).draw(me);
597 void ServerList_PingSort_Click(entity btn, entity me)
599 me.setSortOrder(me, SLIST_FIELD_PING, +1);
601 void ServerList_NameSort_Click(entity btn, entity me)
603 me.setSortOrder(me, SLIST_FIELD_NAME, -1); // why?
605 void ServerList_MapSort_Click(entity btn, entity me)
607 me.setSortOrder(me, SLIST_FIELD_MAP, -1); // why?
609 void ServerList_PlayerSort_Click(entity btn, entity me)
611 me.setSortOrder(me, SLIST_FIELD_NUMHUMANS, -1);
613 void ServerList_TypeSort_Click(entity btn, entity me)
615 string s = me.filterString;
616 int m = strstrofs(s, ":", 0);
619 s = substring(s, 0, m);
620 while(substring(s, m+1, 1) == " ") // skip spaces
626 Gametype first = NULL; FOREACH(Gametypes, !first, first = it; break);
628 FOREACH(Gametypes, s == MapInfo_Type_ToString(it), {
629 // the type was found
630 // choose the next one
632 s = MapInfo_Type_ToString(REGISTRY_GET(Gametypes, it.m_id + 1));
633 if (s == "") s = MapInfo_Type_ToString(first);
638 // choose the first one
639 s = MapInfo_Type_ToString(first);
642 if(s != "") s = strcat(s, ":");
643 s = strcat(s, substring(me.filterString, m+1, strlen(me.filterString) - m - 1));
645 me.controlledTextbox.setText(me.controlledTextbox, s);
646 me.controlledTextbox.keyDown(me.controlledTextbox, K_END, 0, 0);
647 me.controlledTextbox.keyUp(me.controlledTextbox, K_END, 0, 0);
648 //ServerList_Filter_Change(me.controlledTextbox, me);
650 void ServerList_Filter_Change(entity box, entity me)
652 strfree(me.filterString);
654 me.filterString = strzone(box.text);
655 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
657 me.ipAddressBox.setText(me.ipAddressBox, "");
658 me.ipAddressBox.cursorPos = 0;
659 me.ipAddressBoxFocused = -1;
661 void ServerList_Categories_Click(entity box, entity me)
663 box.setChecked(box, autocvar_menu_slist_categories = !autocvar_menu_slist_categories);
664 me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
666 me.ipAddressBox.setText(me.ipAddressBox, "");
667 me.ipAddressBox.cursorPos = 0;
668 me.ipAddressBoxFocused = -1;
670 void ServerList_ShowEmpty_Click(entity box, entity me)
672 box.setChecked(box, me.filterShowEmpty = !me.filterShowEmpty);
673 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
675 me.ipAddressBox.setText(me.ipAddressBox, "");
676 me.ipAddressBox.cursorPos = 0;
677 me.ipAddressBoxFocused = -1;
679 void ServerList_ShowFull_Click(entity box, entity me)
681 box.setChecked(box, me.filterShowFull = !me.filterShowFull);
682 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
684 me.ipAddressBox.setText(me.ipAddressBox, "");
685 me.ipAddressBox.cursorPos = 0;
686 me.ipAddressBoxFocused = -1;
688 void ServerList_ShowLaggy_Click(entity box, entity me)
690 box.setChecked(box, me.filterShowLaggy = !me.filterShowLaggy);
691 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
693 me.ipAddressBox.setText(me.ipAddressBox, "");
694 me.ipAddressBox.cursorPos = 0;
695 me.ipAddressBoxFocused = -1;
697 void XonoticServerList_setSortOrder(entity me, int fld, int direction)
699 if(me.currentSortField == fld)
700 direction = -me.currentSortOrder;
701 me.currentSortOrder = direction;
702 me.currentSortField = fld;
703 me.sortButton1.forcePressed = (fld == SLIST_FIELD_PING);
704 me.sortButton2.forcePressed = (fld == SLIST_FIELD_NAME);
705 me.sortButton3.forcePressed = (fld == SLIST_FIELD_MAP);
706 me.sortButton4.forcePressed = 0;
707 me.sortButton5.forcePressed = (fld == SLIST_FIELD_NUMHUMANS);
709 strfree(me.selectedServer);
710 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
712 void XonoticServerList_positionSortButton(entity me, entity btn, float theOrigin, float theSize, string theTitle, void(entity, entity) theFunc)
714 vector originInLBSpace = eY * (-me.itemHeight);
715 vector sizeInLBSpace = eY * me.itemHeight + eX * (1 - me.controlWidth);
717 vector originInDialogSpace = boxToGlobal(originInLBSpace, me.Container_origin, me.Container_size);
718 vector sizeInDialogSpace = boxToGlobalSize(sizeInLBSpace, me.Container_size);
720 btn.Container_origin_x = originInDialogSpace.x + sizeInDialogSpace.x * theOrigin;
721 btn.Container_size_x = sizeInDialogSpace.x * theSize;
722 btn.setText(btn, theTitle);
723 btn.onClick = theFunc;
724 btn.onClickEntity = me;
727 void XonoticServerList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
729 SUPER(XonoticServerList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
731 me.realFontSize_y = me.fontSize / (absSize.y * me.itemHeight);
732 me.realFontSize_x = me.fontSize / (absSize.x * (1 - me.controlWidth));
733 me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
735 me.columnIconsOrigin = 0;
736 me.columnIconsSize = me.realFontSize.x * 4 * me.iconsSizeFactor;
737 me.columnPingSize = me.realFontSize.x * 3;
738 me.columnMapSize = me.realFontSize.x * 10;
739 me.columnTypeSize = me.realFontSize.x * 4;
740 me.columnPlayersSize = me.realFontSize.x * 5;
741 me.columnNameSize = 1 - me.columnPlayersSize - me.columnMapSize - me.columnPingSize - me.columnIconsSize - me.columnTypeSize - 5 * me.realFontSize.x;
742 me.columnPingOrigin = me.columnIconsOrigin + me.columnIconsSize + me.realFontSize.x;
743 me.columnNameOrigin = me.columnPingOrigin + me.columnPingSize + me.realFontSize.x;
744 me.columnMapOrigin = me.columnNameOrigin + me.columnNameSize + me.realFontSize.x;
745 me.columnTypeOrigin = me.columnMapOrigin + me.columnMapSize + me.realFontSize.x;
746 me.columnPlayersOrigin = me.columnTypeOrigin + me.columnTypeSize + me.realFontSize.x;
748 me.positionSortButton(me, me.sortButton1, me.columnPingOrigin, me.columnPingSize, _("Ping"), ServerList_PingSort_Click);
749 me.positionSortButton(me, me.sortButton2, me.columnNameOrigin, me.columnNameSize, _("Hostname"), ServerList_NameSort_Click);
750 me.positionSortButton(me, me.sortButton3, me.columnMapOrigin, me.columnMapSize, _("Map"), ServerList_MapSort_Click);
751 me.positionSortButton(me, me.sortButton4, me.columnTypeOrigin, me.columnTypeSize, _("Type"), ServerList_TypeSort_Click);
752 me.positionSortButton(me, me.sortButton5, me.columnPlayersOrigin, me.columnPlayersSize, _("Players"), ServerList_PlayerSort_Click);
754 int f = me.currentSortField;
757 me.currentSortField = -1;
758 me.setSortOrder(me, f, me.currentSortOrder); // force resetting the sort order
761 void ServerList_Connect_Click(entity btn, entity me)
763 if (me.ipAddressBox.text != "")
764 localcmd(sprintf("connect %s\n", me.ipAddressBox.text));
766 void ServerList_Favorite_Click(entity btn, entity this)
768 string addr = this.ipAddressBox.text;
769 string ipstr = netaddress_resolve(addr, 26000);
770 if (ipstr == "") return;
771 m_play_click_sound(MENU_SOUND_SELECT);
772 this.toggleFavorite(this, addr);
773 this.ipAddressBoxFocused = -1;
775 void ServerList_Info_Click(entity btn, entity me)
778 main.serverInfoDialog.loadServerInfo(main.serverInfoDialog, me.selectedItem);
780 float thisPos = me.getItemStart(me, me.selectedItem);
781 float thisHeight = me.getItemHeight(me, me.selectedItem);
782 vector org = boxToGlobal(eY * (thisPos - me.scrollPos), me.origin, me.size);
783 vector sz = boxToGlobalSize(eY * thisHeight + eX * (1 - me.controlWidth), me.size);
784 DialogOpenButton_Click_withCoords(me, main.serverInfoDialog, org, sz);
786 void XonoticServerList_doubleClickListBoxItem(entity me, int i, vector where)
788 ServerList_Connect_Click(NULL, me);
790 void XonoticServerList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
792 vector oldscale = draw_scale;
793 vector oldshift = draw_shift;
794 #define SET_YRANGE(start,end) \
795 draw_scale = boxToGlobalSize(eX + eY * ((end) - (start)), oldscale); \
796 draw_shift = boxToGlobal(eY * (start), oldshift, oldscale);
799 for (c = 0; c < category_draw_count; ++c) {
800 // Matches exactly the headings with increased height.
801 if (i == category_item[c])
805 if (c < category_draw_count)
807 entity catent = RetrieveCategoryEnt(category_name[c]);
811 (me.categoriesHeight - 1) / (me.categoriesHeight + 1),
812 me.categoriesHeight / (me.categoriesHeight + 1)
815 eY * me.realUpperMargin
818 eX * (me.columnNameOrigin + (me.columnNameSize - draw_TextWidth(catent.cat_string, 0, me.realFontSize)) * 0.5),
821 eX * (me.columnNameOrigin),
822 strcat(catent.cat_string, ":"),
825 SKINCOLOR_SERVERLIST_CATEGORY,
826 SKINALPHA_SERVERLIST_CATEGORY,
829 SET_YRANGE(me.categoriesHeight / (me.categoriesHeight + 1), 1);
833 // Now categories are done. Set the Y range in stone.
834 oldscale = draw_scale;
835 oldshift = draw_shift;
837 if(isSelected && !me.lockedSelectedItem)
838 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
841 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
842 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
845 string s = gethostcachestring(SLIST_FIELD_QCSTATUS, i);
846 int m = tokenizebyseparator(s, ":");
847 string typestr = "", versionstr = "";
851 versionstr = argv(1);
853 bool pure = false, pure_available = false;
854 int freeslots = -1, sflags = -1;
856 for(int j = 2; j < m; ++j)
860 string key = substring(argv(j), 0, 1);
861 string value = substring(argv(j), 1, -1);
864 pure = (stof(value) == 0);
865 pure_available = true;
868 freeslots = stof(value);
870 sflags = stoi(value);
875 #ifdef COMPAT_NO_MOD_IS_XONOTIC
880 string original_modname = modname;
881 modname = strtolower(modname);
884 SLIST_FIELD_MOD = gethostcacheindexforkey("mod");
885 s = gethostcachestring(SLIST_FIELD_MOD, i);
887 if(modname == "xonotic")
891 // list the mods here on which the pure server check actually works
892 if(modname != "xonotic")
893 if(modname != "instagib" || modname != "minstagib")
896 if(modname != "newtoys")
897 pure_available = false;
900 if(gethostcachenumber(SLIST_FIELD_FREESLOTS, i) <= 0)
901 theAlpha = SKINALPHA_SERVERLIST_FULL;
902 else if(freeslots == 0)
903 theAlpha = SKINALPHA_SERVERLIST_FULL; // g_maxplayers support
904 else if (!gethostcachenumber(SLIST_FIELD_NUMHUMANS, i))
905 theAlpha = SKINALPHA_SERVERLIST_EMPTY;
909 float ping = gethostcachenumber(SLIST_FIELD_PING, i);
910 const int PING_LOW = 75;
911 const int PING_MED = 200;
912 const int PING_HIGH = 500;
915 theColor = SKINCOLOR_SERVERLIST_LOWPING + (SKINCOLOR_SERVERLIST_MEDPING - SKINCOLOR_SERVERLIST_LOWPING) * (ping / PING_LOW);
916 else if(ping < PING_MED)
917 theColor = SKINCOLOR_SERVERLIST_MEDPING + (SKINCOLOR_SERVERLIST_HIGHPING - SKINCOLOR_SERVERLIST_MEDPING) * ((ping - PING_LOW) / (PING_MED - PING_LOW));
918 else if(ping < PING_HIGH)
920 theColor = SKINCOLOR_SERVERLIST_HIGHPING;
921 theAlpha *= 1 + (SKINALPHA_SERVERLIST_HIGHPING - 1) * ((ping - PING_MED) / (PING_HIGH - PING_MED));
926 theAlpha *= SKINALPHA_SERVERLIST_HIGHPING;
929 if(gethostcachenumber(SLIST_FIELD_ISFAVORITE, i))
931 theColor = theColor * (1 - SKINALPHA_SERVERLIST_FAVORITE) + SKINCOLOR_SERVERLIST_FAVORITE * SKINALPHA_SERVERLIST_FAVORITE;
932 theAlpha = theAlpha * (1 - SKINALPHA_SERVERLIST_FAVORITE) + SKINALPHA_SERVERLIST_FAVORITE;
935 s = gethostcachestring(SLIST_FIELD_CNAME, i);
937 bool isv4 = false, isv6 = false;
938 if(substring(s, 0, 1) == "[")
943 else if(IS_DIGIT(substring(s, 0, 1)))
949 int crypto = stof(substring(crypto_getencryptlevel(s), 0, 1));
950 if((crypto <= 0 && cvar("crypto_aeslevel") >= 3) || (crypto >= 3 && cvar("crypto_aeslevel") <= 0))
952 theColor = SKINCOLOR_SERVERLIST_IMPOSSIBLE;
953 theAlpha = SKINALPHA_SERVERLIST_IMPOSSIBLE;
958 if(cvar("crypto_aeslevel") >= 2)
963 if(cvar("crypto_aeslevel") >= 1)
973 // 2: AES recommended but not available
974 // 3: AES possible and will be used
975 // 4: AES recommended and will be used
981 vector iconSize = '0 0 0';
982 iconSize_y = me.realFontSize.y * me.iconsSizeFactor;
983 iconSize_x = me.realFontSize.x * me.iconsSizeFactor * me.serversHeight;
985 vector iconPos = '0 0 0';
986 iconPos_x = (me.columnIconsSize - 3 * iconSize.x) * 0.5;
987 iconPos_y = (1 - iconSize.y) * 0.5;
990 if(me.seenIPv4 && me.seenIPv6)
993 draw_Picture(iconPos, "icon_ipv6", iconSize, '1 1 1', 1);
995 draw_Picture(iconPos, "icon_ipv4", iconSize, '1 1 1', 1);
998 iconPos.x += iconSize.x;
1002 draw_Picture(iconPos, strcat("icon_aeslevel", ftos(crypto)), iconSize, '1 1 1', 1);
1004 iconPos.x += iconSize.x;
1007 if(modname == "xonotic")
1009 // Here, pure_available should always be set. If not, consider
1011 if(pure_available && pure)
1012 draw_Picture(iconPos, "icon_pure1", iconSize, '1 1 1', 1);
1016 string icon = strcat("icon_mod_", modname);
1017 if(draw_PictureSize(icon) == '0 0 0')
1020 // In mods, pure_available not being present indicates
1021 // non-support of the feature. Just show the mod icon as is
1023 if(pure_available && !pure)
1024 draw_Picture(iconPos, icon, iconSize, '1 1 1', SKINALPHA_SERVERLIST_ICON_NONPURE);
1026 draw_Picture(iconPos, icon, iconSize, '1 1 1', 1);
1029 iconPos.x += iconSize.x;
1032 if(sflags >= 0 && (sflags & SERVERFLAG_PLAYERSTATS))
1033 draw_Picture(iconPos, "icon_stats1", iconSize, '1 1 1', 1);
1035 if(isFocused && me.mouseOverIcons && !me.tooltip)
1038 if(me.seenIPv4 && me.seenIPv6)
1039 t = strcat(t, (isv6) ? "IPv6, " : "IPv4, ");
1040 t = strcat(t, _("encryption:"), " ", (crypto ? sprintf(_("AES level %d"), crypto) : ZCTX(_("ENC^none"))), ", ");
1041 t = strcat(t, sprintf(_("mod: %s"), ((modname == "xonotic") ? ZCTX(_("MOD^Default")) : original_modname)));
1043 t = strcat(t, sprintf(" (%s)", (pure) ? _("official settings") : _("modified settings")));
1044 t = strcat(t, ", ");
1045 t = strcat(t, ((sflags >= 0 && (sflags & SERVERFLAG_PLAYERSTATS)) ? _("stats enabled") : _("stats disabled")));
1046 setZonedTooltip(me, t, string_null);
1052 // Center it in the row.
1054 0.5 - 0.5 / me.serversHeight,
1055 0.5 + 0.5 / me.serversHeight
1060 draw_Text(me.realUpperMargin * eY + (me.columnPingOrigin + me.columnPingSize - draw_TextWidth(s, 0, me.realFontSize)) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1063 s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_NAME, i), me.columnNameSize, 0, me.realFontSize);
1064 draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, theColor, theAlpha, 0);
1067 s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_MAP, i), me.columnMapSize, 0, me.realFontSize);
1068 draw_Text(me.realUpperMargin * eY + (me.columnMapOrigin + (me.columnMapSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1071 s = draw_TextShortenToWidth(typestr, me.columnTypeSize, 0, me.realFontSize);
1072 draw_Text(me.realUpperMargin * eY + (me.columnTypeOrigin + (me.columnTypeSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1074 // server playercount
1075 s = strcat(ftos(gethostcachenumber(SLIST_FIELD_NUMHUMANS, i)), "/", ftos(gethostcachenumber(SLIST_FIELD_MAXPLAYERS, i)));
1076 draw_Text(me.realUpperMargin * eY + (me.columnPlayersOrigin + (me.columnPlayersSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1079 void XonoticServerList_focusedItemChangeNotify(entity me)
1084 float XonoticServerList_mouseMove(entity me, vector pos)
1086 if(!SUPER(XonoticServerList).mouseMove(me, pos))
1088 me.mouseOverIcons = false;
1093 me.mouseOverIcons = (pos_x <= me.columnIconsSize);
1094 if(!me.mouseOverIcons)
1099 bool XonoticServerList_keyDown(entity me, int scan, bool ascii, bool shift)
1101 if(scan == K_ENTER || scan == K_KP_ENTER)
1103 ServerList_Connect_Click(NULL, me);
1106 else if(scan == K_MOUSE2 || scan == K_SPACE)
1110 float thisPos = me.getItemStart(me, me.selectedItem);
1111 float thisHeight = me.getItemHeight(me, me.selectedItem);
1112 vector org = boxToGlobal(eY * (thisPos - me.scrollPos), me.origin, me.size);
1113 vector sz = boxToGlobalSize(eY * thisHeight + eX * (1 - me.controlWidth), me.size);
1114 m_play_click_sound(MENU_SOUND_OPEN);
1115 main.serverInfoDialog.loadServerInfo(main.serverInfoDialog, me.selectedItem);
1116 DialogOpenButton_Click_withCoords(me, main.serverInfoDialog, org, sz);
1121 else if(scan == K_INS || scan == K_MOUSE3 || scan == K_KP_INS)
1125 me.toggleFavorite(me, me.selectedServer);
1126 me.ipAddressBoxFocused = -1;
1131 else if(SUPER(XonoticServerList).keyDown(me, scan, ascii, shift))
1133 else if(!me.controlledTextbox)
1136 return me.controlledTextbox.keyDown(me.controlledTextbox, scan, ascii, shift);
1139 float XonoticServerList_getTotalHeight(entity me)
1141 float num_normal_rows = me.nItems;
1142 int num_headers = category_draw_count;
1143 return me.itemHeight * (me.serversHeight * num_normal_rows + me.categoriesHeight * num_headers);
1145 int XonoticServerList_getItemAtPos(entity me, float pos)
1147 pos = pos / me.itemHeight;
1148 for (int i = category_draw_count - 1; i >= 0; --i) {
1149 int itemidx = category_item[i];
1150 float itempos = i * me.categoriesHeight + category_item[i] * me.serversHeight;
1151 if (pos >= itempos + me.categoriesHeight + me.serversHeight)
1152 return itemidx + 1 + floor((pos - (itempos + me.categoriesHeight + me.serversHeight)) / me.serversHeight);
1156 // No category matches? Note that category 0 is... 0. Therefore no headings exist at all.
1157 return floor(pos / me.serversHeight);
1159 float XonoticServerList_getItemStart(entity me, int item)
1161 for (int i = category_draw_count - 1; i >= 0; --i) {
1162 int itemidx = category_item[i];
1163 float itempos = i * me.categoriesHeight + category_item[i] * me.serversHeight;
1164 if (item >= itemidx + 1)
1165 return (itempos + me.categoriesHeight + (1 + item - (itemidx + 1)) * me.serversHeight) * me.itemHeight;
1166 if (item >= itemidx)
1167 return itempos * me.itemHeight;
1169 // No category matches? Note that category 0 is... 0. Therefore no headings exist at all.
1170 return item * me.serversHeight * me.itemHeight;
1172 float XonoticServerList_getItemHeight(entity me, int item)
1174 for (int i = 0; i < category_draw_count; ++i) {
1175 // Matches exactly the headings with increased height.
1176 if (item == category_item[i])
1177 return me.itemHeight * (me.categoriesHeight + 1);
1179 return me.itemHeight * me.serversHeight;