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/>.
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_fixedbuffer
, e_input
, e_graphics
, e_log
, g_window
, g_net
, g_console
,
76 g_map
, g_game
, g_sound
, g_textures
, 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;
121 if (NetMHost
<> nil) or (NetMPeer
<> nil) then
124 if not g_Net_Slist_Connect
then
127 e_WriteLog('Fetching serverlist...', MSG_NOTIFY
);
128 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_FETCH
]);
130 e_Buffer_Clear(@NetOut
);
131 e_Buffer_Write(@NetOut
, Byte(NET_MMSG_GET
));
133 P
:= enet_packet_create(Addr(NetOut
.Data
), NetOut
.Len
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
134 enet_peer_send(NetMPeer
, NET_MCHAN_MAIN
, P
);
135 enet_host_flush(NetMHost
);
137 while enet_host_service(NetMHost
, @NetMEvent
, 5000) > 0 do
139 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
142 MID
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
);
144 if MID
<> NET_MMSG_GET
then continue
;
146 Cnt
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
);
147 e_WriteLog('Retrieved ' + IntToStr(Cnt
) + ' server(s).', MSG_NOTIFY
);
148 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
:= e_Raw_Read_String(NetMEvent
.packet
^.data
);
159 SL
[I
].Port
:= e_Raw_Read_Word(NetMEvent
.packet
^.data
);
160 SL
[I
].Name
:= e_Raw_Read_String(NetMEvent
.packet
^.data
);
161 SL
[I
].Map
:= e_Raw_Read_String(NetMEvent
.packet
^.data
);
162 SL
[I
].GameMode
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
);
163 SL
[I
].Players
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
);
164 SL
[I
].MaxPlayers
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
);
165 SL
[I
].Protocol
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
);
166 SL
[I
].Password
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
) = 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
;
179 e_Buffer_Clear(@NetOut
);
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 e_Buffer_Clear(@NetIn
);
193 Buf
.data
:= Addr(NetIn
.Data
);
194 Buf
.dataLength
:= Length(NetIn
.Data
);
196 while Cnt
< Length(SL
) do
198 if GetTimerMS() - T
> 500 then break
;
200 e_Buffer_Clear(@NetIn
);
202 RX
:= enet_socket_receive(Sock
, @SvAddr
, @Buf
, 1);
203 if RX
<= 0 then continue
;
207 if e_Buffer_Read_Char(@NetIn
) <> 'D' then continue
;
208 if e_Buffer_Read_Char(@NetIn
) <> 'F' then continue
;
210 for I
:= Low(SL
) to High(SL
) do
211 if (SL
[I
].PingAddr
.host
= SvAddr
.host
) and
212 (SL
[I
].PingAddr
.port
= SvAddr
.port
) then
216 Ping
:= e_Buffer_Read_Int64(@NetIn
);
217 Ping
:= GetTimerMS() - Ping
;
218 Name
:= e_Buffer_Read_String(@NetIn
);
219 Map
:= e_Buffer_Read_String(@NetIn
);
220 GameMode
:= e_Buffer_Read_Byte(@NetIn
);
221 Players
:= e_Buffer_Read_Byte(@NetIn
);
222 MaxPlayers
:= e_Buffer_Read_Byte(@NetIn
);
223 Protocol
:= e_Buffer_Read_Byte(@NetIn
);
224 Password
:= e_Buffer_Read_Byte(@NetIn
) = 1;
225 LocalPl
:= e_Buffer_Read_Byte(@NetIn
);
226 Bots
:= e_Buffer_Read_Word(@NetIn
);
233 enet_socket_destroy(Sock
);
236 procedure g_Net_Slist_WriteInfo();
241 Wad
:= g_ExtractWadNameNoPath(gMapInfo
.Map
);
242 Map
:= g_ExtractFileName(gMapInfo
.Map
);
244 e_Buffer_Write(@NetOut
, NetServerName
);
246 e_Buffer_Write(@NetOut
, Wad
+ ':\' + Map
);
247 e_Buffer_Write(@NetOut
, gGameSettings
.GameMode
);
249 Cli
:= NetClientCount
;
250 e_Buffer_Write(@NetOut
, Cli
);
252 e_Buffer_Write(@NetOut
, NetMaxClients
);
254 e_Buffer_Write(@NetOut
, Byte(NET_PROTOCOL_VER
));
255 e_Buffer_Write(@NetOut
, Byte(NetPassword
<> ''));
258 procedure g_Net_Slist_Update
;
264 if (NetMHost
= nil) or (NetMPeer
= nil) then Exit
;
266 e_Buffer_Clear(@NetOut
);
267 e_Buffer_Write(@NetOut
, Byte(NET_MMSG_UPD
));
268 e_Buffer_Write(@NetOut
, NetAddr
.port
);
270 g_Net_Slist_WriteInfo();
272 P
:= enet_packet_create(Addr(NetOut
.Data
), NetOut
.Len
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
273 enet_peer_send(NetMPeer
, NET_MCHAN_UPD
, P
);
275 enet_host_flush(NetMHost
);
276 e_Buffer_Clear(@NetOut
);
279 procedure g_Net_Slist_Remove
;
283 if (NetMHost
= nil) or (NetMPeer
= nil) then Exit
;
284 e_Buffer_Clear(@NetOut
);
285 e_Buffer_Write(@NetOut
, Byte(NET_MMSG_DEL
));
286 e_Buffer_Write(@NetOut
, NetAddr
.port
);
288 P
:= enet_packet_create(Addr(NetOut
.Data
), NetOut
.Len
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
289 enet_peer_send(NetMPeer
, NET_MCHAN_MAIN
, P
);
291 enet_host_flush(NetMHost
);
292 e_Buffer_Clear(@NetOut
);
295 function g_Net_Slist_Connect
: Boolean;
299 NetMHost
:= enet_host_create(nil, 1, NET_MCHANS
, 0, 0);
300 if (NetMHost
= nil) then
302 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
306 NetMPeer
:= enet_host_connect(NetMHost
, @NetSlistAddr
, NET_MCHANS
, 0);
307 if (NetMPeer
= nil) then
309 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
310 enet_host_destroy(NetMHost
);
315 if (enet_host_service(NetMHost
, @NetMEvent
, 3000) > 0) then
316 if NetMEvent
.kind
= ENET_EVENT_TYPE_CONNECT
then
319 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_CONN
]);
323 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
324 enet_packet_destroy(NetMEvent
.packet
);
326 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_SLIST_ERROR
], True);
332 procedure g_Net_Slist_Disconnect
;
334 if (NetMHost
= nil) and (NetMPeer
= nil) then Exit
;
336 if NetMode
= NET_SERVER
then g_Net_Slist_Remove
;
338 enet_peer_disconnect(NetMPeer
, 0);
339 enet_host_flush(NetMHost
);
341 enet_peer_reset(NetMPeer
);
342 enet_host_destroy(NetMHost
);
347 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_DISC
]);
350 procedure g_Net_Slist_Check
;
352 if (NetMHost
= nil) or (NetMPeer
= nil) then Exit
;
354 while (enet_host_service(NetMHost
, @NetMEvent
, 0) > 0) do
356 if NetMEvent
.kind
= ENET_EVENT_TYPE_DISCONNECT
then
358 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_LOST
], True);
359 if NetMPeer
<> nil then enet_peer_reset(NetMPeer
);
360 if NetMHost
<> nil then enet_host_destroy(NetMHost
);
366 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
367 enet_packet_destroy(NetMEvent
.packet
);
371 procedure g_Net_Slist_Set(IP
: string; Port
: Word);
375 enet_address_set_host(@NetSlistAddr
, PChar(Addr(IP
[1])));
376 NetSlistAddr
.Port
:= Port
;
377 e_WriteLog('Masterserver address set to ' + IP
+ ':' + IntToStr(Port
), MSG_NOTIFY
);
381 procedure g_Serverlist_Draw(var SL
: TNetServerList
);
383 sy
, i
, y
, mw
, mx
, l
: Integer;
391 e_CharFont_GetSize(gMenuFont
, _lc
[I_NET_SLIST
], ww
, hh
);
392 e_CharFont_Print(gMenuFont
, (gScreenWidth
div 2) - (ww
div 2), 16, _lc
[I_NET_SLIST
]);
394 e_TextureFontGetSize(gStdFont
, cw
, ch
);
396 ip
:= _lc
[I_NET_SLIST_HELP
];
397 mw
:= (Length(ip
) * cw
) div 2;
399 e_DrawFillQuad(16, 64, gScreenWidth
-16, gScreenHeight
-44, 64, 64, 64, 110);
400 e_DrawQuad(16, 64, gScreenWidth
-16, gScreenHeight
-44, 255, 127, 0);
402 e_TextureFontPrintEx(gScreenWidth
div 2 - mw
, gScreenHeight
-24, ip
, gStdFont
, 225, 225, 225, 1);
406 l
:= Length(slWaitStr
) div 2;
407 e_DrawFillQuad(16, 64, gScreenWidth
-16, gScreenHeight
-44, 64, 64, 64, 128);
408 e_DrawQuad(gScreenWidth
div 2 - 192, gScreenHeight
div 2 - 10,
409 gScreenWidth
div 2 + 192, gScreenHeight
div 2 + 11, 255, 127, 0);
410 e_TextureFontPrint(gScreenWidth
div 2 - cw
* l
, gScreenHeight
div 2 - ch
div 2,
411 slWaitStr
, gStdFont
);
416 if (slSelection
< Length(SL
)) then
419 sy
:= y
+ 42 * I
- 4;
420 ip
:= _lc
[I_NET_ADDRESS
] + ' ' + SL
[I
].IP
+ ':' + IntToStr(SL
[I
].Port
);
421 if SL
[I
].Password
then
422 ip
:= ip
+ ' ' + _lc
[I_NET_SERVER_PASSWORD
] + ' ' + _lc
[I_MENU_YES
]
424 ip
:= ip
+ ' ' + _lc
[I_NET_SERVER_PASSWORD
] + ' ' + _lc
[I_MENU_NO
];
426 if Length(SL
) > 0 then
429 mw
:= (gScreenWidth
- 188);
432 e_DrawFillQuad(16 + 1, sy
, gScreenWidth
- 16 - 1, sy
+ 40, 64, 64, 64, 0);
433 e_DrawLine(1, 16 + 1, sy
, gScreenWidth
- 16 - 1, sy
, 205, 205, 205);
434 e_DrawLine(1, 16 + 1, sy
+ 41, gScreenWidth
- 16 - 1, sy
+ 41, 255, 255, 255);
436 e_DrawLine(1, 16, 85, gScreenWidth
- 16, 85, 255, 127, 0);
437 e_DrawLine(1, 16, gScreenHeight
-64, gScreenWidth
-16, gScreenHeight
-64, 255, 127, 0);
439 e_DrawLine(1, mx
- 70, 64, mx
- 70, gScreenHeight
-44, 255, 127, 0);
440 e_DrawLine(1, mx
, 64, mx
, gScreenHeight
-64, 255, 127, 0);
441 e_DrawLine(1, mx
+ 52, 64, mx
+ 52, gScreenHeight
-64, 255, 127, 0);
442 e_DrawLine(1, mx
+ 104, 64, mx
+ 104, gScreenHeight
-64, 255, 127, 0);
444 e_TextureFontPrintEx(18, 68, 'NAME/MAP', gStdFont
, 255, 127, 0, 1);
447 for I
:= 0 to High(SL
) do
449 e_TextureFontPrintEx(18, y
, SL
[I
].Name
, gStdFont
, 255, 255, 255, 1);
450 e_TextureFontPrintEx(18, y
+ 16, SL
[I
].Map
, gStdFont
, 210, 210, 210, 1);
455 e_TextureFontPrintEx(mx
- 68, 68, 'PING', gStdFont
, 255, 127, 0, 1);
457 for I
:= 0 to High(SL
) do
459 if (SL
[I
].Ping
< 0) or (SL
[I
].Ping
> 999) then
460 e_TextureFontPrintEx(mx
- 68, y
, _lc
[I_NET_SLIST_NO_ACCESS
], gStdFont
, 255, 0, 0, 1)
462 if SL
[I
].Ping
= 0 then
463 e_TextureFontPrintEx(mx
- 68, y
, '<1' + _lc
[I_NET_SLIST_PING_MS
], gStdFont
, 255, 255, 255, 1)
465 e_TextureFontPrintEx(mx
- 68, y
, IntToStr(SL
[I
].Ping
) + _lc
[I_NET_SLIST_PING_MS
], gStdFont
, 255, 255, 255, 1);
470 e_TextureFontPrintEx(mx
+ 2, 68, 'MODE', gStdFont
, 255, 127, 0, 1);
472 for I
:= 0 to High(SL
) do
474 e_TextureFontPrintEx(mx
+ 2, y
, g_Game_ModeToText(SL
[I
].GameMode
), gStdFont
, 255, 255, 255, 1);
479 e_TextureFontPrintEx(mx
+ 54, 68, 'PLRS', gStdFont
, 255, 127, 0, 1);
481 for I
:= 0 to High(SL
) do
483 e_TextureFontPrintEx(mx
+ 54, y
, IntToStr(SL
[I
].Players
) + '/' + IntToStr(SL
[I
].MaxPlayers
), gStdFont
, 255, 255, 255, 1);
484 e_TextureFontPrintEx(mx
+ 54, y
+ 16, IntToStr(SL
[I
].LocalPl
) + '+' + IntToStr(SL
[I
].Bots
), gStdFont
, 210, 210, 210, 1);
488 e_TextureFontPrintEx(mx
+ 106, 68, 'VER', gStdFont
, 255, 127, 0, 1);
490 for I
:= 0 to High(SL
) do
492 e_TextureFontPrintEx(mx
+ 106, y
, IntToStr(SL
[I
].Protocol
), gStdFont
, 255, 255, 255, 1);
497 e_TextureFontPrintEx(20, gScreenHeight
-61, ip
, gStdFont
, 205, 205, 205, 1);
498 ip
:= IntToStr(Length(SL
)) + _lc
[I_NET_SLIST_SERVERS
];
499 e_TextureFontPrintEx(gScreenWidth
- 48 - (Length(ip
) + 1)*cw
,
500 gScreenHeight
-61, ip
, gStdFont
, 205, 205, 205, 1);
503 procedure g_Serverlist_Control(var SL
: TNetServerList
);
505 if gConsoleShow
or gChatShow
then
510 if e_KeyPressed(IK_ESCAPE
) then
513 gState
:= STATE_MENU
;
514 g_GUI_ShowWindow('MainMenu');
515 g_GUI_ShowWindow('NetGameMenu');
516 g_GUI_ShowWindow('NetClientMenu');
517 g_Sound_PlayEx(WINDOW_CLOSESOUND
);
521 if e_KeyPressed(IK_SPACE
) then
523 if not slFetched
then
525 slWaitStr
:= _lc
[I_NET_SLIST_WAIT
];
528 g_window
.ReDrawWindow
;
530 if g_Net_Slist_Fetch(SL
) then
533 slWaitStr
:= _lc
[I_NET_SLIST_NOSERVERS
];
536 slWaitStr
:= _lc
[I_NET_SLIST_ERROR
];
544 if SL
= nil then Exit
;
546 if e_KeyPressed(IK_RETURN
) or e_KeyPressed(IK_KPRETURN
) then
548 if not slReturnPressed
then
550 if SL
[slSelection
].Password
then
552 PromptIP
:= SL
[slSelection
].IP
;
553 PromptPort
:= SL
[slSelection
].Port
;
554 gState
:= STATE_MENU
;
555 g_GUI_ShowWindow('ClientPasswordMenu');
557 slReturnPressed
:= True;
561 g_Game_StartClient(SL
[slSelection
].IP
, SL
[slSelection
].Port
, '');
563 slReturnPressed
:= True;
568 slReturnPressed
:= False;
570 if e_KeyPressed(IK_DOWN
) or e_KeyPressed(IK_KPDOWN
) then
572 if not slDirPressed
then
575 if slSelection
> High(SL
) then slSelection
:= 0;
576 slDirPressed
:= True;
580 if e_KeyPressed(IK_UP
) or e_KeyPressed(IK_KPUP
) then
582 if not slDirPressed
then
584 if slSelection
= 0 then slSelection
:= Length(SL
);
587 slDirPressed
:= True;
591 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
592 slDirPressed
:= False;