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, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE ../shared/a_modes.inc}
41 Players
, MaxPlayers
, LocalPl
, Bots
: Byte;
45 PingAddr
: ENetAddress
;
47 pTNetServer
= ^TNetServer
;
49 TNetServerList
= array of TNetServer
;
50 pTNetServerList
= ^TNetServerList
;
53 NetMHost
: pENetHost
= nil;
54 NetMPeer
: pENetPeer
= nil;
56 slCurrent
: TNetServerList
= nil;
57 slWaitStr
: string = '';
58 slReturnPressed
: Boolean = True;
60 procedure g_Net_Slist_Set(IP
: string; Port
: Word);
61 function g_Net_Slist_Fetch(var SL
: TNetServerList
): Boolean;
62 procedure g_Net_Slist_Update();
63 procedure g_Net_Slist_Remove();
64 function g_Net_Slist_Connect(): Boolean;
65 procedure g_Net_Slist_Check();
66 procedure g_Net_Slist_Disconnect();
67 procedure g_Net_Slist_WriteInfo();
69 procedure g_Serverlist_Draw(var SL
: TNetServerList
);
70 procedure g_Serverlist_Control(var SL
: TNetServerList
);
75 SysUtils
, e_msg
, e_input
, e_graphics
, e_log
, g_window
, g_net
, g_console
,
76 g_map
, g_game
, g_sound
, g_gui
, g_menu
, g_options
, g_language
, wadreader
;
80 slSelection
: Byte = 0;
81 slFetched
: Boolean = False;
82 slDirPressed
: Boolean = False;
84 function GetTimerMS(): Int64;
86 Result
:= GetTimer() {div 1000};
89 procedure PingServer(var S
: TNetServer
; Sock
: ENetSocket
);
92 Ping
: array [0..9] of Byte;
95 ClTime
:= GetTimerMS();
97 Buf
.data
:= Addr(Ping
[0]);
98 Buf
.dataLength
:= 2+8;
102 Int64(Addr(Ping
[2])^) := ClTime
;
104 enet_socket_send(Sock
, Addr(S
.PingAddr
), @Buf
, 1);
107 procedure PingBcast(Sock
: ENetSocket
);
111 S
.IP
:= '255.255.255.255';
112 S
.Port
:= NET_PING_PORT
;
113 enet_address_set_host(Addr(S
.PingAddr
), PChar(Addr(S
.IP
[1])));
115 S
.PingAddr
.port
:= S
.Port
;
119 function g_Net_Slist_Fetch(var SL
: TNetServerList
): Boolean;
132 procedure ProcessLocal();
135 SetLength(SL
, I
+ 1);
138 IP
:= DecodeIPV4(SvAddr
.host
);
139 Port
:= InMsg
.ReadWord();
140 Ping
:= InMsg
.ReadInt64();
141 Ping
:= GetTimerMS() - Ping
;
142 Name
:= InMsg
.ReadString();
143 Map
:= InMsg
.ReadString();
144 GameMode
:= InMsg
.ReadByte();
145 Players
:= InMsg
.ReadByte();
146 MaxPlayers
:= InMsg
.ReadByte();
147 Protocol
:= InMsg
.ReadByte();
148 Password
:= InMsg
.ReadByte() = 1;
149 LocalPl
:= InMsg
.ReadByte();
150 Bots
:= InMsg
.ReadWord();
153 procedure CheckLocalServers();
157 Sock
:= enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM
);
158 if Sock
= ENET_SOCKET_NULL
then Exit
;
159 enet_socket_set_option(Sock
, ENET_SOCKOPT_NONBLOCK
, 1);
160 enet_socket_set_option(Sock
, ENET_SOCKOPT_BROADCAST
, 1);
165 InMsg
.Alloc(NET_BUFSIZE
);
166 Buf
.data
:= InMsg
.Data
;
167 Buf
.dataLength
:= InMsg
.MaxSize
;
168 while GetTimerMS() - T
<= 500 do
172 RX
:= enet_socket_receive(Sock
, @SvAddr
, @Buf
, 1);
173 if RX
<= 0 then continue
;
176 InMsg
.BeginReading();
178 if InMsg
.ReadChar() <> 'D' then continue
;
179 if InMsg
.ReadChar() <> 'F' then continue
;
185 enet_socket_destroy(Sock
);
187 if Length(SL
) = 0 then SL
:= nil;
193 if (NetMHost
<> nil) or (NetMPeer
<> nil) then
199 if not g_Net_Slist_Connect
then
205 e_WriteLog('Fetching serverlist...', TMsgType
.Notify
);
206 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_FETCH
]);
209 NetOut
.Write(Byte(NET_MMSG_GET
));
211 P
:= enet_packet_create(NetOut
.Data
, NetOut
.CurSize
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
212 enet_peer_send(NetMPeer
, NET_MCHAN_MAIN
, P
);
213 enet_host_flush(NetMHost
);
215 while enet_host_service(NetMHost
, @NetMEvent
, 5000) > 0 do
217 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
219 if not InMsg
.Init(NetMEvent
.packet
^.data
, NetMEvent
.packet
^.dataLength
, True) then continue
;
221 MID
:= InMsg
.ReadByte();
223 if MID
<> NET_MMSG_GET
then continue
;
225 Cnt
:= InMsg
.ReadByte();
226 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_SLIST_RETRIEVED
], [Cnt
]), True);
232 for I
:= 0 to Cnt
- 1 do
235 SL
[I
].IP
:= InMsg
.ReadString();
236 SL
[I
].Port
:= InMsg
.ReadWord();
237 SL
[I
].Name
:= InMsg
.ReadString();
238 SL
[I
].Map
:= InMsg
.ReadString();
239 SL
[I
].GameMode
:= InMsg
.ReadByte();
240 SL
[I
].Players
:= InMsg
.ReadByte();
241 SL
[I
].MaxPlayers
:= InMsg
.ReadByte();
242 SL
[I
].Protocol
:= InMsg
.ReadByte();
243 SL
[I
].Password
:= InMsg
.ReadByte() = 1;
244 enet_address_set_host(Addr(SL
[I
].PingAddr
), PChar(Addr(SL
[I
].IP
[1])));
246 SL
[I
].PingAddr
.port
:= NET_PING_PORT
;
255 g_Net_Slist_Disconnect
;
258 if Length(SL
) = 0 then
264 Sock
:= enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM
);
265 if Sock
= ENET_SOCKET_NULL
then Exit
;
266 enet_socket_set_option(Sock
, ENET_SOCKOPT_NONBLOCK
, 1);
268 for I
:= Low(SL
) to High(SL
) do
269 PingServer(SL
[I
], Sock
);
271 enet_socket_set_option(Sock
, ENET_SOCKOPT_BROADCAST
, 1);
276 InMsg
.Alloc(NET_BUFSIZE
);
277 Buf
.data
:= InMsg
.Data
;
278 Buf
.dataLength
:= InMsg
.MaxSize
;
280 while GetTimerMS() - T
<= 500 do
284 RX
:= enet_socket_receive(Sock
, @SvAddr
, @Buf
, 1);
285 if RX
<= 0 then continue
;
288 InMsg
.BeginReading();
290 if InMsg
.ReadChar() <> 'D' then continue
;
291 if InMsg
.ReadChar() <> 'F' then continue
;
294 for I
:= Low(SL
) to High(SL
) do
295 if (SL
[I
].PingAddr
.host
= SvAddr
.host
) and
296 (SL
[I
].PingAddr
.port
= SvAddr
.port
) then
300 Port
:= InMsg
.ReadWord();
301 Ping
:= InMsg
.ReadInt64();
302 Ping
:= GetTimerMS() - Ping
;
303 Name
:= InMsg
.ReadString();
304 Map
:= InMsg
.ReadString();
305 GameMode
:= InMsg
.ReadByte();
306 Players
:= InMsg
.ReadByte();
307 MaxPlayers
:= InMsg
.ReadByte();
308 Protocol
:= InMsg
.ReadByte();
309 Password
:= InMsg
.ReadByte() = 1;
310 LocalPl
:= InMsg
.ReadByte();
311 Bots
:= InMsg
.ReadWord();
322 enet_socket_destroy(Sock
);
325 procedure g_Net_Slist_WriteInfo();
330 Wad
:= g_ExtractWadNameNoPath(gMapInfo
.Map
);
331 Map
:= g_ExtractFileName(gMapInfo
.Map
);
333 NetOut
.Write(NetServerName
);
335 NetOut
.Write(Wad
+ ':\' + Map
);
336 NetOut
.Write(gGameSettings
.GameMode
);
338 Cli
:= NetClientCount
;
341 NetOut
.Write(NetMaxClients
);
343 NetOut
.Write(Byte(NET_PROTOCOL_VER
));
344 NetOut
.Write(Byte(NetPassword
<> ''));
347 procedure g_Net_Slist_Update
;
353 if (NetMHost
= nil) or (NetMPeer
= nil) then Exit
;
356 NetOut
.Write(Byte(NET_MMSG_UPD
));
357 NetOut
.Write(NetAddr
.port
);
359 g_Net_Slist_WriteInfo();
361 P
:= enet_packet_create(NetOut
.Data
, NetOut
.CurSize
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
362 enet_peer_send(NetMPeer
, NET_MCHAN_UPD
, P
);
364 enet_host_flush(NetMHost
);
368 procedure g_Net_Slist_Remove
;
372 if (NetMHost
= nil) or (NetMPeer
= nil) then Exit
;
374 NetOut
.Write(Byte(NET_MMSG_DEL
));
375 NetOut
.Write(NetAddr
.port
);
377 P
:= enet_packet_create(NetOut
.Data
, NetOut
.CurSize
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
378 enet_peer_send(NetMPeer
, NET_MCHAN_MAIN
, P
);
380 enet_host_flush(NetMHost
);
384 function g_Net_Slist_Connect
: Boolean;
388 NetMHost
:= enet_host_create(nil, 1, NET_MCHANS
, 0, 0);
389 if (NetMHost
= nil) then
391 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
395 NetMPeer
:= enet_host_connect(NetMHost
, @NetSlistAddr
, NET_MCHANS
, 0);
396 if (NetMPeer
= nil) then
398 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
399 enet_host_destroy(NetMHost
);
404 if (enet_host_service(NetMHost
, @NetMEvent
, 3000) > 0) then
405 if NetMEvent
.kind
= ENET_EVENT_TYPE_CONNECT
then
408 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_CONN
]);
412 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
413 enet_packet_destroy(NetMEvent
.packet
);
415 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_SLIST_ERROR
], True);
421 procedure g_Net_Slist_Disconnect
;
423 if (NetMHost
= nil) and (NetMPeer
= nil) then Exit
;
425 if NetMode
= NET_SERVER
then g_Net_Slist_Remove
;
427 enet_peer_disconnect(NetMPeer
, 0);
428 enet_host_flush(NetMHost
);
430 enet_peer_reset(NetMPeer
);
431 enet_host_destroy(NetMHost
);
436 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_DISC
]);
439 procedure g_Net_Slist_Check
;
441 if (NetMHost
= nil) or (NetMPeer
= nil) then Exit
;
443 while (enet_host_service(NetMHost
, @NetMEvent
, 0) > 0) do
445 if NetMEvent
.kind
= ENET_EVENT_TYPE_DISCONNECT
then
447 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_LOST
], True);
448 if NetMPeer
<> nil then enet_peer_reset(NetMPeer
);
449 if NetMHost
<> nil then enet_host_destroy(NetMHost
);
455 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
456 enet_packet_destroy(NetMEvent
.packet
);
460 procedure g_Net_Slist_Set(IP
: string; Port
: Word);
464 enet_address_set_host(@NetSlistAddr
, PChar(Addr(IP
[1])));
465 NetSlistAddr
.Port
:= Port
;
466 e_WriteLog('Masterserver address set to ' + IP
+ ':' + IntToStr(Port
), TMsgType
.Notify
);
470 procedure g_Serverlist_Draw(var SL
: TNetServerList
);
472 sy
, i
, y
, mw
, mx
, l
: Integer;
482 e_CharFont_GetSize(gMenuFont
, _lc
[I_NET_SLIST
], ww
, hh
);
483 e_CharFont_Print(gMenuFont
, (gScreenWidth
div 2) - (ww
div 2), 16, _lc
[I_NET_SLIST
]);
485 e_TextureFontGetSize(gStdFont
, cw
, ch
);
487 ip
:= _lc
[I_NET_SLIST_HELP
];
488 mw
:= (Length(ip
) * cw
) div 2;
490 e_DrawFillQuad(16, 64, gScreenWidth
-16, gScreenHeight
-44, 64, 64, 64, 110);
491 e_DrawQuad(16, 64, gScreenWidth
-16, gScreenHeight
-44, 255, 127, 0);
493 e_TextureFontPrintEx(gScreenWidth
div 2 - mw
, gScreenHeight
-24, ip
, gStdFont
, 225, 225, 225, 1);
497 l
:= Length(slWaitStr
) div 2;
498 e_DrawFillQuad(16, 64, gScreenWidth
-16, gScreenHeight
-44, 64, 64, 64, 128);
499 e_DrawQuad(gScreenWidth
div 2 - 192, gScreenHeight
div 2 - 10,
500 gScreenWidth
div 2 + 192, gScreenHeight
div 2 + 11, 255, 127, 0);
501 e_TextureFontPrint(gScreenWidth
div 2 - cw
* l
, gScreenHeight
div 2 - ch
div 2,
502 slWaitStr
, gStdFont
);
507 if (slSelection
< Length(SL
)) then
510 sy
:= y
+ 42 * I
- 4;
511 ip
:= _lc
[I_NET_ADDRESS
] + ' ' + SL
[I
].IP
+ ':' + IntToStr(SL
[I
].Port
);
512 if SL
[I
].Password
then
513 ip
:= ip
+ ' ' + _lc
[I_NET_SERVER_PASSWORD
] + ' ' + _lc
[I_MENU_YES
]
515 ip
:= ip
+ ' ' + _lc
[I_NET_SERVER_PASSWORD
] + ' ' + _lc
[I_MENU_NO
];
517 if Length(SL
) > 0 then
520 mw
:= (gScreenWidth
- 188);
523 e_DrawFillQuad(16 + 1, sy
, gScreenWidth
- 16 - 1, sy
+ 40, 64, 64, 64, 0);
524 e_DrawLine(1, 16 + 1, sy
, gScreenWidth
- 16 - 1, sy
, 205, 205, 205);
525 e_DrawLine(1, 16 + 1, sy
+ 41, gScreenWidth
- 16 - 1, sy
+ 41, 255, 255, 255);
527 e_DrawLine(1, 16, 85, gScreenWidth
- 16, 85, 255, 127, 0);
528 e_DrawLine(1, 16, gScreenHeight
-64, gScreenWidth
-16, gScreenHeight
-64, 255, 127, 0);
530 e_DrawLine(1, mx
- 70, 64, mx
- 70, gScreenHeight
-44, 255, 127, 0);
531 e_DrawLine(1, mx
, 64, mx
, gScreenHeight
-64, 255, 127, 0);
532 e_DrawLine(1, mx
+ 52, 64, mx
+ 52, gScreenHeight
-64, 255, 127, 0);
533 e_DrawLine(1, mx
+ 104, 64, mx
+ 104, gScreenHeight
-64, 255, 127, 0);
535 e_TextureFontPrintEx(18, 68, 'NAME/MAP', gStdFont
, 255, 127, 0, 1);
538 for I
:= 0 to High(SL
) do
540 e_TextureFontPrintEx(18, y
, SL
[I
].Name
, gStdFont
, 255, 255, 255, 1);
541 e_TextureFontPrintEx(18, y
+ 16, SL
[I
].Map
, gStdFont
, 210, 210, 210, 1);
546 e_TextureFontPrintEx(mx
- 68, 68, 'PING', gStdFont
, 255, 127, 0, 1);
548 for I
:= 0 to High(SL
) do
550 if (SL
[I
].Ping
< 0) or (SL
[I
].Ping
> 999) then
551 e_TextureFontPrintEx(mx
- 68, y
, _lc
[I_NET_SLIST_NO_ACCESS
], gStdFont
, 255, 0, 0, 1)
553 if SL
[I
].Ping
= 0 then
554 e_TextureFontPrintEx(mx
- 68, y
, '<1' + _lc
[I_NET_SLIST_PING_MS
], gStdFont
, 255, 255, 255, 1)
556 e_TextureFontPrintEx(mx
- 68, y
, IntToStr(SL
[I
].Ping
) + _lc
[I_NET_SLIST_PING_MS
], gStdFont
, 255, 255, 255, 1);
561 e_TextureFontPrintEx(mx
+ 2, 68, 'MODE', gStdFont
, 255, 127, 0, 1);
563 for I
:= 0 to High(SL
) do
565 e_TextureFontPrintEx(mx
+ 2, y
, g_Game_ModeToText(SL
[I
].GameMode
), gStdFont
, 255, 255, 255, 1);
570 e_TextureFontPrintEx(mx
+ 54, 68, 'PLRS', gStdFont
, 255, 127, 0, 1);
572 for I
:= 0 to High(SL
) do
574 e_TextureFontPrintEx(mx
+ 54, y
, IntToStr(SL
[I
].Players
) + '/' + IntToStr(SL
[I
].MaxPlayers
), gStdFont
, 255, 255, 255, 1);
575 e_TextureFontPrintEx(mx
+ 54, y
+ 16, IntToStr(SL
[I
].LocalPl
) + '+' + IntToStr(SL
[I
].Bots
), gStdFont
, 210, 210, 210, 1);
579 e_TextureFontPrintEx(mx
+ 106, 68, 'VER', gStdFont
, 255, 127, 0, 1);
581 for I
:= 0 to High(SL
) do
583 e_TextureFontPrintEx(mx
+ 106, y
, IntToStr(SL
[I
].Protocol
), gStdFont
, 255, 255, 255, 1);
588 e_TextureFontPrintEx(20, gScreenHeight
-61, ip
, gStdFont
, 205, 205, 205, 1);
589 ip
:= IntToStr(Length(SL
)) + _lc
[I_NET_SLIST_SERVERS
];
590 e_TextureFontPrintEx(gScreenWidth
- 48 - (Length(ip
) + 1)*cw
,
591 gScreenHeight
-61, ip
, gStdFont
, 205, 205, 205, 1);
594 procedure g_Serverlist_Control(var SL
: TNetServerList
);
598 if gConsoleShow
or gChatShow
then
601 qm
:= g_ProcessMessages(); // this updates kbd
603 if qm
or e_KeyPressed(IK_ESCAPE
) or e_KeyPressed(VK_ESCAPE
) then
606 gState
:= STATE_MENU
;
607 g_GUI_ShowWindow('MainMenu');
608 g_GUI_ShowWindow('NetGameMenu');
609 g_GUI_ShowWindow('NetClientMenu');
610 g_Sound_PlayEx(WINDOW_CLOSESOUND
);
614 if e_KeyPressed(IK_SPACE
) or e_KeyPressed(VK_JUMP
) then
616 if not slFetched
then
618 slWaitStr
:= _lc
[I_NET_SLIST_WAIT
];
621 g_window
.ReDrawWindow
;
623 if g_Net_Slist_Fetch(SL
) then
626 slWaitStr
:= _lc
[I_NET_SLIST_NOSERVERS
];
630 slWaitStr
:= _lc
[I_NET_SLIST_ERROR
];
638 if SL
= nil then Exit
;
640 if e_KeyPressed(IK_RETURN
) or e_KeyPressed(IK_KPRETURN
) or e_KeyPressed(VK_FIRE
) or e_KeyPressed(VK_OPEN
) then
642 if not slReturnPressed
then
644 if SL
[slSelection
].Password
then
646 PromptIP
:= SL
[slSelection
].IP
;
647 PromptPort
:= SL
[slSelection
].Port
;
648 gState
:= STATE_MENU
;
649 g_GUI_ShowWindow('ClientPasswordMenu');
651 slReturnPressed
:= True;
655 g_Game_StartClient(SL
[slSelection
].IP
, SL
[slSelection
].Port
, '');
657 slReturnPressed
:= True;
662 slReturnPressed
:= False;
664 if e_KeyPressed(IK_DOWN
) or e_KeyPressed(IK_KPDOWN
) or e_KeyPressed(VK_DOWN
) then
666 if not slDirPressed
then
669 if slSelection
> High(SL
) then slSelection
:= 0;
670 slDirPressed
:= True;
674 if e_KeyPressed(IK_UP
) or e_KeyPressed(IK_KPUP
) or e_KeyPressed(VK_UP
) then
676 if not slDirPressed
then
678 if slSelection
= 0 then slSelection
:= Length(SL
);
681 slDirPressed
:= True;
685 if (not e_KeyPressed(IK_DOWN
)) and (not e_KeyPressed(IK_UP
)) and (not e_KeyPressed(IK_KPDOWN
)) and (not e_KeyPressed(IK_KPUP
)) and (not e_KeyPressed(VK_DOWN
)) and (not e_KeyPressed(VK_UP
)) then
686 slDirPressed
:= False;