1 #include "serverlist.qh"
5 #include "mainwindow.qh"
6 #include "dialog_multiplayer_join_serverinfo.qh"
7 #include <common/mapinfo.qh>
9 #define IsFavorite(srv) IsServerInList(cvar_string("net_slist_favorites"), srv)
10 #define IsPromoted(srv) IsServerInList(_Nex_ExtResponseSystem_PromotedServers, srv)
11 #define IsRecommended(srv) IsServerInList(_Nex_ExtResponseSystem_RecommendedServers, srv)
13 float m_gethostcachecategory(float entry) { return CategoryOverride(CategoryForEntry(entry)); }
15 void SL_ProcessCategoryOverrides(.string override_field_string, .float override_field)
17 for (int i = 0; i < category_ent_count; ++i)
19 string s = categories[i].override_field_string;
20 if (s != "" && s != categories[i].cat_name)
23 for (int x = 0; x < category_ent_count; ++x)
25 if(categories[x].cat_name == s)
33 strfree(categories[i].override_field_string);
34 categories[i].override_field = catnum;
37 LOG_INFOF("RegisterSLCategories(): Improper override '%s' for category '%s'!", s, categories[i].cat_name);
39 strfree(categories[i].override_field_string);
40 categories[i].override_field = 0;
44 void RegisterSLCategories()
47 #define SLIST_CATEGORY(name,enoverride,dioverride,str) \
48 SET_FIELD_COUNT(name, CATEGORY_FIRST, category_ent_count) \
49 CHECK_MAX_COUNT(name, MAX_CATEGORIES, category_ent_count, "SLIST_CATEGORY") \
50 cat = categories[name - 1] = new(slist_category); \
51 cat.cat_name = #name; \
52 cat.cat_enoverride_string = strzone(SLIST_CATEGORY_AUTOCVAR(name)); \
53 cat.cat_dioverride_string = strzone(dioverride); \
54 cat.cat_string = strzone(str);
58 SL_ProcessCategoryOverrides(cat_enoverride_string, cat_enoverride);
59 SL_ProcessCategoryOverrides(cat_dioverride_string, cat_dioverride);
62 // Supporting Functions
63 entity RetrieveCategoryEnt(int catnum)
65 if((catnum > 0) && (catnum <= category_ent_count))
67 return categories[catnum - 1];
71 error(sprintf("RetrieveCategoryEnt(%d): Improper category number!\n", catnum));
76 bool IsServerInList(string list, string srv)
80 srv = netaddress_resolve(srv, 26000);
83 string p = crypto_getidfp(srv);
84 int n = tokenize_console(list);
85 for(int i = 0; i < n; ++i)
87 if(substring(argv(i), 0, 1) != "[" && strlen(argv(i)) == 44 && strstrofs(argv(i), ".", 0) < 0)
94 if(srv == netaddress_resolve(argv(i), 26000))
101 int CategoryOverride(int cat)
103 entity catent = RetrieveCategoryEnt(cat);
106 int override = (autocvar_menu_slist_categories ? catent.cat_enoverride : catent.cat_dioverride);
107 if(override) { return override; }
112 error(sprintf("CategoryOverride(%d): Improper category number!\n", cat));
117 int CategoryForEntry(int entry)
119 string s, k, v, modtype = "";
120 int j, m, impure = 0, freeslots = 0, sflags = 0;
121 s = gethostcachestring(SLIST_FIELD_QCSTATUS, entry);
122 m = tokenizebyseparator(s, ":");
124 for(j = 2; j < m; ++j)
126 if(argv(j) == "") { break; }
127 k = substring(argv(j), 0, 1);
128 v = substring(argv(j), 1, -1);
131 case "P": { impure = stof(v); break; }
132 case "S": { freeslots = stof(v); break; }
133 case "F": { sflags = stof(v); break; }
134 case "M": { modtype = strtolower(v); break; }
138 if(modtype != "xonotic") { impure += autocvar_menu_slist_modimpurity; }
140 // check if this server is favorited
141 if(gethostcachenumber(SLIST_FIELD_ISFAVORITE, entry)) { return CAT_FAVORITED; }
143 // now check if it's recommended
144 if(autocvar_menu_slist_recommendations)
146 string cname = gethostcachestring(SLIST_FIELD_CNAME, entry);
148 if(IsPromoted(cname)) { return CAT_RECOMMENDED; }
151 float recommended = 0;
152 if(autocvar_menu_slist_recommendations & 1)
154 if(IsRecommended(cname)) { ++recommended; }
155 else { --recommended; }
157 if(autocvar_menu_slist_recommendations & 2)
160 ///// check for minimum free slots
161 (freeslots >= autocvar_menu_slist_recommendations_minfreeslots)
163 && // check for purity requirement
165 (autocvar_menu_slist_recommendations_purethreshold < 0)
167 (impure <= autocvar_menu_slist_recommendations_purethreshold)
170 && // check for minimum amount of humans
172 gethostcachenumber(SLIST_FIELD_NUMHUMANS, entry)
174 autocvar_menu_slist_recommendations_minhumans
177 && // check for maximum latency
179 gethostcachenumber(SLIST_FIELD_PING, entry)
181 autocvar_menu_slist_recommendations_maxping
188 if(recommended > 0) { return CAT_RECOMMENDED; }
192 // if not favorited or recommended, check modname
193 if(modtype != "xonotic")
197 // old servers which don't report their mod name are considered modified now
198 case "": { return CAT_MODIFIED; }
200 case "xpm": { return CAT_XPM; }
202 case "instagib": { return CAT_INSTAGIB; }
203 case "overkill": { return CAT_OVERKILL; }
205 // "cts" is allowed as compat, xdf is replacement
207 case "xdf": { return CAT_DEFRAG; }
209 default: { LOG_TRACEF("Found strange mod type: %s", modtype); return CAT_MODIFIED; }
213 // must be normal or impure server
214 return ((impure > autocvar_menu_slist_purethreshold) ? CAT_MODIFIED : CAT_NORMAL);
217 METHOD(XonoticServerList, toggleFavorite, void(XonoticServerList this, string srv))
220 string srv_resolved = netaddress_resolve(srv, 26000);
221 string p = crypto_getidfp(srv_resolved);
222 string s = cvar_string("net_slist_favorites");
224 for (int i = 0, n = tokenize_console(s); i < n; ++i)
227 if (substring(argv(i), 0, 1) != "[" && strlen(argv(i)) == 44 && strstrofs(argv(i), ".", 0) < 0)
229 // it's a pubkey hash
230 match = (p && p == argv(i));
235 match = (srv_resolved == netaddress_resolve(argv(i), 26000));
237 if (!match) continue;
240 string before = (i > 0) ? substring(s, 0, argv_end_index(i - 1)) : "";
241 string after = (i < n - 1) ? substring(s, argv_start_index(i + 1), -1) : "";
242 s = strcat(before, (before != "" && after != "" ? " " : ""), after);
245 // TODO: why not break here?
246 n = tokenize_console(s);
247 --i; // offset the increment that is about to happen
251 ret = strcat(s, (s != "" ? " " : ""), p ? p : srv);
253 cvar_set("net_slist_favorites", ret);
254 this.refreshServerList(this, REFRESHSERVERLIST_RESORT);
257 void ServerList_Update_favoriteButton(entity btn, entity me)
259 entity e = me.favoriteButton;
260 if(IsFavorite(me.ipAddressBox.text))
262 e.setText(e, ZCTX(_("SERVER^Remove favorite")));
263 setZonedTooltip(e, _("Remove the currently highlighted server from bookmarks"), string_null);
267 e.setText(e, ZCTX(_("SERVER^Favorite")));
268 setZonedTooltip(e, _("Bookmark the currently highlighted server so that it's faster to find in the future"), string_null);
272 entity makeXonoticServerList()
275 me = NEW(XonoticServerList);
276 me.configureXonoticServerList(me);
279 void XonoticServerList_configureXonoticServerList(entity me)
281 me.configureXonoticListBox(me);
284 #define SLIST_FIELD(suffix,name) SLIST_FIELD_##suffix = gethostcacheindexforkey(name);
291 void XonoticServerList_setSelected(entity me, int i)
293 me.lockedSelectedItem = false;
294 //int save = me.selectedItem;
295 SUPER(XonoticServerList).setSelected(me, i);
297 if(me.selectedItem == save)
302 if(gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT) != me.nItems)
303 return; // sorry, it would be wrong
305 strcpy(me.selectedServer, gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
307 me.ipAddressBox.setText(me.ipAddressBox, me.selectedServer);
308 me.ipAddressBox.cursorPos = strlen(me.selectedServer);
309 me.ipAddressBoxFocused = -1;
311 void XonoticServerList_refreshServerList(entity me, int mode)
313 //print("refresh of type ", ftos(mode), "\n");
315 if(mode >= REFRESHSERVERLIST_REFILTER)
317 string s = me.filterString;
320 int m = strstrofs(s, ":", 0);
323 typestr = substring(s, 0, m);
324 s = substring(s, m + 1, strlen(s) - m - 1);
325 while(substring(s, 0, 1) == " ")
326 s = substring(s, 1, strlen(s) - 1);
331 string modstr = cvar_string("menu_slist_modfilter");
333 m = SLIST_MASK_AND - 1;
334 resethostcachemasks();
336 // ping: reject negative ping (no idea why this happens in the first place, engine bug)
337 sethostcachemasknumber(++m, SLIST_FIELD_PING, 0, SLIST_TEST_GREATEREQUAL);
340 if(!me.filterShowFull)
342 sethostcachemasknumber(++m, SLIST_FIELD_FREESLOTS, 1, SLIST_TEST_GREATEREQUAL); // legacy
343 sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, ":S0:", SLIST_TEST_NOTCONTAIN); // g_maxplayers support
347 if(!me.filterShowEmpty)
348 sethostcachemasknumber(++m, SLIST_FIELD_NUMHUMANS, 1, SLIST_TEST_GREATEREQUAL);
351 if(!me.filterShowLaggy && autocvar_menu_slist_maxping > 0)
352 sethostcachemasknumber(++m, SLIST_FIELD_PING, autocvar_menu_slist_maxping, SLIST_TEST_LESSEQUAL);
354 // gametype filtering
356 sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, strcat(typestr, ":"), SLIST_TEST_STARTSWITH);
361 if(substring(modstr, 0, 1) == "!")
362 sethostcachemaskstring(++m, SLIST_FIELD_MOD, resolvemod(substring(modstr, 1, strlen(modstr) - 1)), SLIST_TEST_NOTEQUAL);
364 sethostcachemaskstring(++m, SLIST_FIELD_MOD, resolvemod(modstr), SLIST_TEST_EQUAL);
368 int n = tokenizebyseparator(_Nex_ExtResponseSystem_BannedServers, " ");
369 for(int i = 0; i < n; ++i)
371 sethostcachemaskstring(++m, SLIST_FIELD_CNAME, argv(i), SLIST_TEST_NOTSTARTSWITH);
373 m = SLIST_MASK_OR - 1;
376 sethostcachemaskstring(++m, SLIST_FIELD_NAME, s, SLIST_TEST_CONTAINS);
377 sethostcachemaskstring(++m, SLIST_FIELD_MAP, s, SLIST_TEST_CONTAINS);
378 sethostcachemaskstring(++m, SLIST_FIELD_PLAYERS, s, SLIST_TEST_CONTAINS);
379 sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, strcat(s, ":"), SLIST_TEST_STARTSWITH);
382 int sorting_flags = 0;
383 //sorting_flags |= SLSF_FAVORITES;
384 sorting_flags |= SLSF_CATEGORIES;
385 if(me.currentSortOrder < 0) { sorting_flags |= SLSF_DESCENDING; }
386 sethostcachesort(me.currentSortField, sorting_flags);
390 if(mode >= REFRESHSERVERLIST_ASK)
391 refreshhostcache(mode >= REFRESHSERVERLIST_RESET);
393 void XonoticServerList_focusEnter(entity me)
395 SUPER(XonoticServerList).focusEnter(me);
396 if(time < me.nextRefreshTime)
398 //print("sorry, no refresh yet\n");
401 me.nextRefreshTime = time + 10;
402 me.refreshServerList(me, REFRESHSERVERLIST_ASK);
405 void XonoticServerList_draw(entity me)
407 me.serversHeight = (autocvar_menu_slist_categories > 0 ? 1.0 : me.categoriesHeight);
409 if(_Nex_ExtResponseSystem_BannedServersNeedsRefresh)
413 _Nex_ExtResponseSystem_BannedServersNeedsRefresh = 0;
416 if(_Nex_ExtResponseSystem_PromotedServersNeedsRefresh)
420 _Nex_ExtResponseSystem_PromotedServersNeedsRefresh = 0;
423 if(_Nex_ExtResponseSystem_RecommendedServersNeedsRefresh)
427 _Nex_ExtResponseSystem_RecommendedServersNeedsRefresh = 0;
430 if(me.currentSortField == -1)
432 me.setSortOrder(me, SLIST_FIELD_PING, +1);
433 me.refreshServerList(me, REFRESHSERVERLIST_RESET);
435 else if(me.needsRefresh == 1)
437 me.needsRefresh = 2; // delay by one frame to make sure "slist" has been executed
439 else if(me.needsRefresh == 2)
442 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
444 else if(me.needsRefresh == 3)
447 me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
450 bool owned = ((me.selectedServer == me.ipAddressBox.text) && (me.ipAddressBox.text != ""));
452 for(int i = 0; i < category_draw_count; ++i) { category_name[i] = -1; category_item[i] = -1; }
453 category_draw_count = 0;
455 me.nItems = gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT);
457 if(autocvar_menu_slist_categories >= 0) // if less than 0, don't even draw a category heading for favorites
459 int itemcount = me.nItems;
461 //float visible = floor(me.scrollPos / me.itemHeight);
462 // ^ unfortunately no such optimization can be made-- we must process through the
463 // entire list, otherwise there is no way to know which item is first in its category.
465 // binary search method suggested by div
467 for(int j = 1; j <= category_ent_count; ++j) {
469 float last = (itemcount - 1);
474 float catf = gethostcachenumber(SLIST_FIELD_CATEGORY, first);
475 float catl = gethostcachenumber(SLIST_FIELD_CATEGORY, last);
477 // The first one is already > j.
478 // Therefore, category j does not exist.
479 // Higher numbered categories do exist though.
480 } else if (catl < j) {
481 // The last one is < j.
482 // Thus this category - and any following -
485 } else if (catf == j) {
486 // Starts at first. This breaks the loop
487 // invariant in the binary search and thus has
488 // to be handled separately.
489 if(gethostcachenumber(SLIST_FIELD_CATEGORY, first) != j)
490 error("Category mismatch I");
492 if(gethostcachenumber(SLIST_FIELD_CATEGORY, first - 1) == j)
493 error("Category mismatch II");
494 category_name[category_draw_count] = j;
495 category_item[category_draw_count] = first;
496 ++category_draw_count;
499 // At this point, catf <= j < catl, thus
500 // catf < catl, thus first < last.
503 // catf == gethostcachenumber(SLIST_FIELD_CATEGORY(first)
504 // catl == gethostcachenumber(SLIST_FIELD_CATEGORY(last)
507 while (last - first > 1) {
508 float middle = floor((first + last) / 2);
509 // By loop condition, middle != first && middle != last.
510 float cat = gethostcachenumber(SLIST_FIELD_CATEGORY, middle);
520 if(gethostcachenumber(SLIST_FIELD_CATEGORY, last) != j)
521 error("Category mismatch III");
523 if(gethostcachenumber(SLIST_FIELD_CATEGORY, last - 1) == j)
524 error("Category mismatch IV");
525 category_name[category_draw_count] = j;
526 category_item[category_draw_count] = last;
527 ++category_draw_count;
528 begin = last + 1; // already scanned through these, skip 'em
531 begin = last; // already scanned through these, skip 'em
534 if(autocvar_menu_slist_categories_onlyifmultiple && (category_draw_count == 1))
536 category_name[0] = -1;
537 category_item[0] = -1;
538 category_draw_count = 0;
539 me.nItems = itemcount;
543 me.connectButton.disabled = (me.ipAddressBox.text == "");
544 me.infoButton.disabled = !owned;
545 me.favoriteButton.disabled = (me.ipAddressBox.text == "");
548 if(me.lockedSelectedItem)
552 if(gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem) != me.selectedServer)
554 strcpy(me.selectedServer, gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
559 else if(me.selectedServer)
561 for(int i = 0; i < me.nItems; ++i)
563 if(gethostcachestring(SLIST_FIELD_CNAME, i) == me.selectedServer)
565 // don't follow the selected item with SUPER(XonoticServerList).setSelected(me, i);
576 // selected server disappeared, select the last server (scrolling to it)
577 if(me.selectedItem >= me.nItems)
578 SUPER(XonoticServerList).setSelected(me, me.nItems - 1);
579 strcpy(me.selectedServer, gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
585 if(me.selectedServer != me.ipAddressBox.text)
587 me.ipAddressBox.setText(me.ipAddressBox, me.selectedServer);
588 me.ipAddressBox.cursorPos = strlen(me.selectedServer);
589 me.ipAddressBoxFocused = -1;
593 if(me.ipAddressBoxFocused != me.ipAddressBox.focused)
595 if(me.ipAddressBox.focused || me.ipAddressBoxFocused < 0)
596 ServerList_Update_favoriteButton(NULL, me);
597 me.ipAddressBoxFocused = me.ipAddressBox.focused;
600 SUPER(XonoticServerList).draw(me);
602 void ServerList_PingSort_Click(entity btn, entity me)
604 me.setSortOrder(me, SLIST_FIELD_PING, +1);
606 void ServerList_NameSort_Click(entity btn, entity me)
608 me.setSortOrder(me, SLIST_FIELD_NAME, -1); // why?
610 void ServerList_MapSort_Click(entity btn, entity me)
612 me.setSortOrder(me, SLIST_FIELD_MAP, -1); // why?
614 void ServerList_PlayerSort_Click(entity btn, entity me)
616 me.setSortOrder(me, SLIST_FIELD_NUMHUMANS, -1);
618 void ServerList_TypeSort_Click(entity btn, entity me)
620 string s = me.filterString;
621 int m = strstrofs(s, ":", 0);
624 s = substring(s, 0, m);
625 while(substring(s, m+1, 1) == " ") // skip spaces
631 Gametype first = NULL; FOREACH(Gametypes, !first, first = it; break);
633 FOREACH(Gametypes, s == MapInfo_Type_ToString(it), {
634 // the type was found
635 // choose the next one
637 s = MapInfo_Type_ToString(REGISTRY_GET(Gametypes, it.m_id + 1));
638 if (s == "") s = MapInfo_Type_ToString(first);
643 // choose the first one
644 s = MapInfo_Type_ToString(first);
647 if(s != "") s = strcat(s, ":");
648 s = strcat(s, substring(me.filterString, m+1, strlen(me.filterString) - m - 1));
650 me.controlledTextbox.setText(me.controlledTextbox, s);
651 me.controlledTextbox.keyDown(me.controlledTextbox, K_END, 0, 0);
652 me.controlledTextbox.keyUp(me.controlledTextbox, K_END, 0, 0);
653 //ServerList_Filter_Change(me.controlledTextbox, me);
655 void ServerList_Filter_Change(entity box, entity me)
657 strfree(me.filterString);
659 me.filterString = strzone(box.text);
660 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
662 me.ipAddressBox.setText(me.ipAddressBox, "");
663 me.ipAddressBox.cursorPos = 0;
664 me.ipAddressBoxFocused = -1;
666 void ServerList_Categories_Click(entity box, entity me)
668 box.setChecked(box, autocvar_menu_slist_categories = !autocvar_menu_slist_categories);
669 me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
671 me.ipAddressBox.setText(me.ipAddressBox, "");
672 me.ipAddressBox.cursorPos = 0;
673 me.ipAddressBoxFocused = -1;
675 void ServerList_ShowEmpty_Click(entity box, entity me)
677 box.setChecked(box, me.filterShowEmpty = !me.filterShowEmpty);
678 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
680 me.ipAddressBox.setText(me.ipAddressBox, "");
681 me.ipAddressBox.cursorPos = 0;
682 me.ipAddressBoxFocused = -1;
684 void ServerList_ShowFull_Click(entity box, entity me)
686 box.setChecked(box, me.filterShowFull = !me.filterShowFull);
687 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
689 me.ipAddressBox.setText(me.ipAddressBox, "");
690 me.ipAddressBox.cursorPos = 0;
691 me.ipAddressBoxFocused = -1;
693 void ServerList_ShowLaggy_Click(entity box, entity me)
695 box.setChecked(box, me.filterShowLaggy = !me.filterShowLaggy);
696 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
698 me.ipAddressBox.setText(me.ipAddressBox, "");
699 me.ipAddressBox.cursorPos = 0;
700 me.ipAddressBoxFocused = -1;
702 void XonoticServerList_setSortOrder(entity me, int fld, int direction)
704 if(me.currentSortField == fld)
705 direction = -me.currentSortOrder;
706 me.currentSortOrder = direction;
707 me.currentSortField = fld;
708 me.sortButton1.forcePressed = (fld == SLIST_FIELD_PING);
709 me.sortButton2.forcePressed = (fld == SLIST_FIELD_NAME);
710 me.sortButton3.forcePressed = (fld == SLIST_FIELD_MAP);
711 me.sortButton4.forcePressed = 0;
712 me.sortButton5.forcePressed = (fld == SLIST_FIELD_NUMHUMANS);
714 strfree(me.selectedServer);
715 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
717 void XonoticServerList_positionSortButton(entity me, entity btn, float theOrigin, float theSize, string theTitle, void(entity, entity) theFunc)
719 vector originInLBSpace = eY * (-me.itemHeight);
720 vector sizeInLBSpace = eY * me.itemHeight + eX * (1 - me.controlWidth);
722 vector originInDialogSpace = boxToGlobal(originInLBSpace, me.Container_origin, me.Container_size);
723 vector sizeInDialogSpace = boxToGlobalSize(sizeInLBSpace, me.Container_size);
725 btn.Container_origin_x = originInDialogSpace.x + sizeInDialogSpace.x * theOrigin;
726 btn.Container_size_x = sizeInDialogSpace.x * theSize;
727 btn.setText(btn, theTitle);
728 btn.onClick = theFunc;
729 btn.onClickEntity = me;
732 void XonoticServerList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
734 SUPER(XonoticServerList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
736 me.realFontSize_y = me.fontSize / (absSize.y * me.itemHeight);
737 me.realFontSize_x = me.fontSize / (absSize.x * (1 - me.controlWidth));
738 me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
740 me.columnIconsOrigin = 0;
741 me.columnIconsSize = me.realFontSize.x * 5 * me.iconsSizeFactor;
742 me.columnPingSize = me.realFontSize.x * 3;
743 me.columnMapSize = me.realFontSize.x * 10;
744 me.columnTypeSize = me.realFontSize.x * 4;
745 me.columnPlayersSize = me.realFontSize.x * 5;
746 me.columnNameSize = 1 - me.columnPlayersSize - me.columnMapSize - me.columnPingSize - me.columnIconsSize - me.columnTypeSize - 4 * me.realFontSize.x;
747 // no me.realFontSize.x separation between icons and ping columns because in practice they are already separated
748 me.columnPingOrigin = me.columnIconsOrigin + me.columnIconsSize;
749 me.columnNameOrigin = me.columnPingOrigin + me.columnPingSize + me.realFontSize.x;
750 me.columnMapOrigin = me.columnNameOrigin + me.columnNameSize + me.realFontSize.x;
751 me.columnTypeOrigin = me.columnMapOrigin + me.columnMapSize + me.realFontSize.x;
752 me.columnPlayersOrigin = me.columnTypeOrigin + me.columnTypeSize + me.realFontSize.x;
754 me.positionSortButton(me, me.sortButton1, me.columnPingOrigin, me.columnPingSize, _("Ping"), ServerList_PingSort_Click);
755 me.positionSortButton(me, me.sortButton2, me.columnNameOrigin, me.columnNameSize, _("Hostname"), ServerList_NameSort_Click);
756 me.positionSortButton(me, me.sortButton3, me.columnMapOrigin, me.columnMapSize, _("Map"), ServerList_MapSort_Click);
757 me.positionSortButton(me, me.sortButton4, me.columnTypeOrigin, me.columnTypeSize, _("Type"), ServerList_TypeSort_Click);
758 me.positionSortButton(me, me.sortButton5, me.columnPlayersOrigin, me.columnPlayersSize, _("Players"), ServerList_PlayerSort_Click);
760 int f = me.currentSortField;
763 me.currentSortField = -1;
764 me.setSortOrder(me, f, me.currentSortOrder); // force resetting the sort order
767 void ServerList_Connect_Click(entity btn, entity me)
769 if (me.ipAddressBox.text != "")
770 localcmd(sprintf("connect %s\n", me.ipAddressBox.text));
772 void ServerList_Favorite_Click(entity btn, entity this)
774 string addr = this.ipAddressBox.text;
775 string ipstr = netaddress_resolve(addr, 26000);
776 if (ipstr == "") return;
777 m_play_click_sound(MENU_SOUND_SELECT);
778 this.toggleFavorite(this, addr);
779 this.ipAddressBoxFocused = -1;
781 void ServerList_Info_Click(entity btn, entity me)
784 main.serverInfoDialog.loadServerInfo(main.serverInfoDialog, me.selectedItem);
786 float thisPos = me.getItemStart(me, me.selectedItem);
787 float thisHeight = me.getItemHeight(me, me.selectedItem);
788 vector org = boxToGlobal(eY * (thisPos - me.scrollPos), me.origin, me.size);
789 vector sz = boxToGlobalSize(eY * thisHeight + eX * (1 - me.controlWidth), me.size);
790 DialogOpenButton_Click_withCoords(me, main.serverInfoDialog, org, sz);
792 void XonoticServerList_doubleClickListBoxItem(entity me, int i, vector where)
794 ServerList_Connect_Click(NULL, me);
796 void XonoticServerList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
798 vector oldscale = draw_scale;
799 vector oldshift = draw_shift;
800 #define SET_YRANGE(start,end) \
801 draw_scale = boxToGlobalSize(eX + eY * ((end) - (start)), oldscale); \
802 draw_shift = boxToGlobal(eY * (start), oldshift, oldscale);
805 for (c = 0; c < category_draw_count; ++c) {
806 // Matches exactly the headings with increased height.
807 if (i == category_item[c])
811 if (c < category_draw_count)
813 entity catent = RetrieveCategoryEnt(category_name[c]);
817 (me.categoriesHeight - 1) / (me.categoriesHeight + 1),
818 me.categoriesHeight / (me.categoriesHeight + 1)
821 eY * me.realUpperMargin
824 eX * (me.columnNameOrigin + (me.columnNameSize - draw_TextWidth(catent.cat_string, 0, me.realFontSize)) * 0.5),
827 eX * (me.columnNameOrigin),
828 strcat(catent.cat_string, ":"),
831 SKINCOLOR_SERVERLIST_CATEGORY,
832 SKINALPHA_SERVERLIST_CATEGORY,
835 SET_YRANGE(me.categoriesHeight / (me.categoriesHeight + 1), 1);
839 // Now categories are done. Set the Y range in stone.
840 oldscale = draw_scale;
841 oldshift = draw_shift;
843 if(isSelected && !me.lockedSelectedItem)
844 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
847 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
848 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
851 string s = gethostcachestring(SLIST_FIELD_QCSTATUS, i);
852 int m = tokenizebyseparator(s, ":");
853 string typestr = "", versionstr = "";
857 versionstr = argv(1);
859 bool pure = false, pure_available = false;
860 int freeslots = -1, sflags = -1;
862 for(int j = 2; j < m; ++j)
866 string key = substring(argv(j), 0, 1);
867 string value = substring(argv(j), 1, -1);
870 pure = (stof(value) == 0);
871 pure_available = true;
874 freeslots = stof(value);
876 sflags = stoi(value);
881 #ifdef COMPAT_NO_MOD_IS_XONOTIC
886 string original_modname = modname;
887 modname = strtolower(modname);
890 SLIST_FIELD_MOD = gethostcacheindexforkey("mod");
891 s = gethostcachestring(SLIST_FIELD_MOD, i);
893 if(modname == "xonotic")
897 // list the mods here on which the pure server check actually works
898 if(modname != "xonotic")
899 if(modname != "instagib" || modname != "minstagib")
902 if(modname != "newtoys")
903 pure_available = false;
906 if(gethostcachenumber(SLIST_FIELD_FREESLOTS, i) <= 0)
907 theAlpha = SKINALPHA_SERVERLIST_FULL;
908 else if(freeslots == 0)
909 theAlpha = SKINALPHA_SERVERLIST_FULL; // g_maxplayers support
910 else if (!gethostcachenumber(SLIST_FIELD_NUMHUMANS, i))
911 theAlpha = SKINALPHA_SERVERLIST_EMPTY;
915 float ping = gethostcachenumber(SLIST_FIELD_PING, i);
916 const int PING_LOW = 75;
917 const int PING_MED = 200;
918 const int PING_HIGH = 500;
921 theColor = SKINCOLOR_SERVERLIST_LOWPING + (SKINCOLOR_SERVERLIST_MEDPING - SKINCOLOR_SERVERLIST_LOWPING) * (ping / PING_LOW);
922 else if(ping < PING_MED)
923 theColor = SKINCOLOR_SERVERLIST_MEDPING + (SKINCOLOR_SERVERLIST_HIGHPING - SKINCOLOR_SERVERLIST_MEDPING) * ((ping - PING_LOW) / (PING_MED - PING_LOW));
924 else if(ping < PING_HIGH)
926 theColor = SKINCOLOR_SERVERLIST_HIGHPING;
927 theAlpha *= 1 + (SKINALPHA_SERVERLIST_HIGHPING - 1) * ((ping - PING_MED) / (PING_HIGH - PING_MED));
932 theAlpha *= SKINALPHA_SERVERLIST_HIGHPING;
935 if(gethostcachenumber(SLIST_FIELD_ISFAVORITE, i))
937 theColor = theColor * (1 - SKINALPHA_SERVERLIST_FAVORITE) + SKINCOLOR_SERVERLIST_FAVORITE * SKINALPHA_SERVERLIST_FAVORITE;
938 theAlpha = theAlpha * (1 - SKINALPHA_SERVERLIST_FAVORITE) + SKINALPHA_SERVERLIST_FAVORITE;
941 s = gethostcachestring(SLIST_FIELD_CNAME, i);
943 bool isv4 = false, isv6 = false;
944 if(substring(s, 0, 1) == "[")
949 else if(IS_DIGIT(substring(s, 0, 1)))
955 int crypto = stof(substring(crypto_getencryptlevel(s), 0, 1));
956 if((crypto <= 0 && cvar("crypto_aeslevel") >= 3) || (crypto >= 3 && cvar("crypto_aeslevel") <= 0))
958 theColor = SKINCOLOR_SERVERLIST_IMPOSSIBLE;
959 theAlpha = SKINALPHA_SERVERLIST_IMPOSSIBLE;
964 if(cvar("crypto_aeslevel") >= 2)
969 if(cvar("crypto_aeslevel") >= 1)
979 // 2: AES recommended but not available
980 // 3: AES possible and will be used
981 // 4: AES recommended and will be used
987 vector iconSize = '0 0 0';
988 iconSize_y = me.realFontSize.y * me.iconsSizeFactor / me.serversHeight;
989 iconSize_x = me.realFontSize.x * me.iconsSizeFactor;
991 vector iconPos = '0 0 0';
992 if(me.seenIPv4 && me.seenIPv6)
993 iconPos_x = (me.columnIconsSize - 4 * iconSize.x) * 0.5;
995 iconPos_x = (me.columnIconsSize - 3 * iconSize.x) * 0.5;
996 iconPos_y = (1 - iconSize.y) * 0.5;
999 if(me.seenIPv4 && me.seenIPv6)
1002 draw_Picture(iconPos, "icon_ipv6", iconSize, '1 1 1', 1);
1004 draw_Picture(iconPos, "icon_ipv4", iconSize, '1 1 1', 1);
1006 iconPos.x += iconSize.x;
1011 draw_Picture(iconPos, strcat("icon_aeslevel", ftos(crypto)), iconSize, '1 1 1', 1);
1013 iconPos.x += iconSize.x;
1016 if(modname == "xonotic")
1018 // Here, pure_available should always be set. If not, consider
1020 if(pure_available && pure)
1021 draw_Picture(iconPos, "icon_pure1", iconSize, '1 1 1', 1);
1025 string icon = strcat("icon_mod_", modname);
1026 if(draw_PictureSize(icon) == '0 0 0')
1029 // In mods, pure_available not being present indicates
1030 // non-support of the feature. Just show the mod icon as is
1032 if(pure_available && !pure)
1033 draw_Picture(iconPos, icon, iconSize, '1 1 1', SKINALPHA_SERVERLIST_ICON_NONPURE);
1035 draw_Picture(iconPos, icon, iconSize, '1 1 1', 1);
1038 iconPos.x += iconSize.x;
1041 if(sflags >= 0 && (sflags & SERVERFLAG_PLAYERSTATS))
1043 if (sflags & SERVERFLAG_PLAYERSTATS_CUSTOM)
1044 draw_Picture(iconPos, "icon_mod_", iconSize, '1 1 1', 1); // TODO: custom stats server icon
1046 draw_Picture(iconPos, "icon_stats1", iconSize, '1 1 1', 1);
1049 if(isFocused && me.mouseOverIcons && !me.tooltip)
1052 if(me.seenIPv4 && me.seenIPv6)
1053 t = strcat(t, (isv6) ? "IPv6, " : "IPv4, ");
1054 t = strcat(t, _("encryption:"), " ", (crypto ? sprintf(_("AES level %d"), crypto) : ZCTX(_("ENC^none"))), ", ");
1055 t = strcat(t, sprintf(_("mod: %s"), ((modname == "xonotic") ? ZCTX(_("MOD^Default")) : original_modname)));
1057 t = strcat(t, sprintf(" (%s)", (pure) ? _("official settings") : _("modified settings")));
1058 t = strcat(t, ", ");
1059 t = strcat(t, ((sflags >= 0 && (sflags & SERVERFLAG_PLAYERSTATS)) ? ((sflags & SERVERFLAG_PLAYERSTATS_CUSTOM) ? _("custom stats server") : _("stats enabled")) : _("stats disabled")));
1060 setZonedTooltip(me, t, string_null);
1066 // Center it in the row.
1068 0.5 - 0.5 / me.serversHeight,
1069 0.5 + 0.5 / me.serversHeight
1074 draw_Text(me.realUpperMargin * eY + (me.columnPingOrigin + me.columnPingSize - draw_TextWidth(s, 0, me.realFontSize)) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1077 s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_NAME, i), me.columnNameSize, 0, me.realFontSize);
1078 draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, theColor, theAlpha, 0);
1081 s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_MAP, i), me.columnMapSize, 0, me.realFontSize);
1082 draw_Text(me.realUpperMargin * eY + (me.columnMapOrigin + (me.columnMapSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1085 s = draw_TextShortenToWidth(typestr, me.columnTypeSize, 0, me.realFontSize);
1086 draw_Text(me.realUpperMargin * eY + (me.columnTypeOrigin + (me.columnTypeSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1088 // server playercount
1089 s = strcat(ftos(gethostcachenumber(SLIST_FIELD_NUMHUMANS, i)), "/", ftos(gethostcachenumber(SLIST_FIELD_MAXPLAYERS, i)));
1090 draw_Text(me.realUpperMargin * eY + (me.columnPlayersOrigin + (me.columnPlayersSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1093 void XonoticServerList_focusedItemChangeNotify(entity me)
1098 float XonoticServerList_mouseMove(entity me, vector pos)
1100 if(!SUPER(XonoticServerList).mouseMove(me, pos))
1102 me.mouseOverIcons = false;
1107 float iconSize_x = me.realFontSize.x * me.iconsSizeFactor;
1109 if(me.seenIPv4 && me.seenIPv6)
1110 iconPos_x = (me.columnIconsSize - 4 * iconSize_x) * 0.5;
1112 iconPos_x = (me.columnIconsSize - 3 * iconSize_x) * 0.5;
1113 me.mouseOverIcons = (pos.x >= me.columnIconsOrigin + iconPos_x && pos.x <= me.columnIconsOrigin + me.columnIconsSize - iconPos_x);
1114 if(!me.mouseOverIcons)
1119 bool XonoticServerList_keyDown(entity me, int scan, bool ascii, bool shift)
1121 if(scan == K_ENTER || scan == K_KP_ENTER)
1123 ServerList_Connect_Click(NULL, me);
1126 else if(scan == K_MOUSE2 || scan == K_SPACE)
1130 float thisPos = me.getItemStart(me, me.selectedItem);
1131 float thisHeight = me.getItemHeight(me, me.selectedItem);
1132 vector org = boxToGlobal(eY * (thisPos - me.scrollPos), me.origin, me.size);
1133 vector sz = boxToGlobalSize(eY * thisHeight + eX * (1 - me.controlWidth), me.size);
1134 m_play_click_sound(MENU_SOUND_OPEN);
1135 main.serverInfoDialog.loadServerInfo(main.serverInfoDialog, me.selectedItem);
1136 DialogOpenButton_Click_withCoords(me, main.serverInfoDialog, org, sz);
1141 else if(scan == K_INS || scan == K_MOUSE3 || scan == K_KP_INS)
1145 me.toggleFavorite(me, me.selectedServer);
1146 me.ipAddressBoxFocused = -1;
1151 else if(SUPER(XonoticServerList).keyDown(me, scan, ascii, shift))
1153 else if(!me.controlledTextbox)
1156 return me.controlledTextbox.keyDown(me.controlledTextbox, scan, ascii, shift);
1159 float XonoticServerList_getTotalHeight(entity me)
1161 float num_normal_rows = me.nItems;
1162 int num_headers = category_draw_count;
1163 return me.itemHeight * (me.serversHeight * num_normal_rows + me.categoriesHeight * num_headers);
1165 int XonoticServerList_getItemAtPos(entity me, float pos)
1167 pos = pos / me.itemHeight;
1168 for (int i = category_draw_count - 1; i >= 0; --i) {
1169 int itemidx = category_item[i];
1170 float itempos = i * me.categoriesHeight + itemidx * me.serversHeight;
1171 if (pos >= itempos + me.categoriesHeight + me.serversHeight)
1172 return itemidx + 1 + floor((pos - (itempos + me.categoriesHeight + me.serversHeight)) / me.serversHeight);
1176 // No category matches? Note that category 0 is... 0. Therefore no headings exist at all.
1177 return floor(pos / me.serversHeight);
1179 float XonoticServerList_getItemStart(entity me, int item)
1181 for (int i = category_draw_count - 1; i >= 0; --i) {
1182 int itemidx = category_item[i];
1183 float itempos = i * me.categoriesHeight + itemidx * me.serversHeight;
1184 if (item >= itemidx + 1)
1185 return (itempos + me.categoriesHeight + (1 + item - (itemidx + 1)) * me.serversHeight) * me.itemHeight;
1186 if (item >= itemidx)
1187 return itempos * me.itemHeight;
1189 // No category matches? Note that category 0 is... 0. Therefore no headings exist at all.
1190 return item * me.serversHeight * me.itemHeight;
1192 float XonoticServerList_getItemHeight(entity me, int item)
1194 for (int i = 0; i < category_draw_count; ++i) {
1195 // Matches exactly the headings with increased height.
1196 // FIXME server row next to the heading is slightly smaller than others if (autocvar_menu_slist_categories <= 0)
1197 if (item == category_item[i])
1198 return me.itemHeight * (me.categoriesHeight + me.serversHeight);
1200 return me.itemHeight * me.serversHeight;