DEADSOFTWARE

Refactor: Define constants for LiftType
[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, either version 3 of the License, or
6 * (at your option) any later version.
7 *
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.
12 *
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/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_netmaster;
19 interface
21 uses ENet;
23 const
24 NET_MCHANS = 2;
26 NET_MCHAN_MAIN = 0;
27 NET_MCHAN_UPD = 1;
29 NET_MMSG_UPD = 200;
30 NET_MMSG_DEL = 201;
31 NET_MMSG_GET = 202;
33 type
34 TNetServer = record
35 Number: Byte;
36 Protocol: Byte;
37 Name: string;
38 IP: string;
39 Port: Word;
40 Map: string;
41 Players, MaxPlayers, LocalPl, Bots: Byte;
42 Ping: Int64;
43 GameMode: Byte;
44 Password: Boolean;
45 PingAddr: ENetAddress;
46 end;
47 pTNetServer = ^TNetServer;
48 TNetServerRow = record
49 Indices: Array of Integer;
50 Current: Integer;
51 end;
53 TNetServerList = array of TNetServer;
54 pTNetServerList = ^TNetServerList;
55 TNetServerTable = array of TNetServerRow;
57 var
58 NetMHost: pENetHost = nil;
59 NetMPeer: pENetPeer = nil;
61 slCurrent: TNetServerList = nil;
62 slTable: TNetServerTable = nil;
63 slWaitStr: string = '';
64 slReturnPressed: Boolean = True;
66 procedure g_Net_Slist_Set(IP: string; Port: Word);
67 function g_Net_Slist_Fetch(var SL: TNetServerList): Boolean;
68 procedure g_Net_Slist_Update();
69 procedure g_Net_Slist_Remove();
70 function g_Net_Slist_Connect(): Boolean;
71 procedure g_Net_Slist_Check();
72 procedure g_Net_Slist_Disconnect();
73 procedure g_Net_Slist_WriteInfo();
75 procedure g_Serverlist_GenerateTable(SL: TNetServerList; var ST: TNetServerTable);
76 procedure g_Serverlist_Draw(var SL: TNetServerList; var ST: TNetServerTable);
77 procedure g_Serverlist_Control(var SL: TNetServerList; var ST: TNetServerTable);
79 implementation
81 uses
82 SysUtils, e_msg, e_input, e_graphics, e_log, g_window, g_net, g_console,
83 g_map, g_game, g_sound, g_gui, g_menu, g_options, g_language, wadreader;
85 var
86 NetMEvent: ENetEvent;
87 slSelection: Byte = 0;
88 slFetched: Boolean = False;
89 slDirPressed: Boolean = False;
91 function GetTimerMS(): Int64;
92 begin
93 Result := GetTimer() {div 1000};
94 end;
96 procedure PingServer(var S: TNetServer; Sock: ENetSocket);
97 var
98 Buf: ENetBuffer;
99 Ping: array [0..9] of Byte;
100 ClTime: Int64;
101 begin
102 ClTime := GetTimerMS();
104 Buf.data := Addr(Ping[0]);
105 Buf.dataLength := 2+8;
107 Ping[0] := Ord('D');
108 Ping[1] := Ord('F');
109 Int64(Addr(Ping[2])^) := ClTime;
111 enet_socket_send(Sock, Addr(S.PingAddr), @Buf, 1);
112 end;
114 procedure PingBcast(Sock: ENetSocket);
115 var
116 S: TNetServer;
117 begin
118 S.IP := '255.255.255.255';
119 S.Port := NET_PING_PORT;
120 enet_address_set_host(Addr(S.PingAddr), PChar(Addr(S.IP[1])));
121 S.Ping := -1;
122 S.PingAddr.port := S.Port;
123 PingServer(S, Sock);
124 end;
126 function g_Net_Slist_Fetch(var SL: TNetServerList): Boolean;
127 var
128 Cnt: Byte;
129 P: pENetPacket;
130 MID: Byte;
131 I, RX: Integer;
132 T: Int64;
133 Sock: ENetSocket;
134 Buf: ENetBuffer;
135 InMsg: TMsg;
136 SvAddr: ENetAddress;
137 FromSL: Boolean;
139 procedure ProcessLocal();
140 begin
141 I := Length(SL);
142 SetLength(SL, I + 1);
143 with SL[I] do
144 begin
145 IP := DecodeIPV4(SvAddr.host);
146 Port := InMsg.ReadWord();
147 Ping := InMsg.ReadInt64();
148 Ping := GetTimerMS() - Ping;
149 Name := InMsg.ReadString();
150 Map := InMsg.ReadString();
151 GameMode := InMsg.ReadByte();
152 Players := InMsg.ReadByte();
153 MaxPlayers := InMsg.ReadByte();
154 Protocol := InMsg.ReadByte();
155 Password := InMsg.ReadByte() = 1;
156 LocalPl := InMsg.ReadByte();
157 Bots := InMsg.ReadWord();
158 end;
159 end;
160 procedure CheckLocalServers();
161 begin
162 SetLength(SL, 0);
164 Sock := enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM);
165 if Sock = ENET_SOCKET_NULL then Exit;
166 enet_socket_set_option(Sock, ENET_SOCKOPT_NONBLOCK, 1);
167 enet_socket_set_option(Sock, ENET_SOCKOPT_BROADCAST, 1);
168 PingBcast(Sock);
170 T := GetTimerMS();
172 InMsg.Alloc(NET_BUFSIZE);
173 Buf.data := InMsg.Data;
174 Buf.dataLength := InMsg.MaxSize;
175 while GetTimerMS() - T <= 500 do
176 begin
177 InMsg.Clear();
179 RX := enet_socket_receive(Sock, @SvAddr, @Buf, 1);
180 if RX <= 0 then continue;
181 InMsg.CurSize := RX;
183 InMsg.BeginReading();
185 if InMsg.ReadChar() <> 'D' then continue;
186 if InMsg.ReadChar() <> 'F' then continue;
188 ProcessLocal();
189 end;
191 InMsg.Free();
192 enet_socket_destroy(Sock);
194 if Length(SL) = 0 then SL := nil;
195 end;
196 begin
197 Result := False;
198 SL := nil;
200 if (NetMHost <> nil) or (NetMPeer <> nil) then
201 begin
202 CheckLocalServers();
203 Exit;
204 end;
206 if not g_Net_Slist_Connect then
207 begin
208 CheckLocalServers();
209 Exit;
210 end;
212 e_WriteLog('Fetching serverlist...', TMsgType.Notify);
213 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_FETCH]);
215 NetOut.Clear();
216 NetOut.Write(Byte(NET_MMSG_GET));
218 P := enet_packet_create(NetOut.Data, NetOut.CurSize, Cardinal(ENET_PACKET_FLAG_RELIABLE));
219 enet_peer_send(NetMPeer, NET_MCHAN_MAIN, P);
220 enet_host_flush(NetMHost);
222 while enet_host_service(NetMHost, @NetMEvent, 5000) > 0 do
223 begin
224 if NetMEvent.kind = ENET_EVENT_TYPE_RECEIVE then
225 begin
226 if not InMsg.Init(NetMEvent.packet^.data, NetMEvent.packet^.dataLength, True) then continue;
228 MID := InMsg.ReadByte();
230 if MID <> NET_MMSG_GET then continue;
232 Cnt := InMsg.ReadByte();
233 g_Console_Add(_lc[I_NET_MSG] + Format(_lc[I_NET_SLIST_RETRIEVED], [Cnt]), True);
235 if Cnt > 0 then
236 begin
237 SetLength(SL, Cnt);
239 for I := 0 to Cnt - 1 do
240 begin
241 SL[I].Number := I;
242 SL[I].IP := InMsg.ReadString();
243 SL[I].Port := InMsg.ReadWord();
244 SL[I].Name := InMsg.ReadString();
245 SL[I].Map := InMsg.ReadString();
246 SL[I].GameMode := InMsg.ReadByte();
247 SL[I].Players := InMsg.ReadByte();
248 SL[I].MaxPlayers := InMsg.ReadByte();
249 SL[I].Protocol := InMsg.ReadByte();
250 SL[I].Password := InMsg.ReadByte() = 1;
251 enet_address_set_host(Addr(SL[I].PingAddr), PChar(Addr(SL[I].IP[1])));
252 SL[I].Ping := -1;
253 SL[I].PingAddr.port := NET_PING_PORT;
254 end;
255 end;
257 Result := True;
258 break;
259 end;
260 end;
262 g_Net_Slist_Disconnect;
263 NetOut.Clear();
265 if Length(SL) = 0 then
266 begin
267 CheckLocalServers();
268 Exit;
269 end;
271 Sock := enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM);
272 if Sock = ENET_SOCKET_NULL then Exit;
273 enet_socket_set_option(Sock, ENET_SOCKOPT_NONBLOCK, 1);
275 for I := Low(SL) to High(SL) do
276 PingServer(SL[I], Sock);
278 enet_socket_set_option(Sock, ENET_SOCKOPT_BROADCAST, 1);
279 PingBcast(Sock);
281 T := GetTimerMS();
283 InMsg.Alloc(NET_BUFSIZE);
284 Buf.data := InMsg.Data;
285 Buf.dataLength := InMsg.MaxSize;
286 Cnt := 0;
287 while GetTimerMS() - T <= 500 do
288 begin
289 InMsg.Clear();
291 RX := enet_socket_receive(Sock, @SvAddr, @Buf, 1);
292 if RX <= 0 then continue;
293 InMsg.CurSize := RX;
295 InMsg.BeginReading();
297 if InMsg.ReadChar() <> 'D' then continue;
298 if InMsg.ReadChar() <> 'F' then continue;
300 FromSL := False;
301 for I := Low(SL) to High(SL) do
302 if (SL[I].PingAddr.host = SvAddr.host) and
303 (SL[I].PingAddr.port = SvAddr.port) then
304 begin
305 with SL[I] do
306 begin
307 Port := InMsg.ReadWord();
308 Ping := InMsg.ReadInt64();
309 Ping := GetTimerMS() - Ping;
310 Name := InMsg.ReadString();
311 Map := InMsg.ReadString();
312 GameMode := InMsg.ReadByte();
313 Players := InMsg.ReadByte();
314 MaxPlayers := InMsg.ReadByte();
315 Protocol := InMsg.ReadByte();
316 Password := InMsg.ReadByte() = 1;
317 LocalPl := InMsg.ReadByte();
318 Bots := InMsg.ReadWord();
319 end;
320 FromSL := True;
321 Inc(Cnt);
322 break;
323 end;
324 if not FromSL then
325 ProcessLocal();
326 end;
328 InMsg.Free();
329 enet_socket_destroy(Sock);
330 end;
332 procedure g_Net_Slist_WriteInfo();
333 var
334 Wad, Map: string;
335 Cli: Byte;
336 begin
337 Wad := g_ExtractWadNameNoPath(gMapInfo.Map);
338 Map := g_ExtractFileName(gMapInfo.Map);
340 NetOut.Write(NetServerName);
342 NetOut.Write(Wad + ':\' + Map);
343 NetOut.Write(gGameSettings.GameMode);
345 Cli := NetClientCount;
346 NetOut.Write(Cli);
348 NetOut.Write(NetMaxClients);
350 NetOut.Write(Byte(NET_PROTOCOL_VER));
351 NetOut.Write(Byte(NetPassword <> ''));
352 end;
354 procedure g_Net_Slist_Update;
355 var
357 P: pENetPacket;
359 begin
360 if (NetMHost = nil) or (NetMPeer = nil) then Exit;
362 NetOut.Clear();
363 NetOut.Write(Byte(NET_MMSG_UPD));
364 NetOut.Write(NetAddr.port);
366 g_Net_Slist_WriteInfo();
368 P := enet_packet_create(NetOut.Data, NetOut.CurSize, Cardinal(ENET_PACKET_FLAG_RELIABLE));
369 enet_peer_send(NetMPeer, NET_MCHAN_UPD, P);
371 enet_host_flush(NetMHost);
372 NetOut.Clear();
373 end;
375 procedure g_Net_Slist_Remove;
376 var
377 P: pENetPacket;
378 begin
379 if (NetMHost = nil) or (NetMPeer = nil) then Exit;
380 NetOut.Clear();
381 NetOut.Write(Byte(NET_MMSG_DEL));
382 NetOut.Write(NetAddr.port);
384 P := enet_packet_create(NetOut.Data, NetOut.CurSize, Cardinal(ENET_PACKET_FLAG_RELIABLE));
385 enet_peer_send(NetMPeer, NET_MCHAN_MAIN, P);
387 enet_host_flush(NetMHost);
388 NetOut.Clear();
389 end;
391 function g_Net_Slist_Connect: Boolean;
392 begin
393 Result := False;
395 NetMHost := enet_host_create(nil, 1, NET_MCHANS, 0, 0);
396 if (NetMHost = nil) then
397 begin
398 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CLIENT], True);
399 Exit;
400 end;
402 NetMPeer := enet_host_connect(NetMHost, @NetSlistAddr, NET_MCHANS, 0);
403 if (NetMPeer = nil) then
404 begin
405 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CLIENT], True);
406 enet_host_destroy(NetMHost);
407 NetMHost := nil;
408 Exit;
409 end;
411 if (enet_host_service(NetMHost, @NetMEvent, 3000) > 0) then
412 if NetMEvent.kind = ENET_EVENT_TYPE_CONNECT then
413 begin
414 Result := True;
415 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_CONN]);
416 Exit;
417 end
418 else
419 if NetMEvent.kind = ENET_EVENT_TYPE_RECEIVE then
420 enet_packet_destroy(NetMEvent.packet);
422 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_SLIST_ERROR], True);
424 NetMHost := nil;
425 NetMPeer := nil;
426 end;
428 procedure g_Net_Slist_Disconnect;
429 begin
430 if (NetMHost = nil) and (NetMPeer = nil) then Exit;
432 if NetMode = NET_SERVER then g_Net_Slist_Remove;
434 enet_peer_disconnect(NetMPeer, 0);
435 enet_host_flush(NetMHost);
437 enet_peer_reset(NetMPeer);
438 enet_host_destroy(NetMHost);
440 NetMPeer := nil;
441 NetMHost := nil;
443 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_DISC]);
444 end;
446 procedure g_Net_Slist_Check;
447 begin
448 if (NetMHost = nil) or (NetMPeer = nil) then Exit;
450 while (enet_host_service(NetMHost, @NetMEvent, 0) > 0) do
451 begin
452 if NetMEvent.kind = ENET_EVENT_TYPE_DISCONNECT then
453 begin
454 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_LOST], True);
455 if NetMPeer <> nil then enet_peer_reset(NetMPeer);
456 if NetMHost <> nil then enet_host_destroy(NetMHost);
457 NetMPeer := nil;
458 NetMHost := nil;
459 Break;
460 end
461 else
462 if NetMEvent.kind = ENET_EVENT_TYPE_RECEIVE then
463 enet_packet_destroy(NetMEvent.packet);
464 end;
465 end;
467 procedure g_Net_Slist_Set(IP: string; Port: Word);
468 begin
469 if NetInitDone then
470 begin
471 enet_address_set_host(@NetSlistAddr, PChar(Addr(IP[1])));
472 NetSlistAddr.Port := Port;
473 e_WriteLog('Masterserver address set to ' + IP + ':' + IntToStr(Port), TMsgType.Notify);
474 end;
475 end;
477 function GetServerFromTable(Index: Integer; SL: TNetServerList; ST: TNetServerTable): TNetServer;
478 begin
479 Result.Number := 0;
480 Result.Protocol := 0;
481 Result.Name := '';
482 Result.IP := '';
483 Result.Port := 0;
484 Result.Map := '';
485 Result.Players := 0;
486 Result.MaxPlayers := 0;
487 Result.LocalPl := 0;
488 Result.Bots := 0;
489 Result.Ping := 0;
490 Result.GameMode := 0;
491 Result.Password := false;
492 FillChar(Result.PingAddr, SizeOf(ENetAddress), 0);
493 if ST = nil then
494 Exit;
495 if (Index < 0) or (Index >= Length(ST)) then
496 Exit;
497 Result := SL[ST[Index].Indices[ST[Index].Current]];
498 end;
500 procedure g_Serverlist_Draw(var SL: TNetServerList; var ST: TNetServerTable);
501 var
502 Srv: TNetServer;
503 sy, i, y, mw, mx, l: Integer;
504 cw: Byte = 0;
505 ch: Byte = 0;
506 ww: Word = 0;
507 hh: Word = 0;
508 ip: string;
509 begin
510 ip := '';
511 sy := 0;
513 e_CharFont_GetSize(gMenuFont, _lc[I_NET_SLIST], ww, hh);
514 e_CharFont_Print(gMenuFont, (gScreenWidth div 2) - (ww div 2), 16, _lc[I_NET_SLIST]);
516 e_TextureFontGetSize(gStdFont, cw, ch);
518 ip := _lc[I_NET_SLIST_HELP];
519 mw := (Length(ip) * cw) div 2;
521 e_DrawFillQuad(16, 64, gScreenWidth-16, gScreenHeight-44, 64, 64, 64, 110);
522 e_DrawQuad(16, 64, gScreenWidth-16, gScreenHeight-44, 255, 127, 0);
524 e_TextureFontPrintEx(gScreenWidth div 2 - mw, gScreenHeight-24, ip, gStdFont, 225, 225, 225, 1);
526 if SL = nil then
527 begin
528 l := Length(slWaitStr) div 2;
529 e_DrawFillQuad(16, 64, gScreenWidth-16, gScreenHeight-44, 64, 64, 64, 128);
530 e_DrawQuad(gScreenWidth div 2 - 192, gScreenHeight div 2 - 10,
531 gScreenWidth div 2 + 192, gScreenHeight div 2 + 11, 255, 127, 0);
532 e_TextureFontPrint(gScreenWidth div 2 - cw * l, gScreenHeight div 2 - ch div 2,
533 slWaitStr, gStdFont);
534 Exit;
535 end;
537 y := 90;
538 if (slSelection < Length(ST)) then
539 begin
540 I := slSelection;
541 sy := y + 42 * I - 4;
542 Srv := GetServerFromTable(I, SL, ST);
543 ip := _lc[I_NET_ADDRESS] + ' ' + Srv.IP + ':' + IntToStr(Srv.Port);
544 if Srv.Password then
545 ip := ip + ' ' + _lc[I_NET_SERVER_PASSWORD] + ' ' + _lc[I_MENU_YES]
546 else
547 ip := ip + ' ' + _lc[I_NET_SERVER_PASSWORD] + ' ' + _lc[I_MENU_NO];
548 end else
549 if Length(ST) > 0 then
550 slSelection := 0;
552 mw := (gScreenWidth - 188);
553 mx := 16 + mw;
555 e_DrawFillQuad(16 + 1, sy, gScreenWidth - 16 - 1, sy + 40, 64, 64, 64, 0);
556 e_DrawLine(1, 16 + 1, sy, gScreenWidth - 16 - 1, sy, 205, 205, 205);
557 e_DrawLine(1, 16 + 1, sy + 41, gScreenWidth - 16 - 1, sy + 41, 255, 255, 255);
559 e_DrawLine(1, 16, 85, gScreenWidth - 16, 85, 255, 127, 0);
560 e_DrawLine(1, 16, gScreenHeight-64, gScreenWidth-16, gScreenHeight-64, 255, 127, 0);
562 e_DrawLine(1, mx - 70, 64, mx - 70, gScreenHeight-44, 255, 127, 0);
563 e_DrawLine(1, mx, 64, mx, gScreenHeight-64, 255, 127, 0);
564 e_DrawLine(1, mx + 52, 64, mx + 52, gScreenHeight-64, 255, 127, 0);
565 e_DrawLine(1, mx + 104, 64, mx + 104, gScreenHeight-64, 255, 127, 0);
567 e_TextureFontPrintEx(18, 68, 'NAME/MAP', gStdFont, 255, 127, 0, 1);
568 e_TextureFontPrintEx(mx - 68, 68, 'PING', gStdFont, 255, 127, 0, 1);
569 e_TextureFontPrintEx(mx + 2, 68, 'MODE', gStdFont, 255, 127, 0, 1);
570 e_TextureFontPrintEx(mx + 54, 68, 'PLRS', gStdFont, 255, 127, 0, 1);
571 e_TextureFontPrintEx(mx + 106, 68, 'VER', gStdFont, 255, 127, 0, 1);
573 y := 90;
574 for I := 0 to High(ST) do
575 begin
576 Srv := GetServerFromTable(I, SL, ST);
577 // Name and map
578 e_TextureFontPrintEx(18, y, Srv.Name, gStdFont, 255, 255, 255, 1);
579 e_TextureFontPrintEx(18, y + 16, Srv.Map, gStdFont, 210, 210, 210, 1);
581 // Ping and similar count
582 if (Srv.Ping < 0) or (Srv.Ping > 999) then
583 e_TextureFontPrintEx(mx - 68, y, _lc[I_NET_SLIST_NO_ACCESS], gStdFont, 255, 0, 0, 1)
584 else
585 if Srv.Ping = 0 then
586 e_TextureFontPrintEx(mx - 68, y, '<1' + _lc[I_NET_SLIST_PING_MS], gStdFont, 255, 255, 255, 1)
587 else
588 e_TextureFontPrintEx(mx - 68, y, IntToStr(Srv.Ping) + _lc[I_NET_SLIST_PING_MS], gStdFont, 255, 255, 255, 1);
590 if Length(ST[I].Indices) > 1 then
591 e_TextureFontPrintEx(mx - 68, y + 16, '< ' + IntToStr(Length(ST[I].Indices)) + ' >', gStdFont, 210, 210, 210, 1);
593 // Game mode
594 e_TextureFontPrintEx(mx + 2, y, g_Game_ModeToText(Srv.GameMode), gStdFont, 255, 255, 255, 1);
596 // Players
597 e_TextureFontPrintEx(mx + 54, y, IntToStr(Srv.Players) + '/' + IntToStr(Srv.MaxPlayers), gStdFont, 255, 255, 255, 1);
598 e_TextureFontPrintEx(mx + 54, y + 16, IntToStr(Srv.LocalPl) + '+' + IntToStr(Srv.Bots), gStdFont, 210, 210, 210, 1);
600 // Version
601 e_TextureFontPrintEx(mx + 106, y, IntToStr(Srv.Protocol), gStdFont, 255, 255, 255, 1);
603 y := y + 42;
604 end;
606 e_TextureFontPrintEx(20, gScreenHeight-61, ip, gStdFont, 205, 205, 205, 1);
607 ip := IntToStr(Length(ST)) + _lc[I_NET_SLIST_SERVERS];
608 e_TextureFontPrintEx(gScreenWidth - 48 - (Length(ip) + 1)*cw,
609 gScreenHeight-61, ip, gStdFont, 205, 205, 205, 1);
610 end;
612 procedure g_Serverlist_GenerateTable(SL: TNetServerList; var ST: TNetServerTable);
613 var
614 i, j: Integer;
616 function FindServerInTable(Name: string): Integer;
617 var
618 i: Integer;
619 begin
620 Result := -1;
621 if ST = nil then
622 Exit;
623 for i := Low(ST) to High(ST) do
624 begin
625 if Length(ST[i].Indices) = 0 then
626 continue;
627 if SL[ST[i].Indices[0]].Name = Name then
628 begin
629 Result := i;
630 Exit;
631 end;
632 end;
633 end;
634 function ComparePing(i1, i2: Integer): Boolean;
635 var
636 p1, p2: Int64;
637 begin
638 p1 := SL[i1].Ping;
639 p2 := SL[i2].Ping;
640 if (p1 < 0) then p1 := 999;
641 if (p2 < 0) then p2 := 999;
642 Result := p1 > p2;
643 end;
644 procedure SortIndices(var ind: Array of Integer);
645 var
646 I, J: Integer;
647 T: Integer;
648 begin
649 for I := High(ind) downto Low(ind) do
650 for J := Low(ind) to High(ind) - 1 do
651 if ComparePing(ind[j], ind[j+1]) then
652 begin
653 T := ind[j];
654 ind[j] := ind[j+1];
655 ind[j+1] := T;
656 end;
657 end;
658 procedure SortRows();
659 var
660 I, J: Integer;
661 T: TNetServerRow;
662 begin
663 for I := High(ST) downto Low(ST) do
664 for J := Low(ST) to High(ST) - 1 do
665 if ComparePing(ST[j].Indices[0], ST[j+1].Indices[0]) then
666 begin
667 T := ST[j];
668 ST[j] := ST[j+1];
669 ST[j+1] := T;
670 end;
671 end;
672 begin
673 ST := nil;
674 if SL = nil then
675 Exit;
676 for i := Low(SL) to High(SL) do
677 begin
678 j := FindServerInTable(SL[i].Name);
679 if j = -1 then
680 begin
681 j := Length(ST);
682 SetLength(ST, j + 1);
683 ST[j].Current := 0;
684 SetLength(ST[j].Indices, 1);
685 ST[j].Indices[0] := i;
686 end
687 else
688 begin
689 SetLength(ST[j].Indices, Length(ST[j].Indices) + 1);
690 ST[j].Indices[High(ST[j].Indices)] := i;
691 end;
692 end;
694 for i := Low(ST) to High(ST) do
695 SortIndices(ST[i].Indices);
697 SortRows();
698 end;
700 procedure g_Serverlist_Control(var SL: TNetServerList; var ST: TNetServerTable);
701 var
702 qm: Boolean;
703 Srv: TNetServer;
704 begin
705 if gConsoleShow or gChatShow then
706 Exit;
708 qm := g_ProcessMessages(); // this updates kbd
710 if qm or e_KeyPressed(IK_ESCAPE) or e_KeyPressed(VK_ESCAPE) then
711 begin
712 SL := nil;
713 ST := nil;
714 gState := STATE_MENU;
715 g_GUI_ShowWindow('MainMenu');
716 g_GUI_ShowWindow('NetGameMenu');
717 g_GUI_ShowWindow('NetClientMenu');
718 g_Sound_PlayEx(WINDOW_CLOSESOUND);
719 Exit;
720 end;
722 if e_KeyPressed(IK_SPACE) or e_KeyPressed(VK_JUMP) then
723 begin
724 if not slFetched then
725 begin
726 slWaitStr := _lc[I_NET_SLIST_WAIT];
728 g_Game_Draw;
729 g_window.ReDrawWindow;
731 if g_Net_Slist_Fetch(SL) then
732 begin
733 if SL = nil then
734 slWaitStr := _lc[I_NET_SLIST_NOSERVERS];
735 end
736 else
737 if SL = nil then
738 slWaitStr := _lc[I_NET_SLIST_ERROR];
739 slFetched := True;
740 slSelection := 0;
741 g_Serverlist_GenerateTable(SL, ST);
742 end;
743 end
744 else
745 slFetched := False;
747 if SL = nil then Exit;
749 if e_KeyPressed(IK_RETURN) or e_KeyPressed(IK_KPRETURN) or e_KeyPressed(VK_FIRE) or e_KeyPressed(VK_OPEN) then
750 begin
751 if not slReturnPressed then
752 begin
753 Srv := GetServerFromTable(slSelection, SL, ST);
754 if Srv.Password then
755 begin
756 PromptIP := Srv.IP;
757 PromptPort := Srv.Port;
758 gState := STATE_MENU;
759 g_GUI_ShowWindow('ClientPasswordMenu');
760 SL := nil;
761 ST := nil;
762 slReturnPressed := True;
763 Exit;
764 end
765 else
766 g_Game_StartClient(Srv.IP, Srv.Port, '');
767 SL := nil;
768 ST := nil;
769 slReturnPressed := True;
770 Exit;
771 end;
772 end
773 else
774 slReturnPressed := False;
776 if e_KeyPressed(IK_DOWN) or e_KeyPressed(IK_KPDOWN) or e_KeyPressed(VK_DOWN) then
777 begin
778 if not slDirPressed then
779 begin
780 Inc(slSelection);
781 if slSelection > High(ST) then slSelection := 0;
782 slDirPressed := True;
783 end;
784 end;
786 if e_KeyPressed(IK_UP) or e_KeyPressed(IK_KPUP) or e_KeyPressed(VK_UP) then
787 begin
788 if not slDirPressed then
789 begin
790 if slSelection = 0 then slSelection := Length(ST);
791 Dec(slSelection);
793 slDirPressed := True;
794 end;
795 end;
797 if e_KeyPressed(IK_RIGHT) or e_KeyPressed(IK_KPRIGHT) or e_KeyPressed(VK_RIGHT) then
798 begin
799 if not slDirPressed then
800 begin
801 Inc(ST[slSelection].Current);
802 if ST[slSelection].Current > High(ST[slSelection].Indices) then ST[slSelection].Current := 0;
803 slDirPressed := True;
804 end;
805 end;
807 if e_KeyPressed(IK_LEFT) or e_KeyPressed(IK_KPLEFT) or e_KeyPressed(VK_LEFT) then
808 begin
809 if not slDirPressed then
810 begin
811 if ST[slSelection].Current = 0 then ST[slSelection].Current := Length(ST[slSelection].Indices);
812 Dec(ST[slSelection].Current);
814 slDirPressed := True;
815 end;
816 end;
818 if (not e_KeyPressed(IK_DOWN)) and
819 (not e_KeyPressed(IK_UP)) and
820 (not e_KeyPressed(IK_RIGHT)) and
821 (not e_KeyPressed(IK_LEFT)) and
822 (not e_KeyPressed(IK_KPDOWN)) and
823 (not e_KeyPressed(IK_KPUP)) and
824 (not e_KeyPressed(IK_KPRIGHT)) and
825 (not e_KeyPressed(IK_KPLEFT)) and
826 (not e_KeyPressed(VK_DOWN)) and
827 (not e_KeyPressed(VK_UP)) and
828 (not e_KeyPressed(VK_RIGHT)) and
829 (not e_KeyPressed(VK_LEFT)) then
830 slDirPressed := False;
831 end;
833 end.