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 if(_Nex_ExtResponseSystem_BannedServersNeedsRefresh)
405 _Nex_ExtResponseSystem_BannedServersNeedsRefresh = 0;
408 if(_Nex_ExtResponseSystem_PromotedServersNeedsRefresh)
412 _Nex_ExtResponseSystem_PromotedServersNeedsRefresh = 0;
415 if(_Nex_ExtResponseSystem_RecommendedServersNeedsRefresh)
419 _Nex_ExtResponseSystem_RecommendedServersNeedsRefresh = 0;
422 if(me.currentSortField == -1)
424 me.setSortOrder(me, SLIST_FIELD_PING, +1);
425 me.refreshServerList(me, REFRESHSERVERLIST_RESET);
427 else if(me.needsRefresh == 1)
429 me.needsRefresh = 2; // delay by one frame to make sure "slist" has been executed
431 else if(me.needsRefresh == 2)
434 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
436 else if(me.needsRefresh == 3)
439 me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
442 bool owned = ((me.selectedServer == me.ipAddressBox.text) && (me.ipAddressBox.text != ""));
444 for(int i = 0; i < category_draw_count; ++i) { category_name[i] = -1; category_item[i] = -1; }
445 category_draw_count = 0;
447 if(autocvar_menu_slist_categories >= 0) // if less than 0, don't even draw a category heading for favorites
449 float itemcount = gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT);
450 me.nItems = itemcount;
452 //float visible = floor(me.scrollPos / me.itemHeight);
453 // ^ unfortunately no such optimization can be made-- we must process through the
454 // entire list, otherwise there is no way to know which item is first in its category.
456 // binary search method suggested by div
458 for(int j = 1; j <= category_ent_count; ++j) {
460 float last = (itemcount - 1);
465 float catf = gethostcachenumber(SLIST_FIELD_CATEGORY, first);
466 float catl = gethostcachenumber(SLIST_FIELD_CATEGORY, last);
468 // The first one is already > j.
469 // Therefore, category j does not exist.
470 // Higher numbered categories do exist though.
471 } else if (catl < j) {
472 // The last one is < j.
473 // Thus this category - and any following -
476 } else if (catf == j) {
477 // Starts at first. This breaks the loop
478 // invariant in the binary search and thus has
479 // to be handled separately.
480 if(gethostcachenumber(SLIST_FIELD_CATEGORY, first) != j)
481 error("Category mismatch I");
483 if(gethostcachenumber(SLIST_FIELD_CATEGORY, first - 1) == j)
484 error("Category mismatch II");
485 category_name[category_draw_count] = j;
486 category_item[category_draw_count] = first;
487 ++category_draw_count;
490 // At this point, catf <= j < catl, thus
491 // catf < catl, thus first < last.
494 // catf == gethostcachenumber(SLIST_FIELD_CATEGORY(first)
495 // catl == gethostcachenumber(SLIST_FIELD_CATEGORY(last)
498 while (last - first > 1) {
499 float middle = floor((first + last) / 2);
500 // By loop condition, middle != first && middle != last.
501 float cat = gethostcachenumber(SLIST_FIELD_CATEGORY, middle);
511 if(gethostcachenumber(SLIST_FIELD_CATEGORY, last) != j)
512 error("Category mismatch III");
514 if(gethostcachenumber(SLIST_FIELD_CATEGORY, last - 1) == j)
515 error("Category mismatch IV");
516 category_name[category_draw_count] = j;
517 category_item[category_draw_count] = last;
518 ++category_draw_count;
519 begin = last + 1; // already scanned through these, skip 'em
522 begin = last; // already scanned through these, skip 'em
525 if(autocvar_menu_slist_categories_onlyifmultiple && (category_draw_count == 1))
527 category_name[0] = -1;
528 category_item[0] = -1;
529 category_draw_count = 0;
530 me.nItems = itemcount;
533 else { me.nItems = gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT); }
535 me.connectButton.disabled = (me.ipAddressBox.text == "");
536 //me.disconnectButton.disabled = (!(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)));
537 me.infoButton.disabled = !owned;
538 me.favoriteButton.disabled = (me.ipAddressBox.text == "");
541 if(me.lockedSelectedItem)
545 if(gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem) != me.selectedServer)
547 strcpy(me.selectedServer, gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
552 else if(me.selectedServer)
554 for(int i = 0; i < me.nItems; ++i)
556 if(gethostcachestring(SLIST_FIELD_CNAME, i) == me.selectedServer)
558 // don't follow the selected item with SUPER(XonoticServerList).setSelected(me, i);
569 // selected server disappeared, select the last server (scrolling to it)
570 if(me.selectedItem >= me.nItems)
571 SUPER(XonoticServerList).setSelected(me, me.nItems - 1);
572 strcpy(me.selectedServer, gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
578 if(me.selectedServer != me.ipAddressBox.text)
580 me.ipAddressBox.setText(me.ipAddressBox, me.selectedServer);
581 me.ipAddressBox.cursorPos = strlen(me.selectedServer);
582 me.ipAddressBoxFocused = -1;
586 if(me.ipAddressBoxFocused != me.ipAddressBox.focused)
588 if(me.ipAddressBox.focused || me.ipAddressBoxFocused < 0)
589 ServerList_Update_favoriteButton(NULL, me);
590 me.ipAddressBoxFocused = me.ipAddressBox.focused;
593 SUPER(XonoticServerList).draw(me);
595 void ServerList_PingSort_Click(entity btn, entity me)
597 me.setSortOrder(me, SLIST_FIELD_PING, +1);
599 void ServerList_NameSort_Click(entity btn, entity me)
601 me.setSortOrder(me, SLIST_FIELD_NAME, -1); // why?
603 void ServerList_MapSort_Click(entity btn, entity me)
605 me.setSortOrder(me, SLIST_FIELD_MAP, -1); // why?
607 void ServerList_PlayerSort_Click(entity btn, entity me)
609 me.setSortOrder(me, SLIST_FIELD_NUMHUMANS, -1);
611 void ServerList_TypeSort_Click(entity btn, entity me)
613 string s = me.filterString;
614 int m = strstrofs(s, ":", 0);
617 s = substring(s, 0, m);
618 while(substring(s, m+1, 1) == " ") // skip spaces
624 Gametype first = NULL; FOREACH(Gametypes, !first, first = it; break);
626 FOREACH(Gametypes, s == MapInfo_Type_ToString(it), {
627 // the type was found
628 // choose the next one
630 s = MapInfo_Type_ToString(REGISTRY_GET(Gametypes, it.m_id + 1));
631 if (s == "") s = MapInfo_Type_ToString(first);
636 // choose the first one
637 s = MapInfo_Type_ToString(first);
640 if(s != "") s = strcat(s, ":");
641 s = strcat(s, substring(me.filterString, m+1, strlen(me.filterString) - m - 1));
643 me.controlledTextbox.setText(me.controlledTextbox, s);
644 me.controlledTextbox.keyDown(me.controlledTextbox, K_END, 0, 0);
645 me.controlledTextbox.keyUp(me.controlledTextbox, K_END, 0, 0);
646 //ServerList_Filter_Change(me.controlledTextbox, me);
648 void ServerList_Filter_Change(entity box, entity me)
650 strfree(me.filterString);
652 me.filterString = strzone(box.text);
653 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
655 me.ipAddressBox.setText(me.ipAddressBox, "");
656 me.ipAddressBox.cursorPos = 0;
657 me.ipAddressBoxFocused = -1;
659 void ServerList_Categories_Click(entity box, entity me)
661 box.setChecked(box, autocvar_menu_slist_categories = !autocvar_menu_slist_categories);
662 me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
664 me.ipAddressBox.setText(me.ipAddressBox, "");
665 me.ipAddressBox.cursorPos = 0;
666 me.ipAddressBoxFocused = -1;
668 void ServerList_ShowEmpty_Click(entity box, entity me)
670 box.setChecked(box, me.filterShowEmpty = !me.filterShowEmpty);
671 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
673 me.ipAddressBox.setText(me.ipAddressBox, "");
674 me.ipAddressBox.cursorPos = 0;
675 me.ipAddressBoxFocused = -1;
677 void ServerList_ShowFull_Click(entity box, entity me)
679 box.setChecked(box, me.filterShowFull = !me.filterShowFull);
680 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
682 me.ipAddressBox.setText(me.ipAddressBox, "");
683 me.ipAddressBox.cursorPos = 0;
684 me.ipAddressBoxFocused = -1;
686 void ServerList_ShowLaggy_Click(entity box, entity me)
688 box.setChecked(box, me.filterShowLaggy = !me.filterShowLaggy);
689 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
691 me.ipAddressBox.setText(me.ipAddressBox, "");
692 me.ipAddressBox.cursorPos = 0;
693 me.ipAddressBoxFocused = -1;
695 void XonoticServerList_setSortOrder(entity me, int fld, int direction)
697 if(me.currentSortField == fld)
698 direction = -me.currentSortOrder;
699 me.currentSortOrder = direction;
700 me.currentSortField = fld;
701 me.sortButton1.forcePressed = (fld == SLIST_FIELD_PING);
702 me.sortButton2.forcePressed = (fld == SLIST_FIELD_NAME);
703 me.sortButton3.forcePressed = (fld == SLIST_FIELD_MAP);
704 me.sortButton4.forcePressed = 0;
705 me.sortButton5.forcePressed = (fld == SLIST_FIELD_NUMHUMANS);
707 strfree(me.selectedServer);
708 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
710 void XonoticServerList_positionSortButton(entity me, entity btn, float theOrigin, float theSize, string theTitle, void(entity, entity) theFunc)
712 vector originInLBSpace = eY * (-me.itemHeight);
713 vector sizeInLBSpace = eY * me.itemHeight + eX * (1 - me.controlWidth);
715 vector originInDialogSpace = boxToGlobal(originInLBSpace, me.Container_origin, me.Container_size);
716 vector sizeInDialogSpace = boxToGlobalSize(sizeInLBSpace, me.Container_size);
718 btn.Container_origin_x = originInDialogSpace.x + sizeInDialogSpace.x * theOrigin;
719 btn.Container_size_x = sizeInDialogSpace.x * theSize;
720 btn.setText(btn, theTitle);
721 btn.onClick = theFunc;
722 btn.onClickEntity = me;
725 void XonoticServerList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
727 SUPER(XonoticServerList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
729 me.realFontSize_y = me.fontSize / (absSize.y * me.itemHeight);
730 me.realFontSize_x = me.fontSize / (absSize.x * (1 - me.controlWidth));
731 me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
733 me.columnIconsOrigin = 0;
734 me.columnIconsSize = me.realFontSize.x * 4 * me.iconsSizeFactor;
735 me.columnPingSize = me.realFontSize.x * 3;
736 me.columnMapSize = me.realFontSize.x * 10;
737 me.columnTypeSize = me.realFontSize.x * 4;
738 me.columnPlayersSize = me.realFontSize.x * 5;
739 me.columnNameSize = 1 - me.columnPlayersSize - me.columnMapSize - me.columnPingSize - me.columnIconsSize - me.columnTypeSize - 5 * me.realFontSize.x;
740 me.columnPingOrigin = me.columnIconsOrigin + me.columnIconsSize + me.realFontSize.x;
741 me.columnNameOrigin = me.columnPingOrigin + me.columnPingSize + me.realFontSize.x;
742 me.columnMapOrigin = me.columnNameOrigin + me.columnNameSize + me.realFontSize.x;
743 me.columnTypeOrigin = me.columnMapOrigin + me.columnMapSize + me.realFontSize.x;
744 me.columnPlayersOrigin = me.columnTypeOrigin + me.columnTypeSize + me.realFontSize.x;
746 me.positionSortButton(me, me.sortButton1, me.columnPingOrigin, me.columnPingSize, _("Ping"), ServerList_PingSort_Click);
747 me.positionSortButton(me, me.sortButton2, me.columnNameOrigin, me.columnNameSize, _("Hostname"), ServerList_NameSort_Click);
748 me.positionSortButton(me, me.sortButton3, me.columnMapOrigin, me.columnMapSize, _("Map"), ServerList_MapSort_Click);
749 me.positionSortButton(me, me.sortButton4, me.columnTypeOrigin, me.columnTypeSize, _("Type"), ServerList_TypeSort_Click);
750 me.positionSortButton(me, me.sortButton5, me.columnPlayersOrigin, me.columnPlayersSize, _("Players"), ServerList_PlayerSort_Click);
752 int f = me.currentSortField;
755 me.currentSortField = -1;
756 me.setSortOrder(me, f, me.currentSortOrder); // force resetting the sort order
759 void ServerList_Connect_Click(entity btn, entity me)
761 if (me.ipAddressBox.text != "")
762 localcmd(sprintf("connect %s\n", me.ipAddressBox.text));
764 void ServerList_Favorite_Click(entity btn, entity this)
766 string addr = this.ipAddressBox.text;
767 string ipstr = netaddress_resolve(addr, 26000);
768 if (ipstr == "") return;
769 m_play_click_sound(MENU_SOUND_SELECT);
770 this.toggleFavorite(this, addr);
771 this.ipAddressBoxFocused = -1;
773 void ServerList_Info_Click(entity btn, entity me)
776 main.serverInfoDialog.loadServerInfo(main.serverInfoDialog, me.selectedItem);
778 vector org = boxToGlobal(eY * (me.selectedItem * me.itemHeight - me.scrollPos), me.origin, me.size);
779 vector sz = boxToGlobalSize(eY * me.itemHeight + eX * (1 - me.controlWidth), me.size);
780 DialogOpenButton_Click_withCoords(me, main.serverInfoDialog, org, sz);
782 void XonoticServerList_doubleClickListBoxItem(entity me, int i, vector where)
784 ServerList_Connect_Click(NULL, me);
786 void XonoticServerList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
788 vector oldscale = draw_scale;
789 vector oldshift = draw_shift;
790 #define SET_YRANGE(start,end) \
791 draw_scale = boxToGlobalSize(eX + eY * (end - start), oldscale); \
792 draw_shift = boxToGlobal(eY * start, oldshift, oldscale);
795 for (c = 0; c < category_draw_count; ++c) {
796 // Matches exactly the headings with increased height.
797 if (i == category_item[c])
801 if (c < category_draw_count)
803 entity catent = RetrieveCategoryEnt(category_name[c]);
807 (me.categoriesHeight - 1) / (me.categoriesHeight + 1),
808 me.categoriesHeight / (me.categoriesHeight + 1)
811 eY * me.realUpperMargin
814 eX * (me.columnNameOrigin + (me.columnNameSize - draw_TextWidth(catent.cat_string, 0, me.realFontSize)) * 0.5),
817 eX * (me.columnNameOrigin),
818 strcat(catent.cat_string, ":"),
821 SKINCOLOR_SERVERLIST_CATEGORY,
822 SKINALPHA_SERVERLIST_CATEGORY,
825 SET_YRANGE(me.categoriesHeight / (me.categoriesHeight + 1), 1);
829 if(isSelected && !me.lockedSelectedItem)
830 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
833 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
834 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
837 string s = gethostcachestring(SLIST_FIELD_QCSTATUS, i);
838 int m = tokenizebyseparator(s, ":");
839 string typestr = "", versionstr = "";
843 versionstr = argv(1);
845 bool pure = false, pure_available = false;
846 int freeslots = -1, sflags = -1;
848 for(int j = 2; j < m; ++j)
852 string key = substring(argv(j), 0, 1);
853 string value = substring(argv(j), 1, -1);
856 pure = (stof(value) == 0);
857 pure_available = true;
860 freeslots = stof(value);
862 sflags = stoi(value);
867 #ifdef COMPAT_NO_MOD_IS_XONOTIC
872 string original_modname = modname;
873 modname = strtolower(modname);
876 SLIST_FIELD_MOD = gethostcacheindexforkey("mod");
877 s = gethostcachestring(SLIST_FIELD_MOD, i);
879 if(modname == "xonotic")
883 // list the mods here on which the pure server check actually works
884 if(modname != "xonotic")
885 if(modname != "instagib" || modname != "minstagib")
888 if(modname != "newtoys")
889 pure_available = false;
892 if(gethostcachenumber(SLIST_FIELD_FREESLOTS, i) <= 0)
893 theAlpha = SKINALPHA_SERVERLIST_FULL;
894 else if(freeslots == 0)
895 theAlpha = SKINALPHA_SERVERLIST_FULL; // g_maxplayers support
896 else if (!gethostcachenumber(SLIST_FIELD_NUMHUMANS, i))
897 theAlpha = SKINALPHA_SERVERLIST_EMPTY;
901 float ping = gethostcachenumber(SLIST_FIELD_PING, i);
902 const int PING_LOW = 75;
903 const int PING_MED = 200;
904 const int PING_HIGH = 500;
907 theColor = SKINCOLOR_SERVERLIST_LOWPING + (SKINCOLOR_SERVERLIST_MEDPING - SKINCOLOR_SERVERLIST_LOWPING) * (ping / PING_LOW);
908 else if(ping < PING_MED)
909 theColor = SKINCOLOR_SERVERLIST_MEDPING + (SKINCOLOR_SERVERLIST_HIGHPING - SKINCOLOR_SERVERLIST_MEDPING) * ((ping - PING_LOW) / (PING_MED - PING_LOW));
910 else if(ping < PING_HIGH)
912 theColor = SKINCOLOR_SERVERLIST_HIGHPING;
913 theAlpha *= 1 + (SKINALPHA_SERVERLIST_HIGHPING - 1) * ((ping - PING_MED) / (PING_HIGH - PING_MED));
918 theAlpha *= SKINALPHA_SERVERLIST_HIGHPING;
921 if(gethostcachenumber(SLIST_FIELD_ISFAVORITE, i))
923 theColor = theColor * (1 - SKINALPHA_SERVERLIST_FAVORITE) + SKINCOLOR_SERVERLIST_FAVORITE * SKINALPHA_SERVERLIST_FAVORITE;
924 theAlpha = theAlpha * (1 - SKINALPHA_SERVERLIST_FAVORITE) + SKINALPHA_SERVERLIST_FAVORITE;
927 s = gethostcachestring(SLIST_FIELD_CNAME, i);
929 bool isv4 = false, isv6 = false;
930 if(substring(s, 0, 1) == "[")
935 else if(IS_DIGIT(substring(s, 0, 1)))
941 int crypto = stof(substring(crypto_getencryptlevel(s), 0, 1));
942 if((crypto <= 0 && cvar("crypto_aeslevel") >= 3) || (crypto >= 3 && cvar("crypto_aeslevel") <= 0))
944 theColor = SKINCOLOR_SERVERLIST_IMPOSSIBLE;
945 theAlpha = SKINALPHA_SERVERLIST_IMPOSSIBLE;
950 if(cvar("crypto_aeslevel") >= 2)
955 if(cvar("crypto_aeslevel") >= 1)
965 // 2: AES recommended but not available
966 // 3: AES possible and will be used
967 // 4: AES recommended and will be used
973 vector iconSize = '0 0 0';
974 iconSize_y = me.realFontSize.y * me.iconsSizeFactor;
975 iconSize_x = me.realFontSize.x * me.iconsSizeFactor;
977 vector iconPos = '0 0 0';
978 iconPos_x = (me.columnIconsSize - 3 * iconSize.x) * 0.5;
979 iconPos_y = (1 - iconSize.y) * 0.5;
982 if(me.seenIPv4 && me.seenIPv6)
985 draw_Picture(iconPos, "icon_ipv6", iconSize, '1 1 1', 1);
987 draw_Picture(iconPos, "icon_ipv4", iconSize, '1 1 1', 1);
990 iconPos.x += iconSize.x;
994 draw_Picture(iconPos, strcat("icon_aeslevel", ftos(crypto)), iconSize, '1 1 1', 1);
996 iconPos.x += iconSize.x;
999 if(modname == "xonotic")
1001 // Here, pure_available should always be set. If not, consider
1003 if(pure_available && pure)
1004 draw_Picture(iconPos, "icon_pure1", iconSize, '1 1 1', 1);
1008 string icon = strcat("icon_mod_", modname);
1009 if(draw_PictureSize(icon) == '0 0 0')
1012 // In mods, pure_available not being present indicates
1013 // non-support of the feature. Just show the mod icon as is
1015 if(pure_available && !pure)
1016 draw_Picture(iconPos, icon, iconSize, '1 1 1', SKINALPHA_SERVERLIST_ICON_NONPURE);
1018 draw_Picture(iconPos, icon, iconSize, '1 1 1', 1);
1021 iconPos.x += iconSize.x;
1024 if(sflags >= 0 && (sflags & SERVERFLAG_PLAYERSTATS))
1025 draw_Picture(iconPos, "icon_stats1", iconSize, '1 1 1', 1);
1027 if(isFocused && me.mouseOverIcons && !me.tooltip)
1030 if(me.seenIPv4 && me.seenIPv6)
1031 t = strcat(t, (isv6) ? "IPv6, " : "IPv4, ");
1032 t = strcat(t, _("encryption:"), " ", (crypto ? sprintf(_("AES level %d"), crypto) : ZCTX(_("ENC^none"))), ", ");
1033 t = strcat(t, sprintf(_("mod: %s"), ((modname == "xonotic") ? ZCTX(_("MOD^Default")) : original_modname)));
1035 t = strcat(t, sprintf(" (%s)", (pure) ? _("official settings") : _("modified settings")));
1036 t = strcat(t, ", ");
1037 t = strcat(t, ((sflags >= 0 && (sflags & SERVERFLAG_PLAYERSTATS)) ? _("stats enabled") : _("stats disabled")));
1038 setZonedTooltip(me, t, string_null);
1046 draw_Text(me.realUpperMargin * eY + (me.columnPingOrigin + me.columnPingSize - draw_TextWidth(s, 0, me.realFontSize)) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1049 s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_NAME, i), me.columnNameSize, 0, me.realFontSize);
1050 draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, theColor, theAlpha, 0);
1053 s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_MAP, i), me.columnMapSize, 0, me.realFontSize);
1054 draw_Text(me.realUpperMargin * eY + (me.columnMapOrigin + (me.columnMapSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1057 s = draw_TextShortenToWidth(typestr, me.columnTypeSize, 0, me.realFontSize);
1058 draw_Text(me.realUpperMargin * eY + (me.columnTypeOrigin + (me.columnTypeSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1060 // server playercount
1061 s = strcat(ftos(gethostcachenumber(SLIST_FIELD_NUMHUMANS, i)), "/", ftos(gethostcachenumber(SLIST_FIELD_MAXPLAYERS, i)));
1062 draw_Text(me.realUpperMargin * eY + (me.columnPlayersOrigin + (me.columnPlayersSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1065 void XonoticServerList_focusedItemChangeNotify(entity me)
1070 float XonoticServerList_mouseMove(entity me, vector pos)
1072 if(!SUPER(XonoticServerList).mouseMove(me, pos))
1074 me.mouseOverIcons = false;
1079 me.mouseOverIcons = (pos_x <= me.columnIconsSize);
1080 if(!me.mouseOverIcons)
1085 bool XonoticServerList_keyDown(entity me, int scan, bool ascii, bool shift)
1087 if(scan == K_ENTER || scan == K_KP_ENTER)
1089 ServerList_Connect_Click(NULL, me);
1092 else if(scan == K_MOUSE2 || scan == K_SPACE)
1096 vector org = boxToGlobal(eY * (me.selectedItem * me.itemHeight - me.scrollPos), me.origin, me.size);
1097 vector sz = boxToGlobalSize(eY * me.itemHeight + eX * (1 - me.controlWidth), me.size);
1098 m_play_click_sound(MENU_SOUND_OPEN);
1099 main.serverInfoDialog.loadServerInfo(main.serverInfoDialog, me.selectedItem);
1100 DialogOpenButton_Click_withCoords(me, main.serverInfoDialog, org, sz);
1105 else if(scan == K_INS || scan == K_MOUSE3 || scan == K_KP_INS)
1109 me.toggleFavorite(me, me.selectedServer);
1110 me.ipAddressBoxFocused = -1;
1115 else if(SUPER(XonoticServerList).keyDown(me, scan, ascii, shift))
1117 else if(!me.controlledTextbox)
1120 return me.controlledTextbox.keyDown(me.controlledTextbox, scan, ascii, shift);
1123 float XonoticServerList_getTotalHeight(entity me)
1125 float num_normal_rows = me.nItems;
1126 int num_headers = category_draw_count;
1127 return me.itemHeight * (num_normal_rows + me.categoriesHeight * num_headers);
1129 int XonoticServerList_getItemAtPos(entity me, float pos)
1131 pos = pos / me.itemHeight;
1132 for (int i = category_draw_count - 1; i >= 0; --i) {
1133 int itemidx = category_item[i];
1134 float itempos = i * me.categoriesHeight + category_item[i];
1135 if (pos >= itempos + me.categoriesHeight + 1)
1136 return itemidx + 1 + floor(pos - (itempos + me.categoriesHeight + 1));
1140 // No category matches? Note that category 0 is... 0. Therefore no headings exist at all.
1143 float XonoticServerList_getItemStart(entity me, int item)
1145 for (int i = category_draw_count - 1; i >= 0; --i) {
1146 int itemidx = category_item[i];
1147 float itempos = i * me.categoriesHeight + category_item[i];
1148 if (item >= itemidx + 1)
1149 return (itempos + me.categoriesHeight + 1 + item - (itemidx + 1)) * me.itemHeight;
1150 if (item >= itemidx)
1151 return itempos * me.itemHeight;
1153 // No category matches? Note that category 0 is... 0. Therefore no headings exist at all.
1154 return item * me.itemHeight;
1156 float XonoticServerList_getItemHeight(entity me, int item)
1158 for (int i = 0; i < category_draw_count; ++i) {
1159 // Matches exactly the headings with increased height.
1160 if (item == category_item[i])
1161 return me.itemHeight * (me.categoriesHeight + 1);
1163 return me.itemHeight;