25 Players
, MaxPlayers
, LocalPl
, Bots
: Byte;
29 PingAddr
: ENetAddress
;
31 pTNetServer
= ^TNetServer
;
33 TNetServerList
= array of TNetServer
;
34 pTNetServerList
= ^TNetServerList
;
37 NetMHost
: pENetHost
= nil;
38 NetMPeer
: pENetPeer
= nil;
40 slCurrent
: TNetServerList
= nil;
41 slWaitStr
: string = '';
42 slReturnPressed
: Boolean = True;
44 procedure g_Net_Slist_Set(IP
: string; Port
: Word);
45 function g_Net_Slist_Fetch(var SL
: TNetServerList
): Boolean;
46 procedure g_Net_Slist_Update();
47 procedure g_Net_Slist_Remove();
48 function g_Net_Slist_Connect(): Boolean;
49 procedure g_Net_Slist_Check();
50 procedure g_Net_Slist_Disconnect();
51 procedure g_Net_Slist_WriteInfo();
53 procedure g_Serverlist_Draw(var SL
: TNetServerList
);
54 procedure g_Serverlist_Control(var SL
: TNetServerList
);
59 SysUtils
, e_fixedbuffer
, e_input
, e_graphics
, e_log
, g_window
, g_net
, g_console
,
60 g_map
, g_game
, g_sound
, g_textures
, g_gui
, g_menu
, g_options
, g_language
, WADEDITOR
,
65 slSelection
: Byte = 0;
66 slFetched
: Boolean = False;
67 slDirPressed
: Boolean = False;
69 function GetTimerMS(): Integer;
71 Result
:= GetTimer() div 1000;
74 procedure PingServer(var S
: TNetServer
; Sock
: ENetSocket
);
77 Ping
: array [0..5] of Byte;
80 ClTime
:= GetTimerMS();
82 Buf
.data
:= Addr(Ping
[0]);
87 LongInt(Addr(Ping
[2])^) := ClTime
;
89 enet_socket_send(Sock
, Addr(S
.PingAddr
), @Buf
, 1);
92 function g_Net_Slist_Fetch(var SL
: TNetServerList
): Boolean;
105 if (NetMHost
<> nil) or (NetMPeer
<> nil) then
108 if not g_Net_Slist_Connect
then
111 e_WriteLog('Fetching serverlist...', MSG_NOTIFY
);
112 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_FETCH
]);
114 e_Buffer_Clear(@NetOut
);
115 e_Buffer_Write(@NetOut
, Byte(NET_MMSG_GET
));
117 P
:= enet_packet_create(Addr(NetOut
.Data
), NetOut
.Len
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
118 enet_peer_send(NetMPeer
, NET_MCHAN_MAIN
, P
);
119 enet_host_flush(NetMHost
);
121 while enet_host_service(NetMHost
, @NetMEvent
, 5000) > 0 do
123 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
126 MID
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
);
128 if MID
<> NET_MMSG_GET
then continue
;
130 Cnt
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
);
131 e_WriteLog('Retrieved ' + IntToStr(Cnt
) + ' server(s).', MSG_NOTIFY
);
132 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_SLIST_RETRIEVED
], [Cnt
]), True);
138 for I
:= 0 to Cnt
- 1 do
141 SL
[I
].IP
:= e_Raw_Read_String(NetMEvent
.packet
^.data
);
142 SL
[I
].Port
:= e_Raw_Read_Word(NetMEvent
.packet
^.data
);
143 SL
[I
].Name
:= e_Raw_Read_String(NetMEvent
.packet
^.data
);
144 SL
[I
].Map
:= e_Raw_Read_String(NetMEvent
.packet
^.data
);
145 SL
[I
].GameMode
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
);
146 SL
[I
].Players
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
);
147 SL
[I
].MaxPlayers
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
);
148 SL
[I
].Protocol
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
);
149 SL
[I
].Password
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
) = 1;
150 enet_address_set_host(Addr(SL
[I
].PingAddr
), PChar(Addr(SL
[I
].IP
[1])));
152 SL
[I
].PingAddr
.port
:= SL
[I
].Port
+ 1;
161 g_Net_Slist_Disconnect
;
162 e_Buffer_Clear(@NetOut
);
164 if Length(SL
) = 0 then Exit
;
166 Sock
:= enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM
);
167 if Sock
= ENET_SOCKET_NULL
then Exit
;
168 enet_socket_set_option(Sock
, ENET_SOCKOPT_NONBLOCK
, 1);
170 for I
:= Low(SL
) to High(SL
) do
171 PingServer(SL
[I
], Sock
);
175 e_Buffer_Clear(@NetIn
);
176 Buf
.data
:= Addr(NetIn
.Data
);
177 Buf
.dataLength
:= Length(NetIn
.Data
);
179 while Cnt
< Length(SL
) do
181 if GetTimerMS() - T
> 500 then break
;
183 e_Buffer_Clear(@NetIn
);
185 RX
:= enet_socket_receive(Sock
, @SvAddr
, @Buf
, 1);
186 if RX
<= 0 then continue
;
190 if e_Buffer_Read_Char(@NetIn
) <> 'D' then continue
;
191 if e_Buffer_Read_Char(@NetIn
) <> 'F' then continue
;
193 for I
:= Low(SL
) to High(SL
) do
194 if (SL
[I
].PingAddr
.host
= SvAddr
.host
) and
195 (SL
[I
].PingAddr
.port
= SvAddr
.port
) then
199 Ping
:= e_Buffer_Read_LongInt(@NetIn
);
200 Ping
:= GetTimerMS() - Ping
;
201 Name
:= e_Buffer_Read_String(@NetIn
);
202 Map
:= e_Buffer_Read_String(@NetIn
);
203 GameMode
:= e_Buffer_Read_Byte(@NetIn
);
204 Players
:= e_Buffer_Read_Byte(@NetIn
);
205 MaxPlayers
:= e_Buffer_Read_Byte(@NetIn
);
206 Protocol
:= e_Buffer_Read_Byte(@NetIn
);
207 Password
:= e_Buffer_Read_Byte(@NetIn
) = 1;
208 LocalPl
:= e_Buffer_Read_Byte(@NetIn
);
209 Bots
:= e_Buffer_Read_Word(@NetIn
);
216 enet_socket_destroy(Sock
);
219 procedure g_Net_Slist_WriteInfo();
224 g_ProcessResourceStr(gMapInfo
.Map
, @Wad
, nil, @Map
);
225 Wad
:= ExtractFileName(Wad
);
227 e_Buffer_Write(@NetOut
, NetServerName
);
229 e_Buffer_Write(@NetOut
, Wad
+ ':\' + Map
);
230 e_Buffer_Write(@NetOut
, gGameSettings
.GameMode
);
232 Cli
:= NetClientCount
;
233 e_Buffer_Write(@NetOut
, Cli
);
235 e_Buffer_Write(@NetOut
, NetMaxClients
);
237 e_Buffer_Write(@NetOut
, Byte(NET_PROTOCOL_VER
));
238 e_Buffer_Write(@NetOut
, Byte(NetPassword
<> ''));
241 procedure g_Net_Slist_Update
;
247 if (NetMHost
= nil) or (NetMPeer
= nil) then Exit
;
249 e_Buffer_Clear(@NetOut
);
250 e_Buffer_Write(@NetOut
, Byte(NET_MMSG_UPD
));
251 e_Buffer_Write(@NetOut
, NetAddr
.port
);
253 g_Net_Slist_WriteInfo();
255 P
:= enet_packet_create(Addr(NetOut
.Data
), NetOut
.Len
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
256 enet_peer_send(NetMPeer
, NET_MCHAN_UPD
, P
);
258 enet_host_flush(NetMHost
);
259 e_Buffer_Clear(@NetOut
);
262 procedure g_Net_Slist_Remove
;
266 if (NetMHost
= nil) or (NetMPeer
= nil) then Exit
;
267 e_Buffer_Clear(@NetOut
);
268 e_Buffer_Write(@NetOut
, Byte(NET_MMSG_DEL
));
269 e_Buffer_Write(@NetOut
, NetAddr
.port
);
271 P
:= enet_packet_create(Addr(NetOut
.Data
), NetOut
.Len
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
272 enet_peer_send(NetMPeer
, NET_MCHAN_MAIN
, P
);
274 enet_host_flush(NetMHost
);
275 e_Buffer_Clear(@NetOut
);
278 function g_Net_Slist_Connect
: Boolean;
282 NetMHost
:= enet_host_create(nil, 1, NET_MCHANS
, 0, 0);
283 if (NetMHost
= nil) then
285 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
289 NetMPeer
:= enet_host_connect(NetMHost
, @NetSlistAddr
, NET_MCHANS
, 0);
290 if (NetMPeer
= nil) then
292 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
293 enet_host_destroy(NetMHost
);
298 if (enet_host_service(NetMHost
, @NetMEvent
, 3000) > 0) then
299 if NetMEvent
.kind
= ENET_EVENT_TYPE_CONNECT
then
302 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_CONN
]);
306 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
307 enet_packet_destroy(NetMEvent
.packet
);
309 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_SLIST_ERROR
], True);
315 procedure g_Net_Slist_Disconnect
;
317 if (NetMHost
= nil) and (NetMPeer
= nil) then Exit
;
319 if NetMode
= NET_SERVER
then g_Net_Slist_Remove
;
321 enet_peer_disconnect(NetMPeer
, 0);
322 enet_host_flush(NetMHost
);
324 enet_peer_reset(NetMPeer
);
325 enet_host_destroy(NetMHost
);
330 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_DISC
]);
333 procedure g_Net_Slist_Check
;
335 if (NetMHost
= nil) or (NetMPeer
= nil) then Exit
;
337 while (enet_host_service(NetMHost
, @NetMEvent
, 0) > 0) do
339 if NetMEvent
.kind
= ENET_EVENT_TYPE_DISCONNECT
then
341 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_LOST
], True);
342 if NetMPeer
<> nil then enet_peer_reset(NetMPeer
);
343 if NetMHost
<> nil then enet_host_destroy(NetMHost
);
349 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
350 enet_packet_destroy(NetMEvent
.packet
);
354 procedure g_Net_Slist_Set(IP
: string; Port
: Word);
358 enet_address_set_host(@NetSlistAddr
, PChar(Addr(IP
[1])));
359 NetSlistAddr
.Port
:= Port
;
360 e_WriteLog('Masterserver address set to ' + IP
+ ':' + IntToStr(Port
), MSG_NOTIFY
);
364 procedure g_Serverlist_Draw(var SL
: TNetServerList
);
366 sy
, i
, y
, mw
, mx
, l
: Integer;
374 e_CharFont_GetSize(gMenuFont
, _lc
[I_NET_SLIST
], ww
, hh
);
375 e_CharFont_Print(gMenuFont
, (gScreenWidth
div 2) - (ww
div 2), 16, _lc
[I_NET_SLIST
]);
377 e_TextureFontGetSize(gStdFont
, cw
, ch
);
379 ip
:= _lc
[I_NET_SLIST_HELP
];
380 mw
:= (Length(ip
) * cw
) div 2;
382 e_DrawFillQuad(16, 64, gScreenWidth
-16, gScreenHeight
-44, 64, 64, 64, 110);
383 e_DrawQuad(16, 64, gScreenWidth
-16, gScreenHeight
-44, 255, 127, 0);
385 e_TextureFontPrintEx(gScreenWidth
div 2 - mw
, gScreenHeight
-24, ip
, gStdFont
, 225, 225, 225, 1);
389 l
:= Length(slWaitStr
) div 2;
390 e_DrawFillQuad(16, 64, gScreenWidth
-16, gScreenHeight
-44, 64, 64, 64, 128);
391 e_DrawQuad(gScreenWidth
div 2 - 192, gScreenHeight
div 2 - 10,
392 gScreenWidth
div 2 + 192, gScreenHeight
div 2 + 11, 255, 127, 0);
393 e_TextureFontPrint(gScreenWidth
div 2 - cw
* l
, gScreenHeight
div 2 - ch
div 2,
394 slWaitStr
, gStdFont
);
399 if (slSelection
< Length(SL
)) then
402 sy
:= y
+ 42 * I
- 4;
403 ip
:= _lc
[I_NET_ADDRESS
] + ' ' + SL
[I
].IP
+ ':' + IntToStr(SL
[I
].Port
);
404 if SL
[I
].Password
then
405 ip
:= ip
+ ' ' + _lc
[I_NET_SERVER_PASSWORD
] + ' ' + _lc
[I_MENU_YES
]
407 ip
:= ip
+ ' ' + _lc
[I_NET_SERVER_PASSWORD
] + ' ' + _lc
[I_MENU_NO
];
409 if Length(SL
) > 0 then
412 mw
:= (gScreenWidth
- 188);
415 e_DrawFillQuad(16 + 1, sy
, gScreenWidth
- 16 - 1, sy
+ 40, 64, 64, 64, 0);
416 e_DrawLine(1, 16 + 1, sy
, gScreenWidth
- 16 - 1, sy
, 205, 205, 205);
417 e_DrawLine(1, 16 + 1, sy
+ 41, gScreenWidth
- 16 - 1, sy
+ 41, 255, 255, 255);
419 e_DrawLine(1, 16, 85, gScreenWidth
- 16, 85, 255, 127, 0);
420 e_DrawLine(1, 16, gScreenHeight
-64, gScreenWidth
-16, gScreenHeight
-64, 255, 127, 0);
422 e_DrawLine(1, mx
- 70, 64, mx
- 70, gScreenHeight
-44, 255, 127, 0);
423 e_DrawLine(1, mx
, 64, mx
, gScreenHeight
-64, 255, 127, 0);
424 e_DrawLine(1, mx
+ 52, 64, mx
+ 52, gScreenHeight
-64, 255, 127, 0);
425 e_DrawLine(1, mx
+ 104, 64, mx
+ 104, gScreenHeight
-64, 255, 127, 0);
427 e_TextureFontPrintEx(18, 68, 'NAME/MAP', gStdFont
, 255, 127, 0, 1);
430 for I
:= 0 to High(SL
) do
432 e_TextureFontPrintEx(18, y
, SL
[I
].Name
, gStdFont
, 255, 255, 255, 1);
433 e_TextureFontPrintEx(18, y
+ 16, SL
[I
].Map
, gStdFont
, 210, 210, 210, 1);
438 e_TextureFontPrintEx(mx
- 68, 68, 'PING', gStdFont
, 255, 127, 0, 1);
440 for I
:= 0 to High(SL
) do
442 if (SL
[I
].Ping
< 0) or (SL
[I
].Ping
> 999) then
443 e_TextureFontPrintEx(mx
- 68, y
, _lc
[I_NET_SLIST_NO_ACCESS
], gStdFont
, 255, 0, 0, 1)
445 if SL
[I
].Ping
= 0 then
446 e_TextureFontPrintEx(mx
- 68, y
, '<1' + _lc
[I_NET_SLIST_PING_MS
], gStdFont
, 255, 255, 255, 1)
448 e_TextureFontPrintEx(mx
- 68, y
, IntToStr(SL
[I
].Ping
) + _lc
[I_NET_SLIST_PING_MS
], gStdFont
, 255, 255, 255, 1);
453 e_TextureFontPrintEx(mx
+ 2, 68, 'MODE', gStdFont
, 255, 127, 0, 1);
455 for I
:= 0 to High(SL
) do
457 e_TextureFontPrintEx(mx
+ 2, y
, g_Game_ModeToText(SL
[I
].GameMode
), gStdFont
, 255, 255, 255, 1);
462 e_TextureFontPrintEx(mx
+ 54, 68, 'PLRS', gStdFont
, 255, 127, 0, 1);
464 for I
:= 0 to High(SL
) do
466 e_TextureFontPrintEx(mx
+ 54, y
, IntToStr(SL
[I
].Players
) + '/' + IntToStr(SL
[I
].MaxPlayers
), gStdFont
, 255, 255, 255, 1);
467 e_TextureFontPrintEx(mx
+ 54, y
+ 16, IntToStr(SL
[I
].LocalPl
) + '+' + IntToStr(SL
[I
].Bots
), gStdFont
, 210, 210, 210, 1);
471 e_TextureFontPrintEx(mx
+ 106, 68, 'VER', gStdFont
, 255, 127, 0, 1);
473 for I
:= 0 to High(SL
) do
475 e_TextureFontPrintEx(mx
+ 106, y
, IntToStr(SL
[I
].Protocol
), gStdFont
, 255, 255, 255, 1);
480 e_TextureFontPrintEx(20, gScreenHeight
-61, ip
, gStdFont
, 205, 205, 205, 1);
481 ip
:= IntToStr(Length(SL
)) + _lc
[I_NET_SLIST_SERVERS
];
482 e_TextureFontPrintEx(gScreenWidth
- 48 - (Length(ip
) + 1)*cw
,
483 gScreenHeight
-61, ip
, gStdFont
, 205, 205, 205, 1);
486 procedure g_Serverlist_Control(var SL
: TNetServerList
);
488 if gConsoleShow
or gChatShow
then
493 if e_KeyPressed(IK_ESCAPE
) then
496 gState
:= STATE_MENU
;
497 g_GUI_ShowWindow('MainMenu');
498 g_GUI_ShowWindow('NetGameMenu');
499 g_GUI_ShowWindow('NetClientMenu');
500 g_Sound_PlayEx(WINDOW_CLOSESOUND
);
504 if e_KeyPressed(IK_SPACE
) then
506 if not slFetched
then
508 slWaitStr
:= _lc
[I_NET_SLIST_WAIT
];
511 g_window
.ReDrawWindow
;
513 if g_Net_Slist_Fetch(SL
) then
516 slWaitStr
:= _lc
[I_NET_SLIST_NOSERVERS
];
519 slWaitStr
:= _lc
[I_NET_SLIST_ERROR
];
527 if SL
= nil then Exit
;
529 if e_KeyPressed(IK_RETURN
) or e_KeyPressed(IK_KPRETURN
) then
531 if not slReturnPressed
then
533 if SL
[slSelection
].Password
then
535 PromptIP
:= SL
[slSelection
].IP
;
536 PromptPort
:= SL
[slSelection
].Port
;
537 gState
:= STATE_MENU
;
538 g_GUI_ShowWindow('ClientPasswordMenu');
540 slReturnPressed
:= True;
544 g_Game_StartClient(SL
[slSelection
].IP
, SL
[slSelection
].Port
, '');
546 slReturnPressed
:= True;
551 slReturnPressed
:= False;
553 if e_KeyPressed(IK_DOWN
) or e_KeyPressed(IK_KPDOWN
) then
555 if not slDirPressed
then
558 if slSelection
> High(SL
) then slSelection
:= 0;
559 slDirPressed
:= True;
563 if e_KeyPressed(IK_UP
) or e_KeyPressed(IK_KPUP
) then
565 if not slDirPressed
then
567 if slSelection
= 0 then slSelection
:= Length(SL
);
570 slDirPressed
:= True;
574 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
575 slDirPressed
:= False;