1 (* Copyright (C) Doom 2D: Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 {$INCLUDE ../shared/a_modes.inc}
40 Players
, MaxPlayers
, LocalPl
, Bots
: Byte;
44 PingAddr
: ENetAddress
;
46 pTNetServer
= ^TNetServer
;
47 TNetServerRow
= record
48 Indices
: Array of Integer;
52 TNetServerList
= array of TNetServer
;
53 pTNetServerList
= ^TNetServerList
;
54 TNetServerTable
= array of TNetServerRow
;
57 NetMHost
: pENetHost
= nil;
58 NetMPeer
: pENetPeer
= nil;
60 slCurrent
: TNetServerList
= nil;
61 slTable
: TNetServerTable
= nil;
62 slWaitStr
: string = '';
63 slReturnPressed
: Boolean = True;
66 slUrgent
: string = '';
68 procedure g_Net_Slist_Set(IP
: string; Port
: Word);
69 function g_Net_Slist_Fetch(var SL
: TNetServerList
): Boolean;
70 procedure g_Net_Slist_Update();
71 procedure g_Net_Slist_Remove();
72 function g_Net_Slist_Connect(blocking
: Boolean=True): Boolean;
73 procedure g_Net_Slist_Check();
74 procedure g_Net_Slist_Disconnect();
75 procedure g_Net_Slist_WriteInfo();
77 procedure g_Serverlist_GenerateTable(SL
: TNetServerList
; var ST
: TNetServerTable
);
78 procedure g_Serverlist_Draw(var SL
: TNetServerList
; var ST
: TNetServerTable
);
79 procedure g_Serverlist_Control(var SL
: TNetServerList
; var ST
: TNetServerTable
);
81 function GetTimerMS(): Int64;
87 SysUtils
, e_msg
, e_input
, e_graphics
, e_log
, g_window
, g_net
, g_console
,
88 g_map
, g_game
, g_sound
, g_gui
, g_menu
, g_options
, g_language
, g_basic
,
93 slSelection
: Byte = 0;
94 slFetched
: Boolean = False;
95 slDirPressed
: Boolean = False;
96 slReadUrgent
: Boolean = False;
97 // inside the game, calling `g_Net_Slist_Connect()` is disasterous, as it is blocking.
98 // so we'll use this variable to indicate if "connected" event is received.
99 NetHostConnected
: Boolean = false;
100 NetHostConReqTime
: Int64 = 0; // to timeout `connect`
102 function GetTimerMS(): Int64;
104 Result
:= GetTimer() {div 1000};
107 procedure PingServer(var S
: TNetServer
; Sock
: ENetSocket
);
110 Ping
: array [0..9] of Byte;
113 ClTime
:= GetTimerMS();
115 Buf
.data
:= Addr(Ping
[0]);
116 Buf
.dataLength
:= 2+8;
120 Int64(Addr(Ping
[2])^) := ClTime
;
122 enet_socket_send(Sock
, Addr(S
.PingAddr
), @Buf
, 1);
125 procedure PingBcast(Sock
: ENetSocket
);
129 S
.IP
:= '255.255.255.255';
130 S
.Port
:= NET_PING_PORT
;
131 enet_address_set_host(Addr(S
.PingAddr
), PChar(Addr(S
.IP
[1])));
133 S
.PingAddr
.port
:= S
.Port
;
137 function g_Net_Slist_Fetch(var SL
: TNetServerList
): Boolean;
151 procedure ProcessLocal();
154 SetLength(SL
, I
+ 1);
157 IP
:= DecodeIPV4(SvAddr
.host
);
158 Port
:= InMsg
.ReadWord();
159 Ping
:= InMsg
.ReadInt64();
160 Ping
:= GetTimerMS() - Ping
;
161 Name
:= InMsg
.ReadString();
162 Map
:= InMsg
.ReadString();
163 GameMode
:= InMsg
.ReadByte();
164 Players
:= InMsg
.ReadByte();
165 MaxPlayers
:= InMsg
.ReadByte();
166 Protocol
:= InMsg
.ReadByte();
167 Password
:= InMsg
.ReadByte() = 1;
168 LocalPl
:= InMsg
.ReadByte();
169 Bots
:= InMsg
.ReadWord();
172 procedure CheckLocalServers();
176 Sock
:= enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM
);
177 if Sock
= ENET_SOCKET_NULL
then Exit
;
178 enet_socket_set_option(Sock
, ENET_SOCKOPT_NONBLOCK
, 1);
179 enet_socket_set_option(Sock
, ENET_SOCKOPT_BROADCAST
, 1);
184 InMsg
.Alloc(NET_BUFSIZE
);
185 Buf
.data
:= InMsg
.Data
;
186 Buf
.dataLength
:= InMsg
.MaxSize
;
187 while GetTimerMS() - T
<= 500 do
191 RX
:= enet_socket_receive(Sock
, @SvAddr
, @Buf
, 1);
192 if RX
<= 0 then continue
;
195 InMsg
.BeginReading();
197 if InMsg
.ReadChar() <> 'D' then continue
;
198 if InMsg
.ReadChar() <> 'F' then continue
;
204 enet_socket_destroy(Sock
);
206 if Length(SL
) = 0 then SL
:= nil;
212 if (NetMHost
<> nil) or (NetMPeer
<> nil) then
218 if not g_Net_Slist_Connect
then
224 e_WriteLog('Fetching serverlist...', TMsgType
.Notify
);
225 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_FETCH
]);
228 NetOut
.Write(Byte(NET_MMSG_GET
));
230 // TODO: what should we identify the build with?
231 MyVer
:= GAME_VERSION
;
234 P
:= enet_packet_create(NetOut
.Data
, NetOut
.CurSize
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
235 enet_peer_send(NetMPeer
, NET_MCHAN_MAIN
, P
);
236 enet_host_flush(NetMHost
);
238 while enet_host_service(NetMHost
, @NetMEvent
, 5000) > 0 do
240 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
242 if not InMsg
.Init(NetMEvent
.packet
^.data
, NetMEvent
.packet
^.dataLength
, True) then continue
;
244 MID
:= InMsg
.ReadByte();
246 if MID
<> NET_MMSG_GET
then continue
;
248 Cnt
:= InMsg
.ReadByte();
249 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_SLIST_RETRIEVED
], [Cnt
]), True);
255 for I
:= 0 to Cnt
- 1 do
258 SL
[I
].IP
:= InMsg
.ReadString();
259 SL
[I
].Port
:= InMsg
.ReadWord();
260 SL
[I
].Name
:= InMsg
.ReadString();
261 SL
[I
].Map
:= InMsg
.ReadString();
262 SL
[I
].GameMode
:= InMsg
.ReadByte();
263 SL
[I
].Players
:= InMsg
.ReadByte();
264 SL
[I
].MaxPlayers
:= InMsg
.ReadByte();
265 SL
[I
].Protocol
:= InMsg
.ReadByte();
266 SL
[I
].Password
:= InMsg
.ReadByte() = 1;
267 enet_address_set_host(Addr(SL
[I
].PingAddr
), PChar(Addr(SL
[I
].IP
[1])));
269 SL
[I
].PingAddr
.port
:= NET_PING_PORT
;
273 if InMsg
.ReadCount
< InMsg
.CurSize
then
275 // new master, supports version reports
276 Str
:= InMsg
.ReadString();
277 if (Str
<> MyVer
) then
280 g_Console_Add('!!! UpdVer = `' + Str
+ '`');
282 // even newer master, supports extra info
283 if InMsg
.ReadCount
< InMsg
.CurSize
then
285 slMOTD
:= b_Text_Format(InMsg
.ReadString());
286 Str
:= b_Text_Format(InMsg
.ReadString());
287 // check if the message has updated and the user has to read it again
288 if slUrgent
<> Str
then slReadUrgent
:= False;
298 g_Net_Slist_Disconnect
;
301 if Length(SL
) = 0 then
307 Sock
:= enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM
);
308 if Sock
= ENET_SOCKET_NULL
then Exit
;
309 enet_socket_set_option(Sock
, ENET_SOCKOPT_NONBLOCK
, 1);
311 for I
:= Low(SL
) to High(SL
) do
312 PingServer(SL
[I
], Sock
);
314 enet_socket_set_option(Sock
, ENET_SOCKOPT_BROADCAST
, 1);
319 InMsg
.Alloc(NET_BUFSIZE
);
320 Buf
.data
:= InMsg
.Data
;
321 Buf
.dataLength
:= InMsg
.MaxSize
;
323 while GetTimerMS() - T
<= 500 do
327 RX
:= enet_socket_receive(Sock
, @SvAddr
, @Buf
, 1);
328 if RX
<= 0 then continue
;
331 InMsg
.BeginReading();
333 if InMsg
.ReadChar() <> 'D' then continue
;
334 if InMsg
.ReadChar() <> 'F' then continue
;
337 for I
:= Low(SL
) to High(SL
) do
338 if (SL
[I
].PingAddr
.host
= SvAddr
.host
) and
339 (SL
[I
].PingAddr
.port
= SvAddr
.port
) then
343 Port
:= InMsg
.ReadWord();
344 Ping
:= InMsg
.ReadInt64();
345 Ping
:= GetTimerMS() - Ping
;
346 Name
:= InMsg
.ReadString();
347 Map
:= InMsg
.ReadString();
348 GameMode
:= InMsg
.ReadByte();
349 Players
:= InMsg
.ReadByte();
350 MaxPlayers
:= InMsg
.ReadByte();
351 Protocol
:= InMsg
.ReadByte();
352 Password
:= InMsg
.ReadByte() = 1;
353 LocalPl
:= InMsg
.ReadByte();
354 Bots
:= InMsg
.ReadWord();
365 enet_socket_destroy(Sock
);
368 procedure g_Net_Slist_WriteInfo();
373 Wad
:= g_ExtractWadNameNoPath(gMapInfo
.Map
);
374 Map
:= g_ExtractFileName(gMapInfo
.Map
);
376 NetOut
.Write(NetServerName
);
378 NetOut
.Write(Wad
+ ':\' + Map
);
379 NetOut
.Write(gGameSettings
.GameMode
);
381 Cli
:= NetClientCount
;
384 NetOut
.Write(NetMaxClients
);
386 NetOut
.Write(Byte(NET_PROTOCOL_VER
));
387 NetOut
.Write(Byte(NetPassword
<> ''));
390 procedure g_Net_Slist_Update
;
395 if (NetMHost
= nil) or (NetMPeer
= nil) then exit
;
397 // we are waiting for connection
398 if (not NetHostConnected
) then
400 // check for connection packet
401 if (enet_host_service(NetMHost
, @NetMEvent
, 0) > 0) then
403 if NetMEvent
.kind
= ENET_EVENT_TYPE_CONNECT
then
405 NetHostConnected
:= True;
406 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_CONN
]);
410 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then enet_packet_destroy(NetMEvent
.packet
);
413 // check for connection timeout
414 if (not NetHostConnected
) then
417 if (ct
-NetHostConReqTime
>= 3000) then
419 // do not spam with error messages, it looks like the master is down
420 //g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_SLIST_ERROR], True);
421 g_Net_Slist_Disconnect();
428 NetOut
.Write(Byte(NET_MMSG_UPD
));
429 NetOut
.Write(NetAddr
.port
);
431 g_Net_Slist_WriteInfo();
433 P
:= enet_packet_create(NetOut
.Data
, NetOut
.CurSize
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
434 enet_peer_send(NetMPeer
, NET_MCHAN_UPD
, P
);
436 enet_host_flush(NetMHost
);
440 procedure g_Net_Slist_Remove
;
444 if (NetMHost
= nil) or (NetMPeer
= nil) or (not NetHostConnected
) then Exit
;
446 NetOut
.Write(Byte(NET_MMSG_DEL
));
447 NetOut
.Write(NetAddr
.port
);
449 P
:= enet_packet_create(NetOut
.Data
, NetOut
.CurSize
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
450 enet_peer_send(NetMPeer
, NET_MCHAN_MAIN
, P
);
452 enet_host_flush(NetMHost
);
456 function g_Net_Slist_Connect (blocking
: Boolean=True): Boolean;
461 NetHostConnected
:= False; // just in case
462 NetHostConReqTime
:= 0; // just in case
464 NetMHost
:= enet_host_create(nil, 1, NET_MCHANS
, 0, 0);
465 if (NetMHost
= nil) then
467 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
471 NetMPeer
:= enet_host_connect(NetMHost
, @NetSlistAddr
, NET_MCHANS
, 0);
472 if (NetMPeer
= nil) then
474 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
475 enet_host_destroy(NetMHost
);
480 if (blocking
) then delay
:= 3000 else delay
:= 0;
481 if (enet_host_service(NetMHost
, @NetMEvent
, delay
) > 0) then
482 if NetMEvent
.kind
= ENET_EVENT_TYPE_CONNECT
then
485 NetHostConnected
:= True;
486 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_CONN
]);
490 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
491 enet_packet_destroy(NetMEvent
.packet
);
495 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_SLIST_ERROR
], True);
497 if NetMPeer
<> nil then enet_peer_reset(NetMPeer
);
498 if NetMHost
<> nil then enet_host_destroy(NetMHost
);
501 NetHostConnected
:= False;
502 NetHostConReqTime
:= 0;
506 NetHostConReqTime
:= GetTimerMS();
507 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_WCONN
]);
511 procedure g_Net_Slist_Disconnect
;
513 if (NetMHost
= nil) and (NetMPeer
= nil) then Exit
;
515 if NetMode
= NET_SERVER
then g_Net_Slist_Remove
;
517 enet_peer_disconnect(NetMPeer
, 0);
518 enet_host_flush(NetMHost
);
520 enet_peer_reset(NetMPeer
);
521 enet_host_destroy(NetMHost
);
525 NetHostConnected
:= False;
526 NetHostConReqTime
:= 0;
528 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_DISC
]);
531 procedure g_Net_Slist_Check
;
533 if (NetMHost
= nil) or (NetMPeer
= nil) or (not NetHostConnected
) then Exit
;
535 while (enet_host_service(NetMHost
, @NetMEvent
, 0) > 0) do
537 if NetMEvent
.kind
= ENET_EVENT_TYPE_DISCONNECT
then
539 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_LOST
], True);
540 if NetMPeer
<> nil then enet_peer_reset(NetMPeer
);
541 if NetMHost
<> nil then enet_host_destroy(NetMHost
);
544 NetHostConnected
:= False;
545 NetHostConReqTime
:= 0;
549 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
550 enet_packet_destroy(NetMEvent
.packet
);
554 procedure g_Net_Slist_Set(IP
: string; Port
: Word);
558 enet_address_set_host(@NetSlistAddr
, PChar(Addr(IP
[1])));
559 NetSlistAddr
.Port
:= Port
;
560 e_WriteLog('Masterserver address set to ' + IP
+ ':' + IntToStr(Port
), TMsgType
.Notify
);
564 function GetServerFromTable(Index
: Integer; SL
: TNetServerList
; ST
: TNetServerTable
): TNetServer
;
567 Result
.Protocol
:= 0;
573 Result
.MaxPlayers
:= 0;
577 Result
.GameMode
:= 0;
578 Result
.Password
:= false;
579 FillChar(Result
.PingAddr
, SizeOf(ENetAddress
), 0);
582 if (Index
< 0) or (Index
>= Length(ST
)) then
584 Result
:= SL
[ST
[Index
].Indices
[ST
[Index
].Current
]];
587 procedure g_Serverlist_Draw(var SL
: TNetServerList
; var ST
: TNetServerTable
);
590 sy
, i
, y
, mw
, mx
, l
, motdh
: Integer;
600 e_CharFont_GetSize(gMenuFont
, _lc
[I_NET_SLIST
], ww
, hh
);
601 e_CharFont_Print(gMenuFont
, (gScreenWidth
div 2) - (ww
div 2), 16, _lc
[I_NET_SLIST
]);
603 e_TextureFontGetSize(gStdFont
, cw
, ch
);
605 ip
:= _lc
[I_NET_SLIST_HELP
];
606 mw
:= (Length(ip
) * cw
) div 2;
608 motdh
:= gScreenHeight
- 49 - ch
* b_Text_LineCount(slMOTD
);
610 e_DrawFillQuad(16, 64, gScreenWidth
-16, motdh
, 64, 64, 64, 110);
611 e_DrawQuad(16, 64, gScreenWidth
-16, motdh
, 255, 127, 0);
613 e_TextureFontPrintEx(gScreenWidth
div 2 - mw
, gScreenHeight
-24, ip
, gStdFont
, 225, 225, 225, 1);
618 e_DrawFillQuad(16, motdh
, gScreenWidth
-16, gScreenHeight
-44, 64, 64, 64, 110);
619 e_DrawQuad(16, motdh
, gScreenWidth
-16, gScreenHeight
-44, 255, 127, 0);
620 e_TextureFontPrintFmt(20, motdh
+ 3, slMOTD
, gStdFont
, False, True);
624 if not slReadUrgent
and (slUrgent
<> '') then
626 e_DrawFillQuad(17, 65, gScreenWidth
-17, motdh
-1, 64, 64, 64, 128);
627 e_DrawFillQuad(gScreenWidth
div 2 - 256, gScreenHeight
div 2 - 60,
628 gScreenWidth
div 2 + 256, gScreenHeight
div 2 + 60, 64, 64, 64, 128);
629 e_DrawQuad(gScreenWidth
div 2 - 256, gScreenHeight
div 2 - 60,
630 gScreenWidth
div 2 + 256, gScreenHeight
div 2 + 60, 255, 127, 0);
631 e_DrawLine(1, gScreenWidth
div 2 - 256, gScreenHeight
div 2 - 40,
632 gScreenWidth
div 2 + 256, gScreenHeight
div 2 - 40, 255, 127, 0);
633 l
:= Length(_lc
[I_NET_SLIST_URGENT
]) div 2;
634 e_TextureFontPrint(gScreenWidth
div 2 - cw
* l
, gScreenHeight
div 2 - 58,
635 _lc
[I_NET_SLIST_URGENT
], gStdFont
);
636 l
:= Length(slUrgent
) div 2;
637 e_TextureFontPrintFmt(gScreenWidth
div 2 - 253, gScreenHeight
div 2 - 38,
638 slUrgent
, gStdFont
, False, True);
639 l
:= Length(_lc
[I_NET_SLIST_URGENT_CONT
]) div 2;
640 e_TextureFontPrint(gScreenWidth
div 2 - cw
* l
, gScreenHeight
div 2 + 41,
641 _lc
[I_NET_SLIST_URGENT_CONT
], gStdFont
);
642 e_DrawLine(1, gScreenWidth
div 2 - 256, gScreenHeight
div 2 + 40,
643 gScreenWidth
div 2 + 256, gScreenHeight
div 2 + 40, 255, 127, 0);
649 l
:= Length(slWaitStr
) div 2;
650 e_DrawFillQuad(17, 65, gScreenWidth
-17, motdh
-1, 64, 64, 64, 128);
651 e_DrawQuad(gScreenWidth
div 2 - 192, gScreenHeight
div 2 - 10,
652 gScreenWidth
div 2 + 192, gScreenHeight
div 2 + 11, 255, 127, 0);
653 e_TextureFontPrint(gScreenWidth
div 2 - cw
* l
, gScreenHeight
div 2 - ch
div 2,
654 slWaitStr
, gStdFont
);
659 if (slSelection
< Length(ST
)) then
662 sy
:= y
+ 42 * I
- 4;
663 Srv
:= GetServerFromTable(I
, SL
, ST
);
664 ip
:= _lc
[I_NET_ADDRESS
] + ' ' + Srv
.IP
+ ':' + IntToStr(Srv
.Port
);
666 ip
:= ip
+ ' ' + _lc
[I_NET_SERVER_PASSWORD
] + ' ' + _lc
[I_MENU_YES
]
668 ip
:= ip
+ ' ' + _lc
[I_NET_SERVER_PASSWORD
] + ' ' + _lc
[I_MENU_NO
];
670 if Length(ST
) > 0 then
673 mw
:= (gScreenWidth
- 188);
676 e_DrawFillQuad(16 + 1, sy
, gScreenWidth
- 16 - 1, sy
+ 40, 64, 64, 64, 0);
677 e_DrawLine(1, 16 + 1, sy
, gScreenWidth
- 16 - 1, sy
, 205, 205, 205);
678 e_DrawLine(1, 16 + 1, sy
+ 41, gScreenWidth
- 16 - 1, sy
+ 41, 255, 255, 255);
680 e_DrawLine(1, 16, 85, gScreenWidth
- 16, 85, 255, 127, 0);
681 e_DrawLine(1, 16, motdh
-20, gScreenWidth
-16, motdh
-20, 255, 127, 0);
683 e_DrawLine(1, mx
- 70, 64, mx
- 70, motdh
, 255, 127, 0);
684 e_DrawLine(1, mx
, 64, mx
, motdh
-20, 255, 127, 0);
685 e_DrawLine(1, mx
+ 52, 64, mx
+ 52, motdh
-20, 255, 127, 0);
686 e_DrawLine(1, mx
+ 104, 64, mx
+ 104, motdh
-20, 255, 127, 0);
688 e_TextureFontPrintEx(18, 68, 'NAME/MAP', gStdFont
, 255, 127, 0, 1);
689 e_TextureFontPrintEx(mx
- 68, 68, 'PING', gStdFont
, 255, 127, 0, 1);
690 e_TextureFontPrintEx(mx
+ 2, 68, 'MODE', gStdFont
, 255, 127, 0, 1);
691 e_TextureFontPrintEx(mx
+ 54, 68, 'PLRS', gStdFont
, 255, 127, 0, 1);
692 e_TextureFontPrintEx(mx
+ 106, 68, 'VER', gStdFont
, 255, 127, 0, 1);
695 for I
:= 0 to High(ST
) do
697 Srv
:= GetServerFromTable(I
, SL
, ST
);
699 e_TextureFontPrintEx(18, y
, Srv
.Name
, gStdFont
, 255, 255, 255, 1);
700 e_TextureFontPrintEx(18, y
+ 16, Srv
.Map
, gStdFont
, 210, 210, 210, 1);
702 // Ping and similar count
703 if (Srv
.Ping
< 0) or (Srv
.Ping
> 999) then
704 e_TextureFontPrintEx(mx
- 68, y
, _lc
[I_NET_SLIST_NO_ACCESS
], gStdFont
, 255, 0, 0, 1)
707 e_TextureFontPrintEx(mx
- 68, y
, '<1' + _lc
[I_NET_SLIST_PING_MS
], gStdFont
, 255, 255, 255, 1)
709 e_TextureFontPrintEx(mx
- 68, y
, IntToStr(Srv
.Ping
) + _lc
[I_NET_SLIST_PING_MS
], gStdFont
, 255, 255, 255, 1);
711 if Length(ST
[I
].Indices
) > 1 then
712 e_TextureFontPrintEx(mx
- 68, y
+ 16, '< ' + IntToStr(Length(ST
[I
].Indices
)) + ' >', gStdFont
, 210, 210, 210, 1);
715 e_TextureFontPrintEx(mx
+ 2, y
, g_Game_ModeToText(Srv
.GameMode
), gStdFont
, 255, 255, 255, 1);
718 e_TextureFontPrintEx(mx
+ 54, y
, IntToStr(Srv
.Players
) + '/' + IntToStr(Srv
.MaxPlayers
), gStdFont
, 255, 255, 255, 1);
719 e_TextureFontPrintEx(mx
+ 54, y
+ 16, IntToStr(Srv
.LocalPl
) + '+' + IntToStr(Srv
.Bots
), gStdFont
, 210, 210, 210, 1);
722 e_TextureFontPrintEx(mx
+ 106, y
, IntToStr(Srv
.Protocol
), gStdFont
, 255, 255, 255, 1);
727 e_TextureFontPrintEx(20, motdh
-20+3, ip
, gStdFont
, 205, 205, 205, 1);
728 ip
:= IntToStr(Length(ST
)) + _lc
[I_NET_SLIST_SERVERS
];
729 e_TextureFontPrintEx(gScreenWidth
- 48 - (Length(ip
) + 1)*cw
,
730 motdh
-20+3, ip
, gStdFont
, 205, 205, 205, 1);
733 procedure g_Serverlist_GenerateTable(SL
: TNetServerList
; var ST
: TNetServerTable
);
737 function FindServerInTable(Name
: string): Integer;
744 for i
:= Low(ST
) to High(ST
) do
746 if Length(ST
[i
].Indices
) = 0 then
748 if SL
[ST
[i
].Indices
[0]].Name
= Name
then
755 function ComparePing(i1
, i2
: Integer): Boolean;
761 if (p1
< 0) then p1
:= 999;
762 if (p2
< 0) then p2
:= 999;
765 procedure SortIndices(var ind
: Array of Integer);
770 for I
:= High(ind
) downto Low(ind
) do
771 for J
:= Low(ind
) to High(ind
) - 1 do
772 if ComparePing(ind
[j
], ind
[j
+1]) then
779 procedure SortRows();
784 for I
:= High(ST
) downto Low(ST
) do
785 for J
:= Low(ST
) to High(ST
) - 1 do
786 if ComparePing(ST
[j
].Indices
[0], ST
[j
+1].Indices
[0]) then
797 for i
:= Low(SL
) to High(SL
) do
799 j
:= FindServerInTable(SL
[i
].Name
);
803 SetLength(ST
, j
+ 1);
805 SetLength(ST
[j
].Indices
, 1);
806 ST
[j
].Indices
[0] := i
;
810 SetLength(ST
[j
].Indices
, Length(ST
[j
].Indices
) + 1);
811 ST
[j
].Indices
[High(ST
[j
].Indices
)] := i
;
815 for i
:= Low(ST
) to High(ST
) do
816 SortIndices(ST
[i
].Indices
);
821 procedure g_Serverlist_Control(var SL
: TNetServerList
; var ST
: TNetServerTable
);
826 if gConsoleShow
or gChatShow
then
829 qm
:= g_ProcessMessages(); // this updates kbd
831 if qm
or e_KeyPressed(IK_ESCAPE
) or e_KeyPressed(VK_ESCAPE
) or
832 e_KeyPressed(JOY0_JUMP
) or e_KeyPressed(JOY1_JUMP
) or
833 e_KeyPressed(JOY2_JUMP
) or e_KeyPressed(JOY3_JUMP
) then
837 gState
:= STATE_MENU
;
838 g_GUI_ShowWindow('MainMenu');
839 g_GUI_ShowWindow('NetGameMenu');
840 g_GUI_ShowWindow('NetClientMenu');
841 g_Sound_PlayEx(WINDOW_CLOSESOUND
);
845 // if there's a message on the screen,
846 if not slReadUrgent
and (slUrgent
<> '') then
848 if e_KeyPressed(IK_RETURN
) or e_KeyPressed(IK_KPRETURN
) or e_KeyPressed(VK_FIRE
) or e_KeyPressed(VK_OPEN
) or
849 e_KeyPressed(JOY0_ATTACK
) or e_KeyPressed(JOY1_ATTACK
) or e_KeyPressed(JOY2_ATTACK
) or e_KeyPressed(JOY3_ATTACK
) then
850 slReadUrgent
:= True;
854 if e_KeyPressed(IK_SPACE
) or e_KeyPressed(VK_JUMP
) or
855 e_KeyPressed(JOY0_ACTIVATE
) or e_KeyPressed(JOY1_ACTIVATE
) or e_KeyPressed(JOY2_ACTIVATE
) or e_KeyPressed(JOY3_ACTIVATE
) then
857 if not slFetched
then
859 slWaitStr
:= _lc
[I_NET_SLIST_WAIT
];
862 g_window
.ReDrawWindow
;
864 if g_Net_Slist_Fetch(SL
) then
867 slWaitStr
:= _lc
[I_NET_SLIST_NOSERVERS
];
871 slWaitStr
:= _lc
[I_NET_SLIST_ERROR
];
874 g_Serverlist_GenerateTable(SL
, ST
);
880 if SL
= nil then Exit
;
882 if e_KeyPressed(IK_RETURN
) or e_KeyPressed(IK_KPRETURN
) or e_KeyPressed(VK_FIRE
) or e_KeyPressed(VK_OPEN
) or
883 e_KeyPressed(JOY0_ATTACK
) or e_KeyPressed(JOY1_ATTACK
) or e_KeyPressed(JOY2_ATTACK
) or e_KeyPressed(JOY3_ATTACK
) then
885 if not slReturnPressed
then
887 Srv
:= GetServerFromTable(slSelection
, SL
, ST
);
891 PromptPort
:= Srv
.Port
;
892 gState
:= STATE_MENU
;
893 g_GUI_ShowWindow('ClientPasswordMenu');
896 slReturnPressed
:= True;
900 g_Game_StartClient(Srv
.IP
, Srv
.Port
, '');
903 slReturnPressed
:= True;
908 slReturnPressed
:= False;
910 if e_KeyPressed(IK_DOWN
) or e_KeyPressed(IK_KPDOWN
) or e_KeyPressed(VK_DOWN
) or
911 e_KeyPressed(JOY0_DOWN
) or e_KeyPressed(JOY1_DOWN
) or e_KeyPressed(JOY2_DOWN
) or e_KeyPressed(JOY3_DOWN
) then
913 if not slDirPressed
then
916 if slSelection
> High(ST
) then slSelection
:= 0;
917 slDirPressed
:= True;
921 if e_KeyPressed(IK_UP
) or e_KeyPressed(IK_KPUP
) or e_KeyPressed(VK_UP
) or
922 e_KeyPressed(JOY0_UP
) or e_KeyPressed(JOY1_UP
) or e_KeyPressed(JOY2_UP
) or e_KeyPressed(JOY3_UP
) then
924 if not slDirPressed
then
926 if slSelection
= 0 then slSelection
:= Length(ST
);
929 slDirPressed
:= True;
933 if e_KeyPressed(IK_RIGHT
) or e_KeyPressed(IK_KPRIGHT
) or e_KeyPressed(VK_RIGHT
) or
934 e_KeyPressed(JOY0_RIGHT
) or e_KeyPressed(JOY1_RIGHT
) or e_KeyPressed(JOY2_RIGHT
) or e_KeyPressed(JOY3_RIGHT
) then
936 if not slDirPressed
then
938 Inc(ST
[slSelection
].Current
);
939 if ST
[slSelection
].Current
> High(ST
[slSelection
].Indices
) then ST
[slSelection
].Current
:= 0;
940 slDirPressed
:= True;
944 if e_KeyPressed(IK_LEFT
) or e_KeyPressed(IK_KPLEFT
) or e_KeyPressed(VK_LEFT
) or
945 e_KeyPressed(JOY0_LEFT
) or e_KeyPressed(JOY1_LEFT
) or e_KeyPressed(JOY2_LEFT
) or e_KeyPressed(JOY3_LEFT
) then
947 if not slDirPressed
then
949 if ST
[slSelection
].Current
= 0 then ST
[slSelection
].Current
:= Length(ST
[slSelection
].Indices
);
950 Dec(ST
[slSelection
].Current
);
952 slDirPressed
:= True;
956 if (not e_KeyPressed(IK_DOWN
)) and
957 (not e_KeyPressed(IK_UP
)) and
958 (not e_KeyPressed(IK_RIGHT
)) and
959 (not e_KeyPressed(IK_LEFT
)) and
960 (not e_KeyPressed(IK_KPDOWN
)) and
961 (not e_KeyPressed(IK_KPUP
)) and
962 (not e_KeyPressed(IK_KPRIGHT
)) and
963 (not e_KeyPressed(IK_KPLEFT
)) and
964 (not e_KeyPressed(VK_DOWN
)) and
965 (not e_KeyPressed(VK_UP
)) and
966 (not e_KeyPressed(VK_RIGHT
)) and
967 (not e_KeyPressed(VK_LEFT
)) and
968 (not e_KeyPressed(JOY0_UP
)) and (not e_KeyPressed(JOY1_UP
)) and (not e_KeyPressed(JOY2_UP
)) and (not e_KeyPressed(JOY3_UP
)) and
969 (not e_KeyPressed(JOY0_DOWN
)) and (not e_KeyPressed(JOY1_DOWN
)) and (not e_KeyPressed(JOY2_DOWN
)) and (not e_KeyPressed(JOY3_DOWN
)) and
970 (not e_KeyPressed(JOY0_LEFT
)) and (not e_KeyPressed(JOY1_LEFT
)) and (not e_KeyPressed(JOY2_LEFT
)) and (not e_KeyPressed(JOY3_LEFT
)) and
971 (not e_KeyPressed(JOY0_RIGHT
)) and (not e_KeyPressed(JOY1_RIGHT
)) and (not e_KeyPressed(JOY2_RIGHT
)) and (not e_KeyPressed(JOY3_RIGHT
))
973 slDirPressed
:= False;