DEADSOFTWARE

2ecad14c74378d27d7f666bf9c7dc01c39d792d8
[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 slCurrent: TNetServerList = nil;
58 slTable: TNetServerTable = nil;
59 slWaitStr: string = '';
60 slReturnPressed: Boolean = True;
62 slMOTD: string = '';
63 slUrgent: string = '';
65 procedure g_Net_Slist_Set(IP: string; Port: Word);
66 function g_Net_Slist_Fetch(var SL: TNetServerList): Boolean;
67 procedure g_Net_Slist_Update (immediateSend: Boolean=true);
68 procedure g_Net_Slist_Remove();
69 function g_Net_Slist_Connect(blocking: Boolean=True): Boolean;
70 procedure g_Net_Slist_Check();
71 procedure g_Net_Slist_Disconnect (spamConsole: Boolean=true);
72 procedure g_Net_Slist_WriteInfo();
74 function g_Net_Slist_IsConnectionActive (): Boolean; // returns `false` if totally disconnected
75 function g_Net_Slist_IsConnectionInProgress (): Boolean;
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, g_system;
91 var
92 NetMHost: pENetHost = nil;
93 NetMPeer: pENetPeer = nil;
94 NetMEvent: ENetEvent;
95 slSelection: Byte = 0;
96 slFetched: Boolean = False;
97 slDirPressed: Boolean = False;
98 slReadUrgent: Boolean = False;
99 // inside the game, calling `g_Net_Slist_Connect()` is disasterous, as it is blocking.
100 // so we'll use this variable to indicate if "connected" event is received.
101 NetHostConnected: Boolean = false;
102 NetHostConReqTime: Int64 = 0; // to timeout `connect`
103 NetUpdatePending: Boolean = false;
106 function GetTimerMS (): Int64;
107 begin
108 Result := sys_GetTicks() {div 1000};
109 end;
112 // returns `false` if totally disconnected
113 function g_Net_Slist_IsConnectionActive (): Boolean;
114 begin
115 result := (NetMHost <> nil) and (NetMPeer <> nil);
116 end;
119 function g_Net_Slist_IsConnectionInProgress (): Boolean;
120 begin
121 if (NetMHost = nil) or (NetMPeer = nil) then begin result := false; exit; end;
122 result := (not NetHostConnected);
123 end;
126 // should be called only if host/peer is here
127 // returns `false` if not connected/dead
128 function ProcessPendingConnection (): Boolean;
129 var
130 ct: Int64;
131 begin
132 result := false;
133 if (NetMHost = nil) or (NetMPeer = nil) then exit;
134 // are we waiting for connection?
135 if (not NetHostConnected) then
136 begin
137 // check for connection event
138 if (enet_host_service(NetMHost, @NetMEvent, 0) > 0) then
139 begin
140 if (NetMEvent.kind = ENET_EVENT_TYPE_CONNECT) then
141 begin
142 NetHostConnected := true;
143 if NetUpdatePending then g_Net_Slist_Update(false);
144 g_Console_Add(_lc[I_NET_MSG]+_lc[I_NET_SLIST_CONN]);
145 result := true;
146 exit;
147 end;
148 if (NetMEvent.kind = ENET_EVENT_TYPE_RECEIVE) then enet_packet_destroy(NetMEvent.packet);
149 end;
150 // check for connection timeout
151 if (not NetHostConnected) then
152 begin
153 ct := GetTimerMS();
154 if (ct < NetHostConReqTime) or (ct-NetHostConReqTime >= 3000) then
155 begin
156 // do not spam with error messages, it looks like the master is down
157 //g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_SLIST_ERROR], True);
158 g_Net_Slist_Disconnect(false);
159 end;
160 exit;
161 end;
162 end;
163 result := true;
164 end;
167 procedure PingServer(var S: TNetServer; Sock: ENetSocket);
168 var
169 Buf: ENetBuffer;
170 Ping: array [0..9] of Byte;
171 ClTime: Int64;
172 begin
173 ClTime := GetTimerMS();
175 Buf.data := Addr(Ping[0]);
176 Buf.dataLength := 2+8;
178 Ping[0] := Ord('D');
179 Ping[1] := Ord('F');
180 Int64(Addr(Ping[2])^) := ClTime;
182 enet_socket_send(Sock, Addr(S.PingAddr), @Buf, 1);
183 end;
185 procedure PingBcast(Sock: ENetSocket);
186 var
187 S: TNetServer;
188 begin
189 S.IP := '255.255.255.255';
190 S.Port := NET_PING_PORT;
191 enet_address_set_host(Addr(S.PingAddr), PChar(Addr(S.IP[1])));
192 S.Ping := -1;
193 S.PingAddr.port := S.Port;
194 PingServer(S, Sock);
195 end;
197 function g_Net_Slist_Fetch(var SL: TNetServerList): Boolean;
198 var
199 Cnt: Byte;
200 P: pENetPacket;
201 MID: Byte;
202 I, RX: Integer;
203 T: Int64;
204 Sock: ENetSocket;
205 Buf: ENetBuffer;
206 InMsg: TMsg;
207 SvAddr: ENetAddress;
208 FromSL: Boolean;
209 MyVer, Str: string;
211 procedure ProcessLocal();
212 begin
213 I := Length(SL);
214 SetLength(SL, I + 1);
215 with SL[I] do
216 begin
217 IP := DecodeIPV4(SvAddr.host);
218 Port := InMsg.ReadWord();
219 Ping := InMsg.ReadInt64();
220 Ping := GetTimerMS() - Ping;
221 Name := InMsg.ReadString();
222 Map := InMsg.ReadString();
223 GameMode := InMsg.ReadByte();
224 Players := InMsg.ReadByte();
225 MaxPlayers := InMsg.ReadByte();
226 Protocol := InMsg.ReadByte();
227 Password := InMsg.ReadByte() = 1;
228 LocalPl := InMsg.ReadByte();
229 Bots := InMsg.ReadWord();
230 end;
231 end;
232 procedure CheckLocalServers();
233 begin
234 SetLength(SL, 0);
236 Sock := enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM);
237 if Sock = ENET_SOCKET_NULL then Exit;
238 enet_socket_set_option(Sock, ENET_SOCKOPT_NONBLOCK, 1);
239 enet_socket_set_option(Sock, ENET_SOCKOPT_BROADCAST, 1);
240 PingBcast(Sock);
242 T := GetTimerMS();
244 InMsg.Alloc(NET_BUFSIZE);
245 Buf.data := InMsg.Data;
246 Buf.dataLength := InMsg.MaxSize;
247 while GetTimerMS() - T <= 500 do
248 begin
249 InMsg.Clear();
251 RX := enet_socket_receive(Sock, @SvAddr, @Buf, 1);
252 if RX <= 0 then continue;
253 InMsg.CurSize := RX;
255 InMsg.BeginReading();
257 if InMsg.ReadChar() <> 'D' then continue;
258 if InMsg.ReadChar() <> 'F' then continue;
260 ProcessLocal();
261 end;
263 InMsg.Free();
264 enet_socket_destroy(Sock);
266 if Length(SL) = 0 then SL := nil;
267 end;
268 begin
269 Result := False;
270 SL := nil;
272 if (NetMHost <> nil) or (NetMPeer <> nil) then
273 begin
274 CheckLocalServers();
275 Exit;
276 end;
278 if not g_Net_Slist_Connect then
279 begin
280 CheckLocalServers();
281 Exit;
282 end;
284 e_WriteLog('Fetching serverlist...', TMsgType.Notify);
285 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_FETCH]);
287 NetOut.Clear();
288 NetOut.Write(Byte(NET_MMSG_GET));
290 // TODO: what should we identify the build with?
291 MyVer := GAME_VERSION;
292 NetOut.Write(MyVer);
294 P := enet_packet_create(NetOut.Data, NetOut.CurSize, Cardinal(ENET_PACKET_FLAG_RELIABLE));
295 enet_peer_send(NetMPeer, NET_MCHAN_MAIN, P);
296 enet_host_flush(NetMHost);
298 while enet_host_service(NetMHost, @NetMEvent, 5000) > 0 do
299 begin
300 if NetMEvent.kind = ENET_EVENT_TYPE_RECEIVE then
301 begin
302 if not InMsg.Init(NetMEvent.packet^.data, NetMEvent.packet^.dataLength, True) then continue;
304 MID := InMsg.ReadByte();
306 if MID <> NET_MMSG_GET then continue;
308 Cnt := InMsg.ReadByte();
309 g_Console_Add(_lc[I_NET_MSG] + Format(_lc[I_NET_SLIST_RETRIEVED], [Cnt]), True);
311 if Cnt > 0 then
312 begin
313 SetLength(SL, Cnt);
315 for I := 0 to Cnt - 1 do
316 begin
317 SL[I].Number := I;
318 SL[I].IP := InMsg.ReadString();
319 SL[I].Port := InMsg.ReadWord();
320 SL[I].Name := InMsg.ReadString();
321 SL[I].Map := InMsg.ReadString();
322 SL[I].GameMode := InMsg.ReadByte();
323 SL[I].Players := InMsg.ReadByte();
324 SL[I].MaxPlayers := InMsg.ReadByte();
325 SL[I].Protocol := InMsg.ReadByte();
326 SL[I].Password := InMsg.ReadByte() = 1;
327 enet_address_set_host(Addr(SL[I].PingAddr), PChar(Addr(SL[I].IP[1])));
328 SL[I].Ping := -1;
329 SL[I].PingAddr.port := NET_PING_PORT;
330 end;
331 end;
333 if InMsg.ReadCount < InMsg.CurSize then
334 begin
335 // new master, supports version reports
336 Str := InMsg.ReadString();
337 if (Str <> MyVer) then
338 begin
339 { TODO }
340 g_Console_Add('!!! UpdVer = `' + Str + '`');
341 end;
342 // even newer master, supports extra info
343 if InMsg.ReadCount < InMsg.CurSize then
344 begin
345 slMOTD := b_Text_Format(InMsg.ReadString());
346 Str := b_Text_Format(InMsg.ReadString());
347 // check if the message has updated and the user has to read it again
348 if slUrgent <> Str then slReadUrgent := False;
349 slUrgent := Str;
350 end;
351 end;
353 Result := True;
354 break;
355 end;
356 end;
358 g_Net_Slist_Disconnect;
359 NetOut.Clear();
361 if Length(SL) = 0 then
362 begin
363 CheckLocalServers();
364 Exit;
365 end;
367 Sock := enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM);
368 if Sock = ENET_SOCKET_NULL then Exit;
369 enet_socket_set_option(Sock, ENET_SOCKOPT_NONBLOCK, 1);
371 for I := Low(SL) to High(SL) do
372 PingServer(SL[I], Sock);
374 enet_socket_set_option(Sock, ENET_SOCKOPT_BROADCAST, 1);
375 PingBcast(Sock);
377 T := GetTimerMS();
379 InMsg.Alloc(NET_BUFSIZE);
380 Buf.data := InMsg.Data;
381 Buf.dataLength := InMsg.MaxSize;
382 Cnt := 0;
383 while GetTimerMS() - T <= 500 do
384 begin
385 InMsg.Clear();
387 RX := enet_socket_receive(Sock, @SvAddr, @Buf, 1);
388 if RX <= 0 then continue;
389 InMsg.CurSize := RX;
391 InMsg.BeginReading();
393 if InMsg.ReadChar() <> 'D' then continue;
394 if InMsg.ReadChar() <> 'F' then continue;
396 FromSL := False;
397 for I := Low(SL) to High(SL) do
398 if (SL[I].PingAddr.host = SvAddr.host) and
399 (SL[I].PingAddr.port = SvAddr.port) then
400 begin
401 with SL[I] do
402 begin
403 Port := InMsg.ReadWord();
404 Ping := InMsg.ReadInt64();
405 Ping := GetTimerMS() - Ping;
406 Name := InMsg.ReadString();
407 Map := InMsg.ReadString();
408 GameMode := InMsg.ReadByte();
409 Players := InMsg.ReadByte();
410 MaxPlayers := InMsg.ReadByte();
411 Protocol := InMsg.ReadByte();
412 Password := InMsg.ReadByte() = 1;
413 LocalPl := InMsg.ReadByte();
414 Bots := InMsg.ReadWord();
415 end;
416 FromSL := True;
417 Inc(Cnt);
418 break;
419 end;
420 if not FromSL then
421 ProcessLocal();
422 end;
424 InMsg.Free();
425 enet_socket_destroy(Sock);
426 end;
428 procedure g_Net_Slist_WriteInfo();
429 var
430 Wad, Map: string;
431 Cli: Byte;
432 begin
433 Wad := g_ExtractWadNameNoPath(gMapInfo.Map);
434 Map := g_ExtractFileName(gMapInfo.Map);
436 NetOut.Write(NetServerName);
438 NetOut.Write(Wad + ':\' + Map);
439 NetOut.Write(gGameSettings.GameMode);
441 Cli := NetClientCount;
442 NetOut.Write(Cli);
444 NetOut.Write(NetMaxClients);
446 NetOut.Write(Byte(NET_PROTOCOL_VER));
447 NetOut.Write(Byte(NetPassword <> ''));
448 end;
451 procedure g_Net_Slist_Update (immediateSend: Boolean=true);
452 var
453 P: pENetPacket;
454 begin
455 if not ProcessPendingConnection() then
456 begin
457 NetUpdatePending := g_Net_Slist_IsConnectionInProgress();
458 exit;
459 end;
461 NetUpdatePending := false;
463 NetOut.Clear();
464 NetOut.Write(Byte(NET_MMSG_UPD));
465 NetOut.Write(NetAddr.port);
467 g_Net_Slist_WriteInfo();
469 P := enet_packet_create(NetOut.Data, NetOut.CurSize, Cardinal(ENET_PACKET_FLAG_RELIABLE));
470 enet_peer_send(NetMPeer, NET_MCHAN_UPD, P);
472 if (immediateSend) then enet_host_flush(NetMHost);
473 NetOut.Clear();
474 end;
476 procedure g_Net_Slist_Remove;
477 var
478 P: pENetPacket;
479 begin
480 if not ProcessPendingConnection() then exit;
482 NetOut.Clear();
483 NetOut.Write(Byte(NET_MMSG_DEL));
484 NetOut.Write(NetAddr.port);
486 P := enet_packet_create(NetOut.Data, NetOut.CurSize, Cardinal(ENET_PACKET_FLAG_RELIABLE));
487 enet_peer_send(NetMPeer, NET_MCHAN_MAIN, P);
489 enet_host_flush(NetMHost);
490 NetOut.Clear();
491 end;
493 function g_Net_Slist_Connect (blocking: Boolean=True): Boolean;
494 var
495 delay: Integer;
496 begin
497 Result := False;
499 if g_Net_Slist_IsConnectionActive then
500 begin
501 if not blocking then exit;
502 g_Net_Slist_Disconnect(false);
503 end;
505 NetHostConnected := False; // just in case
506 NetHostConReqTime := 0; // just in case
507 NetUpdatePending := false;
509 NetMHost := enet_host_create(nil, 1, NET_MCHANS, 0, 0);
510 if (NetMHost = nil) then
511 begin
512 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CLIENT], True);
513 Exit;
514 end;
516 NetMPeer := enet_host_connect(NetMHost, @NetSlistAddr, NET_MCHANS, 0);
517 if (NetMPeer = nil) then
518 begin
519 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CLIENT], True);
520 enet_host_destroy(NetMHost);
521 NetMHost := nil;
522 Exit;
523 end;
525 if (blocking) then delay := 3000 else delay := 0;
526 if (enet_host_service(NetMHost, @NetMEvent, delay) > 0) then
527 if NetMEvent.kind = ENET_EVENT_TYPE_CONNECT then
528 begin
529 Result := True;
530 NetHostConnected := True;
531 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_CONN]);
532 Exit;
533 end
534 else
535 if NetMEvent.kind = ENET_EVENT_TYPE_RECEIVE then
536 enet_packet_destroy(NetMEvent.packet);
538 if (blocking) then
539 begin
540 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_SLIST_ERROR], True);
542 if NetMPeer <> nil then enet_peer_reset(NetMPeer);
543 if NetMHost <> nil then enet_host_destroy(NetMHost);
544 NetMPeer := nil;
545 NetMHost := nil;
546 NetHostConnected := False;
547 NetHostConReqTime := 0;
548 NetUpdatePending := false;
549 end
550 else
551 begin
552 NetHostConReqTime := GetTimerMS();
553 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_WCONN]);
554 end;
555 end;
557 procedure g_Net_Slist_Disconnect (spamConsole: Boolean=true);
558 begin
559 if (NetMHost = nil) and (NetMPeer = nil) then Exit;
561 if (NetMode = NET_SERVER) and (NetHostConnected) then g_Net_Slist_Remove;
563 enet_peer_disconnect(NetMPeer, 0);
564 enet_host_flush(NetMHost);
566 enet_peer_reset(NetMPeer);
567 enet_host_destroy(NetMHost);
569 NetMPeer := nil;
570 NetMHost := nil;
571 NetHostConnected := False;
572 NetHostConReqTime := 0;
573 NetUpdatePending := false;
575 if (spamConsole) then g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_DISC]);
576 end;
578 procedure g_Net_Slist_Check;
579 begin
580 if not ProcessPendingConnection() then exit;
582 if (NetUpdatePending) then g_Net_Slist_Update(false);
584 while (enet_host_service(NetMHost, @NetMEvent, 0) > 0) do
585 begin
586 if NetMEvent.kind = ENET_EVENT_TYPE_DISCONNECT then
587 begin
588 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_LOST], True);
589 if NetMPeer <> nil then enet_peer_reset(NetMPeer);
590 if NetMHost <> nil then enet_host_destroy(NetMHost);
591 NetMPeer := nil;
592 NetMHost := nil;
593 NetHostConnected := False;
594 NetHostConReqTime := 0;
595 NetUpdatePending := false;
596 Break;
597 end
598 else
599 if NetMEvent.kind = ENET_EVENT_TYPE_RECEIVE then
600 enet_packet_destroy(NetMEvent.packet);
601 end;
602 end;
604 procedure g_Net_Slist_Set(IP: string; Port: Word);
605 begin
606 if NetInitDone then
607 begin
608 enet_address_set_host(@NetSlistAddr, PChar(Addr(IP[1])));
609 NetSlistAddr.Port := Port;
610 e_WriteLog('Masterserver address set to ' + IP + ':' + IntToStr(Port), TMsgType.Notify);
611 end;
612 end;
614 function GetServerFromTable(Index: Integer; SL: TNetServerList; ST: TNetServerTable): TNetServer;
615 begin
616 Result.Number := 0;
617 Result.Protocol := 0;
618 Result.Name := '';
619 Result.IP := '';
620 Result.Port := 0;
621 Result.Map := '';
622 Result.Players := 0;
623 Result.MaxPlayers := 0;
624 Result.LocalPl := 0;
625 Result.Bots := 0;
626 Result.Ping := 0;
627 Result.GameMode := 0;
628 Result.Password := false;
629 FillChar(Result.PingAddr, SizeOf(ENetAddress), 0);
630 if ST = nil then
631 Exit;
632 if (Index < 0) or (Index >= Length(ST)) then
633 Exit;
634 Result := SL[ST[Index].Indices[ST[Index].Current]];
635 end;
637 procedure g_Serverlist_Draw(var SL: TNetServerList; var ST: TNetServerTable);
638 var
639 Srv: TNetServer;
640 sy, i, y, mw, mx, l, motdh: Integer;
641 cw: Byte = 0;
642 ch: Byte = 0;
643 ww: Word = 0;
644 hh: Word = 0;
645 ip: string;
646 begin
647 ip := '';
648 sy := 0;
650 e_CharFont_GetSize(gMenuFont, _lc[I_NET_SLIST], ww, hh);
651 e_CharFont_Print(gMenuFont, (gScreenWidth div 2) - (ww div 2), 16, _lc[I_NET_SLIST]);
653 e_TextureFontGetSize(gStdFont, cw, ch);
655 ip := _lc[I_NET_SLIST_HELP];
656 mw := (Length(ip) * cw) div 2;
658 motdh := gScreenHeight - 49 - ch * b_Text_LineCount(slMOTD);
660 e_DrawFillQuad(16, 64, gScreenWidth-16, motdh, 64, 64, 64, 110);
661 e_DrawQuad(16, 64, gScreenWidth-16, motdh, 255, 127, 0);
663 e_TextureFontPrintEx(gScreenWidth div 2 - mw, gScreenHeight-24, ip, gStdFont, 225, 225, 225, 1);
665 // MOTD
666 if slMOTD <> '' then
667 begin
668 e_DrawFillQuad(16, motdh, gScreenWidth-16, gScreenHeight-44, 64, 64, 64, 110);
669 e_DrawQuad(16, motdh, gScreenWidth-16, gScreenHeight-44, 255, 127, 0);
670 e_TextureFontPrintFmt(20, motdh + 3, slMOTD, gStdFont, False, True);
671 end;
673 // Urgent message
674 if not slReadUrgent and (slUrgent <> '') then
675 begin
676 e_DrawFillQuad(17, 65, gScreenWidth-17, motdh-1, 64, 64, 64, 128);
677 e_DrawFillQuad(gScreenWidth div 2 - 256, gScreenHeight div 2 - 60,
678 gScreenWidth div 2 + 256, gScreenHeight div 2 + 60, 64, 64, 64, 128);
679 e_DrawQuad(gScreenWidth div 2 - 256, gScreenHeight div 2 - 60,
680 gScreenWidth div 2 + 256, gScreenHeight div 2 + 60, 255, 127, 0);
681 e_DrawLine(1, gScreenWidth div 2 - 256, gScreenHeight div 2 - 40,
682 gScreenWidth div 2 + 256, gScreenHeight div 2 - 40, 255, 127, 0);
683 l := Length(_lc[I_NET_SLIST_URGENT]) div 2;
684 e_TextureFontPrint(gScreenWidth div 2 - cw * l, gScreenHeight div 2 - 58,
685 _lc[I_NET_SLIST_URGENT], gStdFont);
686 l := Length(slUrgent) div 2;
687 e_TextureFontPrintFmt(gScreenWidth div 2 - 253, gScreenHeight div 2 - 38,
688 slUrgent, gStdFont, False, True);
689 l := Length(_lc[I_NET_SLIST_URGENT_CONT]) div 2;
690 e_TextureFontPrint(gScreenWidth div 2 - cw * l, gScreenHeight div 2 + 41,
691 _lc[I_NET_SLIST_URGENT_CONT], gStdFont);
692 e_DrawLine(1, gScreenWidth div 2 - 256, gScreenHeight div 2 + 40,
693 gScreenWidth div 2 + 256, gScreenHeight div 2 + 40, 255, 127, 0);
694 Exit;
695 end;
697 if SL = nil then
698 begin
699 l := Length(slWaitStr) div 2;
700 e_DrawFillQuad(17, 65, gScreenWidth-17, motdh-1, 64, 64, 64, 128);
701 e_DrawQuad(gScreenWidth div 2 - 192, gScreenHeight div 2 - 10,
702 gScreenWidth div 2 + 192, gScreenHeight div 2 + 11, 255, 127, 0);
703 e_TextureFontPrint(gScreenWidth div 2 - cw * l, gScreenHeight div 2 - ch div 2,
704 slWaitStr, gStdFont);
705 Exit;
706 end;
708 y := 90;
709 if (slSelection < Length(ST)) then
710 begin
711 I := slSelection;
712 sy := y + 42 * I - 4;
713 Srv := GetServerFromTable(I, SL, ST);
714 ip := _lc[I_NET_ADDRESS] + ' ' + Srv.IP + ':' + IntToStr(Srv.Port);
715 if Srv.Password then
716 ip := ip + ' ' + _lc[I_NET_SERVER_PASSWORD] + ' ' + _lc[I_MENU_YES]
717 else
718 ip := ip + ' ' + _lc[I_NET_SERVER_PASSWORD] + ' ' + _lc[I_MENU_NO];
719 end else
720 if Length(ST) > 0 then
721 slSelection := 0;
723 mw := (gScreenWidth - 188);
724 mx := 16 + mw;
726 e_DrawFillQuad(16 + 1, sy, gScreenWidth - 16 - 1, sy + 40, 64, 64, 64, 0);
727 e_DrawLine(1, 16 + 1, sy, gScreenWidth - 16 - 1, sy, 205, 205, 205);
728 e_DrawLine(1, 16 + 1, sy + 41, gScreenWidth - 16 - 1, sy + 41, 255, 255, 255);
730 e_DrawLine(1, 16, 85, gScreenWidth - 16, 85, 255, 127, 0);
731 e_DrawLine(1, 16, motdh-20, gScreenWidth-16, motdh-20, 255, 127, 0);
733 e_DrawLine(1, mx - 70, 64, mx - 70, motdh, 255, 127, 0);
734 e_DrawLine(1, mx, 64, mx, motdh-20, 255, 127, 0);
735 e_DrawLine(1, mx + 52, 64, mx + 52, motdh-20, 255, 127, 0);
736 e_DrawLine(1, mx + 104, 64, mx + 104, motdh-20, 255, 127, 0);
738 e_TextureFontPrintEx(18, 68, 'NAME/MAP', gStdFont, 255, 127, 0, 1);
739 e_TextureFontPrintEx(mx - 68, 68, 'PING', gStdFont, 255, 127, 0, 1);
740 e_TextureFontPrintEx(mx + 2, 68, 'MODE', gStdFont, 255, 127, 0, 1);
741 e_TextureFontPrintEx(mx + 54, 68, 'PLRS', gStdFont, 255, 127, 0, 1);
742 e_TextureFontPrintEx(mx + 106, 68, 'VER', gStdFont, 255, 127, 0, 1);
744 y := 90;
745 for I := 0 to High(ST) do
746 begin
747 Srv := GetServerFromTable(I, SL, ST);
748 // Name and map
749 e_TextureFontPrintEx(18, y, Srv.Name, gStdFont, 255, 255, 255, 1);
750 e_TextureFontPrintEx(18, y + 16, Srv.Map, gStdFont, 210, 210, 210, 1);
752 // Ping and similar count
753 if (Srv.Ping < 0) or (Srv.Ping > 999) then
754 e_TextureFontPrintEx(mx - 68, y, _lc[I_NET_SLIST_NO_ACCESS], gStdFont, 255, 0, 0, 1)
755 else
756 if Srv.Ping = 0 then
757 e_TextureFontPrintEx(mx - 68, y, '<1' + _lc[I_NET_SLIST_PING_MS], gStdFont, 255, 255, 255, 1)
758 else
759 e_TextureFontPrintEx(mx - 68, y, IntToStr(Srv.Ping) + _lc[I_NET_SLIST_PING_MS], gStdFont, 255, 255, 255, 1);
761 if Length(ST[I].Indices) > 1 then
762 e_TextureFontPrintEx(mx - 68, y + 16, '< ' + IntToStr(Length(ST[I].Indices)) + ' >', gStdFont, 210, 210, 210, 1);
764 // Game mode
765 e_TextureFontPrintEx(mx + 2, y, g_Game_ModeToText(Srv.GameMode), gStdFont, 255, 255, 255, 1);
767 // Players
768 e_TextureFontPrintEx(mx + 54, y, IntToStr(Srv.Players) + '/' + IntToStr(Srv.MaxPlayers), gStdFont, 255, 255, 255, 1);
769 e_TextureFontPrintEx(mx + 54, y + 16, IntToStr(Srv.LocalPl) + '+' + IntToStr(Srv.Bots), gStdFont, 210, 210, 210, 1);
771 // Version
772 e_TextureFontPrintEx(mx + 106, y, IntToStr(Srv.Protocol), gStdFont, 255, 255, 255, 1);
774 y := y + 42;
775 end;
777 e_TextureFontPrintEx(20, motdh-20+3, ip, gStdFont, 205, 205, 205, 1);
778 ip := IntToStr(Length(ST)) + _lc[I_NET_SLIST_SERVERS];
779 e_TextureFontPrintEx(gScreenWidth - 48 - (Length(ip) + 1)*cw,
780 motdh-20+3, ip, gStdFont, 205, 205, 205, 1);
781 end;
783 procedure g_Serverlist_GenerateTable(SL: TNetServerList; var ST: TNetServerTable);
784 var
785 i, j: Integer;
787 function FindServerInTable(Name: string): Integer;
788 var
789 i: Integer;
790 begin
791 Result := -1;
792 if ST = nil then
793 Exit;
794 for i := Low(ST) to High(ST) do
795 begin
796 if Length(ST[i].Indices) = 0 then
797 continue;
798 if SL[ST[i].Indices[0]].Name = Name then
799 begin
800 Result := i;
801 Exit;
802 end;
803 end;
804 end;
805 function ComparePing(i1, i2: Integer): Boolean;
806 var
807 p1, p2: Int64;
808 begin
809 p1 := SL[i1].Ping;
810 p2 := SL[i2].Ping;
811 if (p1 < 0) then p1 := 999;
812 if (p2 < 0) then p2 := 999;
813 Result := p1 > p2;
814 end;
815 procedure SortIndices(var ind: Array of Integer);
816 var
817 I, J: Integer;
818 T: Integer;
819 begin
820 for I := High(ind) downto Low(ind) do
821 for J := Low(ind) to High(ind) - 1 do
822 if ComparePing(ind[j], ind[j+1]) then
823 begin
824 T := ind[j];
825 ind[j] := ind[j+1];
826 ind[j+1] := T;
827 end;
828 end;
829 procedure SortRows();
830 var
831 I, J: Integer;
832 T: TNetServerRow;
833 begin
834 for I := High(ST) downto Low(ST) do
835 for J := Low(ST) to High(ST) - 1 do
836 if ComparePing(ST[j].Indices[0], ST[j+1].Indices[0]) then
837 begin
838 T := ST[j];
839 ST[j] := ST[j+1];
840 ST[j+1] := T;
841 end;
842 end;
843 begin
844 ST := nil;
845 if SL = nil then
846 Exit;
847 for i := Low(SL) to High(SL) do
848 begin
849 j := FindServerInTable(SL[i].Name);
850 if j = -1 then
851 begin
852 j := Length(ST);
853 SetLength(ST, j + 1);
854 ST[j].Current := 0;
855 SetLength(ST[j].Indices, 1);
856 ST[j].Indices[0] := i;
857 end
858 else
859 begin
860 SetLength(ST[j].Indices, Length(ST[j].Indices) + 1);
861 ST[j].Indices[High(ST[j].Indices)] := i;
862 end;
863 end;
865 for i := Low(ST) to High(ST) do
866 SortIndices(ST[i].Indices);
868 SortRows();
869 end;
871 procedure g_Serverlist_Control(var SL: TNetServerList; var ST: TNetServerTable);
872 var
873 qm: Boolean;
874 Srv: TNetServer;
875 begin
876 if gConsoleShow or gChatShow then
877 Exit;
879 qm := sys_HandleInput(); // this updates kbd
881 if qm or e_KeyPressed(IK_ESCAPE) or e_KeyPressed(VK_ESCAPE) or
882 e_KeyPressed(JOY0_JUMP) or e_KeyPressed(JOY1_JUMP) or
883 e_KeyPressed(JOY2_JUMP) or e_KeyPressed(JOY3_JUMP) then
884 begin
885 SL := nil;
886 ST := nil;
887 gState := STATE_MENU;
888 g_GUI_ShowWindow('MainMenu');
889 g_GUI_ShowWindow('NetGameMenu');
890 g_GUI_ShowWindow('NetClientMenu');
891 g_Sound_PlayEx(WINDOW_CLOSESOUND);
892 Exit;
893 end;
895 // if there's a message on the screen,
896 if not slReadUrgent and (slUrgent <> '') then
897 begin
898 if e_KeyPressed(IK_RETURN) or e_KeyPressed(IK_KPRETURN) or e_KeyPressed(VK_FIRE) or e_KeyPressed(VK_OPEN) or
899 e_KeyPressed(JOY0_ATTACK) or e_KeyPressed(JOY1_ATTACK) or e_KeyPressed(JOY2_ATTACK) or e_KeyPressed(JOY3_ATTACK) then
900 slReadUrgent := True;
901 Exit;
902 end;
904 if e_KeyPressed(IK_SPACE) or e_KeyPressed(VK_JUMP) or
905 e_KeyPressed(JOY0_ACTIVATE) or e_KeyPressed(JOY1_ACTIVATE) or e_KeyPressed(JOY2_ACTIVATE) or e_KeyPressed(JOY3_ACTIVATE) then
906 begin
907 if not slFetched then
908 begin
909 slWaitStr := _lc[I_NET_SLIST_WAIT];
911 g_Game_Draw;
912 sys_Repaint;
914 if g_Net_Slist_Fetch(SL) then
915 begin
916 if SL = nil then
917 slWaitStr := _lc[I_NET_SLIST_NOSERVERS];
918 end
919 else
920 if SL = nil then
921 slWaitStr := _lc[I_NET_SLIST_ERROR];
922 slFetched := True;
923 slSelection := 0;
924 g_Serverlist_GenerateTable(SL, ST);
925 end;
926 end
927 else
928 slFetched := False;
930 if SL = nil then Exit;
932 if e_KeyPressed(IK_RETURN) or e_KeyPressed(IK_KPRETURN) or e_KeyPressed(VK_FIRE) or e_KeyPressed(VK_OPEN) or
933 e_KeyPressed(JOY0_ATTACK) or e_KeyPressed(JOY1_ATTACK) or e_KeyPressed(JOY2_ATTACK) or e_KeyPressed(JOY3_ATTACK) then
934 begin
935 if not slReturnPressed then
936 begin
937 Srv := GetServerFromTable(slSelection, SL, ST);
938 if Srv.Password then
939 begin
940 PromptIP := Srv.IP;
941 PromptPort := Srv.Port;
942 gState := STATE_MENU;
943 g_GUI_ShowWindow('ClientPasswordMenu');
944 SL := nil;
945 ST := nil;
946 slReturnPressed := True;
947 Exit;
948 end
949 else
950 g_Game_StartClient(Srv.IP, Srv.Port, '');
951 SL := nil;
952 ST := nil;
953 slReturnPressed := True;
954 Exit;
955 end;
956 end
957 else
958 slReturnPressed := False;
960 if e_KeyPressed(IK_DOWN) or e_KeyPressed(IK_KPDOWN) or e_KeyPressed(VK_DOWN) or
961 e_KeyPressed(JOY0_DOWN) or e_KeyPressed(JOY1_DOWN) or e_KeyPressed(JOY2_DOWN) or e_KeyPressed(JOY3_DOWN) then
962 begin
963 if not slDirPressed then
964 begin
965 Inc(slSelection);
966 if slSelection > High(ST) then slSelection := 0;
967 slDirPressed := True;
968 end;
969 end;
971 if e_KeyPressed(IK_UP) or e_KeyPressed(IK_KPUP) or e_KeyPressed(VK_UP) or
972 e_KeyPressed(JOY0_UP) or e_KeyPressed(JOY1_UP) or e_KeyPressed(JOY2_UP) or e_KeyPressed(JOY3_UP) then
973 begin
974 if not slDirPressed then
975 begin
976 if slSelection = 0 then slSelection := Length(ST);
977 Dec(slSelection);
979 slDirPressed := True;
980 end;
981 end;
983 if e_KeyPressed(IK_RIGHT) or e_KeyPressed(IK_KPRIGHT) or e_KeyPressed(VK_RIGHT) or
984 e_KeyPressed(JOY0_RIGHT) or e_KeyPressed(JOY1_RIGHT) or e_KeyPressed(JOY2_RIGHT) or e_KeyPressed(JOY3_RIGHT) then
985 begin
986 if not slDirPressed then
987 begin
988 Inc(ST[slSelection].Current);
989 if ST[slSelection].Current > High(ST[slSelection].Indices) then ST[slSelection].Current := 0;
990 slDirPressed := True;
991 end;
992 end;
994 if e_KeyPressed(IK_LEFT) or e_KeyPressed(IK_KPLEFT) or e_KeyPressed(VK_LEFT) or
995 e_KeyPressed(JOY0_LEFT) or e_KeyPressed(JOY1_LEFT) or e_KeyPressed(JOY2_LEFT) or e_KeyPressed(JOY3_LEFT) then
996 begin
997 if not slDirPressed then
998 begin
999 if ST[slSelection].Current = 0 then ST[slSelection].Current := Length(ST[slSelection].Indices);
1000 Dec(ST[slSelection].Current);
1002 slDirPressed := True;
1003 end;
1004 end;
1006 if (not e_KeyPressed(IK_DOWN)) and
1007 (not e_KeyPressed(IK_UP)) and
1008 (not e_KeyPressed(IK_RIGHT)) and
1009 (not e_KeyPressed(IK_LEFT)) and
1010 (not e_KeyPressed(IK_KPDOWN)) and
1011 (not e_KeyPressed(IK_KPUP)) and
1012 (not e_KeyPressed(IK_KPRIGHT)) and
1013 (not e_KeyPressed(IK_KPLEFT)) and
1014 (not e_KeyPressed(VK_DOWN)) and
1015 (not e_KeyPressed(VK_UP)) and
1016 (not e_KeyPressed(VK_RIGHT)) and
1017 (not e_KeyPressed(VK_LEFT)) and
1018 (not e_KeyPressed(JOY0_UP)) and (not e_KeyPressed(JOY1_UP)) and (not e_KeyPressed(JOY2_UP)) and (not e_KeyPressed(JOY3_UP)) and
1019 (not e_KeyPressed(JOY0_DOWN)) and (not e_KeyPressed(JOY1_DOWN)) and (not e_KeyPressed(JOY2_DOWN)) and (not e_KeyPressed(JOY3_DOWN)) and
1020 (not e_KeyPressed(JOY0_LEFT)) and (not e_KeyPressed(JOY1_LEFT)) and (not e_KeyPressed(JOY2_LEFT)) and (not e_KeyPressed(JOY3_LEFT)) and
1021 (not e_KeyPressed(JOY0_RIGHT)) and (not e_KeyPressed(JOY1_RIGHT)) and (not e_KeyPressed(JOY2_RIGHT)) and (not e_KeyPressed(JOY3_RIGHT))
1022 then
1023 slDirPressed := False;
1024 end;
1026 end.