DEADSOFTWARE

changed license to GPLv3 only; sorry, no trust to FSF anymore
[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(): 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 implementation
83 uses
84 SysUtils, e_msg, e_input, e_graphics, e_log, g_window, g_net, g_console,
85 g_map, g_game, g_sound, g_gui, g_menu, g_options, g_language, g_basic,
86 wadreader;
88 var
89 NetMEvent: ENetEvent;
90 slSelection: Byte = 0;
91 slFetched: Boolean = False;
92 slDirPressed: Boolean = False;
93 slReadUrgent: Boolean = False;
95 function GetTimerMS(): Int64;
96 begin
97 Result := GetTimer() {div 1000};
98 end;
100 procedure PingServer(var S: TNetServer; Sock: ENetSocket);
101 var
102 Buf: ENetBuffer;
103 Ping: array [0..9] of Byte;
104 ClTime: Int64;
105 begin
106 ClTime := GetTimerMS();
108 Buf.data := Addr(Ping[0]);
109 Buf.dataLength := 2+8;
111 Ping[0] := Ord('D');
112 Ping[1] := Ord('F');
113 Int64(Addr(Ping[2])^) := ClTime;
115 enet_socket_send(Sock, Addr(S.PingAddr), @Buf, 1);
116 end;
118 procedure PingBcast(Sock: ENetSocket);
119 var
120 S: TNetServer;
121 begin
122 S.IP := '255.255.255.255';
123 S.Port := NET_PING_PORT;
124 enet_address_set_host(Addr(S.PingAddr), PChar(Addr(S.IP[1])));
125 S.Ping := -1;
126 S.PingAddr.port := S.Port;
127 PingServer(S, Sock);
128 end;
130 function g_Net_Slist_Fetch(var SL: TNetServerList): Boolean;
131 var
132 Cnt: Byte;
133 P: pENetPacket;
134 MID: Byte;
135 I, RX: Integer;
136 T: Int64;
137 Sock: ENetSocket;
138 Buf: ENetBuffer;
139 InMsg: TMsg;
140 SvAddr: ENetAddress;
141 FromSL: Boolean;
142 MyVer, Str: string;
144 procedure ProcessLocal();
145 begin
146 I := Length(SL);
147 SetLength(SL, I + 1);
148 with SL[I] do
149 begin
150 IP := DecodeIPV4(SvAddr.host);
151 Port := InMsg.ReadWord();
152 Ping := InMsg.ReadInt64();
153 Ping := GetTimerMS() - Ping;
154 Name := InMsg.ReadString();
155 Map := InMsg.ReadString();
156 GameMode := InMsg.ReadByte();
157 Players := InMsg.ReadByte();
158 MaxPlayers := InMsg.ReadByte();
159 Protocol := InMsg.ReadByte();
160 Password := InMsg.ReadByte() = 1;
161 LocalPl := InMsg.ReadByte();
162 Bots := InMsg.ReadWord();
163 end;
164 end;
165 procedure CheckLocalServers();
166 begin
167 SetLength(SL, 0);
169 Sock := enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM);
170 if Sock = ENET_SOCKET_NULL then Exit;
171 enet_socket_set_option(Sock, ENET_SOCKOPT_NONBLOCK, 1);
172 enet_socket_set_option(Sock, ENET_SOCKOPT_BROADCAST, 1);
173 PingBcast(Sock);
175 T := GetTimerMS();
177 InMsg.Alloc(NET_BUFSIZE);
178 Buf.data := InMsg.Data;
179 Buf.dataLength := InMsg.MaxSize;
180 while GetTimerMS() - T <= 500 do
181 begin
182 InMsg.Clear();
184 RX := enet_socket_receive(Sock, @SvAddr, @Buf, 1);
185 if RX <= 0 then continue;
186 InMsg.CurSize := RX;
188 InMsg.BeginReading();
190 if InMsg.ReadChar() <> 'D' then continue;
191 if InMsg.ReadChar() <> 'F' then continue;
193 ProcessLocal();
194 end;
196 InMsg.Free();
197 enet_socket_destroy(Sock);
199 if Length(SL) = 0 then SL := nil;
200 end;
201 begin
202 Result := False;
203 SL := nil;
205 if (NetMHost <> nil) or (NetMPeer <> nil) then
206 begin
207 CheckLocalServers();
208 Exit;
209 end;
211 if not g_Net_Slist_Connect then
212 begin
213 CheckLocalServers();
214 Exit;
215 end;
217 e_WriteLog('Fetching serverlist...', TMsgType.Notify);
218 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_FETCH]);
220 NetOut.Clear();
221 NetOut.Write(Byte(NET_MMSG_GET));
223 // TODO: what should we identify the build with?
224 MyVer := GAME_VERSION;
225 NetOut.Write(MyVer);
227 P := enet_packet_create(NetOut.Data, NetOut.CurSize, Cardinal(ENET_PACKET_FLAG_RELIABLE));
228 enet_peer_send(NetMPeer, NET_MCHAN_MAIN, P);
229 enet_host_flush(NetMHost);
231 while enet_host_service(NetMHost, @NetMEvent, 5000) > 0 do
232 begin
233 if NetMEvent.kind = ENET_EVENT_TYPE_RECEIVE then
234 begin
235 if not InMsg.Init(NetMEvent.packet^.data, NetMEvent.packet^.dataLength, True) then continue;
237 MID := InMsg.ReadByte();
239 if MID <> NET_MMSG_GET then continue;
241 Cnt := InMsg.ReadByte();
242 g_Console_Add(_lc[I_NET_MSG] + Format(_lc[I_NET_SLIST_RETRIEVED], [Cnt]), True);
244 if Cnt > 0 then
245 begin
246 SetLength(SL, Cnt);
248 for I := 0 to Cnt - 1 do
249 begin
250 SL[I].Number := I;
251 SL[I].IP := InMsg.ReadString();
252 SL[I].Port := InMsg.ReadWord();
253 SL[I].Name := InMsg.ReadString();
254 SL[I].Map := InMsg.ReadString();
255 SL[I].GameMode := InMsg.ReadByte();
256 SL[I].Players := InMsg.ReadByte();
257 SL[I].MaxPlayers := InMsg.ReadByte();
258 SL[I].Protocol := InMsg.ReadByte();
259 SL[I].Password := InMsg.ReadByte() = 1;
260 enet_address_set_host(Addr(SL[I].PingAddr), PChar(Addr(SL[I].IP[1])));
261 SL[I].Ping := -1;
262 SL[I].PingAddr.port := NET_PING_PORT;
263 end;
264 end;
266 if InMsg.ReadCount < InMsg.CurSize then
267 begin
268 // new master, supports version reports
269 Str := InMsg.ReadString();
270 if (Str <> MyVer) then
271 begin
272 { TODO }
273 g_Console_Add('!!! UpdVer = `' + Str + '`');
274 end;
275 // even newer master, supports extra info
276 if InMsg.ReadCount < InMsg.CurSize then
277 begin
278 slMOTD := b_Text_Format(InMsg.ReadString());
279 Str := b_Text_Format(InMsg.ReadString());
280 // check if the message has updated and the user has to read it again
281 if slUrgent <> Str then slReadUrgent := False;
282 slUrgent := Str;
283 end;
284 end;
286 Result := True;
287 break;
288 end;
289 end;
291 g_Net_Slist_Disconnect;
292 NetOut.Clear();
294 if Length(SL) = 0 then
295 begin
296 CheckLocalServers();
297 Exit;
298 end;
300 Sock := enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM);
301 if Sock = ENET_SOCKET_NULL then Exit;
302 enet_socket_set_option(Sock, ENET_SOCKOPT_NONBLOCK, 1);
304 for I := Low(SL) to High(SL) do
305 PingServer(SL[I], Sock);
307 enet_socket_set_option(Sock, ENET_SOCKOPT_BROADCAST, 1);
308 PingBcast(Sock);
310 T := GetTimerMS();
312 InMsg.Alloc(NET_BUFSIZE);
313 Buf.data := InMsg.Data;
314 Buf.dataLength := InMsg.MaxSize;
315 Cnt := 0;
316 while GetTimerMS() - T <= 500 do
317 begin
318 InMsg.Clear();
320 RX := enet_socket_receive(Sock, @SvAddr, @Buf, 1);
321 if RX <= 0 then continue;
322 InMsg.CurSize := RX;
324 InMsg.BeginReading();
326 if InMsg.ReadChar() <> 'D' then continue;
327 if InMsg.ReadChar() <> 'F' then continue;
329 FromSL := False;
330 for I := Low(SL) to High(SL) do
331 if (SL[I].PingAddr.host = SvAddr.host) and
332 (SL[I].PingAddr.port = SvAddr.port) then
333 begin
334 with SL[I] do
335 begin
336 Port := InMsg.ReadWord();
337 Ping := InMsg.ReadInt64();
338 Ping := GetTimerMS() - Ping;
339 Name := InMsg.ReadString();
340 Map := InMsg.ReadString();
341 GameMode := InMsg.ReadByte();
342 Players := InMsg.ReadByte();
343 MaxPlayers := InMsg.ReadByte();
344 Protocol := InMsg.ReadByte();
345 Password := InMsg.ReadByte() = 1;
346 LocalPl := InMsg.ReadByte();
347 Bots := InMsg.ReadWord();
348 end;
349 FromSL := True;
350 Inc(Cnt);
351 break;
352 end;
353 if not FromSL then
354 ProcessLocal();
355 end;
357 InMsg.Free();
358 enet_socket_destroy(Sock);
359 end;
361 procedure g_Net_Slist_WriteInfo();
362 var
363 Wad, Map: string;
364 Cli: Byte;
365 begin
366 Wad := g_ExtractWadNameNoPath(gMapInfo.Map);
367 Map := g_ExtractFileName(gMapInfo.Map);
369 NetOut.Write(NetServerName);
371 NetOut.Write(Wad + ':\' + Map);
372 NetOut.Write(gGameSettings.GameMode);
374 Cli := NetClientCount;
375 NetOut.Write(Cli);
377 NetOut.Write(NetMaxClients);
379 NetOut.Write(Byte(NET_PROTOCOL_VER));
380 NetOut.Write(Byte(NetPassword <> ''));
381 end;
383 procedure g_Net_Slist_Update;
384 var
386 P: pENetPacket;
388 begin
389 if (NetMHost = nil) or (NetMPeer = nil) then Exit;
391 NetOut.Clear();
392 NetOut.Write(Byte(NET_MMSG_UPD));
393 NetOut.Write(NetAddr.port);
395 g_Net_Slist_WriteInfo();
397 P := enet_packet_create(NetOut.Data, NetOut.CurSize, Cardinal(ENET_PACKET_FLAG_RELIABLE));
398 enet_peer_send(NetMPeer, NET_MCHAN_UPD, P);
400 enet_host_flush(NetMHost);
401 NetOut.Clear();
402 end;
404 procedure g_Net_Slist_Remove;
405 var
406 P: pENetPacket;
407 begin
408 if (NetMHost = nil) or (NetMPeer = nil) then Exit;
409 NetOut.Clear();
410 NetOut.Write(Byte(NET_MMSG_DEL));
411 NetOut.Write(NetAddr.port);
413 P := enet_packet_create(NetOut.Data, NetOut.CurSize, Cardinal(ENET_PACKET_FLAG_RELIABLE));
414 enet_peer_send(NetMPeer, NET_MCHAN_MAIN, P);
416 enet_host_flush(NetMHost);
417 NetOut.Clear();
418 end;
420 function g_Net_Slist_Connect: Boolean;
421 begin
422 Result := False;
424 NetMHost := enet_host_create(nil, 1, NET_MCHANS, 0, 0);
425 if (NetMHost = nil) then
426 begin
427 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CLIENT], True);
428 Exit;
429 end;
431 NetMPeer := enet_host_connect(NetMHost, @NetSlistAddr, NET_MCHANS, 0);
432 if (NetMPeer = nil) then
433 begin
434 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CLIENT], True);
435 enet_host_destroy(NetMHost);
436 NetMHost := nil;
437 Exit;
438 end;
440 if (enet_host_service(NetMHost, @NetMEvent, 3000) > 0) then
441 if NetMEvent.kind = ENET_EVENT_TYPE_CONNECT then
442 begin
443 Result := True;
444 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_CONN]);
445 Exit;
446 end
447 else
448 if NetMEvent.kind = ENET_EVENT_TYPE_RECEIVE then
449 enet_packet_destroy(NetMEvent.packet);
451 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_SLIST_ERROR], True);
453 NetMHost := nil;
454 NetMPeer := nil;
455 end;
457 procedure g_Net_Slist_Disconnect;
458 begin
459 if (NetMHost = nil) and (NetMPeer = nil) then Exit;
461 if NetMode = NET_SERVER then g_Net_Slist_Remove;
463 enet_peer_disconnect(NetMPeer, 0);
464 enet_host_flush(NetMHost);
466 enet_peer_reset(NetMPeer);
467 enet_host_destroy(NetMHost);
469 NetMPeer := nil;
470 NetMHost := nil;
472 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_DISC]);
473 end;
475 procedure g_Net_Slist_Check;
476 begin
477 if (NetMHost = nil) or (NetMPeer = nil) then Exit;
479 while (enet_host_service(NetMHost, @NetMEvent, 0) > 0) do
480 begin
481 if NetMEvent.kind = ENET_EVENT_TYPE_DISCONNECT then
482 begin
483 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_LOST], True);
484 if NetMPeer <> nil then enet_peer_reset(NetMPeer);
485 if NetMHost <> nil then enet_host_destroy(NetMHost);
486 NetMPeer := nil;
487 NetMHost := nil;
488 Break;
489 end
490 else
491 if NetMEvent.kind = ENET_EVENT_TYPE_RECEIVE then
492 enet_packet_destroy(NetMEvent.packet);
493 end;
494 end;
496 procedure g_Net_Slist_Set(IP: string; Port: Word);
497 begin
498 if NetInitDone then
499 begin
500 enet_address_set_host(@NetSlistAddr, PChar(Addr(IP[1])));
501 NetSlistAddr.Port := Port;
502 e_WriteLog('Masterserver address set to ' + IP + ':' + IntToStr(Port), TMsgType.Notify);
503 end;
504 end;
506 function GetServerFromTable(Index: Integer; SL: TNetServerList; ST: TNetServerTable): TNetServer;
507 begin
508 Result.Number := 0;
509 Result.Protocol := 0;
510 Result.Name := '';
511 Result.IP := '';
512 Result.Port := 0;
513 Result.Map := '';
514 Result.Players := 0;
515 Result.MaxPlayers := 0;
516 Result.LocalPl := 0;
517 Result.Bots := 0;
518 Result.Ping := 0;
519 Result.GameMode := 0;
520 Result.Password := false;
521 FillChar(Result.PingAddr, SizeOf(ENetAddress), 0);
522 if ST = nil then
523 Exit;
524 if (Index < 0) or (Index >= Length(ST)) then
525 Exit;
526 Result := SL[ST[Index].Indices[ST[Index].Current]];
527 end;
529 procedure g_Serverlist_Draw(var SL: TNetServerList; var ST: TNetServerTable);
530 var
531 Srv: TNetServer;
532 sy, i, y, mw, mx, l, motdh: Integer;
533 cw: Byte = 0;
534 ch: Byte = 0;
535 ww: Word = 0;
536 hh: Word = 0;
537 ip: string;
538 begin
539 ip := '';
540 sy := 0;
542 e_CharFont_GetSize(gMenuFont, _lc[I_NET_SLIST], ww, hh);
543 e_CharFont_Print(gMenuFont, (gScreenWidth div 2) - (ww div 2), 16, _lc[I_NET_SLIST]);
545 e_TextureFontGetSize(gStdFont, cw, ch);
547 ip := _lc[I_NET_SLIST_HELP];
548 mw := (Length(ip) * cw) div 2;
550 motdh := gScreenHeight - 49 - ch * b_Text_LineCount(slMOTD);
552 e_DrawFillQuad(16, 64, gScreenWidth-16, motdh, 64, 64, 64, 110);
553 e_DrawQuad(16, 64, gScreenWidth-16, motdh, 255, 127, 0);
555 e_TextureFontPrintEx(gScreenWidth div 2 - mw, gScreenHeight-24, ip, gStdFont, 225, 225, 225, 1);
557 // MOTD
558 if slMOTD <> '' then
559 begin
560 e_DrawFillQuad(16, motdh, gScreenWidth-16, gScreenHeight-44, 64, 64, 64, 110);
561 e_DrawQuad(16, motdh, gScreenWidth-16, gScreenHeight-44, 255, 127, 0);
562 e_TextureFontPrintFmt(20, motdh + 3, slMOTD, gStdFont, False, True);
563 end;
565 // Urgent message
566 if not slReadUrgent and (slUrgent <> '') then
567 begin
568 e_DrawFillQuad(17, 65, gScreenWidth-17, motdh-1, 64, 64, 64, 128);
569 e_DrawFillQuad(gScreenWidth div 2 - 256, gScreenHeight div 2 - 60,
570 gScreenWidth div 2 + 256, gScreenHeight div 2 + 60, 64, 64, 64, 128);
571 e_DrawQuad(gScreenWidth div 2 - 256, gScreenHeight div 2 - 60,
572 gScreenWidth div 2 + 256, gScreenHeight div 2 + 60, 255, 127, 0);
573 e_DrawLine(1, gScreenWidth div 2 - 256, gScreenHeight div 2 - 40,
574 gScreenWidth div 2 + 256, gScreenHeight div 2 - 40, 255, 127, 0);
575 l := Length(_lc[I_NET_SLIST_URGENT]) div 2;
576 e_TextureFontPrint(gScreenWidth div 2 - cw * l, gScreenHeight div 2 - 58,
577 _lc[I_NET_SLIST_URGENT], gStdFont);
578 l := Length(slUrgent) div 2;
579 e_TextureFontPrintFmt(gScreenWidth div 2 - 253, gScreenHeight div 2 - 38,
580 slUrgent, gStdFont, False, True);
581 l := Length(_lc[I_NET_SLIST_URGENT_CONT]) div 2;
582 e_TextureFontPrint(gScreenWidth div 2 - cw * l, gScreenHeight div 2 + 41,
583 _lc[I_NET_SLIST_URGENT_CONT], gStdFont);
584 e_DrawLine(1, gScreenWidth div 2 - 256, gScreenHeight div 2 + 40,
585 gScreenWidth div 2 + 256, gScreenHeight div 2 + 40, 255, 127, 0);
586 Exit;
587 end;
589 if SL = nil then
590 begin
591 l := Length(slWaitStr) div 2;
592 e_DrawFillQuad(17, 65, gScreenWidth-17, motdh-1, 64, 64, 64, 128);
593 e_DrawQuad(gScreenWidth div 2 - 192, gScreenHeight div 2 - 10,
594 gScreenWidth div 2 + 192, gScreenHeight div 2 + 11, 255, 127, 0);
595 e_TextureFontPrint(gScreenWidth div 2 - cw * l, gScreenHeight div 2 - ch div 2,
596 slWaitStr, gStdFont);
597 Exit;
598 end;
600 y := 90;
601 if (slSelection < Length(ST)) then
602 begin
603 I := slSelection;
604 sy := y + 42 * I - 4;
605 Srv := GetServerFromTable(I, SL, ST);
606 ip := _lc[I_NET_ADDRESS] + ' ' + Srv.IP + ':' + IntToStr(Srv.Port);
607 if Srv.Password then
608 ip := ip + ' ' + _lc[I_NET_SERVER_PASSWORD] + ' ' + _lc[I_MENU_YES]
609 else
610 ip := ip + ' ' + _lc[I_NET_SERVER_PASSWORD] + ' ' + _lc[I_MENU_NO];
611 end else
612 if Length(ST) > 0 then
613 slSelection := 0;
615 mw := (gScreenWidth - 188);
616 mx := 16 + mw;
618 e_DrawFillQuad(16 + 1, sy, gScreenWidth - 16 - 1, sy + 40, 64, 64, 64, 0);
619 e_DrawLine(1, 16 + 1, sy, gScreenWidth - 16 - 1, sy, 205, 205, 205);
620 e_DrawLine(1, 16 + 1, sy + 41, gScreenWidth - 16 - 1, sy + 41, 255, 255, 255);
622 e_DrawLine(1, 16, 85, gScreenWidth - 16, 85, 255, 127, 0);
623 e_DrawLine(1, 16, motdh-20, gScreenWidth-16, motdh-20, 255, 127, 0);
625 e_DrawLine(1, mx - 70, 64, mx - 70, motdh, 255, 127, 0);
626 e_DrawLine(1, mx, 64, mx, motdh-20, 255, 127, 0);
627 e_DrawLine(1, mx + 52, 64, mx + 52, motdh-20, 255, 127, 0);
628 e_DrawLine(1, mx + 104, 64, mx + 104, motdh-20, 255, 127, 0);
630 e_TextureFontPrintEx(18, 68, 'NAME/MAP', gStdFont, 255, 127, 0, 1);
631 e_TextureFontPrintEx(mx - 68, 68, 'PING', gStdFont, 255, 127, 0, 1);
632 e_TextureFontPrintEx(mx + 2, 68, 'MODE', gStdFont, 255, 127, 0, 1);
633 e_TextureFontPrintEx(mx + 54, 68, 'PLRS', gStdFont, 255, 127, 0, 1);
634 e_TextureFontPrintEx(mx + 106, 68, 'VER', gStdFont, 255, 127, 0, 1);
636 y := 90;
637 for I := 0 to High(ST) do
638 begin
639 Srv := GetServerFromTable(I, SL, ST);
640 // Name and map
641 e_TextureFontPrintEx(18, y, Srv.Name, gStdFont, 255, 255, 255, 1);
642 e_TextureFontPrintEx(18, y + 16, Srv.Map, gStdFont, 210, 210, 210, 1);
644 // Ping and similar count
645 if (Srv.Ping < 0) or (Srv.Ping > 999) then
646 e_TextureFontPrintEx(mx - 68, y, _lc[I_NET_SLIST_NO_ACCESS], gStdFont, 255, 0, 0, 1)
647 else
648 if Srv.Ping = 0 then
649 e_TextureFontPrintEx(mx - 68, y, '<1' + _lc[I_NET_SLIST_PING_MS], gStdFont, 255, 255, 255, 1)
650 else
651 e_TextureFontPrintEx(mx - 68, y, IntToStr(Srv.Ping) + _lc[I_NET_SLIST_PING_MS], gStdFont, 255, 255, 255, 1);
653 if Length(ST[I].Indices) > 1 then
654 e_TextureFontPrintEx(mx - 68, y + 16, '< ' + IntToStr(Length(ST[I].Indices)) + ' >', gStdFont, 210, 210, 210, 1);
656 // Game mode
657 e_TextureFontPrintEx(mx + 2, y, g_Game_ModeToText(Srv.GameMode), gStdFont, 255, 255, 255, 1);
659 // Players
660 e_TextureFontPrintEx(mx + 54, y, IntToStr(Srv.Players) + '/' + IntToStr(Srv.MaxPlayers), gStdFont, 255, 255, 255, 1);
661 e_TextureFontPrintEx(mx + 54, y + 16, IntToStr(Srv.LocalPl) + '+' + IntToStr(Srv.Bots), gStdFont, 210, 210, 210, 1);
663 // Version
664 e_TextureFontPrintEx(mx + 106, y, IntToStr(Srv.Protocol), gStdFont, 255, 255, 255, 1);
666 y := y + 42;
667 end;
669 e_TextureFontPrintEx(20, motdh-20+3, ip, gStdFont, 205, 205, 205, 1);
670 ip := IntToStr(Length(ST)) + _lc[I_NET_SLIST_SERVERS];
671 e_TextureFontPrintEx(gScreenWidth - 48 - (Length(ip) + 1)*cw,
672 motdh-20+3, ip, gStdFont, 205, 205, 205, 1);
673 end;
675 procedure g_Serverlist_GenerateTable(SL: TNetServerList; var ST: TNetServerTable);
676 var
677 i, j: Integer;
679 function FindServerInTable(Name: string): Integer;
680 var
681 i: Integer;
682 begin
683 Result := -1;
684 if ST = nil then
685 Exit;
686 for i := Low(ST) to High(ST) do
687 begin
688 if Length(ST[i].Indices) = 0 then
689 continue;
690 if SL[ST[i].Indices[0]].Name = Name then
691 begin
692 Result := i;
693 Exit;
694 end;
695 end;
696 end;
697 function ComparePing(i1, i2: Integer): Boolean;
698 var
699 p1, p2: Int64;
700 begin
701 p1 := SL[i1].Ping;
702 p2 := SL[i2].Ping;
703 if (p1 < 0) then p1 := 999;
704 if (p2 < 0) then p2 := 999;
705 Result := p1 > p2;
706 end;
707 procedure SortIndices(var ind: Array of Integer);
708 var
709 I, J: Integer;
710 T: Integer;
711 begin
712 for I := High(ind) downto Low(ind) do
713 for J := Low(ind) to High(ind) - 1 do
714 if ComparePing(ind[j], ind[j+1]) then
715 begin
716 T := ind[j];
717 ind[j] := ind[j+1];
718 ind[j+1] := T;
719 end;
720 end;
721 procedure SortRows();
722 var
723 I, J: Integer;
724 T: TNetServerRow;
725 begin
726 for I := High(ST) downto Low(ST) do
727 for J := Low(ST) to High(ST) - 1 do
728 if ComparePing(ST[j].Indices[0], ST[j+1].Indices[0]) then
729 begin
730 T := ST[j];
731 ST[j] := ST[j+1];
732 ST[j+1] := T;
733 end;
734 end;
735 begin
736 ST := nil;
737 if SL = nil then
738 Exit;
739 for i := Low(SL) to High(SL) do
740 begin
741 j := FindServerInTable(SL[i].Name);
742 if j = -1 then
743 begin
744 j := Length(ST);
745 SetLength(ST, j + 1);
746 ST[j].Current := 0;
747 SetLength(ST[j].Indices, 1);
748 ST[j].Indices[0] := i;
749 end
750 else
751 begin
752 SetLength(ST[j].Indices, Length(ST[j].Indices) + 1);
753 ST[j].Indices[High(ST[j].Indices)] := i;
754 end;
755 end;
757 for i := Low(ST) to High(ST) do
758 SortIndices(ST[i].Indices);
760 SortRows();
761 end;
763 procedure g_Serverlist_Control(var SL: TNetServerList; var ST: TNetServerTable);
764 var
765 qm: Boolean;
766 Srv: TNetServer;
767 begin
768 if gConsoleShow or gChatShow then
769 Exit;
771 qm := g_ProcessMessages(); // this updates kbd
773 if qm or e_KeyPressed(IK_ESCAPE) or e_KeyPressed(VK_ESCAPE) or
774 e_KeyPressed(JOY0_JUMP) or e_KeyPressed(JOY1_JUMP) or
775 e_KeyPressed(JOY2_JUMP) or e_KeyPressed(JOY3_JUMP) then
776 begin
777 SL := nil;
778 ST := nil;
779 gState := STATE_MENU;
780 g_GUI_ShowWindow('MainMenu');
781 g_GUI_ShowWindow('NetGameMenu');
782 g_GUI_ShowWindow('NetClientMenu');
783 g_Sound_PlayEx(WINDOW_CLOSESOUND);
784 Exit;
785 end;
787 // if there's a message on the screen,
788 if not slReadUrgent and (slUrgent <> '') then
789 begin
790 if e_KeyPressed(IK_RETURN) or e_KeyPressed(IK_KPRETURN) or e_KeyPressed(VK_FIRE) or e_KeyPressed(VK_OPEN) or
791 e_KeyPressed(JOY0_ATTACK) or e_KeyPressed(JOY1_ATTACK) or e_KeyPressed(JOY2_ATTACK) or e_KeyPressed(JOY3_ATTACK) then
792 slReadUrgent := True;
793 Exit;
794 end;
796 if e_KeyPressed(IK_SPACE) or e_KeyPressed(VK_JUMP) or
797 e_KeyPressed(JOY0_ACTIVATE) or e_KeyPressed(JOY1_ACTIVATE) or e_KeyPressed(JOY2_ACTIVATE) or e_KeyPressed(JOY3_ACTIVATE) then
798 begin
799 if not slFetched then
800 begin
801 slWaitStr := _lc[I_NET_SLIST_WAIT];
803 g_Game_Draw;
804 g_window.ReDrawWindow;
806 if g_Net_Slist_Fetch(SL) then
807 begin
808 if SL = nil then
809 slWaitStr := _lc[I_NET_SLIST_NOSERVERS];
810 end
811 else
812 if SL = nil then
813 slWaitStr := _lc[I_NET_SLIST_ERROR];
814 slFetched := True;
815 slSelection := 0;
816 g_Serverlist_GenerateTable(SL, ST);
817 end;
818 end
819 else
820 slFetched := False;
822 if SL = nil then Exit;
824 if e_KeyPressed(IK_RETURN) or e_KeyPressed(IK_KPRETURN) or e_KeyPressed(VK_FIRE) or e_KeyPressed(VK_OPEN) or
825 e_KeyPressed(JOY0_ATTACK) or e_KeyPressed(JOY1_ATTACK) or e_KeyPressed(JOY2_ATTACK) or e_KeyPressed(JOY3_ATTACK) then
826 begin
827 if not slReturnPressed then
828 begin
829 Srv := GetServerFromTable(slSelection, SL, ST);
830 if Srv.Password then
831 begin
832 PromptIP := Srv.IP;
833 PromptPort := Srv.Port;
834 gState := STATE_MENU;
835 g_GUI_ShowWindow('ClientPasswordMenu');
836 SL := nil;
837 ST := nil;
838 slReturnPressed := True;
839 Exit;
840 end
841 else
842 g_Game_StartClient(Srv.IP, Srv.Port, '');
843 SL := nil;
844 ST := nil;
845 slReturnPressed := True;
846 Exit;
847 end;
848 end
849 else
850 slReturnPressed := False;
852 if e_KeyPressed(IK_DOWN) or e_KeyPressed(IK_KPDOWN) or e_KeyPressed(VK_DOWN) or
853 e_KeyPressed(JOY0_DOWN) or e_KeyPressed(JOY1_DOWN) or e_KeyPressed(JOY2_DOWN) or e_KeyPressed(JOY3_DOWN) then
854 begin
855 if not slDirPressed then
856 begin
857 Inc(slSelection);
858 if slSelection > High(ST) then slSelection := 0;
859 slDirPressed := True;
860 end;
861 end;
863 if e_KeyPressed(IK_UP) or e_KeyPressed(IK_KPUP) or e_KeyPressed(VK_UP) or
864 e_KeyPressed(JOY0_UP) or e_KeyPressed(JOY1_UP) or e_KeyPressed(JOY2_UP) or e_KeyPressed(JOY3_UP) then
865 begin
866 if not slDirPressed then
867 begin
868 if slSelection = 0 then slSelection := Length(ST);
869 Dec(slSelection);
871 slDirPressed := True;
872 end;
873 end;
875 if e_KeyPressed(IK_RIGHT) or e_KeyPressed(IK_KPRIGHT) or e_KeyPressed(VK_RIGHT) or
876 e_KeyPressed(JOY0_RIGHT) or e_KeyPressed(JOY1_RIGHT) or e_KeyPressed(JOY2_RIGHT) or e_KeyPressed(JOY3_RIGHT) then
877 begin
878 if not slDirPressed then
879 begin
880 Inc(ST[slSelection].Current);
881 if ST[slSelection].Current > High(ST[slSelection].Indices) then ST[slSelection].Current := 0;
882 slDirPressed := True;
883 end;
884 end;
886 if e_KeyPressed(IK_LEFT) or e_KeyPressed(IK_KPLEFT) or e_KeyPressed(VK_LEFT) or
887 e_KeyPressed(JOY0_LEFT) or e_KeyPressed(JOY1_LEFT) or e_KeyPressed(JOY2_LEFT) or e_KeyPressed(JOY3_LEFT) then
888 begin
889 if not slDirPressed then
890 begin
891 if ST[slSelection].Current = 0 then ST[slSelection].Current := Length(ST[slSelection].Indices);
892 Dec(ST[slSelection].Current);
894 slDirPressed := True;
895 end;
896 end;
898 if (not e_KeyPressed(IK_DOWN)) and
899 (not e_KeyPressed(IK_UP)) and
900 (not e_KeyPressed(IK_RIGHT)) and
901 (not e_KeyPressed(IK_LEFT)) and
902 (not e_KeyPressed(IK_KPDOWN)) and
903 (not e_KeyPressed(IK_KPUP)) and
904 (not e_KeyPressed(IK_KPRIGHT)) and
905 (not e_KeyPressed(IK_KPLEFT)) and
906 (not e_KeyPressed(VK_DOWN)) and
907 (not e_KeyPressed(VK_UP)) and
908 (not e_KeyPressed(VK_RIGHT)) and
909 (not e_KeyPressed(VK_LEFT)) and
910 (not e_KeyPressed(JOY0_UP)) and (not e_KeyPressed(JOY1_UP)) and (not e_KeyPressed(JOY2_UP)) and (not e_KeyPressed(JOY3_UP)) and
911 (not e_KeyPressed(JOY0_DOWN)) and (not e_KeyPressed(JOY1_DOWN)) and (not e_KeyPressed(JOY2_DOWN)) and (not e_KeyPressed(JOY3_DOWN)) and
912 (not e_KeyPressed(JOY0_LEFT)) and (not e_KeyPressed(JOY1_LEFT)) and (not e_KeyPressed(JOY2_LEFT)) and (not e_KeyPressed(JOY3_LEFT)) and
913 (not e_KeyPressed(JOY0_RIGHT)) and (not e_KeyPressed(JOY1_RIGHT)) and (not e_KeyPressed(JOY2_RIGHT)) and (not e_KeyPressed(JOY3_RIGHT))
914 then
915 slDirPressed := False;
916 end;
918 end.