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.infoButton.disabled = (me.lockedSelectedItem || me.nItems == 0 || !owned);
550 me.favoriteButton.disabled = (me.lockedSelectedItem || (me.nItems == 0 && me.ipAddressBox.text == ""));
552 if(me.lockedSelectedItem)
556 if(gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem) != me.selectedServer)
558 strcpy(me.selectedServer, gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
563 else if(me.selectedServer)
565 for(i = 0; i < me.nItems; ++i)
567 if(gethostcachestring(SLIST_FIELD_CNAME, i) == me.selectedServer)
569 // don't follow the selected item with SUPER(XonoticServerList).setSelected(me, i);
580 // selected server disappeared, select the last server (scrolling to it)
581 if(me.selectedItem >= me.nItems)
582 SUPER(XonoticServerList).setSelected(me, me.nItems - 1);
583 strcpy(me.selectedServer, gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
589 if(me.selectedServer != me.ipAddressBox.text)
591 me.ipAddressBox.setText(me.ipAddressBox, me.selectedServer);
592 me.ipAddressBox.cursorPos = strlen(me.selectedServer);
593 me.ipAddressBoxFocused = -1;
597 if(me.ipAddressBoxFocused != me.ipAddressBox.focused)
599 if(me.ipAddressBox.focused || me.ipAddressBoxFocused < 0)
600 ServerList_Update_favoriteButton(NULL, me);
601 me.ipAddressBoxFocused = me.ipAddressBox.focused;
604 SUPER(XonoticServerList).draw(me);
606 void ServerList_PingSort_Click(entity btn, entity me)
608 me.setSortOrder(me, SLIST_FIELD_PING, +1);
610 void ServerList_NameSort_Click(entity btn, entity me)
612 me.setSortOrder(me, SLIST_FIELD_NAME, -1); // why?
614 void ServerList_MapSort_Click(entity btn, entity me)
616 me.setSortOrder(me, SLIST_FIELD_MAP, -1); // why?
618 void ServerList_PlayerSort_Click(entity btn, entity me)
620 me.setSortOrder(me, SLIST_FIELD_NUMHUMANS, -1);
622 void ServerList_TypeSort_Click(entity btn, entity me)
624 string s = me.filterString;
625 int m = strstrofs(s, ":", 0);
628 s = substring(s, 0, m);
629 while(substring(s, m+1, 1) == " ") // skip spaces
635 Gametype first = NULL; FOREACH(Gametypes, !first, first = it; break);
637 FOREACH(Gametypes, s == MapInfo_Type_ToString(it), {
638 // the type was found
639 // choose the next one
641 s = MapInfo_Type_ToString(Gametypes_from(it.m_id + 1));
642 if (s == "") s = MapInfo_Type_ToString(first);
647 // choose the first one
648 s = MapInfo_Type_ToString(first);
651 if(s != "") s = strcat(s, ":");
652 s = strcat(s, substring(me.filterString, m+1, strlen(me.filterString) - m - 1));
654 me.controlledTextbox.setText(me.controlledTextbox, s);
655 me.controlledTextbox.keyDown(me.controlledTextbox, K_END, 0, 0);
656 me.controlledTextbox.keyUp(me.controlledTextbox, K_END, 0, 0);
657 //ServerList_Filter_Change(me.controlledTextbox, me);
659 void ServerList_Filter_Change(entity box, entity me)
661 strfree(me.filterString);
663 me.filterString = strzone(box.text);
664 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
666 me.ipAddressBox.setText(me.ipAddressBox, "");
667 me.ipAddressBox.cursorPos = 0;
668 me.ipAddressBoxFocused = -1;
670 void ServerList_Categories_Click(entity box, entity me)
672 box.setChecked(box, autocvar_menu_slist_categories = !autocvar_menu_slist_categories);
673 me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
675 me.ipAddressBox.setText(me.ipAddressBox, "");
676 me.ipAddressBox.cursorPos = 0;
677 me.ipAddressBoxFocused = -1;
679 void ServerList_ShowEmpty_Click(entity box, entity me)
681 box.setChecked(box, me.filterShowEmpty = !me.filterShowEmpty);
682 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
684 me.ipAddressBox.setText(me.ipAddressBox, "");
685 me.ipAddressBox.cursorPos = 0;
686 me.ipAddressBoxFocused = -1;
688 void ServerList_ShowFull_Click(entity box, entity me)
690 box.setChecked(box, me.filterShowFull = !me.filterShowFull);
691 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
693 me.ipAddressBox.setText(me.ipAddressBox, "");
694 me.ipAddressBox.cursorPos = 0;
695 me.ipAddressBoxFocused = -1;
697 void XonoticServerList_setSortOrder(entity me, int fld, int direction)
699 if(me.currentSortField == fld)
700 direction = -me.currentSortOrder;
701 me.currentSortOrder = direction;
702 me.currentSortField = fld;
703 me.sortButton1.forcePressed = (fld == SLIST_FIELD_PING);
704 me.sortButton2.forcePressed = (fld == SLIST_FIELD_NAME);
705 me.sortButton3.forcePressed = (fld == SLIST_FIELD_MAP);
706 me.sortButton4.forcePressed = 0;
707 me.sortButton5.forcePressed = (fld == SLIST_FIELD_NUMHUMANS);
709 strfree(me.selectedServer);
710 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
712 void XonoticServerList_positionSortButton(entity me, entity btn, float theOrigin, float theSize, string theTitle, void(entity, entity) theFunc)
714 vector originInLBSpace, sizeInLBSpace;
715 originInLBSpace = eY * (-me.itemHeight);
716 sizeInLBSpace = eY * me.itemHeight + eX * (1 - me.controlWidth);
718 vector originInDialogSpace, sizeInDialogSpace;
719 originInDialogSpace = boxToGlobal(originInLBSpace, me.Container_origin, me.Container_size);
720 sizeInDialogSpace = boxToGlobalSize(sizeInLBSpace, me.Container_size);
722 btn.Container_origin_x = originInDialogSpace.x + sizeInDialogSpace.x * theOrigin;
723 btn.Container_size_x = sizeInDialogSpace.x * theSize;
724 btn.setText(btn, theTitle);
725 btn.onClick = theFunc;
726 btn.onClickEntity = me;
729 void XonoticServerList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
731 SUPER(XonoticServerList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
733 me.realFontSize_y = me.fontSize / (absSize.y * me.itemHeight);
734 me.realFontSize_x = me.fontSize / (absSize.x * (1 - me.controlWidth));
735 me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
737 me.columnIconsOrigin = 0;
738 me.columnIconsSize = me.realFontSize.x * 4 * me.iconsSizeFactor;
739 me.columnPingSize = me.realFontSize.x * 3;
740 me.columnMapSize = me.realFontSize.x * 10;
741 me.columnTypeSize = me.realFontSize.x * 4;
742 me.columnPlayersSize = me.realFontSize.x * 5;
743 me.columnNameSize = 1 - me.columnPlayersSize - me.columnMapSize - me.columnPingSize - me.columnIconsSize - me.columnTypeSize - 5 * me.realFontSize.x;
744 me.columnPingOrigin = me.columnIconsOrigin + me.columnIconsSize + me.realFontSize.x;
745 me.columnNameOrigin = me.columnPingOrigin + me.columnPingSize + me.realFontSize.x;
746 me.columnMapOrigin = me.columnNameOrigin + me.columnNameSize + me.realFontSize.x;
747 me.columnTypeOrigin = me.columnMapOrigin + me.columnMapSize + me.realFontSize.x;
748 me.columnPlayersOrigin = me.columnTypeOrigin + me.columnTypeSize + me.realFontSize.x;
750 me.positionSortButton(me, me.sortButton1, me.columnPingOrigin, me.columnPingSize, _("Ping"), ServerList_PingSort_Click);
751 me.positionSortButton(me, me.sortButton2, me.columnNameOrigin, me.columnNameSize, _("Hostname"), ServerList_NameSort_Click);
752 me.positionSortButton(me, me.sortButton3, me.columnMapOrigin, me.columnMapSize, _("Map"), ServerList_MapSort_Click);
753 me.positionSortButton(me, me.sortButton4, me.columnTypeOrigin, me.columnTypeSize, _("Type"), ServerList_TypeSort_Click);
754 me.positionSortButton(me, me.sortButton5, me.columnPlayersOrigin, me.columnPlayersSize, _("Players"), ServerList_PlayerSort_Click);
756 int f = me.currentSortField;
759 me.currentSortField = -1;
760 me.setSortOrder(me, f, me.currentSortOrder); // force resetting the sort order
763 void ServerList_Connect_Click(entity btn, entity me)
765 if (me.lockedSelectedItem)
767 string sv = (me.ipAddressBox.text != "") ? me.ipAddressBox.text : me.selectedServer;
768 localcmd(sprintf("connect %s\n", sv));
770 void ServerList_Favorite_Click(entity btn, entity this)
772 string addr = this.ipAddressBox.text;
773 string ipstr = netaddress_resolve(addr, 26000);
774 if (ipstr == "") return;
775 m_play_click_sound(MENU_SOUND_SELECT);
776 this.toggleFavorite(this, addr);
777 this.ipAddressBoxFocused = -1;
779 void ServerList_Info_Click(entity btn, entity me)
782 main.serverInfoDialog.loadServerInfo(main.serverInfoDialog, me.selectedItem);
784 vector org = boxToGlobal(eY * (me.selectedItem * me.itemHeight - me.scrollPos), me.origin, me.size);
785 vector sz = boxToGlobalSize(eY * me.itemHeight + eX * (1 - me.controlWidth), me.size);
786 DialogOpenButton_Click_withCoords(me, main.serverInfoDialog, org, sz);
788 void XonoticServerList_doubleClickListBoxItem(entity me, int i, vector where)
790 ServerList_Connect_Click(NULL, me);
792 void XonoticServerList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
794 // layout: Ping, Server name, Map name, NP, TP, MP
800 bool pure = false, pure_available = false;
801 int freeslots = -1, sflags = -1, j, m;
802 string s, typestr, versionstr, k, v, modname;
804 //printf("time: %f, i: %d, item: %d, nitems: %d\n", time, i, item, me.nItems);
806 vector oldscale = draw_scale;
807 vector oldshift = draw_shift;
808 #define SET_YRANGE(start,end) \
809 draw_scale = boxToGlobalSize(eX * 1 + eY * (end - start), oldscale); \
810 draw_shift = boxToGlobal(eY * start, oldshift, oldscale);
812 for (j = 0; j < category_draw_count; ++j) {
813 // Matches exactly the headings with increased height.
814 if (i == category_item[j])
818 if (j < category_draw_count)
820 entity catent = RetrieveCategoryEnt(category_name[j]);
824 (me.categoriesHeight - 1) / (me.categoriesHeight + 1),
825 me.categoriesHeight / (me.categoriesHeight + 1)
828 eY * me.realUpperMargin
831 eX * (me.columnNameOrigin + (me.columnNameSize - draw_TextWidth(catent.cat_string, 0, me.realFontSize)) * 0.5),
834 eX * (me.columnNameOrigin),
835 strcat(catent.cat_string, ":"),
838 SKINCOLOR_SERVERLIST_CATEGORY,
839 SKINALPHA_SERVERLIST_CATEGORY,
842 SET_YRANGE(me.categoriesHeight / (me.categoriesHeight + 1), 1);
846 if(isSelected && !me.lockedSelectedItem)
847 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
850 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
851 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
854 s = gethostcachestring(SLIST_FIELD_QCSTATUS, i);
855 m = tokenizebyseparator(s, ":");
860 versionstr = argv(1);
864 for(j = 2; j < m; ++j)
868 k = substring(argv(j), 0, 1);
869 v = substring(argv(j), 1, -1);
872 pure = (stof(v) == 0);
873 pure_available = true;
883 #ifdef COMPAT_NO_MOD_IS_XONOTIC
888 string original_modname = modname;
889 modname = strtolower(modname);
892 SLIST_FIELD_MOD = gethostcacheindexforkey("mod");
893 s = gethostcachestring(SLIST_FIELD_MOD, i);
895 if(modname == "xonotic")
899 // list the mods here on which the pure server check actually works
900 if(modname != "xonotic")
901 if(modname != "instagib" || modname != "minstagib")
904 if(modname != "newtoys")
905 pure_available = false;
907 if(gethostcachenumber(SLIST_FIELD_FREESLOTS, i) <= 0)
908 theAlpha = SKINALPHA_SERVERLIST_FULL;
909 else if(freeslots == 0)
910 theAlpha = SKINALPHA_SERVERLIST_FULL; // g_maxplayers support
911 else if (!gethostcachenumber(SLIST_FIELD_NUMHUMANS, i))
912 theAlpha = SKINALPHA_SERVERLIST_EMPTY;
916 p = gethostcachenumber(SLIST_FIELD_PING, i);
917 const int PING_LOW = 75;
918 const int PING_MED = 200;
919 const int PING_HIGH = 500;
921 theColor = SKINCOLOR_SERVERLIST_LOWPING + (SKINCOLOR_SERVERLIST_MEDPING - SKINCOLOR_SERVERLIST_LOWPING) * (p / PING_LOW);
922 else if(p < PING_MED)
923 theColor = SKINCOLOR_SERVERLIST_MEDPING + (SKINCOLOR_SERVERLIST_HIGHPING - SKINCOLOR_SERVERLIST_MEDPING) * ((p - PING_LOW) / (PING_MED - PING_LOW));
924 else if(p < PING_HIGH)
926 theColor = SKINCOLOR_SERVERLIST_HIGHPING;
927 theAlpha *= 1 + (SKINALPHA_SERVERLIST_HIGHPING - 1) * ((p - 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);
944 if(substring(s, 0, 1) == "[")
949 else if(IS_DIGIT(substring(s, 0, 1)))
955 q = stof(substring(crypto_getencryptlevel(s), 0, 1));
956 if((q <= 0 && cvar("crypto_aeslevel") >= 3) || (q >= 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;
989 iconSize_x = me.realFontSize.x * me.iconsSizeFactor;
991 vector iconPos = '0 0 0';
992 iconPos_x = (me.columnIconsSize - 3 * iconSize.x) * 0.5;
993 iconPos_y = (1 - iconSize.y) * 0.5;
996 if(me.seenIPv4 && me.seenIPv6)
999 draw_Picture(iconPos, "icon_ipv6", iconSize, '1 1 1', 1);
1001 draw_Picture(iconPos, "icon_ipv4", iconSize, '1 1 1', 1);
1004 iconPos.x += iconSize.x;
1008 draw_Picture(iconPos, strcat("icon_aeslevel", ftos(q)), iconSize, '1 1 1', 1);
1010 iconPos.x += iconSize.x;
1013 if(modname == "xonotic")
1015 // Here, pure_available should always be set. If not, consider
1017 if(pure_available && pure)
1018 draw_Picture(iconPos, "icon_pure1", iconSize, '1 1 1', 1);
1022 string icon = strcat("icon_mod_", modname);
1023 if(draw_PictureSize(icon) == '0 0 0')
1026 // In mods, pure_available not being present indicates
1027 // non-support of the feature. Just show the mod icon as is
1029 if(pure_available && !pure)
1030 draw_Picture(iconPos, icon, iconSize, '1 1 1', SKINALPHA_SERVERLIST_ICON_NONPURE);
1032 draw_Picture(iconPos, icon, iconSize, '1 1 1', 1);
1035 iconPos.x += iconSize.x;
1038 if(sflags >= 0 && (sflags & SERVERFLAG_PLAYERSTATS))
1039 draw_Picture(iconPos, "icon_stats1", iconSize, '1 1 1', 1);
1041 if(isFocused && me.mouseOverIcons && !me.tooltip)
1044 if(me.seenIPv4 && me.seenIPv6)
1045 t = strcat(t, (isv6) ? "IPv6, " : "IPv4, ");
1046 t = strcat(t, _("encryption:"), " ", (q ? sprintf(_("AES level %d"), q) : ZCTX(_("ENC^none"))), ", ");
1047 t = strcat(t, sprintf(_("mod: %s"), ((modname == "xonotic") ? ZCTX(_("MOD^Default")) : original_modname)));
1049 t = strcat(t, sprintf(" (%s)", (pure) ? _("official settings") : _("modified settings")));
1050 t = strcat(t, ", ");
1051 t = strcat(t, ((sflags >= 0 && (sflags & SERVERFLAG_PLAYERSTATS)) ? _("stats enabled") : _("stats disabled")));
1052 setZonedTooltip(me, t, string_null);
1060 draw_Text(me.realUpperMargin * eY + (me.columnPingOrigin + me.columnPingSize - draw_TextWidth(s, 0, me.realFontSize)) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1063 s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_NAME, i), me.columnNameSize, 0, me.realFontSize);
1064 draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, theColor, theAlpha, 0);
1067 s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_MAP, i), me.columnMapSize, 0, me.realFontSize);
1068 draw_Text(me.realUpperMargin * eY + (me.columnMapOrigin + (me.columnMapSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1071 s = draw_TextShortenToWidth(typestr, me.columnTypeSize, 0, me.realFontSize);
1072 draw_Text(me.realUpperMargin * eY + (me.columnTypeOrigin + (me.columnTypeSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1074 // server playercount
1075 s = strcat(ftos(gethostcachenumber(SLIST_FIELD_NUMHUMANS, i)), "/", ftos(gethostcachenumber(SLIST_FIELD_MAXPLAYERS, i)));
1076 draw_Text(me.realUpperMargin * eY + (me.columnPlayersOrigin + (me.columnPlayersSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1079 void XonoticServerList_focusedItemChangeNotify(entity me)
1084 float XonoticServerList_mouseMove(entity me, vector pos)
1086 if(!SUPER(XonoticServerList).mouseMove(me, pos))
1088 me.mouseOverIcons = false;
1093 me.mouseOverIcons = (pos_x <= me.columnIconsSize);
1094 if(!me.mouseOverIcons)
1099 bool XonoticServerList_keyDown(entity me, int scan, bool ascii, bool shift)
1103 org = boxToGlobal(eY * (me.selectedItem * me.itemHeight - me.scrollPos), me.origin, me.size);
1104 sz = boxToGlobalSize(eY * me.itemHeight + eX * (1 - me.controlWidth), me.size);
1106 if(scan == K_ENTER || scan == K_KP_ENTER)
1108 ServerList_Connect_Click(NULL, me);
1111 else if(scan == K_MOUSE2 || scan == K_SPACE)
1115 m_play_click_sound(MENU_SOUND_OPEN);
1116 main.serverInfoDialog.loadServerInfo(main.serverInfoDialog, me.selectedItem);
1117 DialogOpenButton_Click_withCoords(me, main.serverInfoDialog, org, sz);
1122 else if(scan == K_INS || scan == K_MOUSE3 || scan == K_KP_INS)
1126 me.toggleFavorite(me, me.selectedServer);
1127 me.ipAddressBoxFocused = -1;
1132 else if(SUPER(XonoticServerList).keyDown(me, scan, ascii, shift))
1134 else if(!me.controlledTextbox)
1137 return me.controlledTextbox.keyDown(me.controlledTextbox, scan, ascii, shift);
1140 float XonoticServerList_getTotalHeight(entity me)
1142 float num_normal_rows = me.nItems;
1143 int num_headers = category_draw_count;
1144 return me.itemHeight * (num_normal_rows + me.categoriesHeight * num_headers);
1146 int XonoticServerList_getItemAtPos(entity me, float pos)
1148 pos = pos / me.itemHeight;
1150 for (i = category_draw_count - 1; i >= 0; --i) {
1151 int itemidx = category_item[i];
1152 float itempos = i * me.categoriesHeight + category_item[i];
1153 if (pos >= itempos + me.categoriesHeight + 1)
1154 return itemidx + 1 + floor(pos - (itempos + me.categoriesHeight + 1));
1158 // No category matches? Note that category 0 is... 0. Therefore no headings exist at all.
1161 float XonoticServerList_getItemStart(entity me, int item)
1164 for (i = category_draw_count - 1; i >= 0; --i) {
1165 int itemidx = category_item[i];
1166 float itempos = i * me.categoriesHeight + category_item[i];
1167 if (item >= itemidx + 1)
1168 return (itempos + me.categoriesHeight + 1 + item - (itemidx + 1)) * me.itemHeight;
1169 if (item >= itemidx)
1170 return itempos * me.itemHeight;
1172 // No category matches? Note that category 0 is... 0. Therefore no headings exist at all.
1173 return item * me.itemHeight;
1175 float XonoticServerList_getItemHeight(entity me, int item)
1178 for (i = 0; i < category_draw_count; ++i) {
1179 // Matches exactly the headings with increased height.
1180 if (item == category_item[i])
1181 return me.itemHeight * (me.categoriesHeight + 1);
1183 return me.itemHeight;