1 #include "serverlist.qh"
5 #include "mainwindow.qh"
6 #include "dialog_multiplayer_join_serverinfo.qh"
7 #include <common/mapinfo.qh>
9 void SL_ProcessCategoryOverrides(.string override_field_string, .float override_field)
12 for (int i = 0; i < category_ent_count; ++i)
14 s = categories[i].override_field_string;
15 if (s != "" && s != categories[i].cat_name)
18 for (int x = 0; x < category_ent_count; ++x)
20 if(categories[x].cat_name == s)
28 strfree(categories[i].override_field_string);
29 categories[i].override_field = catnum;
32 LOG_INFOF("RegisterSLCategories(): Improper override '%s' for category '%s'!", s, categories[i].cat_name);
34 strfree(categories[i].override_field_string);
35 categories[i].override_field = 0;
39 void RegisterSLCategories()
42 #define SLIST_CATEGORY(name,enoverride,dioverride,str) \
43 SET_FIELD_COUNT(name, CATEGORY_FIRST, category_ent_count) \
44 CHECK_MAX_COUNT(name, MAX_CATEGORIES, category_ent_count, "SLIST_CATEGORY") \
45 cat = categories[name - 1] = new(slist_category); \
46 cat.cat_name = #name; \
47 cat.cat_enoverride_string = strzone(SLIST_CATEGORY_AUTOCVAR(name)); \
48 cat.cat_dioverride_string = strzone(dioverride); \
49 cat.cat_string = strzone(str);
53 SL_ProcessCategoryOverrides(cat_enoverride_string, cat_enoverride);
54 SL_ProcessCategoryOverrides(cat_dioverride_string, cat_dioverride);
57 // Supporting Functions
58 entity RetrieveCategoryEnt(int catnum)
60 if((catnum > 0) && (catnum <= category_ent_count))
62 return categories[catnum - 1];
66 error(sprintf("RetrieveCategoryEnt(%d): Improper category number!\n", catnum));
71 bool IsServerInList(string list, string srv)
77 srv = netaddress_resolve(srv, 26000);
80 p = crypto_getidfp(srv);
81 n = tokenize_console(list);
82 for(i = 0; i < n; ++i)
84 if(substring(argv(i), 0, 1) != "[" && strlen(argv(i)) == 44 && strstrofs(argv(i), ".", 0) < 0)
92 if(srv == netaddress_resolve(argv(i), 26000))
99 int CheckCategoryOverride(int cat)
101 entity catent = RetrieveCategoryEnt(cat);
104 int override = (autocvar_menu_slist_categories ? catent.cat_enoverride : catent.cat_dioverride);
105 if(override) { return override; }
110 error(sprintf("CheckCategoryOverride(%d): Improper category number!\n", cat));
115 int CheckCategoryForEntry(int entry)
117 string s, k, v, modtype = "";
118 int j, m, impure = 0, freeslots = 0, sflags = 0;
119 s = gethostcachestring(SLIST_FIELD_QCSTATUS, entry);
120 m = tokenizebyseparator(s, ":");
122 for(j = 2; j < m; ++j)
124 if(argv(j) == "") { break; }
125 k = substring(argv(j), 0, 1);
126 v = substring(argv(j), 1, -1);
129 case "P": { impure = stof(v); break; }
130 case "S": { freeslots = stof(v); break; }
131 case "F": { sflags = stof(v); break; }
132 case "M": { modtype = strtolower(v); break; }
136 if(modtype != "xonotic") { impure += autocvar_menu_slist_modimpurity; }
138 // check if this server is favorited
139 if(gethostcachenumber(SLIST_FIELD_ISFAVORITE, entry)) { return CAT_FAVORITED; }
141 // now check if it's recommended
142 if(autocvar_menu_slist_recommendations)
144 string cname = gethostcachestring(SLIST_FIELD_CNAME, entry);
146 if(IsPromoted(cname)) { return CAT_RECOMMENDED; }
149 float recommended = 0;
150 if(autocvar_menu_slist_recommendations & 1)
152 if(IsRecommended(cname)) { ++recommended; }
153 else { --recommended; }
155 if(autocvar_menu_slist_recommendations & 2)
158 ///// check for minimum free slots
159 (freeslots >= autocvar_menu_slist_recommendations_minfreeslots)
161 && // check for purity requirement
163 (autocvar_menu_slist_recommendations_purethreshold < 0)
165 (impure <= autocvar_menu_slist_recommendations_purethreshold)
168 && // check for minimum amount of humans
170 gethostcachenumber(SLIST_FIELD_NUMHUMANS, entry)
172 autocvar_menu_slist_recommendations_minhumans
175 && // check for maximum latency
177 gethostcachenumber(SLIST_FIELD_PING, entry)
179 autocvar_menu_slist_recommendations_maxping
186 if(recommended > 0) { return CAT_RECOMMENDED; }
190 // if not favorited or recommended, check modname
191 if(modtype != "xonotic")
195 // old servers which don't report their mod name are considered modified now
196 case "": { return CAT_MODIFIED; }
198 case "xpm": { return CAT_XPM; }
200 case "instagib": { return CAT_INSTAGIB; }
201 case "overkill": { return CAT_OVERKILL; }
202 //case "nix": { return CAT_NIX; }
203 //case "newtoys": { return CAT_NEWTOYS; }
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, _("Remove"));
267 e.setText(e, _("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)
320 string s, typestr, modstr;
324 m = strstrofs(s, ":", 0);
327 typestr = substring(s, 0, m);
328 s = substring(s, m + 1, strlen(s) - m - 1);
329 while(substring(s, 0, 1) == " ")
330 s = substring(s, 1, strlen(s) - 1);
335 modstr = cvar_string("menu_slist_modfilter");
337 m = SLIST_MASK_AND - 1;
338 resethostcachemasks();
340 // ping: reject negative ping (no idea why this happens in the first place, engine bug)
341 sethostcachemasknumber(++m, SLIST_FIELD_PING, 0, SLIST_TEST_GREATEREQUAL);
344 if(!me.filterShowFull)
346 sethostcachemasknumber(++m, SLIST_FIELD_FREESLOTS, 1, SLIST_TEST_GREATEREQUAL); // legacy
347 sethostcachemaskstring(++m, SLIST_FIELD_QCSTATUS, ":S0:", SLIST_TEST_NOTCONTAIN); // g_maxplayers support
351 if(!me.filterShowEmpty)
352 sethostcachemasknumber(++m, SLIST_FIELD_NUMHUMANS, 1, SLIST_TEST_GREATEREQUAL);
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 n = tokenizebyseparator(_Nex_ExtResponseSystem_BannedServers, " ");
369 for(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);
383 //listflags |= SLSF_FAVORITES;
384 listflags |= SLSF_CATEGORIES;
385 if(me.currentSortOrder < 0) { listflags |= SLSF_DESCENDING; }
386 sethostcachesort(me.currentSortField, listflags);
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)
408 bool found = false, owned;
410 if(_Nex_ExtResponseSystem_BannedServersNeedsRefresh)
414 _Nex_ExtResponseSystem_BannedServersNeedsRefresh = 0;
417 if(_Nex_ExtResponseSystem_PromotedServersNeedsRefresh)
421 _Nex_ExtResponseSystem_PromotedServersNeedsRefresh = 0;
424 if(_Nex_ExtResponseSystem_RecommendedServersNeedsRefresh)
428 _Nex_ExtResponseSystem_RecommendedServersNeedsRefresh = 0;
431 if(me.currentSortField == -1)
433 me.setSortOrder(me, SLIST_FIELD_PING, +1);
434 me.refreshServerList(me, REFRESHSERVERLIST_RESET);
436 else if(me.needsRefresh == 1)
438 me.needsRefresh = 2; // delay by one frame to make sure "slist" has been executed
440 else if(me.needsRefresh == 2)
443 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
445 else if(me.needsRefresh == 3)
448 me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
451 owned = ((me.selectedServer == me.ipAddressBox.text) && (me.ipAddressBox.text != ""));
453 for(i = 0; i < category_draw_count; ++i) { category_name[i] = -1; category_item[i] = -1; }
454 category_draw_count = 0;
456 if(autocvar_menu_slist_categories >= 0) // if less than 0, don't even draw a category heading for favorites
458 float itemcount = gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT);
459 me.nItems = itemcount;
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;
542 else { me.nItems = gethostcachevalue(SLIST_HOSTCACHEVIEWCOUNT); }
544 me.connectButton.disabled = (me.lockedSelectedItem || (me.nItems == 0 && me.ipAddressBox.text == ""));
545 //me.disconnectButton.disabled = (!(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)));
546 me.infoButton.disabled = (me.lockedSelectedItem || me.nItems == 0 || !owned);
547 me.favoriteButton.disabled = (me.lockedSelectedItem || (me.nItems == 0 && me.ipAddressBox.text == ""));
549 if(me.lockedSelectedItem)
553 if(gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem) != me.selectedServer)
555 strcpy(me.selectedServer, gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
560 else if(me.selectedServer)
562 for(i = 0; i < me.nItems; ++i)
564 if(gethostcachestring(SLIST_FIELD_CNAME, i) == me.selectedServer)
566 // don't follow the selected item with SUPER(XonoticServerList).setSelected(me, i);
577 // selected server disappeared, select the last server (scrolling to it)
578 if(me.selectedItem >= me.nItems)
579 SUPER(XonoticServerList).setSelected(me, me.nItems - 1);
580 strcpy(me.selectedServer, gethostcachestring(SLIST_FIELD_CNAME, me.selectedItem));
586 if(me.selectedServer != me.ipAddressBox.text)
588 me.ipAddressBox.setText(me.ipAddressBox, me.selectedServer);
589 me.ipAddressBox.cursorPos = strlen(me.selectedServer);
590 me.ipAddressBoxFocused = -1;
594 if(me.ipAddressBoxFocused != me.ipAddressBox.focused)
596 if(me.ipAddressBox.focused || me.ipAddressBoxFocused < 0)
597 ServerList_Update_favoriteButton(NULL, me);
598 me.ipAddressBoxFocused = me.ipAddressBox.focused;
601 SUPER(XonoticServerList).draw(me);
603 void ServerList_PingSort_Click(entity btn, entity me)
605 me.setSortOrder(me, SLIST_FIELD_PING, +1);
607 void ServerList_NameSort_Click(entity btn, entity me)
609 me.setSortOrder(me, SLIST_FIELD_NAME, -1); // why?
611 void ServerList_MapSort_Click(entity btn, entity me)
613 me.setSortOrder(me, SLIST_FIELD_MAP, -1); // why?
615 void ServerList_PlayerSort_Click(entity btn, entity me)
617 me.setSortOrder(me, SLIST_FIELD_NUMHUMANS, -1);
619 void ServerList_TypeSort_Click(entity btn, entity me)
621 string s = me.filterString;
622 int m = strstrofs(s, ":", 0);
625 s = substring(s, 0, m);
626 while(substring(s, m+1, 1) == " ") // skip spaces
632 Gametype first = NULL; FOREACH(Gametypes, !first, first = it; break);
634 FOREACH(Gametypes, s == MapInfo_Type_ToString(it), {
635 // the type was found
636 // choose the next one
638 s = MapInfo_Type_ToString(Gametypes_from(it.m_id + 1));
639 if (s == "") s = MapInfo_Type_ToString(first);
644 // choose the first one
645 s = MapInfo_Type_ToString(first);
648 if(s != "") s = strcat(s, ":");
649 s = strcat(s, substring(me.filterString, m+1, strlen(me.filterString) - m - 1));
651 me.controlledTextbox.setText(me.controlledTextbox, s);
652 me.controlledTextbox.keyDown(me.controlledTextbox, K_END, 0, 0);
653 me.controlledTextbox.keyUp(me.controlledTextbox, K_END, 0, 0);
654 //ServerList_Filter_Change(me.controlledTextbox, me);
656 void ServerList_Filter_Change(entity box, entity me)
658 strfree(me.filterString);
660 me.filterString = strzone(box.text);
661 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
663 me.ipAddressBox.setText(me.ipAddressBox, "");
664 me.ipAddressBox.cursorPos = 0;
665 me.ipAddressBoxFocused = -1;
667 void ServerList_Categories_Click(entity box, entity me)
669 box.setChecked(box, autocvar_menu_slist_categories = !autocvar_menu_slist_categories);
670 me.refreshServerList(me, REFRESHSERVERLIST_RESORT);
672 me.ipAddressBox.setText(me.ipAddressBox, "");
673 me.ipAddressBox.cursorPos = 0;
674 me.ipAddressBoxFocused = -1;
676 void ServerList_ShowEmpty_Click(entity box, entity me)
678 box.setChecked(box, me.filterShowEmpty = !me.filterShowEmpty);
679 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
681 me.ipAddressBox.setText(me.ipAddressBox, "");
682 me.ipAddressBox.cursorPos = 0;
683 me.ipAddressBoxFocused = -1;
685 void ServerList_ShowFull_Click(entity box, entity me)
687 box.setChecked(box, me.filterShowFull = !me.filterShowFull);
688 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
690 me.ipAddressBox.setText(me.ipAddressBox, "");
691 me.ipAddressBox.cursorPos = 0;
692 me.ipAddressBoxFocused = -1;
694 void XonoticServerList_setSortOrder(entity me, int fld, int direction)
696 if(me.currentSortField == fld)
697 direction = -me.currentSortOrder;
698 me.currentSortOrder = direction;
699 me.currentSortField = fld;
700 me.sortButton1.forcePressed = (fld == SLIST_FIELD_PING);
701 me.sortButton2.forcePressed = (fld == SLIST_FIELD_NAME);
702 me.sortButton3.forcePressed = (fld == SLIST_FIELD_MAP);
703 me.sortButton4.forcePressed = 0;
704 me.sortButton5.forcePressed = (fld == SLIST_FIELD_NUMHUMANS);
706 strfree(me.selectedServer);
707 me.refreshServerList(me, REFRESHSERVERLIST_REFILTER);
709 void XonoticServerList_positionSortButton(entity me, entity btn, float theOrigin, float theSize, string theTitle, void(entity, entity) theFunc)
711 vector originInLBSpace, sizeInLBSpace;
712 originInLBSpace = eY * (-me.itemHeight);
713 sizeInLBSpace = eY * me.itemHeight + eX * (1 - me.controlWidth);
715 vector originInDialogSpace, sizeInDialogSpace;
716 originInDialogSpace = boxToGlobal(originInLBSpace, me.Container_origin, me.Container_size);
717 sizeInDialogSpace = boxToGlobalSize(sizeInLBSpace, me.Container_size);
719 btn.Container_origin_x = originInDialogSpace.x + sizeInDialogSpace.x * theOrigin;
720 btn.Container_size_x = sizeInDialogSpace.x * theSize;
721 btn.setText(btn, theTitle);
722 btn.onClick = theFunc;
723 btn.onClickEntity = me;
726 void XonoticServerList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
728 SUPER(XonoticServerList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
730 me.realFontSize_y = me.fontSize / (absSize.y * me.itemHeight);
731 me.realFontSize_x = me.fontSize / (absSize.x * (1 - me.controlWidth));
732 me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
734 me.columnIconsOrigin = 0;
735 me.columnIconsSize = me.realFontSize.x * 4 * me.iconsSizeFactor;
736 me.columnPingSize = me.realFontSize.x * 3;
737 me.columnMapSize = me.realFontSize.x * 10;
738 me.columnTypeSize = me.realFontSize.x * 4;
739 me.columnPlayersSize = me.realFontSize.x * 5;
740 me.columnNameSize = 1 - me.columnPlayersSize - me.columnMapSize - me.columnPingSize - me.columnIconsSize - me.columnTypeSize - 5 * me.realFontSize.x;
741 me.columnPingOrigin = me.columnIconsOrigin + me.columnIconsSize + me.realFontSize.x;
742 me.columnNameOrigin = me.columnPingOrigin + me.columnPingSize + me.realFontSize.x;
743 me.columnMapOrigin = me.columnNameOrigin + me.columnNameSize + me.realFontSize.x;
744 me.columnTypeOrigin = me.columnMapOrigin + me.columnMapSize + me.realFontSize.x;
745 me.columnPlayersOrigin = me.columnTypeOrigin + me.columnTypeSize + me.realFontSize.x;
747 me.positionSortButton(me, me.sortButton1, me.columnPingOrigin, me.columnPingSize, _("Ping"), ServerList_PingSort_Click);
748 me.positionSortButton(me, me.sortButton2, me.columnNameOrigin, me.columnNameSize, _("Hostname"), ServerList_NameSort_Click);
749 me.positionSortButton(me, me.sortButton3, me.columnMapOrigin, me.columnMapSize, _("Map"), ServerList_MapSort_Click);
750 me.positionSortButton(me, me.sortButton4, me.columnTypeOrigin, me.columnTypeSize, _("Type"), ServerList_TypeSort_Click);
751 me.positionSortButton(me, me.sortButton5, me.columnPlayersOrigin, me.columnPlayersSize, _("Players"), ServerList_PlayerSort_Click);
753 int f = me.currentSortField;
756 me.currentSortField = -1;
757 me.setSortOrder(me, f, me.currentSortOrder); // force resetting the sort order
760 void ServerList_Connect_Click(entity btn, entity me)
762 if (me.lockedSelectedItem)
764 string sv = (me.ipAddressBox.text != "") ? me.ipAddressBox.text : me.selectedServer;
765 localcmd(sprintf("connect %s\n", sv));
767 void ServerList_Favorite_Click(entity btn, entity this)
769 string addr = this.ipAddressBox.text;
770 string ipstr = netaddress_resolve(addr, 26000);
771 if (ipstr == "") return;
772 m_play_click_sound(MENU_SOUND_SELECT);
773 this.toggleFavorite(this, addr);
774 this.ipAddressBoxFocused = -1;
776 void ServerList_Info_Click(entity btn, entity me)
779 main.serverInfoDialog.loadServerInfo(main.serverInfoDialog, me.selectedItem);
781 vector org = boxToGlobal(eY * (me.selectedItem * me.itemHeight - me.scrollPos), me.origin, me.size);
782 vector sz = boxToGlobalSize(eY * me.itemHeight + eX * (1 - me.controlWidth), me.size);
783 DialogOpenButton_Click_withCoords(me, main.serverInfoDialog, org, sz);
785 void XonoticServerList_doubleClickListBoxItem(entity me, int i, vector where)
787 ServerList_Connect_Click(NULL, me);
789 void XonoticServerList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
791 // layout: Ping, Server name, Map name, NP, TP, MP
797 bool pure = false, pure_available = false;
798 int freeslots = -1, sflags = -1, j, m;
799 string s, typestr, versionstr, k, v, modname;
801 //printf("time: %f, i: %d, item: %d, nitems: %d\n", time, i, item, me.nItems);
803 vector oldscale = draw_scale;
804 vector oldshift = draw_shift;
805 #define SET_YRANGE(start,end) \
806 draw_scale = boxToGlobalSize(eX + eY * (end - start), oldscale); \
807 draw_shift = boxToGlobal(eY * start, oldshift, oldscale);
809 for (j = 0; j < category_draw_count; ++j) {
810 // Matches exactly the headings with increased height.
811 if (i == category_item[j])
815 if (j < category_draw_count)
817 entity catent = RetrieveCategoryEnt(category_name[j]);
821 (me.categoriesHeight - 1) / (me.categoriesHeight + 1),
822 me.categoriesHeight / (me.categoriesHeight + 1)
825 eY * me.realUpperMargin
828 eX * (me.columnNameOrigin + (me.columnNameSize - draw_TextWidth(catent.cat_string, 0, me.realFontSize)) * 0.5),
831 eX * (me.columnNameOrigin),
832 strcat(catent.cat_string, ":"),
835 SKINCOLOR_SERVERLIST_CATEGORY,
836 SKINALPHA_SERVERLIST_CATEGORY,
839 SET_YRANGE(me.categoriesHeight / (me.categoriesHeight + 1), 1);
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 s = gethostcachestring(SLIST_FIELD_QCSTATUS, i);
852 m = tokenizebyseparator(s, ":");
857 versionstr = argv(1);
861 for(j = 2; j < m; ++j)
865 k = substring(argv(j), 0, 1);
866 v = substring(argv(j), 1, -1);
869 pure = (stof(v) == 0);
870 pure_available = true;
880 #ifdef COMPAT_NO_MOD_IS_XONOTIC
885 string original_modname = modname;
886 modname = strtolower(modname);
889 SLIST_FIELD_MOD = gethostcacheindexforkey("mod");
890 s = gethostcachestring(SLIST_FIELD_MOD, i);
892 if(modname == "xonotic")
896 // list the mods here on which the pure server check actually works
897 if(modname != "xonotic")
898 if(modname != "instagib" || modname != "minstagib")
901 if(modname != "newtoys")
902 pure_available = false;
904 if(gethostcachenumber(SLIST_FIELD_FREESLOTS, i) <= 0)
905 theAlpha = SKINALPHA_SERVERLIST_FULL;
906 else if(freeslots == 0)
907 theAlpha = SKINALPHA_SERVERLIST_FULL; // g_maxplayers support
908 else if (!gethostcachenumber(SLIST_FIELD_NUMHUMANS, i))
909 theAlpha = SKINALPHA_SERVERLIST_EMPTY;
913 p = gethostcachenumber(SLIST_FIELD_PING, i);
914 const int PING_LOW = 75;
915 const int PING_MED = 200;
916 const int PING_HIGH = 500;
918 theColor = SKINCOLOR_SERVERLIST_LOWPING + (SKINCOLOR_SERVERLIST_MEDPING - SKINCOLOR_SERVERLIST_LOWPING) * (p / PING_LOW);
919 else if(p < PING_MED)
920 theColor = SKINCOLOR_SERVERLIST_MEDPING + (SKINCOLOR_SERVERLIST_HIGHPING - SKINCOLOR_SERVERLIST_MEDPING) * ((p - PING_LOW) / (PING_MED - PING_LOW));
921 else if(p < PING_HIGH)
923 theColor = SKINCOLOR_SERVERLIST_HIGHPING;
924 theAlpha *= 1 + (SKINALPHA_SERVERLIST_HIGHPING - 1) * ((p - PING_MED) / (PING_HIGH - PING_MED));
929 theAlpha *= SKINALPHA_SERVERLIST_HIGHPING;
932 if(gethostcachenumber(SLIST_FIELD_ISFAVORITE, i))
934 theColor = theColor * (1 - SKINALPHA_SERVERLIST_FAVORITE) + SKINCOLOR_SERVERLIST_FAVORITE * SKINALPHA_SERVERLIST_FAVORITE;
935 theAlpha = theAlpha * (1 - SKINALPHA_SERVERLIST_FAVORITE) + SKINALPHA_SERVERLIST_FAVORITE;
938 s = gethostcachestring(SLIST_FIELD_CNAME, i);
941 if(substring(s, 0, 1) == "[")
946 else if(IS_DIGIT(substring(s, 0, 1)))
952 q = stof(substring(crypto_getencryptlevel(s), 0, 1));
953 if((q <= 0 && cvar("crypto_aeslevel") >= 3) || (q >= 3 && cvar("crypto_aeslevel") <= 0))
955 theColor = SKINCOLOR_SERVERLIST_IMPOSSIBLE;
956 theAlpha = SKINALPHA_SERVERLIST_IMPOSSIBLE;
961 if(cvar("crypto_aeslevel") >= 2)
966 if(cvar("crypto_aeslevel") >= 1)
976 // 2: AES recommended but not available
977 // 3: AES possible and will be used
978 // 4: AES recommended and will be used
984 vector iconSize = '0 0 0';
985 iconSize_y = me.realFontSize.y * me.iconsSizeFactor;
986 iconSize_x = me.realFontSize.x * me.iconsSizeFactor;
988 vector iconPos = '0 0 0';
989 iconPos_x = (me.columnIconsSize - 3 * iconSize.x) * 0.5;
990 iconPos_y = (1 - iconSize.y) * 0.5;
993 if(me.seenIPv4 && me.seenIPv6)
996 draw_Picture(iconPos, "icon_ipv6", iconSize, '1 1 1', 1);
998 draw_Picture(iconPos, "icon_ipv4", iconSize, '1 1 1', 1);
1001 iconPos.x += iconSize.x;
1005 draw_Picture(iconPos, strcat("icon_aeslevel", ftos(q)), iconSize, '1 1 1', 1);
1007 iconPos.x += iconSize.x;
1010 if(modname == "xonotic")
1012 // Here, pure_available should always be set. If not, consider
1014 if(pure_available && pure)
1015 draw_Picture(iconPos, "icon_pure1", iconSize, '1 1 1', 1);
1019 string icon = strcat("icon_mod_", modname);
1020 if(draw_PictureSize(icon) == '0 0 0')
1023 // In mods, pure_available not being present indicates
1024 // non-support of the feature. Just show the mod icon as is
1026 if(pure_available && !pure)
1027 draw_Picture(iconPos, icon, iconSize, '1 1 1', SKINALPHA_SERVERLIST_ICON_NONPURE);
1029 draw_Picture(iconPos, icon, iconSize, '1 1 1', 1);
1032 iconPos.x += iconSize.x;
1035 if(sflags >= 0 && (sflags & SERVERFLAG_PLAYERSTATS))
1036 draw_Picture(iconPos, "icon_stats1", iconSize, '1 1 1', 1);
1038 if(isFocused && me.mouseOverIcons && !me.tooltip)
1041 if(me.seenIPv4 && me.seenIPv6)
1042 t = strcat(t, (isv6) ? "IPv6, " : "IPv4, ");
1043 t = strcat(t, _("encryption:"), " ", (q ? sprintf(_("AES level %d"), q) : ZCTX(_("ENC^none"))), ", ");
1044 t = strcat(t, sprintf(_("mod: %s"), ((modname == "xonotic") ? ZCTX(_("MOD^Default")) : original_modname)));
1046 t = strcat(t, sprintf(" (%s)", (pure) ? _("official settings") : _("modified settings")));
1047 t = strcat(t, ", ");
1048 t = strcat(t, ((sflags >= 0 && (sflags & SERVERFLAG_PLAYERSTATS)) ? _("stats enabled") : _("stats disabled")));
1049 setZonedTooltip(me, t, string_null);
1057 draw_Text(me.realUpperMargin * eY + (me.columnPingOrigin + me.columnPingSize - draw_TextWidth(s, 0, me.realFontSize)) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1060 s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_NAME, i), me.columnNameSize, 0, me.realFontSize);
1061 draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, theColor, theAlpha, 0);
1064 s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_MAP, i), me.columnMapSize, 0, me.realFontSize);
1065 draw_Text(me.realUpperMargin * eY + (me.columnMapOrigin + (me.columnMapSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1068 s = draw_TextShortenToWidth(typestr, me.columnTypeSize, 0, me.realFontSize);
1069 draw_Text(me.realUpperMargin * eY + (me.columnTypeOrigin + (me.columnTypeSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1071 // server playercount
1072 s = strcat(ftos(gethostcachenumber(SLIST_FIELD_NUMHUMANS, i)), "/", ftos(gethostcachenumber(SLIST_FIELD_MAXPLAYERS, i)));
1073 draw_Text(me.realUpperMargin * eY + (me.columnPlayersOrigin + (me.columnPlayersSize - draw_TextWidth(s, 0, me.realFontSize)) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
1076 void XonoticServerList_focusedItemChangeNotify(entity me)
1081 float XonoticServerList_mouseMove(entity me, vector pos)
1083 if(!SUPER(XonoticServerList).mouseMove(me, pos))
1085 me.mouseOverIcons = false;
1090 me.mouseOverIcons = (pos_x <= me.columnIconsSize);
1091 if(!me.mouseOverIcons)
1096 bool XonoticServerList_keyDown(entity me, int scan, bool ascii, bool shift)
1098 if(scan == K_ENTER || scan == K_KP_ENTER)
1100 ServerList_Connect_Click(NULL, me);
1103 else if(scan == K_MOUSE2 || scan == K_SPACE)
1107 vector org = boxToGlobal(eY * (me.selectedItem * me.itemHeight - me.scrollPos), me.origin, me.size);
1108 vector sz = boxToGlobalSize(eY * me.itemHeight + eX * (1 - me.controlWidth), me.size);
1109 m_play_click_sound(MENU_SOUND_OPEN);
1110 main.serverInfoDialog.loadServerInfo(main.serverInfoDialog, me.selectedItem);
1111 DialogOpenButton_Click_withCoords(me, main.serverInfoDialog, org, sz);
1116 else if(scan == K_INS || scan == K_MOUSE3 || scan == K_KP_INS)
1120 me.toggleFavorite(me, me.selectedServer);
1121 me.ipAddressBoxFocused = -1;
1126 else if(SUPER(XonoticServerList).keyDown(me, scan, ascii, shift))
1128 else if(!me.controlledTextbox)
1131 return me.controlledTextbox.keyDown(me.controlledTextbox, scan, ascii, shift);
1134 float XonoticServerList_getTotalHeight(entity me)
1136 float num_normal_rows = me.nItems;
1137 int num_headers = category_draw_count;
1138 return me.itemHeight * (num_normal_rows + me.categoriesHeight * num_headers);
1140 int XonoticServerList_getItemAtPos(entity me, float pos)
1142 pos = pos / me.itemHeight;
1144 for (i = category_draw_count - 1; i >= 0; --i) {
1145 int itemidx = category_item[i];
1146 float itempos = i * me.categoriesHeight + category_item[i];
1147 if (pos >= itempos + me.categoriesHeight + 1)
1148 return itemidx + 1 + floor(pos - (itempos + me.categoriesHeight + 1));
1152 // No category matches? Note that category 0 is... 0. Therefore no headings exist at all.
1155 float XonoticServerList_getItemStart(entity me, int item)
1158 for (i = category_draw_count - 1; i >= 0; --i) {
1159 int itemidx = category_item[i];
1160 float itempos = i * me.categoriesHeight + category_item[i];
1161 if (item >= itemidx + 1)
1162 return (itempos + me.categoriesHeight + 1 + item - (itemidx + 1)) * me.itemHeight;
1163 if (item >= itemidx)
1164 return itempos * me.itemHeight;
1166 // No category matches? Note that category 0 is... 0. Therefore no headings exist at all.
1167 return item * me.itemHeight;
1169 float XonoticServerList_getItemHeight(entity me, int item)
1172 for (i = 0; i < category_draw_count; ++i) {
1173 // Matches exactly the headings with increased height.
1174 if (item == category_item[i])
1175 return me.itemHeight * (me.categoriesHeight + 1);
1177 return me.itemHeight;