26 Players
, MaxPlayers
, LocalPl
, Bots
: Byte;
30 PingAddr
: ENetAddress
;
32 pTNetServer
= ^TNetServer
;
34 TNetServerList
= array of TNetServer
;
35 pTNetServerList
= ^TNetServerList
;
38 NetMHost
: pENetHost
= nil;
39 NetMPeer
: pENetPeer
= nil;
41 slCurrent
: TNetServerList
= nil;
42 slWaitStr
: string = '';
43 slReturnPressed
: Boolean = True;
45 procedure g_Net_Slist_Set(IP
: string; Port
: Word);
46 function g_Net_Slist_Fetch(var SL
: TNetServerList
): Boolean;
47 procedure g_Net_Slist_Update();
48 procedure g_Net_Slist_Remove();
49 function g_Net_Slist_Connect(): Boolean;
50 procedure g_Net_Slist_Check();
51 procedure g_Net_Slist_Disconnect();
52 procedure g_Net_Slist_WriteInfo();
54 procedure g_Serverlist_Draw(var SL
: TNetServerList
);
55 procedure g_Serverlist_Control(var SL
: TNetServerList
);
60 SysUtils
, e_fixedbuffer
, e_input
, e_graphics
, e_log
, g_window
, g_net
, g_console
,
61 g_map
, g_game
, g_sound
, g_textures
, g_gui
, g_menu
, g_options
, g_language
, wadreader
,
66 slSelection
: Byte = 0;
67 slFetched
: Boolean = False;
68 slDirPressed
: Boolean = False;
70 function GetTimerMS(): Int64;
72 Result
:= GetTimer() {div 1000};
75 procedure PingServer(var S
: TNetServer
; Sock
: ENetSocket
);
78 Ping
: array [0..9] of Byte;
81 ClTime
:= GetTimerMS();
83 Buf
.data
:= Addr(Ping
[0]);
84 Buf
.dataLength
:= 2+8;
88 Int64(Addr(Ping
[2])^) := ClTime
;
90 enet_socket_send(Sock
, Addr(S
.PingAddr
), @Buf
, 1);
93 function g_Net_Slist_Fetch(var SL
: TNetServerList
): Boolean;
107 if (NetMHost
<> nil) or (NetMPeer
<> nil) then
110 if not g_Net_Slist_Connect
then
113 e_WriteLog('Fetching serverlist...', MSG_NOTIFY
);
114 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_FETCH
]);
116 e_Buffer_Clear(@NetOut
);
117 e_Buffer_Write(@NetOut
, Byte(NET_MMSG_GET
));
119 P
:= enet_packet_create(Addr(NetOut
.Data
), NetOut
.Len
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
120 enet_peer_send(NetMPeer
, NET_MCHAN_MAIN
, P
);
121 enet_host_flush(NetMHost
);
123 while enet_host_service(NetMHost
, @NetMEvent
, 5000) > 0 do
125 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
128 MID
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
);
130 if MID
<> NET_MMSG_GET
then continue
;
132 Cnt
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
);
133 e_WriteLog('Retrieved ' + IntToStr(Cnt
) + ' server(s).', MSG_NOTIFY
);
134 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_SLIST_RETRIEVED
], [Cnt
]), True);
140 for I
:= 0 to Cnt
- 1 do
143 SL
[I
].IP
:= e_Raw_Read_String(NetMEvent
.packet
^.data
);
144 SL
[I
].Port
:= e_Raw_Read_Word(NetMEvent
.packet
^.data
);
145 SL
[I
].Name
:= e_Raw_Read_String(NetMEvent
.packet
^.data
);
146 SL
[I
].Map
:= e_Raw_Read_String(NetMEvent
.packet
^.data
);
147 SL
[I
].GameMode
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
);
148 SL
[I
].Players
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
);
149 SL
[I
].MaxPlayers
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
);
150 SL
[I
].Protocol
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
);
151 SL
[I
].Password
:= e_Raw_Read_Byte(NetMEvent
.packet
^.data
) = 1;
152 enet_address_set_host(Addr(SL
[I
].PingAddr
), PChar(Addr(SL
[I
].IP
[1])));
154 SL
[I
].PingAddr
.port
:= SL
[I
].Port
+ 1;
163 g_Net_Slist_Disconnect
;
164 e_Buffer_Clear(@NetOut
);
166 if Length(SL
) = 0 then Exit
;
168 Sock
:= enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM
);
169 if Sock
= ENET_SOCKET_NULL
then Exit
;
170 enet_socket_set_option(Sock
, ENET_SOCKOPT_NONBLOCK
, 1);
172 for I
:= Low(SL
) to High(SL
) do
173 PingServer(SL
[I
], Sock
);
177 e_Buffer_Clear(@NetIn
);
178 Buf
.data
:= Addr(NetIn
.Data
);
179 Buf
.dataLength
:= Length(NetIn
.Data
);
181 while Cnt
< Length(SL
) do
183 if GetTimerMS() - T
> 500 then break
;
185 e_Buffer_Clear(@NetIn
);
187 RX
:= enet_socket_receive(Sock
, @SvAddr
, @Buf
, 1);
188 if RX
<= 0 then continue
;
192 if e_Buffer_Read_Char(@NetIn
) <> 'D' then continue
;
193 if e_Buffer_Read_Char(@NetIn
) <> 'F' then continue
;
195 for I
:= Low(SL
) to High(SL
) do
196 if (SL
[I
].PingAddr
.host
= SvAddr
.host
) and
197 (SL
[I
].PingAddr
.port
= SvAddr
.port
) then
201 Ping
:= e_Buffer_Read_Int64(@NetIn
);
202 Ping
:= GetTimerMS() - Ping
;
203 Name
:= e_Buffer_Read_String(@NetIn
);
204 Map
:= e_Buffer_Read_String(@NetIn
);
205 GameMode
:= e_Buffer_Read_Byte(@NetIn
);
206 Players
:= e_Buffer_Read_Byte(@NetIn
);
207 MaxPlayers
:= e_Buffer_Read_Byte(@NetIn
);
208 Protocol
:= e_Buffer_Read_Byte(@NetIn
);
209 Password
:= e_Buffer_Read_Byte(@NetIn
) = 1;
210 LocalPl
:= e_Buffer_Read_Byte(@NetIn
);
211 Bots
:= e_Buffer_Read_Word(@NetIn
);
218 enet_socket_destroy(Sock
);
221 procedure g_Net_Slist_WriteInfo();
226 g_ProcessResourceStr(gMapInfo
.Map
, @Wad
, nil, @Map
);
227 Wad
:= ExtractFileName(Wad
);
229 e_Buffer_Write(@NetOut
, NetServerName
);
231 e_Buffer_Write(@NetOut
, Wad
+ ':\' + Map
);
232 e_Buffer_Write(@NetOut
, gGameSettings
.GameMode
);
234 Cli
:= NetClientCount
;
235 e_Buffer_Write(@NetOut
, Cli
);
237 e_Buffer_Write(@NetOut
, NetMaxClients
);
239 e_Buffer_Write(@NetOut
, Byte(NET_PROTOCOL_VER
));
240 e_Buffer_Write(@NetOut
, Byte(NetPassword
<> ''));
243 procedure g_Net_Slist_Update
;
249 if (NetMHost
= nil) or (NetMPeer
= nil) then Exit
;
251 e_Buffer_Clear(@NetOut
);
252 e_Buffer_Write(@NetOut
, Byte(NET_MMSG_UPD
));
253 e_Buffer_Write(@NetOut
, NetAddr
.port
);
255 g_Net_Slist_WriteInfo();
257 P
:= enet_packet_create(Addr(NetOut
.Data
), NetOut
.Len
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
258 enet_peer_send(NetMPeer
, NET_MCHAN_UPD
, P
);
260 enet_host_flush(NetMHost
);
261 e_Buffer_Clear(@NetOut
);
264 procedure g_Net_Slist_Remove
;
268 if (NetMHost
= nil) or (NetMPeer
= nil) then Exit
;
269 e_Buffer_Clear(@NetOut
);
270 e_Buffer_Write(@NetOut
, Byte(NET_MMSG_DEL
));
271 e_Buffer_Write(@NetOut
, NetAddr
.port
);
273 P
:= enet_packet_create(Addr(NetOut
.Data
), NetOut
.Len
, Cardinal(ENET_PACKET_FLAG_RELIABLE
));
274 enet_peer_send(NetMPeer
, NET_MCHAN_MAIN
, P
);
276 enet_host_flush(NetMHost
);
277 e_Buffer_Clear(@NetOut
);
280 function g_Net_Slist_Connect
: Boolean;
284 NetMHost
:= enet_host_create(nil, 1, NET_MCHANS
, 0, 0);
285 if (NetMHost
= nil) then
287 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
291 NetMPeer
:= enet_host_connect(NetMHost
, @NetSlistAddr
, NET_MCHANS
, 0);
292 if (NetMPeer
= nil) then
294 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
295 enet_host_destroy(NetMHost
);
300 if (enet_host_service(NetMHost
, @NetMEvent
, 3000) > 0) then
301 if NetMEvent
.kind
= ENET_EVENT_TYPE_CONNECT
then
304 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_CONN
]);
308 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
309 enet_packet_destroy(NetMEvent
.packet
);
311 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_SLIST_ERROR
], True);
317 procedure g_Net_Slist_Disconnect
;
319 if (NetMHost
= nil) and (NetMPeer
= nil) then Exit
;
321 if NetMode
= NET_SERVER
then g_Net_Slist_Remove
;
323 enet_peer_disconnect(NetMPeer
, 0);
324 enet_host_flush(NetMHost
);
326 enet_peer_reset(NetMPeer
);
327 enet_host_destroy(NetMHost
);
332 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_DISC
]);
335 procedure g_Net_Slist_Check
;
337 if (NetMHost
= nil) or (NetMPeer
= nil) then Exit
;
339 while (enet_host_service(NetMHost
, @NetMEvent
, 0) > 0) do
341 if NetMEvent
.kind
= ENET_EVENT_TYPE_DISCONNECT
then
343 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_SLIST_LOST
], True);
344 if NetMPeer
<> nil then enet_peer_reset(NetMPeer
);
345 if NetMHost
<> nil then enet_host_destroy(NetMHost
);
351 if NetMEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
352 enet_packet_destroy(NetMEvent
.packet
);
356 procedure g_Net_Slist_Set(IP
: string; Port
: Word);
360 enet_address_set_host(@NetSlistAddr
, PChar(Addr(IP
[1])));
361 NetSlistAddr
.Port
:= Port
;
362 e_WriteLog('Masterserver address set to ' + IP
+ ':' + IntToStr(Port
), MSG_NOTIFY
);
366 procedure g_Serverlist_Draw(var SL
: TNetServerList
);
368 sy
, i
, y
, mw
, mx
, l
: Integer;
376 e_CharFont_GetSize(gMenuFont
, _lc
[I_NET_SLIST
], ww
, hh
);
377 e_CharFont_Print(gMenuFont
, (gScreenWidth
div 2) - (ww
div 2), 16, _lc
[I_NET_SLIST
]);
379 e_TextureFontGetSize(gStdFont
, cw
, ch
);
381 ip
:= _lc
[I_NET_SLIST_HELP
];
382 mw
:= (Length(ip
) * cw
) div 2;
384 e_DrawFillQuad(16, 64, gScreenWidth
-16, gScreenHeight
-44, 64, 64, 64, 110);
385 e_DrawQuad(16, 64, gScreenWidth
-16, gScreenHeight
-44, 255, 127, 0);
387 e_TextureFontPrintEx(gScreenWidth
div 2 - mw
, gScreenHeight
-24, ip
, gStdFont
, 225, 225, 225, 1);
391 l
:= Length(slWaitStr
) div 2;
392 e_DrawFillQuad(16, 64, gScreenWidth
-16, gScreenHeight
-44, 64, 64, 64, 128);
393 e_DrawQuad(gScreenWidth
div 2 - 192, gScreenHeight
div 2 - 10,
394 gScreenWidth
div 2 + 192, gScreenHeight
div 2 + 11, 255, 127, 0);
395 e_TextureFontPrint(gScreenWidth
div 2 - cw
* l
, gScreenHeight
div 2 - ch
div 2,
396 slWaitStr
, gStdFont
);
401 if (slSelection
< Length(SL
)) then
404 sy
:= y
+ 42 * I
- 4;
405 ip
:= _lc
[I_NET_ADDRESS
] + ' ' + SL
[I
].IP
+ ':' + IntToStr(SL
[I
].Port
);
406 if SL
[I
].Password
then
407 ip
:= ip
+ ' ' + _lc
[I_NET_SERVER_PASSWORD
] + ' ' + _lc
[I_MENU_YES
]
409 ip
:= ip
+ ' ' + _lc
[I_NET_SERVER_PASSWORD
] + ' ' + _lc
[I_MENU_NO
];
411 if Length(SL
) > 0 then
414 mw
:= (gScreenWidth
- 188);
417 e_DrawFillQuad(16 + 1, sy
, gScreenWidth
- 16 - 1, sy
+ 40, 64, 64, 64, 0);
418 e_DrawLine(1, 16 + 1, sy
, gScreenWidth
- 16 - 1, sy
, 205, 205, 205);
419 e_DrawLine(1, 16 + 1, sy
+ 41, gScreenWidth
- 16 - 1, sy
+ 41, 255, 255, 255);
421 e_DrawLine(1, 16, 85, gScreenWidth
- 16, 85, 255, 127, 0);
422 e_DrawLine(1, 16, gScreenHeight
-64, gScreenWidth
-16, gScreenHeight
-64, 255, 127, 0);
424 e_DrawLine(1, mx
- 70, 64, mx
- 70, gScreenHeight
-44, 255, 127, 0);
425 e_DrawLine(1, mx
, 64, mx
, gScreenHeight
-64, 255, 127, 0);
426 e_DrawLine(1, mx
+ 52, 64, mx
+ 52, gScreenHeight
-64, 255, 127, 0);
427 e_DrawLine(1, mx
+ 104, 64, mx
+ 104, gScreenHeight
-64, 255, 127, 0);
429 e_TextureFontPrintEx(18, 68, 'NAME/MAP', gStdFont
, 255, 127, 0, 1);
432 for I
:= 0 to High(SL
) do
434 e_TextureFontPrintEx(18, y
, SL
[I
].Name
, gStdFont
, 255, 255, 255, 1);
435 e_TextureFontPrintEx(18, y
+ 16, SL
[I
].Map
, gStdFont
, 210, 210, 210, 1);
440 e_TextureFontPrintEx(mx
- 68, 68, 'PING', gStdFont
, 255, 127, 0, 1);
442 for I
:= 0 to High(SL
) do
444 if (SL
[I
].Ping
< 0) or (SL
[I
].Ping
> 999) then
445 e_TextureFontPrintEx(mx
- 68, y
, _lc
[I_NET_SLIST_NO_ACCESS
], gStdFont
, 255, 0, 0, 1)
447 if SL
[I
].Ping
= 0 then
448 e_TextureFontPrintEx(mx
- 68, y
, '<1' + _lc
[I_NET_SLIST_PING_MS
], gStdFont
, 255, 255, 255, 1)
450 e_TextureFontPrintEx(mx
- 68, y
, IntToStr(SL
[I
].Ping
) + _lc
[I_NET_SLIST_PING_MS
], gStdFont
, 255, 255, 255, 1);
455 e_TextureFontPrintEx(mx
+ 2, 68, 'MODE', gStdFont
, 255, 127, 0, 1);
457 for I
:= 0 to High(SL
) do
459 e_TextureFontPrintEx(mx
+ 2, y
, g_Game_ModeToText(SL
[I
].GameMode
), gStdFont
, 255, 255, 255, 1);
464 e_TextureFontPrintEx(mx
+ 54, 68, 'PLRS', gStdFont
, 255, 127, 0, 1);
466 for I
:= 0 to High(SL
) do
468 e_TextureFontPrintEx(mx
+ 54, y
, IntToStr(SL
[I
].Players
) + '/' + IntToStr(SL
[I
].MaxPlayers
), gStdFont
, 255, 255, 255, 1);
469 e_TextureFontPrintEx(mx
+ 54, y
+ 16, IntToStr(SL
[I
].LocalPl
) + '+' + IntToStr(SL
[I
].Bots
), gStdFont
, 210, 210, 210, 1);
473 e_TextureFontPrintEx(mx
+ 106, 68, 'VER', gStdFont
, 255, 127, 0, 1);
475 for I
:= 0 to High(SL
) do
477 e_TextureFontPrintEx(mx
+ 106, y
, IntToStr(SL
[I
].Protocol
), gStdFont
, 255, 255, 255, 1);
482 e_TextureFontPrintEx(20, gScreenHeight
-61, ip
, gStdFont
, 205, 205, 205, 1);
483 ip
:= IntToStr(Length(SL
)) + _lc
[I_NET_SLIST_SERVERS
];
484 e_TextureFontPrintEx(gScreenWidth
- 48 - (Length(ip
) + 1)*cw
,
485 gScreenHeight
-61, ip
, gStdFont
, 205, 205, 205, 1);
488 procedure g_Serverlist_Control(var SL
: TNetServerList
);
490 if gConsoleShow
or gChatShow
then
495 if e_KeyPressed(IK_ESCAPE
) then
498 gState
:= STATE_MENU
;
499 g_GUI_ShowWindow('MainMenu');
500 g_GUI_ShowWindow('NetGameMenu');
501 g_GUI_ShowWindow('NetClientMenu');
502 g_Sound_PlayEx(WINDOW_CLOSESOUND
);
506 if e_KeyPressed(IK_SPACE
) then
508 if not slFetched
then
510 slWaitStr
:= _lc
[I_NET_SLIST_WAIT
];
513 g_window
.ReDrawWindow
;
515 if g_Net_Slist_Fetch(SL
) then
518 slWaitStr
:= _lc
[I_NET_SLIST_NOSERVERS
];
521 slWaitStr
:= _lc
[I_NET_SLIST_ERROR
];
529 if SL
= nil then Exit
;
531 if e_KeyPressed(IK_RETURN
) or e_KeyPressed(IK_KPRETURN
) then
533 if not slReturnPressed
then
535 if SL
[slSelection
].Password
then
537 PromptIP
:= SL
[slSelection
].IP
;
538 PromptPort
:= SL
[slSelection
].Port
;
539 gState
:= STATE_MENU
;
540 g_GUI_ShowWindow('ClientPasswordMenu');
542 slReturnPressed
:= True;
546 g_Game_StartClient(SL
[slSelection
].IP
, SL
[slSelection
].Port
, '');
548 slReturnPressed
:= True;
553 slReturnPressed
:= False;
555 if e_KeyPressed(IK_DOWN
) or e_KeyPressed(IK_KPDOWN
) then
557 if not slDirPressed
then
560 if slSelection
> High(SL
) then slSelection
:= 0;
561 slDirPressed
:= True;
565 if e_KeyPressed(IK_UP
) or e_KeyPressed(IK_KPUP
) then
567 if not slDirPressed
then
569 if slSelection
= 0 then slSelection
:= Length(SL
);
572 slDirPressed
:= True;
576 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
577 slDirPressed
:= False;