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
);
84 SysUtils
, e_msg
, e_input
, e_graphics
, e_log
, g_window
, g_net
, g_console
,
85 g_map
, g_game
, g_sound
, g_gui
, g_menu
, g_options
, g_language
, g_basic
,
90 slSelection
: Byte = 0;
91 slFetched
: Boolean = False;
92 slDirPressed
: Boolean = False;
93 slReadUrgent
: Boolean = False;
94 // inside the game, calling `g_Net_Slist_Connect()` is disasterous, as it is blocking.
95 // so we'll use this variable to indicate if "connected" event is received.
96 NetHostConnected
: Boolean = false;
97 NetHostConReqTime
: Int64 = 0; // to timeout `connect`
99 function GetTimerMS(): Int64;
101 Result
:= GetTimer() {div 1000};
104 procedure PingServer(var S
: TNetServer
; Sock
: ENetSocket
);
107 Ping
: array [0..9] of Byte;
110 ClTime
:= GetTimerMS();
112 Buf
.data
:= Addr(Ping
[0]);
113 Buf
.dataLength
:= 2+8;
117 Int64(Addr(Ping
[2])^) := ClTime
;
119 enet_socket_send(Sock
, Addr(S
.PingAddr
), @Buf
, 1);
122 procedure PingBcast(Sock
: ENetSocket
);
126 S
.IP
:= '255.255.255.255';
127 S
.Port
:= NET_PING_PORT
;
128 enet_address_set_host(Addr(S
.PingAddr
), PChar(Addr(S
.IP
[1])));
130 S
.PingAddr
.port
:= S
.Port
;
134 function g_Net_Slist_Fetch(var SL
: TNetServerList
): Boolean;
148 procedure ProcessLocal();
151 SetLength(SL
, I
+ 1);
154 IP
:= DecodeIPV4(SvAddr
.host
);
155 Port
:= InMsg
.ReadWord();
156 Ping
:= InMsg
.ReadInt64();
157 Ping
:= GetTimerMS() - Ping
;
158 Name
:= InMsg
.ReadString();
159 Map
:= InMsg
.ReadString();
160 GameMode
:= InMsg
.ReadByte();
161 Players
:= InMsg
.ReadByte();
162 MaxPlayers
:= InMsg
.ReadByte();
163 Protocol
:= InMsg
.ReadByte();
164 Password
:= InMsg
.ReadByte() = 1;
165 LocalPl
:= InMsg
.ReadByte();
166 Bots
:= InMsg
.ReadWord();
169 procedure CheckLocalServers();
173 Sock
:= enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM
);
174 if Sock
= ENET_SOCKET_NULL
then Exit
;
175 enet_socket_set_option(Sock
, ENET_SOCKOPT_NONBLOCK
, 1);
176 enet_socket_set_option(Sock
, ENET_SOCKOPT_BROADCAST
, 1);
181 InMsg
.Alloc(NET_BUFSIZE
);
182 Buf
.data
:= InMsg
.Data
;
183 Buf
.dataLength
:= InMsg
.MaxSize
;
184 while GetTimerMS() - T
<= 500 do
188 RX
:= enet_socket_receive(Sock
, @SvAddr
, @Buf
, 1);
189 if RX
<= 0 then continue
;
192 InMsg
.BeginReading();
194 if InMsg
.ReadChar() <> 'D' then continue
;
195 if InMsg
.ReadChar() <> 'F' then continue
;
201 enet_socket_destroy(Sock
);
203 if Length(SL
) = 0 then SL
:= nil;
209 if (NetMHost
<> nil) or (NetMPeer
<> nil) then
215 if not g_Net_Slist_Connect
then
221 e_WriteLog('Fetching serverlist...', TMsgType
.Notify
);
222 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_FETCH
]);
225 NetOut
.Write(Byte(NET_MMSG_GET
));
227 // TODO: what should we identify the build with?
228 MyVer
:= GAME_VERSION
;
231 P
:= enet_packet_create(NetOut
.Data
, NetOut
.CurSize
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
232 enet_peer_send(NetMPeer
, NET_MCHAN_MAIN
, P
);
233 enet_host_flush(NetMHost
);
235 while enet_host_service(NetMHost
, @NetMEvent
, 5000) > 0 do
237 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
239 if not InMsg
.Init(NetMEvent
.packet
^.data
, NetMEvent
.packet
^.dataLength
, True) then continue
;
241 MID
:= InMsg
.ReadByte();
243 if MID
<> NET_MMSG_GET
then continue
;
245 Cnt
:= InMsg
.ReadByte();
246 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_SLIST_RETRIEVED
], [Cnt
]), True);
252 for I
:= 0 to Cnt
- 1 do
255 SL
[I
].IP
:= InMsg
.ReadString();
256 SL
[I
].Port
:= InMsg
.ReadWord();
257 SL
[I
].Name
:= InMsg
.ReadString();
258 SL
[I
].Map
:= InMsg
.ReadString();
259 SL
[I
].GameMode
:= InMsg
.ReadByte();
260 SL
[I
].Players
:= InMsg
.ReadByte();
261 SL
[I
].MaxPlayers
:= InMsg
.ReadByte();
262 SL
[I
].Protocol
:= InMsg
.ReadByte();
263 SL
[I
].Password
:= InMsg
.ReadByte() = 1;
264 enet_address_set_host(Addr(SL
[I
].PingAddr
), PChar(Addr(SL
[I
].IP
[1])));
266 SL
[I
].PingAddr
.port
:= NET_PING_PORT
;
270 if InMsg
.ReadCount
< InMsg
.CurSize
then
272 // new master, supports version reports
273 Str
:= InMsg
.ReadString();
274 if (Str
<> MyVer
) then
277 g_Console_Add('!!! UpdVer = `' + Str
+ '`');
279 // even newer master, supports extra info
280 if InMsg
.ReadCount
< InMsg
.CurSize
then
282 slMOTD
:= b_Text_Format(InMsg
.ReadString());
283 Str
:= b_Text_Format(InMsg
.ReadString());
284 // check if the message has updated and the user has to read it again
285 if slUrgent
<> Str
then slReadUrgent
:= False;
295 g_Net_Slist_Disconnect
;
298 if Length(SL
) = 0 then
304 Sock
:= enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM
);
305 if Sock
= ENET_SOCKET_NULL
then Exit
;
306 enet_socket_set_option(Sock
, ENET_SOCKOPT_NONBLOCK
, 1);
308 for I
:= Low(SL
) to High(SL
) do
309 PingServer(SL
[I
], Sock
);
311 enet_socket_set_option(Sock
, ENET_SOCKOPT_BROADCAST
, 1);
316 InMsg
.Alloc(NET_BUFSIZE
);
317 Buf
.data
:= InMsg
.Data
;
318 Buf
.dataLength
:= InMsg
.MaxSize
;
320 while GetTimerMS() - T
<= 500 do
324 RX
:= enet_socket_receive(Sock
, @SvAddr
, @Buf
, 1);
325 if RX
<= 0 then continue
;
328 InMsg
.BeginReading();
330 if InMsg
.ReadChar() <> 'D' then continue
;
331 if InMsg
.ReadChar() <> 'F' then continue
;
334 for I
:= Low(SL
) to High(SL
) do
335 if (SL
[I
].PingAddr
.host
= SvAddr
.host
) and
336 (SL
[I
].PingAddr
.port
= SvAddr
.port
) then
340 Port
:= InMsg
.ReadWord();
341 Ping
:= InMsg
.ReadInt64();
342 Ping
:= GetTimerMS() - Ping
;
343 Name
:= InMsg
.ReadString();
344 Map
:= InMsg
.ReadString();
345 GameMode
:= InMsg
.ReadByte();
346 Players
:= InMsg
.ReadByte();
347 MaxPlayers
:= InMsg
.ReadByte();
348 Protocol
:= InMsg
.ReadByte();
349 Password
:= InMsg
.ReadByte() = 1;
350 LocalPl
:= InMsg
.ReadByte();
351 Bots
:= InMsg
.ReadWord();
362 enet_socket_destroy(Sock
);
365 procedure g_Net_Slist_WriteInfo();
370 Wad
:= g_ExtractWadNameNoPath(gMapInfo
.Map
);
371 Map
:= g_ExtractFileName(gMapInfo
.Map
);
373 NetOut
.Write(NetServerName
);
375 NetOut
.Write(Wad
+ ':\' + Map
);
376 NetOut
.Write(gGameSettings
.GameMode
);
378 Cli
:= NetClientCount
;
381 NetOut
.Write(NetMaxClients
);
383 NetOut
.Write(Byte(NET_PROTOCOL_VER
));
384 NetOut
.Write(Byte(NetPassword
<> ''));
387 procedure g_Net_Slist_Update
;
392 if (NetMHost
= nil) or (NetMPeer
= nil) then exit
;
394 // we are waiting for connection
395 if (not NetHostConnected
) then
397 // check for connection packet
398 if (enet_host_service(NetMHost
, @NetMEvent
, 0) > 0) then
400 if NetMEvent
.kind
= ENET_EVENT_TYPE_CONNECT
then
402 NetHostConnected
:= True;
403 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_CONN
]);
407 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then enet_packet_destroy(NetMEvent
.packet
);
410 // check for connection timeout
411 if (not NetHostConnected
) then
414 if (ct
-NetHostConReqTime
>= 3000) then
416 // do not spam with error messages, it looks like the master is down
417 //g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_SLIST_ERROR], True);
418 g_Net_Slist_Disconnect();
425 NetOut
.Write(Byte(NET_MMSG_UPD
));
426 NetOut
.Write(NetAddr
.port
);
428 g_Net_Slist_WriteInfo();
430 P
:= enet_packet_create(NetOut
.Data
, NetOut
.CurSize
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
431 enet_peer_send(NetMPeer
, NET_MCHAN_UPD
, P
);
433 enet_host_flush(NetMHost
);
437 procedure g_Net_Slist_Remove
;
441 if (NetMHost
= nil) or (NetMPeer
= nil) or (not NetHostConnected
) then Exit
;
443 NetOut
.Write(Byte(NET_MMSG_DEL
));
444 NetOut
.Write(NetAddr
.port
);
446 P
:= enet_packet_create(NetOut
.Data
, NetOut
.CurSize
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
447 enet_peer_send(NetMPeer
, NET_MCHAN_MAIN
, P
);
449 enet_host_flush(NetMHost
);
453 function g_Net_Slist_Connect (blocking
: Boolean=True): Boolean;
458 NetHostConnected
:= False; // just in case
459 NetHostConReqTime
:= 0; // just in case
461 NetMHost
:= enet_host_create(nil, 1, NET_MCHANS
, 0, 0);
462 if (NetMHost
= nil) then
464 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
468 NetMPeer
:= enet_host_connect(NetMHost
, @NetSlistAddr
, NET_MCHANS
, 0);
469 if (NetMPeer
= nil) then
471 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
472 enet_host_destroy(NetMHost
);
477 if (blocking
) then delay
:= 3000 else delay
:= 0;
478 if (enet_host_service(NetMHost
, @NetMEvent
, delay
) > 0) then
479 if NetMEvent
.kind
= ENET_EVENT_TYPE_CONNECT
then
482 NetHostConnected
:= True;
483 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_CONN
]);
487 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
488 enet_packet_destroy(NetMEvent
.packet
);
492 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_SLIST_ERROR
], True);
494 if NetMPeer
<> nil then enet_peer_reset(NetMPeer
);
495 if NetMHost
<> nil then enet_host_destroy(NetMHost
);
498 NetHostConnected
:= False;
499 NetHostConReqTime
:= 0;
503 NetHostConReqTime
:= GetTimerMS();
504 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_WCONN
]);
508 procedure g_Net_Slist_Disconnect
;
510 if (NetMHost
= nil) and (NetMPeer
= nil) then Exit
;
512 if NetMode
= NET_SERVER
then g_Net_Slist_Remove
;
514 enet_peer_disconnect(NetMPeer
, 0);
515 enet_host_flush(NetMHost
);
517 enet_peer_reset(NetMPeer
);
518 enet_host_destroy(NetMHost
);
522 NetHostConnected
:= False;
523 NetHostConReqTime
:= 0;
525 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_DISC
]);
528 procedure g_Net_Slist_Check
;
530 if (NetMHost
= nil) or (NetMPeer
= nil) or (not NetHostConnected
) then Exit
;
532 while (enet_host_service(NetMHost
, @NetMEvent
, 0) > 0) do
534 if NetMEvent
.kind
= ENET_EVENT_TYPE_DISCONNECT
then
536 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_LOST
], True);
537 if NetMPeer
<> nil then enet_peer_reset(NetMPeer
);
538 if NetMHost
<> nil then enet_host_destroy(NetMHost
);
541 NetHostConnected
:= False;
542 NetHostConReqTime
:= 0;
546 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
547 enet_packet_destroy(NetMEvent
.packet
);
551 procedure g_Net_Slist_Set(IP
: string; Port
: Word);
555 enet_address_set_host(@NetSlistAddr
, PChar(Addr(IP
[1])));
556 NetSlistAddr
.Port
:= Port
;
557 e_WriteLog('Masterserver address set to ' + IP
+ ':' + IntToStr(Port
), TMsgType
.Notify
);
561 function GetServerFromTable(Index
: Integer; SL
: TNetServerList
; ST
: TNetServerTable
): TNetServer
;
564 Result
.Protocol
:= 0;
570 Result
.MaxPlayers
:= 0;
574 Result
.GameMode
:= 0;
575 Result
.Password
:= false;
576 FillChar(Result
.PingAddr
, SizeOf(ENetAddress
), 0);
579 if (Index
< 0) or (Index
>= Length(ST
)) then
581 Result
:= SL
[ST
[Index
].Indices
[ST
[Index
].Current
]];
584 procedure g_Serverlist_Draw(var SL
: TNetServerList
; var ST
: TNetServerTable
);
587 sy
, i
, y
, mw
, mx
, l
, motdh
: Integer;
597 e_CharFont_GetSize(gMenuFont
, _lc
[I_NET_SLIST
], ww
, hh
);
598 e_CharFont_Print(gMenuFont
, (gScreenWidth
div 2) - (ww
div 2), 16, _lc
[I_NET_SLIST
]);
600 e_TextureFontGetSize(gStdFont
, cw
, ch
);
602 ip
:= _lc
[I_NET_SLIST_HELP
];
603 mw
:= (Length(ip
) * cw
) div 2;
605 motdh
:= gScreenHeight
- 49 - ch
* b_Text_LineCount(slMOTD
);
607 e_DrawFillQuad(16, 64, gScreenWidth
-16, motdh
, 64, 64, 64, 110);
608 e_DrawQuad(16, 64, gScreenWidth
-16, motdh
, 255, 127, 0);
610 e_TextureFontPrintEx(gScreenWidth
div 2 - mw
, gScreenHeight
-24, ip
, gStdFont
, 225, 225, 225, 1);
615 e_DrawFillQuad(16, motdh
, gScreenWidth
-16, gScreenHeight
-44, 64, 64, 64, 110);
616 e_DrawQuad(16, motdh
, gScreenWidth
-16, gScreenHeight
-44, 255, 127, 0);
617 e_TextureFontPrintFmt(20, motdh
+ 3, slMOTD
, gStdFont
, False, True);
621 if not slReadUrgent
and (slUrgent
<> '') then
623 e_DrawFillQuad(17, 65, gScreenWidth
-17, motdh
-1, 64, 64, 64, 128);
624 e_DrawFillQuad(gScreenWidth
div 2 - 256, gScreenHeight
div 2 - 60,
625 gScreenWidth
div 2 + 256, gScreenHeight
div 2 + 60, 64, 64, 64, 128);
626 e_DrawQuad(gScreenWidth
div 2 - 256, gScreenHeight
div 2 - 60,
627 gScreenWidth
div 2 + 256, gScreenHeight
div 2 + 60, 255, 127, 0);
628 e_DrawLine(1, gScreenWidth
div 2 - 256, gScreenHeight
div 2 - 40,
629 gScreenWidth
div 2 + 256, gScreenHeight
div 2 - 40, 255, 127, 0);
630 l
:= Length(_lc
[I_NET_SLIST_URGENT
]) div 2;
631 e_TextureFontPrint(gScreenWidth
div 2 - cw
* l
, gScreenHeight
div 2 - 58,
632 _lc
[I_NET_SLIST_URGENT
], gStdFont
);
633 l
:= Length(slUrgent
) div 2;
634 e_TextureFontPrintFmt(gScreenWidth
div 2 - 253, gScreenHeight
div 2 - 38,
635 slUrgent
, gStdFont
, False, True);
636 l
:= Length(_lc
[I_NET_SLIST_URGENT_CONT
]) div 2;
637 e_TextureFontPrint(gScreenWidth
div 2 - cw
* l
, gScreenHeight
div 2 + 41,
638 _lc
[I_NET_SLIST_URGENT_CONT
], gStdFont
);
639 e_DrawLine(1, gScreenWidth
div 2 - 256, gScreenHeight
div 2 + 40,
640 gScreenWidth
div 2 + 256, gScreenHeight
div 2 + 40, 255, 127, 0);
646 l
:= Length(slWaitStr
) div 2;
647 e_DrawFillQuad(17, 65, gScreenWidth
-17, motdh
-1, 64, 64, 64, 128);
648 e_DrawQuad(gScreenWidth
div 2 - 192, gScreenHeight
div 2 - 10,
649 gScreenWidth
div 2 + 192, gScreenHeight
div 2 + 11, 255, 127, 0);
650 e_TextureFontPrint(gScreenWidth
div 2 - cw
* l
, gScreenHeight
div 2 - ch
div 2,
651 slWaitStr
, gStdFont
);
656 if (slSelection
< Length(ST
)) then
659 sy
:= y
+ 42 * I
- 4;
660 Srv
:= GetServerFromTable(I
, SL
, ST
);
661 ip
:= _lc
[I_NET_ADDRESS
] + ' ' + Srv
.IP
+ ':' + IntToStr(Srv
.Port
);
663 ip
:= ip
+ ' ' + _lc
[I_NET_SERVER_PASSWORD
] + ' ' + _lc
[I_MENU_YES
]
665 ip
:= ip
+ ' ' + _lc
[I_NET_SERVER_PASSWORD
] + ' ' + _lc
[I_MENU_NO
];
667 if Length(ST
) > 0 then
670 mw
:= (gScreenWidth
- 188);
673 e_DrawFillQuad(16 + 1, sy
, gScreenWidth
- 16 - 1, sy
+ 40, 64, 64, 64, 0);
674 e_DrawLine(1, 16 + 1, sy
, gScreenWidth
- 16 - 1, sy
, 205, 205, 205);
675 e_DrawLine(1, 16 + 1, sy
+ 41, gScreenWidth
- 16 - 1, sy
+ 41, 255, 255, 255);
677 e_DrawLine(1, 16, 85, gScreenWidth
- 16, 85, 255, 127, 0);
678 e_DrawLine(1, 16, motdh
-20, gScreenWidth
-16, motdh
-20, 255, 127, 0);
680 e_DrawLine(1, mx
- 70, 64, mx
- 70, motdh
, 255, 127, 0);
681 e_DrawLine(1, mx
, 64, mx
, motdh
-20, 255, 127, 0);
682 e_DrawLine(1, mx
+ 52, 64, mx
+ 52, motdh
-20, 255, 127, 0);
683 e_DrawLine(1, mx
+ 104, 64, mx
+ 104, motdh
-20, 255, 127, 0);
685 e_TextureFontPrintEx(18, 68, 'NAME/MAP', gStdFont
, 255, 127, 0, 1);
686 e_TextureFontPrintEx(mx
- 68, 68, 'PING', gStdFont
, 255, 127, 0, 1);
687 e_TextureFontPrintEx(mx
+ 2, 68, 'MODE', gStdFont
, 255, 127, 0, 1);
688 e_TextureFontPrintEx(mx
+ 54, 68, 'PLRS', gStdFont
, 255, 127, 0, 1);
689 e_TextureFontPrintEx(mx
+ 106, 68, 'VER', gStdFont
, 255, 127, 0, 1);
692 for I
:= 0 to High(ST
) do
694 Srv
:= GetServerFromTable(I
, SL
, ST
);
696 e_TextureFontPrintEx(18, y
, Srv
.Name
, gStdFont
, 255, 255, 255, 1);
697 e_TextureFontPrintEx(18, y
+ 16, Srv
.Map
, gStdFont
, 210, 210, 210, 1);
699 // Ping and similar count
700 if (Srv
.Ping
< 0) or (Srv
.Ping
> 999) then
701 e_TextureFontPrintEx(mx
- 68, y
, _lc
[I_NET_SLIST_NO_ACCESS
], gStdFont
, 255, 0, 0, 1)
704 e_TextureFontPrintEx(mx
- 68, y
, '<1' + _lc
[I_NET_SLIST_PING_MS
], gStdFont
, 255, 255, 255, 1)
706 e_TextureFontPrintEx(mx
- 68, y
, IntToStr(Srv
.Ping
) + _lc
[I_NET_SLIST_PING_MS
], gStdFont
, 255, 255, 255, 1);
708 if Length(ST
[I
].Indices
) > 1 then
709 e_TextureFontPrintEx(mx
- 68, y
+ 16, '< ' + IntToStr(Length(ST
[I
].Indices
)) + ' >', gStdFont
, 210, 210, 210, 1);
712 e_TextureFontPrintEx(mx
+ 2, y
, g_Game_ModeToText(Srv
.GameMode
), gStdFont
, 255, 255, 255, 1);
715 e_TextureFontPrintEx(mx
+ 54, y
, IntToStr(Srv
.Players
) + '/' + IntToStr(Srv
.MaxPlayers
), gStdFont
, 255, 255, 255, 1);
716 e_TextureFontPrintEx(mx
+ 54, y
+ 16, IntToStr(Srv
.LocalPl
) + '+' + IntToStr(Srv
.Bots
), gStdFont
, 210, 210, 210, 1);
719 e_TextureFontPrintEx(mx
+ 106, y
, IntToStr(Srv
.Protocol
), gStdFont
, 255, 255, 255, 1);
724 e_TextureFontPrintEx(20, motdh
-20+3, ip
, gStdFont
, 205, 205, 205, 1);
725 ip
:= IntToStr(Length(ST
)) + _lc
[I_NET_SLIST_SERVERS
];
726 e_TextureFontPrintEx(gScreenWidth
- 48 - (Length(ip
) + 1)*cw
,
727 motdh
-20+3, ip
, gStdFont
, 205, 205, 205, 1);
730 procedure g_Serverlist_GenerateTable(SL
: TNetServerList
; var ST
: TNetServerTable
);
734 function FindServerInTable(Name
: string): Integer;
741 for i
:= Low(ST
) to High(ST
) do
743 if Length(ST
[i
].Indices
) = 0 then
745 if SL
[ST
[i
].Indices
[0]].Name
= Name
then
752 function ComparePing(i1
, i2
: Integer): Boolean;
758 if (p1
< 0) then p1
:= 999;
759 if (p2
< 0) then p2
:= 999;
762 procedure SortIndices(var ind
: Array of Integer);
767 for I
:= High(ind
) downto Low(ind
) do
768 for J
:= Low(ind
) to High(ind
) - 1 do
769 if ComparePing(ind
[j
], ind
[j
+1]) then
776 procedure SortRows();
781 for I
:= High(ST
) downto Low(ST
) do
782 for J
:= Low(ST
) to High(ST
) - 1 do
783 if ComparePing(ST
[j
].Indices
[0], ST
[j
+1].Indices
[0]) then
794 for i
:= Low(SL
) to High(SL
) do
796 j
:= FindServerInTable(SL
[i
].Name
);
800 SetLength(ST
, j
+ 1);
802 SetLength(ST
[j
].Indices
, 1);
803 ST
[j
].Indices
[0] := i
;
807 SetLength(ST
[j
].Indices
, Length(ST
[j
].Indices
) + 1);
808 ST
[j
].Indices
[High(ST
[j
].Indices
)] := i
;
812 for i
:= Low(ST
) to High(ST
) do
813 SortIndices(ST
[i
].Indices
);
818 procedure g_Serverlist_Control(var SL
: TNetServerList
; var ST
: TNetServerTable
);
823 if gConsoleShow
or gChatShow
then
826 qm
:= g_ProcessMessages(); // this updates kbd
828 if qm
or e_KeyPressed(IK_ESCAPE
) or e_KeyPressed(VK_ESCAPE
) or
829 e_KeyPressed(JOY0_JUMP
) or e_KeyPressed(JOY1_JUMP
) or
830 e_KeyPressed(JOY2_JUMP
) or e_KeyPressed(JOY3_JUMP
) then
834 gState
:= STATE_MENU
;
835 g_GUI_ShowWindow('MainMenu');
836 g_GUI_ShowWindow('NetGameMenu');
837 g_GUI_ShowWindow('NetClientMenu');
838 g_Sound_PlayEx(WINDOW_CLOSESOUND
);
842 // if there's a message on the screen,
843 if not slReadUrgent
and (slUrgent
<> '') then
845 if e_KeyPressed(IK_RETURN
) or e_KeyPressed(IK_KPRETURN
) or e_KeyPressed(VK_FIRE
) or e_KeyPressed(VK_OPEN
) or
846 e_KeyPressed(JOY0_ATTACK
) or e_KeyPressed(JOY1_ATTACK
) or e_KeyPressed(JOY2_ATTACK
) or e_KeyPressed(JOY3_ATTACK
) then
847 slReadUrgent
:= True;
851 if e_KeyPressed(IK_SPACE
) or e_KeyPressed(VK_JUMP
) or
852 e_KeyPressed(JOY0_ACTIVATE
) or e_KeyPressed(JOY1_ACTIVATE
) or e_KeyPressed(JOY2_ACTIVATE
) or e_KeyPressed(JOY3_ACTIVATE
) then
854 if not slFetched
then
856 slWaitStr
:= _lc
[I_NET_SLIST_WAIT
];
859 g_window
.ReDrawWindow
;
861 if g_Net_Slist_Fetch(SL
) then
864 slWaitStr
:= _lc
[I_NET_SLIST_NOSERVERS
];
868 slWaitStr
:= _lc
[I_NET_SLIST_ERROR
];
871 g_Serverlist_GenerateTable(SL
, ST
);
877 if SL
= nil then Exit
;
879 if e_KeyPressed(IK_RETURN
) or e_KeyPressed(IK_KPRETURN
) or e_KeyPressed(VK_FIRE
) or e_KeyPressed(VK_OPEN
) or
880 e_KeyPressed(JOY0_ATTACK
) or e_KeyPressed(JOY1_ATTACK
) or e_KeyPressed(JOY2_ATTACK
) or e_KeyPressed(JOY3_ATTACK
) then
882 if not slReturnPressed
then
884 Srv
:= GetServerFromTable(slSelection
, SL
, ST
);
888 PromptPort
:= Srv
.Port
;
889 gState
:= STATE_MENU
;
890 g_GUI_ShowWindow('ClientPasswordMenu');
893 slReturnPressed
:= True;
897 g_Game_StartClient(Srv
.IP
, Srv
.Port
, '');
900 slReturnPressed
:= True;
905 slReturnPressed
:= False;
907 if e_KeyPressed(IK_DOWN
) or e_KeyPressed(IK_KPDOWN
) or e_KeyPressed(VK_DOWN
) or
908 e_KeyPressed(JOY0_DOWN
) or e_KeyPressed(JOY1_DOWN
) or e_KeyPressed(JOY2_DOWN
) or e_KeyPressed(JOY3_DOWN
) then
910 if not slDirPressed
then
913 if slSelection
> High(ST
) then slSelection
:= 0;
914 slDirPressed
:= True;
918 if e_KeyPressed(IK_UP
) or e_KeyPressed(IK_KPUP
) or e_KeyPressed(VK_UP
) or
919 e_KeyPressed(JOY0_UP
) or e_KeyPressed(JOY1_UP
) or e_KeyPressed(JOY2_UP
) or e_KeyPressed(JOY3_UP
) then
921 if not slDirPressed
then
923 if slSelection
= 0 then slSelection
:= Length(ST
);
926 slDirPressed
:= True;
930 if e_KeyPressed(IK_RIGHT
) or e_KeyPressed(IK_KPRIGHT
) or e_KeyPressed(VK_RIGHT
) or
931 e_KeyPressed(JOY0_RIGHT
) or e_KeyPressed(JOY1_RIGHT
) or e_KeyPressed(JOY2_RIGHT
) or e_KeyPressed(JOY3_RIGHT
) then
933 if not slDirPressed
then
935 Inc(ST
[slSelection
].Current
);
936 if ST
[slSelection
].Current
> High(ST
[slSelection
].Indices
) then ST
[slSelection
].Current
:= 0;
937 slDirPressed
:= True;
941 if e_KeyPressed(IK_LEFT
) or e_KeyPressed(IK_KPLEFT
) or e_KeyPressed(VK_LEFT
) or
942 e_KeyPressed(JOY0_LEFT
) or e_KeyPressed(JOY1_LEFT
) or e_KeyPressed(JOY2_LEFT
) or e_KeyPressed(JOY3_LEFT
) then
944 if not slDirPressed
then
946 if ST
[slSelection
].Current
= 0 then ST
[slSelection
].Current
:= Length(ST
[slSelection
].Indices
);
947 Dec(ST
[slSelection
].Current
);
949 slDirPressed
:= True;
953 if (not e_KeyPressed(IK_DOWN
)) and
954 (not e_KeyPressed(IK_UP
)) and
955 (not e_KeyPressed(IK_RIGHT
)) and
956 (not e_KeyPressed(IK_LEFT
)) and
957 (not e_KeyPressed(IK_KPDOWN
)) and
958 (not e_KeyPressed(IK_KPUP
)) and
959 (not e_KeyPressed(IK_KPRIGHT
)) and
960 (not e_KeyPressed(IK_KPLEFT
)) and
961 (not e_KeyPressed(VK_DOWN
)) and
962 (not e_KeyPressed(VK_UP
)) and
963 (not e_KeyPressed(VK_RIGHT
)) and
964 (not e_KeyPressed(VK_LEFT
)) and
965 (not e_KeyPressed(JOY0_UP
)) and (not e_KeyPressed(JOY1_UP
)) and (not e_KeyPressed(JOY2_UP
)) and (not e_KeyPressed(JOY3_UP
)) and
966 (not e_KeyPressed(JOY0_DOWN
)) and (not e_KeyPressed(JOY1_DOWN
)) and (not e_KeyPressed(JOY2_DOWN
)) and (not e_KeyPressed(JOY3_DOWN
)) and
967 (not e_KeyPressed(JOY0_LEFT
)) and (not e_KeyPressed(JOY1_LEFT
)) and (not e_KeyPressed(JOY2_LEFT
)) and (not e_KeyPressed(JOY3_LEFT
)) and
968 (not e_KeyPressed(JOY0_RIGHT
)) and (not e_KeyPressed(JOY1_RIGHT
)) and (not e_KeyPressed(JOY2_RIGHT
)) and (not e_KeyPressed(JOY3_RIGHT
))
970 slDirPressed
:= False;