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 function g_Net_Slist_Fetch(var SL
: TNetServerList
): Boolean;
122 if (NetMHost
<> nil) or (NetMPeer
<> nil) then
125 if not g_Net_Slist_Connect
then
128 e_WriteLog('Fetching serverlist...', MSG_NOTIFY
);
129 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_FETCH
]);
132 NetOut
.Write(Byte(NET_MMSG_GET
));
134 P
:= enet_packet_create(NetOut
.Data
, NetOut
.CurSize
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
135 enet_peer_send(NetMPeer
, NET_MCHAN_MAIN
, P
);
136 enet_host_flush(NetMHost
);
138 while enet_host_service(NetMHost
, @NetMEvent
, 5000) > 0 do
140 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
142 if not InMsg
.Init(NetMEvent
.packet
^.data
, NetMEvent
.packet
^.dataLength
, True) then continue
;
144 MID
:= InMsg
.ReadByte();
146 if MID
<> NET_MMSG_GET
then continue
;
148 Cnt
:= InMsg
.ReadByte();
149 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_SLIST_RETRIEVED
], [Cnt
]), True);
155 for I
:= 0 to Cnt
- 1 do
158 SL
[I
].IP
:= InMsg
.ReadString();
159 SL
[I
].Port
:= InMsg
.ReadWord();
160 SL
[I
].Name
:= InMsg
.ReadString();
161 SL
[I
].Map
:= InMsg
.ReadString();
162 SL
[I
].GameMode
:= InMsg
.ReadByte();
163 SL
[I
].Players
:= InMsg
.ReadByte();
164 SL
[I
].MaxPlayers
:= InMsg
.ReadByte();
165 SL
[I
].Protocol
:= InMsg
.ReadByte();
166 SL
[I
].Password
:= InMsg
.ReadByte() = 1;
167 enet_address_set_host(Addr(SL
[I
].PingAddr
), PChar(Addr(SL
[I
].IP
[1])));
169 SL
[I
].PingAddr
.port
:= SL
[I
].Port
+ 1;
178 g_Net_Slist_Disconnect
;
181 if Length(SL
) = 0 then Exit
;
183 Sock
:= enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM
);
184 if Sock
= ENET_SOCKET_NULL
then Exit
;
185 enet_socket_set_option(Sock
, ENET_SOCKOPT_NONBLOCK
, 1);
187 for I
:= Low(SL
) to High(SL
) do
188 PingServer(SL
[I
], Sock
);
192 InMsg
.Alloc(NET_BUFSIZE
);
193 Buf
.data
:= InMsg
.Data
;
194 Buf
.dataLength
:= InMsg
.MaxSize
;
196 while Cnt
< Length(SL
) do
198 if GetTimerMS() - T
> 500 then break
;
202 RX
:= enet_socket_receive(Sock
, @SvAddr
, @Buf
, 1);
203 if RX
<= 0 then continue
;
206 InMsg
.BeginReading();
208 if InMsg
.ReadChar() <> 'D' then continue
;
209 if InMsg
.ReadChar() <> 'F' then continue
;
211 for I
:= Low(SL
) to High(SL
) do
212 if (SL
[I
].PingAddr
.host
= SvAddr
.host
) and
213 (SL
[I
].PingAddr
.port
= SvAddr
.port
) then
217 Ping
:= InMsg
.ReadInt64();
218 Ping
:= GetTimerMS() - Ping
;
219 Name
:= InMsg
.ReadString();
220 Map
:= InMsg
.ReadString();
221 GameMode
:= InMsg
.ReadByte();
222 Players
:= InMsg
.ReadByte();
223 MaxPlayers
:= InMsg
.ReadByte();
224 Protocol
:= InMsg
.ReadByte();
225 Password
:= InMsg
.ReadByte() = 1;
226 LocalPl
:= InMsg
.ReadByte();
227 Bots
:= InMsg
.ReadWord();
235 enet_socket_destroy(Sock
);
238 procedure g_Net_Slist_WriteInfo();
243 Wad
:= g_ExtractWadNameNoPath(gMapInfo
.Map
);
244 Map
:= g_ExtractFileName(gMapInfo
.Map
);
246 NetOut
.Write(NetServerName
);
248 NetOut
.Write(Wad
+ ':\' + Map
);
249 NetOut
.Write(gGameSettings
.GameMode
);
251 Cli
:= NetClientCount
;
254 NetOut
.Write(NetMaxClients
);
256 NetOut
.Write(Byte(NET_PROTOCOL_VER
));
257 NetOut
.Write(Byte(NetPassword
<> ''));
260 procedure g_Net_Slist_Update
;
266 if (NetMHost
= nil) or (NetMPeer
= nil) then Exit
;
269 NetOut
.Write(Byte(NET_MMSG_UPD
));
270 NetOut
.Write(NetAddr
.port
);
272 g_Net_Slist_WriteInfo();
274 P
:= enet_packet_create(NetOut
.Data
, NetOut
.CurSize
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
275 enet_peer_send(NetMPeer
, NET_MCHAN_UPD
, P
);
277 enet_host_flush(NetMHost
);
281 procedure g_Net_Slist_Remove
;
285 if (NetMHost
= nil) or (NetMPeer
= nil) then Exit
;
287 NetOut
.Write(Byte(NET_MMSG_DEL
));
288 NetOut
.Write(NetAddr
.port
);
290 P
:= enet_packet_create(NetOut
.Data
, NetOut
.CurSize
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
291 enet_peer_send(NetMPeer
, NET_MCHAN_MAIN
, P
);
293 enet_host_flush(NetMHost
);
297 function g_Net_Slist_Connect
: Boolean;
301 NetMHost
:= enet_host_create(nil, 1, NET_MCHANS
, 0, 0);
302 if (NetMHost
= nil) then
304 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
308 NetMPeer
:= enet_host_connect(NetMHost
, @NetSlistAddr
, NET_MCHANS
, 0);
309 if (NetMPeer
= nil) then
311 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
312 enet_host_destroy(NetMHost
);
317 if (enet_host_service(NetMHost
, @NetMEvent
, 3000) > 0) then
318 if NetMEvent
.kind
= ENET_EVENT_TYPE_CONNECT
then
321 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_CONN
]);
325 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
326 enet_packet_destroy(NetMEvent
.packet
);
328 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_SLIST_ERROR
], True);
334 procedure g_Net_Slist_Disconnect
;
336 if (NetMHost
= nil) and (NetMPeer
= nil) then Exit
;
338 if NetMode
= NET_SERVER
then g_Net_Slist_Remove
;
340 enet_peer_disconnect(NetMPeer
, 0);
341 enet_host_flush(NetMHost
);
343 enet_peer_reset(NetMPeer
);
344 enet_host_destroy(NetMHost
);
349 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_DISC
]);
352 procedure g_Net_Slist_Check
;
354 if (NetMHost
= nil) or (NetMPeer
= nil) then Exit
;
356 while (enet_host_service(NetMHost
, @NetMEvent
, 0) > 0) do
358 if NetMEvent
.kind
= ENET_EVENT_TYPE_DISCONNECT
then
360 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_LOST
], True);
361 if NetMPeer
<> nil then enet_peer_reset(NetMPeer
);
362 if NetMHost
<> nil then enet_host_destroy(NetMHost
);
368 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
369 enet_packet_destroy(NetMEvent
.packet
);
373 procedure g_Net_Slist_Set(IP
: string; Port
: Word);
377 enet_address_set_host(@NetSlistAddr
, PChar(Addr(IP
[1])));
378 NetSlistAddr
.Port
:= Port
;
379 e_WriteLog('Masterserver address set to ' + IP
+ ':' + IntToStr(Port
), MSG_NOTIFY
);
383 procedure g_Serverlist_Draw(var SL
: TNetServerList
);
385 sy
, i
, y
, mw
, mx
, l
: Integer;
395 e_CharFont_GetSize(gMenuFont
, _lc
[I_NET_SLIST
], ww
, hh
);
396 e_CharFont_Print(gMenuFont
, (gScreenWidth
div 2) - (ww
div 2), 16, _lc
[I_NET_SLIST
]);
398 e_TextureFontGetSize(gStdFont
, cw
, ch
);
400 ip
:= _lc
[I_NET_SLIST_HELP
];
401 mw
:= (Length(ip
) * cw
) div 2;
403 e_DrawFillQuad(16, 64, gScreenWidth
-16, gScreenHeight
-44, 64, 64, 64, 110);
404 e_DrawQuad(16, 64, gScreenWidth
-16, gScreenHeight
-44, 255, 127, 0);
406 e_TextureFontPrintEx(gScreenWidth
div 2 - mw
, gScreenHeight
-24, ip
, gStdFont
, 225, 225, 225, 1);
410 l
:= Length(slWaitStr
) div 2;
411 e_DrawFillQuad(16, 64, gScreenWidth
-16, gScreenHeight
-44, 64, 64, 64, 128);
412 e_DrawQuad(gScreenWidth
div 2 - 192, gScreenHeight
div 2 - 10,
413 gScreenWidth
div 2 + 192, gScreenHeight
div 2 + 11, 255, 127, 0);
414 e_TextureFontPrint(gScreenWidth
div 2 - cw
* l
, gScreenHeight
div 2 - ch
div 2,
415 slWaitStr
, gStdFont
);
420 if (slSelection
< Length(SL
)) then
423 sy
:= y
+ 42 * I
- 4;
424 ip
:= _lc
[I_NET_ADDRESS
] + ' ' + SL
[I
].IP
+ ':' + IntToStr(SL
[I
].Port
);
425 if SL
[I
].Password
then
426 ip
:= ip
+ ' ' + _lc
[I_NET_SERVER_PASSWORD
] + ' ' + _lc
[I_MENU_YES
]
428 ip
:= ip
+ ' ' + _lc
[I_NET_SERVER_PASSWORD
] + ' ' + _lc
[I_MENU_NO
];
430 if Length(SL
) > 0 then
433 mw
:= (gScreenWidth
- 188);
436 e_DrawFillQuad(16 + 1, sy
, gScreenWidth
- 16 - 1, sy
+ 40, 64, 64, 64, 0);
437 e_DrawLine(1, 16 + 1, sy
, gScreenWidth
- 16 - 1, sy
, 205, 205, 205);
438 e_DrawLine(1, 16 + 1, sy
+ 41, gScreenWidth
- 16 - 1, sy
+ 41, 255, 255, 255);
440 e_DrawLine(1, 16, 85, gScreenWidth
- 16, 85, 255, 127, 0);
441 e_DrawLine(1, 16, gScreenHeight
-64, gScreenWidth
-16, gScreenHeight
-64, 255, 127, 0);
443 e_DrawLine(1, mx
- 70, 64, mx
- 70, gScreenHeight
-44, 255, 127, 0);
444 e_DrawLine(1, mx
, 64, mx
, gScreenHeight
-64, 255, 127, 0);
445 e_DrawLine(1, mx
+ 52, 64, mx
+ 52, gScreenHeight
-64, 255, 127, 0);
446 e_DrawLine(1, mx
+ 104, 64, mx
+ 104, gScreenHeight
-64, 255, 127, 0);
448 e_TextureFontPrintEx(18, 68, 'NAME/MAP', gStdFont
, 255, 127, 0, 1);
451 for I
:= 0 to High(SL
) do
453 e_TextureFontPrintEx(18, y
, SL
[I
].Name
, gStdFont
, 255, 255, 255, 1);
454 e_TextureFontPrintEx(18, y
+ 16, SL
[I
].Map
, gStdFont
, 210, 210, 210, 1);
459 e_TextureFontPrintEx(mx
- 68, 68, 'PING', gStdFont
, 255, 127, 0, 1);
461 for I
:= 0 to High(SL
) do
463 if (SL
[I
].Ping
< 0) or (SL
[I
].Ping
> 999) then
464 e_TextureFontPrintEx(mx
- 68, y
, _lc
[I_NET_SLIST_NO_ACCESS
], gStdFont
, 255, 0, 0, 1)
466 if SL
[I
].Ping
= 0 then
467 e_TextureFontPrintEx(mx
- 68, y
, '<1' + _lc
[I_NET_SLIST_PING_MS
], gStdFont
, 255, 255, 255, 1)
469 e_TextureFontPrintEx(mx
- 68, y
, IntToStr(SL
[I
].Ping
) + _lc
[I_NET_SLIST_PING_MS
], gStdFont
, 255, 255, 255, 1);
474 e_TextureFontPrintEx(mx
+ 2, 68, 'MODE', gStdFont
, 255, 127, 0, 1);
476 for I
:= 0 to High(SL
) do
478 e_TextureFontPrintEx(mx
+ 2, y
, g_Game_ModeToText(SL
[I
].GameMode
), gStdFont
, 255, 255, 255, 1);
483 e_TextureFontPrintEx(mx
+ 54, 68, 'PLRS', gStdFont
, 255, 127, 0, 1);
485 for I
:= 0 to High(SL
) do
487 e_TextureFontPrintEx(mx
+ 54, y
, IntToStr(SL
[I
].Players
) + '/' + IntToStr(SL
[I
].MaxPlayers
), gStdFont
, 255, 255, 255, 1);
488 e_TextureFontPrintEx(mx
+ 54, y
+ 16, IntToStr(SL
[I
].LocalPl
) + '+' + IntToStr(SL
[I
].Bots
), gStdFont
, 210, 210, 210, 1);
492 e_TextureFontPrintEx(mx
+ 106, 68, 'VER', gStdFont
, 255, 127, 0, 1);
494 for I
:= 0 to High(SL
) do
496 e_TextureFontPrintEx(mx
+ 106, y
, IntToStr(SL
[I
].Protocol
), gStdFont
, 255, 255, 255, 1);
501 e_TextureFontPrintEx(20, gScreenHeight
-61, ip
, gStdFont
, 205, 205, 205, 1);
502 ip
:= IntToStr(Length(SL
)) + _lc
[I_NET_SLIST_SERVERS
];
503 e_TextureFontPrintEx(gScreenWidth
- 48 - (Length(ip
) + 1)*cw
,
504 gScreenHeight
-61, ip
, gStdFont
, 205, 205, 205, 1);
507 procedure g_Serverlist_Control(var SL
: TNetServerList
);
509 if gConsoleShow
or gChatShow
then
514 if e_KeyPressed(IK_ESCAPE
) then
517 gState
:= STATE_MENU
;
518 g_GUI_ShowWindow('MainMenu');
519 g_GUI_ShowWindow('NetGameMenu');
520 g_GUI_ShowWindow('NetClientMenu');
521 g_Sound_PlayEx(WINDOW_CLOSESOUND
);
525 if e_KeyPressed(IK_SPACE
) then
527 if not slFetched
then
529 slWaitStr
:= _lc
[I_NET_SLIST_WAIT
];
532 g_window
.ReDrawWindow
;
534 if g_Net_Slist_Fetch(SL
) then
537 slWaitStr
:= _lc
[I_NET_SLIST_NOSERVERS
];
540 slWaitStr
:= _lc
[I_NET_SLIST_ERROR
];
548 if SL
= nil then Exit
;
550 if e_KeyPressed(IK_RETURN
) or e_KeyPressed(IK_KPRETURN
) then
552 if not slReturnPressed
then
554 if SL
[slSelection
].Password
then
556 PromptIP
:= SL
[slSelection
].IP
;
557 PromptPort
:= SL
[slSelection
].Port
;
558 gState
:= STATE_MENU
;
559 g_GUI_ShowWindow('ClientPasswordMenu');
561 slReturnPressed
:= True;
565 g_Game_StartClient(SL
[slSelection
].IP
, SL
[slSelection
].Port
, '');
567 slReturnPressed
:= True;
572 slReturnPressed
:= False;
574 if e_KeyPressed(IK_DOWN
) or e_KeyPressed(IK_KPDOWN
) then
576 if not slDirPressed
then
579 if slSelection
> High(SL
) then slSelection
:= 0;
580 slDirPressed
:= True;
584 if e_KeyPressed(IK_UP
) or e_KeyPressed(IK_KPUP
) then
586 if not slDirPressed
then
588 if slSelection
= 0 then slSelection
:= Length(SL
);
591 slDirPressed
:= True;
595 if (not e_KeyPressed(IK_DOWN
)) and (not e_KeyPressed(IK_UP
)) and (not e_KeyPressed(IK_KPDOWN
)) and (not e_KeyPressed(IK_KPUP
)) then
596 slDirPressed
:= False;