1 #include "serverlist.qh"
5 #include "mainwindow.qh"
6 #include "dialog_multiplayer_join_serverinfo.qh"
7 #include <common/mapinfo.qh>
9 void RegisterSLCategories()
12 #define SLIST_CATEGORY(name,enoverride,dioverride,str) \
13 SET_FIELD_COUNT(name, CATEGORY_FIRST, category_ent_count) \
14 CHECK_MAX_COUNT(name, MAX_CATEGORIES, category_ent_count, "SLIST_CATEGORY") \
15 cat = categories[name - 1] = new(slist_category); \
16 cat.cat_name = #name; \
17 cat.cat_enoverride_string = strzone(SLIST_CATEGORY_AUTOCVAR(name)); \
18 cat.cat_dioverride_string = strzone(dioverride); \
19 cat.cat_string = strzone(str);
26 #define PROCESS_OVERRIDE(override_string,override_field) \
27 for(i = 0; i < category_ent_count; ++i) \
29 s = categories[i].override_string; \
30 if((s != "") && (s != categories[i].cat_name)) \
33 for(x = 0; x < category_ent_count; ++x) \
34 { if(categories[x].cat_name == s) { \
40 strfree(categories[i].override_string); \
41 categories[i].override_field = catnum; \
47 "RegisterSLCategories(): Improper override '%s' for category '%s'!", \
49 categories[i].cat_name \
53 strfree(categories[i].override_string); \
54 categories[i].override_field = 0; \
56 PROCESS_OVERRIDE(cat_enoverride_string, cat_enoverride)
57 PROCESS_OVERRIDE(cat_dioverride_string, cat_dioverride)
58 #undef PROCESS_OVERRIDE
61 // Supporting Functions
62 entity RetrieveCategoryEnt(int catnum)
64 if((catnum > 0) && (catnum <= category_ent_count))
66 return categories[catnum - 1];
70 error(sprintf("RetrieveCategoryEnt(%d): Improper category number!\n", catnum));
75 bool IsServerInList(string list, string srv)
81 srv = netaddress_resolve(srv, 26000);
84 p = crypto_getidfp(srv);
85 n = tokenize_console(list);
86 for(i = 0; i < n; ++i)
88 if(substring(argv(i), 0, 1) != "[" && strlen(argv(i)) == 44 && strstrofs(argv(i), ".", 0) < 0)
96 if(srv == netaddress_resolve(argv(i), 26000))
103 int CheckCategoryOverride(int cat)
105 entity catent = RetrieveCategoryEnt(cat);
108 int override = (autocvar_menu_slist_categories ? catent.cat_enoverride : catent.cat_dioverride);
109 if(override) { return override; }
114 error(sprintf("CheckCategoryOverride(%d): Improper category number!\n", cat));
119 int CheckCategoryForEntry(int entry)
121 string s, k, v, modtype = "";
122 int j, m, impure = 0, freeslots = 0, sflags = 0;
123 s = gethostcachestring(SLIST_FIELD_QCSTATUS, entry);
124 m = tokenizebyseparator(s, ":");
126 for(j = 2; j < m; ++j)
128 if(argv(j) == "") { break; }
129 k = substring(argv(j), 0, 1);
130 v = substring(argv(j), 1, -1);
133 case "P": { impure = stof(v); break; }
134 case "S": { freeslots = stof(v); break; }
135 case "F": { sflags = stof(v); break; }
136 case "M": { modtype = strtolower(v); break; }
140 if(modtype != "xonotic") { impure += autocvar_menu_slist_modimpurity; }
142 // check if this server is favorited
143 if(gethostcachenumber(SLIST_FIELD_ISFAVORITE, entry)) { return CAT_FAVORITED; }
145 // now check if it's recommended
146 if(autocvar_menu_slist_recommendations)
148 string cname = gethostcachestring(SLIST_FIELD_CNAME, entry);
150 if(IsPromoted(cname)) { return CAT_RECOMMENDED; }
153 float recommended = 0;
154 if(autocvar_menu_slist_recommendations & 1)
156 if(IsRecommended(cname)) { ++recommended; }
157 else { --recommended; }
159 if(autocvar_menu_slist_recommendations & 2)
162 ///// check for minimum free slots
163 (freeslots >= autocvar_menu_slist_recommendations_minfreeslots)
165 && // check for purity requirement
167 (autocvar_menu_slist_recommendations_purethreshold < 0)
169 (impure <= autocvar_menu_slist_recommendations_purethreshold)
172 && // check for minimum amount of humans
174 gethostcachenumber(SLIST_FIELD_NUMHUMANS, entry)
176 autocvar_menu_slist_recommendations_minhumans
179 && // check for maximum latency
181 gethostcachenumber(SLIST_FIELD_PING, entry)
183 autocvar_menu_slist_recommendations_maxping
190 if(recommended > 0) { return CAT_RECOMMENDED; }
194 // if not favorited or recommended, check modname
195 if(modtype != "xonotic")
199 // old servers which don't report their mod name are considered modified now
200 case "": { return CAT_MODIFIED; }
202 case "xpm": { return CAT_XPM; }
204 case "instagib": { return CAT_INSTAGIB; }
205 case "overkill": { return CAT_OVERKILL; }
206 //case "nix": { return CAT_NIX; }
207 //case "newtoys": { return CAT_NEWTOYS; }
209 // "cts" is allowed as compat, xdf is replacement
211 case "xdf": { return CAT_DEFRAG; }
213 default: { LOG_TRACEF("Found strange mod type: %s", modtype); return CAT_MODIFIED; }
217 // must be normal or impure server
218 return ((impure > autocvar_menu_slist_purethreshold) ? CAT_MODIFIED : CAT_NORMAL);
221 METHOD(XonoticServerList, toggleFavorite, void(XonoticServerList this, string srv))
224 string srv_resolved = netaddress_resolve(srv, 26000);
225 string p = crypto_getidfp(srv_resolved);
226 string s = cvar_string("net_slist_favorites");
228 for (int i = 0, n = tokenize_console(s); i < n; ++i)
231 if (substring(argv(i), 0, 1) != "[" && strlen(argv(i)) == 44 && strstrofs(argv(i), ".", 0) < 0)
233 // it's a pubkey hash
234 match = (p && p == argv(i));
239 match = (srv_resolved == netaddress_resolve(argv(i), 26000));
241 if (!match) continue;
244 string before = (i > 0) ? substring(s, 0, argv_end_index(i - 1)) : "";
245 string after = (i < n - 1) ? substring(s, argv_start_index(i + 1), -1) : "";
246 s = strcat(before, (before != "" && after != "" ? " " : ""), after);
249 // TODO: why not break here?
250 n = tokenize_console(s);
251 --i; // offset the increment that is about to happen
255 ret = strcat(s, (s != "" ? " " : ""), p ? p : srv);
257 cvar_set("net_slist_favorites", ret);
258 this.refreshServerList(this, REFRESHSERVERLIST_RESORT);
261 void ServerList_Update_favoriteButton(entity btn, entity me)
263 entity e = me.favoriteButton;
264 if(IsFavorite(me.ipAddressBox.text))
266 e.setText(e, _("Remove"));
271 e.setText(e, _("Favorite"));
272 setZonedTooltip(e, _("Bookmark the currently highlighted server so that it's faster to find in the future"), string_null);
276 entity makeXonoticServerList()
279 me = NEW(XonoticServerList);
280 me.configureXonoticServerList(me);
283 void XonoticServerList_configureXonoticServerList(entity me)
285 me.configureXonoticListBox(me);
288 #define SLIST_FIELD(suffix,name) SLIST_FIELD_##suffix = gethostcacheindexforkey(name);
295 void XonoticServerList_setSelected(entity me, int i)
297 me.lockedSelectedItem = false;
298 //int save = me.selectedItem;
299 SUPER(XonoticServerList).setSelected(me, i);
301 if(me.selectedItem == save)
306 if(gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT) != me.nItems)
307 return; // sorry, it would be wrong
309 strcpy(me.selectedServer, gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
311 me.ipAddressBox.setText(me.ipAddressBox, me.selectedServer);
312 me.ipAddressBox.cursorPos = strlen(me.selectedServer);
313 me.ipAddressBoxFocused = -1;
315 void XonoticServerList_refreshServerList(entity me, int mode)
317 //print("refresh of type ", ftos(mode), "\n");
319 if(mode >= REFRESHSERVERLIST_REFILTER)
324 string s, typestr, modstr;
328 m = strstrofs(s, ":", 0);
331 typestr = substring(s, 0, m);
332 s = substring(s, m + 1, strlen(s) - m - 1);
333 while(substring(s, 0, 1) == " ")
334 s = substring(s, 1, strlen(s) - 1);
339 modstr = cvar_string("menu_slist_modfilter");
341 m = SLIST_MASK_AND - 1;
342 resethostcachemasks();
344 // ping: reject negative ping (no idea why this happens in the first place, engine bug)
345 sethostcachemasknumber(++m, SLIST_FIELD_PING, 0, SLIST_TEST_GREATEREQUAL);
348 if(!me.filterShowFull)
350 sethostcachemasknumber(++m, SLIST_FIELD_FREESLOTS, 1, SLIST_TEST_GREATEREQUAL); // legacy
351 sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, ":S0:", SLIST_TEST_NOTCONTAIN); // g_maxplayers support
355 if(!me.filterShowEmpty)
356 sethostcachemasknumber(++m, SLIST_FIELD_NUMHUMANS, 1, SLIST_TEST_GREATEREQUAL);
358 // gametype filtering
360 sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, strcat(typestr, ":"), SLIST_TEST_STARTSWITH);
365 if(substring(modstr, 0, 1) == "!")
366 sethostcachemaskstring(++m, SLIST_FIELD_MOD, resolvemod(substring(modstr, 1, strlen(modstr) - 1)), SLIST_TEST_NOTEQUAL);
368 sethostcachemaskstring(++m, SLIST_FIELD_MOD, resolvemod(modstr), SLIST_TEST_EQUAL);
372 n = tokenizebyseparator(_Nex_ExtResponseSystem_BannedServers, " ");
373 for(i = 0; i < n; ++i)
375 sethostcachemaskstring(++m, SLIST_FIELD_CNAME, argv(i), SLIST_TEST_NOTSTARTSWITH);
377 m = SLIST_MASK_OR - 1;
380 sethostcachemaskstring(++m, SLIST_FIELD_NAME, s, SLIST_TEST_CONTAINS);
381 sethostcachemaskstring(++m, SLIST_FIELD_MAP, s, SLIST_TEST_CONTAINS);
382 sethostcachemaskstring(++m, SLIST_FIELD_PLAYERS, s, SLIST_TEST_CONTAINS);
383 sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, strcat(s, ":"), SLIST_TEST_STARTSWITH);
387 //listflags |= SLSF_FAVORITES;
388 listflags |= SLSF_CATEGORIES;
389 if(me.currentSortOrder < 0) { listflags |= SLSF_DESCENDING; }
390 sethostcachesort(me.currentSortField, listflags);
394 if(mode >= REFRESHSERVERLIST_ASK)
395 refreshhostcache(mode >= REFRESHSERVERLIST_RESET);
397 void XonoticServerList_focusEnter(entity me)
399 SUPER(XonoticServerList).focusEnter(me);
400 if(time < me.nextRefreshTime)
402 //print("sorry, no refresh yet\n");
405 me.nextRefreshTime = time + 10;
406 me.refreshServerList(me, REFRESHSERVERLIST_ASK);
409 void XonoticServerList_draw(entity me)
412 bool found = false, owned;
414 if(_Nex_ExtResponseSystem_BannedServersNeedsRefresh)
418 _Nex_ExtResponseSystem_BannedServersNeedsRefresh = 0;
421 if(_Nex_ExtResponseSystem_PromotedServersNeedsRefresh)
425 _Nex_ExtResponseSystem_PromotedServersNeedsRefresh = 0;
428 if(_Nex_ExtResponseSystem_RecommendedServersNeedsRefresh)
432 _Nex_ExtResponseSystem_RecommendedServersNeedsRefresh = 0;
435 if(me.currentSortField == -1)
437 me.setSortOrder(me, SLIST_FIELD_PING, +1);
438 me.refreshServerList(me, REFRESHSERVERLIST_RESET);
440 else if(me.needsRefresh == 1)
442 me.needsRefresh = 2; // delay by one frame to make sure "slist" has been executed
444 else if(me.needsRefresh == 2)
447 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
449 else if(me.needsRefresh == 3)
452 me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
455 owned = ((me.selectedServer == me.ipAddressBox.text) && (me.ipAddressBox.text != ""));
457 for(i = 0; i < category_draw_count; ++i) { category_name[i] = -1; category_item[i] = -1; }
458 category_draw_count = 0;
460 if(autocvar_menu_slist_categories >= 0) // if less than 0, don't even draw a category heading for favorites
462 float itemcount = gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT);
463 me.nItems = itemcount;
465 //float visible = floor(me.scrollPos / me.itemHeight);
466 // ^ unfortunately no such optimization can be made-- we must process through the
467 // entire list, otherwise there is no way to know which item is first in its category.
469 // binary search method suggested by div
471 for(int j = 1; j <= category_ent_count; ++j) {
473 float last = (itemcount - 1);
478 float catf = gethostcachenumber(SLIST_FIELD_CATEGORY, first);
479 float catl = gethostcachenumber(SLIST_FIELD_CATEGORY, last);
481 // The first one is already > j.
482 // Therefore, category j does not exist.
483 // Higher numbered categories do exist though.
484 } else if (catl < j) {
485 // The last one is < j.
486 // Thus this category - and any following -
489 } else if (catf == j) {
490 // Starts at first. This breaks the loop
491 // invariant in the binary search and thus has
492 // to be handled separately.
493 if(gethostcachenumber(SLIST_FIELD_CATEGORY, first) != j)
494 error("Category mismatch I");
496 if(gethostcachenumber(SLIST_FIELD_CATEGORY, first - 1) == j)
497 error("Category mismatch II");
498 category_name[category_draw_count] = j;
499 category_item[category_draw_count] = first;
500 ++category_draw_count;
503 // At this point, catf <= j < catl, thus
504 // catf < catl, thus first < last.
507 // catf == gethostcachenumber(SLIST_FIELD_CATEGORY(first)
508 // catl == gethostcachenumber(SLIST_FIELD_CATEGORY(last)
511 while (last - first > 1) {
512 float middle = floor((first + last) / 2);
513 // By loop condition, middle != first && middle != last.
514 float cat = gethostcachenumber(SLIST_FIELD_CATEGORY, middle);
524 if(gethostcachenumber(SLIST_FIELD_CATEGORY, last) != j)
525 error("Category mismatch III");
527 if(gethostcachenumber(SLIST_FIELD_CATEGORY, last - 1) == j)
528 error("Category mismatch IV");
529 category_name[category_draw_count] = j;
530 category_item[category_draw_count] = last;
531 ++category_draw_count;
532 begin = last + 1; // already scanned through these, skip 'em
535 begin = last; // already scanned through these, skip 'em
538 if(autocvar_menu_slist_categories_onlyifmultiple && (category_draw_count == 1))
540 category_name[0] = -1;
541 category_item[0] = -1;
542 category_draw_count = 0;
543 me.nItems = itemcount;
546 else { me.nItems = gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT); }
548 me.connectButton.disabled = (me.lockedSelectedItem || (me.nItems == 0 && me.ipAddressBox.text == ""));
549 me.disconnectButton.disabled = (!(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)));
550 me.infoButton.disabled = (me.lockedSelectedItem || me.nItems == 0 || !owned);
551 me.favoriteButton.disabled = (me.lockedSelectedItem || (me.nItems == 0 && me.ipAddressBox.text == ""));
553 if(me.lockedSelectedItem)
557 if(gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem) != me.selectedServer)
559 strcpy(me.selectedServer, gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
564 else if(me.selectedServer)
566 for(i = 0; i < me.nItems; ++i)
568 if(gethostcachestring(SLIST_FIELD_CNAME, i) == me.selectedServer)
570 // don't follow the selected item with SUPER(XonoticServerList).setSelected(me, i);
581 // selected server disappeared, select the last server (scrolling to it)
582 if(me.selectedItem >= me.nItems)
583 SUPER(XonoticServerList).setSelected(me, me.nItems - 1);
584 strcpy(me.selectedServer, gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
590 if(me.selectedServer != me.ipAddressBox.text)
592 me.ipAddressBox.setText(me.ipAddressBox, me.selectedServer);
593 me.ipAddressBox.cursorPos = strlen(me.selectedServer);
594 me.ipAddressBoxFocused = -1;
598 if(me.ipAddressBoxFocused != me.ipAddressBox.focused)
600 if(me.ipAddressBox.focused || me.ipAddressBoxFocused < 0)
601 ServerList_Update_favoriteButton(NULL, me);
602 me.ipAddressBoxFocused = me.ipAddressBox.focused;
605 SUPER(XonoticServerList).draw(me);
607 void ServerList_PingSort_Click(entity btn, entity me)
609 me.setSortOrder(me, SLIST_FIELD_PING, +1);
611 void ServerList_NameSort_Click(entity btn, entity me)
613 me.setSortOrder(me, SLIST_FIELD_NAME, -1); // why?
615 void ServerList_MapSort_Click(entity btn, entity me)
617 me.setSortOrder(me, SLIST_FIELD_MAP, -1); // why?
619 void ServerList_PlayerSort_Click(entity btn, entity me)
621 me.setSortOrder(me, SLIST_FIELD_NUMHUMANS, -1);
623 void ServerList_TypeSort_Click(entity btn, entity me)
625 string s = me.filterString;
626 int m = strstrofs(s, ":", 0);
629 s = substring(s, 0, m);
630 while(substring(s, m+1, 1) == " ") // skip spaces
636 Gametype first = NULL; FOREACH(Gametypes, !first, first = it; break);
638 FOREACH(Gametypes, s == MapInfo_Type_ToString(it), {
639 // the type was found
640 // choose the next one
642 s = MapInfo_Type_ToString(Gametypes_from(it.m_id + 1));
643 if (s == "") s = MapInfo_Type_ToString(first);
648 // choose the first one
649 s = MapInfo_Type_ToString(first);
652 if(s != "") s = strcat(s, ":");
653 s = strcat(s, substring(me.filterString, m+1, strlen(me.filterString) - m - 1));
655 me.controlledTextbox.setText(me.controlledTextbox, s);
656 me.controlledTextbox.keyDown(me.controlledTextbox, K_END, 0, 0);
657 me.controlledTextbox.keyUp(me.controlledTextbox, K_END, 0, 0);
658 //ServerList_Filter_Change(me.controlledTextbox, me);
660 void ServerList_Filter_Change(entity box, entity me)
662 strfree(me.filterString);
664 me.filterString = strzone(box.text);
665 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
667 me.ipAddressBox.setText(me.ipAddressBox, "");
668 me.ipAddressBox.cursorPos = 0;
669 me.ipAddressBoxFocused = -1;
671 void ServerList_Categories_Click(entity box, entity me)
673 box.setChecked(box, autocvar_menu_slist_categories = !autocvar_menu_slist_categories);
674 me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
676 me.ipAddressBox.setText(me.ipAddressBox, "");
677 me.ipAddressBox.cursorPos = 0;
678 me.ipAddressBoxFocused = -1;
680 void ServerList_ShowEmpty_Click(entity box, entity me)
682 box.setChecked(box, me.filterShowEmpty = !me.filterShowEmpty);
683 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
685 me.ipAddressBox.setText(me.ipAddressBox, "");
686 me.ipAddressBox.cursorPos = 0;
687 me.ipAddressBoxFocused = -1;
689 void ServerList_ShowFull_Click(entity box, entity me)
691 box.setChecked(box, me.filterShowFull = !me.filterShowFull);
692 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
694 me.ipAddressBox.setText(me.ipAddressBox, "");
695 me.ipAddressBox.cursorPos = 0;
696 me.ipAddressBoxFocused = -1;
698 void XonoticServerList_setSortOrder(entity me, int fld, int direction)
700 if(me.currentSortField == fld)
701 direction = -me.currentSortOrder;
702 me.currentSortOrder = direction;
703 me.currentSortField = fld;
704 me.sortButton1.forcePressed = (fld == SLIST_FIELD_PING);
705 me.sortButton2.forcePressed = (fld == SLIST_FIELD_NAME);
706 me.sortButton3.forcePressed = (fld == SLIST_FIELD_MAP);
707 me.sortButton4.forcePressed = 0;
708 me.sortButton5.forcePressed = (fld == SLIST_FIELD_NUMHUMANS);
710 strfree(me.selectedServer);
711 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
713 void XonoticServerList_positionSortButton(entity me, entity btn, float theOrigin, float theSize, string theTitle, void(entity, entity) theFunc)
715 vector originInLBSpace, sizeInLBSpace;
716 originInLBSpace = eY * (-me.itemHeight);
717 sizeInLBSpace = eY * me.itemHeight + eX * (1 - me.controlWidth);
719 vector originInDialogSpace, sizeInDialogSpace;
720 originInDialogSpace = boxToGlobal(originInLBSpace, me.Container_origin, me.Container_size);
721 sizeInDialogSpace = boxToGlobalSize(sizeInLBSpace, me.Container_size);
723 btn.Container_origin_x = originInDialogSpace.x + sizeInDialogSpace.x * theOrigin;
724 btn.Container_size_x = sizeInDialogSpace.x * theSize;
725 btn.setText(btn, theTitle);
726 btn.onClick = theFunc;
727 btn.onClickEntity = me;
730 void XonoticServerList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
732 SUPER(XonoticServerList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
734 me.realFontSize_y = me.fontSize / (absSize.y * me.itemHeight);
735 me.realFontSize_x = me.fontSize / (absSize.x * (1 - me.controlWidth));
736 me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
738 me.columnIconsOrigin = 0;
739 me.columnIconsSize = me.realFontSize.x * 4 * me.iconsSizeFactor;
740 me.columnPingSize = me.realFontSize.x * 3;
741 me.columnMapSize = me.realFontSize.x * 10;
742 me.columnTypeSize = me.realFontSize.x * 4;
743 me.columnPlayersSize = me.realFontSize.x * 5;
744 me.columnNameSize = 1 - me.columnPlayersSize - me.columnMapSize - me.columnPingSize - me.columnIconsSize - me.columnTypeSize - 5 * me.realFontSize.x;
745 me.columnPingOrigin = me.columnIconsOrigin + me.columnIconsSize + me.realFontSize.x;
746 me.columnNameOrigin = me.columnPingOrigin + me.columnPingSize + me.realFontSize.x;
747 me.columnMapOrigin = me.columnNameOrigin + me.columnNameSize + me.realFontSize.x;
748 me.columnTypeOrigin = me.columnMapOrigin + me.columnMapSize + me.realFontSize.x;
749 me.columnPlayersOrigin = me.columnTypeOrigin + me.columnTypeSize + me.realFontSize.x;
751 me.positionSortButton(me, me.sortButton1, me.columnPingOrigin, me.columnPingSize, _("Ping"), ServerList_PingSort_Click);
752 me.positionSortButton(me, me.sortButton2, me.columnNameOrigin, me.columnNameSize, _("Hostname"), ServerList_NameSort_Click);
753 me.positionSortButton(me, me.sortButton3, me.columnMapOrigin, me.columnMapSize, _("Map"), ServerList_MapSort_Click);
754 me.positionSortButton(me, me.sortButton4, me.columnTypeOrigin, me.columnTypeSize, _("Type"), ServerList_TypeSort_Click);
755 me.positionSortButton(me, me.sortButton5, me.columnPlayersOrigin, me.columnPlayersSize, _("Players"), ServerList_PlayerSort_Click);
757 int f = me.currentSortField;
760 me.currentSortField = -1;
761 me.setSortOrder(me, f, me.currentSortOrder); // force resetting the sort order
764 void ServerList_Connect_Click(entity btn, entity me)
766 if (me.lockedSelectedItem)
768 string sv = (me.ipAddressBox.text != "") ? me.ipAddressBox.text : me.selectedServer;
769 localcmd(sprintf("connect %s\n", sv));
771 void ServerList_Favorite_Click(entity btn, entity this)
773 string addr = this.ipAddressBox.text;
774 string ipstr = netaddress_resolve(addr, 26000);
775 if (ipstr == "") return;
776 m_play_click_sound(MENU_SOUND_SELECT);
777 this.toggleFavorite(this, addr);
778 this.ipAddressBoxFocused = -1;
780 void ServerList_Info_Click(entity btn, entity me)
783 main.serverInfoDialog.loadServerInfo(main.serverInfoDialog, me.selectedItem);
785 vector org = boxToGlobal(eY * (me.selectedItem * me.itemHeight - me.scrollPos), me.origin, me.size);
786 vector sz = boxToGlobalSize(eY * me.itemHeight + eX * (1 - me.controlWidth), me.size);
787 DialogOpenButton_Click_withCoords(me, main.serverInfoDialog, org, sz);
789 void XonoticServerList_doubleClickListBoxItem(entity me, int i, vector where)
791 ServerList_Connect_Click(NULL, me);
793 void XonoticServerList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
795 // layout: Ping, Server name, Map name, NP, TP, MP
801 bool pure = false, pure_available = false;
802 int freeslots = -1, sflags = -1, j, m;
803 string s, typestr, versionstr, k, v, modname;
805 //printf("time: %f, i: %d, item: %d, nitems: %d\n", time, i, item, me.nItems);
807 vector oldscale = draw_scale;
808 vector oldshift = draw_shift;
809 #define SET_YRANGE(start,end) \
810 draw_scale = boxToGlobalSize(eX * 1 + eY * (end - start), oldscale); \
811 draw_shift = boxToGlobal(eY * start, oldshift, oldscale);
813 for (j = 0; j < category_draw_count; ++j) {
814 // Matches exactly the headings with increased height.
815 if (i == category_item[j])
819 if (j < category_draw_count)
821 entity catent = RetrieveCategoryEnt(category_name[j]);
825 (me.categoriesHeight - 1) / (me.categoriesHeight + 1),
826 me.categoriesHeight / (me.categoriesHeight + 1)
829 eY * me.realUpperMargin
832 eX * (me.columnNameOrigin + (me.columnNameSize - draw_TextWidth(catent.cat_string, 0, me.realFontSize)) * 0.5),
835 eX * (me.columnNameOrigin),
836 strcat(catent.cat_string, ":"),
839 SKINCOLOR_SERVERLIST_CATEGORY,
840 SKINALPHA_SERVERLIST_CATEGORY,
843 SET_YRANGE(me.categoriesHeight / (me.categoriesHeight + 1), 1);
847 if(isSelected && !me.lockedSelectedItem)
848 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
851 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
852 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
855 s = gethostcachestring(SLIST_FIELD_QCSTATUS, i);
856 m = tokenizebyseparator(s, ":");
861 versionstr = argv(1);
865 for(j = 2; j < m; ++j)
869 k = substring(argv(j), 0, 1);
870 v = substring(argv(j), 1, -1);
873 pure = (stof(v) == 0);
874 pure_available = true;
884 #ifdef COMPAT_NO_MOD_IS_XONOTIC
889 string original_modname = modname;
890 modname = strtolower(modname);
893 SLIST_FIELD_MOD = gethostcacheindexforkey("mod");
894 s = gethostcachestring(SLIST_FIELD_MOD, i);
896 if(modname == "xonotic")
900 // list the mods here on which the pure server check actually works
901 if(modname != "xonotic")
902 if(modname != "instagib" || modname != "minstagib")
905 if(modname != "newtoys")
906 pure_available = false;
908 if(gethostcachenumber(SLIST_FIELD_FREESLOTS, i) <= 0)
909 theAlpha = SKINALPHA_SERVERLIST_FULL;
910 else if(freeslots == 0)
911 theAlpha = SKINALPHA_SERVERLIST_FULL; // g_maxplayers support
912 else if (!gethostcachenumber(SLIST_FIELD_NUMHUMANS, i))
913 theAlpha = SKINALPHA_SERVERLIST_EMPTY;
917 p = gethostcachenumber(SLIST_FIELD_PING, i);
918 const int PING_LOW = 75;
919 const int PING_MED = 200;
920 const int PING_HIGH = 500;
922 theColor = SKINCOLOR_SERVERLIST_LOWPING + (SKINCOLOR_SERVERLIST_MEDPING - SKINCOLOR_SERVERLIST_LOWPING) * (p / PING_LOW);
923 else if(p < PING_MED)
924 theColor = SKINCOLOR_SERVERLIST_MEDPING + (SKINCOLOR_SERVERLIST_HIGHPING - SKINCOLOR_SERVERLIST_MEDPING) * ((p - PING_LOW) / (PING_MED - PING_LOW));
925 else if(p < PING_HIGH)
927 theColor = SKINCOLOR_SERVERLIST_HIGHPING;
928 theAlpha *= 1 + (SKINALPHA_SERVERLIST_HIGHPING - 1) * ((p - PING_MED) / (PING_HIGH - PING_MED));
933 theAlpha *= SKINALPHA_SERVERLIST_HIGHPING;
936 if(gethostcachenumber(SLIST_FIELD_ISFAVORITE, i))
938 theColor = theColor * (1 - SKINALPHA_SERVERLIST_FAVORITE) + SKINCOLOR_SERVERLIST_FAVORITE * SKINALPHA_SERVERLIST_FAVORITE;
939 theAlpha = theAlpha * (1 - SKINALPHA_SERVERLIST_FAVORITE) + SKINALPHA_SERVERLIST_FAVORITE;
942 s = gethostcachestring(SLIST_FIELD_CNAME, i);
945 if(substring(s, 0, 1) == "[")
950 else if(IS_DIGIT(substring(s, 0, 1)))
956 q = stof(substring(crypto_getencryptlevel(s), 0, 1));
957 if((q <= 0 && cvar("crypto_aeslevel") >= 3) || (q >= 3 && cvar("crypto_aeslevel") <= 0))
959 theColor = SKINCOLOR_SERVERLIST_IMPOSSIBLE;
960 theAlpha = SKINALPHA_SERVERLIST_IMPOSSIBLE;
965 if(cvar("crypto_aeslevel") >= 2)
970 if(cvar("crypto_aeslevel") >= 1)
980 // 2: AES recommended but not available
981 // 3: AES possible and will be used
982 // 4: AES recommended and will be used
988 vector iconSize = '0 0 0';
989 iconSize_y = me.realFontSize.y * me.iconsSizeFactor;
990 iconSize_x = me.realFontSize.x * me.iconsSizeFactor;
992 vector iconPos = '0 0 0';
993 iconPos_x = (me.columnIconsSize - 3 * iconSize.x) * 0.5;
994 iconPos_y = (1 - iconSize.y) * 0.5;
997 if(me.seenIPv4 && me.seenIPv6)
1000 draw_Picture(iconPos, "icon_ipv6", iconSize, '1 1 1', 1);
1002 draw_Picture(iconPos, "icon_ipv4", iconSize, '1 1 1', 1);
1005 iconPos.x += iconSize.x;
1009 draw_Picture(iconPos, strcat("icon_aeslevel", ftos(q)), iconSize, '1 1 1', 1);
1011 iconPos.x += iconSize.x;
1014 if(modname == "xonotic")
1016 // Here, pure_available should always be set. If not, consider
1018 if(pure_available && pure)
1019 draw_Picture(iconPos, "icon_pure1", iconSize, '1 1 1', 1);
1023 string icon = strcat("icon_mod_", modname);
1024 if(draw_PictureSize(icon) == '0 0 0')
1027 // In mods, pure_available not being present indicates
1028 // non-support of the feature. Just show the mod icon as is
1030 if(pure_available && !pure)
1031 draw_Picture(iconPos, icon, iconSize, '1 1 1', SKINALPHA_SERVERLIST_ICON_NONPURE);
1033 draw_Picture(iconPos, icon, iconSize, '1 1 1', 1);
1036 iconPos.x += iconSize.x;
1039 if(sflags >= 0 && (sflags & SERVERFLAG_PLAYERSTATS))
1040 draw_Picture(iconPos, "icon_stats1", iconSize, '1 1 1', 1);
1042 if(isFocused && me.mouseOverIcons && !me.tooltip)
1045 if(me.seenIPv4 && me.seenIPv6)
1046 t = strcat(t, (isv6) ? "IPv6, " : "IPv4, ");
1047 t = strcat(t, _("encryption:"), " ", (q ? sprintf(_("AES level %d"), q) : ZCTX(_("ENC^none"))), ", ");
1048 t = strcat(t, sprintf(_("mod: %s"), ((modname == "xonotic") ? ZCTX(_("MOD^Default")) : original_modname)));
1050 t = strcat(t, sprintf(" (%s)", (pure) ? _("official settings") : _("modified settings")));
1051 t = strcat(t, ", ");
1052 t = strcat(t, ((sflags >= 0 && (sflags & SERVERFLAG_PLAYERSTATS)) ? _("stats enabled") : _("stats disabled")));
1053 setZonedTooltip(me, t, string_null);
1061 draw_Text(me.realUpperMargin * eY + (me.columnPingOrigin + me.columnPingSize - draw_TextWidth(s, 0, me.realFontSize)) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1064 s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_NAME, i), me.columnNameSize, 0, me.realFontSize);
1065 draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, theColor, theAlpha, 0);
1068 s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_MAP, i), me.columnMapSize, 0, me.realFontSize);
1069 draw_Text(me.realUpperMargin * eY + (me.columnMapOrigin + (me.columnMapSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1072 s = draw_TextShortenToWidth(typestr, me.columnTypeSize, 0, me.realFontSize);
1073 draw_Text(me.realUpperMargin * eY + (me.columnTypeOrigin + (me.columnTypeSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1075 // server playercount
1076 s = strcat(ftos(gethostcachenumber(SLIST_FIELD_NUMHUMANS, i)), "/", ftos(gethostcachenumber(SLIST_FIELD_MAXPLAYERS, i)));
1077 draw_Text(me.realUpperMargin * eY + (me.columnPlayersOrigin + (me.columnPlayersSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1080 void XonoticServerList_focusedItemChangeNotify(entity me)
1085 float XonoticServerList_mouseMove(entity me, vector pos)
1087 if(!SUPER(XonoticServerList).mouseMove(me, pos))
1089 me.mouseOverIcons = false;
1094 me.mouseOverIcons = (pos_x <= me.columnIconsSize);
1095 if(!me.mouseOverIcons)
1100 bool XonoticServerList_keyDown(entity me, int scan, bool ascii, bool shift)
1104 org = boxToGlobal(eY * (me.selectedItem * me.itemHeight - me.scrollPos), me.origin, me.size);
1105 sz = boxToGlobalSize(eY * me.itemHeight + eX * (1 - me.controlWidth), me.size);
1107 if(scan == K_ENTER || scan == K_KP_ENTER)
1109 ServerList_Connect_Click(NULL, me);
1112 else if(scan == K_MOUSE2 || scan == K_SPACE)
1116 m_play_click_sound(MENU_SOUND_OPEN);
1117 main.serverInfoDialog.loadServerInfo(main.serverInfoDialog, me.selectedItem);
1118 DialogOpenButton_Click_withCoords(me, main.serverInfoDialog, org, sz);
1123 else if(scan == K_INS || scan == K_MOUSE3 || scan == K_KP_INS)
1127 me.toggleFavorite(me, me.selectedServer);
1128 me.ipAddressBoxFocused = -1;
1133 else if(SUPER(XonoticServerList).keyDown(me, scan, ascii, shift))
1135 else if(!me.controlledTextbox)
1138 return me.controlledTextbox.keyDown(me.controlledTextbox, scan, ascii, shift);
1141 float XonoticServerList_getTotalHeight(entity me)
1143 float num_normal_rows = me.nItems;
1144 int num_headers = category_draw_count;
1145 return me.itemHeight * (num_normal_rows + me.categoriesHeight * num_headers);
1147 int XonoticServerList_getItemAtPos(entity me, float pos)
1149 pos = pos / me.itemHeight;
1151 for (i = category_draw_count - 1; i >= 0; --i) {
1152 int itemidx = category_item[i];
1153 float itempos = i * me.categoriesHeight + category_item[i];
1154 if (pos >= itempos + me.categoriesHeight + 1)
1155 return itemidx + 1 + floor(pos - (itempos + me.categoriesHeight + 1));
1159 // No category matches? Note that category 0 is... 0. Therefore no headings exist at all.
1162 float XonoticServerList_getItemStart(entity me, int item)
1165 for (i = category_draw_count - 1; i >= 0; --i) {
1166 int itemidx = category_item[i];
1167 float itempos = i * me.categoriesHeight + category_item[i];
1168 if (item >= itemidx + 1)
1169 return (itempos + me.categoriesHeight + 1 + item - (itemidx + 1)) * me.itemHeight;
1170 if (item >= itemidx)
1171 return itempos * me.itemHeight;
1173 // No category matches? Note that category 0 is... 0. Therefore no headings exist at all.
1174 return item * me.itemHeight;
1176 float XonoticServerList_getItemHeight(entity me, int item)
1179 for (i = 0; i < category_draw_count; ++i) {
1180 // Matches exactly the headings with increased height.
1181 if (item == category_item[i])
1182 return me.itemHeight * (me.categoriesHeight + 1);
1184 return me.itemHeight;