forked from QW-Group/ezquake-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EX_browser.c
3333 lines (2848 loc) · 75.9 KB
/
EX_browser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2011-2015 azazello and ezQuake team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "quakedef.h"
#include "fs.h"
#include "gl_model.h"
#include "gl_local.h"
#ifndef _WIN32
#include <netinet/in.h>
#include <unistd.h>
#endif
#include "keys.h"
#include "EX_browser.h"
#include "EX_qtvlist.h"
#include "Ctrl_EditBox.h"
#include "settings.h"
#include "settings_page.h"
#include "Ctrl.h"
#include "Ctrl_Tab.h"
#include "menu.h"
#include "utils.h"
#include "menu_multiplayer.h"
#include "qsound.h"
#include "teamplay.h"
int source_unique = 0;
// searching
#define MAX_SEARCH 20
#define SEARCH_TIME 3
double searchtime = -10;
#define MWHEEL_SCROLL_STEP 4
enum {
search_none = 0,
search_server,
search_player
} searchtype = search_none;
char searchstring[MAX_SEARCH+1];
// add source
CEditBox edit1, edit2;
int adding_source = 0;
sb_source_type_t newsource_type; // 0 = file
int newsource_pos;
// add server
int adding_server = 0;
int newserver_pos;
serverbrowser_window_t Browser_window;
// must correspond to server_occupancy enum values
static const char* occupancy_colors[] = { "00f", "0f0", "f00" };
cvar_t sb_status = {"sb_status", "1"}; // Shows Server status at the bottom
// columns
cvar_t sb_showping = {"sb_showping", "1"};
cvar_t sb_showaddress = {"sb_showaddress", "0"};
cvar_t sb_showmap = {"sb_showmap", "1"};
cvar_t sb_showgamedir = {"sb_showgamedir", "0"};
cvar_t sb_showplayers = {"sb_showplayers", "1"};
cvar_t sb_showfraglimit = {"sb_showfraglimit", "0"};
cvar_t sb_showtimelimit = {"sb_showtimelimit", "0"};
cvar_t sb_pingtimeout = {"sb_pingtimeout", "1000"};
cvar_t sb_infotimeout = {"sb_infotimeout", "1000"};
cvar_t sb_pingspersec = {"sb_pingspersec", "150"}; // Pings per second
cvar_t sb_pings = {"sb_pings", "3"}; // Number of times to ping a server
cvar_t sb_inforetries = {"sb_inforetries", "3"};
cvar_t sb_infospersec = {"sb_infospersec", "100"};
cvar_t sb_proxinfopersec= {"sb_proxinfopersec", "10"};
cvar_t sb_proxretries = {"sb_proxretries", "3"};
cvar_t sb_proxtimeout = {"sb_proxtimeout", "1000"};
cvar_t sb_mastertimeout = {"sb_mastertimeout", "1000"};
cvar_t sb_masterretries = {"sb_masterretries", "3"};
cvar_t sb_nosockraw = {"sb_nosockraw", "0"}; // when enabled, forces "new ping" (udp qw query packet, multithreaded) to be used
cvar_t sb_liveupdate = {"sb_liveupdate", "2"}; // not in menu
cvar_t sb_sortservers = {"sb_sortservers", "32"}; // not in new menu
cvar_t sb_sortplayers = {"sb_sortplayers", "92"}; // not in new menu
cvar_t sb_sortsources = {"sb_sortsources", "3"}; // not in new menu
cvar_t sb_autohide = {"sb_autohide", "1"}; // not in menu
cvar_t sb_findroutes = {"sb_findroutes", "0"};
cvar_t sb_ignore_proxy = {"sb_ignore_proxy", ""};
// filters
cvar_t sb_hideempty = {"sb_hideempty", "1"};
cvar_t sb_hidenotempty = {"sb_hidenotempty", "0"};
cvar_t sb_hidefull = {"sb_hidefull", "0"};
cvar_t sb_hidedead = {"sb_hidedead", "1"};
cvar_t sb_hidehighping = {"sb_hidehighping", "0"};
cvar_t sb_pinglimit = {"sb_pinglimit", "80"};
cvar_t sb_showproxies = {"sb_showproxies", "0"};
cvar_t sb_sourcevalidity = {"sb_sourcevalidity", "30"}; // not in menu
cvar_t sb_mastercache = {"sb_mastercache", "1"}; // not in menu
cvar_t sb_autoupdate = {"sb_autoupdate", "1"}; // not in menu
cvar_t sb_listcache = {"sb_listcache", "0"};
// servers table
server_data *servers[MAX_SERVERS];
int serversn;
int serversn_passed;
// all players table
player_host ** all_players = NULL;
int all_players_n = 0;
/// when 1, in the next frame in which the list is drawn also sorting will be done
int resort_servers = 1;
int resort_sources = 1;
void Sort_Servers (void);
int testing_connection = 0;
int ping_phase = 0;
double ping_pos;
int abort_ping;
// allow background threads to fire triggers
int sb_queuedtriggers = 0;
// mouse and server list columns
static qbool mouse_in_header_row = false;
static int mouse_header_pos_y = 0;
static unsigned int mouse_hovered_column;
extern cvar_t cl_proxyaddr;
static SDL_mutex* serverlist_mutex = NULL;
sem_t serverinfo_semaphore;
typedef struct sb_column_t {
const char *name;
unsigned int width;
cvar_t *showvar;
} sb_column_t;
sb_column_t sb_columns[] = {
{ "name", COL_NAME, NULL },
{ "address", COL_IP, &sb_showaddress },
{ "png", COL_PING, &sb_showping },
{ "gamedir", COL_GAMEDIR, &sb_showgamedir },
{ "map", COL_MAP, &sb_showmap },
{ "plyrs", COL_PLAYERS, &sb_showplayers },
{ "fl", COL_FRAGLIMIT, &sb_showfraglimit },
{ "tl", COL_TIMELIMIT, &sb_showtimelimit }
};
static const unsigned int sb_columns_size = sizeof(sb_columns) / sizeof(sb_column_t);
void Serverinfo_Stop(void);
void SB_ServerList_Lock(void)
{
SDL_LockMutex(serverlist_mutex);
}
void SB_ServerList_Unlock(void)
{
SDL_UnlockMutex(serverlist_mutex);
}
static qbool SB_Is_Selected_Proxy(const server_data *s)
{
return (strstr(cl_proxyaddr.string, s->display.ip) != NULL);
}
// removes given proxy from the proxyaddr list
// if cl_proxyaddr "a:b:c" and s->ip = "b" then it changes cl_proxyaddr to "a:c"
static void SB_Proxy_Unselect(const server_data *s)
{
const char *start = strstr(cl_proxyaddr.string, s->display.ip);
const char *end;
char *buf = (char *) Q_malloc(strlen(cl_proxyaddr.string) + 1);
char *bufstart = buf;
const char *cur;
if (start == NULL) {
Q_free(buf);
return; // invalid call, proxy is not selected
}
for (cur = cl_proxyaddr.string; cur < start - 1;) {
*buf++ = *cur++;
}
*buf = '\0';
end = strchr(start, '@');
if (end != NULL) {
cur = end;
if (*bufstart == '\0') cur++; // don't copy the '@' sign
while (*cur) *buf++ = *cur++;
*buf++ = '\0';
}
Cvar_Set(&cl_proxyaddr, bufstart);
Q_free(bufstart);
}
// adds selected proxy to the end of the proxies list
static void SB_Proxy_Select(const server_data *s)
{
size_t len = strlen(s->display.ip) + strlen(cl_proxyaddr.string) + 2;
char *buf = (char *) Q_malloc(len);
*buf = '\0';
strlcat(buf, cl_proxyaddr.string, len);
if (*buf) {
strlcat(buf, "@", len);
}
strlcat(buf, s->display.ip, len);
Cvar_Set(&cl_proxyaddr, buf);
Q_free(buf);
}
static void SB_Select_QWfwd(server_data *s)
{
if (SB_Is_Selected_Proxy(s)) {
SB_Proxy_Unselect(s);
}
else {
SB_Proxy_Select(s);
}
S_LocalSound ("misc/menu2.wav");
Serverinfo_Stop();
}
static const char* SB_Source_Type_Name(sb_source_type_t type)
{
switch (type) {
case type_master: return "master";
case type_file: return "file ";
case type_url: return "url ";
case type_dummy: return "dummy ";
default:
Sys_Error("SB_Source_Type_Name(): Invalid sb_source_type_t type");
return "ERROR";
}
}
static void SB_Browser_Hide(const server_data *s)
{
if (sb_autohide.value)
{
if (sb_autohide.value > 1 || strcmp(s->display.gamedir, "qizmo")) {
key_dest = key_game;
m_state = m_none;
M_Draw();
}
}
}
static void Join_Server (server_data *s)
{
if (sb_findroutes.integer) {
Cbuf_AddText("spectator 0\n");
SB_PingTree_ConnectBestPath(&s->address);
} else {
Cbuf_AddText ("join ");
Cbuf_AddText (s->display.ip);
Cbuf_AddText ("\n");
}
SB_Browser_Hide(s);
}
static void Join_Server_Direct(server_data *s)
{
Cvar_Set(&cl_proxyaddr, "");
Cbuf_AddText ("join ");
Cbuf_AddText (s->display.ip);
Cbuf_AddText ("\n");
SB_Browser_Hide(s);
}
static void Observe_Server (server_data *s)
{
if (sb_findroutes.integer) {
Cbuf_AddText("spectator 1\n");
SB_PingTree_ConnectBestPath(&s->address);
} else {
Cbuf_AddText ("observe ");
Cbuf_AddText (s->display.ip);
Cbuf_AddText ("\n");
}
SB_Browser_Hide(s);
}
static void CopyServerToClipboard (server_data *s)
{
char buf[2048];
if (keydown[K_CTRL] || s->display.name[0] == 0)
strlcpy (buf, s->display.ip, sizeof(buf));
else
snprintf (buf, sizeof (buf), "%s (%s)",
s->display.name,
s->display.ip);
CopyToClipboard(buf);
}
static void PasteServerToConsole (server_data *s)
{
char buf[2048];
snprintf(buf, sizeof (buf), "%s (%s)",
s->display.name,
s->display.ip);
Cbuf_AddText (keydown[K_CTRL] ? "say_team " : "say ");
Cbuf_AddText (buf);
Cbuf_AddText ("\n");
}
//
// browser routines
//
server_data * Create_Server (char *ip)
{
server_data *s;
s = (server_data *) Q_malloc (sizeof(server_data));
memset (s, 0, sizeof(server_data));
s->ping = -1;
if (!NET_StringToAdr (ip, &(s->address)))
{
Q_free(s);
return NULL;
}
if (!strchr(ip, ':'))
s->address.port = htons(27500);
snprintf (s->display.ip, sizeof (s->display.ip), "%d.%d.%d.%d:%d",
s->address.ip[0], s->address.ip[1], s->address.ip[2], s->address.ip[3],
ntohs(s->address.port));
return s;
}
server_data* Clone_Server(server_data* source)
{
int i = 0;
server_data* new_server = Create_Server2(source->address);
new_server->bestping = source->bestping;
memcpy(&new_server->display, &source->display, sizeof(source->display));
new_server->keysn = source->keysn;
for (i = 0; i < new_server->keysn; ++i) {
new_server->keys[i] = Q_strdup(source->keys[i]);
new_server->values[i] = Q_strdup(source->values[i]);
}
new_server->occupancy = source->occupancy;
new_server->passed_filters = source->passed_filters;
new_server->ping = source->ping;
new_server->playersn = source->playersn;
for (i = 0; i < sizeof(source->players) / sizeof(source->players[0]); ++i) {
if (source->players[i]) {
new_server->players[i] = Q_malloc(sizeof(playerinfo));
memcpy(new_server->players[i], source->players[i], sizeof(playerinfo));
}
}
new_server->qizmo = source->qizmo;
new_server->qwfwd = source->qwfwd;
new_server->spectatorsn = source->spectatorsn;
new_server->support_teams = source->support_teams;
return new_server;
}
server_data * Create_Server2 (netadr_t n)
{
server_data *s;
s = (server_data *) Q_malloc (sizeof (server_data));
memset (s, 0, sizeof(server_data));
memcpy (&(s->address), &n, sizeof (netadr_t));
strlcpy (s->display.ip, NET_AdrToString(n), sizeof (s->display.ip));
s->ping = -1;
return s;
}
void Reset_Server (server_data *s)
{
int i;
for (i = 0; i < s->keysn; i++)
{
Q_free(s->keys[i]);
if (s->values[i]) {
// fixme: this was causing a crash so a check for not-null-ness was added
Q_free(s->values[i]);
}
}
s->keysn = 0;
for (i = 0; i < s->playersn + s->spectatorsn; i++)
Q_free(s->players[i]);
s->playersn = 0;
s->spectatorsn = 0;
s->support_teams = false;
rebuild_all_players = 1; // rebuild all-players list
}
void Delete_Server (server_data *s)
{
Reset_Server(s);
Q_free(s);
}
char confirm_text[64];
qbool confirmation;
void (*confirm_func)(void);
void SB_Confirmation (const char *text, void (*func)(void))
{
strlcpy (confirm_text, text, sizeof (confirm_text));
confirm_func = func;
confirmation = 1;
}
void SB_Confirmation_Draw (void)
{
int x, y, w, h;
#define CONFIRM_TEXT_DEFAULT "Are you sure? <y/n>"
w = 32 + 8 * max (strlen (confirm_text), strlen (CONFIRM_TEXT_DEFAULT));
h = 24;
x = (vid.width - w) / 2;
y = (vid.height - h) / 2;
x = (x / 8) * 8;
y = (y / 8) * 8;
Draw_TextBox (x - 16, y - 16, w / 8 + 1, h / 8 + 2);
UI_Print_Center (x, y, w, confirm_text, false);
UI_Print_Center (x, y+16, w, CONFIRM_TEXT_DEFAULT, true);
}
void SB_Confirmation_Key (int key)
{
switch (key)
{
case K_BACKSPACE:
case K_ESCAPE:
case 'n':
case 'N':
confirmation = 0;
break;
case 'y':
case 'Y':
confirmation = 0;
confirm_func();
}
}
/* Menu drawing */
int Servers_pos;
int Sources_pos;
int Players_pos;
int Options_pos;
server_data * show_serverinfo;
int serverinfo_pos;
int Servers_disp; // server# at the top of the list
int Sources_disp; // source# at the top of the list
int Players_disp; // player# at the top of the list
void Serverinfo_Draw (void);
void Serverinfo_Players_Draw(int x, int y, int w, int h);
void Serverinfo_Rules_Draw(int x, int y, int w, int h);
void Serverinfo_Sources_Draw(int x, int y, int w, int h);
void Serverinfo_Key (int key);
void Serverinfo_Players_Key(int key);
void Serverinfo_Rules_Key(int key);
void Serverinfo_Sources_Key(int key);
//
// serverinfo
//
int sourcesn_updated = 0;
int Sources_Compare_Func (const void * p_s1, const void * p_s2)
{
int reverse = 0;
char *sort_string = sb_sortsources.string;
const source_data *s1 = *((source_data **)p_s1);
const source_data *s2 = *((source_data **)p_s2);
if (show_serverinfo)
{
if (!(s1->last_update.wYear) && s2->last_update.wYear)
return 1;
if (!s2->last_update.wYear && s1->last_update.wYear)
return -1;
if (!s1->last_update.wYear && !s2->last_update.wYear)
return s1 - s2;
}
while (true)
{
int d; // difference
if (*sort_string == '-')
{
reverse = 1;
sort_string++;
continue;
}
switch (*sort_string++)
{
case '1':
d = s1->type - s2->type; break;
case '2':
d = funcmp(s1->name, s2->name); break;
case '3':
d = s1->unique - s2->unique; break;
default:
d = s1 - s2;
}
if (d)
return reverse ? -d : d;
}
}
void Sort_Sources (void)
{
int i;
qsort (sources+1, sourcesn-1, sizeof(sources[0]), Sources_Compare_Func);
sourcesn_updated = 1;
for (i = 1; i < sourcesn; i++)
if (sources[i]->last_update.wYear)
sourcesn_updated ++;
}
int serverinfo_players_pos;
int serverinfo_sources_pos;
int serverinfo_sources_disp;
extern int autoupdate_serverinfo; // declared in EX_browser_net.c
void Serverinfo_Stop(void)
{
show_serverinfo = NULL;
autoupdate_serverinfo = 0;
Sys_SemDestroy(&serverinfo_semaphore);
}
void Serverinfo_Start (server_data *s)
{
if (show_serverinfo)
Serverinfo_Stop();
serverinfo_players_pos = 0;
serverinfo_sources_pos = 0;
autoupdate_serverinfo = 1;
show_serverinfo = s;
Sys_SemInit(&serverinfo_semaphore, 1, 1);
// sort for eliminating ot-updated
Sort_Sources();
resort_sources = 1; // and mark for resort on next sources draw
Start_Autoupdate(s);
// testing connection
if (testing_connection)
{
char buf[256];
snprintf(buf, sizeof (buf), "%d.%d.%d.%d",
show_serverinfo->address.ip[0],
show_serverinfo->address.ip[1],
show_serverinfo->address.ip[2],
show_serverinfo->address.ip[3]);
SB_Test_Init(buf);
testing_connection = 1;
}
}
void Serverinfo_Change (server_data *s)
{
Alter_Autoupdate(s);
show_serverinfo = s;
// testing connection
if (testing_connection)
{
char buf[256];
snprintf (buf, sizeof (buf), "%d.%d.%d.%d",
show_serverinfo->address.ip[0],
show_serverinfo->address.ip[1],
show_serverinfo->address.ip[2],
show_serverinfo->address.ip[3]);
SB_Test_Change(buf);
testing_connection = 1;
}
}
// --
qbool AddUnboundServer(char *addr)
{
int i;
server_data *s;
qbool existed = false;
if (sources[0]->serversn >= MAX_UNBOUND)
return false;
s = Create_Server(addr);
if (s == NULL)
return false;
for (i=0; i < sources[0]->serversn; i++)
if (!memcmp(&s->address, &sources[0]->servers[i]->address, sizeof(netadr_t)))
{
Q_free(s);
s = sources[0]->servers[i];
existed = true;
break;
}
if (!existed) // not found
{
sources[0]->servers[sources[0]->serversn] = s;
(sources[0]->serversn) ++;
rebuild_servers_list = true;
}
// start menu
key_dest = key_menu;
Mark_Source(sources[0]);
Menu_MultiPlayer_SwitchToServersTab();
GetServerPing(s);
GetServerInfo(s);
Serverinfo_Start(s);
// M_Menu_ServerList_f();
return true;
}
void AddServer_f(void)
{
if (Cmd_Argc() != 2)
{
Com_Printf("Usage: addserver <addr>\n");
return;
}
if (!AddUnboundServer(Cmd_Argv(1)))
{
if (sources[0]->serversn >= MAX_UNBOUND)
Com_Printf("Error: maximum unbound servers number reached\n");
else
Com_Printf("Error: couldn't resolve\n");
}
}
void SB_PingsDump_f(void)
{
extern qbool useNewPing;
int i;
Com_Printf("// server ping dump\n// format: ip ping\n// protocol: %s\n", useNewPing ? "UDP" : "ICMP");
for (i = 0; i < serversn; i++) {
int ping = servers[i]->ping;
if (ping >= 0 && ping < 999) {
Com_Printf("%s %d\n", NET_AdrToString(servers[i]->address), ping);
}
}
}
//
// drawing routines
//
void Add_ColumnColored(int x, int y, int *pos, const char *t, int w, const char* color)
{
char buf[128];
if ((*pos) - w - 1 <= 5)
return;
snprintf (buf, sizeof(buf), "&c%s%s", color, t);
(*pos) -= w;
UI_Print_Center(x + (*pos)*8, y, 8*(w+5), buf, false);
(*pos)--;
}
void Add_Column2(int x, int y, int *pos, const char *t, int w, int red)
{
if ((*pos) - w - 1 <= 5)
return;
(*pos) -= w;
UI_Print_Center(x + (*pos)*8, y, 8*w, t, red);
(*pos)--;
}
void Draw_Server_Statusbar(int x, int y, int w, int h, server_data *s, int count, int total)
{
int i;
int d_gamedir, d_map;
char buf[1024], line[1024];
memset(line, '\x1E', w/8);
line[w/8] = 0;
line[w/8-1] = '\x1F';
line[0] = '\x1D';
if (total > 0)
{
snprintf(buf, sizeof (buf), "%d/%d", count+1, total);
memset(line+w/8-3-strlen(buf), ' ', strlen(buf)+1);
}
UI_Print(x, y+h-24, line, false);
if (total > 0)
UI_Print(x+w-8*(3+strlen(buf))+4, y+h-24, buf, true);
// line 1
strlcpy (line, s->display.name[1] ? s->display.name : s->display.ip, sizeof (line));
line[w/8] = 0;
UI_Print_Center(x, y+h-16, w, line, false);
// line 2
if (searchtype)
{
int i;
snprintf(line, sizeof (line), "search for: %-7s", searchstring);
line[w/8] = 0;
for (i=0; i < strlen(line); i++)
line[i] ^= 128;
UI_Print_Center(x, y+h-8, w, line, false);
}
else
{
d_gamedir = d_map = 1;
if (ValueForKey(s, "map") == NULL)
d_map = 0;
if (ValueForKey(s, "*gamedir") == NULL)
d_gamedir = 0;
line[0] = 0;
if (d_gamedir)
{
memset(buf, 0, 10);
strlcpy(buf, ValueForKey(s, "*gamedir"), sizeof(buf));
buf[8] = 0;
strlcat (line, buf, sizeof (line));
strlcat (line, "\xa0 ", sizeof (line));
}
if (d_map)
{
memset(buf, 0, 10);
strlcpy(buf, ValueForKey(s, "map"), sizeof(buf));
buf[8] = 0;
strlcat (line, buf, sizeof (line));
strlcat (line, "\xa0 ", sizeof (line));
}
//if (d_players)
{
char buf[10], *max;
max = ValueForKey(s, "maxclients");
snprintf(buf, sizeof(buf), "%d/%s", s->playersn, max==NULL ? "??" : max);
strlcat (line, buf, sizeof (line));
max = ValueForKey(s, "maxspectators");
snprintf(buf, sizeof(buf), "-%d/%s", s->spectatorsn, max==NULL ? "??" : max);
strlcat (line, buf, sizeof(line));
}
if (ValueForKey(s, "status") == NULL)
{
char *dm, *fl, *tl;
char buf[200];
dm = ValueForKey(s, "deathmatch");
fl = ValueForKey(s, "fraglimit");
tl = ValueForKey(s, "timelimit");
if (dm && strlen(line) + 7 <= w/8)
{
snprintf(buf, sizeof(buf), "\xa0 dmm%s", dm);
strlcat(line, buf, sizeof(line));
}
if (fl && strlen(line) + 8 <= w/8)
{
snprintf(buf, sizeof(buf), "\xa0 fl:%s", fl);
strlcat(line, buf, sizeof(line));
}
if (tl && strlen(line) + 7 <= w/8)
{
snprintf(buf, sizeof(buf), "\xa0 tl:%s", tl);
strlcat(line, buf, sizeof(line));
}
}
else
{
char buf[200];
snprintf(buf, sizeof(buf), "\xa0 %s", ValueForKey(s, "status"));
strlcat(line, buf, sizeof(line));
}
// draw line
line[w/8] = 0;
UI_Print_Center(x, y+h-8, w, line, false);
// and dots -- shifted by 4 pixels
for (i=0; i < strlen(line); i++)
line[i] = (line[i]=='\xa0' ? '\x85' : ' ');
UI_Print_Center(x+4, y+h-8, w, line, false);
}
}
void Add_Server_Draw(void)
{
int x, y, w, h;
w = 176;
h = 56;
x = (vid.width - w)/2;
y = (vid.height - h)/2;
x = (x/8) * 8;
y = (y/8) * 8;
Draw_TextBox (x-16, y-16, w/8+1, h/8+2);
x-=4;
y-=4;
w+=8;
h+=8;
UI_Print_Center(x, y+4, w, "Create new server", true);
UI_Print(x+4, y + 20, "Address:", newserver_pos==0);
CEditBox_Draw(&edit1, x+70, y+20, newserver_pos==0);
UI_Print_Center(x+4, y+40, w, "accept", newserver_pos==1);
if (newserver_pos == 1)
Draw_Character (x+59, y+40, 13);
UI_Print_Center(x+4, y+50, w, "cancel", newserver_pos==2);
if (newserver_pos == 2)
Draw_Character (x+59, y+50, 13);
}
void SB_Servers_OnShow (void)
{
static qbool updated = false;
if (sb_autoupdate.value && !updated) {
GetServerPingsAndInfos(true);
updated = true;
}
resort_servers = 1;
}
static const char *SB_Ping_Color(int ping)
{
double frame_duration = 1000.0/77.0;
int frames = ping / frame_duration;
switch (frames) {
case 0: return "1f0"; // 13
case 1: return "3d0"; // 26
case 2: return "790"; // 39
case 3: return "880"; // 51
case 4: return "a60"; // 65
case 5:
case 6:
case 7:
case 8:
case 9: return "d30"; // 70-116
default: return "f00"; // 116+
}
}
static unsigned int SB_Servers_Hovered_Column(int w)
{
int w_from = w, w_to = w;
int col;
for (col = sb_columns_size - 1; col > 0; col--) {
if (sb_columns[col].showvar->integer) {
w_to = w_from;
w_from -= sb_columns[col].width * LETTERWIDTH + LETTERWIDTH;
if (w_from <= mouse_header_pos_y && mouse_header_pos_y < w_to) {
return col;
}
}
}
return 0;
}
// returns the horizontal offset of the second shown column in characters
int SB_Servers_Draw_ColumnHeaders(int x, int y, int w)
{
int pos = w/8;
char line[1024];
unsigned int colidx;
qbool hovered;
mouse_hovered_column = SB_Servers_Hovered_Column(w);
memset(line, ' ', pos);
line[pos] = 0;
UI_DrawColoredAlphaBox(x, y, w, 8, RGBA_TO_COLOR(10, 10, 10, 200));
for (colidx = sb_columns_size - 1; colidx > 0; colidx--) {
sb_column_t *col = &sb_columns[colidx];
hovered = mouse_in_header_row && mouse_hovered_column == colidx;
if (col->showvar->integer) {
if (mouse_in_header_row && hovered) {
UI_DrawColoredAlphaBox(x + pos * LETTERWIDTH - col->width * LETTERWIDTH - LETTERWIDTH,
y, col->width * LETTERWIDTH + LETTERWIDTH, 8, RGBA_TO_COLOR(30, 30, 30, 200));
}
Add_Column2(x, y, &pos, col->name, col->width, !hovered);
}
}
// name is always displayed
hovered = mouse_in_header_row && mouse_hovered_column == colidx;
if (hovered) {
UI_DrawColoredAlphaBox(x, y, pos * LETTERWIDTH + LETTERWIDTH, 8, RGBA_TO_COLOR(30, 30, 30, 200));
}
UI_Print(x, y, "name", !(mouse_in_header_row && mouse_hovered_column == 0));
return pos;
}
void SB_Servers_Draw (int x, int y, int w, int h)
{
char line[1024];
int i, pos, listsize;
if (updating_sources) {
UI_Print_Center(x, y + 8, w, "Updating, please wait", false);
return;
}
if (rebuild_servers_list)
Rebuild_Servers_List();
if (searchtype != search_server || searchtime + SEARCH_TIME < cls.realtime)
searchtype = search_none;