DEADSOFTWARE

925b910241c91bcdda8a5a4e2f7e645441c7fe5a
[d2df-sdl.git] / src / game / g_netmaster.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
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, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 unit g_netmaster;
18 interface
20 uses ENet;
22 const
23 NET_MCHANS = 2;
25 NET_MCHAN_MAIN = 0;
26 NET_MCHAN_UPD = 1;
28 NET_MMSG_UPD = 200;
29 NET_MMSG_DEL = 201;
30 NET_MMSG_GET = 202;
32 type
33 TNetServer = record
34 Number: Byte;
35 Protocol: Byte;
36 Name: string;
37 IP: string;
38 Port: Word;
39 Map: string;
40 Players, MaxPlayers, LocalPl, Bots: Byte;
41 Ping: Int64;
42 GameMode: Byte;
43 Password: Boolean;
44 PingAddr: ENetAddress;
45 end;
46 pTNetServer = ^TNetServer;
47 TNetServerRow = record
48 Indices: Array of Integer;
49 Current: Integer;
50 end;
52 TNetServerList = array of TNetServer;
53 pTNetServerList = ^TNetServerList;
54 TNetServerTable = array of TNetServerRow;
56 var
57 NetMHost: pENetHost = nil;
58 NetMPeer: pENetPeer = nil;
60 slCurrent: TNetServerList = nil;
61 slTable: TNetServerTable = nil;
62 slWaitStr: string = '';
63 slReturnPressed: Boolean = True;
65 slMOTD: string = '';
66 slUrgent: string = '';
68 procedure g_Net_Slist_Set(IP: string; Port: Word);
69 function g_Net_Slist_Fetch(var SL: TNetServerList): Boolean;
70 procedure g_Net_Slist_Update();
71 procedure g_Net_Slist_Remove();
72 function g_Net_Slist_Connect(blocking: Boolean=True): Boolean;
73 procedure g_Net_Slist_Check();
74 procedure g_Net_Slist_Disconnect();
75 procedure g_Net_Slist_WriteInfo();
77 procedure g_Serverlist_GenerateTable(SL: TNetServerList; var ST: TNetServerTable);
78 procedure g_Serverlist_Draw(var SL: TNetServerList; var ST: TNetServerTable);
79 procedure g_Serverlist_Control(var SL: TNetServerList; var ST: TNetServerTable);
81 function GetTimerMS(): Int64;
84 implementation
86 uses
87 SysUtils, e_msg, e_input, e_graphics, e_log, g_window, g_net, g_console,
88 g_map, g_game, g_sound, g_gui, g_menu, g_options, g_language, g_basic,
89 wadreader;
91 var
92 NetMEvent: ENetEvent;
93 slSelection: Byte = 0;
94 slFetched: Boolean = False;
95 slDirPressed: Boolean = False;
96 slReadUrgent: Boolean = False;
97 // inside the game, calling `g_Net_Slist_Connect()` is disasterous, as it is blocking.
98 // so we'll use this variable to indicate if "connected" event is received.
99 NetHostConnected: Boolean = false;
100 NetHostConReqTime: Int64 = 0; // to timeout `connect`
102 function GetTimerMS(): Int64;
103 begin
104 Result := GetTimer() {div 1000};
105 end;
107 procedure PingServer(var S: TNetServer; Sock: ENetSocket);
108 var
109 Buf: ENetBuffer;
110 Ping: array [0..9] of Byte;
111 ClTime: Int64;
112 begin
113 ClTime := GetTimerMS();
115 Buf.data := Addr(Ping[0]);
116 Buf.dataLength := 2+8;
118 Ping[0] := Ord('D');
119 Ping[1] := Ord('F');
120 Int64(Addr(Ping[2])^) := ClTime;
122 enet_socket_send(Sock, Addr(S.PingAddr), @Buf, 1);
123 end;
125 procedure PingBcast(Sock: ENetSocket);
126 var
127 S: TNetServer;
128 begin
129 S.IP := '255.255.255.255';
130 S.Port := NET_PING_PORT;
131 enet_address_set_host(Addr(S.PingAddr), PChar(Addr(S.IP[1])));
132 S.Ping := -1;
133 S.PingAddr.port := S.Port;
134 PingServer(S, Sock);
135 end;
137 function g_Net_Slist_Fetch(var SL: TNetServerList): Boolean;
138 var
139 Cnt: Byte;
140 P: pENetPacket;
141 MID: Byte;
142 I, RX: Integer;
143 T: Int64;
144 Sock: ENetSocket;
145 Buf: ENetBuffer;
146 InMsg: TMsg;
147 SvAddr: ENetAddress;
148 FromSL: Boolean;
149 MyVer, Str: string;
151 procedure ProcessLocal();
152 begin
153 I := Length(SL);
154 SetLength(SL, I + 1);
155 with SL[I] do
156 begin
157 IP := DecodeIPV4(SvAddr.host);
158 Port := InMsg.ReadWord();
159 Ping := InMsg.ReadInt64();
160 Ping := GetTimerMS() - Ping;
161 Name := InMsg.ReadString();
162 Map := InMsg.ReadString();
163 GameMode := InMsg.ReadByte();
164 Players := InMsg.ReadByte();
165 MaxPlayers := InMsg.ReadByte();
166 Protocol := InMsg.ReadByte();
167 Password := InMsg.ReadByte() = 1;
168 LocalPl := InMsg.ReadByte();
169 Bots := InMsg.ReadWord();
170 end;
171 end;
172 procedure CheckLocalServers();
173 begin
174 SetLength(SL, 0);
176 Sock := enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM);
177 if Sock = ENET_SOCKET_NULL then Exit;
178 enet_socket_set_option(Sock, ENET_SOCKOPT_NONBLOCK, 1);
179 enet_socket_set_option(Sock, ENET_SOCKOPT_BROADCAST, 1);
180 PingBcast(Sock);
182 T := GetTimerMS();
184 InMsg.Alloc(NET_BUFSIZE);
185 Buf.data := InMsg.Data;
186 Buf.dataLength := InMsg.MaxSize;
187 while GetTimerMS() - T <= 500 do
188 begin
189 InMsg.Clear();
191 RX := enet_socket_receive(Sock, @SvAddr, @Buf, 1);
192 if RX <= 0 then continue;
193 InMsg.CurSize := RX;
195 InMsg.BeginReading();
197 if InMsg.ReadChar() <> 'D' then continue;
198 if InMsg.ReadChar() <> 'F' then continue;
200 ProcessLocal();
201 end;
203 InMsg.Free();
204 enet_socket_destroy(Sock);
206 if Length(SL) = 0 then SL := nil;
207 end;
208 begin
209 Result := False;
210 SL := nil;
212 if (NetMHost <> nil) or (NetMPeer <> nil) then
213 begin
214 CheckLocalServers();
215 Exit;
216 end;
218 if not g_Net_Slist_Connect then
219 begin
220 CheckLocalServers();
221 Exit;
222 end;
224 e_WriteLog('Fetching serverlist...', TMsgType.Notify);
225 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_FETCH]);
227 NetOut.Clear();
228 NetOut.Write(Byte(NET_MMSG_GET));
230 // TODO: what should we identify the build with?
231 MyVer := GAME_VERSION;
232 NetOut.Write(MyVer);
234 P := enet_packet_create(NetOut.Data, NetOut.CurSize, Cardinal(ENET_PACKET_FLAG_RELIABLE));
235 enet_peer_send(NetMPeer, NET_MCHAN_MAIN, P);
236 enet_host_flush(NetMHost);
238 while enet_host_service(NetMHost, @NetMEvent, 5000) > 0 do
239 begin
240 if NetMEvent.kind = ENET_EVENT_TYPE_RECEIVE then
241 begin
242 if not InMsg.Init(NetMEvent.packet^.data, NetMEvent.packet^.dataLength, True) then continue;
244 MID := InMsg.ReadByte();
246 if MID <> NET_MMSG_GET then continue;
248 Cnt := InMsg.ReadByte();
249 g_Console_Add(_lc[I_NET_MSG] + Format(_lc[I_NET_SLIST_RETRIEVED], [Cnt]), True);
251 if Cnt > 0 then
252 begin
253 SetLength(SL, Cnt);
255 for I := 0 to Cnt - 1 do
256 begin
257 SL[I].Number := I;
258 SL[I].IP := InMsg.ReadString();
259 SL[I].Port := InMsg.ReadWord();
260 SL[I].Name := InMsg.ReadString();
261 SL[I].Map := InMsg.ReadString();
262 SL[I].GameMode := InMsg.ReadByte();
263 SL[I].Players := InMsg.ReadByte();
264 SL[I].MaxPlayers := InMsg.ReadByte();
265 SL[I].Protocol := InMsg.ReadByte();
266 SL[I].Password := InMsg.ReadByte() = 1;
267 enet_address_set_host(Addr(SL[I].PingAddr), PChar(Addr(SL[I].IP[1])));
268 SL[I].Ping := -1;
269 SL[I].PingAddr.port := NET_PING_PORT;
270 end;
271 end;
273 if InMsg.ReadCount < InMsg.CurSize then
274 begin
275 // new master, supports version reports
276 Str := InMsg.ReadString();
277 if (Str <> MyVer) then
278 begin
279 { TODO }
280 g_Console_Add('!!! UpdVer = `' + Str + '`');
281 end;
282 // even newer master, supports extra info
283 if InMsg.ReadCount < InMsg.CurSize then
284 begin
285 slMOTD := b_Text_Format(InMsg.ReadString());
286 Str := b_Text_Format(InMsg.ReadString());
287 // check if the message has updated and the user has to read it again
288 if slUrgent <> Str then slReadUrgent := False;
289 slUrgent := Str;
290 end;
291 end;
293 Result := True;
294 break;
295 end;
296 end;
298 g_Net_Slist_Disconnect;
299 NetOut.Clear();
301 if Length(SL) = 0 then
302 begin
303 CheckLocalServers();
304 Exit;
305 end;
307 Sock := enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM);
308 if Sock = ENET_SOCKET_NULL then Exit;
309 enet_socket_set_option(Sock, ENET_SOCKOPT_NONBLOCK, 1);
311 for I := Low(SL) to High(SL) do
312 PingServer(SL[I], Sock);
314 enet_socket_set_option(Sock, ENET_SOCKOPT_BROADCAST, 1);
315 PingBcast(Sock);
317 T := GetTimerMS();
319 InMsg.Alloc(NET_BUFSIZE);
320 Buf.data := InMsg.Data;
321 Buf.dataLength := InMsg.MaxSize;
322 Cnt := 0;
323 while GetTimerMS() - T <= 500 do
324 begin
325 InMsg.Clear();
327 RX := enet_socket_receive(Sock, @SvAddr, @Buf, 1);
328 if RX <= 0 then continue;
329 InMsg.CurSize := RX;
331 InMsg.BeginReading();
333 if InMsg.ReadChar() <> 'D' then continue;
334 if InMsg.ReadChar() <> 'F' then continue;
336 FromSL := False;
337 for I := Low(SL) to High(SL) do
338 if (SL[I].PingAddr.host = SvAddr.host) and
339 (SL[I].PingAddr.port = SvAddr.port) then
340 begin
341 with SL[I] do
342 begin
343 Port := InMsg.ReadWord();
344 Ping := InMsg.ReadInt64();
345 Ping := GetTimerMS() - Ping;
346 Name := InMsg.ReadString();
347 Map := InMsg.ReadString();
348 GameMode := InMsg.ReadByte();
349 Players := InMsg.ReadByte();
350 MaxPlayers := InMsg.ReadByte();
351 Protocol := InMsg.ReadByte();
352 Password := InMsg.ReadByte() = 1;
353 LocalPl := InMsg.ReadByte();
354 Bots := InMsg.ReadWord();
355 end;
356 FromSL := True;
357 Inc(Cnt);
358 break;
359 end;
360 if not FromSL then
361 ProcessLocal();
362 end;
364 InMsg.Free();
365 enet_socket_destroy(Sock);
366 end;
368 procedure g_Net_Slist_WriteInfo();
369 var
370 Wad, Map: string;
371 Cli: Byte;
372 begin
373 Wad := g_ExtractWadNameNoPath(gMapInfo.Map);
374 Map := g_ExtractFileName(gMapInfo.Map);
376 NetOut.Write(NetServerName);
378 NetOut.Write(Wad + ':\' + Map);
379 NetOut.Write(gGameSettings.GameMode);
381 Cli := NetClientCount;
382 NetOut.Write(Cli);
384 NetOut.Write(NetMaxClients);
386 NetOut.Write(Byte(NET_PROTOCOL_VER));
387 NetOut.Write(Byte(NetPassword <> ''));
388 end;
390 procedure g_Net_Slist_Update;
391 var
392 P: pENetPacket;
393 ct: Int64;
394 begin
395 if (NetMHost = nil) or (NetMPeer = nil) then exit;
397 // we are waiting for connection
398 if (not NetHostConnected) then
399 begin
400 // check for connection packet
401 if (enet_host_service(NetMHost, @NetMEvent, 0) > 0) then
402 begin
403 if NetMEvent.kind = ENET_EVENT_TYPE_CONNECT then
404 begin
405 NetHostConnected := True;
406 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_CONN]);
407 end
408 else
409 begin
410 if NetMEvent.kind = ENET_EVENT_TYPE_RECEIVE then enet_packet_destroy(NetMEvent.packet);
411 end;
412 end;
413 // check for connection timeout
414 if (not NetHostConnected) then
415 begin
416 ct := GetTimerMS();
417 if (ct-NetHostConReqTime >= 3000) then
418 begin
419 // do not spam with error messages, it looks like the master is down
420 //g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_SLIST_ERROR], True);
421 g_Net_Slist_Disconnect();
422 exit;
423 end;
424 end;
425 end;
427 NetOut.Clear();
428 NetOut.Write(Byte(NET_MMSG_UPD));
429 NetOut.Write(NetAddr.port);
431 g_Net_Slist_WriteInfo();
433 P := enet_packet_create(NetOut.Data, NetOut.CurSize, Cardinal(ENET_PACKET_FLAG_RELIABLE));
434 enet_peer_send(NetMPeer, NET_MCHAN_UPD, P);
436 enet_host_flush(NetMHost);
437 NetOut.Clear();
438 end;
440 procedure g_Net_Slist_Remove;
441 var
442 P: pENetPacket;
443 begin
444 if (NetMHost = nil) or (NetMPeer = nil) or (not NetHostConnected) then Exit;
445 NetOut.Clear();
446 NetOut.Write(Byte(NET_MMSG_DEL));
447 NetOut.Write(NetAddr.port);
449 P := enet_packet_create(NetOut.Data, NetOut.CurSize, Cardinal(ENET_PACKET_FLAG_RELIABLE));
450 enet_peer_send(NetMPeer, NET_MCHAN_MAIN, P);
452 enet_host_flush(NetMHost);
453 NetOut.Clear();
454 end;
456 function g_Net_Slist_Connect (blocking: Boolean=True): Boolean;
457 var
458 delay: Integer;
459 begin
460 Result := False;
461 NetHostConnected := False; // just in case
462 NetHostConReqTime := 0; // just in case
464 NetMHost := enet_host_create(nil, 1, NET_MCHANS, 0, 0);
465 if (NetMHost = nil) then
466 begin
467 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CLIENT], True);
468 Exit;
469 end;
471 NetMPeer := enet_host_connect(NetMHost, @NetSlistAddr, NET_MCHANS, 0);
472 if (NetMPeer = nil) then
473 begin
474 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CLIENT], True);
475 enet_host_destroy(NetMHost);
476 NetMHost := nil;
477 Exit;
478 end;
480 if (blocking) then delay := 3000 else delay := 0;
481 if (enet_host_service(NetMHost, @NetMEvent, delay) > 0) then
482 if NetMEvent.kind = ENET_EVENT_TYPE_CONNECT then
483 begin
484 Result := True;
485 NetHostConnected := True;
486 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_CONN]);
487 Exit;
488 end
489 else
490 if NetMEvent.kind = ENET_EVENT_TYPE_RECEIVE then
491 enet_packet_destroy(NetMEvent.packet);
493 if (blocking) then
494 begin
495 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_SLIST_ERROR], True);
497 if NetMPeer <> nil then enet_peer_reset(NetMPeer);
498 if NetMHost <> nil then enet_host_destroy(NetMHost);
499 NetMPeer := nil;
500 NetMHost := nil;
501 NetHostConnected := False;
502 NetHostConReqTime := 0;
503 end
504 else
505 begin
506 NetHostConReqTime := GetTimerMS();
507 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_WCONN]);
508 end;
509 end;
511 procedure g_Net_Slist_Disconnect;
512 begin
513 if (NetMHost = nil) and (NetMPeer = nil) then Exit;
515 if NetMode = NET_SERVER then g_Net_Slist_Remove;
517 enet_peer_disconnect(NetMPeer, 0);
518 enet_host_flush(NetMHost);
520 enet_peer_reset(NetMPeer);
521 enet_host_destroy(NetMHost);
523 NetMPeer := nil;
524 NetMHost := nil;
525 NetHostConnected := False;
526 NetHostConReqTime := 0;
528 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_DISC]);
529 end;
531 procedure g_Net_Slist_Check;
532 begin
533 if (NetMHost = nil) or (NetMPeer = nil) or (not NetHostConnected) then Exit;
535 while (enet_host_service(NetMHost, @NetMEvent, 0) > 0) do
536 begin
537 if NetMEvent.kind = ENET_EVENT_TYPE_DISCONNECT then
538 begin
539 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_LOST], True);
540 if NetMPeer <> nil then enet_peer_reset(NetMPeer);
541 if NetMHost <> nil then enet_host_destroy(NetMHost);
542 NetMPeer := nil;
543 NetMHost := nil;
544 NetHostConnected := False;
545 NetHostConReqTime := 0;
546 Break;
547 end
548 else
549 if NetMEvent.kind = ENET_EVENT_TYPE_RECEIVE then
550 enet_packet_destroy(NetMEvent.packet);
551 end;
552 end;
554 procedure g_Net_Slist_Set(IP: string; Port: Word);
555 begin
556 if NetInitDone then
557 begin
558 enet_address_set_host(@NetSlistAddr, PChar(Addr(IP[1])));
559 NetSlistAddr.Port := Port;
560 e_WriteLog('Masterserver address set to ' + IP + ':' + IntToStr(Port), TMsgType.Notify);
561 end;
562 end;
564 function GetServerFromTable(Index: Integer; SL: TNetServerList; ST: TNetServerTable): TNetServer;
565 begin
566 Result.Number := 0;
567 Result.Protocol := 0;
568 Result.Name := '';
569 Result.IP := '';
570 Result.Port := 0;
571 Result.Map := '';
572 Result.Players := 0;
573 Result.MaxPlayers := 0;
574 Result.LocalPl := 0;
575 Result.Bots := 0;
576 Result.Ping := 0;
577 Result.GameMode := 0;
578 Result.Password := false;
579 FillChar(Result.PingAddr, SizeOf(ENetAddress), 0);
580 if ST = nil then
581 Exit;
582 if (Index < 0) or (Index >= Length(ST)) then
583 Exit;
584 Result := SL[ST[Index].Indices[ST[Index].Current]];
585 end;
587 procedure g_Serverlist_Draw(var SL: TNetServerList; var ST: TNetServerTable);
588 var
589 Srv: TNetServer;
590 sy, i, y, mw, mx, l, motdh: Integer;
591 cw: Byte = 0;
592 ch: Byte = 0;
593 ww: Word = 0;
594 hh: Word = 0;
595 ip: string;
596 begin
597 ip := '';
598 sy := 0;
600 e_CharFont_GetSize(gMenuFont, _lc[I_NET_SLIST], ww, hh);
601 e_CharFont_Print(gMenuFont, (gScreenWidth div 2) - (ww div 2), 16, _lc[I_NET_SLIST]);
603 e_TextureFontGetSize(gStdFont, cw, ch);
605 ip := _lc[I_NET_SLIST_HELP];
606 mw := (Length(ip) * cw) div 2;
608 motdh := gScreenHeight - 49 - ch * b_Text_LineCount(slMOTD);
610 e_DrawFillQuad(16, 64, gScreenWidth-16, motdh, 64, 64, 64, 110);
611 e_DrawQuad(16, 64, gScreenWidth-16, motdh, 255, 127, 0);
613 e_TextureFontPrintEx(gScreenWidth div 2 - mw, gScreenHeight-24, ip, gStdFont, 225, 225, 225, 1);
615 // MOTD
616 if slMOTD <> '' then
617 begin
618 e_DrawFillQuad(16, motdh, gScreenWidth-16, gScreenHeight-44, 64, 64, 64, 110);
619 e_DrawQuad(16, motdh, gScreenWidth-16, gScreenHeight-44, 255, 127, 0);
620 e_TextureFontPrintFmt(20, motdh + 3, slMOTD, gStdFont, False, True);
621 end;
623 // Urgent message
624 if not slReadUrgent and (slUrgent <> '') then
625 begin
626 e_DrawFillQuad(17, 65, gScreenWidth-17, motdh-1, 64, 64, 64, 128);
627 e_DrawFillQuad(gScreenWidth div 2 - 256, gScreenHeight div 2 - 60,
628 gScreenWidth div 2 + 256, gScreenHeight div 2 + 60, 64, 64, 64, 128);
629 e_DrawQuad(gScreenWidth div 2 - 256, gScreenHeight div 2 - 60,
630 gScreenWidth div 2 + 256, gScreenHeight div 2 + 60, 255, 127, 0);
631 e_DrawLine(1, gScreenWidth div 2 - 256, gScreenHeight div 2 - 40,
632 gScreenWidth div 2 + 256, gScreenHeight div 2 - 40, 255, 127, 0);
633 l := Length(_lc[I_NET_SLIST_URGENT]) div 2;
634 e_TextureFontPrint(gScreenWidth div 2 - cw * l, gScreenHeight div 2 - 58,
635 _lc[I_NET_SLIST_URGENT], gStdFont);
636 l := Length(slUrgent) div 2;
637 e_TextureFontPrintFmt(gScreenWidth div 2 - 253, gScreenHeight div 2 - 38,
638 slUrgent, gStdFont, False, True);
639 l := Length(_lc[I_NET_SLIST_URGENT_CONT]) div 2;
640 e_TextureFontPrint(gScreenWidth div 2 - cw * l, gScreenHeight div 2 + 41,
641 _lc[I_NET_SLIST_URGENT_CONT], gStdFont);
642 e_DrawLine(1, gScreenWidth div 2 - 256, gScreenHeight div 2 + 40,
643 gScreenWidth div 2 + 256, gScreenHeight div 2 + 40, 255, 127, 0);
644 Exit;
645 end;
647 if SL = nil then
648 begin
649 l := Length(slWaitStr) div 2;
650 e_DrawFillQuad(17, 65, gScreenWidth-17, motdh-1, 64, 64, 64, 128);
651 e_DrawQuad(gScreenWidth div 2 - 192, gScreenHeight div 2 - 10,
652 gScreenWidth div 2 + 192, gScreenHeight div 2 + 11, 255, 127, 0);
653 e_TextureFontPrint(gScreenWidth div 2 - cw * l, gScreenHeight div 2 - ch div 2,
654 slWaitStr, gStdFont);
655 Exit;
656 end;
658 y := 90;
659 if (slSelection < Length(ST)) then
660 begin
661 I := slSelection;
662 sy := y + 42 * I - 4;
663 Srv := GetServerFromTable(I, SL, ST);
664 ip := _lc[I_NET_ADDRESS] + ' ' + Srv.IP + ':' + IntToStr(Srv.Port);
665 if Srv.Password then
666 ip := ip + ' ' + _lc[I_NET_SERVER_PASSWORD] + ' ' + _lc[I_MENU_YES]
667 else
668 ip := ip + ' ' + _lc[I_NET_SERVER_PASSWORD] + ' ' + _lc[I_MENU_NO];
669 end else
670 if Length(ST) > 0 then
671 slSelection := 0;
673 mw := (gScreenWidth - 188);
674 mx := 16 + mw;
676 e_DrawFillQuad(16 + 1, sy, gScreenWidth - 16 - 1, sy + 40, 64, 64, 64, 0);
677 e_DrawLine(1, 16 + 1, sy, gScreenWidth - 16 - 1, sy, 205, 205, 205);
678 e_DrawLine(1, 16 + 1, sy + 41, gScreenWidth - 16 - 1, sy + 41, 255, 255, 255);
680 e_DrawLine(1, 16, 85, gScreenWidth - 16, 85, 255, 127, 0);
681 e_DrawLine(1, 16, motdh-20, gScreenWidth-16, motdh-20, 255, 127, 0);
683 e_DrawLine(1, mx - 70, 64, mx - 70, motdh, 255, 127, 0);
684 e_DrawLine(1, mx, 64, mx, motdh-20, 255, 127, 0);
685 e_DrawLine(1, mx + 52, 64, mx + 52, motdh-20, 255, 127, 0);
686 e_DrawLine(1, mx + 104, 64, mx + 104, motdh-20, 255, 127, 0);
688 e_TextureFontPrintEx(18, 68, 'NAME/MAP', gStdFont, 255, 127, 0, 1);
689 e_TextureFontPrintEx(mx - 68, 68, 'PING', gStdFont, 255, 127, 0, 1);
690 e_TextureFontPrintEx(mx + 2, 68, 'MODE', gStdFont, 255, 127, 0, 1);
691 e_TextureFontPrintEx(mx + 54, 68, 'PLRS', gStdFont, 255, 127, 0, 1);
692 e_TextureFontPrintEx(mx + 106, 68, 'VER', gStdFont, 255, 127, 0, 1);
694 y := 90;
695 for I := 0 to High(ST) do
696 begin
697 Srv := GetServerFromTable(I, SL, ST);
698 // Name and map
699 e_TextureFontPrintEx(18, y, Srv.Name, gStdFont, 255, 255, 255, 1);
700 e_TextureFontPrintEx(18, y + 16, Srv.Map, gStdFont, 210, 210, 210, 1);
702 // Ping and similar count
703 if (Srv.Ping < 0) or (Srv.Ping > 999) then
704 e_TextureFontPrintEx(mx - 68, y, _lc[I_NET_SLIST_NO_ACCESS], gStdFont, 255, 0, 0, 1)
705 else
706 if Srv.Ping = 0 then
707 e_TextureFontPrintEx(mx - 68, y, '<1' + _lc[I_NET_SLIST_PING_MS], gStdFont, 255, 255, 255, 1)
708 else
709 e_TextureFontPrintEx(mx - 68, y, IntToStr(Srv.Ping) + _lc[I_NET_SLIST_PING_MS], gStdFont, 255, 255, 255, 1);
711 if Length(ST[I].Indices) > 1 then
712 e_TextureFontPrintEx(mx - 68, y + 16, '< ' + IntToStr(Length(ST[I].Indices)) + ' >', gStdFont, 210, 210, 210, 1);
714 // Game mode
715 e_TextureFontPrintEx(mx + 2, y, g_Game_ModeToText(Srv.GameMode), gStdFont, 255, 255, 255, 1);
717 // Players
718 e_TextureFontPrintEx(mx + 54, y, IntToStr(Srv.Players) + '/' + IntToStr(Srv.MaxPlayers), gStdFont, 255, 255, 255, 1);
719 e_TextureFontPrintEx(mx + 54, y + 16, IntToStr(Srv.LocalPl) + '+' + IntToStr(Srv.Bots), gStdFont, 210, 210, 210, 1);
721 // Version
722 e_TextureFontPrintEx(mx + 106, y, IntToStr(Srv.Protocol), gStdFont, 255, 255, 255, 1);
724 y := y + 42;
725 end;
727 e_TextureFontPrintEx(20, motdh-20+3, ip, gStdFont, 205, 205, 205, 1);
728 ip := IntToStr(Length(ST)) + _lc[I_NET_SLIST_SERVERS];
729 e_TextureFontPrintEx(gScreenWidth - 48 - (Length(ip) + 1)*cw,
730 motdh-20+3, ip, gStdFont, 205, 205, 205, 1);
731 end;
733 procedure g_Serverlist_GenerateTable(SL: TNetServerList; var ST: TNetServerTable);
734 var
735 i, j: Integer;
737 function FindServerInTable(Name: string): Integer;
738 var
739 i: Integer;
740 begin
741 Result := -1;
742 if ST = nil then
743 Exit;
744 for i := Low(ST) to High(ST) do
745 begin
746 if Length(ST[i].Indices) = 0 then
747 continue;
748 if SL[ST[i].Indices[0]].Name = Name then
749 begin
750 Result := i;
751 Exit;
752 end;
753 end;
754 end;
755 function ComparePing(i1, i2: Integer): Boolean;
756 var
757 p1, p2: Int64;
758 begin
759 p1 := SL[i1].Ping;
760 p2 := SL[i2].Ping;
761 if (p1 < 0) then p1 := 999;
762 if (p2 < 0) then p2 := 999;
763 Result := p1 > p2;
764 end;
765 procedure SortIndices(var ind: Array of Integer);
766 var
767 I, J: Integer;
768 T: Integer;
769 begin
770 for I := High(ind) downto Low(ind) do
771 for J := Low(ind) to High(ind) - 1 do
772 if ComparePing(ind[j], ind[j+1]) then
773 begin
774 T := ind[j];
775 ind[j] := ind[j+1];
776 ind[j+1] := T;
777 end;
778 end;
779 procedure SortRows();
780 var
781 I, J: Integer;
782 T: TNetServerRow;
783 begin
784 for I := High(ST) downto Low(ST) do
785 for J := Low(ST) to High(ST) - 1 do
786 if ComparePing(ST[j].Indices[0], ST[j+1].Indices[0]) then
787 begin
788 T := ST[j];
789 ST[j] := ST[j+1];
790 ST[j+1] := T;
791 end;
792 end;
793 begin
794 ST := nil;
795 if SL = nil then
796 Exit;
797 for i := Low(SL) to High(SL) do
798 begin
799 j := FindServerInTable(SL[i].Name);
800 if j = -1 then
801 begin
802 j := Length(ST);
803 SetLength(ST, j + 1);
804 ST[j].Current := 0;
805 SetLength(ST[j].Indices, 1);
806 ST[j].Indices[0] := i;
807 end
808 else
809 begin
810 SetLength(ST[j].Indices, Length(ST[j].Indices) + 1);
811 ST[j].Indices[High(ST[j].Indices)] := i;
812 end;
813 end;
815 for i := Low(ST) to High(ST) do
816 SortIndices(ST[i].Indices);
818 SortRows();
819 end;
821 procedure g_Serverlist_Control(var SL: TNetServerList; var ST: TNetServerTable);
822 var
823 qm: Boolean;
824 Srv: TNetServer;
825 begin
826 if gConsoleShow or gChatShow then
827 Exit;
829 qm := g_ProcessMessages(); // this updates kbd
831 if qm or e_KeyPressed(IK_ESCAPE) or e_KeyPressed(VK_ESCAPE) or
832 e_KeyPressed(JOY0_JUMP) or e_KeyPressed(JOY1_JUMP) or
833 e_KeyPressed(JOY2_JUMP) or e_KeyPressed(JOY3_JUMP) then
834 begin
835 SL := nil;
836 ST := nil;
837 gState := STATE_MENU;
838 g_GUI_ShowWindow('MainMenu');
839 g_GUI_ShowWindow('NetGameMenu');
840 g_GUI_ShowWindow('NetClientMenu');
841 g_Sound_PlayEx(WINDOW_CLOSESOUND);
842 Exit;
843 end;
845 // if there's a message on the screen,
846 if not slReadUrgent and (slUrgent <> '') then
847 begin
848 if e_KeyPressed(IK_RETURN) or e_KeyPressed(IK_KPRETURN) or e_KeyPressed(VK_FIRE) or e_KeyPressed(VK_OPEN) or
849 e_KeyPressed(JOY0_ATTACK) or e_KeyPressed(JOY1_ATTACK) or e_KeyPressed(JOY2_ATTACK) or e_KeyPressed(JOY3_ATTACK) then
850 slReadUrgent := True;
851 Exit;
852 end;
854 if e_KeyPressed(IK_SPACE) or e_KeyPressed(VK_JUMP) or
855 e_KeyPressed(JOY0_ACTIVATE) or e_KeyPressed(JOY1_ACTIVATE) or e_KeyPressed(JOY2_ACTIVATE) or e_KeyPressed(JOY3_ACTIVATE) then
856 begin
857 if not slFetched then
858 begin
859 slWaitStr := _lc[I_NET_SLIST_WAIT];
861 g_Game_Draw;
862 g_window.ReDrawWindow;
864 if g_Net_Slist_Fetch(SL) then
865 begin
866 if SL = nil then
867 slWaitStr := _lc[I_NET_SLIST_NOSERVERS];
868 end
869 else
870 if SL = nil then
871 slWaitStr := _lc[I_NET_SLIST_ERROR];
872 slFetched := True;
873 slSelection := 0;
874 g_Serverlist_GenerateTable(SL, ST);
875 end;
876 end
877 else
878 slFetched := False;
880 if SL = nil then Exit;
882 if e_KeyPressed(IK_RETURN) or e_KeyPressed(IK_KPRETURN) or e_KeyPressed(VK_FIRE) or e_KeyPressed(VK_OPEN) or
883 e_KeyPressed(JOY0_ATTACK) or e_KeyPressed(JOY1_ATTACK) or e_KeyPressed(JOY2_ATTACK) or e_KeyPressed(JOY3_ATTACK) then
884 begin
885 if not slReturnPressed then
886 begin
887 Srv := GetServerFromTable(slSelection, SL, ST);
888 if Srv.Password then
889 begin
890 PromptIP := Srv.IP;
891 PromptPort := Srv.Port;
892 gState := STATE_MENU;
893 g_GUI_ShowWindow('ClientPasswordMenu');
894 SL := nil;
895 ST := nil;
896 slReturnPressed := True;
897 Exit;
898 end
899 else
900 g_Game_StartClient(Srv.IP, Srv.Port, '');
901 SL := nil;
902 ST := nil;
903 slReturnPressed := True;
904 Exit;
905 end;
906 end
907 else
908 slReturnPressed := False;
910 if e_KeyPressed(IK_DOWN) or e_KeyPressed(IK_KPDOWN) or e_KeyPressed(VK_DOWN) or
911 e_KeyPressed(JOY0_DOWN) or e_KeyPressed(JOY1_DOWN) or e_KeyPressed(JOY2_DOWN) or e_KeyPressed(JOY3_DOWN) then
912 begin
913 if not slDirPressed then
914 begin
915 Inc(slSelection);
916 if slSelection > High(ST) then slSelection := 0;
917 slDirPressed := True;
918 end;
919 end;
921 if e_KeyPressed(IK_UP) or e_KeyPressed(IK_KPUP) or e_KeyPressed(VK_UP) or
922 e_KeyPressed(JOY0_UP) or e_KeyPressed(JOY1_UP) or e_KeyPressed(JOY2_UP) or e_KeyPressed(JOY3_UP) then
923 begin
924 if not slDirPressed then
925 begin
926 if slSelection = 0 then slSelection := Length(ST);
927 Dec(slSelection);
929 slDirPressed := True;
930 end;
931 end;
933 if e_KeyPressed(IK_RIGHT) or e_KeyPressed(IK_KPRIGHT) or e_KeyPressed(VK_RIGHT) or
934 e_KeyPressed(JOY0_RIGHT) or e_KeyPressed(JOY1_RIGHT) or e_KeyPressed(JOY2_RIGHT) or e_KeyPressed(JOY3_RIGHT) then
935 begin
936 if not slDirPressed then
937 begin
938 Inc(ST[slSelection].Current);
939 if ST[slSelection].Current > High(ST[slSelection].Indices) then ST[slSelection].Current := 0;
940 slDirPressed := True;
941 end;
942 end;
944 if e_KeyPressed(IK_LEFT) or e_KeyPressed(IK_KPLEFT) or e_KeyPressed(VK_LEFT) or
945 e_KeyPressed(JOY0_LEFT) or e_KeyPressed(JOY1_LEFT) or e_KeyPressed(JOY2_LEFT) or e_KeyPressed(JOY3_LEFT) then
946 begin
947 if not slDirPressed then
948 begin
949 if ST[slSelection].Current = 0 then ST[slSelection].Current := Length(ST[slSelection].Indices);
950 Dec(ST[slSelection].Current);
952 slDirPressed := True;
953 end;
954 end;
956 if (not e_KeyPressed(IK_DOWN)) and
957 (not e_KeyPressed(IK_UP)) and
958 (not e_KeyPressed(IK_RIGHT)) and
959 (not e_KeyPressed(IK_LEFT)) and
960 (not e_KeyPressed(IK_KPDOWN)) and
961 (not e_KeyPressed(IK_KPUP)) and
962 (not e_KeyPressed(IK_KPRIGHT)) and
963 (not e_KeyPressed(IK_KPLEFT)) and
964 (not e_KeyPressed(VK_DOWN)) and
965 (not e_KeyPressed(VK_UP)) and
966 (not e_KeyPressed(VK_RIGHT)) and
967 (not e_KeyPressed(VK_LEFT)) and
968 (not e_KeyPressed(JOY0_UP)) and (not e_KeyPressed(JOY1_UP)) and (not e_KeyPressed(JOY2_UP)) and (not e_KeyPressed(JOY3_UP)) and
969 (not e_KeyPressed(JOY0_DOWN)) and (not e_KeyPressed(JOY1_DOWN)) and (not e_KeyPressed(JOY2_DOWN)) and (not e_KeyPressed(JOY3_DOWN)) and
970 (not e_KeyPressed(JOY0_LEFT)) and (not e_KeyPressed(JOY1_LEFT)) and (not e_KeyPressed(JOY2_LEFT)) and (not e_KeyPressed(JOY3_LEFT)) and
971 (not e_KeyPressed(JOY0_RIGHT)) and (not e_KeyPressed(JOY1_RIGHT)) and (not e_KeyPressed(JOY2_RIGHT)) and (not e_KeyPressed(JOY3_RIGHT))
972 then
973 slDirPressed := False;
974 end;
976 end.